ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
lzss.cpp
Go to the documentation of this file.
1#include "lzss.h"
2#include <algorithm>
3#include <bit>
4#include "esphome/core/hal.h"
6
7namespace esphome::lzss {
8
9// Gamma codes up to this many bits after the first, so lengths up to 65536
10static constexpr uint8_t MAX_GAMMA_ZEROS = 15;
11static_assert(Lzss::MAX_MATCH == size_t{1} << (MAX_GAMMA_ZEROS + 1));
12static constexpr size_t LITERAL_BITS = 9;
13static constexpr uint16_t NO_POSITION = 0xFFFF;
14
15static size_t gamma_bits(size_t value) { return 2 * std::bit_width(value) - 1; }
16
17namespace {
18
19// Writes bits from bit 7 of each byte, and stops writing once the output is full
20class BitWriter {
21 public:
22 BitWriter(uint8_t *out, size_t capacity) : out_(out), capacity_(capacity) {}
23
24 void put(uint32_t value, size_t count) {
25 while (count-- != 0) {
26 if (this->used_ == 0) {
27 if (this->length_ == this->capacity_) {
28 this->full_ = true;
29 return;
30 }
31 this->out_[this->length_] = 0;
32 }
33 if ((value >> count) & 1)
34 this->out_[this->length_] |= 0x80 >> this->used_;
35 if (++this->used_ == 8) {
36 this->used_ = 0;
37 this->length_++;
38 }
39 }
40 }
41 void put_gamma(uint32_t value) {
42 size_t width = std::bit_width(value);
43 this->put(0, width - 1);
44 this->put(value, width);
45 }
46 bool is_full() const { return this->full_; }
48 size_t finish() {
49 if (this->full_)
50 return 0;
51 if (this->used_ != 0)
52 this->out_[this->length_++] |= 0xFF >> this->used_;
53 return this->length_;
54 }
55
56 protected:
57 uint8_t *out_;
58 size_t capacity_;
59 size_t length_{0};
60 uint8_t used_{0};
61 bool full_{false};
62};
63
64class BitReader {
65 public:
66 BitReader(const uint8_t *data, size_t size) : data_(data), end_(size) {}
67
68 size_t left() const { return (this->end_ - this->pos_) * 8 - this->used_; }
70 uint32_t get(size_t count) {
71 uint32_t value = 0;
72 while (count-- != 0) {
73 if (this->used_ == 0)
74 this->byte_ = this->data_[this->pos_];
75 value = value << 1 | ((this->byte_ >> (7 - this->used_)) & 1);
76 if (++this->used_ == 8) {
77 this->used_ = 0;
78 this->pos_++;
79 }
80 }
81 return value;
82 }
83
84 protected:
85 const uint8_t *data_;
86 size_t pos_{0};
87 size_t end_;
88 uint8_t byte_{0};
89 uint8_t used_{0};
90};
91
92} // namespace
93
94uint8_t Lzss::byte_at_(const uint8_t *data, size_t pos) const {
95 return pos < this->dictionary_size_ ? progmem_read_byte(this->dictionary_ + pos) : data[pos - this->dictionary_size_];
96}
97
98size_t Lzss::compress(const uint8_t *in, size_t size, uint8_t *out, size_t capacity, uint16_t *chains) const {
99 BitWriter writer(out, capacity);
100 size_t total = this->dictionary_size_ + size;
101 // For each hash, the latest position with it; for each of the last WINDOW positions, the one before it with the
102 // same hash
103 uint16_t *head = chains;
104 uint16_t *previous = chains + (size_t{1} << HASH_BITS);
105 auto hash = [&](size_t pos) {
106 return ((this->byte_at_(in, pos) << 8 | this->byte_at_(in, pos + 1)) * 0x9E3779B1u) >> (32 - HASH_BITS);
107 };
108 auto insert = [&](size_t pos) {
109 if (pos + 1 >= total)
110 return;
111 size_t h = hash(pos);
112 previous[pos & (WINDOW - 1)] = head[h];
113 head[h] = pos;
114 };
115 std::fill_n(head, size_t{1} << HASH_BITS, NO_POSITION);
116 // A match reaches back at most WINDOW bytes, so earlier dictionary bytes are never used
117 for (size_t pos = this->dictionary_size_ > WINDOW ? this->dictionary_size_ - WINDOW : 0; pos < this->dictionary_size_;
118 pos++)
119 insert(pos);
120 size_t i = 0;
121 while (i < size && !writer.is_full()) {
122 size_t pos = this->dictionary_size_ + i;
123 size_t longest = clamp_at_most(size - i, MAX_MATCH);
124 size_t best_length = 0;
125 size_t best_offset = 0;
126 if (longest >= 2) {
127 // The chain runs from the nearest, so of two matches of the same length the nearer is kept. Positions are
128 // kept modulo 65536, so each entry gives only a distance, and a stale one still names bytes that are compared
129 const size_t reach = clamp_at_most(pos, WINDOW);
130 size_t last = 0;
131 size_t tries = MAX_CHAIN;
132 for (uint16_t entry = head[hash(pos)]; tries-- != 0;) {
133 const size_t distance = static_cast<uint16_t>(pos - entry);
134 // A chain only reaches further back, so a nearer entry is a slot reused by a later position
135 if (distance <= last || distance > reach)
136 break;
137 last = distance;
138 const size_t ref = pos - distance;
139 size_t length = 0;
140 // A match may run on past pos, copying bytes the expander has just written
141 while (length < longest && this->byte_at_(in, ref + length) == in[i + length])
142 length++;
143 if (length > best_length) {
144 best_length = length;
145 best_offset = distance;
146 if (length == longest)
147 break;
148 }
149 entry = previous[ref & (WINDOW - 1)];
150 }
151 }
152 size_t step = 1;
153 const size_t offset_bits = std::bit_width(clamp_at_most(pos, WINDOW) - 1);
154 if (best_length >= 2 && 1 + offset_bits + gamma_bits(best_length - 1) < LITERAL_BITS * best_length) {
155 writer.put(0, 1);
156 writer.put(best_offset - 1, offset_bits);
157 writer.put_gamma(best_length - 1);
158 step = best_length;
159 } else {
160 writer.put(1, 1);
161 writer.put(in[i], 8);
162 }
163 for (size_t end = pos + step; pos != end; pos++)
164 insert(pos);
165 i += step;
166 }
167 return writer.finish();
168}
169
170size_t Lzss::expand(const uint8_t *in, size_t size, uint8_t *out, size_t capacity) const {
171 BitReader reader(in, size);
172 size_t length = 0;
173 while (reader.left() != 0) {
174 if (reader.get(1) != 0) {
175 size_t left = reader.left();
176 if (left < 8) {
177 // The padding: the rest must be 1 bits
178 return reader.get(left) == (1u << left) - 1 ? length : SIZE_MAX;
179 }
180 if (length == capacity)
181 return SIZE_MAX;
182 out[length++] = reader.get(8);
183 continue;
184 }
185 size_t behind = this->dictionary_size_ + length;
186 if (behind == 0)
187 return SIZE_MAX;
188 size_t offset_bits = std::bit_width(clamp_at_most(behind, WINDOW) - 1);
189 if (reader.left() < offset_bits)
190 return SIZE_MAX;
191 size_t distance = reader.get(offset_bits) + 1;
192 if (distance > behind)
193 return SIZE_MAX;
194 uint8_t zeros = 0;
195 for (;;) {
196 if (reader.left() == 0 || zeros > MAX_GAMMA_ZEROS)
197 return SIZE_MAX;
198 if (reader.get(1) != 0)
199 break;
200 zeros++;
201 }
202 if (reader.left() < zeros)
203 return SIZE_MAX;
204 size_t count = ((size_t{1} << zeros) | reader.get(zeros)) + 1;
205 if (count > capacity - length)
206 return SIZE_MAX;
207 // One byte at a time, as the match may overlap the bytes it is writing
208 for (; count != 0; count--, length++)
209 out[length] = this->byte_at_(out, this->dictionary_size_ + length - distance);
210 }
211 return length;
212}
213
214} // namespace esphome::lzss
uint8_t h
Definition bl0906.h:2
static constexpr size_t MAX_CHAIN
How many earlier positions the compressor tries for each match; more finds longer matches,...
Definition lzss.h:26
static constexpr size_t MAX_MATCH
The longest match, as the decoder reads a length code of at most 16 bits after the first.
Definition lzss.h:31
static constexpr uint8_t HASH_BITS
The compressor finds earlier positions starting with the same two bytes through a hash table of this ...
Definition lzss.h:24
const uint8_t * dictionary_
Definition lzss.h:53
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.
Definition lzss.cpp:94
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...
Definition lzss.cpp:98
static constexpr size_t WINDOW
Part of the format: a match reaches back at most this far, and offsets take the bits that the bytes b...
Definition lzss.h:29
size_t dictionary_size_
Definition lzss.h:54
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 i...
Definition lzss.cpp:170
StringRef ref
std::vector< uint8_t > & out
Definition gif.cpp:49
unsigned count
Definition gif.cpp:51
uint8_t byte_
Definition lzss.cpp:88
uint8_t used_
Definition lzss.cpp:60
size_t capacity_
Definition lzss.cpp:58
uint8_t * out_
Definition lzss.cpp:57
bool full_
Definition lzss.cpp:61
size_t length_
Definition lzss.cpp:59
const uint8_t * data_
Definition lzss.cpp:85
std::span< const uint8_t > data
T clamp_at_most(T value, U max)
Definition helpers.h:2859
uint32_t value
Definition hal.cpp:59
uint16_t size
Definition helpers.cpp:44
size_t size_t pos
Definition helpers.h:1530
uint16_t uint16_t & capacity
Definition helpers.cpp:44
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:51
const char * end_
Definition polygon.cpp:55
const char * pos_
Definition polygon.cpp:54
static void uint32_t
uint8_t end[39]
Definition sun_gtil2.cpp:17
uint16_t length
Definition tt21100.cpp:0