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 | |
bnc | 3b58cfc | 2019-11-27 12:52:39 -0800 | [diff] [blame] | 5 | #include "net/third_party/quiche/src/quic/core/qpack/qpack_instructions.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6 | |
| 7 | #include <limits> |
| 8 | |
vasilvv | 0fb4443 | 2019-03-13 22:47:36 -0700 | [diff] [blame] | 9 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
QUICHE team | 11f55d4 | 2019-12-11 10:36:09 -0800 | [diff] [blame] | 10 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 11 | |
| 12 | namespace quic { |
| 13 | |
| 14 | namespace { |
| 15 | |
| 16 | // Validate that |
| 17 | // * in each instruction, the bits of |value| that are zero in |mask| are zero; |
| 18 | // * every byte matches exactly one opcode. |
| 19 | void ValidateLangague(const QpackLanguage* language) { |
| 20 | #ifndef NDEBUG |
| 21 | for (const auto* instruction : *language) { |
| 22 | DCHECK_EQ(0, instruction->opcode.value & ~instruction->opcode.mask); |
| 23 | } |
| 24 | |
| 25 | for (uint8_t byte = 0; byte < std::numeric_limits<uint8_t>::max(); ++byte) { |
| 26 | size_t match_count = 0; |
| 27 | for (const auto* instruction : *language) { |
| 28 | if ((byte & instruction->opcode.mask) == instruction->opcode.value) { |
| 29 | ++match_count; |
| 30 | } |
| 31 | } |
| 32 | DCHECK_EQ(1u, match_count) << static_cast<int>(byte); |
| 33 | } |
dschinazi | ce59da9 | 2019-06-18 19:58:59 -0700 | [diff] [blame] | 34 | #else |
| 35 | (void)language; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 36 | #endif |
| 37 | } |
| 38 | |
| 39 | } // namespace |
| 40 | |
| 41 | bool operator==(const QpackInstructionOpcode& a, |
| 42 | const QpackInstructionOpcode& b) { |
| 43 | return std::tie(a.value, a.mask) == std::tie(b.value, b.mask); |
| 44 | } |
| 45 | |
| 46 | const QpackInstruction* InsertWithNameReferenceInstruction() { |
| 47 | static const QpackInstructionOpcode* const opcode = |
| 48 | new QpackInstructionOpcode{0b10000000, 0b10000000}; |
| 49 | static const QpackInstruction* const instruction = |
| 50 | new QpackInstruction{*opcode, |
| 51 | {{QpackInstructionFieldType::kSbit, 0b01000000}, |
| 52 | {QpackInstructionFieldType::kVarint, 6}, |
| 53 | {QpackInstructionFieldType::kValue, 7}}}; |
| 54 | return instruction; |
| 55 | } |
| 56 | |
| 57 | const QpackInstruction* InsertWithoutNameReferenceInstruction() { |
| 58 | static const QpackInstructionOpcode* const opcode = |
| 59 | new QpackInstructionOpcode{0b01000000, 0b11000000}; |
| 60 | static const QpackInstruction* const instruction = |
| 61 | new QpackInstruction{*opcode, |
| 62 | {{QpackInstructionFieldType::kName, 5}, |
| 63 | {QpackInstructionFieldType::kValue, 7}}}; |
| 64 | return instruction; |
| 65 | } |
| 66 | |
| 67 | const QpackInstruction* DuplicateInstruction() { |
| 68 | static const QpackInstructionOpcode* const opcode = |
| 69 | new QpackInstructionOpcode{0b00000000, 0b11100000}; |
| 70 | static const QpackInstruction* const instruction = |
| 71 | new QpackInstruction{*opcode, {{QpackInstructionFieldType::kVarint, 5}}}; |
| 72 | return instruction; |
| 73 | } |
| 74 | |
| 75 | const QpackInstruction* SetDynamicTableCapacityInstruction() { |
| 76 | static const QpackInstructionOpcode* const opcode = |
| 77 | new QpackInstructionOpcode{0b00100000, 0b11100000}; |
| 78 | static const QpackInstruction* const instruction = |
| 79 | new QpackInstruction{*opcode, {{QpackInstructionFieldType::kVarint, 5}}}; |
| 80 | return instruction; |
| 81 | } |
| 82 | |
| 83 | const QpackLanguage* QpackEncoderStreamLanguage() { |
| 84 | static const QpackLanguage* const language = new QpackLanguage{ |
| 85 | InsertWithNameReferenceInstruction(), |
| 86 | InsertWithoutNameReferenceInstruction(), DuplicateInstruction(), |
| 87 | SetDynamicTableCapacityInstruction()}; |
| 88 | ValidateLangague(language); |
| 89 | return language; |
| 90 | } |
| 91 | |
| 92 | const QpackInstruction* InsertCountIncrementInstruction() { |
| 93 | static const QpackInstructionOpcode* const opcode = |
| 94 | new QpackInstructionOpcode{0b00000000, 0b11000000}; |
| 95 | static const QpackInstruction* const instruction = |
| 96 | new QpackInstruction{*opcode, {{QpackInstructionFieldType::kVarint, 6}}}; |
| 97 | return instruction; |
| 98 | } |
| 99 | |
| 100 | const QpackInstruction* HeaderAcknowledgementInstruction() { |
| 101 | static const QpackInstructionOpcode* const opcode = |
| 102 | new QpackInstructionOpcode{0b10000000, 0b10000000}; |
| 103 | static const QpackInstruction* const instruction = |
| 104 | new QpackInstruction{*opcode, {{QpackInstructionFieldType::kVarint, 7}}}; |
| 105 | return instruction; |
| 106 | } |
| 107 | |
| 108 | const QpackInstruction* StreamCancellationInstruction() { |
| 109 | static const QpackInstructionOpcode* const opcode = |
| 110 | new QpackInstructionOpcode{0b01000000, 0b11000000}; |
| 111 | static const QpackInstruction* const instruction = |
| 112 | new QpackInstruction{*opcode, {{QpackInstructionFieldType::kVarint, 6}}}; |
| 113 | return instruction; |
| 114 | } |
| 115 | |
| 116 | const QpackLanguage* QpackDecoderStreamLanguage() { |
| 117 | static const QpackLanguage* const language = new QpackLanguage{ |
| 118 | InsertCountIncrementInstruction(), HeaderAcknowledgementInstruction(), |
| 119 | StreamCancellationInstruction()}; |
| 120 | ValidateLangague(language); |
| 121 | return language; |
| 122 | } |
| 123 | |
| 124 | const QpackInstruction* QpackPrefixInstruction() { |
| 125 | // This opcode matches every input. |
| 126 | static const QpackInstructionOpcode* const opcode = |
| 127 | new QpackInstructionOpcode{0b00000000, 0b00000000}; |
| 128 | static const QpackInstruction* const instruction = |
| 129 | new QpackInstruction{*opcode, |
| 130 | {{QpackInstructionFieldType::kVarint, 8}, |
| 131 | {QpackInstructionFieldType::kSbit, 0b10000000}, |
| 132 | {QpackInstructionFieldType::kVarint2, 7}}}; |
| 133 | return instruction; |
| 134 | } |
| 135 | |
| 136 | const QpackLanguage* QpackPrefixLanguage() { |
| 137 | static const QpackLanguage* const language = |
| 138 | new QpackLanguage{QpackPrefixInstruction()}; |
| 139 | ValidateLangague(language); |
| 140 | return language; |
| 141 | } |
| 142 | |
| 143 | const QpackInstruction* QpackIndexedHeaderFieldInstruction() { |
| 144 | static const QpackInstructionOpcode* const opcode = |
| 145 | new QpackInstructionOpcode{0b10000000, 0b10000000}; |
| 146 | static const QpackInstruction* const instruction = |
| 147 | new QpackInstruction{*opcode, |
| 148 | {{QpackInstructionFieldType::kSbit, 0b01000000}, |
| 149 | {QpackInstructionFieldType::kVarint, 6}}}; |
| 150 | return instruction; |
| 151 | } |
| 152 | |
| 153 | const QpackInstruction* QpackIndexedHeaderFieldPostBaseInstruction() { |
| 154 | static const QpackInstructionOpcode* const opcode = |
| 155 | new QpackInstructionOpcode{0b00010000, 0b11110000}; |
| 156 | static const QpackInstruction* const instruction = |
| 157 | new QpackInstruction{*opcode, {{QpackInstructionFieldType::kVarint, 4}}}; |
| 158 | return instruction; |
| 159 | } |
| 160 | |
| 161 | const QpackInstruction* QpackLiteralHeaderFieldNameReferenceInstruction() { |
| 162 | static const QpackInstructionOpcode* const opcode = |
| 163 | new QpackInstructionOpcode{0b01000000, 0b11000000}; |
| 164 | static const QpackInstruction* const instruction = |
| 165 | new QpackInstruction{*opcode, |
| 166 | {{QpackInstructionFieldType::kSbit, 0b00010000}, |
| 167 | {QpackInstructionFieldType::kVarint, 4}, |
| 168 | {QpackInstructionFieldType::kValue, 7}}}; |
| 169 | return instruction; |
| 170 | } |
| 171 | |
| 172 | const QpackInstruction* QpackLiteralHeaderFieldPostBaseInstruction() { |
| 173 | static const QpackInstructionOpcode* const opcode = |
| 174 | new QpackInstructionOpcode{0b00000000, 0b11110000}; |
| 175 | static const QpackInstruction* const instruction = |
| 176 | new QpackInstruction{*opcode, |
| 177 | {{QpackInstructionFieldType::kVarint, 3}, |
| 178 | {QpackInstructionFieldType::kValue, 7}}}; |
| 179 | return instruction; |
| 180 | } |
| 181 | |
| 182 | const QpackInstruction* QpackLiteralHeaderFieldInstruction() { |
| 183 | static const QpackInstructionOpcode* const opcode = |
| 184 | new QpackInstructionOpcode{0b00100000, 0b11100000}; |
| 185 | static const QpackInstruction* const instruction = |
| 186 | new QpackInstruction{*opcode, |
| 187 | {{QpackInstructionFieldType::kName, 3}, |
| 188 | {QpackInstructionFieldType::kValue, 7}}}; |
| 189 | return instruction; |
| 190 | } |
| 191 | |
| 192 | const QpackLanguage* QpackRequestStreamLanguage() { |
| 193 | static const QpackLanguage* const language = |
| 194 | new QpackLanguage{QpackIndexedHeaderFieldInstruction(), |
| 195 | QpackIndexedHeaderFieldPostBaseInstruction(), |
| 196 | QpackLiteralHeaderFieldNameReferenceInstruction(), |
| 197 | QpackLiteralHeaderFieldPostBaseInstruction(), |
| 198 | QpackLiteralHeaderFieldInstruction()}; |
| 199 | ValidateLangague(language); |
| 200 | return language; |
| 201 | } |
| 202 | |
bnc | 1c1dcab | 2019-10-31 05:04:08 -0700 | [diff] [blame] | 203 | // static |
| 204 | QpackInstructionWithValues QpackInstructionWithValues::InsertWithNameReference( |
| 205 | bool is_static, |
| 206 | uint64_t name_index, |
QUICHE team | 11f55d4 | 2019-12-11 10:36:09 -0800 | [diff] [blame] | 207 | quiche::QuicheStringPiece value) { |
bnc | 1c1dcab | 2019-10-31 05:04:08 -0700 | [diff] [blame] | 208 | QpackInstructionWithValues instruction_with_values; |
| 209 | instruction_with_values.instruction_ = InsertWithNameReferenceInstruction(); |
| 210 | instruction_with_values.s_bit_ = is_static; |
| 211 | instruction_with_values.varint_ = name_index; |
| 212 | instruction_with_values.value_ = value; |
| 213 | |
| 214 | return instruction_with_values; |
| 215 | } |
| 216 | |
| 217 | // static |
| 218 | QpackInstructionWithValues |
QUICHE team | 11f55d4 | 2019-12-11 10:36:09 -0800 | [diff] [blame] | 219 | QpackInstructionWithValues::InsertWithoutNameReference( |
| 220 | quiche::QuicheStringPiece name, |
| 221 | quiche::QuicheStringPiece value) { |
bnc | 1c1dcab | 2019-10-31 05:04:08 -0700 | [diff] [blame] | 222 | QpackInstructionWithValues instruction_with_values; |
| 223 | instruction_with_values.instruction_ = |
| 224 | InsertWithoutNameReferenceInstruction(); |
| 225 | instruction_with_values.name_ = name; |
| 226 | instruction_with_values.value_ = value; |
| 227 | |
| 228 | return instruction_with_values; |
| 229 | } |
| 230 | |
| 231 | // static |
| 232 | QpackInstructionWithValues QpackInstructionWithValues::Duplicate( |
| 233 | uint64_t index) { |
| 234 | QpackInstructionWithValues instruction_with_values; |
| 235 | instruction_with_values.instruction_ = DuplicateInstruction(); |
| 236 | instruction_with_values.varint_ = index; |
| 237 | |
| 238 | return instruction_with_values; |
| 239 | } |
| 240 | |
| 241 | // static |
| 242 | QpackInstructionWithValues QpackInstructionWithValues::SetDynamicTableCapacity( |
| 243 | uint64_t capacity) { |
| 244 | QpackInstructionWithValues instruction_with_values; |
| 245 | instruction_with_values.instruction_ = SetDynamicTableCapacityInstruction(); |
| 246 | instruction_with_values.varint_ = capacity; |
| 247 | |
| 248 | return instruction_with_values; |
| 249 | } |
| 250 | |
| 251 | // static |
| 252 | QpackInstructionWithValues QpackInstructionWithValues::InsertCountIncrement( |
| 253 | uint64_t increment) { |
| 254 | QpackInstructionWithValues instruction_with_values; |
| 255 | instruction_with_values.instruction_ = InsertCountIncrementInstruction(); |
| 256 | instruction_with_values.varint_ = increment; |
| 257 | |
| 258 | return instruction_with_values; |
| 259 | } |
| 260 | |
| 261 | // static |
| 262 | QpackInstructionWithValues QpackInstructionWithValues::HeaderAcknowledgement( |
| 263 | uint64_t stream_id) { |
| 264 | QpackInstructionWithValues instruction_with_values; |
| 265 | instruction_with_values.instruction_ = HeaderAcknowledgementInstruction(); |
| 266 | instruction_with_values.varint_ = stream_id; |
| 267 | |
| 268 | return instruction_with_values; |
| 269 | } |
| 270 | |
| 271 | // static |
| 272 | QpackInstructionWithValues QpackInstructionWithValues::StreamCancellation( |
| 273 | uint64_t stream_id) { |
| 274 | QpackInstructionWithValues instruction_with_values; |
| 275 | instruction_with_values.instruction_ = StreamCancellationInstruction(); |
| 276 | instruction_with_values.varint_ = stream_id; |
| 277 | |
| 278 | return instruction_with_values; |
| 279 | } |
| 280 | |
| 281 | // static |
| 282 | QpackInstructionWithValues QpackInstructionWithValues::Prefix( |
| 283 | uint64_t required_insert_count) { |
| 284 | QpackInstructionWithValues instruction_with_values; |
| 285 | instruction_with_values.instruction_ = QpackPrefixInstruction(); |
| 286 | instruction_with_values.varint_ = required_insert_count; |
| 287 | instruction_with_values.varint2_ = 0; // Delta Base. |
| 288 | instruction_with_values.s_bit_ = false; // Delta Base sign. |
| 289 | |
| 290 | return instruction_with_values; |
| 291 | } |
| 292 | |
| 293 | // static |
| 294 | QpackInstructionWithValues QpackInstructionWithValues::IndexedHeaderField( |
| 295 | bool is_static, |
| 296 | uint64_t index) { |
| 297 | QpackInstructionWithValues instruction_with_values; |
| 298 | instruction_with_values.instruction_ = QpackIndexedHeaderFieldInstruction(); |
| 299 | instruction_with_values.s_bit_ = is_static; |
| 300 | instruction_with_values.varint_ = index; |
| 301 | |
| 302 | return instruction_with_values; |
| 303 | } |
| 304 | |
| 305 | // static |
| 306 | QpackInstructionWithValues |
| 307 | QpackInstructionWithValues::LiteralHeaderFieldNameReference( |
| 308 | bool is_static, |
| 309 | uint64_t index, |
QUICHE team | 11f55d4 | 2019-12-11 10:36:09 -0800 | [diff] [blame] | 310 | quiche::QuicheStringPiece value) { |
bnc | 1c1dcab | 2019-10-31 05:04:08 -0700 | [diff] [blame] | 311 | QpackInstructionWithValues instruction_with_values; |
| 312 | instruction_with_values.instruction_ = |
| 313 | QpackLiteralHeaderFieldNameReferenceInstruction(); |
| 314 | instruction_with_values.s_bit_ = is_static; |
| 315 | instruction_with_values.varint_ = index; |
| 316 | instruction_with_values.value_ = value; |
| 317 | |
| 318 | return instruction_with_values; |
| 319 | } |
| 320 | |
| 321 | // static |
| 322 | QpackInstructionWithValues QpackInstructionWithValues::LiteralHeaderField( |
QUICHE team | 11f55d4 | 2019-12-11 10:36:09 -0800 | [diff] [blame] | 323 | quiche::QuicheStringPiece name, |
| 324 | quiche::QuicheStringPiece value) { |
bnc | 1c1dcab | 2019-10-31 05:04:08 -0700 | [diff] [blame] | 325 | QpackInstructionWithValues instruction_with_values; |
| 326 | instruction_with_values.instruction_ = QpackLiteralHeaderFieldInstruction(); |
| 327 | instruction_with_values.name_ = name; |
| 328 | instruction_with_values.value_ = value; |
| 329 | |
| 330 | return instruction_with_values; |
| 331 | } |
| 332 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 333 | } // namespace quic |