Skip to content

Commit 83a07c9

Browse files
committed
Add ICO image to the file sniffer. Update the manifest to 1.0.11.
1 parent 73c4080 commit 83a07c9

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

codecs/codecs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export function getFullMIMEString(info) {
159159
}
160160
}
161161

162-
if (codecFrags.length === 0) return contentType;
162+
if (codecFrags.size === 0) return contentType;
163163
return contentType + '; codecs="' + Array.from(codecFrags).join(',') + '"';
164164
}
165165

file/sniffer.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,25 @@
1313
const fileSignatures = {
1414
// Document formats.
1515
'application/pdf': [[0x25, 0x50, 0x44, 0x46, 0x2d]],
16+
1617
// Archive formats:
1718
'application/x-tar': [
1819
[0x75, 0x73, 0x74, 0x61, 0x72, 0x00, 0x30, 0x30],
1920
[0x75, 0x73, 0x74, 0x61, 0x72, 0x20, 0x20, 0x00],
2021
],
21-
// Compressed archive formats.
2222
'application/x-7z-compressed': [[0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C]],
2323
'application/x-bzip2': [[0x42, 0x5A, 0x68]],
2424
'application/x-rar-compressed': [[0x52, 0x61, 0x72, 0x21, 0x1A, 0x07]],
2525
'application/zip': [[0x50, 0x4B, 0x03, 0x04], [0x50, 0x4B, 0x05, 0x06], [0x50, 0x4B, 0x07, 0x08]],
26+
2627
// Image formats.
2728
'image/bmp': [[0x42, 0x4D]],
2829
'image/gif': [[0x47, 0x49, 0x46, 0x38]],
2930
'image/jpeg': [[0xFF, 0xD8, 0xFF]],
3031
'image/png': [[0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]],
3132
'image/webp': [[0x52, 0x49, 0x46, 0x46, '??', '??', '??', '??', 0x57, 0x45, 0x42, 0x50]],
33+
'image/x-icon': [[0x00, 0x00, 0x01, 0x00]],
34+
3235
// Audio/Video formats.
3336
'application/ogg': [[0x4F, 0x67, 0x67, 0x53]],
3437
'audio/flac': [[0x66, 0x4C, 0x61, 0x43]],
@@ -77,8 +80,12 @@ export function initialize() {
7780
for (const byte of signature) {
7881
if (curNode.children[byte] === undefined) {
7982
if (byte === '??' && !curNode.children['??'] && Object.keys(curNode.children).length > 0) {
83+
// Reset the byte tree, it is bogus.
84+
root = null;
8085
throw 'Cannot add a placeholder child to a node that has non-placeholder children';
8186
} else if (byte !== '??' && curNode.children['??']) {
87+
// Reset the byte tree, it is bogus.
88+
root = null;
8289
throw 'Cannot add a non-placeholder child to a node that has a placeholder child';
8390
}
8491
curNode.children[byte] = new Node(byte);
@@ -114,6 +121,7 @@ export function findMimeType(ab) {
114121
const depth = ab.byteLength < maxDepth ? ab.byteLength : maxDepth;
115122
const arr = new Uint8Array(ab).subarray(0, depth);
116123
let curNode = root;
124+
let mimeType;
117125
// Step through bytes, updating curNode as it walks down the byte tree.
118126
for (const byte of arr) {
119127
// If this node has a placeholder child, just step into it.
@@ -123,6 +131,10 @@ export function findMimeType(ab) {
123131
}
124132
if (curNode.children[byte] === undefined) return undefined;
125133
curNode = curNode.children[byte];
126-
if (curNode.mimeType) return curNode.mimeType;
134+
if (curNode.mimeType) {
135+
mimeType = curNode.mimeType;
136+
break;
137+
}
127138
}
139+
return mimeType;
128140
}

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@codedread/bitjs",
3-
"version": "1.0.10",
3+
"version": "1.0.11",
44
"description": "Binary Tools for JavaScript",
55
"homepage": "https://github.com/codedread/bitjs",
66
"author": "Jeff Schiller",
@@ -33,18 +33,18 @@
3333
"bugs": {
3434
"url": "https://github.com/codedread/bitjs/issues"
3535
},
36+
"dependencies": {},
37+
"devDependencies": {
38+
"chai": "^4.3.4",
39+
"mocha": "^10.1.0",
40+
"typescript": "^4.8.0"
41+
},
3642
"directories": {
3743
"doc": "docs",
3844
"test": "tests"
3945
},
4046
"scripts": {
4147
"build-webpshim": "cd build; make",
4248
"test": "./node_modules/.bin/mocha tests/*.spec.js"
43-
},
44-
"devDependencies": {
45-
"chai": "^4.3.4",
46-
"mocha": "^10.1.0",
47-
"typescript": "^4.8.0"
48-
},
49-
"dependencies": {}
49+
}
5050
}

tests/file-sniffer.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe('bitjs.file.sniffer', () => {
2222
it('JPG', () => { sniffTest('image/jpeg', [0xFF, 0xD8, 0xFF, 0x23]); });
2323
it('PNG', () => { sniffTest('image/png', [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0xFF]); });
2424
it('WebP', () => { sniffTest('image/webp', [0x52, 0x49, 0x46, 0x46, 0x01, 0x02, 0x03, 0x04, 0x57, 0x45, 0x42, 0x50, 0x81]); });
25+
it('ICO', () => { sniffTest('image/x-icon', [0x00, 0x00, 0x01, 0x00, 0x69])});
2526
it('ZIP', () => { sniffTest('application/zip', [0x50, 0x4B, 0x03, 0x04, 0x20]); });
2627
it('ZIP_empty', () => { sniffTest('application/zip', [0x50, 0x4B, 0x05, 0x06, 0x20]); });
2728
it('ZIP_spanned', () => { sniffTest('application/zip', [0x50, 0x4B, 0x07, 0x08, 0x20]); });

0 commit comments

Comments
 (0)