| 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/quic_utils.h" | 5 #include "net/quic/quic_utils.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/port.h" | 8 #include "base/port.h" |
| 9 | 9 |
| 10 namespace net { | 10 namespace net { |
| 11 | 11 |
| 12 // static | 12 // static |
| 13 uint64 QuicUtils::FNV1a_64_Hash(const char* data, int len) { | |
| 14 static const uint64 kOffset = 14695981039346656037u; | |
| 15 static const uint64 kPrime = 1099511628211u; | |
| 16 | |
| 17 const uint8* octets = reinterpret_cast<const uint8*>(data); | |
| 18 | |
| 19 uint64 hash = kOffset; | |
| 20 | |
| 21 for (int i = 0; i < len; ++i) { | |
| 22 hash = hash ^ octets[i]; | |
| 23 hash = hash * kPrime; | |
| 24 } | |
| 25 | |
| 26 return hash; | |
| 27 } | |
| 28 | |
| 29 // static | |
| 30 uint128 QuicUtils::FNV1a_128_Hash(const char* data, int len) { | 13 uint128 QuicUtils::FNV1a_128_Hash(const char* data, int len) { |
| 31 // The following two constants are defined as part of the hash algorithm. | 14 // The following two constants are defined as part of the hash algorithm. |
| 32 // see http://www.isthe.com/chongo/tech/comp/fnv/ | 15 // see http://www.isthe.com/chongo/tech/comp/fnv/ |
| 33 // 309485009821345068724781371 | 16 // 309485009821345068724781371 |
| 34 const uint128 kPrime(16777216, 315); | 17 const uint128 kPrime(16777216, 315); |
| 35 // 144066263297769815596495629667062367629 | 18 // 144066263297769815596495629667062367629 |
| 36 const uint128 kOffset(GG_UINT64_C(7809847782465536322), | 19 const uint128 kOffset(GG_UINT64_C(7809847782465536322), |
| 37 GG_UINT64_C(7113472399480571277)); | 20 GG_UINT64_C(7113472399480571277)); |
| 38 | 21 |
| 39 const uint8* octets = reinterpret_cast<const uint8*>(data); | 22 const uint8* octets = reinterpret_cast<const uint8*>(data); |
| 40 | 23 |
| 41 uint128 hash = kOffset; | 24 uint128 hash = kOffset; |
| 42 | 25 |
| 43 for (int i = 0; i < len; ++i) { | 26 for (int i = 0; i < len; ++i) { |
| 44 hash = hash ^ uint128(0, octets[i]); | 27 hash = hash ^ uint128(0, octets[i]); |
| 45 hash = hash * kPrime; | 28 hash = hash * kPrime; |
| 46 } | 29 } |
| 47 | 30 |
| 48 return hash; | 31 return hash; |
| 49 } | 32 } |
| 50 | 33 |
| 51 // static | |
| 52 void QuicUtils::SerializeUint128(uint128 v, uint8* out) { | |
| 53 const uint64 lo = Uint128Low64(v); | |
| 54 const uint64 hi = Uint128High64(v); | |
| 55 // This assumes that the system is little-endian. | |
| 56 memcpy(out, &lo, sizeof(lo)); | |
| 57 memcpy(out + sizeof(lo), &hi, sizeof(hi)); | |
| 58 } | |
| 59 | |
| 60 // static | |
| 61 uint128 QuicUtils::ParseUint128(const uint8* in) { | |
| 62 uint64 lo, hi; | |
| 63 memcpy(&lo, in, sizeof(lo)); | |
| 64 memcpy(&hi, in + sizeof(lo), sizeof(hi)); | |
| 65 return uint128(hi, lo); | |
| 66 } | |
| 67 | |
| 68 #define RETURN_STRING_LITERAL(x) \ | 34 #define RETURN_STRING_LITERAL(x) \ |
| 69 case x: \ | 35 case x: \ |
| 70 return #x; | 36 return #x; |
| 71 | 37 |
| 72 // static | 38 // static |
| 73 const char* QuicUtils::StreamErrorToString(QuicRstStreamErrorCode error) { | 39 const char* QuicUtils::StreamErrorToString(QuicRstStreamErrorCode error) { |
| 74 switch (error) { | 40 switch (error) { |
| 75 RETURN_STRING_LITERAL(QUIC_STREAM_NO_ERROR); | 41 RETURN_STRING_LITERAL(QUIC_STREAM_NO_ERROR); |
| 76 RETURN_STRING_LITERAL(QUIC_STREAM_CONNECTION_ERROR); | 42 RETURN_STRING_LITERAL(QUIC_STREAM_CONNECTION_ERROR); |
| 77 RETURN_STRING_LITERAL(QUIC_SERVER_ERROR_PROCESSING_STREAM); | 43 RETURN_STRING_LITERAL(QUIC_SERVER_ERROR_PROCESSING_STREAM); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 RETURN_STRING_LITERAL(QUIC_CRYPTO_NO_SUPPORT); | 81 RETURN_STRING_LITERAL(QUIC_CRYPTO_NO_SUPPORT); |
| 116 RETURN_STRING_LITERAL(QUIC_INVALID_CRYPTO_MESSAGE_TYPE); | 82 RETURN_STRING_LITERAL(QUIC_INVALID_CRYPTO_MESSAGE_TYPE); |
| 117 RETURN_STRING_LITERAL(QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER); | 83 RETURN_STRING_LITERAL(QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER); |
| 118 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND); | 84 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND); |
| 119 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP); | 85 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP); |
| 120 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_INDEX_NOT_FOUND); | 86 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_INDEX_NOT_FOUND); |
| 121 RETURN_STRING_LITERAL(QUIC_INVALID_STREAM_ID); | 87 RETURN_STRING_LITERAL(QUIC_INVALID_STREAM_ID); |
| 122 RETURN_STRING_LITERAL(QUIC_TOO_MANY_OPEN_STREAMS); | 88 RETURN_STRING_LITERAL(QUIC_TOO_MANY_OPEN_STREAMS); |
| 123 RETURN_STRING_LITERAL(QUIC_PUBLIC_RESET); | 89 RETURN_STRING_LITERAL(QUIC_PUBLIC_RESET); |
| 124 RETURN_STRING_LITERAL(QUIC_INVALID_VERSION); | 90 RETURN_STRING_LITERAL(QUIC_INVALID_VERSION); |
| 125 RETURN_STRING_LITERAL(QUIC_STREAM_RST_BEFORE_HEADERS_DECOMPRESSED); | |
| 126 RETURN_STRING_LITERAL(QUIC_INVALID_HEADER_ID); | |
| 127 RETURN_STRING_LITERAL(QUIC_CONNECTION_TIMED_OUT); | 91 RETURN_STRING_LITERAL(QUIC_CONNECTION_TIMED_OUT); |
| 128 RETURN_STRING_LITERAL(QUIC_PROOF_INVALID); | 92 RETURN_STRING_LITERAL(QUIC_PROOF_INVALID); |
| 129 RETURN_STRING_LITERAL(QUIC_CRYPTO_DUPLICATE_TAG); | 93 RETURN_STRING_LITERAL(QUIC_CRYPTO_DUPLICATE_TAG); |
| 130 RETURN_STRING_LITERAL(QUIC_LAST_ERROR); | 94 RETURN_STRING_LITERAL(QUIC_LAST_ERROR); |
| 131 // Intentionally have no default case, so we'll break the build | 95 // Intentionally have no default case, so we'll break the build |
| 132 // if we add errors and don't put them here. | 96 // if we add errors and don't put them here. |
| 133 } | 97 } |
| 134 // Return a default value so that we return this when |error| doesn't match | 98 // Return a default value so that we return this when |error| doesn't match |
| 135 // any of the QuicErrorCodes. This can happen when the ConnectionClose | 99 // any of the QuicErrorCodes. This can happen when the ConnectionClose |
| 136 // frame sent by the peer (attacker) has invalid error code. | 100 // frame sent by the peer (attacker) has invalid error code. |
| 137 return "INVALID_ERROR_CODE"; | 101 return "INVALID_ERROR_CODE"; |
| 138 } | 102 } |
| 139 | 103 |
| 140 } // namespace net | 104 } // namespace net |
| OLD | NEW |