feat: extract complete multi-cabinet sets
This commit is contained in:
@@ -29,39 +29,68 @@ import {
|
||||
const CAB_SIGNATURE = 0x4643_534d;
|
||||
const FLAG_PREVIOUS_CABINET = 0x0001;
|
||||
const FLAG_NEXT_CABINET = 0x0002;
|
||||
const FLAG_RESERVE_PRESENT = 0x0004;
|
||||
export const FLAG_RESERVE_PRESENT = 0x0004;
|
||||
const ATTRIBUTE_NAME_IS_UTF8 = 0x0080;
|
||||
const MAX_CAB_BLOCK_OUTPUT = 32 * 1024;
|
||||
const MAX_CAB_BLOCK_INPUT = MAX_CAB_BLOCK_OUTPUT + 6 * 1024;
|
||||
const MAX_MSZIP_BLOCK_INPUT = 32 * 1024 + 12;
|
||||
export const FOLDER_CONTINUED_FROM_PREVIOUS = 0xfffd;
|
||||
export const FOLDER_CONTINUED_TO_NEXT = 0xfffe;
|
||||
export const FOLDER_CONTINUED_PREVIOUS_AND_NEXT = 0xffff;
|
||||
const STRICT_UTF8 = new TextDecoder('utf-8', { fatal: true });
|
||||
|
||||
interface CabDataBlock {
|
||||
export interface CabDataBlockInternal {
|
||||
headerOffset: number;
|
||||
checksum: number;
|
||||
compressedSize: number;
|
||||
uncompressedSize: number;
|
||||
reserve: Uint8Array;
|
||||
dataOffset: number;
|
||||
sourceBytes: Uint8Array;
|
||||
}
|
||||
|
||||
interface CabFolderInternal {
|
||||
export interface CabFolderInternal {
|
||||
index: number;
|
||||
firstDataBlockOffset: number;
|
||||
dataBlockCount: number;
|
||||
compression: CabCompressionMethod;
|
||||
blocks: CabDataBlock[];
|
||||
reserve: Uint8Array;
|
||||
blocks: CabDataBlockInternal[];
|
||||
compressedSize: number;
|
||||
uncompressedSize: number;
|
||||
dataEnd: number;
|
||||
}
|
||||
|
||||
interface ParsedCabinet {
|
||||
export interface CabFileRecordInternal {
|
||||
entry: CabFileEntry;
|
||||
rawFolderIndex: number;
|
||||
duplicateKey: string;
|
||||
date: number;
|
||||
time: number;
|
||||
entryOffset: number;
|
||||
}
|
||||
|
||||
export interface ParsedCabinetInternal {
|
||||
bytes: Uint8Array;
|
||||
limits: OnePkgLimits;
|
||||
flags: number;
|
||||
previousCabinetName?: string;
|
||||
previousDiskName?: string;
|
||||
nextCabinetName?: string;
|
||||
nextDiskName?: string;
|
||||
headerReserveSize: number;
|
||||
folderReserveSize: number;
|
||||
dataReserveSize: number;
|
||||
headerReserve: Uint8Array;
|
||||
folders: CabFolderInternal[];
|
||||
fileRecords: CabFileRecordInternal[];
|
||||
result: CabEnumerationResult;
|
||||
}
|
||||
|
||||
function verifyDataBlockChecksum(bytes: Uint8Array, block: CabDataBlock): void {
|
||||
export function verifyDataBlockChecksumInternal(
|
||||
bytes: Uint8Array,
|
||||
block: CabDataBlockInternal
|
||||
): void {
|
||||
if (block.checksum === 0) return;
|
||||
const compressed = bytes.subarray(
|
||||
block.dataOffset,
|
||||
@@ -95,6 +124,31 @@ function checkedAdd(
|
||||
return sum;
|
||||
}
|
||||
|
||||
function readLinkedCabinetString(
|
||||
reader: BinaryReader,
|
||||
limits: OnePkgLimits,
|
||||
structure: string,
|
||||
allowEmpty: boolean
|
||||
): string {
|
||||
const offset = reader.offset;
|
||||
const bytes = reader.readNullTerminatedBytes(limits.maxNameBytes, structure);
|
||||
if (!allowEmpty && bytes.length === 0) {
|
||||
fail('cab-linked-name-empty', 'Linked cabinet name cannot be empty.', {
|
||||
offset,
|
||||
structure,
|
||||
});
|
||||
}
|
||||
try {
|
||||
return STRICT_UTF8.decode(bytes).normalize('NFC');
|
||||
} catch (error) {
|
||||
fail(
|
||||
'cab-linked-name-invalid-utf8',
|
||||
`Linked cabinet metadata is not valid UTF-8${error instanceof Error ? `: ${error.message}` : '.'}`,
|
||||
{ offset, structure }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function parseCompression(
|
||||
raw: number,
|
||||
limits: OnePkgLimits,
|
||||
@@ -201,10 +255,11 @@ function parseCompression(
|
||||
);
|
||||
}
|
||||
|
||||
function parseCabinet(
|
||||
export function parseCabinetInternal(
|
||||
bytes: Uint8Array,
|
||||
options: CabOpenOptions
|
||||
): ParsedCabinet {
|
||||
options: CabOpenOptions,
|
||||
allowMultipart = false
|
||||
): ParsedCabinetInternal {
|
||||
if (!(bytes instanceof Uint8Array)) {
|
||||
throw new TypeError('CAB input must be a Uint8Array');
|
||||
}
|
||||
@@ -276,7 +331,10 @@ function parseCabinet(
|
||||
}
|
||||
);
|
||||
}
|
||||
if ((flags & (FLAG_PREVIOUS_CABINET | FLAG_NEXT_CABINET)) !== 0) {
|
||||
if (
|
||||
!allowMultipart &&
|
||||
(flags & (FLAG_PREVIOUS_CABINET | FLAG_NEXT_CABINET)) !== 0
|
||||
) {
|
||||
fail(
|
||||
'cab-multipart-unsupported',
|
||||
'Multi-cabinet archives are not supported for local .onepkg files.',
|
||||
@@ -293,15 +351,41 @@ function parseCabinet(
|
||||
|
||||
// Re-bind reads to the trusted cbCabinet boundary before parsing variable data.
|
||||
const reader = new BinaryReader(bytes, header.offset, declaredSize);
|
||||
let headerReserveSize = 0;
|
||||
let folderReserveSize = 0;
|
||||
let dataReserveSize = 0;
|
||||
let headerReserve: Uint8Array = new Uint8Array(0);
|
||||
if ((flags & FLAG_RESERVE_PRESENT) !== 0) {
|
||||
const headerReserveSize = reader.readU16('CFHEADER.cbCFHeader');
|
||||
headerReserveSize = reader.readU16('CFHEADER.cbCFHeader');
|
||||
folderReserveSize = reader.readU8('CFHEADER.cbCFFolder');
|
||||
dataReserveSize = reader.readU8('CFHEADER.cbCFData');
|
||||
reader.skip(headerReserveSize, 'CFHEADER.abReserve');
|
||||
if (headerReserveSize > 60_000) {
|
||||
fail(
|
||||
'cab-header-reserve-limit',
|
||||
'CAB header reserve area exceeds the 60,000-byte format limit.',
|
||||
{ offset: header.offset, structure: 'CFHEADER.cbCFHeader' }
|
||||
);
|
||||
}
|
||||
headerReserve = reader.readBytes(headerReserveSize, 'CFHEADER.abReserve');
|
||||
}
|
||||
|
||||
const previousCabinetName =
|
||||
(flags & FLAG_PREVIOUS_CABINET) !== 0
|
||||
? readLinkedCabinetString(reader, limits, 'CFHEADER.szCabinetPrev', false)
|
||||
: undefined;
|
||||
const previousDiskName =
|
||||
(flags & FLAG_PREVIOUS_CABINET) !== 0
|
||||
? readLinkedCabinetString(reader, limits, 'CFHEADER.szDiskPrev', true)
|
||||
: undefined;
|
||||
const nextCabinetName =
|
||||
(flags & FLAG_NEXT_CABINET) !== 0
|
||||
? readLinkedCabinetString(reader, limits, 'CFHEADER.szCabinetNext', false)
|
||||
: undefined;
|
||||
const nextDiskName =
|
||||
(flags & FLAG_NEXT_CABINET) !== 0
|
||||
? readLinkedCabinetString(reader, limits, 'CFHEADER.szDiskNext', true)
|
||||
: undefined;
|
||||
|
||||
const folders: CabFolderInternal[] = [];
|
||||
let totalDataBlocks = 0;
|
||||
for (let index = 0; index < folderCount; index += 1) {
|
||||
@@ -315,7 +399,7 @@ function parseCabinet(
|
||||
limits,
|
||||
compressionRawOffset
|
||||
);
|
||||
reader.skip(folderReserveSize, 'CFFOLDER.abReserve');
|
||||
const reserve = reader.readBytes(folderReserveSize, 'CFFOLDER.abReserve');
|
||||
totalDataBlocks = checkedAdd(
|
||||
totalDataBlocks,
|
||||
dataBlockCount,
|
||||
@@ -334,6 +418,7 @@ function parseCabinet(
|
||||
firstDataBlockOffset,
|
||||
dataBlockCount,
|
||||
compression,
|
||||
reserve,
|
||||
blocks: [],
|
||||
compressedSize: 0,
|
||||
uncompressedSize: 0,
|
||||
@@ -349,6 +434,7 @@ function parseCabinet(
|
||||
}
|
||||
const fileReader = new BinaryReader(bytes, firstFileOffset, declaredSize);
|
||||
const entries: CabFileEntry[] = [];
|
||||
const fileRecords: CabFileRecordInternal[] = [];
|
||||
const diagnostics = [];
|
||||
const duplicatePaths = new Set<string>();
|
||||
let extractedByteTotal = 0;
|
||||
@@ -357,9 +443,9 @@ function parseCabinet(
|
||||
const entryOffset = fileReader.offset;
|
||||
const uncompressedSize = fileReader.readU32('CFFILE.cbFile');
|
||||
const uncompressedOffset = fileReader.readU32('CFFILE.uoffFolderStart');
|
||||
const folderIndex = fileReader.readU16('CFFILE.iFolder');
|
||||
fileReader.readU16('CFFILE.date');
|
||||
fileReader.readU16('CFFILE.time');
|
||||
const rawFolderIndex = fileReader.readU16('CFFILE.iFolder');
|
||||
const date = fileReader.readU16('CFFILE.date');
|
||||
const time = fileReader.readU16('CFFILE.time');
|
||||
const attributes = fileReader.readU16('CFFILE.attribs');
|
||||
const nameOffset = fileReader.offset;
|
||||
const nameBytes = fileReader.readNullTerminatedBytes(
|
||||
@@ -367,12 +453,42 @@ function parseCabinet(
|
||||
'CFFILE.szName'
|
||||
);
|
||||
|
||||
if (folderIndex >= 0xfffd) {
|
||||
fail(
|
||||
'cab-multipart-file-unsupported',
|
||||
'CAB file spans another cabinet; multi-cabinet files are unsupported.',
|
||||
{ offset: entryOffset + 8, structure: 'CFFILE.iFolder' }
|
||||
);
|
||||
let folderIndex = rawFolderIndex;
|
||||
if (rawFolderIndex >= FOLDER_CONTINUED_FROM_PREVIOUS) {
|
||||
if (!allowMultipart) {
|
||||
fail(
|
||||
'cab-multipart-file-unsupported',
|
||||
'CAB file spans another cabinet; multi-cabinet files are unsupported.',
|
||||
{ offset: entryOffset + 8, structure: 'CFFILE.iFolder' }
|
||||
);
|
||||
}
|
||||
const continuesFromPrevious =
|
||||
rawFolderIndex === FOLDER_CONTINUED_FROM_PREVIOUS ||
|
||||
rawFolderIndex === FOLDER_CONTINUED_PREVIOUS_AND_NEXT;
|
||||
const continuesToNext =
|
||||
rawFolderIndex === FOLDER_CONTINUED_TO_NEXT ||
|
||||
rawFolderIndex === FOLDER_CONTINUED_PREVIOUS_AND_NEXT;
|
||||
if (
|
||||
(continuesFromPrevious && (flags & FLAG_PREVIOUS_CABINET) === 0) ||
|
||||
(continuesToNext && (flags & FLAG_NEXT_CABINET) === 0)
|
||||
) {
|
||||
fail(
|
||||
'cab-spanning-file-flag-mismatch',
|
||||
'CAB spanning file marker does not match its prev/next header flags.',
|
||||
{ offset: entryOffset + 8, structure: 'CFFILE.iFolder' }
|
||||
);
|
||||
}
|
||||
if (
|
||||
rawFolderIndex === FOLDER_CONTINUED_PREVIOUS_AND_NEXT &&
|
||||
folders.length !== 1
|
||||
) {
|
||||
fail(
|
||||
'cab-spanning-folder-ambiguous',
|
||||
'A folder continued from both cabinets must be the only folder in this cabinet.',
|
||||
{ offset: entryOffset + 8, structure: 'CFFILE.iFolder' }
|
||||
);
|
||||
}
|
||||
folderIndex = continuesFromPrevious ? 0 : folders.length - 1;
|
||||
}
|
||||
const folder = folders[folderIndex];
|
||||
if (folder === undefined) {
|
||||
@@ -392,18 +508,20 @@ function parseCabinet(
|
||||
{ offset: entryOffset, structure: 'CFFILE.cbFile' }
|
||||
);
|
||||
}
|
||||
extractedByteTotal = checkedAdd(
|
||||
extractedByteTotal,
|
||||
uncompressedSize,
|
||||
'cab-extracted-size-overflow',
|
||||
'CFFILE.cbFile'
|
||||
);
|
||||
if (extractedByteTotal > limits.maxExtractedBytes) {
|
||||
fail(
|
||||
'cab-extracted-size-limit',
|
||||
`Extracted files exceed the configured ${limits.maxExtractedBytes}-byte limit.`,
|
||||
{ offset: entryOffset, structure: 'CFFILE.cbFile' }
|
||||
if (!allowMultipart) {
|
||||
extractedByteTotal = checkedAdd(
|
||||
extractedByteTotal,
|
||||
uncompressedSize,
|
||||
'cab-extracted-size-overflow',
|
||||
'CFFILE.cbFile'
|
||||
);
|
||||
if (extractedByteTotal > limits.maxExtractedBytes) {
|
||||
fail(
|
||||
'cab-extracted-size-limit',
|
||||
`Extracted files exceed the configured ${limits.maxExtractedBytes}-byte limit.`,
|
||||
{ offset: entryOffset, structure: 'CFFILE.cbFile' }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const path = decodeCabFileName(
|
||||
@@ -432,7 +550,7 @@ function parseCabinet(
|
||||
);
|
||||
}
|
||||
duplicatePaths.add(normalized.duplicateKey);
|
||||
entries.push({
|
||||
const entry: CabFileEntry = {
|
||||
index,
|
||||
path: normalized.path,
|
||||
normalizedPath: normalized.normalizedPath,
|
||||
@@ -441,6 +559,15 @@ function parseCabinet(
|
||||
uncompressedSize,
|
||||
attributes,
|
||||
compression: folder.compression,
|
||||
};
|
||||
entries.push(entry);
|
||||
fileRecords.push({
|
||||
entry,
|
||||
rawFolderIndex,
|
||||
duplicateKey: normalized.duplicateKey,
|
||||
date,
|
||||
time,
|
||||
entryOffset,
|
||||
});
|
||||
}
|
||||
const metadataEnd = fileReader.offset;
|
||||
@@ -449,6 +576,12 @@ function parseCabinet(
|
||||
let totalLzxOutput = 0;
|
||||
let totalQuantumOutput = 0;
|
||||
for (const folder of folders) {
|
||||
const folderContinuesToNext = fileRecords.some(
|
||||
(record) =>
|
||||
record.entry.folderIndex === folder.index &&
|
||||
(record.rawFolderIndex === FOLDER_CONTINUED_TO_NEXT ||
|
||||
record.rawFolderIndex === FOLDER_CONTINUED_PREVIOUS_AND_NEXT)
|
||||
);
|
||||
if (folder.firstDataBlockOffset > declaredSize) {
|
||||
fail(
|
||||
'cab-invalid-data-offset',
|
||||
@@ -493,17 +626,32 @@ function parseCabinet(
|
||||
);
|
||||
const dataOffset = blockReader.offset;
|
||||
blockReader.skip(compressedSize, 'CFDATA.ab');
|
||||
if (
|
||||
compressedSize === 0 ||
|
||||
uncompressedSize === 0 ||
|
||||
uncompressedSize > MAX_CAB_BLOCK_OUTPUT
|
||||
) {
|
||||
if (compressedSize === 0 || uncompressedSize > MAX_CAB_BLOCK_OUTPUT) {
|
||||
fail(
|
||||
'cab-invalid-data-block-size',
|
||||
`Invalid CAB data block sizes ${compressedSize}/${uncompressedSize}.`,
|
||||
{ offset: headerOffset + 4, structure: 'CFDATA' }
|
||||
);
|
||||
}
|
||||
if (
|
||||
uncompressedSize === 0 &&
|
||||
(!allowMultipart ||
|
||||
blockIndex !== folder.dataBlockCount - 1 ||
|
||||
!folderContinuesToNext)
|
||||
) {
|
||||
fail(
|
||||
'cab-invalid-split-data-block',
|
||||
'A zero-output CFDATA fragment must be the final block of a folder continued in the next cabinet.',
|
||||
{ offset: headerOffset + 6, structure: 'CFDATA.cbUncomp' }
|
||||
);
|
||||
}
|
||||
if (compressedSize > MAX_CAB_BLOCK_INPUT) {
|
||||
fail(
|
||||
'cab-invalid-compressed-block-size',
|
||||
`CAB data block size ${compressedSize} exceeds ${MAX_CAB_BLOCK_INPUT} bytes.`,
|
||||
{ offset: headerOffset + 4, structure: 'CFDATA.cbData' }
|
||||
);
|
||||
}
|
||||
if (
|
||||
folder.compression.kind === 'mszip' &&
|
||||
compressedSize > MAX_MSZIP_BLOCK_INPUT
|
||||
@@ -516,6 +664,7 @@ function parseCabinet(
|
||||
}
|
||||
if (
|
||||
folder.compression.kind === 'none' &&
|
||||
!allowMultipart &&
|
||||
compressedSize !== uncompressedSize
|
||||
) {
|
||||
fail(
|
||||
@@ -550,12 +699,14 @@ function parseCabinet(
|
||||
uncompressedSize,
|
||||
reserve,
|
||||
dataOffset,
|
||||
sourceBytes: bytes,
|
||||
});
|
||||
}
|
||||
folder.dataEnd = blockReader.offset;
|
||||
if (
|
||||
!allowMultipart &&
|
||||
folder.uncompressedSize / folder.compressedSize >
|
||||
limits.maxCompressionRatio
|
||||
limits.maxCompressionRatio
|
||||
) {
|
||||
fail(
|
||||
'cab-compression-ratio-limit',
|
||||
@@ -624,20 +775,22 @@ function parseCabinet(
|
||||
}
|
||||
}
|
||||
|
||||
for (const entry of entries) {
|
||||
const folder = folders[entry.folderIndex]!;
|
||||
const fileEnd = checkedAdd(
|
||||
entry.uncompressedOffset,
|
||||
entry.uncompressedSize,
|
||||
'cab-file-range-overflow',
|
||||
'CFFILE'
|
||||
);
|
||||
if (fileEnd > folder.uncompressedSize) {
|
||||
fail(
|
||||
'cab-file-range-out-of-bounds',
|
||||
`CAB file ${entry.normalizedPath} extends beyond its folder data.`,
|
||||
{ structure: 'CFFILE' }
|
||||
if (!allowMultipart) {
|
||||
for (const entry of entries) {
|
||||
const folder = folders[entry.folderIndex]!;
|
||||
const fileEnd = checkedAdd(
|
||||
entry.uncompressedOffset,
|
||||
entry.uncompressedSize,
|
||||
'cab-file-range-overflow',
|
||||
'CFFILE'
|
||||
);
|
||||
if (fileEnd > folder.uncompressedSize) {
|
||||
fail(
|
||||
'cab-file-range-out-of-bounds',
|
||||
`CAB file ${entry.normalizedPath} extends beyond its folder data.`,
|
||||
{ structure: 'CFFILE' }
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -662,7 +815,17 @@ function parseCabinet(
|
||||
return {
|
||||
bytes,
|
||||
limits,
|
||||
flags,
|
||||
previousCabinetName,
|
||||
previousDiskName,
|
||||
nextCabinetName,
|
||||
nextDiskName,
|
||||
headerReserveSize,
|
||||
folderReserveSize,
|
||||
dataReserveSize,
|
||||
headerReserve,
|
||||
folders,
|
||||
fileRecords,
|
||||
result: {
|
||||
cabinetSetId,
|
||||
cabinetSetIndex,
|
||||
@@ -684,14 +847,14 @@ export function enumerateCabinet(
|
||||
bytes: Uint8Array,
|
||||
options: CabOpenOptions = {}
|
||||
): CabEnumerationResult {
|
||||
return parseCabinet(bytes, options).result;
|
||||
return parseCabinetInternal(bytes, options).result;
|
||||
}
|
||||
|
||||
export async function extractCabinet(
|
||||
bytes: Uint8Array,
|
||||
options: CabOpenOptions = {}
|
||||
): Promise<CabExtractionResult> {
|
||||
const parsed = parseCabinet(bytes, options);
|
||||
const parsed = parseCabinetInternal(bytes, options);
|
||||
const byFolder = new Map<number, CabFileEntry[]>();
|
||||
for (const entry of parsed.result.entries) {
|
||||
const current = byFolder.get(entry.folderIndex);
|
||||
@@ -728,7 +891,7 @@ export async function extractCabinet(
|
||||
while (quantumQueuedBlock < lookahead) {
|
||||
quantumQueuedBlock += 1;
|
||||
const queued = folder.blocks[quantumQueuedBlock]!;
|
||||
verifyDataBlockChecksum(parsed.bytes, queued);
|
||||
verifyDataBlockChecksumInternal(parsed.bytes, queued);
|
||||
quantumDecoder.appendCabBlock(
|
||||
parsed.bytes.subarray(
|
||||
queued.dataOffset,
|
||||
@@ -743,7 +906,7 @@ export async function extractCabinet(
|
||||
block.dataOffset + block.compressedSize
|
||||
);
|
||||
if (quantumDecoder === undefined) {
|
||||
verifyDataBlockChecksum(parsed.bytes, block);
|
||||
verifyDataBlockChecksumInternal(parsed.bytes, block);
|
||||
}
|
||||
const output =
|
||||
decoder !== undefined
|
||||
|
||||
Reference in New Issue
Block a user