| ... | @@ -8,11 +8,39 @@ const mem = std.mem; | ... | @@ -8,11 +8,39 @@ const mem = std.mem; |
| 8 | const testing = std.testing; | 8 | const testing = std.testing; |
| 9 | const Allocator = mem.Allocator; | 9 | const Allocator = mem.Allocator; |
| 10 | | 10 | |
| 11 | // pub const Blob = union(enum) { | 11 | const Blob = struct { |
| 12 | // Signature: struct{ | 12 | inner: macho.CodeDirectory, |
| 13 | // inner: | 13 | data: std.ArrayListUnmanaged(u8) = .{}, |
| 14 | // } | 14 | |
| 15 | // }; | 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 | alloc: *Allocator, | 45 | alloc: *Allocator, |
| 18 | inner: macho.SuperBlob = .{ | 46 | inner: macho.SuperBlob = .{ |
| ... | @@ -20,16 +48,44 @@ inner: macho.SuperBlob = .{ | ... | @@ -20,16 +48,44 @@ inner: macho.SuperBlob = .{ |
| 20 | .length = @sizeOf(macho.SuperBlob), | 48 | .length = @sizeOf(macho.SuperBlob), |
| 21 | .count = 0, | 49 | .count = 0, |
| 22 | }, | 50 | }, |
| 23 | // blobs: std.ArrayList(Blob), | 51 | blob: ?Blob = null, |
| 24 | | 52 | |
| 25 | pub fn init(alloc: *Allocator) CodeSignature { | 53 | pub fn init(alloc: *Allocator) CodeSignature { |
| 26 | return .{ | 54 | return .{ |
| 27 | .alloc = alloc, | 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 | pub fn size(self: CodeSignature) u32 { | 90 | pub fn size(self: CodeSignature) u32 { |
| 35 | return self.inner.length; | 91 | return self.inner.length; |
| ... | @@ -38,9 +94,16 @@ pub fn size(self: CodeSignature) u32 { | ... | @@ -38,9 +94,16 @@ pub fn size(self: CodeSignature) u32 { |
| 38 | pub fn write(self: CodeSignature, buffer: []u8) void { | 94 | pub fn write(self: CodeSignature, buffer: []u8) void { |
| 39 | assert(buffer.len >= self.inner.length); | 95 | assert(buffer.len >= self.inner.length); |
| 40 | self.writeHeader(buffer); | 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 | fn writeHeader(self: CodeSignature, buffer: []u8) void { | 108 | fn writeHeader(self: CodeSignature, buffer: []u8) void { |
| 46 | assert(buffer.len >= @sizeOf(macho.SuperBlob)); | 109 | assert(buffer.len >= @sizeOf(macho.SuperBlob)); |
| ... | @@ -49,11 +112,19 @@ fn writeHeader(self: CodeSignature, buffer: []u8) void { | ... | @@ -49,11 +112,19 @@ fn writeHeader(self: CodeSignature, buffer: []u8) void { |
| 49 | mem.writeIntBig(u32, buffer[8..12], self.inner.count); | 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 | test "CodeSignature header" { | 121 | test "CodeSignature header" { |
| 53 | var code_sig = CodeSignature.init(testing.allocator); | 122 | var code_sig = CodeSignature.init(testing.allocator); |
| 54 | defer code_sig.deinit(); | 123 | defer code_sig.deinit(); |
| | 124 | |
| 55 | var buffer: [@sizeOf(macho.SuperBlob)]u8 = undefined; | 125 | var buffer: [@sizeOf(macho.SuperBlob)]u8 = undefined; |
| 56 | code_sig.writeHeader(buffer[0..]); | 126 | code_sig.writeHeader(buffer[0..]); |
| | 127 | |
| 57 | const expected = &[_]u8{ 0xfa, 0xde, 0x0c, 0xc0, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0 }; | 128 | const expected = &[_]u8{ 0xfa, 0xde, 0x0c, 0xc0, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0 }; |
| 58 | testing.expect(mem.eql(u8, expected[0..], buffer[0..])); | 129 | testing.expect(mem.eql(u8, expected[0..], buffer[0..])); |
| 59 | } | 130 | } |