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 5 #include "quiche/http2/test_tools/hpack_example.h" 6 7 #include <ctype.h> 8 9 #include "absl/strings/escaping.h" 10 #include "absl/strings/str_cat.h" 11 #include "quiche/common/platform/api/quiche_bug_tracker.h" 12 #include "quiche/common/platform/api/quiche_logging.h" 13 14 namespace http2 { 15 namespace test { 16 namespace { 17 HpackExampleToStringOrDie(absl::string_view example,std::string * output)18void HpackExampleToStringOrDie(absl::string_view example, std::string* output) { 19 while (!example.empty()) { 20 const char c0 = example[0]; 21 if (isxdigit(c0)) { 22 QUICHE_CHECK_GT(example.size(), 1u) << "Truncated hex byte?"; 23 const char c1 = example[1]; 24 QUICHE_CHECK(isxdigit(c1)) << "Found half a byte?"; 25 *output += absl::HexStringToBytes(example.substr(0, 2)); 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'); 36 if (pos == absl::string_view::npos) { 37 // End of input. 38 break; 39 } 40 example.remove_prefix(pos + 1); 41 continue; 42 } 43 QUICHE_BUG(http2_bug_107_1) 44 << "Can't parse byte " << static_cast<int>(c0) 45 << absl::StrCat(" (0x", absl::Hex(c0), ")") << "\nExample: " << example; 46 } 47 QUICHE_CHECK_LT(0u, output->size()) << "Example is empty."; 48 } 49 50 } // namespace 51 HpackExampleToStringOrDie(absl::string_view example)52std::string HpackExampleToStringOrDie(absl::string_view example) { 53 std::string output; 54 HpackExampleToStringOrDie(example, &output); 55 return output; 56 } 57 58 } // namespace test 59 } // namespace http2 60