| ... | @@ -46,8 +46,6 @@ const CodeDirectory = struct { | ... | @@ -46,8 +46,6 @@ const CodeDirectory = struct { |
| 46 | } | 46 | } |
| 47 | }; | 47 | }; |
| 48 | | 48 | |
| 49 | allocator: *Allocator, | | |
| 50 | | | |
| 51 | /// Code signature blob header. | 49 | /// Code signature blob header. |
| 52 | inner: macho.SuperBlob = .{ | 50 | inner: macho.SuperBlob = .{ |
| 53 | .magic = macho.CSMAGIC_EMBEDDED_SIGNATURE, | 51 | .magic = macho.CSMAGIC_EMBEDDED_SIGNATURE, |
| ... | @@ -58,24 +56,15 @@ inner: macho.SuperBlob = .{ | ... | @@ -58,24 +56,15 @@ inner: macho.SuperBlob = .{ |
| 58 | /// CodeDirectory header which holds the hash of the binary. | 56 | /// CodeDirectory header which holds the hash of the binary. |
| 59 | cdir: ?CodeDirectory = null, | 57 | cdir: ?CodeDirectory = null, |
| 60 | | 58 | |
| 61 | /// Page size is dependent on the target cpu architecture. | | |
| 62 | /// For x86_64 that's 4KB, whereas for aarch64, that's 16KB. | | |
| 63 | page_size: u16, | | |
| 64 | | | |
| 65 | pub fn init(allocator: *Allocator, page_size: u16) CodeSignature { | | |
| 66 | return .{ | | |
| 67 | .allocator = allocator, | | |
| 68 | .page_size = page_size, | | |
| 69 | }; | | |
| 70 | } | | |
| 71 | | | |
| 72 | pub fn calcAdhocSignature( | 59 | pub fn calcAdhocSignature( |
| 73 | self: *CodeSignature, | 60 | self: *CodeSignature, |
| | 61 | allocator: *Allocator, |
| 74 | file: fs.File, | 62 | file: fs.File, |
| 75 | id: []const u8, | 63 | id: []const u8, |
| 76 | text_segment: macho.segment_command_64, | 64 | text_segment: macho.segment_command_64, |
| 77 | code_sig_cmd: macho.linkedit_data_command, | 65 | code_sig_cmd: macho.linkedit_data_command, |
| 78 | output_mode: std.builtin.OutputMode, | 66 | output_mode: std.builtin.OutputMode, |
| | 67 | page_size: u16, |
| 79 | ) !void { | 68 | ) !void { |
| 80 | const execSegBase: u64 = text_segment.fileoff; | 69 | const execSegBase: u64 = text_segment.fileoff; |
| 81 | const execSegLimit: u64 = text_segment.filesize; | 70 | const execSegLimit: u64 = text_segment.filesize; |
| ... | @@ -95,7 +84,7 @@ pub fn calcAdhocSignature( | ... | @@ -95,7 +84,7 @@ pub fn calcAdhocSignature( |
| 95 | .hashSize = hash_size, | 84 | .hashSize = hash_size, |
| 96 | .hashType = macho.CS_HASHTYPE_SHA256, | 85 | .hashType = macho.CS_HASHTYPE_SHA256, |
| 97 | .platform = 0, | 86 | .platform = 0, |
| 98 | .pageSize = @truncate(u8, std.math.log2(self.page_size)), | 87 | .pageSize = @truncate(u8, std.math.log2(page_size)), |
| 99 | .spare2 = 0, | 88 | .spare2 = 0, |
| 100 | .scatterOffset = 0, | 89 | .scatterOffset = 0, |
| 101 | .teamOffset = 0, | 90 | .teamOffset = 0, |
| ... | @@ -107,13 +96,13 @@ pub fn calcAdhocSignature( | ... | @@ -107,13 +96,13 @@ pub fn calcAdhocSignature( |
| 107 | }, | 96 | }, |
| 108 | }; | 97 | }; |
| 109 | | 98 | |
| 110 | const total_pages = mem.alignForward(file_size, self.page_size) / self.page_size; | 99 | const total_pages = mem.alignForward(file_size, page_size) / page_size; |
| 111 | | 100 | |
| 112 | var hash: [hash_size]u8 = undefined; | 101 | var hash: [hash_size]u8 = undefined; |
| 113 | var buffer = try self.allocator.alloc(u8, self.page_size); | 102 | var buffer = try allocator.alloc(u8, page_size); |
| 114 | defer self.allocator.free(buffer); | 103 | defer allocator.free(buffer); |
| 115 | | 104 | |
| 116 | try cdir.data.ensureCapacity(self.allocator, total_pages * hash_size + id.len + 1); | 105 | try cdir.data.ensureCapacity(allocator, total_pages * hash_size + id.len + 1); |
| 117 | | 106 | |
| 118 | // 1. Save the identifier and update offsets | 107 | // 1. Save the identifier and update offsets |
| 119 | cdir.inner.identOffset = cdir.inner.length; | 108 | cdir.inner.identOffset = cdir.inner.length; |
| ... | @@ -126,8 +115,8 @@ pub fn calcAdhocSignature( | ... | @@ -126,8 +115,8 @@ pub fn calcAdhocSignature( |
| 126 | cdir.inner.hashOffset = cdir.inner.identOffset + @intCast(u32, id.len) + 1; | 115 | cdir.inner.hashOffset = cdir.inner.identOffset + @intCast(u32, id.len) + 1; |
| 127 | var i: usize = 0; | 116 | var i: usize = 0; |
| 128 | while (i < total_pages) : (i += 1) { | 117 | while (i < total_pages) : (i += 1) { |
| 129 | const fstart = i * self.page_size; | 118 | const fstart = i * page_size; |
| 130 | const fsize = if (fstart + self.page_size > file_size) file_size - fstart else self.page_size; | 119 | const fsize = if (fstart + page_size > file_size) file_size - fstart else page_size; |
| 131 | const len = try file.preadAll(buffer, fstart); | 120 | const len = try file.preadAll(buffer, fstart); |
| 132 | assert(fsize <= len); | 121 | assert(fsize <= len); |
| 133 | | 122 | |
| ... | @@ -156,9 +145,9 @@ pub fn write(self: CodeSignature, writer: anytype) !void { | ... | @@ -156,9 +145,9 @@ pub fn write(self: CodeSignature, writer: anytype) !void { |
| 156 | try self.cdir.?.write(writer); | 145 | try self.cdir.?.write(writer); |
| 157 | } | 146 | } |
| 158 | | 147 | |
| 159 | pub fn deinit(self: *CodeSignature) void { | 148 | pub fn deinit(self: *CodeSignature, allocator: *Allocator) void { |
| 160 | if (self.cdir) |*cdir| { | 149 | if (self.cdir) |*cdir| { |
| 161 | cdir.data.deinit(self.allocator); | 150 | cdir.data.deinit(allocator); |
| 162 | } | 151 | } |
| 163 | } | 152 | } |
| 164 | | 153 | |