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