| 63 | |
| 64 | |
| 65 | static bool ExtractFatArchForCPU(BinaryView* data, fat_arch_64& arch, cpu_type_t cputype, cpu_subtype_t cpusubtype) |
| 66 | { |
| 67 | DataBuffer sig = data->ReadBuffer(0, 4); |
| 68 | if (sig.GetLength() != 4) |
| 69 | return false; |
| 70 | |
| 71 | uint32_t magic = ToBE32(*(uint32_t*)sig.GetData()); |
| 72 | if ((magic != FAT_MAGIC) && (magic != FAT_MAGIC_64)) |
| 73 | return false; |
| 74 | |
| 75 | bool fat64 = (magic == FAT_MAGIC_64); |
| 76 | |
| 77 | fat_header header; |
| 78 | BinaryReader reader(data); |
| 79 | |
| 80 | // According to docs, Fat files are always BigEndian |
| 81 | reader.SetEndianness(BigEndian); |
| 82 | header.magic = reader.Read32(); |
| 83 | header.nfat_arch = reader.Read32(); |
| 84 | |
| 85 | // Malformed header, too many archs |
| 86 | size_t expectSize = header.nfat_arch * (fat64 ? 32 /* sizeof(fat_arch_64) */ : 20 /* sizeof(fat_arch) */) + 8 /* sizeof(fat_header) */; |
| 87 | if (expectSize > data->GetLength()) |
| 88 | { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | // Because Mach-O files can "hide" extra archs after the end for weird legacy reasons, |
| 93 | // we shouldn't assume the header has the right number of archs |
| 94 | while (true) |
| 95 | { |
| 96 | // Just use a fat_arch_64 struct since we read manually and it can hold both sizes |
| 97 | fat_arch_64 iarch; |
| 98 | try |
| 99 | { |
| 100 | if (fat64) |
| 101 | { |
| 102 | iarch.cputype = reader.Read32(); |
| 103 | iarch.cpusubtype = reader.Read32(); |
| 104 | iarch.offset = reader.Read64(); |
| 105 | iarch.size = reader.Read64(); |
| 106 | iarch.align = reader.Read32(); |
| 107 | iarch.reserved = reader.Read32(); |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | iarch.cputype = reader.Read32(); |
| 112 | iarch.cpusubtype = reader.Read32(); |
| 113 | iarch.offset = reader.Read32(); |
| 114 | iarch.size = reader.Read32(); |
| 115 | iarch.align = reader.Read32(); |
| 116 | } |
| 117 | } |
| 118 | catch (ReadException &) |
| 119 | { |
| 120 | return false; |
| 121 | } |
| 122 |
no test coverage detected