| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/hpack/hpack_input_stream.h" | 5 #include "net/spdy/hpack/hpack_input_stream.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | |
| 10 #include "base/logging.h" | 9 #include "base/logging.h" |
| 11 | 10 |
| 12 namespace net { | 11 namespace net { |
| 13 | 12 |
| 14 using base::StringPiece; | 13 using base::StringPiece; |
| 15 using std::string; | 14 using std::string; |
| 16 | 15 |
| 17 HpackInputStream::HpackInputStream(uint32 max_string_literal_size, | 16 HpackInputStream::HpackInputStream(uint32_t max_string_literal_size, |
| 18 StringPiece buffer) | 17 StringPiece buffer) |
| 19 : max_string_literal_size_(max_string_literal_size), | 18 : max_string_literal_size_(max_string_literal_size), |
| 20 buffer_(buffer), | 19 buffer_(buffer), |
| 21 bit_offset_(0) {} | 20 bit_offset_(0) {} |
| 22 | 21 |
| 23 HpackInputStream::~HpackInputStream() {} | 22 HpackInputStream::~HpackInputStream() {} |
| 24 | 23 |
| 25 bool HpackInputStream::HasMoreData() const { | 24 bool HpackInputStream::HasMoreData() const { |
| 26 return !buffer_.empty(); | 25 return !buffer_.empty(); |
| 27 } | 26 } |
| 28 | 27 |
| 29 bool HpackInputStream::MatchPrefixAndConsume(HpackPrefix prefix) { | 28 bool HpackInputStream::MatchPrefixAndConsume(HpackPrefix prefix) { |
| 30 DCHECK_GT(prefix.bit_size, 0u); | 29 DCHECK_GT(prefix.bit_size, 0u); |
| 31 DCHECK_LE(prefix.bit_size, 8u); | 30 DCHECK_LE(prefix.bit_size, 8u); |
| 32 | 31 |
| 33 uint32 peeked = 0; | 32 uint32_t peeked = 0; |
| 34 size_t peeked_count = 0; | 33 size_t peeked_count = 0; |
| 35 | 34 |
| 36 if (!PeekBits(&peeked_count, &peeked)) { | 35 if (!PeekBits(&peeked_count, &peeked)) { |
| 37 return false; | 36 return false; |
| 38 } | 37 } |
| 39 | 38 |
| 40 if ((peeked >> (32 - prefix.bit_size)) == prefix.bits) { | 39 if ((peeked >> (32 - prefix.bit_size)) == prefix.bits) { |
| 41 ConsumeBits(prefix.bit_size); | 40 ConsumeBits(prefix.bit_size); |
| 42 return true; | 41 return true; |
| 43 } | 42 } |
| 44 return false; | 43 return false; |
| 45 } | 44 } |
| 46 | 45 |
| 47 bool HpackInputStream::PeekNextOctet(uint8* next_octet) { | 46 bool HpackInputStream::PeekNextOctet(uint8_t* next_octet) { |
| 48 if ((bit_offset_ > 0) || buffer_.empty()) { | 47 if ((bit_offset_ > 0) || buffer_.empty()) { |
| 49 return false; | 48 return false; |
| 50 } | 49 } |
| 51 | 50 |
| 52 *next_octet = buffer_[0]; | 51 *next_octet = buffer_[0]; |
| 53 return true; | 52 return true; |
| 54 } | 53 } |
| 55 | 54 |
| 56 bool HpackInputStream::DecodeNextOctet(uint8* next_octet) { | 55 bool HpackInputStream::DecodeNextOctet(uint8_t* next_octet) { |
| 57 if (!PeekNextOctet(next_octet)) { | 56 if (!PeekNextOctet(next_octet)) { |
| 58 return false; | 57 return false; |
| 59 } | 58 } |
| 60 | 59 |
| 61 buffer_.remove_prefix(1); | 60 buffer_.remove_prefix(1); |
| 62 return true; | 61 return true; |
| 63 } | 62 } |
| 64 | 63 |
| 65 bool HpackInputStream::DecodeNextUint32(uint32* I) { | 64 bool HpackInputStream::DecodeNextUint32(uint32_t* I) { |
| 66 size_t N = 8 - bit_offset_; | 65 size_t N = 8 - bit_offset_; |
| 67 DCHECK_GT(N, 0u); | 66 DCHECK_GT(N, 0u); |
| 68 DCHECK_LE(N, 8u); | 67 DCHECK_LE(N, 8u); |
| 69 | 68 |
| 70 bit_offset_ = 0; | 69 bit_offset_ = 0; |
| 71 | 70 |
| 72 *I = 0; | 71 *I = 0; |
| 73 | 72 |
| 74 uint8 next_marker = (1 << N) - 1; | 73 uint8_t next_marker = (1 << N) - 1; |
| 75 uint8 next_octet = 0; | 74 uint8_t next_octet = 0; |
| 76 if (!DecodeNextOctet(&next_octet)) { | 75 if (!DecodeNextOctet(&next_octet)) { |
| 77 return false; | 76 return false; |
| 78 } | 77 } |
| 79 *I = next_octet & next_marker; | 78 *I = next_octet & next_marker; |
| 80 | 79 |
| 81 bool has_more = (*I == next_marker); | 80 bool has_more = (*I == next_marker); |
| 82 size_t shift = 0; | 81 size_t shift = 0; |
| 83 while (has_more && (shift < 32)) { | 82 while (has_more && (shift < 32)) { |
| 84 uint8 next_octet = 0; | 83 uint8_t next_octet = 0; |
| 85 if (!DecodeNextOctet(&next_octet)) { | 84 if (!DecodeNextOctet(&next_octet)) { |
| 86 return false; | 85 return false; |
| 87 } | 86 } |
| 88 has_more = (next_octet & 0x80) != 0; | 87 has_more = (next_octet & 0x80) != 0; |
| 89 next_octet &= 0x7f; | 88 next_octet &= 0x7f; |
| 90 uint32 addend = next_octet << shift; | 89 uint32_t addend = next_octet << shift; |
| 91 // Check for overflow. | 90 // Check for overflow. |
| 92 if ((addend >> shift) != next_octet) { | 91 if ((addend >> shift) != next_octet) { |
| 93 return false; | 92 return false; |
| 94 } | 93 } |
| 95 *I += addend; | 94 *I += addend; |
| 96 shift += 7; | 95 shift += 7; |
| 97 } | 96 } |
| 98 | 97 |
| 99 return !has_more; | 98 return !has_more; |
| 100 } | 99 } |
| 101 | 100 |
| 102 bool HpackInputStream::DecodeNextIdentityString(StringPiece* str) { | 101 bool HpackInputStream::DecodeNextIdentityString(StringPiece* str) { |
| 103 uint32 size = 0; | 102 uint32_t size = 0; |
| 104 if (!DecodeNextUint32(&size)) { | 103 if (!DecodeNextUint32(&size)) { |
| 105 return false; | 104 return false; |
| 106 } | 105 } |
| 107 | 106 |
| 108 if (size > max_string_literal_size_) { | 107 if (size > max_string_literal_size_) { |
| 109 return false; | 108 return false; |
| 110 } | 109 } |
| 111 | 110 |
| 112 if (size > buffer_.size()) { | 111 if (size > buffer_.size()) { |
| 113 return false; | 112 return false; |
| 114 } | 113 } |
| 115 | 114 |
| 116 *str = StringPiece(buffer_.data(), size); | 115 *str = StringPiece(buffer_.data(), size); |
| 117 buffer_.remove_prefix(size); | 116 buffer_.remove_prefix(size); |
| 118 return true; | 117 return true; |
| 119 } | 118 } |
| 120 | 119 |
| 121 bool HpackInputStream::DecodeNextHuffmanString(const HpackHuffmanTable& table, | 120 bool HpackInputStream::DecodeNextHuffmanString(const HpackHuffmanTable& table, |
| 122 string* str) { | 121 string* str) { |
| 123 uint32 encoded_size = 0; | 122 uint32_t encoded_size = 0; |
| 124 if (!DecodeNextUint32(&encoded_size)) { | 123 if (!DecodeNextUint32(&encoded_size)) { |
| 125 return false; | 124 return false; |
| 126 } | 125 } |
| 127 | 126 |
| 128 if (encoded_size > buffer_.size()) { | 127 if (encoded_size > buffer_.size()) { |
| 129 return false; | 128 return false; |
| 130 } | 129 } |
| 131 | 130 |
| 132 HpackInputStream bounded_reader(max_string_literal_size_, | 131 HpackInputStream bounded_reader(max_string_literal_size_, |
| 133 StringPiece(buffer_.data(), encoded_size)); | 132 StringPiece(buffer_.data(), encoded_size)); |
| 134 buffer_.remove_prefix(encoded_size); | 133 buffer_.remove_prefix(encoded_size); |
| 135 | 134 |
| 136 // HpackHuffmanTable will not decode beyond |max_string_literal_size_|. | 135 // HpackHuffmanTable will not decode beyond |max_string_literal_size_|. |
| 137 return table.DecodeString(&bounded_reader, max_string_literal_size_, str); | 136 return table.DecodeString(&bounded_reader, max_string_literal_size_, str); |
| 138 } | 137 } |
| 139 | 138 |
| 140 bool HpackInputStream::PeekBits(size_t* peeked_count, uint32* out) const { | 139 bool HpackInputStream::PeekBits(size_t* peeked_count, uint32_t* out) const { |
| 141 size_t byte_offset = (bit_offset_ + *peeked_count) / 8; | 140 size_t byte_offset = (bit_offset_ + *peeked_count) / 8; |
| 142 size_t bit_offset = (bit_offset_ + *peeked_count) % 8; | 141 size_t bit_offset = (bit_offset_ + *peeked_count) % 8; |
| 143 | 142 |
| 144 if (*peeked_count >= 32 || byte_offset >= buffer_.size()) { | 143 if (*peeked_count >= 32 || byte_offset >= buffer_.size()) { |
| 145 return false; | 144 return false; |
| 146 } | 145 } |
| 147 // We'll read the minimum of the current byte remainder, | 146 // We'll read the minimum of the current byte remainder, |
| 148 // and the remaining unfilled bits of |out|. | 147 // and the remaining unfilled bits of |out|. |
| 149 size_t bits_to_read = std::min(32 - *peeked_count, 8 - bit_offset); | 148 size_t bits_to_read = std::min(32 - *peeked_count, 8 - bit_offset); |
| 150 | 149 |
| 151 uint32 new_bits = static_cast<uint32>(buffer_[byte_offset]); | 150 uint32_t new_bits = static_cast<uint32_t>(buffer_[byte_offset]); |
| 152 // Shift byte remainder to most-signifcant bits of |new_bits|. | 151 // Shift byte remainder to most-signifcant bits of |new_bits|. |
| 153 // This drops the leading |bit_offset| bits of the byte. | 152 // This drops the leading |bit_offset| bits of the byte. |
| 154 new_bits = new_bits << (24 + bit_offset); | 153 new_bits = new_bits << (24 + bit_offset); |
| 155 // Shift bits to the most-significant open bits of |out|. | 154 // Shift bits to the most-significant open bits of |out|. |
| 156 new_bits = new_bits >> *peeked_count; | 155 new_bits = new_bits >> *peeked_count; |
| 157 | 156 |
| 158 CHECK_EQ(*out & new_bits, 0u); | 157 CHECK_EQ(*out & new_bits, 0u); |
| 159 *out |= new_bits; | 158 *out |= new_bits; |
| 160 | 159 |
| 161 *peeked_count += bits_to_read; | 160 *peeked_count += bits_to_read; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 172 buffer_.remove_prefix(byte_count); | 171 buffer_.remove_prefix(byte_count); |
| 173 } | 172 } |
| 174 | 173 |
| 175 void HpackInputStream::ConsumeByteRemainder() { | 174 void HpackInputStream::ConsumeByteRemainder() { |
| 176 if (bit_offset_ != 0) { | 175 if (bit_offset_ != 0) { |
| 177 ConsumeBits(8 - bit_offset_); | 176 ConsumeBits(8 - bit_offset_); |
| 178 } | 177 } |
| 179 } | 178 } |
| 180 | 179 |
| 181 } // namespace net | 180 } // namespace net |
| OLD | NEW |