{"record":{"id":"5bace3ba787d8277","repo":"BabylonJS/Babylon.js","slug":"zlib-unexpected-end-of-input","errorCode":null,"errorMessage":"zlib: unexpected end of input","messagePattern":"zlib: unexpected end of input","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/dev/loaders/src/FBX/parsers/zlibInflate.ts","lineNumber":22,"sourceCode":"\r\nconst LENGTH_BASE = [3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258];\r\nconst LENGTH_EXTRA_BITS = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0];\r\nconst DISTANCE_BASE = [1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577];\r\nconst DISTANCE_EXTRA_BITS = [0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13];\r\nconst CODE_LENGTH_ORDER = [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15];\r\n\r\n/**\r\n * Inflate a zlib-wrapped deflate stream.\r\n *\r\n * This implementation is intentionally scoped to FBX binary array payloads: one-shot,\r\n * synchronous zlib streams with the exact uncompressed length known up front.\r\n */\r\nexport function inflateZlib(input: Uint8Array, expectedLength: number): Uint8Array {\r\n    if (!Number.isInteger(expectedLength) || expectedLength < 0) {\r\n        throw new Error(\"zlib: invalid expected length\");\r\n    }\r\n    if (input.byteLength < 6) {\r\n        throw new Error(\"zlib: unexpected end of input\");\r\n    }\r\n\r\n    const cmf = input[0];\r\n    const flg = input[1];\r\n    if ((cmf & 0x0f) !== 8 || cmf >> 4 > 7 || ((cmf << 8) + flg) % 31 !== 0) {\r\n        throw new Error(\"zlib: invalid header\");\r\n    }\r\n    if ((flg & 0x20) !== 0) {\r\n        throw new Error(\"zlib: preset dictionary not supported\");\r\n    }\r\n\r\n    const reader = new BitReader(input, 2, input.byteLength - 4);\r\n    const output = new OutputWriter(expectedLength);\r\n\r\n    let isFinalBlock = false;\r\n    while (!isFinalBlock) {\r\n        isFinalBlock = reader.readBits(1) === 1;\r\n        const blockType = reader.readBits(2);\r","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/BabylonJS/Babylon.js/blob/0592b347b8a4ee0236089ea86a749cacfdb266d8/packages/dev/loaders/src/FBX/parsers/zlibInflate.ts#L4-L40","documentation":"inflateZlib requires at least 6 bytes (2-byte zlib header + 4-byte Adler-32 trailer) and throws 'zlib: unexpected end of input' when the compressed buffer is shorter. The compressed payload passed from parseArrayProperty is too small to be a valid zlib stream.","triggerScenarios":"Compressed array property whose compressedLength is < 6 (e.g., 0-5 bytes) after the FBX record header claimed compression (encoding 1); a slice taken at the wrong offset yielding a tiny buffer.","commonSituations":"Truncated FBX files where the compressed array was cut off; wrong byte offset when slicing the compressed region; a file where an array is marked compressed but contains only a stub.","solutions":["Verify the FBX file is complete (size/hash) and re-download/re-export if truncated","Check that parseArrayProperty slices the compressed data from the correct offset with the declared compressedLength","Validate the file with a reference FBX reader to confirm the compressed record is intact","Catch this error and report a corrupt-file error instead of a raw parser failure"],"exampleFix":"// before\nconst compressed = bytes.slice(offset, offset + len); inflateZlib(compressed, expected);\n// after\nif (len < 6) throw new Error(`Compressed FBX array truncated (${len} bytes)`);\nconst compressed = bytes.slice(offset, offset + len); inflateZlib(compressed, expected);","handlingStrategy":"validation","validationCode":"if (compressed.byteLength < 6) {\n  throw new Error(`FBX compressed array too small: ${compressed.byteLength} bytes`);\n}\ninflateZlib(compressed, expectedLength);","typeGuard":"function isPlausibleZlibBuffer(b: Uint8Array): boolean {\n  return b.byteLength >= 6;\n}","tryCatchPattern":"try {\n  return inflateZlib(compressed, expectedLength);\n} catch (e) {\n  if ((e as Error).message === 'zlib: unexpected end of input') {\n    throw new Error('Compressed FBX array payload truncated', { cause: e });\n  }\n  throw e;\n}","preventionTips":["Check compressedLength from the FBX header before slicing","Verify complete file downloads before parsing","Never slice compressed regions with hardcoded offsets"],"tags":["zlib","compression","truncated-data","fbx"],"backgroundTag":"unexpected-end-of-input","analyzedSha":"0592b347b8a4ee0236089ea86a749cacfdb266d8","analyzedAt":"2026-08-30T15:11:20.442Z","schemaVersion":2},"datasetVersion":"2026-08-30T18:17:15.746Z"}