| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #ifndef WEBRTC_BASE_BYTEORDER_H_ | 11 #ifndef WEBRTC_BASE_BYTEORDER_H_ |
| 12 #define WEBRTC_BASE_BYTEORDER_H_ | 12 #define WEBRTC_BASE_BYTEORDER_H_ |
| 13 | 13 |
| 14 #if defined(WEBRTC_POSIX) && !defined(__native_client__) | |
| 15 #include <arpa/inet.h> | |
| 16 #endif | |
| 17 | 14 |
| 18 #include "webrtc/base/basictypes.h" | 15 // This header is deprecated and is just left here temporarily during |
| 19 | 16 // refactoring. See https://bugs.webrtc.org/7634 for more details. |
| 20 #if defined(WEBRTC_MAC) | 17 #include "webrtc/rtc_base/byteorder.h" |
| 21 #include <libkern/OSByteOrder.h> | |
| 22 | |
| 23 #define htobe16(v) OSSwapHostToBigInt16(v) | |
| 24 #define htobe32(v) OSSwapHostToBigInt32(v) | |
| 25 #define htobe64(v) OSSwapHostToBigInt64(v) | |
| 26 #define be16toh(v) OSSwapBigToHostInt16(v) | |
| 27 #define be32toh(v) OSSwapBigToHostInt32(v) | |
| 28 #define be64toh(v) OSSwapBigToHostInt64(v) | |
| 29 | |
| 30 #define htole16(v) OSSwapHostToLittleInt16(v) | |
| 31 #define htole32(v) OSSwapHostToLittleInt32(v) | |
| 32 #define htole64(v) OSSwapHostToLittleInt64(v) | |
| 33 #define le16toh(v) OSSwapLittleToHostInt16(v) | |
| 34 #define le32toh(v) OSSwapLittleToHostInt32(v) | |
| 35 #define le64toh(v) OSSwapLittleToHostInt64(v) | |
| 36 #elif defined(WEBRTC_WIN) || defined(__native_client__) | |
| 37 | |
| 38 #if defined(WEBRTC_WIN) | |
| 39 #include <stdlib.h> | |
| 40 #include <winsock2.h> | |
| 41 #else | |
| 42 #include <netinet/in.h> | |
| 43 #endif | |
| 44 | |
| 45 #define htobe16(v) htons(v) | |
| 46 #define htobe32(v) htonl(v) | |
| 47 #define be16toh(v) ntohs(v) | |
| 48 #define be32toh(v) ntohl(v) | |
| 49 #if defined(WEBRTC_WIN) | |
| 50 #define htobe64(v) htonll(v) | |
| 51 #define be64toh(v) ntohll(v) | |
| 52 #endif | |
| 53 | |
| 54 #if defined(RTC_ARCH_CPU_LITTLE_ENDIAN) | |
| 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) | |
| 61 #if defined(__native_client__) | |
| 62 #define htobe64(v) __builtin_bswap64(v) | |
| 63 #define be64toh(v) __builtin_bswap64(v) | |
| 64 #endif | |
| 65 #elif defined(RTC_ARCH_CPU_BIG_ENDIAN) | |
| 66 #define htole16(v) __builtin_bswap16(v) | |
| 67 #define htole32(v) __builtin_bswap32(v) | |
| 68 #define htole64(v) __builtin_bswap64(v) | |
| 69 #define le16toh(v) __builtin_bswap16(v) | |
| 70 #define le32toh(v) __builtin_bswap32(v) | |
| 71 #define le64toh(v) __builtin_bswap64(v) | |
| 72 #if defined(__native_client__) | |
| 73 #define htobe64(v) (v) | |
| 74 #define be64toh(v) (v) | |
| 75 #endif | |
| 76 #else | |
| 77 #error RTC_ARCH_CPU_BIG_ENDIAN or RTC_ARCH_CPU_LITTLE_ENDIAN must be defined. | |
| 78 #endif // defined(RTC_ARCH_CPU_LITTLE_ENDIAN) | |
| 79 #elif defined(WEBRTC_POSIX) | |
| 80 #include <endian.h> | |
| 81 #endif | |
| 82 | |
| 83 namespace rtc { | |
| 84 | |
| 85 // Reading and writing of little and big-endian numbers from memory | |
| 86 | |
| 87 inline void Set8(void* memory, size_t offset, uint8_t v) { | |
| 88 static_cast<uint8_t*>(memory)[offset] = v; | |
| 89 } | |
| 90 | |
| 91 inline uint8_t Get8(const void* memory, size_t offset) { | |
| 92 return static_cast<const uint8_t*>(memory)[offset]; | |
| 93 } | |
| 94 | |
| 95 inline void SetBE16(void* memory, uint16_t v) { | |
| 96 *static_cast<uint16_t*>(memory) = htobe16(v); | |
| 97 } | |
| 98 | |
| 99 inline void SetBE32(void* memory, uint32_t v) { | |
| 100 *static_cast<uint32_t*>(memory) = htobe32(v); | |
| 101 } | |
| 102 | |
| 103 inline void SetBE64(void* memory, uint64_t v) { | |
| 104 *static_cast<uint64_t*>(memory) = htobe64(v); | |
| 105 } | |
| 106 | |
| 107 inline uint16_t GetBE16(const void* memory) { | |
| 108 return be16toh(*static_cast<const uint16_t*>(memory)); | |
| 109 } | |
| 110 | |
| 111 inline uint32_t GetBE32(const void* memory) { | |
| 112 return be32toh(*static_cast<const uint32_t*>(memory)); | |
| 113 } | |
| 114 | |
| 115 inline uint64_t GetBE64(const void* memory) { | |
| 116 return be64toh(*static_cast<const uint64_t*>(memory)); | |
| 117 } | |
| 118 | |
| 119 inline void SetLE16(void* memory, uint16_t v) { | |
| 120 *static_cast<uint16_t*>(memory) = htole16(v); | |
| 121 } | |
| 122 | |
| 123 inline void SetLE32(void* memory, uint32_t v) { | |
| 124 *static_cast<uint32_t*>(memory) = htole32(v); | |
| 125 } | |
| 126 | |
| 127 inline void SetLE64(void* memory, uint64_t v) { | |
| 128 *static_cast<uint64_t*>(memory) = htole64(v); | |
| 129 } | |
| 130 | |
| 131 inline uint16_t GetLE16(const void* memory) { | |
| 132 return le16toh(*static_cast<const uint16_t*>(memory)); | |
| 133 } | |
| 134 | |
| 135 inline uint32_t GetLE32(const void* memory) { | |
| 136 return le32toh(*static_cast<const uint32_t*>(memory)); | |
| 137 } | |
| 138 | |
| 139 inline uint64_t GetLE64(const void* memory) { | |
| 140 return le64toh(*static_cast<const uint64_t*>(memory)); | |
| 141 } | |
| 142 | |
| 143 // Check if the current host is big endian. | |
| 144 inline bool IsHostBigEndian() { | |
| 145 #if defined(RTC_ARCH_CPU_BIG_ENDIAN) | |
| 146 return true; | |
| 147 #else | |
| 148 return false; | |
| 149 #endif | |
| 150 } | |
| 151 | |
| 152 inline uint16_t HostToNetwork16(uint16_t n) { | |
| 153 return htobe16(n); | |
| 154 } | |
| 155 | |
| 156 inline uint32_t HostToNetwork32(uint32_t n) { | |
| 157 return htobe32(n); | |
| 158 } | |
| 159 | |
| 160 inline uint64_t HostToNetwork64(uint64_t n) { | |
| 161 return htobe64(n); | |
| 162 } | |
| 163 | |
| 164 inline uint16_t NetworkToHost16(uint16_t n) { | |
| 165 return be16toh(n); | |
| 166 } | |
| 167 | |
| 168 inline uint32_t NetworkToHost32(uint32_t n) { | |
| 169 return be32toh(n); | |
| 170 } | |
| 171 | |
| 172 inline uint64_t NetworkToHost64(uint64_t n) { | |
| 173 return be64toh(n); | |
| 174 } | |
| 175 | |
| 176 } // namespace rtc | |
| 177 | 18 |
| 178 #endif // WEBRTC_BASE_BYTEORDER_H_ | 19 #endif // WEBRTC_BASE_BYTEORDER_H_ |
| OLD | NEW |