diff --git a/file/sniffer.js b/file/sniffer.js new file mode 100644 index 0000000..4696850 --- /dev/null +++ b/file/sniffer.js @@ -0,0 +1,63 @@ +/** + * File Sniffer. + * + * Licensed under the MIT License + * + * Copyright(c) 2020 Google Inc. + */ + +const fileSignatures = { + 'image/png': [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A], +}; + +// TODO: Build a tree of signature bytes that end in a MIME type. +class Node { + /** + * @param {number} value + */ + constructor(value) { + this.value = value; + this.children = {}; + this.mimeType = undefined; + } +} + +// Top-level node in the tree. +const root = new Node(); +let maxDepth = 0; + +// Construct the tree. +(function() { + for (const mimeType in fileSignatures) { + let curNode = root; + let depth = 0; + for (const byte of fileSignatures[mimeType]) { + if (curNode.children[byte] === undefined) { + curNode.children[byte] = new Node(byte); + } + depth++; + curNode = curNode.children[byte]; + } + if (maxDepth < depth) { + maxDepth = depth; + } + curNode.mimeType = mimeType; + } +})(); + +/** + * @param {ArrayBuffer} ab + * @return {string} The MIME type of the buffer, or undefined. + */ +export function findMimeType(ab) { + const depth = ab.byteLength < maxDepth ? ab.byteLength : maxDepth; + const arr = new Uint8Array(ab).subarray(0, depth); + let curNode = root; + for (const byte of arr) { + if (curNode.children[byte] === undefined) return undefined; + curNode = curNode.children[byte]; + if (curNode.mimeType) return curNode.mimeType; + } +} + +debugger; \ No newline at end of file diff --git a/tests/file-sniffer-test.html b/tests/file-sniffer-test.html new file mode 100644 index 0000000..851f693 --- /dev/null +++ b/tests/file-sniffer-test.html @@ -0,0 +1,27 @@ + + + + Unit tests for bitjs.io.ByteStreamBitStream + + + + \ No newline at end of file