{"record":{"id":"624df308736596fd","repo":"can1357/oh-my-pi","slug":"corrupt-compress-z-dictionary-code-code","errorCode":null,"errorMessage":"Corrupt compress (.Z) dictionary code ${code}","messagePattern":"Corrupt compress \\(\\.Z\\) dictionary code (.+?)","errorType":"exception","errorClass":"ArchiveError","httpStatus":null,"severity":"error","filePath":"packages/utils/src/ar/codecs/lzw.ts","lineNumber":134,"sourceCode":"\tconst dictionaryLimit = 2 ** maxBits;\n\tconst parents = new Uint16Array(dictionaryLimit);\n\tconst suffixes = new Uint8Array(dictionaryLimit);\n\tconst stack = new Uint8Array(dictionaryLimit);\n\tconst reader = new LsbCodeReader(bytes.subarray(3));\n\tconst output = new BoundedOutput(maxOutput, bytes.byteLength);\n\n\tlet width = MIN_BITS;\n\tlet dictionaryHead = blockMode ? FIRST_BLOCK_CODE : 256;\n\tlet needsPreviousSuffix = false;\n\n\tfor (;;) {\n\t\tconst code = reader.read(width);\n\t\tif (code === undefined) {\n\t\t\treader.assertFinalPadding();\n\t\t\treturn output.finish();\n\t\t}\n\t\tif (code >= dictionaryHead) {\n\t\t\tthrow new ArchiveError(`Corrupt compress (.Z) dictionary code ${code}`);\n\t\t}\n\t\tif (blockMode && code === CLEAR_CODE) {\n\t\t\treader.alignCodeGroup(width);\n\t\t\twidth = MIN_BITS;\n\t\t\tdictionaryHead = FIRST_BLOCK_CODE;\n\t\t\tneedsPreviousSuffix = false;\n\t\t\tcontinue;\n\t\t}\n\n\t\tlet current = code;\n\t\tlet stackLength = 0;\n\t\twhile (current >= 256) {\n\t\t\tif (current >= dictionaryHead || stackLength >= stack.length - 1) {\n\t\t\t\tthrow new ArchiveError(\"Corrupt compress (.Z) dictionary chain\");\n\t\t\t}\n\t\t\tstack[stackLength++] = suffixes[current]!;\n\t\t\tcurrent = parents[current]!;\n\t\t}","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/utils/src/ar/codecs/lzw.ts#L116-L152","documentation":"While reading LZW codes, each code must reference an existing dictionary entry (code < dictionaryHead). This error means the bitstream contained a code equal to or greater than the number of dictionary entries defined so far — the compressed data is corrupt, truncated mid-stream, or was not produced by the compress algorithm (e.g. raw LZW with different semantics).","triggerScenarios":"Calling lzwDecompress on a stream whose code stream references codes at or beyond the current dictionary position: bit-shifted data (e.g. a byte inserted/removed after the 3-byte header), a non-block-mode stream fed where codes were written differently, truncated file decoded anyway, or plain-LZW (GIF/TIFF-style) data misidentified as .Z.","commonSituations":"Concatenated .Z files (each has its own header) being decoded as one stream; off-by-one slicing after stripping the header; corrupted disk/transfer data; attempting to decode .Z output of 'compress -C' variants with different bit-packing.","solutions":["Confirm the buffer starts exactly at the 3-byte header and contains only one .Z stream; split or trim concatenated/extra data.","Re-download/re-extract the file and verify integrity (cksum, md5) — this error almost always means corrupt or truncated payload.","Verify the data is ncompress-format LZW, not GIF/TIFF LZW; use a codec matching the actual format.","If decoding concatenated members, call lzwDecompress per member instead of once over the joined bytes."],"exampleFix":"// before\nconst all = Buffer.concat([z1, z2]);\nawait lzwDecompress(all, limit); // throws: corrupt dictionary code\n// after\nconst out1 = await lzwDecompress(z1, limit);\nconst out2 = await lzwDecompress(z2, limit);","handlingStrategy":"try-catch","validationCode":"if (!isCompressZ(bytes)) throw new Error(\"not .Z\");\n// one stream per call — do not concatenate multiple .Z members","typeGuard":null,"tryCatchPattern":"try {\n  return await lzwDecompress(bytes, maxOutput);\n} catch (e) {\n  if (e instanceof ArchiveError && e.message.includes(\"Corrupt compress (.Z) dictionary code\")) {\n    throw new Error(\".Z payload corrupt, truncated, or contains multiple concatenated streams\");\n  }\n  throw e;\n}","preventionTips":["Decode each .Z member separately; never Buffer.concat multiple streams.","Strip exactly the 3-byte header inside the decoder — don't pre-slice with guessed offsets.","Verify transfer integrity (checksum) before decompressing untrusted files."],"tags":["archive","lzw","corrupt-data","bitstream"],"backgroundTag":"corrupt-compressed-data","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}