diff --git a/package.json b/package.json index b5cd56c..665098d 100644 --- a/package.json +++ b/package.json @@ -72,7 +72,7 @@ "scripts": { "lint": "eslint ./src/", "lint:fix": "eslint ./src/ --fix", - "build:watch": "cross-env NODE_ENV=development rollup --config rollup.config.js --watch --sourcemap inline", + "build:watch": "cross-env NODE_ENV=development rollup --config rollup.config.js --sourcemap inline --watch", "build:dev": "cross-env NODE_ENV=development rollup --config rollup.config.js --sourcemap inline", "build": "cross-env NODE_ENV=production rollup --config rollup.config.js", "test": "ava", diff --git a/src/index.test.js b/src/index.test.js index 6c89dbc..21522db 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -65,3 +65,18 @@ test('returns false when missing IDAT', (t) => { ), ) }) + +test('chunks should be found when preceded by a partial of themselves', (t) => { + t.true( + isApng( + new Uint8Array([ + // PNG header + 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, + // a acTL + 0x61, 0x61, 0x63, 0x54, 0x4c, + // I IDAT + 0x49, 0x49, 0x44, 0x41, 0x54, + ]), + ), + ) +}) diff --git a/src/index.ts b/src/index.ts index 2d94702..ace9376 100644 --- a/src/index.ts +++ b/src/index.ts @@ -39,22 +39,29 @@ export default function isApng(buffer: Buffer | Uint8Array): boolean { let firstIndex = 0 let secondIndex = 0 for (let i = 0; i < buffer.length; i++) { - if (buffer[i] === sequences.animationControlChunk[firstIndex]) { + if ( + !foundFirst && + (buffer[i] === sequences.animationControlChunk[firstIndex] || + (firstIndex > 0 && + ((firstIndex = 0) || + buffer[i] === sequences.animationControlChunk[firstIndex]))) + ) { firstIndex++ if (firstIndex === sequences.animationControlChunk.length) { foundFirst = true } - } else { - firstIndex = 0 } - if (buffer[i] === sequences.imageDataChunk[secondIndex]) { + if ( + buffer[i] === sequences.imageDataChunk[secondIndex] || + (secondIndex > 0 && + ((secondIndex = 0) || + buffer[i] === sequences.imageDataChunk[secondIndex])) + ) { secondIndex++ if (secondIndex === sequences.imageDataChunk.length) { return foundFirst } - } else { - secondIndex = 0 } }