{"record":{"id":"1b13e4b54d06a955","repo":"can1357/oh-my-pi","slug":"invalid-xz-stream-block-crc64-mismatch","errorCode":null,"errorMessage":"Invalid XZ stream: block CRC64 mismatch","messagePattern":"Invalid XZ stream: block CRC64 mismatch","errorType":"exception","errorClass":"ArchiveError","httpStatus":null,"severity":"error","filePath":"packages/utils/src/ar/codecs/xz.ts","lineNumber":421,"sourceCode":"\t\tcase 11:\n\t\t\triscvDecode(bytes, startOffset);\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tthrow new ArchiveError(`Unsupported XZ filter ID 0x${filter.id.toString(16)}`);\n\t}\n}\n\nfunction verifyCheck(checkId: number, output: Uint8Array, expected: Uint8Array): void {\n\tif (checkId === 0) return;\n\tif (checkId === 1) {\n\t\tif (read32LE(expected, 0) !== crc32(output)) throw new ArchiveError(\"Invalid XZ stream: block CRC32 mismatch\");\n\t\treturn;\n\t}\n\tif (checkId === 4) {\n\t\tconst actual = crc64(output);\n\t\tlet stored = 0n;\n\t\tfor (let index = 0; index < 8; index++) stored |= BigInt(expected[index]!) << BigInt(index * 8);\n\t\tif (actual !== stored) throw new ArchiveError(\"Invalid XZ stream: block CRC64 mismatch\");\n\t\treturn;\n\t}\n\tconst actual = new Uint8Array(new Bun.CryptoHasher(\"sha256\").update(output).digest());\n\tif (!equalBytes(actual, expected)) throw new ArchiveError(\"Invalid XZ stream: block SHA-256 mismatch\");\n}\n\nasync function decodeBlock(bytes: Uint8Array, offset: number, record: XzRecord, checkId: number): Promise<Uint8Array> {\n\tif (offset >= bytes.byteLength || bytes[offset] === 0)\n\t\tthrow new ArchiveError(\"Invalid XZ stream: missing block header\");\n\tconst headerSize = (bytes[offset]! + 1) * 4;\n\tif (offset + headerSize > bytes.byteLength || headerSize < 8)\n\t\tthrow new ArchiveError(\"Invalid XZ stream: truncated block header\");\n\tif (crc32(bytes.subarray(offset, offset + headerSize - 4)) !== read32LE(bytes, offset + headerSize - 4))\n\t\tthrow new ArchiveError(\"Invalid XZ stream: block header CRC32 mismatch\");\n\tconst cursor: Cursor = { bytes, pos: offset + 1, limit: offset + headerSize - 4 };\n\tconst flags = bytes[cursor.pos++]!;\n\tif ((flags & 0x3c) !== 0) throw new ArchiveError(\"Unsupported XZ block flags\");\n\tconst filterCount = (flags & 3) + 1;","sourceCodeStart":403,"sourceCodeEnd":439,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/utils/src/ar/codecs/xz.ts#L403-L439","documentation":"XZ check type 4 is CRC64. After decoding a block the library computes a CRC64 over the uncompressed data and compares it to the 8 stored little-endian bytes. A mismatch means the block's contents do not match the encoder's integrity hash — the input stream is corrupt or altered.","triggerScenarios":"Decoding an XZ stream created with `xz --check=crc64` (a common default) where any block's stored CRC64 differs from the computed CRC64 of its decoded output.","commonSituations":"Incomplete or corrupted downloads; failed copies to removable media; archives modified in place; memory/disk faults during compression producing a bad stored hash.","solutions":["Re-download or restore the archive; the mismatch indicates real data damage","Confirm externally via `xz -t file.xz` that the file fails integrity there too","Retry the transfer if the source is a flaky network or medium, then re-verify","Surface a 'corrupt archive' error to users instead of attempting decompression of known-bad bytes"],"exampleFix":"// before: assuming a transient bug and retrying decode\nawait decodeXz(buf); await decodeXz(buf);\n// after: replace the source file on integrity failure\ncatch (e) { if (String(e.message).includes('CRC64 mismatch')) await reFetchArchive(); }","handlingStrategy":"try-catch","validationCode":"const t = await $`xz -t archive.xz`.quiet().nothrow();\nif (t.exitCode !== 0) throw new Error('archive.xz failed integrity check');","typeGuard":"null","tryCatchPattern":"try {\n  return await decodeXz(bytes);\n} catch (err) {\n  if (err instanceof ArchiveError && err.message.includes('CRC64 mismatch')) {\n    throw new Error('XZ block data is corrupt (CRC64); re-download the archive');\n  }\n  throw err;\n}","preventionTips":["Always transfer archives in binary mode (never FTP ASCII) — this is a classic CRC64 killer","Verify sizes/checksums after downloads before decode","Watch for failing disks: repeated integrity failures on stored files indicate hardware issues","Use `xz -t` in CI when archives are build artifacts"],"tags":["xz","archive","crc64","integrity","corrupt-input"],"backgroundTag":"checksum-mismatch","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}