82 std::uint8_t minimum_length = 21U;
83 std::uint8_t maximum_length = 0U;
84 for (std::uint8_t length : lengths)
86 if (length == 0U || length > 20U)
90 minimum_length = std::min(minimum_length, length);
91 maximum_length = std::max(maximum_length, length);
95 std::uint32_t code = 0U;
96 for (std::uint8_t length = minimum_length; length <= maximum_length; ++length)
98 if (length != minimum_length)
102 for (std::size_t symbol = 0U; symbol < lengths.size(); ++symbol)
104 if (lengths[symbol] != length)
108 if (code >= (std::uint32_t{1U} << length))
112 std::size_t node_index = 0U;
113 for (std::uint8_t depth = 0U; depth < length; ++depth)
115 if (table.nodes[node_index].symbol.has_value())
119 std::uint8_t
const bit =
static_cast< std::uint8_t
>((code >> (length - depth - 1U)) & 1U);
120 std::optional< std::uint16_t >& child = bit == 0U ? table.nodes[node_index].zero_child : table.nodes[node_index].one_child;
121 if (!child.has_value())
123 if (table.nodes.size() >= std::numeric_limits< std::uint16_t >::max())
127 std::uint16_t
const new_node =
static_cast< std::uint16_t
>(table.nodes.size());
129 node_index = new_node;
137 if (table.nodes[node_index].symbol.has_value() || table.nodes[node_index].zero_child.has_value() || table.nodes[node_index].one_child.has_value())
141 table.nodes[node_index].symbol =
static_cast< std::uint16_t
>(symbol);
180 std::vector< std::byte > output;
181 output.reserve(input.size());
182 std::size_t position = 0U;
183 while (position < input.size())
185 std::size_t run_end = position + 1U;
186 while (run_end < input.size() && input[run_end] == input[position])
190 std::size_t remaining = run_end - position;
191 while (remaining != 0U)
193 std::size_t
const chunk_size = std::min< std::size_t >(remaining, 259U);
196 output.insert(output.end(), chunk_size, input[position]);
200 output.insert(output.end(), 4U, input[position]);
201 output.push_back(
static_cast< std::byte
>(chunk_size - 4U));
203 remaining -= chunk_size;
222 std::size_t
const size = input.size();
223 std::vector< std::size_t > order(size, 0U);
224 std::vector< std::size_t > classes(size, 0U);
225 std::array< std::size_t, 256U > byte_counts{};
226 for (std::byte value : input)
228 ++byte_counts[std::to_integer< std::uint8_t >(value)];
230 std::array< std::size_t, 256U > byte_positions{};
231 for (std::size_t
byte = 1U;
byte < byte_positions.size(); ++byte)
233 byte_positions[byte] = byte_positions[
byte - 1U] + byte_counts[
byte - 1U];
235 for (std::size_t index = 0U; index < size; ++index)
237 std::uint8_t
const value = std::to_integer< std::uint8_t >(input[index]);
238 order[byte_positions[value]++] = index;
240 std::size_t class_count = 1U;
241 classes[order[0U]] = 0U;
242 for (std::size_t index = 1U; index < size; ++index)
244 if (input[order[index]] != input[order[index - 1U]])
248 classes[order[index]] = class_count - 1U;
251 std::vector< std::size_t > shifted(size, 0U);
252 std::vector< std::size_t > new_classes(size, 0U);
253 for (std::size_t shift = 1U; shift < size; shift <<= 1U)
255 for (std::size_t index = 0U; index < size; ++index)
257 shifted[index] = order[index] >= shift ? order[index] - shift : order[index] + size - shift;
259 std::vector< std::size_t > counts(class_count, 0U);
260 for (std::size_t index : shifted)
262 ++counts[classes[index]];
264 std::vector< std::size_t > positions(class_count, 0U);
265 for (std::size_t class_index = 1U; class_index < class_count; ++class_index)
267 positions[class_index] = positions[class_index - 1U] + counts[class_index - 1U];
269 for (std::size_t index : shifted)
271 std::size_t
const class_index = classes[index];
272 order[positions[class_index]++] = index;
275 std::size_t new_class_count = 1U;
276 new_classes[order[0U]] = 0U;
277 for (std::size_t index = 1U; index < size; ++index)
279 std::size_t
const current = order[index];
280 std::size_t
const previous = order[index - 1U];
281 if (classes[current] != classes[previous] || classes[(current + shift) % size] != classes[(previous + shift) % size])
285 new_classes[current] = new_class_count - 1U;
287 classes.swap(new_classes);
288 class_count = new_class_count;
289 if (shift > size / 2U)
295 std::vector< std::byte > last_column;
296 last_column.reserve(size);
297 std::optional< std::size_t > original_pointer;
298 for (std::size_t row = 0U; row < size; ++row)
300 std::size_t
const rotation = order[row];
303 original_pointer = row;
305 last_column.push_back(input[rotation == 0U ? size - 1U : rotation - 1U]);
307 if (!original_pointer.has_value())
323 void compress_block(writer_type& writer, std::span< std::byte const > input, std::uint32_t& combined_crc)
325 std::uint32_t
const block_crc =
crc32(input);
326 combined_crc = ((combined_crc << 1U) | (combined_crc >> 31U)) ^ block_crc;
330 std::array< bool, 256U > used{};
333 used[std::to_integer< std::uint8_t >(value)] =
true;
335 std::vector< std::uint8_t > alphabet;
336 for (std::size_t symbol = 0U; symbol < used.size(); ++symbol)
340 alphabet.push_back(
static_cast< std::uint8_t
>(symbol));
344 std::vector< std::uint8_t > move_to_front = alphabet;
345 std::vector< std::uint16_t > codes;
346 codes.reserve(transformed.
last_column.size() + 1U);
347 std::size_t zero_run = 0U;
348 auto flush_zero_run = [&]
354 std::size_t value = zero_run - 1U;
357 codes.push_back(
static_cast< std::uint16_t
>((value & 1U) != 0U ? 1U : 0U));
362 value = (value - 2U) / 2U;
368 std::uint8_t
const symbol = std::to_integer< std::uint8_t >(
byte);
369 std::size_t mtf_index = 0U;
370 while (mtf_index < move_to_front.size() && move_to_front[mtf_index] != symbol)
374 if (mtf_index == move_to_front.size())
384 codes.push_back(
static_cast< std::uint16_t
>(mtf_index + 1U));
385 for (std::size_t index = mtf_index; index > 0U; --index)
387 move_to_front[index] = move_to_front[index - 1U];
389 move_to_front[0U] = symbol;
392 std::size_t
const alphabet_size = alphabet.size() + 2U;
393 codes.push_back(
static_cast< std::uint16_t
>(alphabet_size - 1U));
395 writer.write_bits(0x314159265359ULL, 48U);
396 writer.write_bits(block_crc, 32U);
397 writer.write_bits(0U, 1U);
399 std::uint16_t range_mask = 0U;
400 std::array< std::uint16_t, 16U > symbol_masks{};
401 for (std::size_t symbol = 0U; symbol < used.size(); ++symbol)
405 std::size_t
const range = symbol / 16U;
406 range_mask |=
static_cast< std::uint16_t
>(std::uint16_t{1U} << (15U - range));
407 symbol_masks[range] |=
static_cast< std::uint16_t
>(std::uint16_t{1U} << (15U - symbol % 16U));
410 writer.write_bits(range_mask, 16U);
411 for (std::size_t range = 0U; range < symbol_masks.size(); ++range)
413 if (symbol_masks[range] != 0U)
415 writer.write_bits(symbol_masks[range], 16U);
419 writer.write_bits(2U, 3U);
420 std::size_t
const selector_count = (codes.size() + 49U) / 50U;
421 if (selector_count == 0U || selector_count > 18002U)
425 writer.write_bits(selector_count, 15U);
426 for (std::size_t selector = 0U; selector < selector_count; ++selector)
428 writer.write_bits(0U, 1U);
431 std::uint8_t
const code_length =
static_cast< std::uint8_t
>(std::bit_width(alphabet_size - 1U));
432 for (std::size_t group = 0U; group < 2U; ++group)
434 writer.write_bits(code_length, 5U);
435 for (std::size_t symbol = 0U; symbol < alphabet_size; ++symbol)
437 writer.write_bits(0U, 1U);
440 for (std::uint16_t code : codes)
442 writer.write_bits(code, code_length);
458 [[nodiscard]] std::vector< std::byte >
decompress_block(reader_type& reader, std::size_t block_size_limit, std::size_t maximum_output_size, std::uint32_t& combined_crc)
460 std::uint32_t
const expected_block_crc =
static_cast< std::uint32_t
>(reader.read_bits(32U));
461 if (reader.read_bits(1U) != 0U)
465 std::size_t
const original_pointer =
static_cast< std::size_t
>(reader.read_bits(24U));
467 std::uint16_t
const used_ranges =
static_cast< std::uint16_t
>(reader.read_bits(16U));
468 std::vector< std::uint8_t > symbols;
469 symbols.reserve(256U);
470 for (std::uint8_t range = 0U; range < 16U; ++range)
472 if ((used_ranges & (std::uint16_t{1U} << (15U - range))) == 0U)
476 std::uint16_t
const used_symbols =
static_cast< std::uint16_t
>(reader.read_bits(16U));
477 for (std::uint8_t symbol = 0U; symbol < 16U; ++symbol)
479 if ((used_symbols & (std::uint16_t{1U} << (15U - symbol))) != 0U)
481 symbols.push_back(
static_cast< std::uint8_t
>(range * 16U + symbol));
490 std::size_t
const group_count =
static_cast< std::size_t
>(reader.read_bits(3U));
491 if (group_count < 2U || group_count > 6U)
495 std::size_t
const selector_count =
static_cast< std::size_t
>(reader.read_bits(15U));
496 if (selector_count == 0U || selector_count > 18002U)
500 std::vector< std::uint8_t > selector_mtf(group_count);
501 for (std::size_t index = 0U; index < group_count; ++index)
503 selector_mtf[index] =
static_cast< std::uint8_t
>(index);
505 std::vector< std::uint8_t > selectors;
506 selectors.reserve(selector_count);
507 for (std::size_t selector = 0U; selector < selector_count; ++selector)
509 std::size_t mtf_index = 0U;
510 while (reader.read_bits(1U) != 0U)
513 if (mtf_index >= group_count)
518 std::uint8_t
const group = selector_mtf[mtf_index];
519 for (std::size_t index = mtf_index; index > 0U; --index)
521 selector_mtf[index] = selector_mtf[index - 1U];
523 selector_mtf[0U] = group;
524 selectors.push_back(group);
527 std::size_t
const alphabet_size = symbols.size() + 2U;
528 std::vector< huffman_table > tables;
529 tables.reserve(group_count);
530 for (std::size_t group = 0U; group < group_count; ++group)
532 std::int16_t length =
static_cast< std::int16_t
>(reader.read_bits(5U));
533 std::vector< std::uint8_t > lengths;
534 lengths.reserve(alphabet_size);
535 for (std::size_t symbol = 0U; symbol < alphabet_size; ++symbol)
537 while (reader.read_bits(1U) != 0U)
539 if (reader.read_bits(1U) != 0U)
547 if (length < 1 || length > 20)
552 lengths.push_back(
static_cast< std::uint8_t
>(length));
557 std::vector< std::uint8_t > move_to_front = symbols;
558 std::vector< std::byte > transformed;
559 transformed.reserve(block_size_limit);
560 std::size_t selector_position = 0U;
561 std::size_t symbols_in_group = 0U;
562 std::size_t repeat_count = 0U;
563 std::size_t repeat_power = 0U;
566 if (symbols_in_group == 0U)
568 if (selector_position >= selectors.size())
572 symbols_in_group = 50U;
574 std::uint8_t
const group = selectors[selector_position];
577 if (symbols_in_group == 0U)
584 if (repeat_count == 0U)
588 if (repeat_power > block_size_limit || (value != 0U && repeat_power > (block_size_limit - repeat_count) / 2U))
592 repeat_count += repeat_power << value;
594 if (repeat_count > block_size_limit)
601 if (repeat_count != 0U)
603 if (repeat_count > block_size_limit - transformed.size())
607 transformed.insert(transformed.end(), repeat_count,
static_cast< std::byte
>(move_to_front[0U]));
610 if (value == alphabet_size - 1U)
614 std::size_t
const mtf_index = value - 1U;
615 if (mtf_index >= move_to_front.size())
619 std::uint8_t
const symbol = move_to_front[mtf_index];
620 for (std::size_t index = mtf_index; index > 0U; --index)
622 move_to_front[index] = move_to_front[index - 1U];
624 move_to_front[0U] = symbol;
625 if (transformed.size() == block_size_limit)
629 transformed.push_back(
static_cast< std::byte
>(symbol));
632 if (transformed.empty() || original_pointer >= transformed.size())
636 std::array< std::size_t, 256U > counts{};
637 for (std::byte value : transformed)
639 ++counts[std::to_integer< std::uint8_t >(value)];
641 std::array< std::size_t, 256U > positions{};
642 std::size_t cumulative = 0U;
643 for (std::size_t symbol = 0U; symbol < counts.size(); ++symbol)
645 positions[symbol] = cumulative;
646 cumulative += counts[symbol];
648 std::vector< std::size_t > next(transformed.size(), 0U);
649 for (std::size_t index = 0U; index < transformed.size(); ++index)
651 std::uint8_t
const symbol = std::to_integer< std::uint8_t >(transformed[index]);
652 next[positions[symbol]++] = index;
655 std::vector< std::byte > output;
656 output.reserve(std::min(block_size_limit, maximum_output_size));
657 std::size_t row = original_pointer;
658 std::optional< std::byte > previous_byte;
659 std::uint8_t repeated_bytes = 0U;
660 for (std::size_t index = 0U; index < transformed.size(); ++index)
663 std::byte
const value = transformed[row];
664 if (repeated_bytes == 3U)
666 std::size_t
const additional_count = std::to_integer< std::uint8_t >(value);
667 if (!previous_byte.has_value() || output.size() > maximum_output_size || additional_count > maximum_output_size - output.size())
671 output.insert(output.end(), additional_count, *previous_byte);
672 previous_byte.reset();
676 if (output.size() >= maximum_output_size)
680 output.push_back(value);
681 if (previous_byte.has_value() && *previous_byte == value)
687 previous_byte = value;
692 std::uint32_t
const actual_block_crc =
crc32(output);
693 if (actual_block_crc != expected_block_crc)
697 combined_crc = ((combined_crc << 1U) | (combined_crc >> 31U)) ^ actual_block_crc;
879 std::size_t total_output = 0U;
880 bool decoded_stream =
false;
888 if (reader.read_bits(16U) != 0x425aU || reader.read_bits(8U) != 0x68U)
892 std::uint8_t
const level_byte =
static_cast< std::uint8_t
>(reader.read_bits(8U));
893 if (level_byte <
static_cast< std::uint8_t
>(
'1') || level_byte >
static_cast< std::uint8_t
>(
'9'))
897 std::size_t
const block_size_limit =
static_cast< std::size_t
>(level_byte -
static_cast< std::uint8_t
>(
'0')) * 100000U;
898 std::uint32_t combined_crc = 0U;
901 std::uint64_t
const magic = reader.read_bits(48U);
902 if (magic == 0x177245385090ULL)
904 if (combined_crc !=
static_cast< std::uint32_t
>(reader.read_bits(32U)))
908 reader.align_to_byte();
911 if (magic != 0x314159265359ULL)
916 for (std::byte value : block)
926 decoded_stream =
true;
927 }
while (!source.
empty());