| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/spdy/core/hpack/hpack_huffman_decoder.h" | 5 #include "net/spdy/core/hpack/hpack_huffman_decoder.h" |
| 6 | 6 |
| 7 #include <bitset> | 7 #include <bitset> |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 public: | 40 public: |
| 41 static HuffmanCodeLength CodeLengthOfPrefix(HuffmanWord value) { | 41 static HuffmanCodeLength CodeLengthOfPrefix(HuffmanWord value) { |
| 42 return HpackHuffmanDecoder::CodeLengthOfPrefix(value); | 42 return HpackHuffmanDecoder::CodeLengthOfPrefix(value); |
| 43 } | 43 } |
| 44 | 44 |
| 45 static HuffmanWord DecodeToCanonical(HuffmanCodeLength code_length, | 45 static HuffmanWord DecodeToCanonical(HuffmanCodeLength code_length, |
| 46 HuffmanWord bits) { | 46 HuffmanWord bits) { |
| 47 return HpackHuffmanDecoder::DecodeToCanonical(code_length, bits); | 47 return HpackHuffmanDecoder::DecodeToCanonical(code_length, bits); |
| 48 } | 48 } |
| 49 | 49 |
| 50 static char CanonicalToSource(HuffmanWord canonical) { | 50 static uint8_t CanonicalToSource(HuffmanWord canonical) { |
| 51 return HpackHuffmanDecoder::CanonicalToSource(canonical); | 51 return HpackHuffmanDecoder::CanonicalToSource(canonical); |
| 52 } | 52 } |
| 53 }; | 53 }; |
| 54 | 54 |
| 55 // Tests of the ability to decode the HPACK Huffman Code, defined in: | 55 // Tests of the ability to decode the HPACK Huffman Code, defined in: |
| 56 // https://httpwg.github.io/specs/rfc7541.html#huffman.code | 56 // https://httpwg.github.io/specs/rfc7541.html#huffman.code |
| 57 class HpackHuffmanDecoderTest : public ::testing::Test { | 57 class HpackHuffmanDecoderTest : public ::testing::Test { |
| 58 protected: | 58 protected: |
| 59 HpackHuffmanDecoderTest() : table_(ObtainHpackHuffmanTable()) {} | 59 HpackHuffmanDecoderTest() : table_(ObtainHpackHuffmanTable()) {} |
| 60 | 60 |
| 61 // Since kHpackHuffmanCode doesn't include the canonical symbol value, | 61 // Since kHpackHuffmanCode doesn't include the canonical symbol value, |
| 62 // this helper helps us to decode directly to the source symbol, allowing | 62 // this helper helps us to decode directly to the source symbol, allowing |
| 63 // for EOS. | 63 // for EOS. |
| 64 uint16_t DecodeToSource(HuffmanCodeLength code_length, HuffmanWord bits) { | 64 uint16_t DecodeToSource(HuffmanCodeLength code_length, HuffmanWord bits) { |
| 65 HuffmanWord canonical = | 65 HuffmanWord canonical = |
| 66 HpackHuffmanDecoderPeer::DecodeToCanonical(code_length, bits); | 66 HpackHuffmanDecoderPeer::DecodeToCanonical(code_length, bits); |
| 67 EXPECT_LE(canonical, 256u); | 67 EXPECT_LE(canonical, 256u); |
| 68 if (canonical == 256u) { | 68 if (canonical == 256u) { |
| 69 return canonical; | 69 return canonical; |
| 70 } | 70 } |
| 71 return static_cast<unsigned char>( | 71 return HpackHuffmanDecoderPeer::CanonicalToSource(canonical); |
| 72 HpackHuffmanDecoderPeer::CanonicalToSource(canonical)); | |
| 73 } | 72 } |
| 74 | 73 |
| 75 void EncodeString(SpdyStringPiece input, SpdyString* encoded) { | 74 void EncodeString(SpdyStringPiece input, SpdyString* encoded) { |
| 76 HpackOutputStream output_stream; | 75 HpackOutputStream output_stream; |
| 77 table_.EncodeString(input, &output_stream); | 76 table_.EncodeString(input, &output_stream); |
| 78 encoded->clear(); | 77 encoded->clear(); |
| 79 output_stream.TakeString(encoded); | 78 output_stream.TakeString(encoded); |
| 80 // Verify EncodedSize() agrees with EncodeString(). | 79 // Verify EncodedSize() agrees with EncodeString(). |
| 81 EXPECT_EQ(encoded->size(), table_.EncodedSize(input)); | 80 EXPECT_EQ(encoded->size(), table_.EncodedSize(input)); |
| 82 } | 81 } |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 } | 245 } |
| 247 EncodeString(input, &encoded); | 246 EncodeString(input, &encoded); |
| 248 HpackInputStream input_stream(encoded); | 247 HpackInputStream input_stream(encoded); |
| 249 EXPECT_TRUE(HpackHuffmanDecoder::DecodeString(&input_stream, &decoded)); | 248 EXPECT_TRUE(HpackHuffmanDecoder::DecodeString(&input_stream, &decoded)); |
| 250 EXPECT_EQ(input, decoded); | 249 EXPECT_EQ(input, decoded); |
| 251 } | 250 } |
| 252 } | 251 } |
| 253 | 252 |
| 254 } // namespace test | 253 } // namespace test |
| 255 } // namespace net | 254 } // namespace net |
| OLD | NEW |