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