From 253b8e272c85ad5ea2d9c5880ec99d89b4469054 Mon Sep 17 00:00:00 2001 From: Evan Hahn Date: Thu, 9 Nov 2023 09:27:23 -0600 Subject: [PATCH] GIF: clean up reading of trailer block --- public/gif/parseGif.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/public/gif/parseGif.js b/public/gif/parseGif.js index d4ae67d..0314c77 100644 --- a/public/gif/parseGif.js +++ b/public/gif/parseGif.js @@ -226,6 +226,19 @@ const readExtensionBlock = (bytes, startingOffset) => { } }; +/** + * @param {Uint8Array} bytes + * @param {number} offset + * @returns {{ node: GifNode, newOffset: number }} + */ +const readTrailerBlock = (bytes, offset) => ({ + newOffset: offset + 1, + node: { + type: GifNodeType.gifTerminator, + bytes: bytes.subarray(offset, offset + 1), + }, +}); + /** * @param {Uint8Array} bytes * @returns {null | GifNode} The root node of the GIF tree, or null if the GIF is invalid. @@ -302,8 +315,7 @@ export default (bytes) => { offset += sizeOfGlobalColorTableInBytes; } - readingBlocks: - while (true) { + while (offset < bytes.byteLength) { const blockType = bytes[offset]; let readerFn; @@ -315,11 +327,8 @@ export default (bytes) => { readerFn = readExtensionBlock; break; case BLOCK_TYPE_TRAILER: - children.push({ - type: GifNodeType.gifTerminator, - bytes: bytes.subarray(offset, offset + 1), - }); - break readingBlocks; + readerFn = readTrailerBlock; + break; default: throw new Error(`Unknown GIF block type: ${blockType}`); }