QUICHE team | 173c48f | 2019-11-19 16:34:44 -0800 | [diff] [blame] | 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 | |
bnc | b8fd1f8 | 2020-10-16 17:37:52 -0700 | [diff] [blame] | 5 | #ifndef QUICHE_COMMON_QUICHE_ENDIAN_H_ |
| 6 | #define QUICHE_COMMON_QUICHE_ENDIAN_H_ |
QUICHE team | 173c48f | 2019-11-19 16:34:44 -0800 | [diff] [blame] | 7 | |
vasilvv | 6b2b752 | 2020-09-18 11:47:57 -0700 | [diff] [blame] | 8 | #include <algorithm> |
| 9 | #include <cstdint> |
| 10 | #include <type_traits> |
| 11 | |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 12 | #include "common/platform/api/quiche_export.h" |
QUICHE team | 173c48f | 2019-11-19 16:34:44 -0800 | [diff] [blame] | 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 (can be either little or big endian depending on the |
| 23 | // platform). |
| 24 | class QUICHE_EXPORT_PRIVATE QuicheEndian { |
| 25 | public: |
vasilvv | 6b2b752 | 2020-09-18 11:47:57 -0700 | [diff] [blame] | 26 | // Convert |x| from host order (little endian) to network order (big endian). |
| 27 | #if defined(__clang__) || \ |
| 28 | (defined(__GNUC__) && \ |
| 29 | ((__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || __GNUC__ >= 5)) |
| 30 | static uint16_t HostToNet16(uint16_t x) { return __builtin_bswap16(x); } |
| 31 | static uint32_t HostToNet32(uint32_t x) { return __builtin_bswap32(x); } |
| 32 | static uint64_t HostToNet64(uint64_t x) { return __builtin_bswap64(x); } |
| 33 | #else |
| 34 | static uint16_t HostToNet16(uint16_t x) { return PortableByteSwap(x); } |
| 35 | static uint32_t HostToNet32(uint32_t x) { return PortableByteSwap(x); } |
| 36 | static uint64_t HostToNet64(uint64_t x) { return PortableByteSwap(x); } |
| 37 | #endif |
QUICHE team | 173c48f | 2019-11-19 16:34:44 -0800 | [diff] [blame] | 38 | |
vasilvv | 6b2b752 | 2020-09-18 11:47:57 -0700 | [diff] [blame] | 39 | // Convert |x| from network order (big endian) to host order (little endian). |
| 40 | static uint16_t NetToHost16(uint16_t x) { return HostToNet16(x); } |
| 41 | static uint32_t NetToHost32(uint32_t x) { return HostToNet32(x); } |
| 42 | static uint64_t NetToHost64(uint64_t x) { return HostToNet64(x); } |
QUICHE team | 173c48f | 2019-11-19 16:34:44 -0800 | [diff] [blame] | 43 | |
| 44 | // Returns true if current host order is little endian. |
vasilvv | 6b2b752 | 2020-09-18 11:47:57 -0700 | [diff] [blame] | 45 | static bool HostIsLittleEndian() { return true; } |
| 46 | |
| 47 | // Left public for tests. |
| 48 | template <typename T> |
| 49 | static T PortableByteSwap(T input) { |
| 50 | static_assert(std::is_unsigned<T>::value, "T has to be uintNN_t"); |
| 51 | union { |
| 52 | T number; |
| 53 | char bytes[sizeof(T)]; |
| 54 | } value; |
| 55 | value.number = input; |
| 56 | std::reverse(std::begin(value.bytes), std::end(value.bytes)); |
| 57 | return value.number; |
QUICHE team | 173c48f | 2019-11-19 16:34:44 -0800 | [diff] [blame] | 58 | } |
| 59 | }; |
| 60 | |
| 61 | } // namespace quiche |
| 62 | |
bnc | b8fd1f8 | 2020-10-16 17:37:52 -0700 | [diff] [blame] | 63 | #endif // QUICHE_COMMON_QUICHE_ENDIAN_H_ |