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) | 17 #if defined(OS_WIN) |
18 #include <winsock2.h> | 18 #include <winsock2.h> |
19 #else | 19 #else |
20 #include <arpa/inet.h> | 20 #include <arpa/inet.h> |
21 #endif | 21 #endif |
22 | 22 |
23 // Include headers to provide byteswap for all platforms. | 23 // Include headers to provide byteswap for all platforms. |
24 #if defined(COMPILER_MSVC) | 24 #if defined(COMPILER_MSVC) |
25 #include <stdlib.h> | 25 #include <stdlib.h> |
26 #elif defined(OS_MACOSX) | 26 #elif defined(OS_MACOSX) |
27 #include <libkern/OSByteOrder.h> | 27 #include <libkern/OSByteOrder.h> |
28 #elif defined(OS_OPENBSD) | 28 #elif defined(OS_BSD) |
29 #include <sys/endian.h> | 29 #include <sys/endian.h> |
30 #else | 30 #else |
31 #include <byteswap.h> | 31 #include <byteswap.h> |
32 #endif | 32 #endif |
33 | 33 |
34 | 34 |
35 namespace base { | 35 namespace base { |
36 | 36 |
37 // Returns a value with all bytes in |x| swapped, i.e. reverses the endianness. | 37 // Returns a value with all bytes in |x| swapped, i.e. reverses the endianness. |
38 inline uint16 ByteSwap(uint16 x) { | 38 inline uint16 ByteSwap(uint16 x) { |
39 #if defined(COMPILER_MSVC) | 39 #if defined(COMPILER_MSVC) |
40 return _byteswap_ushort(x); | 40 return _byteswap_ushort(x); |
41 #elif defined(OS_MACOSX) | 41 #elif defined(OS_MACOSX) |
42 return OSSwapInt16(x); | 42 return OSSwapInt16(x); |
43 #elif defined(OS_OPENBSD) | 43 #elif defined(OS_OPENBSD) |
44 return swap16(x); | 44 return swap16(x); |
| 45 #elif defined(OS_FREEBSD) |
| 46 return bswap16(x); |
45 #else | 47 #else |
46 return bswap_16(x); | 48 return bswap_16(x); |
47 #endif | 49 #endif |
48 } | 50 } |
49 inline uint32 ByteSwap(uint32 x) { | 51 inline uint32 ByteSwap(uint32 x) { |
50 #if defined(COMPILER_MSVC) | 52 #if defined(COMPILER_MSVC) |
51 return _byteswap_ulong(x); | 53 return _byteswap_ulong(x); |
52 #elif defined(OS_MACOSX) | 54 #elif defined(OS_MACOSX) |
53 return OSSwapInt32(x); | 55 return OSSwapInt32(x); |
54 #elif defined(OS_OPENBSD) | 56 #elif defined(OS_OPENBSD) |
55 return swap32(x); | 57 return swap32(x); |
| 58 #elif defined(OS_FREEBSD) |
| 59 return bswap32(x); |
56 #else | 60 #else |
57 return bswap_32(x); | 61 return bswap_32(x); |
58 #endif | 62 #endif |
59 } | 63 } |
60 inline uint64 ByteSwap(uint64 x) { | 64 inline uint64 ByteSwap(uint64 x) { |
61 #if defined(COMPILER_MSVC) | 65 #if defined(COMPILER_MSVC) |
62 return _byteswap_uint64(x); | 66 return _byteswap_uint64(x); |
63 #elif defined(OS_MACOSX) | 67 #elif defined(OS_MACOSX) |
64 return OSSwapInt64(x); | 68 return OSSwapInt64(x); |
65 #elif defined(OS_OPENBSD) | 69 #elif defined(OS_OPENBSD) |
66 return swap64(x); | 70 return swap64(x); |
| 71 #elif defined(OS_FREEBSD) |
| 72 return bswap64(x); |
67 #else | 73 #else |
68 return bswap_64(x); | 74 return bswap_64(x); |
69 #endif | 75 #endif |
70 } | 76 } |
71 | 77 |
72 // Converts the bytes in |x| from host order (endianness) to little endian, and | 78 // Converts the bytes in |x| from host order (endianness) to little endian, and |
73 // returns the result. | 79 // returns the result. |
74 inline uint16 ByteSwapToLE16(uint16 x) { | 80 inline uint16 ByteSwapToLE16(uint16 x) { |
75 #if defined(ARCH_CPU_LITTLE_ENDIAN) | 81 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
76 return x; | 82 return x; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 return ByteSwap(x); | 144 return ByteSwap(x); |
139 #else | 145 #else |
140 return x; | 146 return x; |
141 #endif | 147 #endif |
142 } | 148 } |
143 | 149 |
144 } // namespace base | 150 } // namespace base |
145 | 151 |
146 | 152 |
147 #endif // BASE_SYS_BYTEORDER_H_ | 153 #endif // BASE_SYS_BYTEORDER_H_ |
OLD | NEW |