OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 NET_SPDY_HPACK_HPACK_HUFFMAN_DECODER_H_ |
| 6 #define NET_SPDY_HPACK_HPACK_HUFFMAN_DECODER_H_ |
| 7 |
| 8 #include <stddef.h> |
| 9 #include <stdint.h> |
| 10 |
| 11 #include <string> |
| 12 |
| 13 #include "net/base/net_export.h" |
| 14 #include "net/spdy/hpack/hpack_input_stream.h" |
| 15 |
| 16 namespace net { |
| 17 namespace test { |
| 18 class HpackHuffmanDecoderPeer; |
| 19 } // namespace test |
| 20 |
| 21 // Declared as a class to simplify testing. |
| 22 // No instances are actually allocated. |
| 23 class NET_EXPORT_PRIVATE HpackHuffmanDecoder { |
| 24 public: |
| 25 typedef uint32_t HuffmanWord; |
| 26 typedef size_t HuffmanCodeLength; |
| 27 |
| 28 HpackHuffmanDecoder() = delete; |
| 29 |
| 30 // Decodes a string that has been encoded using the HPACK Huffman Code (see |
| 31 // https://httpwg.github.io/specs/rfc7541.html#huffman.code), reading the |
| 32 // encoded bitstream from |*in|, appending each decoded char to |*out|. |
| 33 // To avoid repeatedly growing the |*out| string, the caller should reserve |
| 34 // sufficient space in |*out| to hold decoded output. |
| 35 // DecodeString() halts when |in| runs out of input, in which case true is |
| 36 // returned. It also halts (returning false) if an invalid Huffman code |
| 37 // prefix is read, or if |out_capacity| would otherwise be overflowed. |
| 38 static bool DecodeString(HpackInputStream* in, |
| 39 size_t out_capacity, |
| 40 std::string* out); |
| 41 |
| 42 private: |
| 43 friend class test::HpackHuffmanDecoderPeer; |
| 44 |
| 45 // The following private methods are declared here rather than simply |
| 46 // inlined into DecodeString so that they can be tested directly. |
| 47 |
| 48 // Returns the length (in bits) of the HPACK Huffman code that starts with |
| 49 // the high bits of |value|. |
| 50 static HuffmanCodeLength CodeLengthOfPrefix(HuffmanWord value); |
| 51 |
| 52 // Decodes the code in the high |code_length| bits of |bits| to the |
| 53 // corresponding canonical symbol. |
| 54 // Returns a value in the range [0, 256] (257 values). 256 is the EOS symbol, |
| 55 // which must not be explicitly encoded; the HPACK spec says that a decoder |
| 56 // must treat EOS as a decoding error. |
| 57 // Note that the canonical symbol is not the final value to be output because |
| 58 // the source symbols are not in descending probability order, so another |
| 59 // translation is required (see CanonicalToSource below). |
| 60 static HuffmanWord DecodeToCanonical(HuffmanCodeLength code_length, |
| 61 HuffmanWord bits); |
| 62 |
| 63 // Converts a canonical symbol to the source symbol (the char in the original |
| 64 // string that was encoded). |
| 65 static char CanonicalToSource(HuffmanWord canonical); |
| 66 }; |
| 67 |
| 68 } // namespace net |
| 69 |
| 70 #endif // NET_SPDY_HPACK_HPACK_HUFFMAN_DECODER_H_ |
OLD | NEW |