• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
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.
10 
11 #ifndef BASE_SYS_BYTEORDER_H_
12 #define BASE_SYS_BYTEORDER_H_
13 
14 #include <stdint.h>
15 
16 #include "build/build_config.h"
17 
18 #if defined(COMPILER_MSVC)
19 #include <stdlib.h>
20 #endif
21 
22 namespace base {
23 
24 // Returns a value with all bytes in |x| swapped, i.e. reverses the endianness.
ByteSwap(uint16_t x)25 inline uint16_t ByteSwap(uint16_t x) {
26 #if defined(COMPILER_MSVC)
27   return _byteswap_ushort(x);
28 #else
29   return __builtin_bswap16(x);
30 #endif
31 }
32 
ByteSwap(uint32_t x)33 inline uint32_t ByteSwap(uint32_t x) {
34 #if defined(COMPILER_MSVC)
35   return _byteswap_ulong(x);
36 #else
37   return __builtin_bswap32(x);
38 #endif
39 }
40 
ByteSwap(uint64_t x)41 inline uint64_t ByteSwap(uint64_t x) {
42 #if defined(COMPILER_MSVC)
43   return _byteswap_uint64(x);
44 #else
45   return __builtin_bswap64(x);
46 #endif
47 }
48 
49 // Converts the bytes in |x| from host order (endianness) to little endian, and
50 // returns the result.
ByteSwapToLE16(uint16_t x)51 inline uint16_t ByteSwapToLE16(uint16_t x) {
52 #if defined(ARCH_CPU_LITTLE_ENDIAN)
53   return x;
54 #else
55   return ByteSwap(x);
56 #endif
57 }
ByteSwapToLE32(uint32_t x)58 inline uint32_t ByteSwapToLE32(uint32_t x) {
59 #if defined(ARCH_CPU_LITTLE_ENDIAN)
60   return x;
61 #else
62   return ByteSwap(x);
63 #endif
64 }
ByteSwapToLE64(uint64_t x)65 inline uint64_t ByteSwapToLE64(uint64_t x) {
66 #if defined(ARCH_CPU_LITTLE_ENDIAN)
67   return x;
68 #else
69   return ByteSwap(x);
70 #endif
71 }
72 
73 // Converts the bytes in |x| from network to host order (endianness), and
74 // returns the result.
NetToHost16(uint16_t x)75 inline uint16_t NetToHost16(uint16_t x) {
76 #if defined(ARCH_CPU_LITTLE_ENDIAN)
77   return ByteSwap(x);
78 #else
79   return x;
80 #endif
81 }
NetToHost32(uint32_t x)82 inline uint32_t NetToHost32(uint32_t x) {
83 #if defined(ARCH_CPU_LITTLE_ENDIAN)
84   return ByteSwap(x);
85 #else
86   return x;
87 #endif
88 }
NetToHost64(uint64_t x)89 inline uint64_t NetToHost64(uint64_t x) {
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.
HostToNet16(uint16_t x)99 inline uint16_t HostToNet16(uint16_t x) {
100 #if defined(ARCH_CPU_LITTLE_ENDIAN)
101   return ByteSwap(x);
102 #else
103   return x;
104 #endif
105 }
HostToNet32(uint32_t x)106 inline uint32_t HostToNet32(uint32_t x) {
107 #if defined(ARCH_CPU_LITTLE_ENDIAN)
108   return ByteSwap(x);
109 #else
110   return x;
111 #endif
112 }
HostToNet64(uint64_t x)113 inline uint64_t HostToNet64(uint64_t x) {
114 #if defined(ARCH_CPU_LITTLE_ENDIAN)
115   return ByteSwap(x);
116 #else
117   return x;
118 #endif
119 }
120 
121 }  // namespace base
122 
123 #endif  // BASE_SYS_BYTEORDER_H_
124