1 // Copyright 2019 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 QUICHE_COMMON_QUICHE_ENDIAN_H_ 6 #define QUICHE_COMMON_QUICHE_ENDIAN_H_ 7 8 #include <algorithm> 9 #include <cstdint> 10 #include <type_traits> 11 12 #include "quiche/common/platform/api/quiche_export.h" 13 14 namespace quiche { 15 16 enum Endianness { 17 NETWORK_BYTE_ORDER, // big endian 18 HOST_BYTE_ORDER // little endian 19 }; 20 21 // Provide utility functions that convert from/to network order (big endian) 22 // to/from host order (little endian). 23 class QUICHE_EXPORT QuicheEndian { 24 public: 25 // Convert |x| from host order (little endian) to network order (big endian). 26 #if defined(__clang__) || \ 27 (defined(__GNUC__) && \ 28 ((__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ >= 5)) HostToNet16(uint16_t x)29 static uint16_t HostToNet16(uint16_t x) { return __builtin_bswap16(x); } HostToNet32(uint32_t x)30 static uint32_t HostToNet32(uint32_t x) { return __builtin_bswap32(x); } HostToNet64(uint64_t x)31 static uint64_t HostToNet64(uint64_t x) { return __builtin_bswap64(x); } 32 #else 33 static uint16_t HostToNet16(uint16_t x) { return PortableByteSwap(x); } 34 static uint32_t HostToNet32(uint32_t x) { return PortableByteSwap(x); } 35 static uint64_t HostToNet64(uint64_t x) { return PortableByteSwap(x); } 36 #endif 37 38 // Convert |x| from network order (big endian) to host order (little endian). NetToHost16(uint16_t x)39 static uint16_t NetToHost16(uint16_t x) { return HostToNet16(x); } NetToHost32(uint32_t x)40 static uint32_t NetToHost32(uint32_t x) { return HostToNet32(x); } NetToHost64(uint64_t x)41 static uint64_t NetToHost64(uint64_t x) { return HostToNet64(x); } 42 43 // Left public for tests. 44 template <typename T> PortableByteSwap(T input)45 static T PortableByteSwap(T input) { 46 static_assert(std::is_unsigned<T>::value, "T has to be uintNN_t"); 47 union { 48 T number; 49 char bytes[sizeof(T)]; 50 } value; 51 value.number = input; 52 std::reverse(&value.bytes[0], &value.bytes[sizeof(T)]); 53 return value.number; 54 } 55 }; 56 57 enum QuicheVariableLengthIntegerLength : uint8_t { 58 // Length zero means the variable length integer is not present. 59 VARIABLE_LENGTH_INTEGER_LENGTH_0 = 0, 60 VARIABLE_LENGTH_INTEGER_LENGTH_1 = 1, 61 VARIABLE_LENGTH_INTEGER_LENGTH_2 = 2, 62 VARIABLE_LENGTH_INTEGER_LENGTH_4 = 4, 63 VARIABLE_LENGTH_INTEGER_LENGTH_8 = 8, 64 65 // By default we write the IETF long header length using the 2-byte encoding 66 // of variable length integers, even when the length is below 64, which allows 67 // us to fill in the length before knowing what the length actually is. 68 kQuicheDefaultLongHeaderLengthLength = VARIABLE_LENGTH_INTEGER_LENGTH_2, 69 }; 70 71 } // namespace quiche 72 73 #endif // QUICHE_COMMON_QUICHE_ENDIAN_H_ 74