QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 1 | // Copyright 2016 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 "http2/hpack/tools/hpack_example.h" |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 6 | |
| 7 | #include <ctype.h> |
| 8 | |
bnc | 3bf526d | 2021-04-19 05:01:31 -0700 | [diff] [blame] | 9 | #include "absl/strings/escaping.h" |
vasilvv | bca9deb | 2020-12-03 13:42:00 -0800 | [diff] [blame] | 10 | #include "absl/strings/str_cat.h" |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 11 | #include "http2/platform/api/http2_bug_tracker.h" |
| 12 | #include "http2/platform/api/http2_logging.h" |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 13 | |
| 14 | namespace http2 { |
| 15 | namespace test { |
| 16 | namespace { |
| 17 | |
vasilvv | 999985c | 2020-10-12 19:15:19 -0700 | [diff] [blame] | 18 | void HpackExampleToStringOrDie(absl::string_view example, std::string* output) { |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 19 | while (!example.empty()) { |
| 20 | const char c0 = example[0]; |
| 21 | if (isxdigit(c0)) { |
vasilvv | afcc317 | 2021-02-02 12:01:07 -0800 | [diff] [blame] | 22 | QUICHE_CHECK_GT(example.size(), 1u) << "Truncated hex byte?"; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 23 | const char c1 = example[1]; |
vasilvv | afcc317 | 2021-02-02 12:01:07 -0800 | [diff] [blame] | 24 | QUICHE_CHECK(isxdigit(c1)) << "Found half a byte?"; |
bnc | 3bf526d | 2021-04-19 05:01:31 -0700 | [diff] [blame] | 25 | *output += absl::HexStringToBytes(example.substr(0, 2)); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 26 | example.remove_prefix(2); |
| 27 | continue; |
| 28 | } |
| 29 | if (isspace(c0)) { |
| 30 | example.remove_prefix(1); |
| 31 | continue; |
| 32 | } |
| 33 | if (!example.empty() && example[0] == '|') { |
| 34 | // Start of a comment. Skip to end of line or of input. |
| 35 | auto pos = example.find('\n'); |
vasilvv | 999985c | 2020-10-12 19:15:19 -0700 | [diff] [blame] | 36 | if (pos == absl::string_view::npos) { |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 37 | // End of input. |
| 38 | break; |
| 39 | } |
| 40 | example.remove_prefix(pos + 1); |
| 41 | continue; |
| 42 | } |
QUICHE team | be19128 | 2021-03-18 09:44:39 -0700 | [diff] [blame] | 43 | HTTP2_BUG(http2_bug_107_1) |
QUICHE team | 0ec81ca | 2021-03-11 09:29:06 -0800 | [diff] [blame] | 44 | << "Can't parse byte " << static_cast<int>(c0) |
bnc | 760e723 | 2021-03-25 11:14:30 -0700 | [diff] [blame] | 45 | << absl::StrCat(" (0x", absl::Hex(c0), ")") << "\nExample: " << example; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 46 | } |
vasilvv | afcc317 | 2021-02-02 12:01:07 -0800 | [diff] [blame] | 47 | QUICHE_CHECK_LT(0u, output->size()) << "Example is empty."; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | } // namespace |
| 51 | |
vasilvv | 999985c | 2020-10-12 19:15:19 -0700 | [diff] [blame] | 52 | std::string HpackExampleToStringOrDie(absl::string_view example) { |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 53 | std::string output; |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 54 | HpackExampleToStringOrDie(example, &output); |
| 55 | return output; |
| 56 | } |
| 57 | |
| 58 | } // namespace test |
| 59 | } // namespace http2 |