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