• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <google/protobuf/port_def.inc>
50 
51 #undef PROTOBUF_LITTLE_ENDIAN
52 #ifdef _WIN32
53   // Assuming windows is always little-endian.
54   // TODO(xiaofeng): The PROTOBUF_LITTLE_ENDIAN is not only used for
55   // optimization but also for correctness. We should define an
56   // different macro to test the big-endian code path in coded_stream.
57   #if !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
58     #define PROTOBUF_LITTLE_ENDIAN 1
59   #endif
60   #if _MSC_VER >= 1300 && !defined(__INTEL_COMPILER)
61     // If MSVC has "/RTCc" set, it will complain about truncating casts at
62     // runtime.  This file contains some intentional truncating casts.
63     #pragma runtime_checks("c", off)
64   #endif
65 #else
66   #include <sys/param.h>   // __BYTE_ORDER
67   #if defined(__OpenBSD__)
68     #include <endian.h>
69   #endif
70   #if ((defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) || \
71          (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN) || \
72          (defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN)) && \
73       !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
74     #define PROTOBUF_LITTLE_ENDIAN 1
75   #endif
76 #endif
77 
78 // These #includes are for the byte swap functions declared later on.
79 #ifdef _MSC_VER
80 #include <stdlib.h>  // NOLINT(build/include)
81 #include <intrin.h>
82 #elif defined(__APPLE__)
83 #include <libkern/OSByteOrder.h>
84 #elif defined(__GLIBC__) || defined(__BIONIC__) || defined(__CYGWIN__)
85 #include <byteswap.h>  // IWYU pragma: export
86 #endif
87 
88 // Legacy: some users reference these (internal-only) macros even though we
89 // don't need them any more.
90 #if defined(_MSC_VER) && defined(PROTOBUF_USE_DLLS)
91   #ifdef LIBPROTOBUF_EXPORTS
92     #define LIBPROTOBUF_EXPORT __declspec(dllexport)
93   #else
94     #define LIBPROTOBUF_EXPORT __declspec(dllimport)
95   #endif
96   #ifdef LIBPROTOC_EXPORTS
97     #define LIBPROTOC_EXPORT   __declspec(dllexport)
98   #else
99     #define LIBPROTOC_EXPORT   __declspec(dllimport)
100   #endif
101 #else
102   #define LIBPROTOBUF_EXPORT
103   #define LIBPROTOC_EXPORT
104 #endif
105 
106 #define PROTOBUF_RUNTIME_DEPRECATED(message)
107 #define GOOGLE_PROTOBUF_RUNTIME_DEPRECATED(message)
108 
109 // ===================================================================
110 // from google3/base/port.h
111 
112 #if (defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L || \
113      (defined(_MSC_VER) && _MSC_VER >= 1900))
114 // Define this to 1 if the code is compiled in C++11 mode; leave it
115 // undefined otherwise.  Do NOT define it to 0 -- that causes
116 // '#ifdef LANG_CXX11' to behave differently from '#if LANG_CXX11'.
117 #define LANG_CXX11 1
118 #endif
119 
120 #if LANG_CXX11 && !defined(__NVCC__)
121 #define PROTOBUF_CXX11 1
122 #else
123 #define PROTOBUF_CXX11 0
124 #endif
125 
126 #if PROTOBUF_CXX11
127 #define PROTOBUF_FINAL final
128 #else
129 #define PROTOBUF_FINAL
130 #endif
131 
132 namespace google {
133 namespace protobuf {
134 
135 typedef unsigned int uint;
136 
137 #ifdef _MSC_VER
138 typedef signed __int8  int8;
139 typedef __int16 int16;
140 typedef __int32 int32;
141 typedef __int64 int64;
142 
143 typedef unsigned __int8  uint8;
144 typedef unsigned __int16 uint16;
145 typedef unsigned __int32 uint32;
146 typedef unsigned __int64 uint64;
147 #else
148 typedef int8_t int8;
149 typedef int16_t int16;
150 typedef int32_t int32;
151 typedef int64_t int64;
152 
153 typedef uint8_t uint8;
154 typedef uint16_t uint16;
155 typedef uint32_t uint32;
156 typedef uint64_t uint64;
157 #endif
158 
159 static const int32 kint32max = 0x7FFFFFFF;
160 static const int32 kint32min = -kint32max - 1;
161 static const int64 kint64max = PROTOBUF_LONGLONG(0x7FFFFFFFFFFFFFFF);
162 static const int64 kint64min = -kint64max - 1;
163 static const uint32 kuint32max = 0xFFFFFFFFu;
164 static const uint64 kuint64max = PROTOBUF_ULONGLONG(0xFFFFFFFFFFFFFFFF);
165 
166 #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) ||\
167     defined(MEMORY_SANITIZER)
168 
169 #ifdef __cplusplus
170 extern "C" {
171 #endif  // __cplusplus
172 uint16_t __sanitizer_unaligned_load16(const void *p);
173 uint32_t __sanitizer_unaligned_load32(const void *p);
174 uint64_t __sanitizer_unaligned_load64(const void *p);
175 void __sanitizer_unaligned_store16(void *p, uint16_t v);
176 void __sanitizer_unaligned_store32(void *p, uint32_t v);
177 void __sanitizer_unaligned_store64(void *p, uint64_t v);
178 #ifdef __cplusplus
179 }  // extern "C"
180 #endif  // __cplusplus
181 
GOOGLE_UNALIGNED_LOAD16(const void * p)182 inline uint16 GOOGLE_UNALIGNED_LOAD16(const void *p) {
183   return __sanitizer_unaligned_load16(p);
184 }
185 
GOOGLE_UNALIGNED_LOAD32(const void * p)186 inline uint32 GOOGLE_UNALIGNED_LOAD32(const void *p) {
187   return __sanitizer_unaligned_load32(p);
188 }
189 
GOOGLE_UNALIGNED_LOAD64(const void * p)190 inline uint64 GOOGLE_UNALIGNED_LOAD64(const void *p) {
191   return __sanitizer_unaligned_load64(p);
192 }
193 
GOOGLE_UNALIGNED_STORE16(void * p,uint16 v)194 inline void GOOGLE_UNALIGNED_STORE16(void *p, uint16 v) {
195   __sanitizer_unaligned_store16(p, v);
196 }
197 
GOOGLE_UNALIGNED_STORE32(void * p,uint32 v)198 inline void GOOGLE_UNALIGNED_STORE32(void *p, uint32 v) {
199   __sanitizer_unaligned_store32(p, v);
200 }
201 
GOOGLE_UNALIGNED_STORE64(void * p,uint64 v)202 inline void GOOGLE_UNALIGNED_STORE64(void *p, uint64 v) {
203   __sanitizer_unaligned_store64(p, v);
204 }
205 
206 #elif GOOGLE_PROTOBUF_USE_UNALIGNED
207 
208 #define GOOGLE_UNALIGNED_LOAD16(_p) (*reinterpret_cast<const uint16 *>(_p))
209 #define GOOGLE_UNALIGNED_LOAD32(_p) (*reinterpret_cast<const uint32 *>(_p))
210 #define GOOGLE_UNALIGNED_LOAD64(_p) (*reinterpret_cast<const uint64 *>(_p))
211 
212 #define GOOGLE_UNALIGNED_STORE16(_p, _val) (*reinterpret_cast<uint16 *>(_p) = (_val))
213 #define GOOGLE_UNALIGNED_STORE32(_p, _val) (*reinterpret_cast<uint32 *>(_p) = (_val))
214 #define GOOGLE_UNALIGNED_STORE64(_p, _val) (*reinterpret_cast<uint64 *>(_p) = (_val))
215 
216 #else
GOOGLE_UNALIGNED_LOAD16(const void * p)217 inline uint16 GOOGLE_UNALIGNED_LOAD16(const void *p) {
218   uint16 t;
219   memcpy(&t, p, sizeof t);
220   return t;
221 }
222 
GOOGLE_UNALIGNED_LOAD32(const void * p)223 inline uint32 GOOGLE_UNALIGNED_LOAD32(const void *p) {
224   uint32 t;
225   memcpy(&t, p, sizeof t);
226   return t;
227 }
228 
GOOGLE_UNALIGNED_LOAD64(const void * p)229 inline uint64 GOOGLE_UNALIGNED_LOAD64(const void *p) {
230   uint64 t;
231   memcpy(&t, p, sizeof t);
232   return t;
233 }
234 
GOOGLE_UNALIGNED_STORE16(void * p,uint16 v)235 inline void GOOGLE_UNALIGNED_STORE16(void *p, uint16 v) {
236   memcpy(p, &v, sizeof v);
237 }
238 
GOOGLE_UNALIGNED_STORE32(void * p,uint32 v)239 inline void GOOGLE_UNALIGNED_STORE32(void *p, uint32 v) {
240   memcpy(p, &v, sizeof v);
241 }
242 
GOOGLE_UNALIGNED_STORE64(void * p,uint64 v)243 inline void GOOGLE_UNALIGNED_STORE64(void *p, uint64 v) {
244   memcpy(p, &v, sizeof v);
245 }
246 #endif
247 
248 #if defined(GOOGLE_PROTOBUF_OS_NACL) \
249     || (defined(__ANDROID__) && defined(__clang__) \
250         && (__clang_major__ == 3 && __clang_minor__ == 8) \
251         && (__clang_patchlevel__ < 275480))
252 # define GOOGLE_PROTOBUF_USE_PORTABLE_LOG2
253 #endif
254 
255 #if defined(_MSC_VER)
256 #define GOOGLE_THREAD_LOCAL __declspec(thread)
257 #else
258 #define GOOGLE_THREAD_LOCAL __thread
259 #endif
260 
261 // The following guarantees declaration of the byte swap functions.
262 #ifdef _MSC_VER
263 #define bswap_16(x) _byteswap_ushort(x)
264 #define bswap_32(x) _byteswap_ulong(x)
265 #define bswap_64(x) _byteswap_uint64(x)
266 
267 #elif defined(__APPLE__)
268 // Mac OS X / Darwin features
269 #define bswap_16(x) OSSwapInt16(x)
270 #define bswap_32(x) OSSwapInt32(x)
271 #define bswap_64(x) OSSwapInt64(x)
272 
273 #elif !defined(__GLIBC__) && !defined(__BIONIC__) && !defined(__CYGWIN__)
274 
275 #ifndef bswap_16
bswap_16(uint16 x)276 static inline uint16 bswap_16(uint16 x) {
277   return static_cast<uint16>(((x & 0xFF) << 8) | ((x & 0xFF00) >> 8));
278 }
279 #define bswap_16(x) bswap_16(x)
280 #endif
281 
282 #ifndef bswap_32
bswap_32(uint32 x)283 static inline uint32 bswap_32(uint32 x) {
284   return (((x & 0xFF) << 24) |
285           ((x & 0xFF00) << 8) |
286           ((x & 0xFF0000) >> 8) |
287           ((x & 0xFF000000) >> 24));
288 }
289 #define bswap_32(x) bswap_32(x)
290 #endif
291 
292 #ifndef bswap_64
bswap_64(uint64 x)293 static inline uint64 bswap_64(uint64 x) {
294   return (((x & PROTOBUF_ULONGLONG(0xFF)) << 56) |
295           ((x & PROTOBUF_ULONGLONG(0xFF00)) << 40) |
296           ((x & PROTOBUF_ULONGLONG(0xFF0000)) << 24) |
297           ((x & PROTOBUF_ULONGLONG(0xFF000000)) << 8) |
298           ((x & PROTOBUF_ULONGLONG(0xFF00000000)) >> 8) |
299           ((x & PROTOBUF_ULONGLONG(0xFF0000000000)) >> 24) |
300           ((x & PROTOBUF_ULONGLONG(0xFF000000000000)) >> 40) |
301           ((x & PROTOBUF_ULONGLONG(0xFF00000000000000)) >> 56));
302 }
303 #define bswap_64(x) bswap_64(x)
304 #endif
305 
306 #endif
307 
308 // ===================================================================
309 // from google3/util/bits/bits.h
310 
311 class Bits {
312  public:
Log2FloorNonZero(uint32 n)313   static uint32 Log2FloorNonZero(uint32 n) {
314 #if defined(__GNUC__)
315   return 31 ^ static_cast<uint32>(__builtin_clz(n));
316 #elif defined(_MSC_VER)
317   unsigned long where;
318   _BitScanReverse(&where, n);
319   return where;
320 #else
321   return Log2FloorNonZero_Portable(n);
322 #endif
323   }
324 
Log2FloorNonZero64(uint64 n)325   static uint32 Log2FloorNonZero64(uint64 n) {
326     // Older versions of clang run into an instruction-selection failure when
327     // it encounters __builtin_clzll:
328     // https://bugs.chromium.org/p/nativeclient/issues/detail?id=4395
329     // This includes arm-nacl-clang and clang in older Android NDK versions.
330     // To work around this, when we build with those we use the portable
331     // implementation instead.
332 #if defined(__GNUC__) && !defined(GOOGLE_PROTOBUF_USE_PORTABLE_LOG2)
333   return 63 ^ static_cast<uint32>(__builtin_clzll(n));
334 #elif defined(_MSC_VER) && defined(_M_X64)
335   unsigned long where;
336   _BitScanReverse64(&where, n);
337   return where;
338 #else
339   return Log2FloorNonZero64_Portable(n);
340 #endif
341   }
342  private:
Log2FloorNonZero_Portable(uint32 n)343   static int Log2FloorNonZero_Portable(uint32 n) {
344     if (n == 0)
345       return -1;
346     int log = 0;
347     uint32 value = n;
348     for (int i = 4; i >= 0; --i) {
349       int shift = (1 << i);
350       uint32 x = value >> shift;
351       if (x != 0) {
352         value = x;
353         log += shift;
354       }
355     }
356     assert(value == 1);
357     return log;
358   }
359 
Log2FloorNonZero64_Portable(uint64 n)360   static int Log2FloorNonZero64_Portable(uint64 n) {
361     const uint32 topbits = static_cast<uint32>(n >> 32);
362     if (topbits == 0) {
363       // Top bits are zero, so scan in bottom bits
364       return static_cast<int>(Log2FloorNonZero(static_cast<uint32>(n)));
365     } else {
366       return 32 + static_cast<int>(Log2FloorNonZero(topbits));
367     }
368   }
369 };
370 
371 // ===================================================================
372 // from google3/util/endian/endian.h
373 PROTOBUF_EXPORT uint32 ghtonl(uint32 x);
374 
375 class BigEndian {
376  public:
377 #ifdef PROTOBUF_LITTLE_ENDIAN
378 
FromHost16(uint16 x)379   static uint16 FromHost16(uint16 x) { return bswap_16(x); }
ToHost16(uint16 x)380   static uint16 ToHost16(uint16 x) { return bswap_16(x); }
381 
FromHost32(uint32 x)382   static uint32 FromHost32(uint32 x) { return bswap_32(x); }
ToHost32(uint32 x)383   static uint32 ToHost32(uint32 x) { return bswap_32(x); }
384 
FromHost64(uint64 x)385   static uint64 FromHost64(uint64 x) { return bswap_64(x); }
ToHost64(uint64 x)386   static uint64 ToHost64(uint64 x) { return bswap_64(x); }
387 
IsLittleEndian()388   static bool IsLittleEndian() { return true; }
389 
390 #else
391 
392   static uint16 FromHost16(uint16 x) { return x; }
393   static uint16 ToHost16(uint16 x) { return x; }
394 
395   static uint32 FromHost32(uint32 x) { return x; }
396   static uint32 ToHost32(uint32 x) { return x; }
397 
398   static uint64 FromHost64(uint64 x) { return x; }
399   static uint64 ToHost64(uint64 x) { return x; }
400 
401   static bool IsLittleEndian() { return false; }
402 
403 #endif /* ENDIAN */
404 
405   // Functions to do unaligned loads and stores in big-endian order.
Load16(const void * p)406   static uint16 Load16(const void *p) {
407     return ToHost16(GOOGLE_UNALIGNED_LOAD16(p));
408   }
409 
Store16(void * p,uint16 v)410   static void Store16(void *p, uint16 v) {
411     GOOGLE_UNALIGNED_STORE16(p, FromHost16(v));
412   }
413 
Load32(const void * p)414   static uint32 Load32(const void *p) {
415     return ToHost32(GOOGLE_UNALIGNED_LOAD32(p));
416   }
417 
Store32(void * p,uint32 v)418   static void Store32(void *p, uint32 v) {
419     GOOGLE_UNALIGNED_STORE32(p, FromHost32(v));
420   }
421 
Load64(const void * p)422   static uint64 Load64(const void *p) {
423     return ToHost64(GOOGLE_UNALIGNED_LOAD64(p));
424   }
425 
Store64(void * p,uint64 v)426   static void Store64(void *p, uint64 v) {
427     GOOGLE_UNALIGNED_STORE64(p, FromHost64(v));
428   }
429 };
430 
431 }  // namespace protobuf
432 }  // namespace google
433 
434 #ifdef PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
435 #define GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER \
436   PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
437 #else
438 #define GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER 0
439 #endif
440 
441 #include <google/protobuf/port_undef.inc>
442 
443 #endif  // GOOGLE_PROTOBUF_STUBS_PORT_H_
444