| ... | ... | @@ -2,6 +2,7 @@ const std = @import("std"); |
| 2 | 2 | const fs = std.fs; |
| 3 | 3 | const io = std.io; |
| 4 | 4 | const mem = std.mem; |
| 5 | const meta = std.meta; |
| 5 | 6 | const macho = std.macho; |
| 6 | 7 | const testing = std.testing; |
| 7 | 8 | |
| ... | ... | @@ -25,8 +26,7 @@ pub const LoadCommand = union(enum) { |
| 25 | 26 | const header = try reader.readStruct(macho.load_command); |
| 26 | 27 | var buffer = try allocator.alloc(u8, header.cmdsize); |
| 27 | 28 | defer allocator.free(buffer); |
| 28 | | const slice = [1]macho.load_command{header}; |
| 29 | | mem.copy(u8, buffer[0..], mem.sliceAsBytes(slice[0..1])); |
| 29 | mem.copy(u8, buffer[0..], mem.asBytes(&header)); |
| 30 | 30 | try reader.readNoEof(buffer[@sizeOf(macho.load_command)..]); |
| 31 | 31 | var stream = io.fixedBufferStream(buffer[0..]); |
| 32 | 32 | |
| ... | ... | @@ -126,30 +126,25 @@ pub const LoadCommand = union(enum) { |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | fn writeStruct(command: anytype, writer: anytype) !void { |
| 129 | | const slice = [1]@TypeOf(command){command}; |
| 130 | | return writer.writeAll(mem.sliceAsBytes(slice[0..1])); |
| 129 | return writer.writeAll(mem.asBytes(&command)); |
| 131 | 130 | } |
| 132 | 131 | |
| 133 | 132 | fn eql(self: LoadCommand, other: LoadCommand) bool { |
| 134 | 133 | if (@as(@TagType(LoadCommand), self) != @as(@TagType(LoadCommand), other)) return false; |
| 135 | 134 | return switch (self) { |
| 136 | | .DyldInfoOnly => |x| eqlStruct(x, other.DyldInfoOnly), |
| 137 | | .Symtab => |x| eqlStruct(x, other.Symtab), |
| 138 | | .Dysymtab => |x| eqlStruct(x, other.Dysymtab), |
| 139 | | .Main => |x| eqlStruct(x, other.Main), |
| 140 | | .VersionMin => |x| eqlStruct(x, other.VersionMin), |
| 141 | | .SourceVersion => |x| eqlStruct(x, other.SourceVersion), |
| 142 | | .LinkeditData => |x| eqlStruct(x, other.LinkeditData), |
| 135 | .DyldInfoOnly => |x| meta.eql(x, other.DyldInfoOnly), |
| 136 | .Symtab => |x| meta.eql(x, other.Symtab), |
| 137 | .Dysymtab => |x| meta.eql(x, other.Dysymtab), |
| 138 | .Main => |x| meta.eql(x, other.Main), |
| 139 | .VersionMin => |x| meta.eql(x, other.VersionMin), |
| 140 | .SourceVersion => |x| meta.eql(x, other.SourceVersion), |
| 141 | .LinkeditData => |x| meta.eql(x, other.LinkeditData), |
| 143 | 142 | .Segment => |x| x.eql(other.Segment), |
| 144 | 143 | .Dylinker => |x| x.eql(other.Dylinker), |
| 145 | 144 | .Dylib => |x| x.eql(other.Dylib), |
| 146 | 145 | .Unknown => |x| x.eql(other.Unknown), |
| 147 | 146 | }; |
| 148 | 147 | } |
| 149 | | |
| 150 | | fn eqlStruct(lhs: anytype, rhs: anytype) bool { |
| 151 | | return mem.eql(u8, mem.asBytes(&lhs), mem.asBytes(&rhs)); |
| 152 | | } |
| 153 | 148 | }; |
| 154 | 149 | |
| 155 | 150 | pub const SegmentCommand = struct { |
| ... | ... | @@ -177,12 +172,9 @@ pub const SegmentCommand = struct { |
| 177 | 172 | } |
| 178 | 173 | |
| 179 | 174 | pub fn write(self: SegmentCommand, writer: anytype) !void { |
| 180 | | const cmd = [1]macho.segment_command_64{self.inner}; |
| 181 | | try writer.writeAll(mem.sliceAsBytes(cmd[0..1])); |
| 182 | | |
| 175 | try writer.writeAll(mem.asBytes(&self.inner)); |
| 183 | 176 | for (self.sections.items) |sect| { |
| 184 | | const section = [1]macho.section_64{sect}; |
| 185 | | try writer.writeAll(mem.sliceAsBytes(section[0..1])); |
| 177 | try writer.writeAll(mem.asBytes(&sect)); |
| 186 | 178 | } |
| 187 | 179 | } |
| 188 | 180 | |
| ... | ... | @@ -191,12 +183,12 @@ pub const SegmentCommand = struct { |
| 191 | 183 | } |
| 192 | 184 | |
| 193 | 185 | fn eql(self: SegmentCommand, other: SegmentCommand) bool { |
| 194 | | if (!mem.eql(u8, mem.asBytes(&self.inner), mem.asBytes(&other.inner))) return false; |
| 186 | if (!meta.eql(self.inner, other.inner)) return false; |
| 195 | 187 | const lhs = self.sections.items; |
| 196 | 188 | const rhs = other.sections.items; |
| 197 | 189 | var i: usize = 0; |
| 198 | 190 | while (i < self.inner.nsects) : (i += 1) { |
| 199 | | if (!mem.eql(u8, mem.asBytes(&lhs[i]), mem.asBytes(&rhs[i]))) return false; |
| 191 | if (!meta.eql(lhs[i], rhs[i])) return false; |
| 200 | 192 | } |
| 201 | 193 | return true; |
| 202 | 194 | } |
| ... | ... | @@ -226,8 +218,7 @@ pub fn GenericCommandWithData(comptime Cmd: type) type { |
| 226 | 218 | } |
| 227 | 219 | |
| 228 | 220 | pub fn write(self: Self, writer: anytype) !void { |
| 229 | | const cmd = [1]Cmd{self.inner}; |
| 230 | | try writer.writeAll(mem.sliceAsBytes(cmd[0..1])); |
| 221 | try writer.writeAll(mem.asBytes(&self.inner)); |
| 231 | 222 | try writer.writeAll(self.data); |
| 232 | 223 | } |
| 233 | 224 | |
| ... | ... | @@ -235,8 +226,8 @@ pub fn GenericCommandWithData(comptime Cmd: type) type { |
| 235 | 226 | allocator.free(self.data); |
| 236 | 227 | } |
| 237 | 228 | |
| 238 | | pub fn eql(self: Self, other: Self) bool { |
| 239 | | if (!mem.eql(u8, mem.asBytes(&self.inner), mem.asBytes(&other.inner))) return false; |
| 229 | fn eql(self: Self, other: Self) bool { |
| 230 | if (!meta.eql(self.inner, other.inner)) return false; |
| 240 | 231 | return mem.eql(u8, self.data, other.data); |
| 241 | 232 | } |
| 242 | 233 | }; |