refactor(dos): centralize MZ admission and load-image evidence

This commit is contained in:
morluto committed 2026-10-26 23:02:53 +08:00
1 parent aa6c1d49fd
commit a30ffd27a6
10 files changed
+491 -96

No files matched your search

+8 -5
View File
@@ -20,6 +20,7 @@ import { readElfFixedHeader } from "./elfFixedHeader.js";
import {
mzWindowsHeaderLayoutLimitation,
parseMzHeaderEvidence,
type MzHeaderEvidence,
type MzWindowsHeaderSignatureEvidence,
} from "./dosMz.js";
import { decodePeMachine, readPeOptionalHeader } from "./peMachine.js";
@@ -126,7 +127,10 @@ export const parseExecutableHeader = (
)
return parseElf(bytes, fileSize);
if (bytes.length >= 2 && bytes[0] === 0x4d && bytes[1] === 0x5a)
return parseMz(bytes, fileSize, mzWindowsHeaderSignature);
return parseMzExecutableMetadata(
bytes,
parseMzHeaderEvidence(bytes, fileSize, mzWindowsHeaderSignature),
);
if (bytes.length < 8) return err("truncated or unsupported binary header");
const magic = bytes.readUInt32BE(0);
if ([0xfeedface, 0xfeedfacf, 0xcefaedfe, 0xcffaedfe].includes(magic))
@@ -434,12 +438,11 @@ const parseElf = (
});
};
const parseMz = (
/** Select executable metadata from the reader's completed MZ observations. */
export const parseMzExecutableMetadata = (
bytes: Buffer,
fileSize: number,
signatureAtOffset?: MzWindowsHeaderSignatureEvidence,
evidence: MzHeaderEvidence,
): Result<ExecutableMetadata, string> => {
const evidence = parseMzHeaderEvidence(bytes, fileSize, signatureAtOffset);
const { windowsHeader } = evidence;
if (
windowsHeader.state === "header-needed" ||