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 | |
| 11 | #ifndef WEBRTC_BASE_BYTEORDER_H_ |
| 12 | #define WEBRTC_BASE_BYTEORDER_H_ |
| 13 | |
| 14 | #if defined(WEBRTC_POSIX) && !defined(__native_client__) |
| 15 | #include <arpa/inet.h> |
| 16 | #endif |
| 17 | |
jbauch | 38fd44e | 2017-03-16 10:15:12 -0700 | [diff] [blame^] | 18 | #include "webrtc/base/basictypes.h" |
| 19 | |
| 20 | #if defined(WEBRTC_MAC) |
| 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_POSIX) |
| 37 | #include <endian.h> |
| 38 | #elif defined(WEBRTC_WIN) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 39 | #include <stdlib.h> |
jbauch | 38fd44e | 2017-03-16 10:15:12 -0700 | [diff] [blame^] | 40 | #include <winsock2.h> |
| 41 | |
| 42 | #define htobe16(v) htons(v) |
| 43 | #define htobe32(v) htonl(v) |
| 44 | #define htobe64(v) htonll(v) |
| 45 | #define be16toh(v) ntohs(v) |
| 46 | #define be32toh(v) ntohl(v) |
| 47 | #define be64toh(v) ntohll(v) |
| 48 | |
| 49 | #if defined(RTC_ARCH_CPU_LITTLE_ENDIAN) |
| 50 | #define htole16(v) (v) |
| 51 | #define htole32(v) (v) |
| 52 | #define htole64(v) (v) |
| 53 | #define le16toh(v) (v) |
| 54 | #define le32toh(v) (v) |
| 55 | #define le64toh(v) (v) |
| 56 | #elif defined(RTC_ARCH_CPU_BIG_ENDIAN) |
| 57 | #define htole16(v) __builtin_bswap16(v) |
| 58 | #define htole32(v) __builtin_bswap32(v) |
| 59 | #define htole64(v) __builtin_bswap64(v) |
| 60 | #define le16toh(v) __builtin_bswap16(v) |
| 61 | #define le32toh(v) __builtin_bswap32(v) |
| 62 | #define le64toh(v) __builtin_bswap64(v) |
| 63 | #else |
| 64 | #error RTC_ARCH_CPU_BIG_ENDIAN or RTC_ARCH_CPU_LITTLE_ENDIAN must be defined. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 65 | #endif |
| 66 | |
jbauch | 38fd44e | 2017-03-16 10:15:12 -0700 | [diff] [blame^] | 67 | #endif // defined(WEBRTC_WIN) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 68 | |
| 69 | namespace rtc { |
| 70 | |
| 71 | // Reading and writing of little and big-endian numbers from memory |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 72 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 73 | inline void Set8(void* memory, size_t offset, uint8_t v) { |
| 74 | static_cast<uint8_t*>(memory)[offset] = v; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 77 | inline uint8_t Get8(const void* memory, size_t offset) { |
| 78 | return static_cast<const uint8_t*>(memory)[offset]; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 81 | inline void SetBE16(void* memory, uint16_t v) { |
jbauch | 38fd44e | 2017-03-16 10:15:12 -0700 | [diff] [blame^] | 82 | *static_cast<uint16_t*>(memory) = htobe16(v); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 85 | inline void SetBE32(void* memory, uint32_t v) { |
jbauch | 38fd44e | 2017-03-16 10:15:12 -0700 | [diff] [blame^] | 86 | *static_cast<uint32_t*>(memory) = htobe32(v); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 89 | inline void SetBE64(void* memory, uint64_t v) { |
jbauch | 38fd44e | 2017-03-16 10:15:12 -0700 | [diff] [blame^] | 90 | *static_cast<uint64_t*>(memory) = htobe64(v); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 93 | inline uint16_t GetBE16(const void* memory) { |
jbauch | 38fd44e | 2017-03-16 10:15:12 -0700 | [diff] [blame^] | 94 | return be16toh(*static_cast<const uint16_t*>(memory)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 97 | inline uint32_t GetBE32(const void* memory) { |
jbauch | 38fd44e | 2017-03-16 10:15:12 -0700 | [diff] [blame^] | 98 | return be32toh(*static_cast<const uint32_t*>(memory)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 101 | inline uint64_t GetBE64(const void* memory) { |
jbauch | 38fd44e | 2017-03-16 10:15:12 -0700 | [diff] [blame^] | 102 | return be64toh(*static_cast<const uint64_t*>(memory)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 105 | inline void SetLE16(void* memory, uint16_t v) { |
jbauch | 38fd44e | 2017-03-16 10:15:12 -0700 | [diff] [blame^] | 106 | *static_cast<uint16_t*>(memory) = htole16(v); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 109 | inline void SetLE32(void* memory, uint32_t v) { |
jbauch | 38fd44e | 2017-03-16 10:15:12 -0700 | [diff] [blame^] | 110 | *static_cast<uint32_t*>(memory) = htole32(v); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 113 | inline void SetLE64(void* memory, uint64_t v) { |
jbauch | 38fd44e | 2017-03-16 10:15:12 -0700 | [diff] [blame^] | 114 | *static_cast<uint64_t*>(memory) = htole64(v); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 117 | inline uint16_t GetLE16(const void* memory) { |
jbauch | 38fd44e | 2017-03-16 10:15:12 -0700 | [diff] [blame^] | 118 | return le16toh(*static_cast<const uint16_t*>(memory)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 121 | inline uint32_t GetLE32(const void* memory) { |
jbauch | 38fd44e | 2017-03-16 10:15:12 -0700 | [diff] [blame^] | 122 | return le32toh(*static_cast<const uint32_t*>(memory)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 125 | inline uint64_t GetLE64(const void* memory) { |
jbauch | 38fd44e | 2017-03-16 10:15:12 -0700 | [diff] [blame^] | 126 | return le64toh(*static_cast<const uint64_t*>(memory)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | // Check if the current host is big endian. |
| 130 | inline bool IsHostBigEndian() { |
jbauch | 38fd44e | 2017-03-16 10:15:12 -0700 | [diff] [blame^] | 131 | #if defined(RTC_ARCH_CPU_BIG_ENDIAN) |
| 132 | return true; |
| 133 | #else |
| 134 | return false; |
| 135 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 138 | inline uint16_t HostToNetwork16(uint16_t n) { |
jbauch | 38fd44e | 2017-03-16 10:15:12 -0700 | [diff] [blame^] | 139 | return htobe16(n); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 142 | inline uint32_t HostToNetwork32(uint32_t n) { |
jbauch | 38fd44e | 2017-03-16 10:15:12 -0700 | [diff] [blame^] | 143 | return htobe32(n); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 146 | inline uint64_t HostToNetwork64(uint64_t n) { |
jbauch | 38fd44e | 2017-03-16 10:15:12 -0700 | [diff] [blame^] | 147 | return htobe64(n); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 150 | inline uint16_t NetworkToHost16(uint16_t n) { |
jbauch | 38fd44e | 2017-03-16 10:15:12 -0700 | [diff] [blame^] | 151 | return be16toh(n); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 154 | inline uint32_t NetworkToHost32(uint32_t n) { |
jbauch | 38fd44e | 2017-03-16 10:15:12 -0700 | [diff] [blame^] | 155 | return be32toh(n); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 158 | inline uint64_t NetworkToHost64(uint64_t n) { |
jbauch | 38fd44e | 2017-03-16 10:15:12 -0700 | [diff] [blame^] | 159 | return be64toh(n); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | } // namespace rtc |
| 163 | |
| 164 | #endif // WEBRTC_BASE_BYTEORDER_H_ |