2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 #ifndef RTC_BASE_BYTE_ORDER_H_
12 #define RTC_BASE_BYTE_ORDER_H_
16 #if defined(WEBRTC_POSIX) && !defined(__native_client__)
17 #include <arpa/inet.h>
20 #include "rtc_base/system/arch.h"
22 #if defined(WEBRTC_MAC)
23 #include <libkern/OSByteOrder.h>
25 #define htobe16(v) OSSwapHostToBigInt16(v)
26 #define htobe32(v) OSSwapHostToBigInt32(v)
27 #define htobe64(v) OSSwapHostToBigInt64(v)
28 #define be16toh(v) OSSwapBigToHostInt16(v)
29 #define be32toh(v) OSSwapBigToHostInt32(v)
30 #define be64toh(v) OSSwapBigToHostInt64(v)
32 #define htole16(v) OSSwapHostToLittleInt16(v)
33 #define htole32(v) OSSwapHostToLittleInt32(v)
34 #define htole64(v) OSSwapHostToLittleInt64(v)
35 #define le16toh(v) OSSwapLittleToHostInt16(v)
36 #define le32toh(v) OSSwapLittleToHostInt32(v)
37 #define le64toh(v) OSSwapLittleToHostInt64(v)
39 #elif defined(WEBRTC_WIN) || defined(__native_client__)
41 #if defined(WEBRTC_WIN)
45 #include <netinet/in.h>
46 #endif // defined(WEBRTC_WIN)
48 #if defined(WEBRTC_ARCH_LITTLE_ENDIAN)
49 #define htobe16(v) htons(v)
50 #define htobe32(v) htonl(v)
51 #define be16toh(v) ntohs(v)
52 #define be32toh(v) ntohl(v)
53 #define htole16(v) (v)
54 #define htole32(v) (v)
55 #define htole64(v) (v)
56 #define le16toh(v) (v)
57 #define le32toh(v) (v)
58 #define le64toh(v) (v)
59 #if defined(WEBRTC_WIN)
60 #define htobe64(v) _byteswap_uint64(v)
61 #define be64toh(v) _byteswap_uint64(v)
62 #endif // defined(WEBRTC_WIN)
63 #if defined(__native_client__)
64 #define htobe64(v) __builtin_bswap64(v)
65 #define be64toh(v) __builtin_bswap64(v)
66 #endif // defined(__native_client__)
68 #elif defined(WEBRTC_ARCH_BIG_ENDIAN)
69 #define htobe16(v) (v)
70 #define htobe32(v) (v)
71 #define be16toh(v) (v)
72 #define be32toh(v) (v)
73 #define htole16(v) __builtin_bswap16(v)
74 #define htole32(v) __builtin_bswap32(v)
75 #define htole64(v) __builtin_bswap64(v)
76 #define le16toh(v) __builtin_bswap16(v)
77 #define le32toh(v) __builtin_bswap32(v)
78 #define le64toh(v) __builtin_bswap64(v)
79 #if defined(WEBRTC_WIN)
80 #define htobe64(v) (v)
81 #define be64toh(v) (v)
82 #endif // defined(WEBRTC_WIN)
83 #if defined(__native_client__)
84 #define htobe64(v) (v)
85 #define be64toh(v) (v)
86 #endif // defined(__native_client__)
88 #error WEBRTC_ARCH_BIG_ENDIAN or WEBRTC_ARCH_LITTLE_ENDIAN must be defined.
89 #endif // defined(WEBRTC_ARCH_LITTLE_ENDIAN)
91 #elif defined(WEBRTC_POSIX)
92 #include <sys/endian.h>
94 #error "Missing byte order functions for this arch."
95 #endif // defined(WEBRTC_MAC)
99 // Reading and writing of little and big-endian numbers from memory
101 inline void Set8(void* memory, size_t offset, uint8_t v) {
102 static_cast<uint8_t*>(memory)[offset] = v;
105 inline uint8_t Get8(const void* memory, size_t offset) {
106 return static_cast<const uint8_t*>(memory)[offset];
109 inline void SetBE16(void* memory, uint16_t v) {
110 *static_cast<uint16_t*>(memory) = htobe16(v);
113 inline void SetBE32(void* memory, uint32_t v) {
114 *static_cast<uint32_t*>(memory) = htobe32(v);
117 inline void SetBE64(void* memory, uint64_t v) {
118 *static_cast<uint64_t*>(memory) = htobe64(v);
121 inline uint16_t GetBE16(const void* memory) {
122 return be16toh(*static_cast<const uint16_t*>(memory));
125 inline uint32_t GetBE32(const void* memory) {
126 return be32toh(*static_cast<const uint32_t*>(memory));
129 inline uint64_t GetBE64(const void* memory) {
130 return be64toh(*static_cast<const uint64_t*>(memory));
133 inline void SetLE16(void* memory, uint16_t v) {
134 *static_cast<uint16_t*>(memory) = htole16(v);
137 inline void SetLE32(void* memory, uint32_t v) {
138 *static_cast<uint32_t*>(memory) = htole32(v);
141 inline void SetLE64(void* memory, uint64_t v) {
142 *static_cast<uint64_t*>(memory) = htole64(v);
145 inline uint16_t GetLE16(const void* memory) {
146 return le16toh(*static_cast<const uint16_t*>(memory));
149 inline uint32_t GetLE32(const void* memory) {
150 return le32toh(*static_cast<const uint32_t*>(memory));
153 inline uint64_t GetLE64(const void* memory) {
154 return le64toh(*static_cast<const uint64_t*>(memory));
157 // Check if the current host is big endian.
158 inline bool IsHostBigEndian() {
159 #if defined(WEBRTC_ARCH_BIG_ENDIAN)
166 inline uint16_t HostToNetwork16(uint16_t n) {
170 inline uint32_t HostToNetwork32(uint32_t n) {
174 inline uint64_t HostToNetwork64(uint64_t n) {
178 inline uint16_t NetworkToHost16(uint16_t n) {
182 inline uint32_t NetworkToHost32(uint32_t n) {
186 inline uint64_t NetworkToHost64(uint64_t n) {
192 #endif // RTC_BASE_BYTE_ORDER_H_