wez@chromium.org | 3174528 | 2012-03-29 05:19:31 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
isherman@chromium.org | 333d7fd | 2011-12-29 08:18:21 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
wez@chromium.org | cd68c02 | 2012-04-06 13:32:28 +0900 | [diff] [blame] | 5 | // This header defines cross-platform ByteSwap() implementations for 16, 32 and |
| 6 | // 64-bit values, and NetToHostXX() / HostToNextXX() functions equivalent to |
| 7 | // the traditional ntohX() and htonX() functions. |
| 8 | // Use the functions defined here rather than using the platform-specific |
| 9 | // functions directly. |
isherman@chromium.org | 333d7fd | 2011-12-29 08:18:21 +0900 | [diff] [blame] | 10 | |
| 11 | #ifndef BASE_SYS_BYTEORDER_H_ |
| 12 | #define BASE_SYS_BYTEORDER_H_ |
| 13 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 14 | #include <stdint.h> |
| 15 | |
isherman@chromium.org | 333d7fd | 2011-12-29 08:18:21 +0900 | [diff] [blame] | 16 | #include "build/build_config.h" |
| 17 | |
robert.bradford | 5a04446 | 2016-06-18 08:26:11 +0900 | [diff] [blame^] | 18 | #if defined(COMPILER_MSVC) |
| 19 | #include <stdlib.h> |
| 20 | #endif |
| 21 | |
isherman@chromium.org | 333d7fd | 2011-12-29 08:18:21 +0900 | [diff] [blame] | 22 | namespace base { |
| 23 | |
| 24 | // Returns a value with all bytes in |x| swapped, i.e. reverses the endianness. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 25 | inline uint16_t ByteSwap(uint16_t x) { |
robert.bradford | 5a04446 | 2016-06-18 08:26:11 +0900 | [diff] [blame^] | 26 | #if defined(COMPILER_MSVC) |
| 27 | return _byteswap_ushort(x); |
| 28 | #else |
| 29 | return __builtin_bswap16(x); |
| 30 | #endif |
wez@chromium.org | 3174528 | 2012-03-29 05:19:31 +0900 | [diff] [blame] | 31 | } |
sergeyu@chromium.org | b9b2847 | 2013-12-23 13:02:49 +0900 | [diff] [blame] | 32 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 33 | inline uint32_t ByteSwap(uint32_t x) { |
robert.bradford | 5a04446 | 2016-06-18 08:26:11 +0900 | [diff] [blame^] | 34 | #if defined(COMPILER_MSVC) |
| 35 | return _byteswap_ulong(x); |
| 36 | #else |
| 37 | return __builtin_bswap32(x); |
| 38 | #endif |
wez@chromium.org | 3174528 | 2012-03-29 05:19:31 +0900 | [diff] [blame] | 39 | } |
sergeyu@chromium.org | b9b2847 | 2013-12-23 13:02:49 +0900 | [diff] [blame] | 40 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 41 | inline uint64_t ByteSwap(uint64_t x) { |
robert.bradford | 5a04446 | 2016-06-18 08:26:11 +0900 | [diff] [blame^] | 42 | #if defined(COMPILER_MSVC) |
| 43 | return _byteswap_uint64(x); |
| 44 | #else |
| 45 | return __builtin_bswap64(x); |
| 46 | #endif |
isherman@chromium.org | 333d7fd | 2011-12-29 08:18:21 +0900 | [diff] [blame] | 47 | } |
| 48 | |
jwd@chromium.org | d0dab47 | 2012-04-17 00:29:27 +0900 | [diff] [blame] | 49 | // Converts the bytes in |x| from host order (endianness) to little endian, and |
| 50 | // returns the result. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 51 | inline uint16_t ByteSwapToLE16(uint16_t x) { |
jwd@chromium.org | d0dab47 | 2012-04-17 00:29:27 +0900 | [diff] [blame] | 52 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 53 | return x; |
| 54 | #else |
| 55 | return ByteSwap(x); |
| 56 | #endif |
| 57 | } |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 58 | inline uint32_t ByteSwapToLE32(uint32_t x) { |
jwd@chromium.org | d0dab47 | 2012-04-17 00:29:27 +0900 | [diff] [blame] | 59 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 60 | return x; |
| 61 | #else |
| 62 | return ByteSwap(x); |
| 63 | #endif |
| 64 | } |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 65 | inline uint64_t ByteSwapToLE64(uint64_t x) { |
jwd@chromium.org | d0dab47 | 2012-04-17 00:29:27 +0900 | [diff] [blame] | 66 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 67 | return x; |
| 68 | #else |
| 69 | return ByteSwap(x); |
| 70 | #endif |
| 71 | } |
| 72 | |
isherman@chromium.org | 333d7fd | 2011-12-29 08:18:21 +0900 | [diff] [blame] | 73 | // Converts the bytes in |x| from network to host order (endianness), and |
| 74 | // returns the result. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 75 | inline uint16_t NetToHost16(uint16_t x) { |
wez@chromium.org | 3174528 | 2012-03-29 05:19:31 +0900 | [diff] [blame] | 76 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 77 | return ByteSwap(x); |
| 78 | #else |
| 79 | return x; |
| 80 | #endif |
| 81 | } |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 82 | inline uint32_t NetToHost32(uint32_t x) { |
wez@chromium.org | 3174528 | 2012-03-29 05:19:31 +0900 | [diff] [blame] | 83 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 84 | return ByteSwap(x); |
| 85 | #else |
| 86 | return x; |
| 87 | #endif |
| 88 | } |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 89 | inline uint64_t NetToHost64(uint64_t x) { |
isherman@chromium.org | 333d7fd | 2011-12-29 08:18:21 +0900 | [diff] [blame] | 90 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 91 | return ByteSwap(x); |
| 92 | #else |
| 93 | return x; |
| 94 | #endif |
| 95 | } |
| 96 | |
| 97 | // Converts the bytes in |x| from host to network order (endianness), and |
| 98 | // returns the result. |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 99 | inline uint16_t HostToNet16(uint16_t x) { |
wez@chromium.org | 3174528 | 2012-03-29 05:19:31 +0900 | [diff] [blame] | 100 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 101 | return ByteSwap(x); |
| 102 | #else |
| 103 | return x; |
| 104 | #endif |
| 105 | } |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 106 | inline uint32_t HostToNet32(uint32_t x) { |
wez@chromium.org | 3174528 | 2012-03-29 05:19:31 +0900 | [diff] [blame] | 107 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 108 | return ByteSwap(x); |
| 109 | #else |
| 110 | return x; |
| 111 | #endif |
| 112 | } |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 113 | inline uint64_t HostToNet64(uint64_t x) { |
isherman@chromium.org | 333d7fd | 2011-12-29 08:18:21 +0900 | [diff] [blame] | 114 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 115 | return ByteSwap(x); |
| 116 | #else |
| 117 | return x; |
| 118 | #endif |
| 119 | } |
| 120 | |
| 121 | } // namespace base |
| 122 | |
isherman@chromium.org | 333d7fd | 2011-12-29 08:18:21 +0900 | [diff] [blame] | 123 | #endif // BASE_SYS_BYTEORDER_H_ |