blob: ae1c6345ba96a5149c7e819f3b8daf8cd886157e [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_BYTE_ORDER_H_
12#define RTC_BASE_BYTE_ORDER_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Niels Möller14682a32018-05-24 08:54:25 +020014#include <stdint.h>
15
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016#if defined(WEBRTC_POSIX) && !defined(__native_client__)
17#include <arpa/inet.h>
18#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019
Niels Möllerb06b0a62018-05-25 10:05:34 +020020#include "rtc_base/system/arch.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020021
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)
Mirko Bonadeib7217612019-03-28 17:10:29 +010038
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020039#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>
Mirko Bonadeib7217612019-03-28 17:10:29 +010046#endif // defined(WEBRTC_WIN)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020047
Mirko Bonadeib7217612019-03-28 17:10:29 +010048#if defined(WEBRTC_ARCH_LITTLE_ENDIAN)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020049#define htobe16(v) htons(v)
50#define htobe32(v) htonl(v)
51#define be16toh(v) ntohs(v)
52#define be32toh(v) ntohl(v)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020053#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)
Mirko Bonadeib7217612019-03-28 17:10:29 +010059#if defined(WEBRTC_WIN)
60#define htobe64(v) _byteswap_uint64(v)
61#define be64toh(v) _byteswap_uint64(v)
62#endif // defined(WEBRTC_WIN)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020063#if defined(__native_client__)
64#define htobe64(v) __builtin_bswap64(v)
65#define be64toh(v) __builtin_bswap64(v)
Mirko Bonadeib7217612019-03-28 17:10:29 +010066#endif // defined(__native_client__)
67
Niels Möllerb06b0a62018-05-25 10:05:34 +020068#elif defined(WEBRTC_ARCH_BIG_ENDIAN)
Mirko Bonadeib7217612019-03-28 17:10:29 +010069#define htobe16(v) (v)
70#define htobe32(v) (v)
71#define be16toh(v) (v)
72#define be32toh(v) (v)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020073#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)
Mirko Bonadeib7217612019-03-28 17:10:29 +010079#if defined(WEBRTC_WIN)
80#define htobe64(v) (v)
81#define be64toh(v) (v)
82#endif // defined(WEBRTC_WIN)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020083#if defined(__native_client__)
84#define htobe64(v) (v)
85#define be64toh(v) (v)
Mirko Bonadeib7217612019-03-28 17:10:29 +010086#endif // defined(__native_client__)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020087#else
Niels Möllerb06b0a62018-05-25 10:05:34 +020088#error WEBRTC_ARCH_BIG_ENDIAN or WEBRTC_ARCH_LITTLE_ENDIAN must be defined.
89#endif // defined(WEBRTC_ARCH_LITTLE_ENDIAN)
Mirko Bonadeib7217612019-03-28 17:10:29 +010090
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020091#elif defined(WEBRTC_POSIX)
92#include <endian.h>
Mirko Bonadeib7217612019-03-28 17:10:29 +010093#else
94#error "Missing byte order functions for this arch."
95#endif // defined(WEBRTC_MAC)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020096
97namespace rtc {
98
99// Reading and writing of little and big-endian numbers from memory
100
101inline void Set8(void* memory, size_t offset, uint8_t v) {
102 static_cast<uint8_t*>(memory)[offset] = v;
103}
104
105inline uint8_t Get8(const void* memory, size_t offset) {
106 return static_cast<const uint8_t*>(memory)[offset];
107}
108
109inline void SetBE16(void* memory, uint16_t v) {
110 *static_cast<uint16_t*>(memory) = htobe16(v);
111}
112
113inline void SetBE32(void* memory, uint32_t v) {
114 *static_cast<uint32_t*>(memory) = htobe32(v);
115}
116
117inline void SetBE64(void* memory, uint64_t v) {
118 *static_cast<uint64_t*>(memory) = htobe64(v);
119}
120
121inline uint16_t GetBE16(const void* memory) {
122 return be16toh(*static_cast<const uint16_t*>(memory));
123}
124
125inline uint32_t GetBE32(const void* memory) {
126 return be32toh(*static_cast<const uint32_t*>(memory));
127}
128
129inline uint64_t GetBE64(const void* memory) {
130 return be64toh(*static_cast<const uint64_t*>(memory));
131}
132
133inline void SetLE16(void* memory, uint16_t v) {
134 *static_cast<uint16_t*>(memory) = htole16(v);
135}
136
137inline void SetLE32(void* memory, uint32_t v) {
138 *static_cast<uint32_t*>(memory) = htole32(v);
139}
140
141inline void SetLE64(void* memory, uint64_t v) {
142 *static_cast<uint64_t*>(memory) = htole64(v);
143}
144
145inline uint16_t GetLE16(const void* memory) {
146 return le16toh(*static_cast<const uint16_t*>(memory));
147}
148
149inline uint32_t GetLE32(const void* memory) {
150 return le32toh(*static_cast<const uint32_t*>(memory));
151}
152
153inline 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.
158inline bool IsHostBigEndian() {
Niels Möllerb06b0a62018-05-25 10:05:34 +0200159#if defined(WEBRTC_ARCH_BIG_ENDIAN)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200160 return true;
161#else
162 return false;
163#endif
164}
165
166inline uint16_t HostToNetwork16(uint16_t n) {
167 return htobe16(n);
168}
169
170inline uint32_t HostToNetwork32(uint32_t n) {
171 return htobe32(n);
172}
173
174inline uint64_t HostToNetwork64(uint64_t n) {
175 return htobe64(n);
176}
177
178inline uint16_t NetworkToHost16(uint16_t n) {
179 return be16toh(n);
180}
181
182inline uint32_t NetworkToHost32(uint32_t n) {
183 return be32toh(n);
184}
185
186inline uint64_t NetworkToHost64(uint64_t n) {
187 return be64toh(n);
188}
189
190} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000191
Steve Anton10542f22019-01-11 09:11:00 -0800192#endif // RTC_BASE_BYTE_ORDER_H_