Record error when CreateDir fails.
[chromium-blink-merge.git] / base / sys_byteorder.h
blob97e33acfdab33d5093cf5441284eb5f25a314c2b
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
3 // found in the LICENSE file.
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.
11 #ifndef BASE_SYS_BYTEORDER_H_
12 #define BASE_SYS_BYTEORDER_H_
14 #include "base/basictypes.h"
15 #include "build/build_config.h"
17 #if defined(OS_WIN)
18 #include <winsock2.h>
19 #else
20 #include <arpa/inet.h>
21 #endif
23 // Include headers to provide byteswap for all platforms.
24 #if defined(COMPILER_MSVC)
25 #include <stdlib.h>
26 #elif defined(OS_MACOSX)
27 #include <libkern/OSByteOrder.h>
28 #elif defined(OS_OPENBSD)
29 #include <sys/endian.h>
30 #else
31 #include <byteswap.h>
32 #endif
35 namespace base {
37 // Returns a value with all bytes in |x| swapped, i.e. reverses the endianness.
38 inline uint16 ByteSwap(uint16 x) {
39 #if defined(COMPILER_MSVC)
40 return _byteswap_ushort(x);
41 #elif defined(OS_MACOSX)
42 return OSSwapInt16(x);
43 #elif defined(OS_OPENBSD)
44 return swap16(x);
45 #else
46 return bswap_16(x);
47 #endif
49 inline uint32 ByteSwap(uint32 x) {
50 #if defined(COMPILER_MSVC)
51 return _byteswap_ulong(x);
52 #elif defined(OS_MACOSX)
53 return OSSwapInt32(x);
54 #elif defined(OS_OPENBSD)
55 return swap32(x);
56 #else
57 return bswap_32(x);
58 #endif
60 inline uint64 ByteSwap(uint64 x) {
61 #if defined(COMPILER_MSVC)
62 return _byteswap_uint64(x);
63 #elif defined(OS_MACOSX)
64 return OSSwapInt64(x);
65 #elif defined(OS_OPENBSD)
66 return swap64(x);
67 #else
68 return bswap_64(x);
69 #endif
72 // Converts the bytes in |x| from host order (endianness) to little endian, and
73 // returns the result.
74 inline uint16 ByteSwapToLE16(uint16 x) {
75 #if defined(ARCH_CPU_LITTLE_ENDIAN)
76 return x;
77 #else
78 return ByteSwap(x);
79 #endif
81 inline uint32 ByteSwapToLE32(uint32 x) {
82 #if defined(ARCH_CPU_LITTLE_ENDIAN)
83 return x;
84 #else
85 return ByteSwap(x);
86 #endif
88 inline uint64 ByteSwapToLE64(uint64 x) {
89 #if defined(ARCH_CPU_LITTLE_ENDIAN)
90 return x;
91 #else
92 return ByteSwap(x);
93 #endif
96 // Converts the bytes in |x| from network to host order (endianness), and
97 // returns the result.
98 inline uint16 NetToHost16(uint16 x) {
99 #if defined(ARCH_CPU_LITTLE_ENDIAN)
100 return ByteSwap(x);
101 #else
102 return x;
103 #endif
105 inline uint32 NetToHost32(uint32 x) {
106 #if defined(ARCH_CPU_LITTLE_ENDIAN)
107 return ByteSwap(x);
108 #else
109 return x;
110 #endif
112 inline uint64 NetToHost64(uint64 x) {
113 #if defined(ARCH_CPU_LITTLE_ENDIAN)
114 return ByteSwap(x);
115 #else
116 return x;
117 #endif
120 // Converts the bytes in |x| from host to network order (endianness), and
121 // returns the result.
122 inline uint16 HostToNet16(uint16 x) {
123 #if defined(ARCH_CPU_LITTLE_ENDIAN)
124 return ByteSwap(x);
125 #else
126 return x;
127 #endif
129 inline uint32 HostToNet32(uint32 x) {
130 #if defined(ARCH_CPU_LITTLE_ENDIAN)
131 return ByteSwap(x);
132 #else
133 return x;
134 #endif
136 inline uint64 HostToNet64(uint64 x) {
137 #if defined(ARCH_CPU_LITTLE_ENDIAN)
138 return ByteSwap(x);
139 #else
140 return x;
141 #endif
144 } // namespace base
147 #endif // BASE_SYS_BYTEORDER_H_