10static constexpr uint8_t MAX_GAMMA_ZEROS = 15;
12static constexpr size_t LITERAL_BITS = 9;
13static constexpr uint16_t NO_POSITION = 0xFFFF;
15static size_t gamma_bits(
size_t value) {
return 2 * std::bit_width(
value) - 1; }
25 while (
count-- != 0) {
26 if (this->
used_ == 0) {
35 if (++this->
used_ == 8) {
42 size_t width = std::bit_width(
value);
43 this->put(0, width - 1);
44 this->put(
value, width);
46 bool is_full()
const {
return this->
full_; }
68 size_t left()
const {
return (this->end_ - this->pos_) * 8 - this->
used_; }
72 while (
count-- != 0) {
76 if (++this->used_ == 8) {
103 uint16_t *head = chains;
104 uint16_t *previous = chains + (
size_t{1} <<
HASH_BITS);
105 auto hash = [&](
size_t pos) {
108 auto insert = [&](
size_t pos) {
109 if (
pos + 1 >= total)
111 size_t h = hash(
pos);
115 std::fill_n(head,
size_t{1} <<
HASH_BITS, NO_POSITION);
121 while (i <
size && !writer.is_full()) {
122 size_t pos = this->dictionary_size_ + i;
124 size_t best_length = 0;
125 size_t best_offset = 0;
132 for (uint16_t entry = head[hash(
pos)]; tries-- != 0;) {
133 const size_t distance =
static_cast<uint16_t
>(
pos - entry);
135 if (distance <= last || distance > reach)
138 const size_t ref =
pos - distance;
143 if (
length > best_length) {
145 best_offset = distance;
154 if (best_length >= 2 && 1 + offset_bits + gamma_bits(best_length - 1) < LITERAL_BITS * best_length) {
156 writer.put(best_offset - 1, offset_bits);
157 writer.put_gamma(best_length - 1);
161 writer.put(in[i], 8);
167 return writer.finish();
171 BitReader reader(in,
size);
173 while (reader.left() != 0) {
174 if (reader.get(1) != 0) {
175 size_t left = reader.left();
178 return reader.get(left) == (1u << left) - 1 ?
length : SIZE_MAX;
189 if (reader.left() < offset_bits)
191 size_t distance = reader.get(offset_bits) + 1;
192 if (distance > behind)
196 if (reader.left() == 0 || zeros > MAX_GAMMA_ZEROS)
198 if (reader.get(1) != 0)
202 if (reader.left() < zeros)
204 size_t count = ((
size_t{1} << zeros) | reader.get(zeros)) + 1;
static constexpr size_t MAX_CHAIN
How many earlier positions the compressor tries for each match; more finds longer matches,...
static constexpr size_t MAX_MATCH
The longest match, as the decoder reads a length code of at most 16 bits after the first.
static constexpr uint8_t HASH_BITS
The compressor finds earlier positions starting with the same two bytes through a hash table of this ...
const uint8_t * dictionary_
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.
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...
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...
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...
std::vector< uint8_t > & out
std::span< const uint8_t > data
T clamp_at_most(T value, U max)
uint16_t uint16_t & capacity
uint8_t progmem_read_byte(const uint8_t *addr)