[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 | |||||
robert.bradford | 1a09b177 | 2016-06-17 23:26:11 | [diff] [blame] | 18 | #if defined(COMPILER_MSVC) |
19 | #include <stdlib.h> | ||||
20 | #endif | ||||
21 | |||||
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [diff] [blame] | 22 | namespace base { |
23 | |||||
24 | // Returns a value with all bytes in |x| swapped, i.e. reverses the endianness. | ||||
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 25 | inline uint16_t ByteSwap(uint16_t x) { |
robert.bradford | 1a09b177 | 2016-06-17 23:26:11 | [diff] [blame] | 26 | #if defined(COMPILER_MSVC) |
27 | return _byteswap_ushort(x); | ||||
28 | #else | ||||
29 | return __builtin_bswap16(x); | ||||
30 | #endif | ||||
[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 31 | } |
[email protected] | 2090c52 | 2013-12-23 04:02:49 | [diff] [blame] | 32 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 33 | inline uint32_t ByteSwap(uint32_t x) { |
robert.bradford | 1a09b177 | 2016-06-17 23:26:11 | [diff] [blame] | 34 | #if defined(COMPILER_MSVC) |
35 | return _byteswap_ulong(x); | ||||
36 | #else | ||||
37 | return __builtin_bswap32(x); | ||||
38 | #endif | ||||
[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 39 | } |
[email protected] | 2090c52 | 2013-12-23 04:02:49 | [diff] [blame] | 40 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 41 | inline uint64_t ByteSwap(uint64_t x) { |
robert.bradford | 1a09b177 | 2016-06-17 23:26:11 | [diff] [blame] | 42 | #if defined(COMPILER_MSVC) |
43 | return _byteswap_uint64(x); | ||||
44 | #else | ||||
45 | return __builtin_bswap64(x); | ||||
46 | #endif | ||||
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [diff] [blame] | 47 | } |
48 | |||||
[email protected] | 7f8ce3d9 | 2012-04-16 15:29:27 | [diff] [blame] | 49 | // Converts the bytes in |x| from host order (endianness) to little endian, and |
50 | // returns the result. | ||||
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 51 | inline uint16_t ByteSwapToLE16(uint16_t x) { |
[email protected] | 7f8ce3d9 | 2012-04-16 15:29:27 | [diff] [blame] | 52 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
53 | return x; | ||||
54 | #else | ||||
55 | return ByteSwap(x); | ||||
56 | #endif | ||||
57 | } | ||||
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 58 | inline uint32_t ByteSwapToLE32(uint32_t x) { |
[email protected] | 7f8ce3d9 | 2012-04-16 15:29:27 | [diff] [blame] | 59 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
60 | return x; | ||||
61 | #else | ||||
62 | return ByteSwap(x); | ||||
63 | #endif | ||||
64 | } | ||||
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 65 | inline uint64_t ByteSwapToLE64(uint64_t x) { |
[email protected] | 7f8ce3d9 | 2012-04-16 15:29:27 | [diff] [blame] | 66 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
67 | return x; | ||||
68 | #else | ||||
69 | return ByteSwap(x); | ||||
70 | #endif | ||||
71 | } | ||||
72 | |||||
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [diff] [blame] | 73 | // Converts the bytes in |x| from network to host order (endianness), and |
74 | // returns the result. | ||||
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 75 | inline uint16_t NetToHost16(uint16_t x) { |
[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 76 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
77 | return ByteSwap(x); | ||||
78 | #else | ||||
79 | return x; | ||||
80 | #endif | ||||
81 | } | ||||
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 82 | inline uint32_t NetToHost32(uint32_t x) { |
[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 83 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
84 | return ByteSwap(x); | ||||
85 | #else | ||||
86 | return x; | ||||
87 | #endif | ||||
88 | } | ||||
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 89 | inline uint64_t NetToHost64(uint64_t x) { |
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [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 | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 99 | inline uint16_t HostToNet16(uint16_t x) { |
[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 100 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
101 | return ByteSwap(x); | ||||
102 | #else | ||||
103 | return x; | ||||
104 | #endif | ||||
105 | } | ||||
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 106 | inline uint32_t HostToNet32(uint32_t x) { |
[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 107 | #if defined(ARCH_CPU_LITTLE_ENDIAN) |
108 | return ByteSwap(x); | ||||
109 | #else | ||||
110 | return x; | ||||
111 | #endif | ||||
112 | } | ||||
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 113 | inline uint64_t HostToNet64(uint64_t x) { |
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [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 | |||||
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [diff] [blame] | 123 | #endif // BASE_SYS_BYTEORDER_H_ |