QUICHE team | 82dee2f | 2019-01-18 12:35:12 -0500 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef QUICHE_SPDY_CORE_HPACK_HPACK_HUFFMAN_TABLE_H_ |
| 6 | #define QUICHE_SPDY_CORE_HPACK_HPACK_HUFFMAN_TABLE_H_ |
| 7 | |
| 8 | #include <cstddef> |
| 9 | #include <cstdint> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "net/third_party/quiche/src/spdy/core/hpack/hpack_constants.h" |
| 13 | #include "net/third_party/quiche/src/spdy/platform/api/spdy_export.h" |
| 14 | #include "net/third_party/quiche/src/spdy/platform/api/spdy_string.h" |
| 15 | #include "net/third_party/quiche/src/spdy/platform/api/spdy_string_piece.h" |
| 16 | |
| 17 | namespace spdy { |
| 18 | |
| 19 | namespace test { |
| 20 | class HpackHuffmanTablePeer; |
| 21 | } // namespace test |
| 22 | |
| 23 | class HpackOutputStream; |
| 24 | |
| 25 | // HpackHuffmanTable encodes string literals using a constructed canonical |
| 26 | // Huffman code. Once initialized, an instance is read only and may be accessed |
| 27 | // only through its const interface. |
| 28 | class SPDY_EXPORT_PRIVATE HpackHuffmanTable { |
| 29 | public: |
| 30 | friend class test::HpackHuffmanTablePeer; |
| 31 | |
| 32 | typedef HpackHuffmanSymbol Symbol; |
| 33 | |
| 34 | HpackHuffmanTable(); |
| 35 | ~HpackHuffmanTable(); |
| 36 | |
| 37 | // Prepares HpackHuffmanTable to encode the canonical Huffman code as |
| 38 | // determined by the given symbols. Must be called exactly once. |
| 39 | // Returns false if the input symbols define an invalid coding, and true |
| 40 | // otherwise. Symbols must be presented in ascending ID order with no gaps, |
| 41 | // and |symbol_count| must fit in a uint16_t. |
| 42 | bool Initialize(const Symbol* input_symbols, size_t symbol_count); |
| 43 | |
| 44 | // Returns whether Initialize() has been successfully called. |
| 45 | bool IsInitialized() const; |
| 46 | |
| 47 | // Encodes the input string to the output stream using the table's Huffman |
| 48 | // context. |
| 49 | void EncodeString(SpdyStringPiece in, HpackOutputStream* out) const; |
| 50 | |
| 51 | // Returns the encoded size of the input string. |
| 52 | size_t EncodedSize(SpdyStringPiece in) const; |
| 53 | |
| 54 | // Returns the estimate of dynamically allocated memory in bytes. |
| 55 | size_t EstimateMemoryUsage() const; |
| 56 | |
| 57 | private: |
| 58 | // Expects symbols ordered on ID ascending. |
| 59 | void BuildEncodeTable(const std::vector<Symbol>& symbols); |
| 60 | |
| 61 | // Symbol code and code length, in ascending symbol ID order. |
| 62 | // Codes are stored in the most-significant bits of the word. |
| 63 | std::vector<uint32_t> code_by_id_; |
| 64 | std::vector<uint8_t> length_by_id_; |
| 65 | |
| 66 | // The first 8 bits of the longest code. Applied when generating padding bits. |
| 67 | uint8_t pad_bits_; |
| 68 | |
| 69 | // If initialization fails, preserve the symbol ID which failed validation |
| 70 | // for examination in tests. |
| 71 | uint16_t failed_symbol_id_; |
| 72 | }; |
| 73 | |
| 74 | } // namespace spdy |
| 75 | |
| 76 | #endif // QUICHE_SPDY_CORE_HPACK_HPACK_HUFFMAN_TABLE_H_ |