| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/quic/core/quic_utils.h" | 5 #include "net/quic/core/quic_utils.h" |
| 6 | 6 |
| 7 #include <ctype.h> | 7 #include <ctype.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/containers/adapters.h" | 13 #include "base/containers/adapters.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/strings/string_number_conversions.h" | |
| 16 #include "base/strings/string_split.h" | |
| 17 #include "base/strings/stringprintf.h" | |
| 18 #include "net/quic/core/quic_constants.h" | 15 #include "net/quic/core/quic_constants.h" |
| 19 #include "net/quic/core/quic_flags.h" | 16 #include "net/quic/core/quic_flags.h" |
| 20 | 17 |
| 21 using base::StringPiece; | 18 using base::StringPiece; |
| 22 using std::string; | 19 using std::string; |
| 23 | 20 |
| 24 namespace net { | 21 namespace net { |
| 25 namespace { | 22 namespace { |
| 26 | 23 |
| 27 // We know that >= GCC 4.8 and Clang have a __uint128_t intrinsic. Other | 24 // We know that >= GCC 4.8 and Clang have a __uint128_t intrinsic. Other |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 const int kSubnetMaskLength = 24; | 218 const int kSubnetMaskLength = 24; |
| 222 if (old_address.host().InSameSubnet(new_address.host(), kSubnetMaskLength)) { | 219 if (old_address.host().InSameSubnet(new_address.host(), kSubnetMaskLength)) { |
| 223 // Subnet part does not change (here, we use /24), which is considered to be | 220 // Subnet part does not change (here, we use /24), which is considered to be |
| 224 // caused by NATs. | 221 // caused by NATs. |
| 225 return IPV4_SUBNET_CHANGE; | 222 return IPV4_SUBNET_CHANGE; |
| 226 } | 223 } |
| 227 | 224 |
| 228 return IPV4_TO_IPV4_CHANGE; | 225 return IPV4_TO_IPV4_CHANGE; |
| 229 } | 226 } |
| 230 | 227 |
| 231 string QuicUtils::HexEncode(const char* data, size_t length) { | |
| 232 return HexEncode(StringPiece(data, length)); | |
| 233 } | |
| 234 | |
| 235 string QuicUtils::HexEncode(StringPiece data) { | |
| 236 return ::base::HexEncode(data.data(), data.size()); | |
| 237 } | |
| 238 | |
| 239 string QuicUtils::HexDecode(StringPiece data) { | |
| 240 if (data.empty()) | |
| 241 return ""; | |
| 242 std::vector<uint8_t> v; | |
| 243 if (!base::HexStringToBytes(data.as_string(), &v)) | |
| 244 return ""; | |
| 245 string out; | |
| 246 if (!v.empty()) | |
| 247 out.assign(reinterpret_cast<const char*>(&v[0]), v.size()); | |
| 248 return out; | |
| 249 } | |
| 250 | |
| 251 string QuicUtils::HexDump(StringPiece binary_input) { | |
| 252 int offset = 0; | |
| 253 const int kBytesPerLine = 16; // Max bytes dumped per line | |
| 254 const char* buf = binary_input.data(); | |
| 255 int bytes_remaining = binary_input.size(); | |
| 256 string s; // our output | |
| 257 const char* p = buf; | |
| 258 while (bytes_remaining > 0) { | |
| 259 const int line_bytes = std::min(bytes_remaining, kBytesPerLine); | |
| 260 base::StringAppendF(&s, "0x%04x: ", offset); // Do the line header | |
| 261 for (int i = 0; i < kBytesPerLine; ++i) { | |
| 262 if (i < line_bytes) { | |
| 263 base::StringAppendF(&s, "%02x", static_cast<unsigned char>(p[i])); | |
| 264 } else { | |
| 265 s += " "; // two-space filler instead of two-space hex digits | |
| 266 } | |
| 267 if (i % 2) | |
| 268 s += ' '; | |
| 269 } | |
| 270 s += ' '; | |
| 271 for (int i = 0; i < line_bytes; ++i) { // Do the ASCII dump | |
| 272 s += (p[i] > 32 && p[i] < 127) ? p[i] : '.'; | |
| 273 } | |
| 274 | |
| 275 bytes_remaining -= line_bytes; | |
| 276 offset += line_bytes; | |
| 277 p += line_bytes; | |
| 278 s += '\n'; | |
| 279 } | |
| 280 return s; | |
| 281 } | |
| 282 | |
| 283 } // namespace net | 228 } // namespace net |
| OLD | NEW |