ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
esphome::lzss::Lzss Class Reference

LZSS compression of a block of data on its own, with a preset dictionary: text that both ends hold, which the block may copy from as if it came just before the block. More...

#include <lzss.h>

Public Member Functions

 Lzss (const uint8_t *dictionary, size_t dictionary_size)
 
size_t dictionary_size () const
 
size_t compress (const uint8_t *in, size_t size, uint8_t *out, size_t capacity, uint16_t *chains) const
 Compress size bytes into out, which holds capacity bytes, using chains (CHAIN_WORDS words) as working space; returns the length, or 0 if it does not fit.
 
size_t expand (const uint8_t *in, size_t size, uint8_t *out, size_t capacity) const
 Expand size bytes into out, which holds capacity bytes; returns the length, or SIZE_MAX if the data is malformed or expands to more than capacity.
 

Static Public Attributes

static constexpr uint8_t HASH_BITS = 8
 The compressor finds earlier positions starting with the same two bytes through a hash table of this many bits.
 
static constexpr size_t MAX_CHAIN = 32
 How many earlier positions the compressor tries for each match; more finds longer matches, but takes longer.
 
static constexpr size_t WINDOW = 512
 Part of the format: a match reaches back at most this far, and offsets take the bits that the bytes behind the current position need, counting at most this many; a power of 2.
 
static constexpr size_t MAX_MATCH = 65536
 The longest match, as the decoder reads a length code of at most 16 bits after the first.
 
static constexpr size_t CHAIN_WORDS = (size_t{1} << HASH_BITS) + WINDOW
 The working space compress() needs: the latest position for each hash, then the previous one for each position in the window.
 

Protected Member Functions

uint8_t byte_at_ (const uint8_t *data, size_t pos) const
 The byte at pos counting from the start of the dictionary, with data following it.
 

Protected Attributes

const uint8_t * dictionary_
 
size_t dictionary_size_
 

Detailed Description

LZSS compression of a block of data on its own, with a preset dictionary: text that both ends hold, which the block may copy from as if it came just before the block.

Short blocks gain most from a dictionary that looks like them.

The compressed data is a stream of bits, the first in bit 7 of each byte:

  • a 1 bit and 8 bits of a byte to copy;
  • a 0 bit, an offset back from the current position, and a length from 2 to MAX_MATCH. The offset less 1 takes as many bits as the number of bytes behind the current position, counting the dictionary but at most WINDOW, less 1 needs, so near the start of a block it takes few bits. The length less 1 is an Elias gamma code: one 0 bit for each bit after the first, then the value. The last byte is padded with 1 bits, which read as the start of a byte to copy with too few bits left.

Definition at line 21 of file lzss.h.

Constructor & Destructor Documentation

◆ Lzss()

esphome::lzss::Lzss::Lzss ( const uint8_t * dictionary,
size_t dictionary_size )
inline
Parameters
dictionaryThe preset dictionary, in flash; may be null when dictionary_size is 0

Definition at line 37 of file lzss.h.

Member Function Documentation

◆ byte_at_()

uint8_t esphome::lzss::Lzss::byte_at_ ( const uint8_t * data,
size_t pos ) const
protected

The byte at pos counting from the start of the dictionary, with data following it.

Definition at line 94 of file lzss.cpp.

◆ compress()

size_t esphome::lzss::Lzss::compress ( const uint8_t * in,
size_t size,
uint8_t * out,
size_t capacity,
uint16_t * chains ) const

Compress size bytes into out, which holds capacity bytes, using chains (CHAIN_WORDS words) as working space; returns the length, or 0 if it does not fit.

Definition at line 98 of file lzss.cpp.

◆ dictionary_size()

size_t esphome::lzss::Lzss::dictionary_size ( ) const
inline

Definition at line 40 of file lzss.h.

◆ expand()

size_t esphome::lzss::Lzss::expand ( const uint8_t * in,
size_t size,
uint8_t * out,
size_t capacity ) const

Expand size bytes into out, which holds capacity bytes; returns the length, or SIZE_MAX if the data is malformed or expands to more than capacity.

Definition at line 170 of file lzss.cpp.

Field Documentation

◆ CHAIN_WORDS

size_t esphome::lzss::Lzss::CHAIN_WORDS = (size_t{1} << HASH_BITS) + WINDOW
staticconstexpr

The working space compress() needs: the latest position for each hash, then the previous one for each position in the window.

Definition at line 34 of file lzss.h.

◆ dictionary_

const uint8_t* esphome::lzss::Lzss::dictionary_
protected

Definition at line 53 of file lzss.h.

◆ dictionary_size_

size_t esphome::lzss::Lzss::dictionary_size_
protected

Definition at line 54 of file lzss.h.

◆ HASH_BITS

uint8_t esphome::lzss::Lzss::HASH_BITS = 8
staticconstexpr

The compressor finds earlier positions starting with the same two bytes through a hash table of this many bits.

Definition at line 24 of file lzss.h.

◆ MAX_CHAIN

size_t esphome::lzss::Lzss::MAX_CHAIN = 32
staticconstexpr

How many earlier positions the compressor tries for each match; more finds longer matches, but takes longer.

Definition at line 26 of file lzss.h.

◆ MAX_MATCH

size_t esphome::lzss::Lzss::MAX_MATCH = 65536
staticconstexpr

The longest match, as the decoder reads a length code of at most 16 bits after the first.

Definition at line 31 of file lzss.h.

◆ WINDOW

size_t esphome::lzss::Lzss::WINDOW = 512
staticconstexpr

Part of the format: a match reaches back at most this far, and offsets take the bits that the bytes behind the current position need, counting at most this many; a power of 2.

Definition at line 29 of file lzss.h.


The documentation for this class was generated from the following files: