OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This header defines cross-platform ByteSwap() implementations for 16, 32 and | 5 // This header defines cross-platform ByteSwap() implementations for 16, 32 and |
6 // 64-bit values, and NetToHostXX() / HostToNextXX() functions equivalent to | 6 // 64-bit values, and NetToHostXX() / HostToNextXX() functions equivalent to |
7 // the traditional ntohX() and htonX() functions. | 7 // the traditional ntohX() and htonX() functions. |
8 // Use the functions defined here rather than using the platform-specific | 8 // Use the functions defined here rather than using the platform-specific |
9 // functions directly. | 9 // functions directly. |
10 | 10 |
11 #ifndef BASE_SYS_BYTEORDER_H_ | 11 #ifndef BASE_SYS_BYTEORDER_H_ |
12 #define BASE_SYS_BYTEORDER_H_ | 12 #define BASE_SYS_BYTEORDER_H_ |
13 | 13 |
14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
15 #include "build/build_config.h" | 15 #include "build/build_config.h" |
16 | 16 |
17 #if defined(OS_WIN) | |
18 #include <winsock2.h> | |
19 #else | |
20 #include <arpa/inet.h> | |
21 #endif | |
22 | |
23 namespace base { | 17 namespace base { |
24 | 18 |
25 // Returns a value with all bytes in |x| swapped, i.e. reverses the endianness. | 19 // Returns a value with all bytes in |x| swapped, i.e. reverses the endianness. |
26 inline uint16 ByteSwap(uint16 x) { | 20 inline uint16 ByteSwap(uint16 x) { |
27 return ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8); | 21 return ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8); |
28 } | 22 } |
29 | 23 |
30 inline uint32 ByteSwap(uint32 x) { | 24 inline uint32 ByteSwap(uint32 x) { |
31 return ((x & 0x000000fful) << 24) | ((x & 0x0000ff00ul) << 8) | | 25 return ((x & 0x000000fful) << 24) | ((x & 0x0000ff00ul) << 8) | |
32 ((x & 0x00ff0000ul) >> 8) | ((x & 0xff000000ul) >> 24); | 26 ((x & 0x00ff0000ul) >> 8) | ((x & 0xff000000ul) >> 24); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 #if defined(ARCH_CPU_LITTLE_ENDIAN) | 105 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
112 return ByteSwap(x); | 106 return ByteSwap(x); |
113 #else | 107 #else |
114 return x; | 108 return x; |
115 #endif | 109 #endif |
116 } | 110 } |
117 | 111 |
118 } // namespace base | 112 } // namespace base |
119 | 113 |
120 #endif // BASE_SYS_BYTEORDER_H_ | 114 #endif // BASE_SYS_BYTEORDER_H_ |
OLD | NEW |