libfly  6.2.2
C++20 utility library for Linux, macOS, and Windows
huffman_decoder.hpp
1 #pragma once
2 
3 #include "fly/coders/coder.hpp"
4 #include "fly/coders/huffman/types.hpp"
5 
6 #include <array>
7 #include <memory>
8 #include <ostream>
9 
10 namespace fly {
11 class BitStreamReader;
12 } // namespace fly
13 
14 namespace fly::coders {
15 
23 {
24 public:
28  HuffmanDecoder() noexcept;
29 
36  code_type compute_kraft_mcmillan_constant() const;
37 
38 protected:
66  bool decode_binary(fly::BitStreamReader &encoded, std::ostream &decoded) override;
67 
68 private:
78  bool decode_header(fly::BitStreamReader &encoded, std::uint32_t &chunk_size);
79 
89  bool decode_header_version1(fly::BitStreamReader &encoded, std::uint32_t &chunk_size);
90 
100  bool decode_codes(fly::BitStreamReader &encoded, length_type &max_code_length);
101 
107  void convert_to_prefix_table(length_type max_code_length);
108 
121  bool decode_symbols(
122  fly::BitStreamReader &encoded,
123  length_type max_code_length,
124  std::uint32_t chunk_size,
125  std::ostream &decoded) const;
126 
127  std::unique_ptr<symbol_type[]> m_chunk_buffer;
128 
129  // Sized to fit 8-bit ASCII symbols.
130  std::array<HuffmanCode, 1 << 8> m_huffman_codes;
131  std::uint16_t m_huffman_codes_size;
132  length_type m_max_code_length;
133 
134  // Will be sized to fit the global maximum Huffman code length used by the encoder. The size
135  // will be 2^L, were L is the maximum code length.
136  std::unique_ptr<HuffmanCode[]> m_prefix_table;
137 };
138 
139 } // namespace fly::coders
Definition: bit_stream_reader.hpp:27
Definition: coder.hpp:141
Definition: huffman_decoder.hpp:23
bool decode_binary(fly::BitStreamReader &encoded, std::ostream &decoded) override
Definition: huffman_decoder.cpp:32
code_type compute_kraft_mcmillan_constant() const
Definition: huffman_decoder.cpp:19
HuffmanDecoder() noexcept
Definition: huffman_decoder.cpp:14