henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #ifndef RTC_BASE_BYTE_ORDER_H_ |
| 12 | #define RTC_BASE_BYTE_ORDER_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 13 | |
Niels Möller | 14682a3 | 2018-05-24 06:54:25 | [diff] [blame] | 14 | #include <stdint.h> |
| 15 | |
Byoungchan Lee | 1b80be3 | 2022-12-04 23:52:39 | [diff] [blame] | 16 | #include <cstring> |
| 17 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 18 | #if defined(WEBRTC_POSIX) && !defined(__native_client__) |
| 19 | #include <arpa/inet.h> |
| 20 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 21 | |
Niels Möller | b06b0a6 | 2018-05-25 08:05:34 | [diff] [blame] | 22 | #include "rtc_base/system/arch.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 23 | |
| 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 Bonadei | b721761 | 2019-03-28 16:10:29 | [diff] [blame] | 40 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 41 | #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 Bonadei | b721761 | 2019-03-28 16:10:29 | [diff] [blame] | 48 | #endif // defined(WEBRTC_WIN) |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 49 | |
Mirko Bonadei | b721761 | 2019-03-28 16:10:29 | [diff] [blame] | 50 | #if defined(WEBRTC_ARCH_LITTLE_ENDIAN) |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 51 | #define htobe16(v) htons(v) |
| 52 | #define htobe32(v) htonl(v) |
| 53 | #define be16toh(v) ntohs(v) |
| 54 | #define be32toh(v) ntohl(v) |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 55 | #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 Bonadei | b721761 | 2019-03-28 16:10:29 | [diff] [blame] | 61 | #if defined(WEBRTC_WIN) |
| 62 | #define htobe64(v) _byteswap_uint64(v) |
| 63 | #define be64toh(v) _byteswap_uint64(v) |
| 64 | #endif // defined(WEBRTC_WIN) |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 65 | #if defined(__native_client__) |
| 66 | #define htobe64(v) __builtin_bswap64(v) |
| 67 | #define be64toh(v) __builtin_bswap64(v) |
Mirko Bonadei | b721761 | 2019-03-28 16:10:29 | [diff] [blame] | 68 | #endif // defined(__native_client__) |
| 69 | |
Niels Möller | b06b0a6 | 2018-05-25 08:05:34 | [diff] [blame] | 70 | #elif defined(WEBRTC_ARCH_BIG_ENDIAN) |
Mirko Bonadei | b721761 | 2019-03-28 16:10:29 | [diff] [blame] | 71 | #define htobe16(v) (v) |
| 72 | #define htobe32(v) (v) |
| 73 | #define be16toh(v) (v) |
| 74 | #define be32toh(v) (v) |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 75 | #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 Bonadei | b721761 | 2019-03-28 16:10:29 | [diff] [blame] | 81 | #if defined(WEBRTC_WIN) |
| 82 | #define htobe64(v) (v) |
| 83 | #define be64toh(v) (v) |
| 84 | #endif // defined(WEBRTC_WIN) |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 85 | #if defined(__native_client__) |
| 86 | #define htobe64(v) (v) |
| 87 | #define be64toh(v) (v) |
Mirko Bonadei | b721761 | 2019-03-28 16:10:29 | [diff] [blame] | 88 | #endif // defined(__native_client__) |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 89 | #else |
Niels Möller | b06b0a6 | 2018-05-25 08:05:34 | [diff] [blame] | 90 | #error WEBRTC_ARCH_BIG_ENDIAN or WEBRTC_ARCH_LITTLE_ENDIAN must be defined. |
| 91 | #endif // defined(WEBRTC_ARCH_LITTLE_ENDIAN) |
Mirko Bonadei | b721761 | 2019-03-28 16:10:29 | [diff] [blame] | 92 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 93 | #elif defined(WEBRTC_POSIX) |
| 94 | #include <endian.h> |
Mirko Bonadei | b721761 | 2019-03-28 16:10:29 | [diff] [blame] | 95 | #else |
| 96 | #error "Missing byte order functions for this arch." |
| 97 | #endif // defined(WEBRTC_MAC) |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 98 | |
| 99 | namespace rtc { |
| 100 | |
| 101 | // Reading and writing of little and big-endian numbers from memory |
| 102 | |
| 103 | inline void Set8(void* memory, size_t offset, uint8_t v) { |
| 104 | static_cast<uint8_t*>(memory)[offset] = v; |
| 105 | } |
| 106 | |
| 107 | inline uint8_t Get8(const void* memory, size_t offset) { |
| 108 | return static_cast<const uint8_t*>(memory)[offset]; |
| 109 | } |
| 110 | |
| 111 | inline void SetBE16(void* memory, uint16_t v) { |
Byoungchan Lee | 1b80be3 | 2022-12-04 23:52:39 | [diff] [blame] | 112 | uint16_t val = htobe16(v); |
| 113 | memcpy(memory, &val, sizeof(val)); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | inline void SetBE32(void* memory, uint32_t v) { |
Byoungchan Lee | 1b80be3 | 2022-12-04 23:52:39 | [diff] [blame] | 117 | uint32_t val = htobe32(v); |
| 118 | memcpy(memory, &val, sizeof(val)); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | inline void SetBE64(void* memory, uint64_t v) { |
Byoungchan Lee | 1b80be3 | 2022-12-04 23:52:39 | [diff] [blame] | 122 | uint64_t val = htobe64(v); |
| 123 | memcpy(memory, &val, sizeof(val)); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | inline uint16_t GetBE16(const void* memory) { |
Byoungchan Lee | 1b80be3 | 2022-12-04 23:52:39 | [diff] [blame] | 127 | uint16_t val; |
| 128 | memcpy(&val, memory, sizeof(val)); |
| 129 | return be16toh(val); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | inline uint32_t GetBE32(const void* memory) { |
Byoungchan Lee | 1b80be3 | 2022-12-04 23:52:39 | [diff] [blame] | 133 | uint32_t val; |
| 134 | memcpy(&val, memory, sizeof(val)); |
| 135 | return be32toh(val); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | inline uint64_t GetBE64(const void* memory) { |
Byoungchan Lee | 1b80be3 | 2022-12-04 23:52:39 | [diff] [blame] | 139 | uint64_t val; |
| 140 | memcpy(&val, memory, sizeof(val)); |
| 141 | return be64toh(val); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | inline void SetLE16(void* memory, uint16_t v) { |
Byoungchan Lee | 1b80be3 | 2022-12-04 23:52:39 | [diff] [blame] | 145 | uint16_t val = htole16(v); |
| 146 | memcpy(memory, &val, sizeof(val)); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | inline void SetLE32(void* memory, uint32_t v) { |
Byoungchan Lee | 1b80be3 | 2022-12-04 23:52:39 | [diff] [blame] | 150 | uint32_t val = htole32(v); |
| 151 | memcpy(memory, &val, sizeof(val)); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | inline void SetLE64(void* memory, uint64_t v) { |
Byoungchan Lee | 1b80be3 | 2022-12-04 23:52:39 | [diff] [blame] | 155 | uint64_t val = htole64(v); |
| 156 | memcpy(memory, &val, sizeof(val)); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | inline uint16_t GetLE16(const void* memory) { |
Byoungchan Lee | 1b80be3 | 2022-12-04 23:52:39 | [diff] [blame] | 160 | uint16_t val; |
| 161 | memcpy(&val, memory, sizeof(val)); |
| 162 | return le16toh(val); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | inline uint32_t GetLE32(const void* memory) { |
Byoungchan Lee | 1b80be3 | 2022-12-04 23:52:39 | [diff] [blame] | 166 | uint32_t val; |
| 167 | memcpy(&val, memory, sizeof(val)); |
| 168 | return le32toh(val); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | inline uint64_t GetLE64(const void* memory) { |
Byoungchan Lee | 1b80be3 | 2022-12-04 23:52:39 | [diff] [blame] | 172 | uint64_t val; |
| 173 | memcpy(&val, memory, sizeof(val)); |
| 174 | return le64toh(val); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // Check if the current host is big endian. |
| 178 | inline bool IsHostBigEndian() { |
Niels Möller | b06b0a6 | 2018-05-25 08:05:34 | [diff] [blame] | 179 | #if defined(WEBRTC_ARCH_BIG_ENDIAN) |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 180 | return true; |
| 181 | #else |
| 182 | return false; |
| 183 | #endif |
| 184 | } |
| 185 | |
| 186 | inline uint16_t HostToNetwork16(uint16_t n) { |
| 187 | return htobe16(n); |
| 188 | } |
| 189 | |
| 190 | inline uint32_t HostToNetwork32(uint32_t n) { |
| 191 | return htobe32(n); |
| 192 | } |
| 193 | |
| 194 | inline uint64_t HostToNetwork64(uint64_t n) { |
| 195 | return htobe64(n); |
| 196 | } |
| 197 | |
| 198 | inline uint16_t NetworkToHost16(uint16_t n) { |
| 199 | return be16toh(n); |
| 200 | } |
| 201 | |
| 202 | inline uint32_t NetworkToHost32(uint32_t n) { |
| 203 | return be32toh(n); |
| 204 | } |
| 205 | |
| 206 | inline uint64_t NetworkToHost64(uint64_t n) { |
| 207 | return be64toh(n); |
| 208 | } |
| 209 | |
| 210 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 211 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 212 | #endif // RTC_BASE_BYTE_ORDER_H_ |