diff --git a/src/domain/inesContainer.ts b/src/domain/inesContainer.ts index 07c26b9d475..a45aef79462 100644 --- a/src/domain/inesContainer.ts +++ b/src/domain/inesContainer.ts @@ -6,25 +6,28 @@ import { err, ok, type Result } from "./result.js"; type FailureSite = { readonly offset?: number; readonly field?: string }; /** iNES-family failures in the shared parser algebra, with where they were found. */ -const malformed = (detail: string, at: FailureSite = {}): ParseFailure => ({ - kind: "malformed", - format: "ines", - detail, - ...at, -}); -const truncated = (detail: string, at: FailureSite = {}): ParseFailure => ({ - kind: "truncated", - format: "ines", - detail, - ...at, -}); -const unsupported = (detail: string, at: FailureSite = {}): ParseFailure => ({ - kind: "unsupported", - format: "ines", - detail, - undecodedContent: true, - ...at, -}); +const malformed = (detail: string, at: FailureSite = {}) => + err({ + kind: "malformed", + format: "ines", + detail, + ...at, + }); +const truncated = (detail: string, at: FailureSite = {}) => + err({ + kind: "truncated", + format: "ines", + detail, + ...at, + }); +const unsupported = (detail: string, at: FailureSite = {}) => + err({ + kind: "unsupported", + format: "ines", + detail, + undecodedContent: true, + ...at, + }); /** Byte signatures select a container family, never an executable architecture. */ export const nesContainerFormat = ( @@ -182,28 +185,22 @@ const classicDeclaration = ( const flags6 = reader.u8(6); const flags7 = reader.u8(7); if ((flags7 & 0x0c) !== 0) - return err( - unsupported("unsupported iNES header variant", { - offset: 7, - field: "flags 7", - }), - ); + return unsupported("unsupported iNES header variant", { + offset: 7, + field: "flags 7", + }); // Dirty legacy headers make the upper mapper bits ambiguous. Do not repair // them or claim a mapper that the observed header cannot establish. if (reader.range(12, 4, "reserved header bytes").some((byte) => byte !== 0)) - return err( - malformed( - "ambiguous iNES mapper: reserved header bytes 12..15 are nonzero", - { offset: 12, field: "bytes 12..15" }, - ), + return malformed( + "ambiguous iNES mapper: reserved header bytes 12..15 are nonzero", + { offset: 12, field: "bytes 12..15" }, ); if (prgUnits === 0) - return err( - malformed("iNES requires at least one PRG-ROM unit", { - offset: 4, - field: "PRG-ROM size", - }), - ); + return malformed("iNES requires at least one PRG-ROM unit", { + offset: 4, + field: "PRG-ROM size", + }); return ok({ prg: { bytes: prgUnits * 16_384, unitBytes: 16_384 }, chr: { bytes: chrUnits * 8_192, unitBytes: 8_192 }, @@ -250,21 +247,20 @@ const nes2Declaration = ( [15, 0x80], ] as const) if ((byte(index) & reserved) !== 0) - return err( - malformed(`reserved NES 2.0 header bits are nonzero in byte ${index}`, { + return malformed( + `reserved NES 2.0 header bits are nonzero in byte ${index}`, + { offset: index, field: `byte ${index}`, - }), + }, ); const prg = nes2RomArea(byte(4), byte(9) & 0x0f, 16_384); const chr = nes2RomArea(byte(5), byte(9) >>> 4, 8_192); if (prg.bytes === 0) - return err( - malformed("NES 2.0 requires at least one PRG-ROM unit", { - offset: 4, - field: "PRG-ROM size", - }), - ); + return malformed("NES 2.0 requires at least one PRG-ROM unit", { + offset: 4, + field: "PRG-ROM size", + }); return ok({ prg, chr, @@ -317,34 +313,26 @@ export const parseInesLayout = ( ): Result => { const format = nesContainerFormat(prefix); if (format === undefined) - return err( - malformed("missing iNES byte signature", { - offset: 0, - field: "signature", - }), - ); + return malformed("missing iNES byte signature", { + offset: 0, + field: "signature", + }); if (format === "unif") - return err( - unsupported("unif containers are not supported by the iNES lane", { - offset: 0, - field: "signature", - }), - ); + return unsupported("unif containers are not supported by the iNES lane", { + offset: 0, + field: "signature", + }); const reader = new BinaryReader(prefix, true, "ines"); if (!Number.isSafeInteger(fileSize) || fileSize < 0) - return err( - malformed( - "invalid iNES file extent: expected a nonnegative safe integer", - { field: "file extent" }, - ), + return malformed( + "invalid iNES file extent: expected a nonnegative safe integer", + { field: "file extent" }, ); if (fileSize < 16 || prefix.length < 16) - return err( - truncated("truncated iNES header: expected 16 bytes", { - offset: 0, - field: "header", - }), - ); + return truncated("truncated iNES header: expected 16 bytes", { + offset: 0, + field: "header", + }); const declared = format === "ines" ? classicDeclaration(reader) : nes2Declaration(reader); if (!declared.ok) return declared; @@ -356,11 +344,9 @@ export const parseInesLayout = ( const declaredEnd = BigInt(16 + (trainer ? 512 : 0)) + BigInt(prg.bytes) + BigInt(chr.bytes); if (declaredEnd > BigInt(fileSize)) - return err( - truncated( - `truncated ${label} payload: header declares ${declaredEnd} bytes; file has ${fileSize}`, - { field: "payload" }, - ), + return truncated( + `truncated ${label} payload: header declares ${declaredEnd} bytes; file has ${fileSize}`, + { field: "payload" }, ); const regions: InesRegion[] = []; let offset = 0; @@ -391,11 +377,9 @@ export const parseInesLayout = ( fileSize - offset, ); else if (miscRomCount > 0) - return err( - truncated( - `${label} declares ${miscRomCount} miscellaneous ROM${miscRomCount === 1 ? "" : "s"} but no bytes follow CHR-ROM`, - { offset, field: "miscellaneous ROM" }, - ), + return truncated( + `${label} declares ${miscRomCount} miscellaneous ROM${miscRomCount === 1 ? "" : "s"} but no bytes follow CHR-ROM`, + { offset, field: "miscellaneous ROM" }, ); return ok({ header, regions }); };