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