From 83a07c970d81693b22fce6a0cb56c50d50163f43 Mon Sep 17 00:00:00 2001 From: Jeff Schiller Date: Wed, 15 Feb 2023 21:33:38 -0800 Subject: [PATCH] Add ICO image to the file sniffer. Update the manifest to 1.0.11. --- codecs/codecs.js | 2 +- file/sniffer.js | 16 ++++++++++++++-- package.json | 16 ++++++++-------- tests/file-sniffer.spec.js | 1 + 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/codecs/codecs.js b/codecs/codecs.js index c41e6b3..7329860 100644 --- a/codecs/codecs.js +++ b/codecs/codecs.js @@ -159,7 +159,7 @@ export function getFullMIMEString(info) { } } - if (codecFrags.length === 0) return contentType; + if (codecFrags.size === 0) return contentType; return contentType + '; codecs="' + Array.from(codecFrags).join(',') + '"'; } diff --git a/file/sniffer.js b/file/sniffer.js index 85ff1db..7218fd9 100644 --- a/file/sniffer.js +++ b/file/sniffer.js @@ -13,22 +13,25 @@ const fileSignatures = { // Document formats. 'application/pdf': [[0x25, 0x50, 0x44, 0x46, 0x2d]], + // Archive formats: 'application/x-tar': [ [0x75, 0x73, 0x74, 0x61, 0x72, 0x00, 0x30, 0x30], [0x75, 0x73, 0x74, 0x61, 0x72, 0x20, 0x20, 0x00], ], - // Compressed archive formats. 'application/x-7z-compressed': [[0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C]], 'application/x-bzip2': [[0x42, 0x5A, 0x68]], 'application/x-rar-compressed': [[0x52, 0x61, 0x72, 0x21, 0x1A, 0x07]], 'application/zip': [[0x50, 0x4B, 0x03, 0x04], [0x50, 0x4B, 0x05, 0x06], [0x50, 0x4B, 0x07, 0x08]], + // Image formats. 'image/bmp': [[0x42, 0x4D]], 'image/gif': [[0x47, 0x49, 0x46, 0x38]], 'image/jpeg': [[0xFF, 0xD8, 0xFF]], 'image/png': [[0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]], 'image/webp': [[0x52, 0x49, 0x46, 0x46, '??', '??', '??', '??', 0x57, 0x45, 0x42, 0x50]], + 'image/x-icon': [[0x00, 0x00, 0x01, 0x00]], + // Audio/Video formats. 'application/ogg': [[0x4F, 0x67, 0x67, 0x53]], 'audio/flac': [[0x66, 0x4C, 0x61, 0x43]], @@ -77,8 +80,12 @@ export function initialize() { for (const byte of signature) { if (curNode.children[byte] === undefined) { if (byte === '??' && !curNode.children['??'] && Object.keys(curNode.children).length > 0) { + // Reset the byte tree, it is bogus. + root = null; throw 'Cannot add a placeholder child to a node that has non-placeholder children'; } else if (byte !== '??' && curNode.children['??']) { + // Reset the byte tree, it is bogus. + root = null; throw 'Cannot add a non-placeholder child to a node that has a placeholder child'; } curNode.children[byte] = new Node(byte); @@ -114,6 +121,7 @@ export function findMimeType(ab) { const depth = ab.byteLength < maxDepth ? ab.byteLength : maxDepth; const arr = new Uint8Array(ab).subarray(0, depth); let curNode = root; + let mimeType; // Step through bytes, updating curNode as it walks down the byte tree. for (const byte of arr) { // If this node has a placeholder child, just step into it. @@ -123,6 +131,10 @@ export function findMimeType(ab) { } if (curNode.children[byte] === undefined) return undefined; curNode = curNode.children[byte]; - if (curNode.mimeType) return curNode.mimeType; + if (curNode.mimeType) { + mimeType = curNode.mimeType; + break; + } } + return mimeType; } diff --git a/package.json b/package.json index 735fe5e..6d0cbfd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@codedread/bitjs", - "version": "1.0.10", + "version": "1.0.11", "description": "Binary Tools for JavaScript", "homepage": "https://github.com/codedread/bitjs", "author": "Jeff Schiller", @@ -33,6 +33,12 @@ "bugs": { "url": "https://github.com/codedread/bitjs/issues" }, + "dependencies": {}, + "devDependencies": { + "chai": "^4.3.4", + "mocha": "^10.1.0", + "typescript": "^4.8.0" + }, "directories": { "doc": "docs", "test": "tests" @@ -40,11 +46,5 @@ "scripts": { "build-webpshim": "cd build; make", "test": "./node_modules/.bin/mocha tests/*.spec.js" - }, - "devDependencies": { - "chai": "^4.3.4", - "mocha": "^10.1.0", - "typescript": "^4.8.0" - }, - "dependencies": {} + } } diff --git a/tests/file-sniffer.spec.js b/tests/file-sniffer.spec.js index 00224c9..b24d6d6 100644 --- a/tests/file-sniffer.spec.js +++ b/tests/file-sniffer.spec.js @@ -22,6 +22,7 @@ describe('bitjs.file.sniffer', () => { it('JPG', () => { sniffTest('image/jpeg', [0xFF, 0xD8, 0xFF, 0x23]); }); it('PNG', () => { sniffTest('image/png', [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0xFF]); }); it('WebP', () => { sniffTest('image/webp', [0x52, 0x49, 0x46, 0x46, 0x01, 0x02, 0x03, 0x04, 0x57, 0x45, 0x42, 0x50, 0x81]); }); + it('ICO', () => { sniffTest('image/x-icon', [0x00, 0x00, 0x01, 0x00, 0x69])}); it('ZIP', () => { sniffTest('application/zip', [0x50, 0x4B, 0x03, 0x04, 0x20]); }); it('ZIP_empty', () => { sniffTest('application/zip', [0x50, 0x4B, 0x05, 0x06, 0x20]); }); it('ZIP_spanned', () => { sniffTest('application/zip', [0x50, 0x4B, 0x07, 0x08, 0x20]); });