blob: b8f8ae9f7ad8086833d4b6a787138535afd7682a [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 17:11:0011#ifndef RTC_BASE_BYTE_ORDER_H_
12#define RTC_BASE_BYTE_ORDER_H_
henrike@webrtc.orgf0488722014-05-13 18:00:2613
Niels Möller14682a32018-05-24 06:54:2514#include <stdint.h>
15
Byoungchan Lee1b80be32022-12-04 23:52:3916#include <cstring>
17
Henrik Kjellanderec78f1c2017-06-29 05:52:5018#if defined(WEBRTC_POSIX) && !defined(__native_client__)
19#include <arpa/inet.h>
20#endif
henrike@webrtc.orgf0488722014-05-13 18:00:2621
Niels Möllerb06b0a62018-05-25 08:05:3422#include "rtc_base/system/arch.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5023
24#if defined(WEBRTC_MAC)
25#include <libkern/OSByteOrder.h>
26
27#define htobe16(v) OSSwapHostToBigInt16(v)
28#define htobe32(v) OSSwapHostToBigInt32(v)
29#define htobe64(v) OSSwapHostToBigInt64(v)
30#define be16toh(v) OSSwapBigToHostInt16(v)
31#define be32toh(v) OSSwapBigToHostInt32(v)
32#define be64toh(v) OSSwapBigToHostInt64(v)
33
34#define htole16(v) OSSwapHostToLittleInt16(v)
35#define htole32(v) OSSwapHostToLittleInt32(v)
36#define htole64(v) OSSwapHostToLittleInt64(v)
37#define le16toh(v) OSSwapLittleToHostInt16(v)
38#define le32toh(v) OSSwapLittleToHostInt32(v)
39#define le64toh(v) OSSwapLittleToHostInt64(v)
Mirko Bonadeib7217612019-03-28 16:10:2940
Henrik Kjellanderec78f1c2017-06-29 05:52:5041#elif defined(WEBRTC_WIN) || defined(__native_client__)
42
43#if defined(WEBRTC_WIN)
44#include <stdlib.h>
45#include <winsock2.h>
46#else
47#include <netinet/in.h>
Mirko Bonadeib7217612019-03-28 16:10:2948#endif // defined(WEBRTC_WIN)
Henrik Kjellanderec78f1c2017-06-29 05:52:5049
Mirko Bonadeib7217612019-03-28 16:10:2950#if defined(WEBRTC_ARCH_LITTLE_ENDIAN)
Henrik Kjellanderec78f1c2017-06-29 05:52:5051#define htobe16(v) htons(v)
52#define htobe32(v) htonl(v)
53#define be16toh(v) ntohs(v)
54#define be32toh(v) ntohl(v)
Henrik Kjellanderec78f1c2017-06-29 05:52:5055#define htole16(v) (v)
56#define htole32(v) (v)
57#define htole64(v) (v)
58#define le16toh(v) (v)
59#define le32toh(v) (v)
60#define le64toh(v) (v)
Mirko Bonadeib7217612019-03-28 16:10:2961#if defined(WEBRTC_WIN)
62#define htobe64(v) _byteswap_uint64(v)
63#define be64toh(v) _byteswap_uint64(v)
64#endif // defined(WEBRTC_WIN)
Henrik Kjellanderec78f1c2017-06-29 05:52:5065#if defined(__native_client__)
66#define htobe64(v) __builtin_bswap64(v)
67#define be64toh(v) __builtin_bswap64(v)
Mirko Bonadeib7217612019-03-28 16:10:2968#endif // defined(__native_client__)
69
Niels Möllerb06b0a62018-05-25 08:05:3470#elif defined(WEBRTC_ARCH_BIG_ENDIAN)
Mirko Bonadeib7217612019-03-28 16:10:2971#define htobe16(v) (v)
72#define htobe32(v) (v)
73#define be16toh(v) (v)
74#define be32toh(v) (v)
Henrik Kjellanderec78f1c2017-06-29 05:52:5075#define htole16(v) __builtin_bswap16(v)
76#define htole32(v) __builtin_bswap32(v)
77#define htole64(v) __builtin_bswap64(v)
78#define le16toh(v) __builtin_bswap16(v)
79#define le32toh(v) __builtin_bswap32(v)
80#define le64toh(v) __builtin_bswap64(v)
Mirko Bonadeib7217612019-03-28 16:10:2981#if defined(WEBRTC_WIN)
82#define htobe64(v) (v)
83#define be64toh(v) (v)
84#endif // defined(WEBRTC_WIN)
Henrik Kjellanderec78f1c2017-06-29 05:52:5085#if defined(__native_client__)
86#define htobe64(v) (v)
87#define be64toh(v) (v)
Mirko Bonadeib7217612019-03-28 16:10:2988#endif // defined(__native_client__)
Henrik Kjellanderec78f1c2017-06-29 05:52:5089#else
Niels Möllerb06b0a62018-05-25 08:05:3490#error WEBRTC_ARCH_BIG_ENDIAN or WEBRTC_ARCH_LITTLE_ENDIAN must be defined.
91#endif // defined(WEBRTC_ARCH_LITTLE_ENDIAN)
Mirko Bonadeib7217612019-03-28 16:10:2992
Henrik Kjellanderec78f1c2017-06-29 05:52:5093#elif defined(WEBRTC_POSIX)
94#include <endian.h>
Mirko Bonadeib7217612019-03-28 16:10:2995#else
96#error "Missing byte order functions for this arch."
97#endif // defined(WEBRTC_MAC)
Henrik Kjellanderec78f1c2017-06-29 05:52:5098
99namespace rtc {
100
101// Reading and writing of little and big-endian numbers from memory
102
103inline void Set8(void* memory, size_t offset, uint8_t v) {
104 static_cast<uint8_t*>(memory)[offset] = v;
105}
106
107inline uint8_t Get8(const void* memory, size_t offset) {
108 return static_cast<const uint8_t*>(memory)[offset];
109}
110
111inline void SetBE16(void* memory, uint16_t v) {
Byoungchan Lee1b80be32022-12-04 23:52:39112 uint16_t val = htobe16(v);
113 memcpy(memory, &val, sizeof(val));
Henrik Kjellanderec78f1c2017-06-29 05:52:50114}
115
116inline void SetBE32(void* memory, uint32_t v) {
Byoungchan Lee1b80be32022-12-04 23:52:39117 uint32_t val = htobe32(v);
118 memcpy(memory, &val, sizeof(val));
Henrik Kjellanderec78f1c2017-06-29 05:52:50119}
120
121inline void SetBE64(void* memory, uint64_t v) {
Byoungchan Lee1b80be32022-12-04 23:52:39122 uint64_t val = htobe64(v);
123 memcpy(memory, &val, sizeof(val));
Henrik Kjellanderec78f1c2017-06-29 05:52:50124}
125
126inline uint16_t GetBE16(const void* memory) {
Byoungchan Lee1b80be32022-12-04 23:52:39127 uint16_t val;
128 memcpy(&val, memory, sizeof(val));
129 return be16toh(val);
Henrik Kjellanderec78f1c2017-06-29 05:52:50130}
131
132inline uint32_t GetBE32(const void* memory) {
Byoungchan Lee1b80be32022-12-04 23:52:39133 uint32_t val;
134 memcpy(&val, memory, sizeof(val));
135 return be32toh(val);
Henrik Kjellanderec78f1c2017-06-29 05:52:50136}
137
138inline uint64_t GetBE64(const void* memory) {
Byoungchan Lee1b80be32022-12-04 23:52:39139 uint64_t val;
140 memcpy(&val, memory, sizeof(val));
141 return be64toh(val);
Henrik Kjellanderec78f1c2017-06-29 05:52:50142}
143
144inline void SetLE16(void* memory, uint16_t v) {
Byoungchan Lee1b80be32022-12-04 23:52:39145 uint16_t val = htole16(v);
146 memcpy(memory, &val, sizeof(val));
Henrik Kjellanderec78f1c2017-06-29 05:52:50147}
148
149inline void SetLE32(void* memory, uint32_t v) {
Byoungchan Lee1b80be32022-12-04 23:52:39150 uint32_t val = htole32(v);
151 memcpy(memory, &val, sizeof(val));
Henrik Kjellanderec78f1c2017-06-29 05:52:50152}
153
154inline void SetLE64(void* memory, uint64_t v) {
Byoungchan Lee1b80be32022-12-04 23:52:39155 uint64_t val = htole64(v);
156 memcpy(memory, &val, sizeof(val));
Henrik Kjellanderec78f1c2017-06-29 05:52:50157}
158
159inline uint16_t GetLE16(const void* memory) {
Byoungchan Lee1b80be32022-12-04 23:52:39160 uint16_t val;
161 memcpy(&val, memory, sizeof(val));
162 return le16toh(val);
Henrik Kjellanderec78f1c2017-06-29 05:52:50163}
164
165inline uint32_t GetLE32(const void* memory) {
Byoungchan Lee1b80be32022-12-04 23:52:39166 uint32_t val;
167 memcpy(&val, memory, sizeof(val));
168 return le32toh(val);
Henrik Kjellanderec78f1c2017-06-29 05:52:50169}
170
171inline uint64_t GetLE64(const void* memory) {
Byoungchan Lee1b80be32022-12-04 23:52:39172 uint64_t val;
173 memcpy(&val, memory, sizeof(val));
174 return le64toh(val);
Henrik Kjellanderec78f1c2017-06-29 05:52:50175}
176
177// Check if the current host is big endian.
178inline bool IsHostBigEndian() {
Niels Möllerb06b0a62018-05-25 08:05:34179#if defined(WEBRTC_ARCH_BIG_ENDIAN)
Henrik Kjellanderec78f1c2017-06-29 05:52:50180 return true;
181#else
182 return false;
183#endif
184}
185
186inline uint16_t HostToNetwork16(uint16_t n) {
187 return htobe16(n);
188}
189
190inline uint32_t HostToNetwork32(uint32_t n) {
191 return htobe32(n);
192}
193
194inline uint64_t HostToNetwork64(uint64_t n) {
195 return htobe64(n);
196}
197
198inline uint16_t NetworkToHost16(uint16_t n) {
199 return be16toh(n);
200}
201
202inline uint32_t NetworkToHost32(uint32_t n) {
203 return be32toh(n);
204}
205
206inline uint64_t NetworkToHost64(uint64_t n) {
207 return be64toh(n);
208}
209
210} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26211
Steve Anton10542f22019-01-11 17:11:00212#endif // RTC_BASE_BYTE_ORDER_H_