1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
/*
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef RTC_BASE_BYTE_ORDER_H_
#define RTC_BASE_BYTE_ORDER_H_
#include <stdint.h>
#include <cstring>
#if defined(WEBRTC_POSIX) && !defined(__native_client__)
#include <arpa/inet.h>
#endif
#include "rtc_base/system/arch.h"
#if defined(WEBRTC_MAC)
#include <libkern/OSByteOrder.h>
#define htobe16(v) OSSwapHostToBigInt16(v)
#define htobe32(v) OSSwapHostToBigInt32(v)
#define htobe64(v) OSSwapHostToBigInt64(v)
#define be16toh(v) OSSwapBigToHostInt16(v)
#define be32toh(v) OSSwapBigToHostInt32(v)
#define be64toh(v) OSSwapBigToHostInt64(v)
#define htole16(v) OSSwapHostToLittleInt16(v)
#define htole32(v) OSSwapHostToLittleInt32(v)
#define htole64(v) OSSwapHostToLittleInt64(v)
#define le16toh(v) OSSwapLittleToHostInt16(v)
#define le32toh(v) OSSwapLittleToHostInt32(v)
#define le64toh(v) OSSwapLittleToHostInt64(v)
#elif defined(WEBRTC_WIN) || defined(__native_client__)
#if defined(WEBRTC_WIN)
#include <stdlib.h>
#include <winsock2.h>
#else
#include <netinet/in.h> // no-presubmit-check
#endif // defined(WEBRTC_WIN)
#if defined(WEBRTC_ARCH_LITTLE_ENDIAN)
#define htobe16(v) htons(v)
#define htobe32(v) htonl(v)
#define be16toh(v) ntohs(v)
#define be32toh(v) ntohl(v)
#define htole16(v) (v)
#define htole32(v) (v)
#define htole64(v) (v)
#define le16toh(v) (v)
#define le32toh(v) (v)
#define le64toh(v) (v)
#if defined(WEBRTC_WIN)
#define htobe64(v) _byteswap_uint64(v)
#define be64toh(v) _byteswap_uint64(v)
#endif // defined(WEBRTC_WIN)
#if defined(__native_client__)
#define htobe64(v) __builtin_bswap64(v)
#define be64toh(v) __builtin_bswap64(v)
#endif // defined(__native_client__)
#elif defined(WEBRTC_ARCH_BIG_ENDIAN)
#define htobe16(v) (v)
#define htobe32(v) (v)
#define be16toh(v) (v)
#define be32toh(v) (v)
#define htole16(v) __builtin_bswap16(v)
#define htole32(v) __builtin_bswap32(v)
#define htole64(v) __builtin_bswap64(v)
#define le16toh(v) __builtin_bswap16(v)
#define le32toh(v) __builtin_bswap32(v)
#define le64toh(v) __builtin_bswap64(v)
#if defined(WEBRTC_WIN)
#define htobe64(v) (v)
#define be64toh(v) (v)
#endif // defined(WEBRTC_WIN)
#if defined(__native_client__)
#define htobe64(v) (v)
#define be64toh(v) (v)
#endif // defined(__native_client__)
#else
#error WEBRTC_ARCH_BIG_ENDIAN or WEBRTC_ARCH_LITTLE_ENDIAN must be defined.
#endif // defined(WEBRTC_ARCH_LITTLE_ENDIAN)
#elif defined(WEBRTC_POSIX)
#include <endian.h>
#else
#error "Missing byte order functions for this arch."
#endif // defined(WEBRTC_MAC)
namespace webrtc {
// Reading and writing of little and big-endian numbers from memory
inline void Set8(void* memory, size_t offset, uint8_t v) {
static_cast<uint8_t*>(memory)[offset] = v;
}
inline uint8_t Get8(const void* memory, size_t offset) {
return static_cast<const uint8_t*>(memory)[offset];
}
inline void SetBE16(void* memory, uint16_t v) {
uint16_t val = htobe16(v);
memcpy(memory, &val, sizeof(val));
}
inline void SetBE32(void* memory, uint32_t v) {
uint32_t val = htobe32(v);
memcpy(memory, &val, sizeof(val));
}
inline void SetBE64(void* memory, uint64_t v) {
uint64_t val = htobe64(v);
memcpy(memory, &val, sizeof(val));
}
inline uint16_t GetBE16(const void* memory) {
uint16_t val;
memcpy(&val, memory, sizeof(val));
return be16toh(val);
}
inline uint32_t GetBE32(const void* memory) {
uint32_t val;
memcpy(&val, memory, sizeof(val));
return be32toh(val);
}
inline uint64_t GetBE64(const void* memory) {
uint64_t val;
memcpy(&val, memory, sizeof(val));
return be64toh(val);
}
inline void SetLE16(void* memory, uint16_t v) {
uint16_t val = htole16(v);
memcpy(memory, &val, sizeof(val));
}
inline void SetLE32(void* memory, uint32_t v) {
uint32_t val = htole32(v);
memcpy(memory, &val, sizeof(val));
}
inline void SetLE64(void* memory, uint64_t v) {
uint64_t val = htole64(v);
memcpy(memory, &val, sizeof(val));
}
inline uint16_t GetLE16(const void* memory) {
uint16_t val;
memcpy(&val, memory, sizeof(val));
return le16toh(val);
}
inline uint32_t GetLE32(const void* memory) {
uint32_t val;
memcpy(&val, memory, sizeof(val));
return le32toh(val);
}
inline uint64_t GetLE64(const void* memory) {
uint64_t val;
memcpy(&val, memory, sizeof(val));
return le64toh(val);
}
// Check if the current host is big endian.
inline bool IsHostBigEndian() {
#if defined(WEBRTC_ARCH_BIG_ENDIAN)
return true;
#else
return false;
#endif
}
inline uint16_t HostToNetwork16(uint16_t n) {
return htobe16(n);
}
inline uint32_t HostToNetwork32(uint32_t n) {
return htobe32(n);
}
inline uint64_t HostToNetwork64(uint64_t n) {
return htobe64(n);
}
inline uint16_t NetworkToHost16(uint16_t n) {
return be16toh(n);
}
inline uint32_t NetworkToHost32(uint32_t n) {
return be32toh(n);
}
inline uint64_t NetworkToHost64(uint64_t n) {
return be64toh(n);
}
} // namespace webrtc
// Re-export symbols from the webrtc namespace for backwards compatibility.
// TODO(bugs.webrtc.org/4222596): Remove once all references are updated.
#ifdef WEBRTC_ALLOW_DEPRECATED_NAMESPACES
namespace rtc {
using ::webrtc::Get8;
using ::webrtc::GetBE16;
using ::webrtc::GetBE32;
using ::webrtc::GetBE64;
using ::webrtc::GetLE16;
using ::webrtc::GetLE32;
using ::webrtc::GetLE64;
using ::webrtc::HostToNetwork16;
using ::webrtc::HostToNetwork32;
using ::webrtc::HostToNetwork64;
using ::webrtc::IsHostBigEndian;
using ::webrtc::NetworkToHost16;
using ::webrtc::NetworkToHost32;
using ::webrtc::NetworkToHost64;
using ::webrtc::Set8;
using ::webrtc::SetBE16;
using ::webrtc::SetBE32;
using ::webrtc::SetBE64;
using ::webrtc::SetLE16;
using ::webrtc::SetLE32;
using ::webrtc::SetLE64;
} // namespace rtc
#endif // WEBRTC_ALLOW_DEPRECATED_NAMESPACES
#endif // RTC_BASE_BYTE_ORDER_H_
|