| 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_output_stream.h" | 5 #include "net/spdy/hpack/hpack_output_stream.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 | 8 |
| 9 namespace net { | 9 namespace net { |
| 10 | 10 |
| 11 using base::StringPiece; | 11 using base::StringPiece; |
| 12 using std::string; | 12 using std::string; |
| 13 | 13 |
| 14 HpackOutputStream::HpackOutputStream() : bit_offset_(0) {} | 14 HpackOutputStream::HpackOutputStream() : bit_offset_(0) {} |
| 15 | 15 |
| 16 HpackOutputStream::~HpackOutputStream() {} | 16 HpackOutputStream::~HpackOutputStream() {} |
| 17 | 17 |
| 18 void HpackOutputStream::AppendBits(uint8 bits, size_t bit_size) { | 18 void HpackOutputStream::AppendBits(uint8_t bits, size_t bit_size) { |
| 19 DCHECK_GT(bit_size, 0u); | 19 DCHECK_GT(bit_size, 0u); |
| 20 DCHECK_LE(bit_size, 8u); | 20 DCHECK_LE(bit_size, 8u); |
| 21 DCHECK_EQ(bits >> bit_size, 0); | 21 DCHECK_EQ(bits >> bit_size, 0); |
| 22 size_t new_bit_offset = bit_offset_ + bit_size; | 22 size_t new_bit_offset = bit_offset_ + bit_size; |
| 23 if (bit_offset_ == 0) { | 23 if (bit_offset_ == 0) { |
| 24 // Buffer ends on a byte boundary. | 24 // Buffer ends on a byte boundary. |
| 25 DCHECK_LE(bit_size, 8u); | 25 DCHECK_LE(bit_size, 8u); |
| 26 buffer_.append(1, bits << (8 - bit_size)); | 26 buffer_.append(1, bits << (8 - bit_size)); |
| 27 } else if (new_bit_offset <= 8) { | 27 } else if (new_bit_offset <= 8) { |
| 28 // Buffer does not end on a byte boundary but the given bits fit | 28 // Buffer does not end on a byte boundary but the given bits fit |
| (...skipping 10 matching lines...) Expand all Loading... |
| 39 | 39 |
| 40 void HpackOutputStream::AppendPrefix(HpackPrefix prefix) { | 40 void HpackOutputStream::AppendPrefix(HpackPrefix prefix) { |
| 41 AppendBits(prefix.bits, prefix.bit_size); | 41 AppendBits(prefix.bits, prefix.bit_size); |
| 42 } | 42 } |
| 43 | 43 |
| 44 void HpackOutputStream::AppendBytes(StringPiece buffer) { | 44 void HpackOutputStream::AppendBytes(StringPiece buffer) { |
| 45 DCHECK_EQ(bit_offset_, 0u); | 45 DCHECK_EQ(bit_offset_, 0u); |
| 46 buffer_.append(buffer.data(), buffer.size()); | 46 buffer_.append(buffer.data(), buffer.size()); |
| 47 } | 47 } |
| 48 | 48 |
| 49 void HpackOutputStream::AppendUint32(uint32 I) { | 49 void HpackOutputStream::AppendUint32(uint32_t I) { |
| 50 // The algorithm below is adapted from the pseudocode in 6.1. | 50 // The algorithm below is adapted from the pseudocode in 6.1. |
| 51 size_t N = 8 - bit_offset_; | 51 size_t N = 8 - bit_offset_; |
| 52 uint8 max_first_byte = static_cast<uint8>((1 << N) - 1); | 52 uint8_t max_first_byte = static_cast<uint8_t>((1 << N) - 1); |
| 53 if (I < max_first_byte) { | 53 if (I < max_first_byte) { |
| 54 AppendBits(static_cast<uint8>(I), N); | 54 AppendBits(static_cast<uint8_t>(I), N); |
| 55 } else { | 55 } else { |
| 56 AppendBits(max_first_byte, N); | 56 AppendBits(max_first_byte, N); |
| 57 I -= max_first_byte; | 57 I -= max_first_byte; |
| 58 while ((I & ~0x7f) != 0) { | 58 while ((I & ~0x7f) != 0) { |
| 59 buffer_.append(1, (I & 0x7f) | 0x80); | 59 buffer_.append(1, (I & 0x7f) | 0x80); |
| 60 I >>= 7; | 60 I >>= 7; |
| 61 } | 61 } |
| 62 AppendBits(static_cast<uint8>(I), 8); | 62 AppendBits(static_cast<uint8_t>(I), 8); |
| 63 } | 63 } |
| 64 } | 64 } |
| 65 | 65 |
| 66 void HpackOutputStream::TakeString(string* output) { | 66 void HpackOutputStream::TakeString(string* output) { |
| 67 // This must hold, since all public functions cause the buffer to | 67 // This must hold, since all public functions cause the buffer to |
| 68 // end on a byte boundary. | 68 // end on a byte boundary. |
| 69 DCHECK_EQ(bit_offset_, 0u); | 69 DCHECK_EQ(bit_offset_, 0u); |
| 70 buffer_.swap(*output); | 70 buffer_.swap(*output); |
| 71 buffer_.clear(); | 71 buffer_.clear(); |
| 72 bit_offset_ = 0; | 72 bit_offset_ = 0; |
| 73 } | 73 } |
| 74 | 74 |
| 75 } // namespace net | 75 } // namespace net |
| OLD | NEW |