| 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 <stdint.h> |
| 15 |
| 15 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 16 | 17 |
| 17 namespace base { | 18 namespace base { |
| 18 | 19 |
| 19 // Returns a value with all bytes in |x| swapped, i.e. reverses the endianness. | 20 // Returns a value with all bytes in |x| swapped, i.e. reverses the endianness. |
| 20 inline uint16 ByteSwap(uint16 x) { | 21 inline uint16_t ByteSwap(uint16_t x) { |
| 21 return ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8); | 22 return ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8); |
| 22 } | 23 } |
| 23 | 24 |
| 24 inline uint32 ByteSwap(uint32 x) { | 25 inline uint32_t ByteSwap(uint32_t x) { |
| 25 return ((x & 0x000000fful) << 24) | ((x & 0x0000ff00ul) << 8) | | 26 return ((x & 0x000000fful) << 24) | ((x & 0x0000ff00ul) << 8) | |
| 26 ((x & 0x00ff0000ul) >> 8) | ((x & 0xff000000ul) >> 24); | 27 ((x & 0x00ff0000ul) >> 8) | ((x & 0xff000000ul) >> 24); |
| 27 } | 28 } |
| 28 | 29 |
| 29 inline uint64 ByteSwap(uint64 x) { | 30 inline uint64_t ByteSwap(uint64_t x) { |
| 30 return ((x & 0x00000000000000ffull) << 56) | | 31 return ((x & 0x00000000000000ffull) << 56) | |
| 31 ((x & 0x000000000000ff00ull) << 40) | | 32 ((x & 0x000000000000ff00ull) << 40) | |
| 32 ((x & 0x0000000000ff0000ull) << 24) | | 33 ((x & 0x0000000000ff0000ull) << 24) | |
| 33 ((x & 0x00000000ff000000ull) << 8) | | 34 ((x & 0x00000000ff000000ull) << 8) | |
| 34 ((x & 0x000000ff00000000ull) >> 8) | | 35 ((x & 0x000000ff00000000ull) >> 8) | |
| 35 ((x & 0x0000ff0000000000ull) >> 24) | | 36 ((x & 0x0000ff0000000000ull) >> 24) | |
| 36 ((x & 0x00ff000000000000ull) >> 40) | | 37 ((x & 0x00ff000000000000ull) >> 40) | |
| 37 ((x & 0xff00000000000000ull) >> 56); | 38 ((x & 0xff00000000000000ull) >> 56); |
| 38 } | 39 } |
| 39 | 40 |
| 40 // Converts the bytes in |x| from host order (endianness) to little endian, and | 41 // Converts the bytes in |x| from host order (endianness) to little endian, and |
| 41 // returns the result. | 42 // returns the result. |
| 42 inline uint16 ByteSwapToLE16(uint16 x) { | 43 inline uint16_t ByteSwapToLE16(uint16_t x) { |
| 43 #if defined(ARCH_CPU_LITTLE_ENDIAN) | 44 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 44 return x; | 45 return x; |
| 45 #else | 46 #else |
| 46 return ByteSwap(x); | 47 return ByteSwap(x); |
| 47 #endif | 48 #endif |
| 48 } | 49 } |
| 49 inline uint32 ByteSwapToLE32(uint32 x) { | 50 inline uint32_t ByteSwapToLE32(uint32_t x) { |
| 50 #if defined(ARCH_CPU_LITTLE_ENDIAN) | 51 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 51 return x; | 52 return x; |
| 52 #else | 53 #else |
| 53 return ByteSwap(x); | 54 return ByteSwap(x); |
| 54 #endif | 55 #endif |
| 55 } | 56 } |
| 56 inline uint64 ByteSwapToLE64(uint64 x) { | 57 inline uint64_t ByteSwapToLE64(uint64_t x) { |
| 57 #if defined(ARCH_CPU_LITTLE_ENDIAN) | 58 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 58 return x; | 59 return x; |
| 59 #else | 60 #else |
| 60 return ByteSwap(x); | 61 return ByteSwap(x); |
| 61 #endif | 62 #endif |
| 62 } | 63 } |
| 63 | 64 |
| 64 // Converts the bytes in |x| from network to host order (endianness), and | 65 // Converts the bytes in |x| from network to host order (endianness), and |
| 65 // returns the result. | 66 // returns the result. |
| 66 inline uint16 NetToHost16(uint16 x) { | 67 inline uint16_t NetToHost16(uint16_t x) { |
| 67 #if defined(ARCH_CPU_LITTLE_ENDIAN) | 68 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 68 return ByteSwap(x); | 69 return ByteSwap(x); |
| 69 #else | 70 #else |
| 70 return x; | 71 return x; |
| 71 #endif | 72 #endif |
| 72 } | 73 } |
| 73 inline uint32 NetToHost32(uint32 x) { | 74 inline uint32_t NetToHost32(uint32_t x) { |
| 74 #if defined(ARCH_CPU_LITTLE_ENDIAN) | 75 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 75 return ByteSwap(x); | 76 return ByteSwap(x); |
| 76 #else | 77 #else |
| 77 return x; | 78 return x; |
| 78 #endif | 79 #endif |
| 79 } | 80 } |
| 80 inline uint64 NetToHost64(uint64 x) { | 81 inline uint64_t NetToHost64(uint64_t x) { |
| 81 #if defined(ARCH_CPU_LITTLE_ENDIAN) | 82 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 82 return ByteSwap(x); | 83 return ByteSwap(x); |
| 83 #else | 84 #else |
| 84 return x; | 85 return x; |
| 85 #endif | 86 #endif |
| 86 } | 87 } |
| 87 | 88 |
| 88 // Converts the bytes in |x| from host to network order (endianness), and | 89 // Converts the bytes in |x| from host to network order (endianness), and |
| 89 // returns the result. | 90 // returns the result. |
| 90 inline uint16 HostToNet16(uint16 x) { | 91 inline uint16_t HostToNet16(uint16_t x) { |
| 91 #if defined(ARCH_CPU_LITTLE_ENDIAN) | 92 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 92 return ByteSwap(x); | 93 return ByteSwap(x); |
| 93 #else | 94 #else |
| 94 return x; | 95 return x; |
| 95 #endif | 96 #endif |
| 96 } | 97 } |
| 97 inline uint32 HostToNet32(uint32 x) { | 98 inline uint32_t HostToNet32(uint32_t x) { |
| 98 #if defined(ARCH_CPU_LITTLE_ENDIAN) | 99 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 99 return ByteSwap(x); | 100 return ByteSwap(x); |
| 100 #else | 101 #else |
| 101 return x; | 102 return x; |
| 102 #endif | 103 #endif |
| 103 } | 104 } |
| 104 inline uint64 HostToNet64(uint64 x) { | 105 inline uint64_t HostToNet64(uint64_t x) { |
| 105 #if defined(ARCH_CPU_LITTLE_ENDIAN) | 106 #if defined(ARCH_CPU_LITTLE_ENDIAN) |
| 106 return ByteSwap(x); | 107 return ByteSwap(x); |
| 107 #else | 108 #else |
| 108 return x; | 109 return x; |
| 109 #endif | 110 #endif |
| 110 } | 111 } |
| 111 | 112 |
| 112 } // namespace base | 113 } // namespace base |
| 113 | 114 |
| 114 #endif // BASE_SYS_BYTEORDER_H_ | 115 #endif // BASE_SYS_BYTEORDER_H_ |
| OLD | NEW |