0

Here's my logic to seek through a file inside a binary:

blob.arrayBuffer().then(arrayBuffer => {
    const uint8Array = new Uint8Array(arrayBuffer);
    for (let start = 0; start <= uint8Array.byteLength; start++) {
        if ((uint8Array[start] & 0xff) === 0xff &&
            (uint8Array[start + 1] & 0xff) === 0xd8 &&
            (uint8Array[start + 2] & 0xff) === 0xff &&
            (uint8Array[start + 3] & 0xff) === 0xe0 &&
            1) {
            // Found start of file
            for (let i = start; i <= (uint8Array.byteLength - start); i++) {
                if ((uint8Array[i] & 0xff) === 0xff &&
                    (uint8Array[i + 1] & 0xff) === 0xd9 &&
                    1) {
                    // Found ending of file
                    const offsetIdx = start;
                    const endIdx = (i + 1);
                    process(offsetIdx, endIdx);
                    break;
                }
            }
        }
    }
});

The above code searches files inside a binary by looking for the indexes (start, ending markers). The question is what is the fastest way to get the start and end index of a files inside a binary file? Or this this algorithm I have is the fastest one possible already?

quarks
  • 33,478
  • 73
  • 290
  • 513
  • `start <= uint8Array.byteLength` should be `start < uint8Array.byteLength - 4` – Barmar Apr 15 '20 at 20:12
  • Also see https://codereview.stackexchange.com/questions/20136/uint8array-indexof-method-that-allows-to-search-for-byte-sequences for commentary on the code in the linked question. – Barmar Apr 15 '20 at 20:18
  • The linked question's answer is not great, using `Array.indexOf` instead of `TypedArray.indexOf` (probably justified given the age) and using tail recursion ([not safe on most runtimes](https://stackoverflow.com/questions/37224520/are-functions-in-javascript-tail-call-optimized/37224563#37224563)). – Veedrac Apr 16 '20 at 06:50
  • 1
    This post's code also had an out-of-bounds bug, if `start + 1` (or `+ 2` or `+ 3`) is out of bounds. I suspect the second loop is also incorrect. – Veedrac Apr 16 '20 at 07:00
  • @Veedrac I've added the `uint8Array.byteLength - 4` because of this, is this what you mean? – quarks Apr 16 '20 at 07:08
  • Oh, yeah, I didn't read the other comment. The second loop still looks like it has a buggy end condition. – Veedrac Apr 16 '20 at 07:09
  • @Veedrac which part of it do you think has issues? I'd like to understand. – quarks Apr 16 '20 at 07:10
  • Why are you doing `i <= (uint8Array.byteLength - start)`? I'd have thought you'd want `i < uint8Array.byteLength - 1`. – Veedrac Apr 16 '20 at 07:12

0 Answers0