GIF: clean up reading of trailer block

This commit is contained in:
Evan Hahn 2023-11-09 09:27:23 -06:00
parent 40c7091386
commit 253b8e272c
1 changed files with 16 additions and 7 deletions

View File

@ -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 * @param {Uint8Array} bytes
* @returns {null | GifNode} The root node of the GIF tree, or null if the GIF is invalid. * @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; offset += sizeOfGlobalColorTableInBytes;
} }
readingBlocks: while (offset < bytes.byteLength) {
while (true) {
const blockType = bytes[offset]; const blockType = bytes[offset];
let readerFn; let readerFn;
@ -315,11 +327,8 @@ export default (bytes) => {
readerFn = readExtensionBlock; readerFn = readExtensionBlock;
break; break;
case BLOCK_TYPE_TRAILER: case BLOCK_TYPE_TRAILER:
children.push({ readerFn = readTrailerBlock;
type: GifNodeType.gifTerminator, break;
bytes: bytes.subarray(offset, offset + 1),
});
break readingBlocks;
default: default:
throw new Error(`Unknown GIF block type: ${blockType}`); throw new Error(`Unknown GIF block type: ${blockType}`);
} }