OLD | NEW |
(Empty) | |
| 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 #ifndef NET_QUIC_PLATFORM_IMPL_QUIC_TEXT_UTILS_IMPL_H_ |
| 6 #define NET_QUIC_PLATFORM_IMPL_QUIC_TEXT_UTILS_IMPL_H_ |
| 7 |
| 8 #include <algorithm> |
| 9 |
| 10 #include "base/base64.h" |
| 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_piece.h" |
| 13 #include "base/strings/string_split.h" |
| 14 #include "base/strings/string_util.h" |
| 15 #include "base/strings/stringprintf.h" |
| 16 |
| 17 namespace net { |
| 18 |
| 19 // google3 implementation of QuicTextUtils. |
| 20 class QuicTextUtilsImpl { |
| 21 public: |
| 22 // Returns true of |data| starts with |prefix|, case sensitively. |
| 23 static bool StartsWith(base::StringPiece data, base::StringPiece prefix) { |
| 24 return base::StartsWith(data, prefix, base::CompareCase::SENSITIVE); |
| 25 } |
| 26 |
| 27 // Returns true of |data| ends with |suffix|, case insensitively. |
| 28 static bool EndsWithIgnoreCase(base::StringPiece data, |
| 29 base::StringPiece suffix) { |
| 30 return base::EndsWith(data, suffix, base::CompareCase::INSENSITIVE_ASCII); |
| 31 } |
| 32 |
| 33 // Returns a new std::string in which |data| has been converted to lower case. |
| 34 static std::string ToLower(base::StringPiece data) { |
| 35 return base::ToLowerASCII(data); |
| 36 } |
| 37 |
| 38 // Remove leading and trailing whitespace from |data|. |
| 39 static void RemoveLeadingAndTrailingWhitespace(base::StringPiece* data) { |
| 40 *data = base::TrimWhitespaceASCII(*data, base::TRIM_ALL); |
| 41 } |
| 42 |
| 43 // Returns true if |in| represents a valid uint64, and stores that value in |
| 44 // |out|. |
| 45 static bool StringToUint64(base::StringPiece in, uint64_t* out) { |
| 46 return base::StringToUint64(in, out); |
| 47 } |
| 48 |
| 49 // Returns a new std::string representing |in|. |
| 50 static std::string Uint64ToString(uint64_t in) { |
| 51 return base::Uint64ToString(in); |
| 52 } |
| 53 |
| 54 // This converts |length| bytes of binary to a 2*|length|-character |
| 55 // hexadecimal representation. |
| 56 // Return value: 2*|length| characters of ASCII std::string. |
| 57 static std::string HexEncode(base::StringPiece data) { |
| 58 return base::ToLowerASCII(::base::HexEncode(data.data(), data.size())); |
| 59 } |
| 60 |
| 61 // Converts |data| from a hexadecimal ASCII string to a binary string |
| 62 // that is |data.length()/2| bytes long. |
| 63 static std::string HexDecode(base::StringPiece data) { |
| 64 if (data.empty()) |
| 65 return ""; |
| 66 std::vector<uint8_t> v; |
| 67 if (!base::HexStringToBytes(data.as_string(), &v)) |
| 68 return ""; |
| 69 std::string out; |
| 70 if (!v.empty()) |
| 71 out.assign(reinterpret_cast<const char*>(&v[0]), v.size()); |
| 72 return out; |
| 73 } |
| 74 |
| 75 // Base64 encodes with no padding |data_len| bytes of |data| into |output|. |
| 76 static void Base64Encode(const uint8_t* data, |
| 77 size_t data_len, |
| 78 std::string* output) { |
| 79 base::Base64Encode( |
| 80 std::string(reinterpret_cast<const char*>(data), data_len), output); |
| 81 // Remove padding. |
| 82 size_t len = output->size(); |
| 83 if (len >= 2) { |
| 84 if ((*output)[len - 1] == '=') { |
| 85 len--; |
| 86 if ((*output)[len - 1] == '=') { |
| 87 len--; |
| 88 } |
| 89 output->resize(len); |
| 90 } |
| 91 } |
| 92 } |
| 93 |
| 94 // Returns a std::string containing hex and ASCII representations of |binary|, |
| 95 // side-by-side in the style of hexdump. Non-printable characters will be |
| 96 // printed as '.' in the ASCII output. |
| 97 // For example, given the input "Hello, QUIC!\01\02\03\04", returns: |
| 98 // "0x0000: 4865 6c6c 6f2c 2051 5549 4321 0102 0304 Hello,.QUIC!...." |
| 99 static std::string HexDump(base::StringPiece binary_input) { |
| 100 int offset = 0; |
| 101 const int kBytesPerLine = 16; // Max bytes dumped per line |
| 102 const char* buf = binary_input.data(); |
| 103 int bytes_remaining = binary_input.size(); |
| 104 std::string s; // our output |
| 105 const char* p = buf; |
| 106 while (bytes_remaining > 0) { |
| 107 const int line_bytes = std::min(bytes_remaining, kBytesPerLine); |
| 108 base::StringAppendF(&s, "0x%04x: ", offset); // Do the line header |
| 109 for (int i = 0; i < kBytesPerLine; ++i) { |
| 110 if (i < line_bytes) { |
| 111 base::StringAppendF(&s, "%02x", static_cast<unsigned char>(p[i])); |
| 112 } else { |
| 113 s += " "; // two-space filler instead of two-space hex digits |
| 114 } |
| 115 if (i % 2) |
| 116 s += ' '; |
| 117 } |
| 118 s += ' '; |
| 119 for (int i = 0; i < line_bytes; ++i) { // Do the ASCII dump |
| 120 s += (p[i] > 32 && p[i] < 127) ? p[i] : '.'; |
| 121 } |
| 122 |
| 123 bytes_remaining -= line_bytes; |
| 124 offset += line_bytes; |
| 125 p += line_bytes; |
| 126 s += '\n'; |
| 127 } |
| 128 return s; |
| 129 } |
| 130 |
| 131 // Returns true if |data| contains any uppercase characters. |
| 132 static bool ContainsUpperCase(base::StringPiece data) { |
| 133 return std::any_of(data.begin(), data.end(), base::IsAsciiUpper<char>); |
| 134 } |
| 135 |
| 136 // Splits |data| into a vector of pieces delimited by |delim|. |
| 137 static std::vector<base::StringPiece> Split(base::StringPiece data, |
| 138 char delim) { |
| 139 return base::SplitStringPiece(data, base::StringPiece(&delim, 1), |
| 140 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 141 } |
| 142 }; |
| 143 |
| 144 } // namespace net |
| 145 |
| 146 #endif // NET_QUIC_PLATFORM_IMPL_QUIC_TEXT_UTILS_IMPL_H_ |
OLD | NEW |