[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [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 | |||||
[email protected] | eda78e6 | 2012-04-06 04:32:28 | [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. | ||||
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [diff] [blame] | 10 | |
11 | #ifndef BASE_SYS_BYTEORDER_H_ | ||||
12 | #define BASE_SYS_BYTEORDER_H_ | ||||
13 | |||||
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 14 | #include <stdint.h> |
15 | |||||
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [diff] [blame] | 16 | #include "build/build_config.h" |
17 | |||||
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [diff] [blame] | 18 | namespace base { |
19 | |||||
20 | // Returns a value with all bytes in |x| swapped, i.e. reverses the endianness. | ||||
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 21 | inline uint16_t ByteSwap(uint16_t x) { |
[email protected] | 2090c52 | 2013-12-23 04:02:49 | [diff] [blame] | 22 | return ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8); |
[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 23 | } |
[email protected] | 2090c52 | 2013-12-23 04:02:49 | [diff] [blame] | 24 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 25 | inline uint32_t ByteSwap(uint32_t x) { |
[email protected] | 2090c52 | 2013-12-23 04:02:49 | [diff] [blame] | 26 | return ((x & 0x000000fful) << 24) | ((x & 0x0000ff00ul) << 8) | |
27 | ((x & 0x00ff0000ul) >> 8) | ((x & 0xff000000ul) >> 24); | ||||
[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 28 | } |
[email protected] | 2090c52 | 2013-12-23 04:02:49 | [diff] [blame] | 29 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 30 | inline uint64_t ByteSwap(uint64_t x) { |
[email protected] | 2090c52 | 2013-12-23 04:02:49 | [diff] [blame] | 31 | return ((x & 0x00000000000000ffull) << 56) | |
32 | ((x & 0x000000000000ff00ull) << 40) | | ||||
33 | ((x & 0x0000000000ff0000ull) << 24) | | ||||
34 | ((x & 0x00000000ff000000ull) << 8) | | ||||
35 | ((x & 0x000000ff00000000ull) >> 8) | | ||||
36 | ((x & 0x0000ff0000000000ull) >> 24) | | ||||
37 | ((x & 0x00ff000000000000ull) >> 40) | | ||||
38 | ((x & 0xff00000000000000ull) >> 56); | ||||
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [diff] [blame] | 39 | } |
40 | |||||
[email protected] | 7f8ce3d9 | 2012-04-16 15:29:27 | [diff] [blame] | 41 | // Converts the bytes in |x| from host order (endianness) to little endian, and |
42 | // returns the result. | ||||
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 43 | inline uint16_t ByteSwapToLE16(uint16_t x) { |
[email protected] | 7f8ce3d9 | 2012-04-16 15:29:27 | [diff] [blame] | 44 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
45 | return x; | ||||
46 | #else | ||||
47 | return ByteSwap(x); | ||||
48 | #endif | ||||
49 | } | ||||
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 50 | inline uint32_t ByteSwapToLE32(uint32_t x) { |
[email protected] | 7f8ce3d9 | 2012-04-16 15:29:27 | [diff] [blame] | 51 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
52 | return x; | ||||
53 | #else | ||||
54 | return ByteSwap(x); | ||||
55 | #endif | ||||
56 | } | ||||
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 57 | inline uint64_t ByteSwapToLE64(uint64_t x) { |
[email protected] | 7f8ce3d9 | 2012-04-16 15:29:27 | [diff] [blame] | 58 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
59 | return x; | ||||
60 | #else | ||||
61 | return ByteSwap(x); | ||||
62 | #endif | ||||
63 | } | ||||
64 | |||||
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [diff] [blame] | 65 | // Converts the bytes in |x| from network to host order (endianness), and |
66 | // returns the result. | ||||
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 67 | inline uint16_t NetToHost16(uint16_t x) { |
[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 68 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
69 | return ByteSwap(x); | ||||
70 | #else | ||||
71 | return x; | ||||
72 | #endif | ||||
73 | } | ||||
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 74 | inline uint32_t NetToHost32(uint32_t x) { |
[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 75 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
76 | return ByteSwap(x); | ||||
77 | #else | ||||
78 | return x; | ||||
79 | #endif | ||||
80 | } | ||||
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 81 | inline uint64_t NetToHost64(uint64_t x) { |
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [diff] [blame] | 82 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
83 | return ByteSwap(x); | ||||
84 | #else | ||||
85 | return x; | ||||
86 | #endif | ||||
87 | } | ||||
88 | |||||
89 | // Converts the bytes in |x| from host to network order (endianness), and | ||||
90 | // returns the result. | ||||
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 91 | inline uint16_t HostToNet16(uint16_t x) { |
[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 92 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
93 | return ByteSwap(x); | ||||
94 | #else | ||||
95 | return x; | ||||
96 | #endif | ||||
97 | } | ||||
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 98 | inline uint32_t HostToNet32(uint32_t x) { |
[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 99 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
100 | return ByteSwap(x); | ||||
101 | #else | ||||
102 | return x; | ||||
103 | #endif | ||||
104 | } | ||||
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 105 | inline uint64_t HostToNet64(uint64_t x) { |
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [diff] [blame] | 106 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
107 | return ByteSwap(x); | ||||
108 | #else | ||||
109 | return x; | ||||
110 | #endif | ||||
111 | } | ||||
112 | |||||
113 | } // namespace base | ||||
114 | |||||
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [diff] [blame] | 115 | #endif // BASE_SYS_BYTEORDER_H_ |