blob: ddb3f5bcda405fc07be49866304419bb0b7a55ff [file] [log] [blame]
[email protected]9eb7b11b2012-03-28 20:19:311// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]0e6f6192011-12-28 23:18:212// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]eda78e62012-04-06 04:32:285// 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.
[email protected]0e6f6192011-12-28 23:18:2110
11#ifndef BASE_SYS_BYTEORDER_H_
12#define BASE_SYS_BYTEORDER_H_
13
avi9b6f42932015-12-26 22:15:1414#include <stdint.h>
15
[email protected]0e6f6192011-12-28 23:18:2116#include "build/build_config.h"
17
[email protected]0e6f6192011-12-28 23:18:2118namespace base {
19
20// Returns a value with all bytes in |x| swapped, i.e. reverses the endianness.
avi9b6f42932015-12-26 22:15:1421inline uint16_t ByteSwap(uint16_t x) {
[email protected]2090c522013-12-23 04:02:4922 return ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8);
[email protected]9eb7b11b2012-03-28 20:19:3123}
[email protected]2090c522013-12-23 04:02:4924
avi9b6f42932015-12-26 22:15:1425inline uint32_t ByteSwap(uint32_t x) {
[email protected]2090c522013-12-23 04:02:4926 return ((x & 0x000000fful) << 24) | ((x & 0x0000ff00ul) << 8) |
27 ((x & 0x00ff0000ul) >> 8) | ((x & 0xff000000ul) >> 24);
[email protected]9eb7b11b2012-03-28 20:19:3128}
[email protected]2090c522013-12-23 04:02:4929
avi9b6f42932015-12-26 22:15:1430inline uint64_t ByteSwap(uint64_t x) {
[email protected]2090c522013-12-23 04:02:4931 return ((x & 0x00000000000000ffull) << 56) |
32 ((x & 0x000000000000ff00ull) << 40) |
33 ((x & 0x0000000000ff0000ull) << 24) |
34 ((x & 0x00000000ff000000ull) << 8) |
35 ((x & 0x000000ff00000000ull) >> 8) |
36 ((x & 0x0000ff0000000000ull) >> 24) |
37 ((x & 0x00ff000000000000ull) >> 40) |
38 ((x & 0xff00000000000000ull) >> 56);
[email protected]0e6f6192011-12-28 23:18:2139}
40
[email protected]7f8ce3d92012-04-16 15:29:2741// Converts the bytes in |x| from host order (endianness) to little endian, and
42// returns the result.
avi9b6f42932015-12-26 22:15:1443inline uint16_t ByteSwapToLE16(uint16_t x) {
[email protected]7f8ce3d92012-04-16 15:29:2744#if defined(ARCH_CPU_LITTLE_ENDIAN)
45 return x;
46#else
47 return ByteSwap(x);
48#endif
49}
avi9b6f42932015-12-26 22:15:1450inline uint32_t ByteSwapToLE32(uint32_t x) {
[email protected]7f8ce3d92012-04-16 15:29:2751#if defined(ARCH_CPU_LITTLE_ENDIAN)
52 return x;
53#else
54 return ByteSwap(x);
55#endif
56}
avi9b6f42932015-12-26 22:15:1457inline uint64_t ByteSwapToLE64(uint64_t x) {
[email protected]7f8ce3d92012-04-16 15:29:2758#if defined(ARCH_CPU_LITTLE_ENDIAN)
59 return x;
60#else
61 return ByteSwap(x);
62#endif
63}
64
[email protected]0e6f6192011-12-28 23:18:2165// Converts the bytes in |x| from network to host order (endianness), and
66// returns the result.
avi9b6f42932015-12-26 22:15:1467inline uint16_t NetToHost16(uint16_t x) {
[email protected]9eb7b11b2012-03-28 20:19:3168#if defined(ARCH_CPU_LITTLE_ENDIAN)
69 return ByteSwap(x);
70#else
71 return x;
72#endif
73}
avi9b6f42932015-12-26 22:15:1474inline uint32_t NetToHost32(uint32_t x) {
[email protected]9eb7b11b2012-03-28 20:19:3175#if defined(ARCH_CPU_LITTLE_ENDIAN)
76 return ByteSwap(x);
77#else
78 return x;
79#endif
80}
avi9b6f42932015-12-26 22:15:1481inline uint64_t NetToHost64(uint64_t x) {
[email protected]0e6f6192011-12-28 23:18:2182#if defined(ARCH_CPU_LITTLE_ENDIAN)
83 return ByteSwap(x);
84#else
85 return x;
86#endif
87}
88
89// Converts the bytes in |x| from host to network order (endianness), and
90// returns the result.
avi9b6f42932015-12-26 22:15:1491inline uint16_t HostToNet16(uint16_t x) {
[email protected]9eb7b11b2012-03-28 20:19:3192#if defined(ARCH_CPU_LITTLE_ENDIAN)
93 return ByteSwap(x);
94#else
95 return x;
96#endif
97}
avi9b6f42932015-12-26 22:15:1498inline uint32_t HostToNet32(uint32_t x) {
[email protected]9eb7b11b2012-03-28 20:19:3199#if defined(ARCH_CPU_LITTLE_ENDIAN)
100 return ByteSwap(x);
101#else
102 return x;
103#endif
104}
avi9b6f42932015-12-26 22:15:14105inline uint64_t HostToNet64(uint64_t x) {
[email protected]0e6f6192011-12-28 23:18:21106#if defined(ARCH_CPU_LITTLE_ENDIAN)
107 return ByteSwap(x);
108#else
109 return x;
110#endif
111}
112
113} // namespace base
114
[email protected]0e6f6192011-12-28 23:18:21115#endif // BASE_SYS_BYTEORDER_H_