{"record":{"id":"89e8b8d445e26ebc","repo":"mozilla/pdf.js","slug":"unknown-block-type-in-flate-stream","errorCode":null,"errorMessage":"Unknown block type in flate stream","messagePattern":"Unknown block type in flate stream","errorType":"exception","errorClass":"FormatError","httpStatus":null,"severity":"error","filePath":"src/core/flate_stream.js","lineNumber":412,"sourceCode":"        } else {\n          codeLengths[i++] = len = code;\n          continue;\n        }\n\n        let repeatLength = this.getBits(bitsLength) + bitsOffset;\n        while (repeatLength-- > 0) {\n          codeLengths[i++] = what;\n        }\n      }\n\n      litCodeTable = this.generateHuffmanTable(\n        codeLengths.subarray(0, numLitCodes)\n      );\n      distCodeTable = this.generateHuffmanTable(\n        codeLengths.subarray(numLitCodes, codes)\n      );\n    } else {\n      throw new FormatError(\"Unknown block type in flate stream\");\n    }\n\n    buffer = this.buffer;\n    let limit = buffer ? buffer.length : 0;\n    let pos = this.bufferLength;\n    while (true) {\n      let code1 = this.getCode(litCodeTable);\n      if (code1 < 256) {\n        if (pos + 1 >= limit) {\n          buffer = this.ensureBuffer(pos + 1);\n          limit = buffer.length;\n        }\n        buffer[pos++] = code1;\n        continue;\n      }\n      if (code1 === 256) {\n        this.bufferLength = pos;\n        return;","sourceCodeStart":394,"sourceCodeEnd":430,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/core/flate_stream.js#L394-L430","documentation":"Thrown by readBlock() when the 2-bit block-type field (BTYPE) read from the DEFLATE stream is 3 (binary 11), which is a reserved/invalid value per RFC 1951. Only values 0 (stored), 1 (fixed Huffman), and 2 (dynamic Huffman) are valid; the decoder cannot proceed.","triggerScenarios":"readBlock() reads hdr = getBits(3), shifts right by 1, and the resulting block type is not 0, 1, or 2 — i.e., the raw 2-bit value was 3. This means the block header bits are corrupt or the stream has fallen out of sync.","commonSituations":"Severe bit-level corruption in a compressed stream. A stream that was truncated and then padded with garbage bytes. Misaligned parsing caused by earlier corruption that shifted the bit-reader's position. PDFs produced by buggy or experimental generators.","solutions":["Obtain a pristine copy of the PDF from the original source.","Rewrite the PDF with qpdf --check=verbose to identify and repair the damaged stream object.","If generating PDFs, use a mature library (e.g., pdfkit, iText, Ghostscript) rather than hand-rolling DEFLATE output.","Wrap page rendering in error handling and render remaining pages."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Check PDF stream integrity with zlib server-side\nconst zlib = require('zlib');\nfunction canInflate(buf) {\n  try { zlib.inflateSync(buf); return true; }\n  catch { return false; }\n}","typeGuard":null,"tryCatchPattern":"try {\n  await page.render(renderParams).promise;\n} catch (err) {\n  if (err.message?.includes('Unknown block type in flate stream')) {\n    console.error('Severe DEFLATE corruption; PDF may be damaged.');\n    showErrorToUser('This PDF appears to be corrupted.');\n  } else { throw err; }\n}","preventionTips":["Always obtain PDFs from trusted sources.","Verify file hashes after transfer to detect corruption.","Use qpdf --check or pdfinfo to validate structural integrity."],"tags":["flate","deflate","block-type","corrupt-data","pdf-stream","format-error"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}