| ... | @@ -24,20 +24,18 @@ pub fn parse(self: *Archive, macho_file: *MachO, path: []const u8, handle_index: | ... | @@ -24,20 +24,18 @@ pub fn parse(self: *Archive, macho_file: *MachO, path: []const u8, handle_index: |
| 24 | const handle = macho_file.getFileHandle(handle_index); | 24 | const handle = macho_file.getFileHandle(handle_index); |
| 25 | const offset = if (fat_arch) |ar| ar.offset else 0; | 25 | const offset = if (fat_arch) |ar| ar.offset else 0; |
| 26 | const size = if (fat_arch) |ar| ar.size else (try handle.stat()).size; | 26 | const size = if (fat_arch) |ar| ar.size else (try handle.stat()).size; |
| 27 | try handle.seekTo(offset); | | |
| 28 | | 27 | |
| 29 | const reader = handle.reader(); | 28 | var pos: usize = offset + SARMAG; |
| 30 | _ = try reader.readBytesNoEof(SARMAG); | | |
| 31 | | | |
| 32 | var pos: usize = SARMAG; | | |
| 33 | while (true) { | 29 | while (true) { |
| 34 | if (pos >= size) break; | 30 | if (pos >= size) break; |
| 35 | if (!mem.isAligned(pos, 2)) { | 31 | if (!mem.isAligned(pos, 2)) pos += 1; |
| 36 | try handle.seekBy(1); | | |
| 37 | pos += 1; | | |
| 38 | } | | |
| 39 | | 32 | |
| 40 | const hdr = try reader.readStruct(ar_hdr); | 33 | var hdr_buffer: [@sizeOf(ar_hdr)]u8 = undefined; |
| | 34 | { |
| | 35 | const amt = try handle.preadAll(&hdr_buffer, pos); |
| | 36 | if (amt != @sizeOf(ar_hdr)) return error.InputOutput; |
| | 37 | } |
| | 38 | const hdr = @as(*align(1) const ar_hdr, @ptrCast(&hdr_buffer)).*; |
| 41 | pos += @sizeOf(ar_hdr); | 39 | pos += @sizeOf(ar_hdr); |
| 42 | | 40 | |
| 43 | if (!mem.eql(u8, &hdr.ar_fmag, ARFMAG)) { | 41 | if (!mem.eql(u8, &hdr.ar_fmag, ARFMAG)) { |
| ... | @@ -53,17 +51,15 @@ pub fn parse(self: *Archive, macho_file: *MachO, path: []const u8, handle_index: | ... | @@ -53,17 +51,15 @@ pub fn parse(self: *Archive, macho_file: *MachO, path: []const u8, handle_index: |
| 53 | if (try hdr.nameLength()) |len| { | 51 | if (try hdr.nameLength()) |len| { |
| 54 | hdr_size -= len; | 52 | hdr_size -= len; |
| 55 | const buf = try arena.allocator().alloc(u8, len); | 53 | const buf = try arena.allocator().alloc(u8, len); |
| 56 | try reader.readNoEof(buf); | 54 | const amt = try handle.preadAll(buf, pos); |
| | 55 | if (amt != len) return error.InputOutput; |
| 57 | pos += len; | 56 | pos += len; |
| 58 | const actual_len = mem.indexOfScalar(u8, buf, @as(u8, 0)) orelse len; | 57 | const actual_len = mem.indexOfScalar(u8, buf, @as(u8, 0)) orelse len; |
| 59 | break :name buf[0..actual_len]; | 58 | break :name buf[0..actual_len]; |
| 60 | } | 59 | } |
| 61 | unreachable; | 60 | unreachable; |
| 62 | }; | 61 | }; |
| 63 | defer { | 62 | defer pos += hdr_size; |
| 64 | _ = handle.seekBy(hdr_size) catch {}; | | |
| 65 | pos += hdr_size; | | |
| 66 | } | | |
| 67 | | 63 | |
| 68 | if (mem.eql(u8, name, SYMDEF) or | 64 | if (mem.eql(u8, name, SYMDEF) or |
| 69 | mem.eql(u8, name, SYMDEF64) or | 65 | mem.eql(u8, name, SYMDEF64) or |
| ... | @@ -73,7 +69,7 @@ pub fn parse(self: *Archive, macho_file: *MachO, path: []const u8, handle_index: | ... | @@ -73,7 +69,7 @@ pub fn parse(self: *Archive, macho_file: *MachO, path: []const u8, handle_index: |
| 73 | const object = Object{ | 69 | const object = Object{ |
| 74 | .archive = .{ | 70 | .archive = .{ |
| 75 | .path = try gpa.dupe(u8, path), | 71 | .path = try gpa.dupe(u8, path), |
| 76 | .offset = offset + pos, | 72 | .offset = pos, |
| 77 | }, | 73 | }, |
| 78 | .path = try gpa.dupe(u8, name), | 74 | .path = try gpa.dupe(u8, name), |
| 79 | .file_handle = handle_index, | 75 | .file_handle = handle_index, |