Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions packages/zstdify/src/decode/fusedSequences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,17 @@ function buildRLETable(symbol: number, tableLog: number): FSEDecodeTable {
return cached;
}
}
const tableSize = 1 << tableLog;
const symbolByState = new Uint16Array(tableSize);
const bitsByState = new Uint8Array(tableSize);
const baselineByState = new Int32Array(tableSize);
for (let i = 0; i < tableSize; i++) {
symbolByState[i] = symbol;
bitsByState[i] = tableLog;
}
// Sequence RLE mode is a degenerate single-state table: no initial-state bits
// are read, and no state-update bits are consumed while decoding tuples.
const symbolByState = new Uint16Array([symbol]);
const bitsByState = new Uint8Array([0]);
const baselineByState = new Int32Array([0]);
const table: FSEDecodeTable = {
symbol: symbolByState,
numBits: bitsByState,
baseline: baselineByState,
tableLog,
length: tableSize,
tableLog: 0,
length: 1,
};
if (cache) {
cache[symbol] = table;
Expand All @@ -88,7 +85,7 @@ export function decodeAndExecuteSequencesInto(
updateHistory: boolean,
collectMetadata = true,
): FusedDecodeExecuteResult {
if (seqSize < 2) {
if (seqSize < 1) {
throw new ZstdError('Sequences section too short', 'corruption_detected');
}
const sectionStart = seqOffset;
Expand Down Expand Up @@ -139,7 +136,7 @@ export function decodeAndExecuteSequencesInto(
if (llMode === 1) {
if (pos >= sectionStart + seqSize) throw new ZstdError('Sequences section truncated', 'corruption_detected');
llTable = buildRLETable(blockContent[pos]!, 6);
llTableLog = 6;
llTableLog = 0;
pos++;
} else if (llMode === 2) {
const result = readNCount(blockContent, pos, 35, 9);
Expand All @@ -155,7 +152,7 @@ export function decodeAndExecuteSequencesInto(
if (ofMode === 1) {
if (pos >= sectionStart + seqSize) throw new ZstdError('Sequences section truncated', 'corruption_detected');
ofTable = buildRLETable(blockContent[pos]!, 5);
ofTableLog = 5;
ofTableLog = 0;
pos++;
} else if (ofMode === 2) {
const result = readNCount(blockContent, pos, 31, 8);
Expand All @@ -171,7 +168,7 @@ export function decodeAndExecuteSequencesInto(
if (mlMode === 1) {
if (pos >= sectionStart + seqSize) throw new ZstdError('Sequences section truncated', 'corruption_detected');
mlTable = buildRLETable(blockContent[pos]!, 6);
mlTableLog = 6;
mlTableLog = 0;
pos++;
} else if (mlMode === 2) {
const result = readNCount(blockContent, pos, 52, 9);
Expand Down
31 changes: 31 additions & 0 deletions packages/zstdify/src/decode/sequences.modes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ describe('decodeSequences modes and extended counts', () => {
expect(result.bytesRead).toBe(2);
});

it('accepts a one-byte zero-sequences section', () => {
const result = decodeSequences(new Uint8Array([0x00]), 0, 1, null);
expect(result.sequences.length).toBe(0);
expect(result.bytesRead).toBe(1);
});

it('parses 255 marker path and rejects truncated section afterwards', () => {
const data = new Uint8Array([0xff, 0x00, 0x00]);
expect(() => decodeSequences(data, 0, data.length, null)).toThrowError(/truncated/i);
Expand All @@ -27,6 +33,31 @@ describe('decodeSequences modes and extended counts', () => {
]);
const result = decodeSequences(data, 0, data.length, null);
expect(result.sequences.length).toBe(1);
expect(result.tables.llTable.length).toBe(1);
expect(result.tables.ofTable.length).toBe(1);
expect(result.tables.mlTable.length).toBe(1);
expect(result.metadata.llTableLog).toBe(0);
expect(result.metadata.ofTableLog).toBe(0);
expect(result.metadata.mlTableLog).toBe(0);
});

it('keeps RLE offset mode bit consumption at zero for VRoid regression block', () => {
const block = Buffer.from(
'08004198e00f0012f8ffffbf6030c30ff9da3abf777eebfcd679adf37be7b5ceeb9ddf3bafebbcd6f9bdf37ae7b5ceeb9ddf3aaf757eefbcd679bdce6f9ddf3bbf757eebbcd6f9bdf35ae7f5ceef9dd7755eebfcee07289314',
'hex',
);
const literalsLength = 1;
const seqOffset = 1 + literalsLength;
const result = decodeSequences(block, seqOffset, block.length - seqOffset, null);
const totalLiteralsLength = Array.from(result.sequences.literalsLength.subarray(0, result.sequences.length)).reduce(
(sum, value) => sum + value,
0,
);

expect(result.sequences.length).toBe(65);
expect(totalLiteralsLength).toBe(1);
expect(result.tables.ofTable.length).toBe(1);
expect(result.metadata.ofTableLog).toBe(0);
});

it('enters FSE mode and rejects malformed FSE table stream', () => {
Expand Down
25 changes: 11 additions & 14 deletions packages/zstdify/src/decode/sequences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,17 @@ function buildRLETable(symbol: number, tableLog: number): FSEDecodeTable {
return cached;
}
}
const tableSize = 1 << tableLog;
const symbolByState = new Uint16Array(tableSize);
const bitsByState = new Uint8Array(tableSize);
const baselineByState = new Int32Array(tableSize);
for (let i = 0; i < tableSize; i++) {
symbolByState[i] = symbol;
bitsByState[i] = tableLog;
}
// Sequence RLE mode is a degenerate single-state table: no initial-state bits
// are read, and no state-update bits are consumed while decoding tuples.
const symbolByState = new Uint16Array([symbol]);
const bitsByState = new Uint8Array([0]);
const baselineByState = new Int32Array([0]);
const table: FSEDecodeTable = {
symbol: symbolByState,
numBits: bitsByState,
baseline: baselineByState,
tableLog,
length: tableSize,
tableLog: 0,
length: 1,
};
if (cache) {
cache[symbol] = table;
Expand All @@ -114,7 +111,7 @@ export function decodeSequences(
prevTables: SequenceTables | null,
sequenceReuse?: PackedSequences,
): DecodeSequencesResult {
if (size < 2) {
if (size < 1) {
throw new ZstdError('Sequences section too short', 'corruption_detected');
}

Expand Down Expand Up @@ -187,7 +184,7 @@ export function decodeSequences(
const sym = data[pos]!;
pos++;
llTable = buildRLETable(sym, 6);
llTableLog = 6;
llTableLog = 0;
} else if (llMode === 2) {
const result = readNCount(data, pos, 35, 9);
pos += result.bytesRead;
Expand All @@ -207,7 +204,7 @@ export function decodeSequences(
const sym = data[pos]!;
pos++;
ofTable = buildRLETable(sym, 5);
ofTableLog = 5;
ofTableLog = 0;
} else if (ofMode === 2) {
const result = readNCount(data, pos, 31, 8);
pos += result.bytesRead;
Expand All @@ -227,7 +224,7 @@ export function decodeSequences(
const sym = data[pos]!;
pos++;
mlTable = buildRLETable(sym, 6);
mlTableLog = 6;
mlTableLog = 0;
} else if (mlMode === 2) {
const result = readNCount(data, pos, 52, 9);
pos += result.bytesRead;
Expand Down