Purpose

mozlz4 is a file format used by Mozilla Firefox to store some browser data in a compressed format. This is mainly used for storing bookmarks and the search engines available in the search bar.

As it can be seen from the name, the compression algorithm used is LZ4. In fact, mozlz4 is just a very thin wrapper around the LZ4 block format.

Format

The format is very simple. It consists of a 8-byte magic number, the length of the data, and the compressed data itself in the LZ4 block format.

The magic number is mozLz40\0, or 6d6f7a4c7a343000 in hex.

The length of the data is stored as a 32-bit little-endian integer.

The compressed data is stored in the LZ4 block format. From my testing, the Python LZ4 library is able to both compress and decompress the data without any issues.

Python example

I use the following code snippets to compress and decompress data in the mozlz4 format. Usually this is not needed very often, but it is useful for occasionally interacting with the data stored in the Firefox profile.

Decompressing

import lz4.block
import sys

with open('file.mozlz4', 'rb') as f:
    magic = f.read(8)
    assert magic == b'mozLz40\0'
    l = int.from_bytes(f.read(4), 'little')
    decompressed = lz4.block.decompress(f.read(), uncompressed_size=l)
    assert len(decompressed) == l
    sys.stdout.buffer.write(decompressed)
    sys.stdout.buffer.flush()

Compressing

import lz4.block
import sys

with open('file', 'rb') as f:
    data = f.read()
    compressed = lz4.block.compress(data, store_size=False)
    sys.stdout.buffer.write(b'mozLz40\0')
    sys.stdout.buffer.write(len(data).to_bytes(4, 'little'))
    sys.stdout.buffer.write(compressed)
    sys.stdout.buffer.flush()