| ... | ... | @@ -8,11 +8,39 @@ const mem = std.mem; |
| 8 | 8 | const testing = std.testing; |
| 9 | 9 | const Allocator = mem.Allocator; |
| 10 | 10 | |
| 11 | | // pub const Blob = union(enum) { |
| 12 | | // Signature: struct{ |
| 13 | | // inner: |
| 14 | | // } |
| 15 | | // }; |
| 11 | const Blob = struct { |
| 12 | inner: macho.CodeDirectory, |
| 13 | data: std.ArrayListUnmanaged(u8) = .{}, |
| 14 | |
| 15 | fn size(self: Blob) u32 { |
| 16 | return self.inner.length; |
| 17 | } |
| 18 | |
| 19 | fn write(self: Blob, buffer: []u8) void { |
| 20 | assert(buffer.len >= self.inner.length); |
| 21 | mem.writeIntBig(u32, buffer[0..4], self.inner.magic); |
| 22 | mem.writeIntBig(u32, buffer[4..8], self.inner.length); |
| 23 | mem.writeIntBig(u32, buffer[8..12], self.inner.version); |
| 24 | mem.writeIntBig(u32, buffer[12..16], self.inner.flags); |
| 25 | mem.writeIntBig(u32, buffer[16..20], self.inner.hashOffset); |
| 26 | mem.writeIntBig(u32, buffer[20..24], self.inner.identOffset); |
| 27 | mem.writeIntBig(u32, buffer[24..28], self.inner.nSpecialSlots); |
| 28 | mem.writeIntBig(u32, buffer[28..32], self.inner.nCodeSlots); |
| 29 | mem.writeIntBig(u32, buffer[32..36], self.inner.codeLimit); |
| 30 | mem.writeIntBig(u8, buffer[36..37], self.inner.hashSize); |
| 31 | mem.writeIntBig(u8, buffer[37..38], self.inner.hashType); |
| 32 | mem.writeIntBig(u8, buffer[38..39], self.inner.platform); |
| 33 | mem.writeIntBig(u8, buffer[39..40], self.inner.pageSize); |
| 34 | mem.writeIntBig(u32, buffer[40..44], self.inner.spare2); |
| 35 | mem.writeIntBig(u32, buffer[44..48], self.inner.scatterOffset); |
| 36 | mem.writeIntBig(u32, buffer[48..52], self.inner.teamOffset); |
| 37 | mem.writeIntBig(u32, buffer[52..56], self.inner.spare3); |
| 38 | mem.writeIntBig(u64, buffer[56..64], self.inner.codeLimit64); |
| 39 | mem.writeIntBig(u64, buffer[64..72], self.inner.execSegBase); |
| 40 | mem.writeIntBig(u64, buffer[72..80], self.inner.execSegLimit); |
| 41 | mem.writeIntBig(u64, buffer[80..88], self.inner.execSegFlags); |
| 42 | } |
| 43 | }; |
| 16 | 44 | |
| 17 | 45 | alloc: *Allocator, |
| 18 | 46 | inner: macho.SuperBlob = .{ |
| ... | ... | @@ -20,16 +48,44 @@ inner: macho.SuperBlob = .{ |
| 20 | 48 | .length = @sizeOf(macho.SuperBlob), |
| 21 | 49 | .count = 0, |
| 22 | 50 | }, |
| 23 | | // blobs: std.ArrayList(Blob), |
| 51 | blob: ?Blob = null, |
| 24 | 52 | |
| 25 | 53 | pub fn init(alloc: *Allocator) CodeSignature { |
| 26 | 54 | return .{ |
| 27 | 55 | .alloc = alloc, |
| 28 | | // .indices = std.ArrayList(Blob).init(alloc), |
| 29 | 56 | }; |
| 30 | 57 | } |
| 31 | 58 | |
| 32 | | pub fn calcAdhocSignature(self: *CodeSignature) !void {} |
| 59 | pub fn calcAdhocSignature(self: *CodeSignature) !void { |
| 60 | var blob = Blob{ |
| 61 | .inner = .{ |
| 62 | .magic = macho.CSMAGIC_CODEDIRECTORY, |
| 63 | .length = @sizeOf(macho.CodeDirectory), |
| 64 | .version = 0x20400, |
| 65 | .flags = 0, |
| 66 | .hashOffset = 0, |
| 67 | .identOffset = 0, |
| 68 | .nSpecialSlots = 0, |
| 69 | .nCodeSlots = 0, |
| 70 | .codeLimit = 0, |
| 71 | .hashSize = 0, |
| 72 | .hashType = 0, |
| 73 | .platform = 0, |
| 74 | .pageSize = 0, |
| 75 | .spare2 = 0, |
| 76 | .scatterOffset = 0, |
| 77 | .teamOffset = 0, |
| 78 | .spare3 = 0, |
| 79 | .codeLimit64 = 0, |
| 80 | .execSegBase = 0, |
| 81 | .execSegLimit = 0, |
| 82 | .execSegFlags = 0, |
| 83 | }, |
| 84 | }; |
| 85 | self.inner.length += @sizeOf(macho.BlobIndex) + blob.size(); |
| 86 | self.inner.count = 1; |
| 87 | self.blob = blob; |
| 88 | } |
| 33 | 89 | |
| 34 | 90 | pub fn size(self: CodeSignature) u32 { |
| 35 | 91 | return self.inner.length; |
| ... | ... | @@ -38,9 +94,16 @@ pub fn size(self: CodeSignature) u32 { |
| 38 | 94 | pub fn write(self: CodeSignature, buffer: []u8) void { |
| 39 | 95 | assert(buffer.len >= self.inner.length); |
| 40 | 96 | self.writeHeader(buffer); |
| 97 | const offset: u32 = @sizeOf(macho.SuperBlob) + @sizeOf(macho.BlobIndex); |
| 98 | writeBlobIndex(macho.CSSLOT_CODEDIRECTORY, offset, buffer[@sizeOf(macho.SuperBlob)..]); |
| 99 | self.blob.?.write(buffer[offset..]); |
| 41 | 100 | } |
| 42 | 101 | |
| 43 | | pub fn deinit(self: *CodeSignature) void {} |
| 102 | pub fn deinit(self: *CodeSignature) void { |
| 103 | if (self.blob) |*b| { |
| 104 | b.data.deinit(self.alloc); |
| 105 | } |
| 106 | } |
| 44 | 107 | |
| 45 | 108 | fn writeHeader(self: CodeSignature, buffer: []u8) void { |
| 46 | 109 | assert(buffer.len >= @sizeOf(macho.SuperBlob)); |
| ... | ... | @@ -49,11 +112,19 @@ fn writeHeader(self: CodeSignature, buffer: []u8) void { |
| 49 | 112 | mem.writeIntBig(u32, buffer[8..12], self.inner.count); |
| 50 | 113 | } |
| 51 | 114 | |
| 115 | fn writeBlobIndex(tt: u32, offset: u32, buffer: []u8) void { |
| 116 | assert(buffer.len >= @sizeOf(macho.BlobIndex)); |
| 117 | mem.writeIntBig(u32, buffer[0..4], tt); |
| 118 | mem.writeIntBig(u32, buffer[4..8], offset); |
| 119 | } |
| 120 | |
| 52 | 121 | test "CodeSignature header" { |
| 53 | 122 | var code_sig = CodeSignature.init(testing.allocator); |
| 54 | 123 | defer code_sig.deinit(); |
| 124 | |
| 55 | 125 | var buffer: [@sizeOf(macho.SuperBlob)]u8 = undefined; |
| 56 | 126 | code_sig.writeHeader(buffer[0..]); |
| 127 | |
| 57 | 128 | const expected = &[_]u8{ 0xfa, 0xde, 0x0c, 0xc0, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0 }; |
| 58 | 129 | testing.expect(mem.eql(u8, expected[0..], buffer[0..])); |
| 59 | 130 | } |