You're getting bitten by a quirk of Javascript bitwise operators. Let me demonstrate.
> 0xffffffff & 0xffe00000
-2097152
> 0xffe00000
4292870144
The result of a Javascript bitwise operation is always treated as a signed 32-bit integer. Since your constant is an unsigned 32-bit integer, it'll never match.
One potential fix is to use ~~
to perform the same mangling on your constant:
if ((header & 0xffe00000) == ~~0xffe00000) {
Another would be to just read in 16-bit integers instead, since the sync word will fit into that size anyway.
Unrelated but worth mentioning:
You're actually going to need to use
chunk.readUInt32BE(i)
here, not LE. Right now you're finding instances ofFE FF
in the stream, notFF FE
like you want.In the rare instance that a sync word spans two chunks, your code misses it.