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 | 8f05ab2 | 2017-03-16 10:34:37 -0700 | [diff] [blame] | 18 | #if defined(WEBRTC_WIN) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | #endif |
| 21 | |
kthelgason | 44122bd | 2017-03-17 02:28:28 -0700 | [diff] [blame] | 22 | #include "webrtc/base/basictypes.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 23 | |
| 24 | namespace rtc { |
| 25 | |
| 26 | // Reading and writing of little and big-endian numbers from memory |
kthelgason | 44122bd | 2017-03-17 02:28:28 -0700 | [diff] [blame] | 27 | // TODO: Optimized versions, with direct read/writes of |
| 28 | // integers in host-endian format, when the platform supports it. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 29 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 30 | inline void Set8(void* memory, size_t offset, uint8_t v) { |
| 31 | static_cast<uint8_t*>(memory)[offset] = v; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 34 | inline uint8_t Get8(const void* memory, size_t offset) { |
| 35 | return static_cast<const uint8_t*>(memory)[offset]; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 38 | inline void SetBE16(void* memory, uint16_t v) { |
kthelgason | 44122bd | 2017-03-17 02:28:28 -0700 | [diff] [blame] | 39 | Set8(memory, 0, static_cast<uint8_t>(v >> 8)); |
| 40 | Set8(memory, 1, static_cast<uint8_t>(v >> 0)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 43 | inline void SetBE32(void* memory, uint32_t v) { |
kthelgason | 44122bd | 2017-03-17 02:28:28 -0700 | [diff] [blame] | 44 | Set8(memory, 0, static_cast<uint8_t>(v >> 24)); |
| 45 | Set8(memory, 1, static_cast<uint8_t>(v >> 16)); |
| 46 | Set8(memory, 2, static_cast<uint8_t>(v >> 8)); |
| 47 | Set8(memory, 3, static_cast<uint8_t>(v >> 0)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 50 | inline void SetBE64(void* memory, uint64_t v) { |
kthelgason | 44122bd | 2017-03-17 02:28:28 -0700 | [diff] [blame] | 51 | Set8(memory, 0, static_cast<uint8_t>(v >> 56)); |
| 52 | Set8(memory, 1, static_cast<uint8_t>(v >> 48)); |
| 53 | Set8(memory, 2, static_cast<uint8_t>(v >> 40)); |
| 54 | Set8(memory, 3, static_cast<uint8_t>(v >> 32)); |
| 55 | Set8(memory, 4, static_cast<uint8_t>(v >> 24)); |
| 56 | Set8(memory, 5, static_cast<uint8_t>(v >> 16)); |
| 57 | Set8(memory, 6, static_cast<uint8_t>(v >> 8)); |
| 58 | Set8(memory, 7, static_cast<uint8_t>(v >> 0)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 61 | inline uint16_t GetBE16(const void* memory) { |
kthelgason | 44122bd | 2017-03-17 02:28:28 -0700 | [diff] [blame] | 62 | return static_cast<uint16_t>((Get8(memory, 0) << 8) | (Get8(memory, 1) << 0)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 65 | inline uint32_t GetBE32(const void* memory) { |
kthelgason | 44122bd | 2017-03-17 02:28:28 -0700 | [diff] [blame] | 66 | return (static_cast<uint32_t>(Get8(memory, 0)) << 24) | |
| 67 | (static_cast<uint32_t>(Get8(memory, 1)) << 16) | |
| 68 | (static_cast<uint32_t>(Get8(memory, 2)) << 8) | |
| 69 | (static_cast<uint32_t>(Get8(memory, 3)) << 0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 72 | inline uint64_t GetBE64(const void* memory) { |
kthelgason | 44122bd | 2017-03-17 02:28:28 -0700 | [diff] [blame] | 73 | return (static_cast<uint64_t>(Get8(memory, 0)) << 56) | |
| 74 | (static_cast<uint64_t>(Get8(memory, 1)) << 48) | |
| 75 | (static_cast<uint64_t>(Get8(memory, 2)) << 40) | |
| 76 | (static_cast<uint64_t>(Get8(memory, 3)) << 32) | |
| 77 | (static_cast<uint64_t>(Get8(memory, 4)) << 24) | |
| 78 | (static_cast<uint64_t>(Get8(memory, 5)) << 16) | |
| 79 | (static_cast<uint64_t>(Get8(memory, 6)) << 8) | |
| 80 | (static_cast<uint64_t>(Get8(memory, 7)) << 0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 83 | inline void SetLE16(void* memory, uint16_t v) { |
kthelgason | 44122bd | 2017-03-17 02:28:28 -0700 | [diff] [blame] | 84 | Set8(memory, 0, static_cast<uint8_t>(v >> 0)); |
| 85 | Set8(memory, 1, static_cast<uint8_t>(v >> 8)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 88 | inline void SetLE32(void* memory, uint32_t v) { |
kthelgason | 44122bd | 2017-03-17 02:28:28 -0700 | [diff] [blame] | 89 | Set8(memory, 0, static_cast<uint8_t>(v >> 0)); |
| 90 | Set8(memory, 1, static_cast<uint8_t>(v >> 8)); |
| 91 | Set8(memory, 2, static_cast<uint8_t>(v >> 16)); |
| 92 | Set8(memory, 3, static_cast<uint8_t>(v >> 24)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 95 | inline void SetLE64(void* memory, uint64_t v) { |
kthelgason | 44122bd | 2017-03-17 02:28:28 -0700 | [diff] [blame] | 96 | Set8(memory, 0, static_cast<uint8_t>(v >> 0)); |
| 97 | Set8(memory, 1, static_cast<uint8_t>(v >> 8)); |
| 98 | Set8(memory, 2, static_cast<uint8_t>(v >> 16)); |
| 99 | Set8(memory, 3, static_cast<uint8_t>(v >> 24)); |
| 100 | Set8(memory, 4, static_cast<uint8_t>(v >> 32)); |
| 101 | Set8(memory, 5, static_cast<uint8_t>(v >> 40)); |
| 102 | Set8(memory, 6, static_cast<uint8_t>(v >> 48)); |
| 103 | Set8(memory, 7, static_cast<uint8_t>(v >> 56)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 106 | inline uint16_t GetLE16(const void* memory) { |
kthelgason | 44122bd | 2017-03-17 02:28:28 -0700 | [diff] [blame] | 107 | return static_cast<uint16_t>((Get8(memory, 0) << 0) | (Get8(memory, 1) << 8)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 110 | inline uint32_t GetLE32(const void* memory) { |
kthelgason | 44122bd | 2017-03-17 02:28:28 -0700 | [diff] [blame] | 111 | return (static_cast<uint32_t>(Get8(memory, 0)) << 0) | |
| 112 | (static_cast<uint32_t>(Get8(memory, 1)) << 8) | |
| 113 | (static_cast<uint32_t>(Get8(memory, 2)) << 16) | |
| 114 | (static_cast<uint32_t>(Get8(memory, 3)) << 24); |
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 uint64_t GetLE64(const void* memory) { |
kthelgason | 44122bd | 2017-03-17 02:28:28 -0700 | [diff] [blame] | 118 | return (static_cast<uint64_t>(Get8(memory, 0)) << 0) | |
| 119 | (static_cast<uint64_t>(Get8(memory, 1)) << 8) | |
| 120 | (static_cast<uint64_t>(Get8(memory, 2)) << 16) | |
| 121 | (static_cast<uint64_t>(Get8(memory, 3)) << 24) | |
| 122 | (static_cast<uint64_t>(Get8(memory, 4)) << 32) | |
| 123 | (static_cast<uint64_t>(Get8(memory, 5)) << 40) | |
| 124 | (static_cast<uint64_t>(Get8(memory, 6)) << 48) | |
| 125 | (static_cast<uint64_t>(Get8(memory, 7)) << 56); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // Check if the current host is big endian. |
| 129 | inline bool IsHostBigEndian() { |
kthelgason | 44122bd | 2017-03-17 02:28:28 -0700 | [diff] [blame] | 130 | static const int number = 1; |
| 131 | return 0 == *reinterpret_cast<const char*>(&number); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 134 | inline uint16_t HostToNetwork16(uint16_t n) { |
kthelgason | 44122bd | 2017-03-17 02:28:28 -0700 | [diff] [blame] | 135 | uint16_t result; |
| 136 | SetBE16(&result, n); |
| 137 | return result; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 140 | inline uint32_t HostToNetwork32(uint32_t n) { |
kthelgason | 44122bd | 2017-03-17 02:28:28 -0700 | [diff] [blame] | 141 | uint32_t result; |
| 142 | SetBE32(&result, n); |
| 143 | return result; |
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) { |
kthelgason | 44122bd | 2017-03-17 02:28:28 -0700 | [diff] [blame] | 147 | uint64_t result; |
| 148 | SetBE64(&result, n); |
| 149 | return result; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 152 | inline uint16_t NetworkToHost16(uint16_t n) { |
kthelgason | 44122bd | 2017-03-17 02:28:28 -0700 | [diff] [blame] | 153 | return GetBE16(&n); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 156 | inline uint32_t NetworkToHost32(uint32_t n) { |
kthelgason | 44122bd | 2017-03-17 02:28:28 -0700 | [diff] [blame] | 157 | return GetBE32(&n); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 160 | inline uint64_t NetworkToHost64(uint64_t n) { |
kthelgason | 44122bd | 2017-03-17 02:28:28 -0700 | [diff] [blame] | 161 | return GetBE64(&n); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | } // namespace rtc |
| 165 | |
| 166 | #endif // WEBRTC_BASE_BYTEORDER_H_ |