{"record":{"id":"ffdebd89cff86e9e","repo":"can1357/oh-my-pi","slug":"invalid-xz-stream-header-position-or-magic-is-inv","errorCode":null,"errorMessage":"Invalid XZ stream: header position or magic is invalid","messagePattern":"Invalid XZ stream: header position or magic is invalid","errorType":"exception","errorClass":"ArchiveError","httpStatus":null,"severity":"error","filePath":"packages/utils/src/ar/codecs/xz.ts","lineNumber":140,"sourceCode":"\t\t\tthrow new ArchiveError(\"Invalid XZ stream: footer CRC32 mismatch\");\n\t\tconst flag0 = bytes[footerStart + 8]!;\n\t\tconst flag1 = bytes[footerStart + 9]!;\n\t\tif (flag0 !== 0 || (flag1 & 0xf0) !== 0) throw new ArchiveError(\"Unsupported XZ stream flags\");\n\t\tconst checkId = flag1 & 0x0f;\n\t\tcheckSize(checkId);\n\t\tconst indexSize = (read32LE(bytes, footerStart + 4) + 1) * 4;\n\t\tif (!Number.isSafeInteger(indexSize) || indexSize > footerStart)\n\t\t\tthrow new ArchiveError(\"Invalid XZ stream: backward index size is invalid\");\n\t\tconst indexStart = footerStart - indexSize;\n\t\tconst records = parseIndex(bytes, indexStart, indexSize);\n\t\tlet blocksSize = 0;\n\t\tfor (const record of records) {\n\t\t\tblocksSize += Math.ceil(record.unpaddedSize / 4) * 4;\n\t\t\tif (!Number.isSafeInteger(blocksSize)) throw new ArchiveError(\"XZ stream uses sizes too large to read safely\");\n\t\t}\n\t\tconst start = indexStart - blocksSize - 12;\n\t\tif (start < 0 || start + 12 > bytes.byteLength || !equalBytes(bytes.subarray(start, start + 6), XZ_MAGIC)) {\n\t\t\tthrow new ArchiveError(\"Invalid XZ stream: header position or magic is invalid\");\n\t\t}\n\t\tif (bytes[start + 6] !== flag0 || bytes[start + 7] !== flag1)\n\t\t\tthrow new ArchiveError(\"Invalid XZ stream: header and footer flags differ\");\n\t\tif (crc32(bytes.subarray(start + 6, start + 8)) !== read32LE(bytes, start + 8))\n\t\t\tthrow new ArchiveError(\"Invalid XZ stream: header CRC32 mismatch\");\n\t\tstreams.unshift({ start, indexStart, footerStart, checkId, records });\n\t\tend = start;\n\t\tvoid padding;\n\t}\n\treturn streams;\n}\n\ninterface XzFilter {\n\tid: number;\n\tproperties: Uint8Array;\n}\n\nfunction deltaDecode(bytes: Uint8Array, distance: number): void {","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/utils/src/ar/codecs/xz.ts#L122-L158","documentation":"After parsing the footer and index, discoverStreams() computes where the stream header must begin (indexStart minus total block sizes minus the 12-byte header) and verifies the position is in-bounds and carries the 6-byte XZ magic (fd 37 7a 58 5a 00). If the computed position is negative, out of range, or the magic bytes are absent, the buffer is not a structurally valid XZ stream.","triggerScenarios":"Corrupted block padding/sizes making the computed header offset land on non-magic bytes; concatenating streams with extra non-zero bytes that shift block boundaries; a buffer whose tail parses as a plausible footer+index but whose body is not XZ data (e.g. random or other-format bytes).","commonSituations":"Opening files that merely resemble xz (renamed archives), splicing/multiplexing streams incorrectly, corruption in the block area of an otherwise intact footer/index.","solutions":["Verify the file is genuine .xz (magic bytes at each stream start) and passes xz -t","Re-download/restore the file to fix mid-stream corruption","If concatenating streams, ensure exact stream boundaries with only 4-byte zero padding between them","Use the library on whole buffers rather than hand-cut slices so offsets are computed internally"],"exampleFix":"// before\nconst bytes = concat([streamA, junkBytes, streamB]);\nawait xzDecode(bytes);\n// after\nconst bytes = concat([streamA, new Uint8Array(4), streamB]); // streams back-to-back; padding only\nawait xzDecode(bytes);","handlingStrategy":"try-catch","validationCode":"function startsWithXzMagic(bytes: Uint8Array): boolean {\n  const m = [0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00];\n  return m.every((b, i) => bytes[i] === b);\n}","typeGuard":"null","tryCatchPattern":"try {\n  await xzDecode(bytes);\n} catch (err) {\n  if (err instanceof ArchiveError && err.message.includes(\"header position or magic\")) {\n    throw new Error(\"XZ body does not match its footer/index — corrupt or spliced archive\");\n  }\n  throw err;\n}","preventionTips":["Concatenate streams back-to-back with only zero padding between them","Never splice headers/bodies across files","Verify archives with xz -t after any transfer"],"tags":["xz","archive","corrupt-input","magic-bytes"],"backgroundTag":"invalid-archive-format","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}