QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright 2018 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 | |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 5 | #include "quic/core/qpack/qpack_instruction_decoder.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6 | |
| 7 | #include <algorithm> |
| 8 | #include <utility> |
| 9 | |
vasilvv | 7cac7b0 | 2020-10-08 12:32:10 -0700 | [diff] [blame] | 10 | #include "absl/strings/string_view.h" |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 11 | #include "quic/platform/api/quic_bug_tracker.h" |
| 12 | #include "quic/platform/api/quic_logging.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 13 | |
| 14 | namespace quic { |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | // Maximum length of header name and header value. This limits the amount of |
| 19 | // memory the peer can make the decoder allocate when sending string literals. |
| 20 | const size_t kStringLiteralLengthLimit = 1024 * 1024; |
| 21 | |
| 22 | } // namespace |
| 23 | |
| 24 | QpackInstructionDecoder::QpackInstructionDecoder(const QpackLanguage* language, |
| 25 | Delegate* delegate) |
| 26 | : language_(language), |
| 27 | delegate_(delegate), |
| 28 | s_bit_(false), |
| 29 | varint_(0), |
| 30 | varint2_(0), |
| 31 | is_huffman_encoded_(false), |
| 32 | string_length_(0), |
| 33 | error_detected_(false), |
| 34 | state_(State::kStartInstruction) {} |
| 35 | |
vasilvv | 7cac7b0 | 2020-10-08 12:32:10 -0700 | [diff] [blame] | 36 | bool QpackInstructionDecoder::Decode(absl::string_view data) { |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 37 | QUICHE_DCHECK(!data.empty()); |
| 38 | QUICHE_DCHECK(!error_detected_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 39 | |
| 40 | while (true) { |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 41 | bool success = true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 42 | size_t bytes_consumed = 0; |
| 43 | |
| 44 | switch (state_) { |
| 45 | case State::kStartInstruction: |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 46 | success = DoStartInstruction(data); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 47 | break; |
| 48 | case State::kStartField: |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 49 | success = DoStartField(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 50 | break; |
| 51 | case State::kReadBit: |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 52 | success = DoReadBit(data); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 53 | break; |
| 54 | case State::kVarintStart: |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 55 | success = DoVarintStart(data, &bytes_consumed); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 56 | break; |
| 57 | case State::kVarintResume: |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 58 | success = DoVarintResume(data, &bytes_consumed); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 59 | break; |
| 60 | case State::kVarintDone: |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 61 | success = DoVarintDone(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 62 | break; |
| 63 | case State::kReadString: |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 64 | success = DoReadString(data, &bytes_consumed); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 65 | break; |
| 66 | case State::kReadStringDone: |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 67 | success = DoReadStringDone(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 68 | break; |
| 69 | } |
| 70 | |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 71 | if (!success) { |
| 72 | return false; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 73 | } |
| 74 | |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 75 | // |success| must be false if an error is detected. |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 76 | QUICHE_DCHECK(!error_detected_); |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 77 | |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 78 | QUICHE_DCHECK_LE(bytes_consumed, data.size()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 79 | |
vasilvv | 7cac7b0 | 2020-10-08 12:32:10 -0700 | [diff] [blame] | 80 | data = absl::string_view(data.data() + bytes_consumed, |
| 81 | data.size() - bytes_consumed); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 82 | |
| 83 | // Stop processing if no more data but next state would require it. |
| 84 | if (data.empty() && (state_ != State::kStartField) && |
| 85 | (state_ != State::kVarintDone) && (state_ != State::kReadStringDone)) { |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 86 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 87 | } |
| 88 | } |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 89 | |
| 90 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | bool QpackInstructionDecoder::AtInstructionBoundary() const { |
| 94 | return state_ == State::kStartInstruction; |
| 95 | } |
| 96 | |
vasilvv | 7cac7b0 | 2020-10-08 12:32:10 -0700 | [diff] [blame] | 97 | bool QpackInstructionDecoder::DoStartInstruction(absl::string_view data) { |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 98 | QUICHE_DCHECK(!data.empty()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 99 | |
| 100 | instruction_ = LookupOpcode(data[0]); |
| 101 | field_ = instruction_->fields.begin(); |
| 102 | |
| 103 | state_ = State::kStartField; |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 104 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 105 | } |
| 106 | |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 107 | bool QpackInstructionDecoder::DoStartField() { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 108 | if (field_ == instruction_->fields.end()) { |
| 109 | // Completed decoding this instruction. |
| 110 | |
| 111 | if (!delegate_->OnInstructionDecoded(instruction_)) { |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 112 | return false; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | state_ = State::kStartInstruction; |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 116 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | switch (field_->type) { |
| 120 | case QpackInstructionFieldType::kSbit: |
| 121 | case QpackInstructionFieldType::kName: |
| 122 | case QpackInstructionFieldType::kValue: |
| 123 | state_ = State::kReadBit; |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 124 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 125 | case QpackInstructionFieldType::kVarint: |
| 126 | case QpackInstructionFieldType::kVarint2: |
| 127 | state_ = State::kVarintStart; |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 128 | return true; |
| 129 | default: |
QUICHE team | 407e1c7 | 2021-03-16 14:46:23 -0700 | [diff] [blame] | 130 | QUIC_BUG(quic_bug_10767_1) << "Invalid field type."; |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 131 | return false; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
vasilvv | 7cac7b0 | 2020-10-08 12:32:10 -0700 | [diff] [blame] | 135 | bool QpackInstructionDecoder::DoReadBit(absl::string_view data) { |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 136 | QUICHE_DCHECK(!data.empty()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 137 | |
| 138 | switch (field_->type) { |
| 139 | case QpackInstructionFieldType::kSbit: { |
| 140 | const uint8_t bitmask = field_->param; |
| 141 | s_bit_ = (data[0] & bitmask) == bitmask; |
| 142 | |
| 143 | ++field_; |
| 144 | state_ = State::kStartField; |
| 145 | |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 146 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 147 | } |
| 148 | case QpackInstructionFieldType::kName: |
| 149 | case QpackInstructionFieldType::kValue: { |
| 150 | const uint8_t prefix_length = field_->param; |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 151 | QUICHE_DCHECK_GE(7, prefix_length); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 152 | const uint8_t bitmask = 1 << prefix_length; |
| 153 | is_huffman_encoded_ = (data[0] & bitmask) == bitmask; |
| 154 | |
| 155 | state_ = State::kVarintStart; |
| 156 | |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 157 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 158 | } |
| 159 | default: |
QUICHE team | 407e1c7 | 2021-03-16 14:46:23 -0700 | [diff] [blame] | 160 | QUIC_BUG(quic_bug_10767_2) << "Invalid field type."; |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 161 | return false; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
vasilvv | 7cac7b0 | 2020-10-08 12:32:10 -0700 | [diff] [blame] | 165 | bool QpackInstructionDecoder::DoVarintStart(absl::string_view data, |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 166 | size_t* bytes_consumed) { |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 167 | QUICHE_DCHECK(!data.empty()); |
| 168 | QUICHE_DCHECK(field_->type == QpackInstructionFieldType::kVarint || |
| 169 | field_->type == QpackInstructionFieldType::kVarint2 || |
| 170 | field_->type == QpackInstructionFieldType::kName || |
| 171 | field_->type == QpackInstructionFieldType::kValue); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 172 | |
| 173 | http2::DecodeBuffer buffer(data.data() + 1, data.size() - 1); |
| 174 | http2::DecodeStatus status = |
| 175 | varint_decoder_.Start(data[0], field_->param, &buffer); |
| 176 | |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 177 | *bytes_consumed = 1 + buffer.Offset(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 178 | switch (status) { |
| 179 | case http2::DecodeStatus::kDecodeDone: |
| 180 | state_ = State::kVarintDone; |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 181 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 182 | case http2::DecodeStatus::kDecodeInProgress: |
| 183 | state_ = State::kVarintResume; |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 184 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 185 | case http2::DecodeStatus::kDecodeError: |
bnc | 4e44010 | 2020-10-20 17:32:29 -0700 | [diff] [blame] | 186 | OnError(ErrorCode::INTEGER_TOO_LARGE, "Encoded integer too large."); |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 187 | return false; |
danzh | 9693b99 | 2019-07-12 11:05:45 -0700 | [diff] [blame] | 188 | default: |
QUICHE team | 407e1c7 | 2021-03-16 14:46:23 -0700 | [diff] [blame] | 189 | QUIC_BUG(quic_bug_10767_3) << "Unknown decode status " << status; |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 190 | return false; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
vasilvv | 7cac7b0 | 2020-10-08 12:32:10 -0700 | [diff] [blame] | 194 | bool QpackInstructionDecoder::DoVarintResume(absl::string_view data, |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 195 | size_t* bytes_consumed) { |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 196 | QUICHE_DCHECK(!data.empty()); |
| 197 | QUICHE_DCHECK(field_->type == QpackInstructionFieldType::kVarint || |
| 198 | field_->type == QpackInstructionFieldType::kVarint2 || |
| 199 | field_->type == QpackInstructionFieldType::kName || |
| 200 | field_->type == QpackInstructionFieldType::kValue); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 201 | |
| 202 | http2::DecodeBuffer buffer(data); |
| 203 | http2::DecodeStatus status = varint_decoder_.Resume(&buffer); |
| 204 | |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 205 | *bytes_consumed = buffer.Offset(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 206 | switch (status) { |
| 207 | case http2::DecodeStatus::kDecodeDone: |
| 208 | state_ = State::kVarintDone; |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 209 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 210 | case http2::DecodeStatus::kDecodeInProgress: |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 211 | QUICHE_DCHECK_EQ(*bytes_consumed, data.size()); |
| 212 | QUICHE_DCHECK(buffer.Empty()); |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 213 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 214 | case http2::DecodeStatus::kDecodeError: |
bnc | 4e44010 | 2020-10-20 17:32:29 -0700 | [diff] [blame] | 215 | OnError(ErrorCode::INTEGER_TOO_LARGE, "Encoded integer too large."); |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 216 | return false; |
danzh | 9693b99 | 2019-07-12 11:05:45 -0700 | [diff] [blame] | 217 | default: |
QUICHE team | 407e1c7 | 2021-03-16 14:46:23 -0700 | [diff] [blame] | 218 | QUIC_BUG(quic_bug_10767_4) << "Unknown decode status " << status; |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 219 | return false; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 223 | bool QpackInstructionDecoder::DoVarintDone() { |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 224 | QUICHE_DCHECK(field_->type == QpackInstructionFieldType::kVarint || |
| 225 | field_->type == QpackInstructionFieldType::kVarint2 || |
| 226 | field_->type == QpackInstructionFieldType::kName || |
| 227 | field_->type == QpackInstructionFieldType::kValue); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 228 | |
| 229 | if (field_->type == QpackInstructionFieldType::kVarint) { |
| 230 | varint_ = varint_decoder_.value(); |
| 231 | |
| 232 | ++field_; |
| 233 | state_ = State::kStartField; |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 234 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | if (field_->type == QpackInstructionFieldType::kVarint2) { |
| 238 | varint2_ = varint_decoder_.value(); |
| 239 | |
| 240 | ++field_; |
| 241 | state_ = State::kStartField; |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 242 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | string_length_ = varint_decoder_.value(); |
| 246 | if (string_length_ > kStringLiteralLengthLimit) { |
bnc | 4e44010 | 2020-10-20 17:32:29 -0700 | [diff] [blame] | 247 | OnError(ErrorCode::STRING_LITERAL_TOO_LONG, "String literal too long."); |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 248 | return false; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 249 | } |
| 250 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 251 | std::string* const string = |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 252 | (field_->type == QpackInstructionFieldType::kName) ? &name_ : &value_; |
| 253 | string->clear(); |
| 254 | |
| 255 | if (string_length_ == 0) { |
| 256 | ++field_; |
| 257 | state_ = State::kStartField; |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 258 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | string->reserve(string_length_); |
| 262 | |
| 263 | state_ = State::kReadString; |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 264 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 265 | } |
| 266 | |
vasilvv | 7cac7b0 | 2020-10-08 12:32:10 -0700 | [diff] [blame] | 267 | bool QpackInstructionDecoder::DoReadString(absl::string_view data, |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 268 | size_t* bytes_consumed) { |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 269 | QUICHE_DCHECK(!data.empty()); |
| 270 | QUICHE_DCHECK(field_->type == QpackInstructionFieldType::kName || |
| 271 | field_->type == QpackInstructionFieldType::kValue); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 272 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 273 | std::string* const string = |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 274 | (field_->type == QpackInstructionFieldType::kName) ? &name_ : &value_; |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 275 | QUICHE_DCHECK_LT(string->size(), string_length_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 276 | |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 277 | *bytes_consumed = std::min(string_length_ - string->size(), data.size()); |
| 278 | string->append(data.data(), *bytes_consumed); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 279 | |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 280 | QUICHE_DCHECK_LE(string->size(), string_length_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 281 | if (string->size() == string_length_) { |
| 282 | state_ = State::kReadStringDone; |
| 283 | } |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 284 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 285 | } |
| 286 | |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 287 | bool QpackInstructionDecoder::DoReadStringDone() { |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 288 | QUICHE_DCHECK(field_->type == QpackInstructionFieldType::kName || |
| 289 | field_->type == QpackInstructionFieldType::kValue); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 290 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 291 | std::string* const string = |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 292 | (field_->type == QpackInstructionFieldType::kName) ? &name_ : &value_; |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 293 | QUICHE_DCHECK_EQ(string->size(), string_length_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 294 | |
| 295 | if (is_huffman_encoded_) { |
| 296 | huffman_decoder_.Reset(); |
| 297 | // HpackHuffmanDecoder::Decode() cannot perform in-place decoding. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 298 | std::string decoded_value; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 299 | huffman_decoder_.Decode(*string, &decoded_value); |
| 300 | if (!huffman_decoder_.InputProperlyTerminated()) { |
bnc | 4e44010 | 2020-10-20 17:32:29 -0700 | [diff] [blame] | 301 | OnError(ErrorCode::HUFFMAN_ENCODING_ERROR, |
| 302 | "Error in Huffman-encoded string."); |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 303 | return false; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 304 | } |
| 305 | *string = std::move(decoded_value); |
| 306 | } |
| 307 | |
| 308 | ++field_; |
| 309 | state_ = State::kStartField; |
bnc | fb4f4fc | 2019-11-18 17:14:56 -0800 | [diff] [blame] | 310 | return true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | const QpackInstruction* QpackInstructionDecoder::LookupOpcode( |
| 314 | uint8_t byte) const { |
| 315 | for (const auto* instruction : *language_) { |
| 316 | if ((byte & instruction->opcode.mask) == instruction->opcode.value) { |
| 317 | return instruction; |
| 318 | } |
| 319 | } |
| 320 | // |language_| should be defined such that instruction opcodes cover every |
| 321 | // possible input. |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 322 | QUICHE_DCHECK(false); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 323 | return nullptr; |
| 324 | } |
| 325 | |
bnc | 4e44010 | 2020-10-20 17:32:29 -0700 | [diff] [blame] | 326 | void QpackInstructionDecoder::OnError(ErrorCode error_code, |
| 327 | absl::string_view error_message) { |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 328 | QUICHE_DCHECK(!error_detected_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 329 | |
| 330 | error_detected_ = true; |
bnc | 4e44010 | 2020-10-20 17:32:29 -0700 | [diff] [blame] | 331 | delegate_->OnInstructionDecodingError(error_code, error_message); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | } // namespace quic |