LZ4 Legacy Decoder

May 5, 2025

LZ4 is a lossless data compression algorithm. As mentioned in the documentation, it is an LZ77 type compressor and is byte oriented. It does not have an entropy encoding stage, unlike other algorithms e.g. DEFLATE. Hence, it has a poorer compression ratio but significantly higher speed than DEFLATE (zlib). See blog by Meta.

The LZ4 format is a hierarchy of

A frame is composed of data blocks which in turn is composed of sequences. LZ4 has two frame formats viz. General and Legacy. The general frame format has a lot of metadata fields e.g. magic number, version number, maximum block size, checksums etc. The legacy format is extremely simple and is shown below. It contains just the magic number, followed by a repeating tuple of (LZ4 block size, LZ4 block). The magic number is a 4 byte value, 0x184C2102 in little endian. The LZ4 block size is also a 4 byte field, and indicates the size of the block in bytes. It's format is also little endian.