From 54e6bbdd208edfbcfc8f7c430e09f1da2e4b6cf7 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Tue, 1 Dec 2026 22:56:03 +0800 Subject: [PATCH] refactor(unityfs): share compressed block decoding (#15490) --- src/artifacts/UnityFsArtifactReader.ts | 13 +++---- src/artifacts/UnityFsStructures.ts | 47 ++++++-------------------- 2 files changed, 17 insertions(+), 43 deletions(-) diff --git a/src/artifacts/UnityFsArtifactReader.ts b/src/artifacts/UnityFsArtifactReader.ts index f74beb69d7f..9c9a68f366b 100644 --- a/src/artifacts/UnityFsArtifactReader.ts +++ b/src/artifacts/UnityFsArtifactReader.ts @@ -26,8 +26,7 @@ import { UNITYFS_COMPRESSION_NONE, } from "../domain/unityfsFormat.js"; import { - decodeUnityFsLz4, - decodeUnityFsLzma, + decodeUnityFsCompressedBlock, readUnityFsBlocksInfo, readUnityFsHeader, UNITYFS_BLOCK_DECODED_BUDGET_BYTES, @@ -396,10 +395,12 @@ export class UnityFsArtifactReader implements ArtifactReader { this.#dataStart + block.compressedStart + block.compressedSize, signal, ); - const bytes = - block.compression === UNITYFS_COMPRESSION_LZMA - ? await decodeUnityFsLzma(encoded, block.uncompressedSize, signal) - : await decodeUnityFsLz4(encoded, block.uncompressedSize, signal); + const bytes = await decodeUnityFsCompressedBlock( + block.compression === UNITYFS_COMPRESSION_LZMA ? "LZMA" : "LZ4", + encoded, + block.uncompressedSize, + signal, + ); this.#assertOpen(signal); this.#cachedBlock = { index, bytes }; return bytes; diff --git a/src/artifacts/UnityFsStructures.ts b/src/artifacts/UnityFsStructures.ts index 3bec9a2daf0..38716b2a195 100644 --- a/src/artifacts/UnityFsStructures.ts +++ b/src/artifacts/UnityFsStructures.ts @@ -255,13 +255,15 @@ const decodeBlocksInfoTable = async ( compression === UNITYFS_COMPRESSION_LZ4 || compression === UNITYFS_COMPRESSION_LZ4HC ) - return decodeUnityFsLz4( + return decodeUnityFsCompressedBlock( + "LZ4", compressed, header.uncompressedBlocksInfoSize, signal, ); if (compression === UNITYFS_COMPRESSION_LZMA) - return decodeUnityFsLzma( + return decodeUnityFsCompressedBlock( + "LZMA", compressed, header.uncompressedBlocksInfoSize, signal, @@ -273,13 +275,15 @@ const decodeBlocksInfoTable = async ( }; /** Translate decoder failures once, without erasing cancellation or resource reasons. */ -export const decodeUnityFsLz4 = async ( +export const decodeUnityFsCompressedBlock = async ( + codec: "LZ4" | "LZMA", encoded: Buffer, expected: number, signal?: AbortSignal, ): Promise => { + const decode = codec === "LZ4" ? decodeLz4Block : decodeUnityLzmaBlock; try { - return await decodeLz4Block( + return await decode( encoded, expected, MAX_BLOCK_UNCOMPRESSED_BYTES, @@ -290,44 +294,13 @@ export const decodeUnityFsLz4 = async ( if (cause instanceof BinaryResourceFailure) throw new ArtifactReaderFailure( "limit", - "UnityFS LZ4 exceeds the decoder memory budget", + `UnityFS ${codec} exceeds the decoder memory budget`, { cause, boundary: "memory" }, ); if (cause instanceof BinaryFormatFailure) throw malformedArtifact( "unityfs", - "UnityFS is malformed: LZ4 block does not decode to its declared size", - { cause, field: "block" }, - ); - throw cause; - } -}; - -/** Translate LZMA decoder failures once, without erasing cancellation or resource reasons. */ -export const decodeUnityFsLzma = async ( - encoded: Buffer, - expected: number, - signal?: AbortSignal, -): Promise => { - try { - return await decodeUnityLzmaBlock( - encoded, - expected, - MAX_BLOCK_UNCOMPRESSED_BYTES, - signal, - ); - } catch (cause: unknown) { - if (cause instanceof ArtifactReaderFailure) throw cause; - if (cause instanceof BinaryResourceFailure) - throw new ArtifactReaderFailure( - "limit", - "UnityFS LZMA exceeds the decoder memory budget", - { cause, boundary: "memory" }, - ); - if (cause instanceof BinaryFormatFailure) - throw malformedArtifact( - "unityfs", - "UnityFS is malformed: LZMA block does not decode to its declared size", + `UnityFS is malformed: ${codec} block does not decode to its declared size`, { cause, field: "block" }, ); throw cause;