diff --git a/scripts/verify/baselines/raw-buffer-read.json b/scripts/verify/baselines/raw-buffer-read.json index b1473377dd..1e4269dccf 100644 --- a/scripts/verify/baselines/raw-buffer-read.json +++ b/scripts/verify/baselines/raw-buffer-read.json @@ -3,7 +3,6 @@ "src/application/MipsElfMetadata.ts": 10, "src/application/MipsGnuAttributes.ts": 2, "src/artifacts/DesktopFileInspection.ts": 4, - "src/artifacts/GodotPckArtifactReader.ts": 6, "src/artifacts/UnityFsStructures.ts": 8, "src/artifacts/WxapkgStructures.ts": 3, "src/artifacts/apple/NibArchive.ts": 8, @@ -42,7 +41,6 @@ "src/domain/firmware/devicetreeParser.ts": 4, "src/domain/firmware/fitImage.ts": 1, "src/domain/gamecubeDol.ts": 6, - "src/domain/godotPckFormat.ts": 5, "src/domain/il2cpp/il2cppMetadataHeaderPrefix.ts": 3, "src/domain/inesContainer.ts": 5, "src/domain/managed/managedCliHeaderPrefix.ts": 5, diff --git a/src/artifacts/GodotPckArtifactReader.ts b/src/artifacts/GodotPckArtifactReader.ts index b7f0939196..35fa0384f7 100644 --- a/src/artifacts/GodotPckArtifactReader.ts +++ b/src/artifacts/GodotPckArtifactReader.ts @@ -1,4 +1,5 @@ import { readArtifactFileBytes } from "./ArtifactFileRange.js"; +import { BinaryReader } from "../domain/binaryContainer.js"; import { DEFAULT_GODOT_READER_LIMITS, type GodotReaderLimits, @@ -211,6 +212,7 @@ const readGodotPckHeader = async ( throw formatFailure("no standalone header or executable trailer"); const { start, end, embedded } = location; const fixed = await readArtifactFileBytes(handle, start, 40, end, signal); + const fixedReader = new BinaryReader(fixed, true, "godot-pck"); const header = readGodotPckHeaderFacts(fixed); const version = header.formatVersion; if (![1, 2, 3, 4].includes(version)) @@ -218,7 +220,7 @@ const readGodotPckHeader = async ( "unavailable", `Godot PCK format ${String(version)} is unsupported; plain formats 1, 2, 3 and 4 are supported`, ); - const flags = version === 1 ? 0 : fixed.readUInt32LE(20); + const flags = version === 1 ? 0 : fixedReader.u32(20); if ( (flags & ~(PACK_DIR_ENCRYPTED | PACK_REL_FILEBASE | PACK_SPARSE_BUNDLE)) !== 0 @@ -231,18 +233,20 @@ const readGodotPckHeader = async ( version === 1 ? 0 : toSafeOffset( - fixed.readBigUInt64LE(24), + fixedReader.word(24, 64), version >= 3 || (flags & PACK_REL_FILEBASE) !== 0 ? start : 0, end, "file base", ); const directory = version >= 3 - ? toSafeOffset(fixed.readBigUInt64LE(32), start, end, "directory") + ? toSafeOffset(fixedReader.word(32, 64), start, end, "directory") : start + (version === 1 ? 84 : 96); - const count = ( - await readArtifactFileBytes(handle, directory, 4, end, signal) - ).readUInt32LE(0); + const count = new BinaryReader( + await readArtifactFileBytes(handle, directory, 4, end, signal), + true, + "godot-pck", + ).u32(0); const status = (flags & PACK_DIR_ENCRYPTED) !== 0 ? "encrypted" @@ -339,10 +343,10 @@ class PckCursor { return this.#buffer.subarray(start, start + length); } async uint32(): Promise { - return (await this.bytes(4)).readUInt32LE(0); + return new BinaryReader(await this.bytes(4), true, "godot-pck").u32(0); } async uint64(): Promise { - return (await this.bytes(8)).readBigUInt64LE(0); + return new BinaryReader(await this.bytes(8), true, "godot-pck").word(0, 64); } } diff --git a/src/domain/godotPckFormat.ts b/src/domain/godotPckFormat.ts index 70283a8555..181949d263 100644 --- a/src/domain/godotPckFormat.ts +++ b/src/domain/godotPckFormat.ts @@ -1,3 +1,5 @@ +import { BinaryReader } from "./binaryContainer.js"; + /** Whether bytes start with a standalone Godot PCK header. */ export const hasGodotPckHeader = (bytes: Uint8Array): boolean => bytes.length >= 4 && @@ -14,16 +16,25 @@ export interface GodotPckHeaderFacts { readonly enginePatch: number; } +const pckReader = (bytes: Uint8Array): BinaryReader => + new BinaryReader( + Buffer.isBuffer(bytes) + ? bytes + : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength), + true, + "godot-pck", + ); + /** Decode declared fields after the caller admits a complete 20-byte header. */ export const readGodotPckHeaderFacts = ( bytes: Uint8Array, ): GodotPckHeaderFacts => { - const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); + const reader = pckReader(bytes); return { - formatVersion: view.getUint32(4, true), - engineMajor: view.getUint32(8, true), - engineMinor: view.getUint32(12, true), - enginePatch: view.getUint32(16, true), + formatVersion: reader.u32(4), + engineMajor: reader.u32(8), + engineMinor: reader.u32(12), + enginePatch: reader.u32(16), }; }; @@ -43,12 +54,7 @@ export const readGodotPckTrailerFacts = ( fileSize: number, ): GodotPckTrailerFacts => { if (!hasGodotPckHeader(trailer.subarray(8, 12))) return { state: "absent" }; - const view = new DataView( - trailer.buffer, - trailer.byteOffset, - trailer.byteLength, - ); - const packBytes = view.getBigUint64(0, true); + const packBytes = pckReader(trailer).word(0, 64); const end = fileSize - 12; return { state: "declared",