blob: 2b50f0d3d04a4031477d3b2b55f0f296550a3665 [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)
38#elif defined(WEBRTC_WIN) || defined(__native_client__)
39
40#if defined(WEBRTC_WIN)
41#include <stdlib.h>
42#include <winsock2.h>
43#else
44#include <netinet/in.h>
45#endif
46
47#define htobe16(v) htons(v)
48#define htobe32(v) htonl(v)
49#define be16toh(v) ntohs(v)
50#define be32toh(v) ntohl(v)
51#if defined(WEBRTC_WIN)
52#define htobe64(v) htonll(v)
53#define be64toh(v) ntohll(v)
54#endif
55
Niels Möllerb06b0a62018-05-25 10:05:34 +020056#if defined(WEBRTC_ARCH_LITTLE_ENDIAN)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020057#define htole16(v) (v)
58#define htole32(v) (v)
59#define htole64(v) (v)
60#define le16toh(v) (v)
61#define le32toh(v) (v)
62#define le64toh(v) (v)
63#if defined(__native_client__)
64#define htobe64(v) __builtin_bswap64(v)
65#define be64toh(v) __builtin_bswap64(v)
66#endif
Niels Möllerb06b0a62018-05-25 10:05:34 +020067#elif defined(WEBRTC_ARCH_BIG_ENDIAN)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020068#define htole16(v) __builtin_bswap16(v)
69#define htole32(v) __builtin_bswap32(v)
70#define htole64(v) __builtin_bswap64(v)
71#define le16toh(v) __builtin_bswap16(v)
72#define le32toh(v) __builtin_bswap32(v)
73#define le64toh(v) __builtin_bswap64(v)
74#if defined(__native_client__)
75#define htobe64(v) (v)
76#define be64toh(v) (v)
77#endif
78#else
Niels Möllerb06b0a62018-05-25 10:05:34 +020079#error WEBRTC_ARCH_BIG_ENDIAN or WEBRTC_ARCH_LITTLE_ENDIAN must be defined.
80#endif // defined(WEBRTC_ARCH_LITTLE_ENDIAN)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020081#elif defined(WEBRTC_POSIX)
82#include <endian.h>
83#endif
84
85namespace rtc {
86
87// Reading and writing of little and big-endian numbers from memory
88
89inline void Set8(void* memory, size_t offset, uint8_t v) {
90 static_cast<uint8_t*>(memory)[offset] = v;
91}
92
93inline uint8_t Get8(const void* memory, size_t offset) {
94 return static_cast<const uint8_t*>(memory)[offset];
95}
96
97inline void SetBE16(void* memory, uint16_t v) {
98 *static_cast<uint16_t*>(memory) = htobe16(v);
99}
100
101inline void SetBE32(void* memory, uint32_t v) {
102 *static_cast<uint32_t*>(memory) = htobe32(v);
103}
104
105inline void SetBE64(void* memory, uint64_t v) {
106 *static_cast<uint64_t*>(memory) = htobe64(v);
107}
108
109inline uint16_t GetBE16(const void* memory) {
110 return be16toh(*static_cast<const uint16_t*>(memory));
111}
112
113inline uint32_t GetBE32(const void* memory) {
114 return be32toh(*static_cast<const uint32_t*>(memory));
115}
116
117inline uint64_t GetBE64(const void* memory) {
118 return be64toh(*static_cast<const uint64_t*>(memory));
119}
120
121inline void SetLE16(void* memory, uint16_t v) {
122 *static_cast<uint16_t*>(memory) = htole16(v);
123}
124
125inline void SetLE32(void* memory, uint32_t v) {
126 *static_cast<uint32_t*>(memory) = htole32(v);
127}
128
129inline void SetLE64(void* memory, uint64_t v) {
130 *static_cast<uint64_t*>(memory) = htole64(v);
131}
132
133inline uint16_t GetLE16(const void* memory) {
134 return le16toh(*static_cast<const uint16_t*>(memory));
135}
136
137inline uint32_t GetLE32(const void* memory) {
138 return le32toh(*static_cast<const uint32_t*>(memory));
139}
140
141inline uint64_t GetLE64(const void* memory) {
142 return le64toh(*static_cast<const uint64_t*>(memory));
143}
144
145// Check if the current host is big endian.
146inline bool IsHostBigEndian() {
Niels Möllerb06b0a62018-05-25 10:05:34 +0200147#if defined(WEBRTC_ARCH_BIG_ENDIAN)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200148 return true;
149#else
150 return false;
151#endif
152}
153
154inline uint16_t HostToNetwork16(uint16_t n) {
155 return htobe16(n);
156}
157
158inline uint32_t HostToNetwork32(uint32_t n) {
159 return htobe32(n);
160}
161
162inline uint64_t HostToNetwork64(uint64_t n) {
163 return htobe64(n);
164}
165
166inline uint16_t NetworkToHost16(uint16_t n) {
167 return be16toh(n);
168}
169
170inline uint32_t NetworkToHost32(uint32_t n) {
171 return be32toh(n);
172}
173
174inline uint64_t NetworkToHost64(uint64_t n) {
175 return be64toh(n);
176}
177
178} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000179
Steve Anton10542f22019-01-11 09:11:00 -0800180#endif // RTC_BASE_BYTE_ORDER_H_