From 4f5e0c007d7a4f2b25e380b7fe474aa64312c9ff Mon Sep 17 00:00:00 2001 From: n1073645 Date: Thu, 7 Nov 2019 15:06:30 +0000 Subject: [PATCH] GIF extractor for GIF89a --- src/core/lib/FileSignatures.mjs | 34 +++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/core/lib/FileSignatures.mjs b/src/core/lib/FileSignatures.mjs index 7783fd6e..bcf3417f 100644 --- a/src/core/lib/FileSignatures.mjs +++ b/src/core/lib/FileSignatures.mjs @@ -40,7 +40,7 @@ export const FILE_SIGNATURES = { 4: [0x37, 0x39], // 7|9 5: 0x61 // a }, - extractor: null + extractor: extractGIF }, { name: "Portable Network Graphics image", @@ -2440,6 +2440,36 @@ export function extractJPEG(bytes, offset) { } +/** + * GIF extractor. + * + * @param {Uint8Array} bytes + * @param {Number} offset + * @returns {Uint8Array} + */ +export function extractGIF(bytes, offset) { + const stream = new Stream(bytes.slice(offset)); + stream.continueUntil([0x21, 0xff]); + stream.continueUntil([0x21, 0xf9]); + while (stream.hasMore()) { + stream.continueUntil(0x2c); + stream.moveForwardsBy(11); + while (stream.getBytes(2) !== [0x21, 0xf9]) { + stream.moveBackwardsBy(2); + stream.moveForwardsBy(stream.getBytes(1)[0]); + if (!stream.getBytes(1)[0]) + break; + stream.moveBackwardsBy(1); + } + if (stream.getBytes(1)[0] === 0x3b) + break; + stream.moveBackwardsBy(1); + } + stream.moveBackwardsBy(10); + return stream.carve(); +} + + /** * Portable executable extractor. * Assumes that the offset refers to an MZ header. @@ -2809,7 +2839,7 @@ export function extractZlib(bytes, offset) { /** - * XZ extractor + * XZ extractor. * * @param {Uint8Array} bytes * @param {Number} offset