• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
3  *
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.
9  */
10 
11 #ifndef RTC_BASE_BYTE_ORDER_H_
12 #define RTC_BASE_BYTE_ORDER_H_
13 
14 #include <stdint.h>
15 
16 #if defined(WEBRTC_POSIX) && !defined(__native_client__)
17 #include <arpa/inet.h>
18 #endif
19 
20 #include "rtc_base/system/arch.h"
21 
22 #if defined(WEBRTC_MAC)
23 #include <libkern/OSByteOrder.h>
24 
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)
31 
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)
38 
39 #elif defined(WEBRTC_WIN) || defined(__native_client__)
40 
41 #if defined(WEBRTC_WIN)
42 #include <stdlib.h>
43 #include <winsock2.h>
44 #else
45 #include <netinet/in.h>
46 #endif  // defined(WEBRTC_WIN)
47 
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__)
67 
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__)
87 #else
88 #error WEBRTC_ARCH_BIG_ENDIAN or WEBRTC_ARCH_LITTLE_ENDIAN must be defined.
89 #endif  // defined(WEBRTC_ARCH_LITTLE_ENDIAN)
90 
91 #elif defined(WEBRTC_POSIX)
92 #include <endian.h>
93 #else
94 #error "Missing byte order functions for this arch."
95 #endif  // defined(WEBRTC_MAC)
96 
97 namespace rtc {
98 
99 // Reading and writing of little and big-endian numbers from memory
100 
Set8(void * memory,size_t offset,uint8_t v)101 inline void Set8(void* memory, size_t offset, uint8_t v) {
102   static_cast<uint8_t*>(memory)[offset] = v;
103 }
104 
Get8(const void * memory,size_t offset)105 inline uint8_t Get8(const void* memory, size_t offset) {
106   return static_cast<const uint8_t*>(memory)[offset];
107 }
108 
SetBE16(void * memory,uint16_t v)109 inline void SetBE16(void* memory, uint16_t v) {
110   *static_cast<uint16_t*>(memory) = htobe16(v);
111 }
112 
SetBE32(void * memory,uint32_t v)113 inline void SetBE32(void* memory, uint32_t v) {
114   *static_cast<uint32_t*>(memory) = htobe32(v);
115 }
116 
SetBE64(void * memory,uint64_t v)117 inline void SetBE64(void* memory, uint64_t v) {
118   *static_cast<uint64_t*>(memory) = htobe64(v);
119 }
120 
GetBE16(const void * memory)121 inline uint16_t GetBE16(const void* memory) {
122   return be16toh(*static_cast<const uint16_t*>(memory));
123 }
124 
GetBE32(const void * memory)125 inline uint32_t GetBE32(const void* memory) {
126   return be32toh(*static_cast<const uint32_t*>(memory));
127 }
128 
GetBE64(const void * memory)129 inline uint64_t GetBE64(const void* memory) {
130   return be64toh(*static_cast<const uint64_t*>(memory));
131 }
132 
SetLE16(void * memory,uint16_t v)133 inline void SetLE16(void* memory, uint16_t v) {
134   *static_cast<uint16_t*>(memory) = htole16(v);
135 }
136 
SetLE32(void * memory,uint32_t v)137 inline void SetLE32(void* memory, uint32_t v) {
138   *static_cast<uint32_t*>(memory) = htole32(v);
139 }
140 
SetLE64(void * memory,uint64_t v)141 inline void SetLE64(void* memory, uint64_t v) {
142   *static_cast<uint64_t*>(memory) = htole64(v);
143 }
144 
GetLE16(const void * memory)145 inline uint16_t GetLE16(const void* memory) {
146   return le16toh(*static_cast<const uint16_t*>(memory));
147 }
148 
GetLE32(const void * memory)149 inline uint32_t GetLE32(const void* memory) {
150   return le32toh(*static_cast<const uint32_t*>(memory));
151 }
152 
GetLE64(const void * memory)153 inline uint64_t GetLE64(const void* memory) {
154   return le64toh(*static_cast<const uint64_t*>(memory));
155 }
156 
157 // Check if the current host is big endian.
IsHostBigEndian()158 inline bool IsHostBigEndian() {
159 #if defined(WEBRTC_ARCH_BIG_ENDIAN)
160   return true;
161 #else
162   return false;
163 #endif
164 }
165 
HostToNetwork16(uint16_t n)166 inline uint16_t HostToNetwork16(uint16_t n) {
167   return htobe16(n);
168 }
169 
HostToNetwork32(uint32_t n)170 inline uint32_t HostToNetwork32(uint32_t n) {
171   return htobe32(n);
172 }
173 
HostToNetwork64(uint64_t n)174 inline uint64_t HostToNetwork64(uint64_t n) {
175   return htobe64(n);
176 }
177 
NetworkToHost16(uint16_t n)178 inline uint16_t NetworkToHost16(uint16_t n) {
179   return be16toh(n);
180 }
181 
NetworkToHost32(uint32_t n)182 inline uint32_t NetworkToHost32(uint32_t n) {
183   return be32toh(n);
184 }
185 
NetworkToHost64(uint64_t n)186 inline uint64_t NetworkToHost64(uint64_t n) {
187   return be64toh(n);
188 }
189 
190 }  // namespace rtc
191 
192 #endif  // RTC_BASE_BYTE_ORDER_H_
193