@mega-yfue/eufy-sdk / lz4BlockDecompress
Function: lz4BlockDecompress()
function lz4BlockDecompress(block, size): Buffer<ArrayBufferLike> | undefined;Decompress an LZ4 block into exactly size bytes, or undefined if the block is malformed.
Every failure is undefined, and the length check is what makes that trustworthy. A truncated or corrupt block does not fail loudly on its own — LZ4 has no checksum and no terminator, so a mangled block usually just stops producing output early, and a decompressor that returned what it had would hand back a plausible, short, wrong map. Requiring the output to reach size exactly turns that into a rejection, and it costs nothing: the caller already knows the size, which is how it knew to call.
The other rejections are the reads that would otherwise wander: a length that runs off the end of the block, a back-reference pointing before the start of the output or nowhere at all, and a match that would overrun size.
Matches may overlap their own output — an offset of 1 with a length of 200 is how a run of one repeated byte is encoded, and it is the common case in a mostly-empty map. That is why the copy below is a byte-at-a-time loop and not a block copy: the bytes it reads are ones it just wrote.
Parameters
block
Uint8Array
size
number
Returns
Buffer<ArrayBufferLike> | undefined