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 <stdlib.h>
36 #include <cstddef>
37 #include <string>
38 #include <string.h>
39 #if defined(__osf__)
40 // Tru64 lacks stdint.h, but has inttypes.h which defines a superset of
41 // what stdint.h would define.
42 #include <inttypes.h>
43 #elif !defined(_MSC_VER)
44 #include <stdint.h>
45 #endif
46
47 #include <google/protobuf/stubs/platform_macros.h>
48
49 #undef PROTOBUF_LITTLE_ENDIAN
50 #ifdef _WIN32
51 // Assuming windows is always little-endian.
52 // TODO(xiaofeng): The PROTOBUF_LITTLE_ENDIAN is not only used for
53 // optimization but also for correctness. We should define an
54 // different macro to test the big-endian code path in coded_stream.
55 #if !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
56 #define PROTOBUF_LITTLE_ENDIAN 1
57 #endif
58 #if _MSC_VER >= 1300 && !defined(__INTEL_COMPILER)
59 // If MSVC has "/RTCc" set, it will complain about truncating casts at
60 // runtime. This file contains some intentional truncating casts.
61 #pragma runtime_checks("c", off)
62 #endif
63 #else
64 #include <sys/param.h> // __BYTE_ORDER
65 #if defined(__OpenBSD__)
66 #include <endian.h>
67 #endif
68 #if ((defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) || \
69 (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN) || \
70 (defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN)) && \
71 !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
72 #define PROTOBUF_LITTLE_ENDIAN 1
73 #endif
74 #endif
75 #if defined(_MSC_VER) && defined(PROTOBUF_USE_DLLS)
76 #ifdef LIBPROTOBUF_EXPORTS
77 #define LIBPROTOBUF_EXPORT __declspec(dllexport)
78 #else
79 #define LIBPROTOBUF_EXPORT __declspec(dllimport)
80 #endif
81 #ifdef LIBPROTOC_EXPORTS
82 #define LIBPROTOC_EXPORT __declspec(dllexport)
83 #else
84 #define LIBPROTOC_EXPORT __declspec(dllimport)
85 #endif
86 #else
87 #define LIBPROTOBUF_EXPORT
88 #define LIBPROTOC_EXPORT
89 #endif
90
91 // These #includes are for the byte swap functions declared later on.
92 #ifdef _MSC_VER
93 #include <stdlib.h> // NOLINT(build/include)
94 #elif defined(__APPLE__)
95 #include <libkern/OSByteOrder.h>
96 #elif defined(__GLIBC__) || defined(__CYGWIN__)
97 #include <byteswap.h> // IWYU pragma: export
98 #endif
99
100 #define PROTOBUF_RUNTIME_DEPRECATED(message)
101
102 // ===================================================================
103 // from google3/base/port.h
104
105 #if (defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L || \
106 (defined(_MSC_VER) && _MSC_VER >= 1900))
107 // Define this to 1 if the code is compiled in C++11 mode; leave it
108 // undefined otherwise. Do NOT define it to 0 -- that causes
109 // '#ifdef LANG_CXX11' to behave differently from '#if LANG_CXX11'.
110 #define LANG_CXX11 1
111 #endif
112
113 #if LANG_CXX11 && !defined(__NVCC__)
114 #define PROTOBUF_CXX11 1
115 #else
116 #define PROTOBUF_CXX11 0
117 #endif
118
119 #if PROTOBUF_CXX11
120 #define PROTOBUF_FINAL final
121 #else
122 #define PROTOBUF_FINAL
123 #endif
124
125 namespace google {
126 namespace protobuf {
127
128 typedef unsigned int uint;
129
130 #ifdef _MSC_VER
131 typedef signed __int8 int8;
132 typedef __int16 int16;
133 typedef __int32 int32;
134 typedef __int64 int64;
135
136 typedef unsigned __int8 uint8;
137 typedef unsigned __int16 uint16;
138 typedef unsigned __int32 uint32;
139 typedef unsigned __int64 uint64;
140 #else
141 typedef int8_t int8;
142 typedef int16_t int16;
143 typedef int32_t int32;
144 typedef int64_t int64;
145
146 typedef uint8_t uint8;
147 typedef uint16_t uint16;
148 typedef uint32_t uint32;
149 typedef uint64_t uint64;
150 #endif
151
152 // long long macros to be used because gcc and vc++ use different suffixes,
153 // and different size specifiers in format strings
154 #undef GOOGLE_LONGLONG
155 #undef GOOGLE_ULONGLONG
156 #undef GOOGLE_LL_FORMAT
157
158 #ifdef _MSC_VER
159 #define GOOGLE_LONGLONG(x) x##I64
160 #define GOOGLE_ULONGLONG(x) x##UI64
161 #define GOOGLE_LL_FORMAT "I64" // As in printf("%I64d", ...)
162 #else
163 // By long long, we actually mean int64.
164 #define GOOGLE_LONGLONG(x) x##LL
165 #define GOOGLE_ULONGLONG(x) x##ULL
166 // Used to format real long long integers.
167 #define GOOGLE_LL_FORMAT "ll" // As in "%lld". Note that "q" is poor form also.
168 #endif
169
170 static const int32 kint32max = 0x7FFFFFFF;
171 static const int32 kint32min = -kint32max - 1;
172 static const int64 kint64max = GOOGLE_LONGLONG(0x7FFFFFFFFFFFFFFF);
173 static const int64 kint64min = -kint64max - 1;
174 static const uint32 kuint32max = 0xFFFFFFFFu;
175 static const uint64 kuint64max = GOOGLE_ULONGLONG(0xFFFFFFFFFFFFFFFF);
176
177 // -------------------------------------------------------------------
178 // Annotations: Some parts of the code have been annotated in ways that might
179 // be useful to some compilers or tools, but are not supported universally.
180 // You can #define these annotations yourself if the default implementation
181 // is not right for you.
182
183 #ifndef GOOGLE_ATTRIBUTE_ALWAYS_INLINE
184 #if defined(__GNUC__) && (__GNUC__ > 3 ||(__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
185 // For functions we want to force inline.
186 // Introduced in gcc 3.1.
187 #define GOOGLE_ATTRIBUTE_ALWAYS_INLINE __attribute__ ((always_inline))
188 #else
189 // Other compilers will have to figure it out for themselves.
190 #define GOOGLE_ATTRIBUTE_ALWAYS_INLINE
191 #endif
192 #endif
193
194 #define GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE GOOGLE_ATTRIBUTE_ALWAYS_INLINE
195
196 #ifndef GOOGLE_ATTRIBUTE_NOINLINE
197 #if defined(__GNUC__) && (__GNUC__ > 3 ||(__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
198 // For functions we want to force not inline.
199 // Introduced in gcc 3.1.
200 #define GOOGLE_ATTRIBUTE_NOINLINE __attribute__ ((noinline))
201 #elif defined(_MSC_VER) && (_MSC_VER >= 1400)
202 // Seems to have been around since at least Visual Studio 2005
203 #define GOOGLE_ATTRIBUTE_NOINLINE __declspec(noinline)
204 #else
205 // Other compilers will have to figure it out for themselves.
206 #define GOOGLE_ATTRIBUTE_NOINLINE
207 #endif
208 #endif
209
210 #define GOOGLE_PROTOBUF_ATTRIBUTE_NOINLINE GOOGLE_ATTRIBUTE_NOINLINE
211
212 #ifndef GOOGLE_PREDICT_TRUE
213 #ifdef __GNUC__
214 // Provided at least since GCC 3.0.
215 #define GOOGLE_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
216 #else
217 #define GOOGLE_PREDICT_TRUE(x) (x)
218 #endif
219 #endif
220
221 #ifndef GOOGLE_PREDICT_FALSE
222 #ifdef __GNUC__
223 // Provided at least since GCC 3.0.
224 #define GOOGLE_PREDICT_FALSE(x) (__builtin_expect(x, 0))
225 #else
226 #define GOOGLE_PREDICT_FALSE(x) (x)
227 #endif
228 #endif
229
230 // Delimits a block of code which may write to memory which is simultaneously
231 // written by other threads, but which has been determined to be thread-safe
232 // (e.g. because it is an idempotent write).
233 #ifndef GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN
234 #define GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN()
235 #endif
236 #ifndef GOOGLE_SAFE_CONCURRENT_WRITES_END
237 #define GOOGLE_SAFE_CONCURRENT_WRITES_END()
238 #endif
239
240 #if defined(__clang__) && defined(__has_cpp_attribute) \
241 && !defined(GOOGLE_PROTOBUF_OS_APPLE)
242 # if defined(GOOGLE_PROTOBUF_OS_NACL) || __has_cpp_attribute(clang::fallthrough)
243 # define GOOGLE_FALLTHROUGH_INTENDED [[clang::fallthrough]]
244 # endif
245 #elif defined(__GNUC__) && __GNUC__ > 6
246 # define GOOGLE_FALLTHROUGH_INTENDED [[gnu::fallthrough]]
247 #endif
248
249 #ifndef GOOGLE_FALLTHROUGH_INTENDED
250 # define GOOGLE_FALLTHROUGH_INTENDED
251 #endif
252
253 #define GOOGLE_GUARDED_BY(x)
254 #define GOOGLE_ATTRIBUTE_COLD
255
256 #ifdef GOOGLE_PROTOBUF_DONT_USE_UNALIGNED
257 # define GOOGLE_PROTOBUF_USE_UNALIGNED 0
258 #else
259 # if defined(_M_X64) || defined(__x86_64__) || defined(_M_IX86) || defined(__i386__)
260 # define GOOGLE_PROTOBUF_USE_UNALIGNED 1
261 # else
262 # define GOOGLE_PROTOBUF_USE_UNALIGNED 0
263 # endif
264 #endif
265
266 #define GOOGLE_PROTOBUF_ATTRIBUTE_COLD GOOGLE_ATTRIBUTE_COLD
267
268 #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) ||\
269 defined(MEMORY_SANITIZER)
270
271 #ifdef __cplusplus
272 extern "C" {
273 #endif // __cplusplus
274 uint16_t __sanitizer_unaligned_load16(const void *p);
275 uint32_t __sanitizer_unaligned_load32(const void *p);
276 uint64_t __sanitizer_unaligned_load64(const void *p);
277 void __sanitizer_unaligned_store16(void *p, uint16_t v);
278 void __sanitizer_unaligned_store32(void *p, uint32_t v);
279 void __sanitizer_unaligned_store64(void *p, uint64_t v);
280 #ifdef __cplusplus
281 } // extern "C"
282 #endif // __cplusplus
283
GOOGLE_UNALIGNED_LOAD16(const void * p)284 inline uint16 GOOGLE_UNALIGNED_LOAD16(const void *p) {
285 return __sanitizer_unaligned_load16(p);
286 }
287
GOOGLE_UNALIGNED_LOAD32(const void * p)288 inline uint32 GOOGLE_UNALIGNED_LOAD32(const void *p) {
289 return __sanitizer_unaligned_load32(p);
290 }
291
GOOGLE_UNALIGNED_LOAD64(const void * p)292 inline uint64 GOOGLE_UNALIGNED_LOAD64(const void *p) {
293 return __sanitizer_unaligned_load64(p);
294 }
295
GOOGLE_UNALIGNED_STORE16(void * p,uint16 v)296 inline void GOOGLE_UNALIGNED_STORE16(void *p, uint16 v) {
297 __sanitizer_unaligned_store16(p, v);
298 }
299
GOOGLE_UNALIGNED_STORE32(void * p,uint32 v)300 inline void GOOGLE_UNALIGNED_STORE32(void *p, uint32 v) {
301 __sanitizer_unaligned_store32(p, v);
302 }
303
GOOGLE_UNALIGNED_STORE64(void * p,uint64 v)304 inline void GOOGLE_UNALIGNED_STORE64(void *p, uint64 v) {
305 __sanitizer_unaligned_store64(p, v);
306 }
307
308 #elif GOOGLE_PROTOBUF_USE_UNALIGNED
309
310 #define GOOGLE_UNALIGNED_LOAD16(_p) (*reinterpret_cast<const uint16 *>(_p))
311 #define GOOGLE_UNALIGNED_LOAD32(_p) (*reinterpret_cast<const uint32 *>(_p))
312 #define GOOGLE_UNALIGNED_LOAD64(_p) (*reinterpret_cast<const uint64 *>(_p))
313
314 #define GOOGLE_UNALIGNED_STORE16(_p, _val) (*reinterpret_cast<uint16 *>(_p) = (_val))
315 #define GOOGLE_UNALIGNED_STORE32(_p, _val) (*reinterpret_cast<uint32 *>(_p) = (_val))
316 #define GOOGLE_UNALIGNED_STORE64(_p, _val) (*reinterpret_cast<uint64 *>(_p) = (_val))
317
318 #else
GOOGLE_UNALIGNED_LOAD16(const void * p)319 inline uint16 GOOGLE_UNALIGNED_LOAD16(const void *p) {
320 uint16 t;
321 memcpy(&t, p, sizeof t);
322 return t;
323 }
324
GOOGLE_UNALIGNED_LOAD32(const void * p)325 inline uint32 GOOGLE_UNALIGNED_LOAD32(const void *p) {
326 uint32 t;
327 memcpy(&t, p, sizeof t);
328 return t;
329 }
330
GOOGLE_UNALIGNED_LOAD64(const void * p)331 inline uint64 GOOGLE_UNALIGNED_LOAD64(const void *p) {
332 uint64 t;
333 memcpy(&t, p, sizeof t);
334 return t;
335 }
336
GOOGLE_UNALIGNED_STORE16(void * p,uint16 v)337 inline void GOOGLE_UNALIGNED_STORE16(void *p, uint16 v) {
338 memcpy(p, &v, sizeof v);
339 }
340
GOOGLE_UNALIGNED_STORE32(void * p,uint32 v)341 inline void GOOGLE_UNALIGNED_STORE32(void *p, uint32 v) {
342 memcpy(p, &v, sizeof v);
343 }
344
GOOGLE_UNALIGNED_STORE64(void * p,uint64 v)345 inline void GOOGLE_UNALIGNED_STORE64(void *p, uint64 v) {
346 memcpy(p, &v, sizeof v);
347 }
348 #endif
349
350 #if defined(_MSC_VER)
351 #define GOOGLE_THREAD_LOCAL __declspec(thread)
352 #else
353 #define GOOGLE_THREAD_LOCAL __thread
354 #endif
355
356 // The following guarantees declaration of the byte swap functions.
357 #ifdef _MSC_VER
358 #define bswap_16(x) _byteswap_ushort(x)
359 #define bswap_32(x) _byteswap_ulong(x)
360 #define bswap_64(x) _byteswap_uint64(x)
361
362 #elif defined(__APPLE__)
363 // Mac OS X / Darwin features
364 #define bswap_16(x) OSSwapInt16(x)
365 #define bswap_32(x) OSSwapInt32(x)
366 #define bswap_64(x) OSSwapInt64(x)
367
368 #elif !defined(__GLIBC__) && !defined(__CYGWIN__)
369
bswap_16(uint16 x)370 static inline uint16 bswap_16(uint16 x) {
371 return static_cast<uint16>(((x & 0xFF) << 8) | ((x & 0xFF00) >> 8));
372 }
373 #define bswap_16(x) bswap_16(x)
bswap_32(uint32 x)374 static inline uint32 bswap_32(uint32 x) {
375 return (((x & 0xFF) << 24) |
376 ((x & 0xFF00) << 8) |
377 ((x & 0xFF0000) >> 8) |
378 ((x & 0xFF000000) >> 24));
379 }
380 #define bswap_32(x) bswap_32(x)
bswap_64(uint64 x)381 static inline uint64 bswap_64(uint64 x) {
382 return (((x & GOOGLE_ULONGLONG(0xFF)) << 56) |
383 ((x & GOOGLE_ULONGLONG(0xFF00)) << 40) |
384 ((x & GOOGLE_ULONGLONG(0xFF0000)) << 24) |
385 ((x & GOOGLE_ULONGLONG(0xFF000000)) << 8) |
386 ((x & GOOGLE_ULONGLONG(0xFF00000000)) >> 8) |
387 ((x & GOOGLE_ULONGLONG(0xFF0000000000)) >> 24) |
388 ((x & GOOGLE_ULONGLONG(0xFF000000000000)) >> 40) |
389 ((x & GOOGLE_ULONGLONG(0xFF00000000000000)) >> 56));
390 }
391 #define bswap_64(x) bswap_64(x)
392
393 #endif
394
395 // ===================================================================
396 // from google3/util/bits/bits.h
397
398 class Bits {
399 public:
Log2FloorNonZero(uint32 n)400 static uint32 Log2FloorNonZero(uint32 n) {
401 #if defined(__GNUC__)
402 return 31 ^ static_cast<uint32>(__builtin_clz(n));
403 #elif defined(COMPILER_MSVC) && defined(_M_IX86)
404 _asm {
405 bsr ebx, n
406 mov n, ebx
407 }
408 return n;
409 #else
410 return Log2FloorNonZero_Portable(n);
411 #endif
412 }
413
Log2FloorNonZero64(uint64 n)414 static uint32 Log2FloorNonZero64(uint64 n) {
415 // arm-nacl-clang runs into an instruction-selection failure when it
416 // encounters __builtin_clzll:
417 // https://bugs.chromium.org/p/nativeclient/issues/detail?id=4395
418 // To work around this, when we build for NaCl we use the portable
419 // implementation instead.
420 #if defined(__GNUC__) && !defined(GOOGLE_PROTOBUF_OS_NACL)
421 return 63 ^ static_cast<uint32>(__builtin_clzll(n));
422 #else
423 return Log2FloorNonZero64_Portable(n);
424 #endif
425 }
426 private:
Log2FloorNonZero_Portable(uint32 n)427 static int Log2FloorNonZero_Portable(uint32 n) {
428 if (n == 0)
429 return -1;
430 int log = 0;
431 uint32 value = n;
432 for (int i = 4; i >= 0; --i) {
433 int shift = (1 << i);
434 uint32 x = value >> shift;
435 if (x != 0) {
436 value = x;
437 log += shift;
438 }
439 }
440 assert(value == 1);
441 return log;
442 }
443
Log2FloorNonZero64_Portable(uint64 n)444 static int Log2FloorNonZero64_Portable(uint64 n) {
445 const uint32 topbits = static_cast<uint32>(n >> 32);
446 if (topbits == 0) {
447 // Top bits are zero, so scan in bottom bits
448 return static_cast<int>(Log2FloorNonZero(static_cast<uint32>(n)));
449 } else {
450 return 32 + static_cast<int>(Log2FloorNonZero(topbits));
451 }
452 }
453 };
454
455 // ===================================================================
456 // from google3/util/endian/endian.h
457 LIBPROTOBUF_EXPORT uint32 ghtonl(uint32 x);
458
459 class BigEndian {
460 public:
461 #ifdef PROTOBUF_LITTLE_ENDIAN
462
FromHost16(uint16 x)463 static uint16 FromHost16(uint16 x) { return bswap_16(x); }
ToHost16(uint16 x)464 static uint16 ToHost16(uint16 x) { return bswap_16(x); }
465
FromHost32(uint32 x)466 static uint32 FromHost32(uint32 x) { return bswap_32(x); }
ToHost32(uint32 x)467 static uint32 ToHost32(uint32 x) { return bswap_32(x); }
468
FromHost64(uint64 x)469 static uint64 FromHost64(uint64 x) { return bswap_64(x); }
ToHost64(uint64 x)470 static uint64 ToHost64(uint64 x) { return bswap_64(x); }
471
IsLittleEndian()472 static bool IsLittleEndian() { return true; }
473
474 #else
475
476 static uint16 FromHost16(uint16 x) { return x; }
477 static uint16 ToHost16(uint16 x) { return x; }
478
479 static uint32 FromHost32(uint32 x) { return x; }
480 static uint32 ToHost32(uint32 x) { return x; }
481
482 static uint64 FromHost64(uint64 x) { return x; }
483 static uint64 ToHost64(uint64 x) { return x; }
484
485 static bool IsLittleEndian() { return false; }
486
487 #endif /* ENDIAN */
488
489 // Functions to do unaligned loads and stores in big-endian order.
Load16(const void * p)490 static uint16 Load16(const void *p) {
491 return ToHost16(GOOGLE_UNALIGNED_LOAD16(p));
492 }
493
Store16(void * p,uint16 v)494 static void Store16(void *p, uint16 v) {
495 GOOGLE_UNALIGNED_STORE16(p, FromHost16(v));
496 }
497
Load32(const void * p)498 static uint32 Load32(const void *p) {
499 return ToHost32(GOOGLE_UNALIGNED_LOAD32(p));
500 }
501
Store32(void * p,uint32 v)502 static void Store32(void *p, uint32 v) {
503 GOOGLE_UNALIGNED_STORE32(p, FromHost32(v));
504 }
505
Load64(const void * p)506 static uint64 Load64(const void *p) {
507 return ToHost64(GOOGLE_UNALIGNED_LOAD64(p));
508 }
509
Store64(void * p,uint64 v)510 static void Store64(void *p, uint64 v) {
511 GOOGLE_UNALIGNED_STORE64(p, FromHost64(v));
512 }
513 };
514
515 #ifndef GOOGLE_ATTRIBUTE_SECTION_VARIABLE
516 #define GOOGLE_ATTRIBUTE_SECTION_VARIABLE(name)
517 #endif
518
519 #define GOOGLE_PROTOBUF_ATTRIBUTE_SECTION_VARIABLE(name)
520
521 } // namespace protobuf
522 } // namespace google
523
524 #endif // GOOGLE_PROTOBUF_STUBS_PORT_H_
525