| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "net/spdy/hpack/hpack_output_stream.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "net/spdy/platform/api/spdy_estimate_memory_usage.h" | |
| 11 | |
| 12 namespace net { | |
| 13 | |
| 14 HpackOutputStream::HpackOutputStream() : bit_offset_(0) {} | |
| 15 | |
| 16 HpackOutputStream::~HpackOutputStream() {} | |
| 17 | |
| 18 void HpackOutputStream::AppendBits(uint8_t bits, size_t bit_size) { | |
| 19 DCHECK_GT(bit_size, 0u); | |
| 20 DCHECK_LE(bit_size, 8u); | |
| 21 DCHECK_EQ(bits >> bit_size, 0); | |
| 22 size_t new_bit_offset = bit_offset_ + bit_size; | |
| 23 if (bit_offset_ == 0) { | |
| 24 // Buffer ends on a byte boundary. | |
| 25 DCHECK_LE(bit_size, 8u); | |
| 26 buffer_.append(1, bits << (8 - bit_size)); | |
| 27 } else if (new_bit_offset <= 8) { | |
| 28 // Buffer does not end on a byte boundary but the given bits fit | |
| 29 // in the remainder of the last byte. | |
| 30 *buffer_.rbegin() |= bits << (8 - new_bit_offset); | |
| 31 } else { | |
| 32 // Buffer does not end on a byte boundary and the given bits do | |
| 33 // not fit in the remainder of the last byte. | |
| 34 *buffer_.rbegin() |= bits >> (new_bit_offset - 8); | |
| 35 buffer_.append(1, bits << (16 - new_bit_offset)); | |
| 36 } | |
| 37 bit_offset_ = new_bit_offset % 8; | |
| 38 } | |
| 39 | |
| 40 void HpackOutputStream::AppendPrefix(HpackPrefix prefix) { | |
| 41 AppendBits(prefix.bits, prefix.bit_size); | |
| 42 } | |
| 43 | |
| 44 void HpackOutputStream::AppendBytes(SpdyStringPiece buffer) { | |
| 45 DCHECK_EQ(bit_offset_, 0u); | |
| 46 buffer_.append(buffer.data(), buffer.size()); | |
| 47 } | |
| 48 | |
| 49 void HpackOutputStream::AppendUint32(uint32_t I) { | |
| 50 // The algorithm below is adapted from the pseudocode in 6.1. | |
| 51 size_t N = 8 - bit_offset_; | |
| 52 uint8_t max_first_byte = static_cast<uint8_t>((1 << N) - 1); | |
| 53 if (I < max_first_byte) { | |
| 54 AppendBits(static_cast<uint8_t>(I), N); | |
| 55 } else { | |
| 56 AppendBits(max_first_byte, N); | |
| 57 I -= max_first_byte; | |
| 58 while ((I & ~0x7f) != 0) { | |
| 59 buffer_.append(1, (I & 0x7f) | 0x80); | |
| 60 I >>= 7; | |
| 61 } | |
| 62 AppendBits(static_cast<uint8_t>(I), 8); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 void HpackOutputStream::TakeString(SpdyString* output) { | |
| 67 // This must hold, since all public functions cause the buffer to | |
| 68 // end on a byte boundary. | |
| 69 DCHECK_EQ(bit_offset_, 0u); | |
| 70 buffer_.swap(*output); | |
| 71 buffer_.clear(); | |
| 72 bit_offset_ = 0; | |
| 73 } | |
| 74 | |
| 75 void HpackOutputStream::BoundedTakeString(size_t max_size, SpdyString* output) { | |
| 76 if (buffer_.size() > max_size) { | |
| 77 // Save off overflow bytes to temporary string (causes a copy). | |
| 78 SpdyString overflow(buffer_.data() + max_size, buffer_.size() - max_size); | |
| 79 | |
| 80 // Resize buffer down to the given limit. | |
| 81 buffer_.resize(max_size); | |
| 82 | |
| 83 // Give buffer to output string. | |
| 84 *output = std::move(buffer_); | |
| 85 | |
| 86 // Reset to contain overflow. | |
| 87 buffer_ = std::move(overflow); | |
| 88 } else { | |
| 89 TakeString(output); | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 size_t HpackOutputStream::EstimateMemoryUsage() const { | |
| 94 return SpdyEstimateMemoryUsage(buffer_); | |
| 95 } | |
| 96 | |
| 97 } // namespace net | |
| OLD | NEW |