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