| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/http2/hpack/tools/hpack_example.h" | 5 #include "net/http2/hpack/tools/hpack_example.h" |
| 6 | 6 |
| 7 #include <ctype.h> | 7 #include <ctype.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "net/spdy/spdy_test_utils.h" | 10 #include "net/spdy/spdy_test_utils.h" |
| 11 | 11 |
| 12 using base::StringPiece; | 12 using base::StringPiece; |
| 13 using std::string; | 13 using std::string; |
| 14 | 14 |
| 15 namespace net { | 15 namespace net { |
| 16 namespace test { | 16 namespace test { |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 void HpackExampleToStringOrDie(StringPiece example, string* output) { | 19 void HpackExampleToStringOrDie(StringPiece example, string* output) { |
| 20 while (!example.empty()) { | 20 while (!example.empty()) { |
| 21 const char c0 = example[0]; | 21 const char c0 = example[0]; |
| 22 if (isxdigit(c0)) { | 22 if (isxdigit(c0)) { |
| 23 CHECK_GT(example.size(), 1u) << "Truncated hex byte?"; | 23 // Truncated hex byte? |
| 24 CHECK_GT(example.size(), 1u); |
| 24 const char c1 = example[1]; | 25 const char c1 = example[1]; |
| 25 CHECK(isxdigit(c1)) << "Found half a byte?"; | 26 // Found half a byte? |
| 27 CHECK(isxdigit(c1)); |
| 26 *output += a2b_hex(example.substr(0, 2).as_string().c_str()); | 28 *output += a2b_hex(example.substr(0, 2).as_string().c_str()); |
| 27 example.remove_prefix(2); | 29 example.remove_prefix(2); |
| 28 continue; | 30 continue; |
| 29 } | 31 } |
| 30 if (isspace(c0)) { | 32 if (isspace(c0)) { |
| 31 example.remove_prefix(1); | 33 example.remove_prefix(1); |
| 32 continue; | 34 continue; |
| 33 } | 35 } |
| 34 if (example.starts_with("|")) { | 36 if (example.starts_with("|")) { |
| 35 // Start of a comment. Skip to end of line or of input. | 37 // Start of a comment. Skip to end of line or of input. |
| 36 auto pos = example.find('\n'); | 38 auto pos = example.find('\n'); |
| 37 if (pos == StringPiece::npos) { | 39 if (pos == StringPiece::npos) { |
| 38 // End of input. | 40 // End of input. |
| 39 break; | 41 break; |
| 40 } | 42 } |
| 41 example.remove_prefix(pos + 1); | 43 example.remove_prefix(pos + 1); |
| 42 continue; | 44 continue; |
| 43 } | 45 } |
| 44 CHECK(false) << "Can't parse byte " << static_cast<int>(c0) << " (0x" | 46 CHECK(false); |
| 45 << std::hex << c0 << ")" | |
| 46 << "\nExample: " << example; | |
| 47 } | 47 } |
| 48 CHECK_LT(0u, output->size()) << "Example is empty."; | 48 // Example is empty. |
| 49 CHECK_LT(0u, output->size()); |
| 49 return; | 50 return; |
| 50 } | 51 } |
| 51 | 52 |
| 52 } // namespace | 53 } // namespace |
| 53 | 54 |
| 54 string HpackExampleToStringOrDie(StringPiece example) { | 55 string HpackExampleToStringOrDie(StringPiece example) { |
| 55 string output; | 56 string output; |
| 56 HpackExampleToStringOrDie(example, &output); | 57 HpackExampleToStringOrDie(example, &output); |
| 57 return output; | 58 return output; |
| 58 } | 59 } |
| 59 | 60 |
| 60 } // namespace test | 61 } // namespace test |
| 61 } // namespace net | 62 } // namespace net |
| OLD | NEW |