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.
5 #include "net/spdy/hpack/hpack_input_stream.h"
9 #include "base/basictypes.h"
10 #include "base/logging.h"
14 using base::StringPiece
;
17 HpackInputStream::HpackInputStream(uint32 max_string_literal_size
,
19 : max_string_literal_size_(max_string_literal_size
),
23 HpackInputStream::~HpackInputStream() {}
25 bool HpackInputStream::HasMoreData() const {
26 return !buffer_
.empty();
29 bool HpackInputStream::MatchPrefixAndConsume(HpackPrefix prefix
) {
30 DCHECK_GT(prefix
.bit_size
, 0u);
31 DCHECK_LE(prefix
.bit_size
, 8u);
34 size_t peeked_count
= 0;
36 if (!PeekBits(&peeked_count
, &peeked
)) {
40 if ((peeked
>> (32 - prefix
.bit_size
)) == prefix
.bits
) {
41 ConsumeBits(prefix
.bit_size
);
47 bool HpackInputStream::PeekNextOctet(uint8
* next_octet
) {
48 if ((bit_offset_
> 0) || buffer_
.empty()) {
52 *next_octet
= buffer_
[0];
56 bool HpackInputStream::DecodeNextOctet(uint8
* next_octet
) {
57 if (!PeekNextOctet(next_octet
)) {
61 buffer_
.remove_prefix(1);
65 bool HpackInputStream::DecodeNextUint32(uint32
* I
) {
66 size_t N
= 8 - bit_offset_
;
74 uint8 next_marker
= (1 << N
) - 1;
76 if (!DecodeNextOctet(&next_octet
)) {
79 *I
= next_octet
& next_marker
;
81 bool has_more
= (*I
== next_marker
);
83 while (has_more
&& (shift
< 32)) {
85 if (!DecodeNextOctet(&next_octet
)) {
88 has_more
= (next_octet
& 0x80) != 0;
90 uint32 addend
= next_octet
<< shift
;
91 // Check for overflow.
92 if ((addend
>> shift
) != next_octet
) {
102 bool HpackInputStream::DecodeNextIdentityString(StringPiece
* str
) {
104 if (!DecodeNextUint32(&size
)) {
108 if (size
> max_string_literal_size_
) {
112 if (size
> buffer_
.size()) {
116 *str
= StringPiece(buffer_
.data(), size
);
117 buffer_
.remove_prefix(size
);
121 bool HpackInputStream::DecodeNextHuffmanString(const HpackHuffmanTable
& table
,
123 uint32 encoded_size
= 0;
124 if (!DecodeNextUint32(&encoded_size
)) {
128 if (encoded_size
> buffer_
.size()) {
132 HpackInputStream
bounded_reader(max_string_literal_size_
,
133 StringPiece(buffer_
.data(), encoded_size
));
134 buffer_
.remove_prefix(encoded_size
);
136 // HpackHuffmanTable will not decode beyond |max_string_literal_size_|.
137 return table
.DecodeString(&bounded_reader
, max_string_literal_size_
, str
);
140 bool HpackInputStream::PeekBits(size_t* peeked_count
, uint32
* out
) {
141 size_t byte_offset
= (bit_offset_
+ *peeked_count
) / 8;
142 size_t bit_offset
= (bit_offset_
+ *peeked_count
) % 8;
144 if (*peeked_count
>= 32 || byte_offset
>= buffer_
.size()) {
147 // We'll read the minimum of the current byte remainder,
148 // and the remaining unfilled bits of |out|.
149 size_t bits_to_read
= std::min(32 - *peeked_count
, 8 - bit_offset
);
151 uint32 new_bits
= static_cast<uint32
>(buffer_
[byte_offset
]);
152 // Shift byte remainder to most-signifcant bits of |new_bits|.
153 // This drops the leading |bit_offset| bits of the byte.
154 new_bits
= new_bits
<< (24 + bit_offset
);
155 // Shift bits to the most-significant open bits of |out|.
156 new_bits
= new_bits
>> *peeked_count
;
158 CHECK_EQ(*out
& new_bits
, 0u);
161 *peeked_count
+= bits_to_read
;
165 void HpackInputStream::ConsumeBits(size_t bit_count
) {
166 size_t byte_count
= (bit_offset_
+ bit_count
) / 8;
167 bit_offset_
= (bit_offset_
+ bit_count
) % 8;
168 CHECK_GE(buffer_
.size(), byte_count
);
169 if (bit_offset_
!= 0) {
170 CHECK_GT(buffer_
.size(), 0u);
172 buffer_
.remove_prefix(byte_count
);
175 void HpackInputStream::ConsumeByteRemainder() {
176 if (bit_offset_
!= 0) {
177 ConsumeBits(8 - bit_offset_
);