1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 #ifndef GOOGLE_PROTOBUF_STUBS_PORT_H_
32 #define GOOGLE_PROTOBUF_STUBS_PORT_H_
33
34 #include <assert.h>
35 #include <cstdint>
36 #include <stdlib.h>
37 #include <cstddef>
38 #include <string>
39 #include <string.h>
40
41 #include <google/protobuf/stubs/platform_macros.h>
42
43 #include <google/protobuf/port_def.inc>
44
45 #undef PROTOBUF_LITTLE_ENDIAN
46 #ifdef _WIN32
47 // Assuming windows is always little-endian.
48 // TODO(xiaofeng): The PROTOBUF_LITTLE_ENDIAN is not only used for
49 // optimization but also for correctness. We should define an
50 // different macro to test the big-endian code path in coded_stream.
51 #if !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
52 #define PROTOBUF_LITTLE_ENDIAN 1
53 #endif
54 #if _MSC_VER >= 1300 && !defined(__INTEL_COMPILER)
55 // If MSVC has "/RTCc" set, it will complain about truncating casts at
56 // runtime. This file contains some intentional truncating casts.
57 #pragma runtime_checks("c", off)
58 #endif
59 #else
60 #include <sys/param.h> // __BYTE_ORDER
61 #if defined(__OpenBSD__)
62 #include <endian.h>
63 #endif
64 #if ((defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) || \
65 (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN) || \
66 (defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN)) && \
67 !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
68 #define PROTOBUF_LITTLE_ENDIAN 1
69 #endif
70 #endif
71
72 // These #includes are for the byte swap functions declared later on.
73 #ifdef _MSC_VER
74 #include <stdlib.h> // NOLINT(build/include)
75 #include <intrin.h>
76 #elif defined(__APPLE__)
77 #include <libkern/OSByteOrder.h>
78 #elif defined(__GLIBC__) || defined(__BIONIC__) || defined(__CYGWIN__)
79 #include <byteswap.h> // IWYU pragma: export
80 #endif
81
82 // Legacy: some users reference these (internal-only) macros even though we
83 // don't need them any more.
84 #if defined(_MSC_VER) && defined(PROTOBUF_USE_DLLS)
85 #ifdef LIBPROTOBUF_EXPORTS
86 #define LIBPROTOBUF_EXPORT __declspec(dllexport)
87 #else
88 #define LIBPROTOBUF_EXPORT __declspec(dllimport)
89 #endif
90 #ifdef LIBPROTOC_EXPORTS
91 #define LIBPROTOC_EXPORT __declspec(dllexport)
92 #else
93 #define LIBPROTOC_EXPORT __declspec(dllimport)
94 #endif
95 #else
96 #define LIBPROTOBUF_EXPORT
97 #define LIBPROTOC_EXPORT
98 #endif
99
100 #define PROTOBUF_RUNTIME_DEPRECATED(message) PROTOBUF_DEPRECATED_MSG(message)
101 #define GOOGLE_PROTOBUF_RUNTIME_DEPRECATED(message) \
102 PROTOBUF_DEPRECATED_MSG(message)
103
104 // ===================================================================
105 // from google3/base/port.h
106
107 #if (defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L || \
108 (defined(_MSC_VER) && _MSC_VER >= 1900))
109 // Define this to 1 if the code is compiled in C++11 mode; leave it
110 // undefined otherwise. Do NOT define it to 0 -- that causes
111 // '#ifdef LANG_CXX11' to behave differently from '#if LANG_CXX11'.
112 #define LANG_CXX11 1
113 #else
114 #error "Protobuf requires at least C++11."
115 #endif
116
117 namespace google {
118 namespace protobuf {
119
120 using ConstStringParam = const std::string &;
121
122 typedef unsigned int uint;
123
124 typedef int8_t int8;
125 typedef int16_t int16;
126 typedef int32_t int32;
127 typedef int64_t int64;
128
129 typedef uint8_t uint8;
130 typedef uint16_t uint16;
131 typedef uint32_t uint32;
132 typedef uint64_t uint64;
133
134 static const int32 kint32max = 0x7FFFFFFF;
135 static const int32 kint32min = -kint32max - 1;
136 static const int64 kint64max = PROTOBUF_LONGLONG(0x7FFFFFFFFFFFFFFF);
137 static const int64 kint64min = -kint64max - 1;
138 static const uint32 kuint32max = 0xFFFFFFFFu;
139 static const uint64 kuint64max = PROTOBUF_ULONGLONG(0xFFFFFFFFFFFFFFFF);
140
141 #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) ||\
142 defined(MEMORY_SANITIZER)
143
144 #ifdef __cplusplus
145 extern "C" {
146 #endif // __cplusplus
147 uint16_t __sanitizer_unaligned_load16(const void *p);
148 uint32_t __sanitizer_unaligned_load32(const void *p);
149 uint64_t __sanitizer_unaligned_load64(const void *p);
150 void __sanitizer_unaligned_store16(void *p, uint16_t v);
151 void __sanitizer_unaligned_store32(void *p, uint32_t v);
152 void __sanitizer_unaligned_store64(void *p, uint64_t v);
153 #ifdef __cplusplus
154 } // extern "C"
155 #endif // __cplusplus
156
GOOGLE_UNALIGNED_LOAD16(const void * p)157 inline uint16 GOOGLE_UNALIGNED_LOAD16(const void *p) {
158 return __sanitizer_unaligned_load16(p);
159 }
160
GOOGLE_UNALIGNED_LOAD32(const void * p)161 inline uint32 GOOGLE_UNALIGNED_LOAD32(const void *p) {
162 return __sanitizer_unaligned_load32(p);
163 }
164
GOOGLE_UNALIGNED_LOAD64(const void * p)165 inline uint64 GOOGLE_UNALIGNED_LOAD64(const void *p) {
166 return __sanitizer_unaligned_load64(p);
167 }
168
GOOGLE_UNALIGNED_STORE16(void * p,uint16 v)169 inline void GOOGLE_UNALIGNED_STORE16(void *p, uint16 v) {
170 __sanitizer_unaligned_store16(p, v);
171 }
172
GOOGLE_UNALIGNED_STORE32(void * p,uint32 v)173 inline void GOOGLE_UNALIGNED_STORE32(void *p, uint32 v) {
174 __sanitizer_unaligned_store32(p, v);
175 }
176
GOOGLE_UNALIGNED_STORE64(void * p,uint64 v)177 inline void GOOGLE_UNALIGNED_STORE64(void *p, uint64 v) {
178 __sanitizer_unaligned_store64(p, v);
179 }
180
181 #elif defined(GOOGLE_PROTOBUF_USE_UNALIGNED) && GOOGLE_PROTOBUF_USE_UNALIGNED
182
183 #define GOOGLE_UNALIGNED_LOAD16(_p) (*reinterpret_cast<const uint16 *>(_p))
184 #define GOOGLE_UNALIGNED_LOAD32(_p) (*reinterpret_cast<const uint32 *>(_p))
185 #define GOOGLE_UNALIGNED_LOAD64(_p) (*reinterpret_cast<const uint64 *>(_p))
186
187 #define GOOGLE_UNALIGNED_STORE16(_p, _val) (*reinterpret_cast<uint16 *>(_p) = (_val))
188 #define GOOGLE_UNALIGNED_STORE32(_p, _val) (*reinterpret_cast<uint32 *>(_p) = (_val))
189 #define GOOGLE_UNALIGNED_STORE64(_p, _val) (*reinterpret_cast<uint64 *>(_p) = (_val))
190
191 #else
GOOGLE_UNALIGNED_LOAD16(const void * p)192 inline uint16 GOOGLE_UNALIGNED_LOAD16(const void *p) {
193 uint16 t;
194 memcpy(&t, p, sizeof t);
195 return t;
196 }
197
GOOGLE_UNALIGNED_LOAD32(const void * p)198 inline uint32 GOOGLE_UNALIGNED_LOAD32(const void *p) {
199 uint32 t;
200 memcpy(&t, p, sizeof t);
201 return t;
202 }
203
GOOGLE_UNALIGNED_LOAD64(const void * p)204 inline uint64 GOOGLE_UNALIGNED_LOAD64(const void *p) {
205 uint64 t;
206 memcpy(&t, p, sizeof t);
207 return t;
208 }
209
GOOGLE_UNALIGNED_STORE16(void * p,uint16 v)210 inline void GOOGLE_UNALIGNED_STORE16(void *p, uint16 v) {
211 memcpy(p, &v, sizeof v);
212 }
213
GOOGLE_UNALIGNED_STORE32(void * p,uint32 v)214 inline void GOOGLE_UNALIGNED_STORE32(void *p, uint32 v) {
215 memcpy(p, &v, sizeof v);
216 }
217
GOOGLE_UNALIGNED_STORE64(void * p,uint64 v)218 inline void GOOGLE_UNALIGNED_STORE64(void *p, uint64 v) {
219 memcpy(p, &v, sizeof v);
220 }
221 #endif
222
223 #if defined(GOOGLE_PROTOBUF_OS_NACL) \
224 || (defined(__ANDROID__) && defined(__clang__) \
225 && (__clang_major__ == 3 && __clang_minor__ == 8) \
226 && (__clang_patchlevel__ < 275480))
227 # define GOOGLE_PROTOBUF_USE_PORTABLE_LOG2
228 #endif
229
230 // The following guarantees declaration of the byte swap functions.
231 #ifdef _MSC_VER
232 #define bswap_16(x) _byteswap_ushort(x)
233 #define bswap_32(x) _byteswap_ulong(x)
234 #define bswap_64(x) _byteswap_uint64(x)
235
236 #elif defined(__APPLE__)
237 // Mac OS X / Darwin features
238 #define bswap_16(x) OSSwapInt16(x)
239 #define bswap_32(x) OSSwapInt32(x)
240 #define bswap_64(x) OSSwapInt64(x)
241
242 #elif !defined(__GLIBC__) && !defined(__BIONIC__) && !defined(__CYGWIN__)
243
244 #ifndef bswap_16
bswap_16(uint16 x)245 static inline uint16 bswap_16(uint16 x) {
246 return static_cast<uint16>(((x & 0xFF) << 8) | ((x & 0xFF00) >> 8));
247 }
248 #define bswap_16(x) bswap_16(x)
249 #endif
250
251 #ifndef bswap_32
bswap_32(uint32 x)252 static inline uint32 bswap_32(uint32 x) {
253 return (((x & 0xFF) << 24) |
254 ((x & 0xFF00) << 8) |
255 ((x & 0xFF0000) >> 8) |
256 ((x & 0xFF000000) >> 24));
257 }
258 #define bswap_32(x) bswap_32(x)
259 #endif
260
261 #ifndef bswap_64
bswap_64(uint64 x)262 static inline uint64 bswap_64(uint64 x) {
263 return (((x & PROTOBUF_ULONGLONG(0xFF)) << 56) |
264 ((x & PROTOBUF_ULONGLONG(0xFF00)) << 40) |
265 ((x & PROTOBUF_ULONGLONG(0xFF0000)) << 24) |
266 ((x & PROTOBUF_ULONGLONG(0xFF000000)) << 8) |
267 ((x & PROTOBUF_ULONGLONG(0xFF00000000)) >> 8) |
268 ((x & PROTOBUF_ULONGLONG(0xFF0000000000)) >> 24) |
269 ((x & PROTOBUF_ULONGLONG(0xFF000000000000)) >> 40) |
270 ((x & PROTOBUF_ULONGLONG(0xFF00000000000000)) >> 56));
271 }
272 #define bswap_64(x) bswap_64(x)
273 #endif
274
275 #endif
276
277 // ===================================================================
278 // from google3/util/bits/bits.h
279
280 class Bits {
281 public:
Log2FloorNonZero(uint32 n)282 static uint32 Log2FloorNonZero(uint32 n) {
283 #if defined(__GNUC__)
284 return 31 ^ static_cast<uint32>(__builtin_clz(n));
285 #elif defined(_MSC_VER)
286 unsigned long where;
287 _BitScanReverse(&where, n);
288 return where;
289 #else
290 return Log2FloorNonZero_Portable(n);
291 #endif
292 }
293
Log2FloorNonZero64(uint64 n)294 static uint32 Log2FloorNonZero64(uint64 n) {
295 // Older versions of clang run into an instruction-selection failure when
296 // it encounters __builtin_clzll:
297 // https://bugs.chromium.org/p/nativeclient/issues/detail?id=4395
298 // This includes arm-nacl-clang and clang in older Android NDK versions.
299 // To work around this, when we build with those we use the portable
300 // implementation instead.
301 #if defined(__GNUC__) && !defined(GOOGLE_PROTOBUF_USE_PORTABLE_LOG2)
302 return 63 ^ static_cast<uint32>(__builtin_clzll(n));
303 #elif defined(_MSC_VER) && defined(_M_X64)
304 unsigned long where;
305 _BitScanReverse64(&where, n);
306 return where;
307 #else
308 return Log2FloorNonZero64_Portable(n);
309 #endif
310 }
311 private:
Log2FloorNonZero_Portable(uint32 n)312 static int Log2FloorNonZero_Portable(uint32 n) {
313 if (n == 0)
314 return -1;
315 int log = 0;
316 uint32 value = n;
317 for (int i = 4; i >= 0; --i) {
318 int shift = (1 << i);
319 uint32 x = value >> shift;
320 if (x != 0) {
321 value = x;
322 log += shift;
323 }
324 }
325 assert(value == 1);
326 return log;
327 }
328
Log2FloorNonZero64_Portable(uint64 n)329 static int Log2FloorNonZero64_Portable(uint64 n) {
330 const uint32 topbits = static_cast<uint32>(n >> 32);
331 if (topbits == 0) {
332 // Top bits are zero, so scan in bottom bits
333 return static_cast<int>(Log2FloorNonZero(static_cast<uint32>(n)));
334 } else {
335 return 32 + static_cast<int>(Log2FloorNonZero(topbits));
336 }
337 }
338 };
339
340 // ===================================================================
341 // from google3/util/endian/endian.h
342 PROTOBUF_EXPORT uint32 ghtonl(uint32 x);
343
344 class BigEndian {
345 public:
346 #ifdef PROTOBUF_LITTLE_ENDIAN
347
FromHost16(uint16 x)348 static uint16 FromHost16(uint16 x) { return bswap_16(x); }
ToHost16(uint16 x)349 static uint16 ToHost16(uint16 x) { return bswap_16(x); }
350
FromHost32(uint32 x)351 static uint32 FromHost32(uint32 x) { return bswap_32(x); }
ToHost32(uint32 x)352 static uint32 ToHost32(uint32 x) { return bswap_32(x); }
353
FromHost64(uint64 x)354 static uint64 FromHost64(uint64 x) { return bswap_64(x); }
ToHost64(uint64 x)355 static uint64 ToHost64(uint64 x) { return bswap_64(x); }
356
IsLittleEndian()357 static bool IsLittleEndian() { return true; }
358
359 #else
360
361 static uint16 FromHost16(uint16 x) { return x; }
362 static uint16 ToHost16(uint16 x) { return x; }
363
364 static uint32 FromHost32(uint32 x) { return x; }
365 static uint32 ToHost32(uint32 x) { return x; }
366
367 static uint64 FromHost64(uint64 x) { return x; }
368 static uint64 ToHost64(uint64 x) { return x; }
369
370 static bool IsLittleEndian() { return false; }
371
372 #endif /* ENDIAN */
373
374 // Functions to do unaligned loads and stores in big-endian order.
Load16(const void * p)375 static uint16 Load16(const void *p) {
376 return ToHost16(GOOGLE_UNALIGNED_LOAD16(p));
377 }
378
Store16(void * p,uint16 v)379 static void Store16(void *p, uint16 v) {
380 GOOGLE_UNALIGNED_STORE16(p, FromHost16(v));
381 }
382
Load32(const void * p)383 static uint32 Load32(const void *p) {
384 return ToHost32(GOOGLE_UNALIGNED_LOAD32(p));
385 }
386
Store32(void * p,uint32 v)387 static void Store32(void *p, uint32 v) {
388 GOOGLE_UNALIGNED_STORE32(p, FromHost32(v));
389 }
390
Load64(const void * p)391 static uint64 Load64(const void *p) {
392 return ToHost64(GOOGLE_UNALIGNED_LOAD64(p));
393 }
394
Store64(void * p,uint64 v)395 static void Store64(void *p, uint64 v) {
396 GOOGLE_UNALIGNED_STORE64(p, FromHost64(v));
397 }
398 };
399
400 } // namespace protobuf
401 } // namespace google
402
403 #include <google/protobuf/port_undef.inc>
404
405 #endif // GOOGLE_PROTOBUF_STUBS_PORT_H_
406