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