mirror of
https://github.com/morluto/rea.git
synced 2026-11-21 06:04:03 +00:00
* feat(artifacts): inspect NES 2.0 container headers and regions Problem: the iNES lane refused every header whose byte 7 bits 2-3 select NES 2.0, so NES 2.0 cartridges could not be inventoried, extracted or reconstructed even though their file layout extends classic iNES. Fix: the single iNES-family header parser now reads NES 2.0 alongside classic iNES. It reports the 12-bit mapper, submapper, 12-bit and exponent-multiplier PRG/CHR sizes, declared PRG/CHR RAM and NVRAM sizes, CPU/PPU timing, console type with Vs. System and extended console codes, miscellaneous ROM count and default expansion device in a "nes-2.0" header variant; classic headers keep their exact serialization. Regions add one prg.bin/chr.bin for exponent sizes and misc-rom.bin for the bytes after CHR-ROM when miscellaneous ROMs are declared. Oversized declarations (with exact totals, including exponent sizes beyond safe integers), nonzero unused header bits, empty PRG-ROM and declared but absent miscellaneous ROM bytes are refused through the existing format error. Nested byte classification now admits NES 2.0 through the same parser. The reader limitation, tool description, guide and skill reference now state that UNIF remains unsupported and that RAM, timing and console values are declarations. Validation: new synthetic NES 2.0 unit cases fail on the base (15 of 24) and pass after the change; focused boundary and acceptance iNES suites (75 tests) pass, including NES 2.0 inventory, extraction, reconstruction and compiled CLI inspection; npm run check:fast passes. Refs morluto/rea#2072 * test(artifacts): extract an exponent-sized NES 2.0 ROM area Review follow-up for NES 2.0 headers. The exponent-multiplier case stopped at parseInesLayout, and the extraction test used counted ROM units, so prg.bin and chr.bin from an exponent-sized area never went through the reader or extraction. A boundary case now builds a NES 2.0 file with a 2^10 * 3 byte PRG-ROM area beside one counted 8 KiB CHR-ROM unit. It checks the header omits the PRG unit count, the source offsets of prg.bin and chr/000.bin, the verified reconstruction order, and that the extracted regions match the original bytes exactly. Tests: tests/boundary/filesystem/inesArtifact.test.ts passes (38). * refactor(artifacts): read NES header bytes through one helper The NES 2.0 declaration added a sixth raw Buffer read to inesContainer.ts. On current main the architecture guard allows five there, so the merged tree failed Static checks. iNES and NES 2.0 declarations and the format probe now read header bytes through one headerByte helper. The file has one raw read, and its counted baseline drops from 5 to 1 through --shrink-baselines. Tests: inesContainer domain tests (24) and the NES boundary and acceptance tests (76) pass. architecture-guards, format:check and check:ci:fast pass on the branch merged with main. * refactor(artifacts): report iNES header failures through ParseFailure Review follow-up: the NES 2.0 declaration added plain-string failures. The iNES-family parser now returns Result<_, ParseFailure> with its kind and where it was found: - malformed: a missing signature, a dirty legacy header, an empty PRG-ROM area, reserved NES 2.0 bits or an invalid file extent; - truncated: a short header, a short payload, or declared miscellaneous ROM with no bytes; - unsupported: UNIF or another header variant, with undecoded content. The detail text is unchanged. The format registry refuses with the detail, and the iNES reader and root classifier translate the failure at their boundary into the existing format ArtifactReaderFailure, with the ParseFailureError as its cause. Tests: the refusal table now asserts each failure's kind and format as well as its detail. iNES domain and recognition tests, NES boundary and acceptance tests (76), architecture-guards, format:check and check:ci:fast pass on the branch merged with main. * fix(artifacts): preserve NES header declarations and iNES evidence --------- Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
169 lines
6.2 KiB
TypeScript
169 lines
6.2 KiB
TypeScript
import type { ArtifactFormatId } from "./artifactGraph.js";
|
|
import { hasAndroidBootImageMagic } from "./androidBootImageFormat.js";
|
|
import { hasBinaryPlistSignature } from "./apple/appleArchiveFormat.js";
|
|
import { hasDfuSePrefix, isDfuSeFile } from "./firmware/dfuse.js";
|
|
import { ESP8266_REFUSAL, recognizeEspImage } from "./firmware/espImage.js";
|
|
import { hasFitImagesNode } from "./firmware/fitImage.js";
|
|
import { detectHexRecordEncoding } from "./firmware/hexRecords.js";
|
|
import { hasUf2Block } from "./firmware/uf2.js";
|
|
import { hasGodotPckHeader } from "./godotPckFormat.js";
|
|
import { nesContainerFormat, parseInesLayout } from "./inesContainer.js";
|
|
import { recognizeAndroidOtaPayload } from "./firmware/androidOtaPayload.js";
|
|
import { hasUImageMagic } from "./uimageFormat.js";
|
|
import { hasUnityFsHeader } from "./unityfsFormat.js";
|
|
import { hasMftFileRecordHeader } from "./windows/ntfsMft.js";
|
|
import { hasWxapkgEncryptedMagic, hasWxapkgHeader } from "./wxapkgFormat.js";
|
|
import { hasZipSignature } from "./zipPackageFormat.js";
|
|
|
|
/** Formats recognized from leading bytes before native executable headers. */
|
|
export type RecognizedArtifactFormat = Extract<
|
|
ArtifactFormatId,
|
|
| "ines"
|
|
| "zip"
|
|
| "godot-pck"
|
|
| "unityfs"
|
|
| "wxapkg"
|
|
| "uimage"
|
|
| "android-boot-image"
|
|
| "android-ota-payload"
|
|
| "intel-hex"
|
|
| "srec"
|
|
| "uf2"
|
|
| "dfuse"
|
|
| "esp-flash-image"
|
|
| "esp-image"
|
|
| "fit"
|
|
| "ntfs-mft"
|
|
| "plist"
|
|
>;
|
|
|
|
/** The bytes one route has read when it asks which format a file holds. */
|
|
export interface ArtifactRecognitionInput {
|
|
/** Leading bytes; routes read different prefix lengths. */
|
|
readonly prefix: Buffer;
|
|
readonly fileSize: number;
|
|
/** The final DFU suffix bytes, when the route has read them. */
|
|
readonly suffix?: Uint8Array | undefined;
|
|
/** Whether an ESP flash partition layout was located, when the route looked. */
|
|
readonly espFlashLayout?: boolean | undefined;
|
|
}
|
|
|
|
/**
|
|
* What one recognizer established:
|
|
*
|
|
* - `recognized`: the bytes are this format.
|
|
* - `refused`: the header names this format, but this file is one REA does
|
|
* not read as it (an unsupported variant or an extent that does not fit).
|
|
* - `needs-input`: deciding needs bytes the route has not read yet.
|
|
*
|
|
* `index` is the recognizer's position, so a route can supply the missing
|
|
* input or skip a refusal and resume from there.
|
|
*/
|
|
type Recognition =
|
|
| {
|
|
readonly state: "recognized" | "needs-input";
|
|
readonly format: RecognizedArtifactFormat;
|
|
}
|
|
| {
|
|
readonly state: "refused";
|
|
readonly format: RecognizedArtifactFormat;
|
|
readonly reason: string;
|
|
};
|
|
|
|
export type ArtifactFormatRecognition = Recognition & {
|
|
readonly index: number;
|
|
};
|
|
|
|
/** `undefined` when the bytes do not claim this recognizer's format. */
|
|
type ArtifactFormatRecognizer = (
|
|
input: ArtifactRecognitionInput,
|
|
) => Recognition | undefined;
|
|
|
|
const when =
|
|
(format: RecognizedArtifactFormat, claims: (prefix: Buffer) => boolean) =>
|
|
({ prefix }: ArtifactRecognitionInput): Recognition | undefined =>
|
|
claims(prefix) ? { state: "recognized", format } : undefined;
|
|
|
|
/**
|
|
* The one ordered recognizer table. Inventory classification and binary
|
|
* target resolution both walk it, so the same bytes reach the same verdict;
|
|
* each route decides only what a refusal or a missing input means for it.
|
|
*/
|
|
export const ARTIFACT_FORMAT_RECOGNIZERS: readonly ArtifactFormatRecognizer[] =
|
|
[
|
|
({ prefix, fileSize }) => {
|
|
if (nesContainerFormat(prefix) === undefined) return undefined;
|
|
const layout = parseInesLayout(prefix, fileSize);
|
|
return layout.ok
|
|
? { state: "recognized", format: "ines" }
|
|
: { state: "refused", format: "ines", reason: layout.error.detail };
|
|
},
|
|
when("zip", hasZipSignature),
|
|
when("godot-pck", hasGodotPckHeader),
|
|
when("unityfs", hasUnityFsHeader),
|
|
when(
|
|
"wxapkg",
|
|
(prefix) => hasWxapkgEncryptedMagic(prefix) || hasWxapkgHeader(prefix),
|
|
),
|
|
when("uimage", hasUImageMagic),
|
|
when("android-boot-image", hasAndroidBootImageMagic),
|
|
({ prefix }) => {
|
|
const ota = recognizeAndroidOtaPayload(prefix);
|
|
if (ota === undefined) return undefined;
|
|
return ota.state === "recognized"
|
|
? { state: "recognized", format: "android-ota-payload" }
|
|
: {
|
|
state: "refused",
|
|
format: "android-ota-payload",
|
|
reason: ota.reason,
|
|
};
|
|
},
|
|
({ prefix }) => {
|
|
const format = detectHexRecordEncoding(prefix);
|
|
return format === undefined ? undefined : { state: "recognized", format };
|
|
},
|
|
when("uf2", hasUf2Block),
|
|
({ prefix, suffix, fileSize }) => {
|
|
if (!hasDfuSePrefix(prefix)) return undefined;
|
|
if (suffix === undefined)
|
|
return { state: "needs-input", format: "dfuse" };
|
|
return isDfuSeFile(prefix, suffix, fileSize)
|
|
? { state: "recognized", format: "dfuse" }
|
|
: undefined;
|
|
},
|
|
// A C3 or S3 flash dump begins with its bootloader image, so the flash
|
|
// layout is decided before the image header.
|
|
({ espFlashLayout }) =>
|
|
espFlashLayout === undefined
|
|
? { state: "needs-input", format: "esp-flash-image" }
|
|
: espFlashLayout
|
|
? { state: "recognized", format: "esp-flash-image" }
|
|
: undefined,
|
|
({ prefix, fileSize }) => {
|
|
const esp = recognizeEspImage(prefix, fileSize);
|
|
if (esp === undefined) return undefined;
|
|
return esp === "esp-image"
|
|
? { state: "recognized", format: "esp-image" }
|
|
: { state: "refused", format: "esp-image", reason: ESP8266_REFUSAL };
|
|
},
|
|
when("fit", hasFitImagesNode),
|
|
when("ntfs-mft", hasMftFileRecordHeader),
|
|
// Binary plists, including keyed archives, often use other extensions.
|
|
when("plist", hasBinaryPlistSignature),
|
|
];
|
|
|
|
/**
|
|
* The first recognizer at or after `from` whose format the bytes claim, or
|
|
* `undefined` when none does.
|
|
*/
|
|
export const recognizeArtifactFormat = (
|
|
input: ArtifactRecognitionInput,
|
|
from = 0,
|
|
): ArtifactFormatRecognition | undefined => {
|
|
for (let index = from; index < ARTIFACT_FORMAT_RECOGNIZERS.length; index++) {
|
|
const recognition = ARTIFACT_FORMAT_RECOGNIZERS[index]!(input);
|
|
if (recognition !== undefined) return { ...recognition, index };
|
|
}
|
|
return undefined;
|
|
};
|