{"record":{"id":"b58656b0054b3f20","repo":"gchq/CyberChef","slug":"invalid-marker-while-parsing-jpeg-at-pos-stream","errorCode":null,"errorMessage":"Invalid marker while parsing JPEG at pos ${stream.position}: ${marker}","messagePattern":"Invalid marker while parsing JPEG at pos (.+?): (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/core/lib/FileSignatures.mjs","lineNumber":2634,"sourceCode":"            extractor: null\n        }\n    ]\n};\n\n\n/**\n * JPEG extractor.\n *\n * @param {Uint8Array} bytes\n * @param {number} offset\n * @returns {Uint8Array}\n */\nexport function extractJPEG(bytes, offset) {\n    const stream = new Stream(bytes.slice(offset));\n\n    while (stream.hasMore()) {\n        const marker = stream.getBytes(2);\n        if (marker[0] !== 0xff) throw new Error(`Invalid marker while parsing JPEG at pos ${stream.position}: ${marker}`);\n\n        let segmentSize = 0;\n        switch (marker[1]) {\n            // No length\n            case 0xd8: // Start of Image\n            case 0x01: // For temporary use in arithmetic coding\n                break;\n            case 0xd9: // End found\n                return stream.carve();\n\n            // Variable size segment\n            case 0xc0: // Start of frame (Baseline DCT)\n            case 0xc1: // Start of frame (Extended sequential DCT)\n            case 0xc2: // Start of frame (Progressive DCT)\n            case 0xc3: // Start of frame (Lossless sequential)\n            case 0xc4: // Define Huffman Table\n            case 0xc5: // Start of frame (Differential sequential DCT)\n            case 0xc6: // Start of frame (Differential progressive DCT)","sourceCodeStart":2616,"sourceCodeEnd":2652,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/lib/FileSignatures.mjs#L2616-L2652","documentation":"Thrown by extractJPEG while walking JPEG segments. Every JPEG marker is two bytes beginning with 0xFF; if the first byte read is not 0xFF the stream is misaligned or the data is not JPEG. This throws a plain Error (not OperationError), so it surfaces as an internal/unexpected failure rather than recipe output.","triggerScenarios":"Calling extractJPEG(bytes, offset) on data that is not a JPEG, with an offset that lands mid-scan, or on a truncated/corrupted file where segment boundaries are wrong. Note the message interpolates `marker` (a Uint8Array), so the value prints as a comma-separated sequence like '0,1'.","commonSituations":"Wrong file type routed to the JPEG extractor; offset computed incorrectly by a caller; bit-flipped or crafted/malicious file; truncated download where a segment size jumped past real data.","solutions":["Confirm the input actually starts with the JPEG SOI marker (0xFFD8) before calling extractJPEG.","Verify the offset points at a real JPEG segment boundary (typically the SOI).","If the file is corrupt, re-acquire it or route it to the correct format extractor."],"exampleFix":"// before\nextractJPEG(arbitraryBytes, 0);\n\n// after\nif (arbitraryBytes[0] === 0xff && arbitraryBytes[1] === 0xd8) {\n    extractJPEG(arbitraryBytes, 0);\n}","handlingStrategy":"validation","validationCode":"function looksLikeJPEG(bytes, offset = 0) {\n  return bytes.length - offset >= 2 && bytes[offset] === 0xff && bytes[offset + 1] === 0xd8;\n}\nif (!looksLikeJPEG(bytes, offset)) throw new Error(\"Input is not a JPEG (no SOI marker)\");\nextractJPEG(bytes, offset);","typeGuard":"const isJPEGSOI = (bytes, offset = 0) =>\n  bytes.length - offset >= 2 && bytes[offset] === 0xff && bytes[offset + 1] === 0xd8;","tryCatchPattern":"try {\n  extractJPEG(bytes, offset);\n} catch (err) {\n  if (/Invalid marker while parsing JPEG/.test(err.message)) {\n    // not a JPEG or corrupt; route to a different extractor\n  } else throw err;\n}","preventionTips":["Check for the JPEG SOI marker (FFD8) before calling extractJPEG.","Make sure the caller computes the offset from a real JPEG segment, not arbitrary bytes.","Treat extractor throws as plain Error (they are not OperationError)."],"tags":["jpeg","file-format","parsing","corruption","file-signatures"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}