@@ -35,8 +35,10 @@ export const decodeAppleDispatchMetadata = (
|
||||
provenance: { path: string; sha256: string },
|
||||
architecture = "arm64",
|
||||
): ObjcSwiftMetadata => {
|
||||
if (bytes.length < 32) throw new RangeError("Truncated Mach-O header");
|
||||
if (bytes.length < 4) throw new RangeError("Truncated Mach-O header");
|
||||
const { slice, sliceEnd } = selectMachoSlice(bytes, architecture);
|
||||
if (slice === 0 && sliceEnd === bytes.length && bytes.length < 32)
|
||||
throw new RangeError("Truncated Mach-O header");
|
||||
if (slice + 32 > sliceEnd || bytes.readUInt32LE(slice) !== 0xfeedfacf)
|
||||
throw new TypeError(
|
||||
"Only little-endian 64-bit Mach-O metadata is supported",
|
||||
@@ -658,32 +660,45 @@ export const inspectAppleDispatchMetadata = async (
|
||||
|
||||
const selectMachoSlice = (bytes: Buffer, architecture: string) => {
|
||||
const magic = bytes.readUInt32BE(0);
|
||||
if (magic !== 0xcafebabe && magic !== 0xcafebabf)
|
||||
const littleEndian = magic === 0xbebafeca || magic === 0xbfbafeca;
|
||||
if (magic !== 0xcafebabe && magic !== 0xcafebabf && !littleEndian)
|
||||
return { slice: 0, sliceEnd: bytes.length };
|
||||
const fat64 = magic === 0xcafebabf;
|
||||
const fat64 = magic === 0xcafebabf || magic === 0xbfbafeca;
|
||||
const stride = fat64 ? 32 : 20;
|
||||
const count = bytes.readUInt32BE(4);
|
||||
const readUInt32 = littleEndian
|
||||
? (offset: number) => bytes.readUInt32LE(offset)
|
||||
: (offset: number) => bytes.readUInt32BE(offset);
|
||||
const readUInt64 = littleEndian
|
||||
? (offset: number) => bytes.readBigUInt64LE(offset)
|
||||
: (offset: number) => bytes.readBigUInt64BE(offset);
|
||||
const byteOrder = littleEndian ? "little-endian" : "big-endian";
|
||||
const malformed = (reason: string) =>
|
||||
new RangeError(`${reason} (FAT header byte order: ${byteOrder})`);
|
||||
const invalid = (reason: string) =>
|
||||
new TypeError(`${reason} (FAT header byte order: ${byteOrder})`);
|
||||
if (bytes.length < 8) throw malformed("Malformed FAT architecture table");
|
||||
const count = readUInt32(4);
|
||||
const headerEnd = 8 + count * stride;
|
||||
if (count > 128 || headerEnd > bytes.length)
|
||||
throw new RangeError("Malformed FAT architecture table");
|
||||
throw malformed("Malformed FAT architecture table");
|
||||
const cpu = architecture === "arm64" ? 0x0100000c : 0x01000007;
|
||||
let selected: { slice: number; sliceEnd: number } | undefined;
|
||||
for (let index = 0; index < count; index++) {
|
||||
const offset = 8 + index * stride;
|
||||
if (bytes.readUInt32BE(offset) !== cpu) continue;
|
||||
if (readUInt32(offset) !== cpu) continue;
|
||||
if (selected !== undefined)
|
||||
throw new TypeError("Ambiguous FAT architecture slice");
|
||||
throw invalid("Ambiguous FAT architecture slice");
|
||||
const start = fat64
|
||||
? bytes.readBigUInt64BE(offset + 8)
|
||||
: BigInt(bytes.readUInt32BE(offset + 8));
|
||||
? readUInt64(offset + 8)
|
||||
: BigInt(readUInt32(offset + 8));
|
||||
const size = fat64
|
||||
? bytes.readBigUInt64BE(offset + 16)
|
||||
: BigInt(bytes.readUInt32BE(offset + 12));
|
||||
? readUInt64(offset + 16)
|
||||
: BigInt(readUInt32(offset + 12));
|
||||
if (start < BigInt(headerEnd) || start + size > BigInt(bytes.length))
|
||||
throw new RangeError("FAT slice exceeds file");
|
||||
throw malformed("FAT slice exceeds file");
|
||||
selected = { slice: Number(start), sliceEnd: Number(start + size) };
|
||||
}
|
||||
if (selected === undefined)
|
||||
throw new TypeError("Requested FAT architecture is absent");
|
||||
throw invalid("Requested FAT architecture is absent");
|
||||
return selected;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user