Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
kcenon::network::protocols::http2::huffman Namespace Reference

Functions

auto encode (std::string_view input) -> std::vector< uint8_t >
 Encode string using Huffman coding.
 
auto decode (std::span< const uint8_t > data) -> Result< std::string >
 Decode Huffman encoded string.
 
auto encoded_size (std::string_view input) -> size_t
 Get encoded size for string.
 

Function Documentation

◆ decode()

auto kcenon::network::protocols::http2::huffman::decode ( std::span< const uint8_t > data) -> Result<std::string>

Decode Huffman encoded string.

Parameters
dataEncoded bytes
Returns
Decoded string or error

Definition at line 808 of file hpack.cpp.

809 {
810 static const std::vector<decode_node> tree = build_decode_tree();
811
812 std::string out;
813 int node = 0;
814 int partial_bits = 0;
815 bool partial_all_ones = true;
816
817 for (uint8_t byte : data)
818 {
819 for (int i = 7; i >= 0; --i)
820 {
821 int bit = (byte >> i) & 1;
822 node = tree[node].children[bit];
823 if (node == -1)
824 {
825 return error_info(108, "Invalid Huffman code", "hpack");
826 }
827 ++partial_bits;
828 if (bit == 0)
829 {
830 partial_all_ones = false;
831 }
832
833 if (tree[node].symbol >= 0)
834 {
835 if (tree[node].symbol == kEosSymbol)
836 {
837 // RFC 7541 5.2: EOS in the encoded data is an error.
838 return error_info(108, "EOS symbol in Huffman-encoded data",
839 "hpack");
840 }
841 out.push_back(static_cast<char>(tree[node].symbol));
842 node = 0;
843 partial_bits = 0;
844 partial_all_ones = true;
845 }
846 }
847 }
848
849 // RFC 7541 5.2: any trailing bits must be valid EOS padding —
850 // at most 7 bits, all set to 1.
851 if (node != 0 && (partial_bits > 7 || !partial_all_ones))
852 {
853 return error_info(108, "Invalid Huffman padding", "hpack");
854 }
855
856 return out;
857 }
int symbol
Definition hpack.cpp:747
simple_error error_info

References kcenon::network::protocols::http2::data, and symbol.

Referenced by kcenon::network::protocols::http2::hpack_decoder::decode_string().

Here is the caller graph for this function:

◆ encode()

auto kcenon::network::protocols::http2::huffman::encode ( std::string_view input) -> std::vector<uint8_t>

Encode string using Huffman coding.

Parameters
inputString to encode
Returns
Encoded bytes

Definition at line 775 of file hpack.cpp.

776 {
777 std::vector<uint8_t> out;
778 out.reserve(input.size());
779
780 uint64_t buffer = 0;
781 int buffer_bits = 0;
782
783 for (unsigned char ch : input)
784 {
785 const auto& entry = kHuffmanTable[ch];
786 buffer = (buffer << entry.bits) | entry.code;
787 buffer_bits += entry.bits;
788
789 while (buffer_bits >= 8)
790 {
791 buffer_bits -= 8;
792 out.push_back(static_cast<uint8_t>(buffer >> buffer_bits));
793 }
794 // Keep only the still-pending low bits to avoid emitting stale data.
795 buffer &= (1ull << buffer_bits) - 1;
796 }
797
798 if (buffer_bits > 0)
799 {
800 // Pad the final byte with the most significant bits of EOS (all 1s).
801 int pad = 8 - buffer_bits;
802 out.push_back(static_cast<uint8_t>((buffer << pad) | ((1u << pad) - 1)));
803 }
804
805 return out;
806 }

Referenced by kcenon::network::protocols::http2::hpack_encoder::encode_string().

Here is the caller graph for this function:

◆ encoded_size()

auto kcenon::network::protocols::http2::huffman::encoded_size ( std::string_view input) -> size_t

Get encoded size for string.

Parameters
inputString to measure
Returns
Size in bytes if Huffman encoded

Definition at line 859 of file hpack.cpp.

860 {
861 size_t bits = 0;
862 for (unsigned char ch : input)
863 {
864 bits += kHuffmanTable[ch].bits;
865 }
866 return (bits + 7) / 8;
867 }
uint8_t bits
Definition hpack.cpp:669

References bits.