• 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 #undef PROTOBUF_LITTLE_ENDIAN
48 #ifdef _WIN32
49   // Assuming windows is always little-endian.
50   // TODO(xiaofeng): The PROTOBUF_LITTLE_ENDIAN is not only used for
51   // optimization but also for correctness. We should define an
52   // different macro to test the big-endian code path in coded_stream.
53   #if !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
54     #define PROTOBUF_LITTLE_ENDIAN 1
55   #endif
56   #if _MSC_VER >= 1300 && !defined(__INTEL_COMPILER)
57     // If MSVC has "/RTCc" set, it will complain about truncating casts at
58     // runtime.  This file contains some intentional truncating casts.
59     #pragma runtime_checks("c", off)
60   #endif
61 #else
62   #include <sys/param.h>   // __BYTE_ORDER
63   #if ((defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) || \
64          (defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN)) && \
65       !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
66     #define PROTOBUF_LITTLE_ENDIAN 1
67   #endif
68 #endif
69 #if defined(_MSC_VER) && defined(PROTOBUF_USE_DLLS)
70   #ifdef LIBPROTOBUF_EXPORTS
71     #define LIBPROTOBUF_EXPORT __declspec(dllexport)
72   #else
73     #define LIBPROTOBUF_EXPORT __declspec(dllimport)
74   #endif
75   #ifdef LIBPROTOC_EXPORTS
76     #define LIBPROTOC_EXPORT   __declspec(dllexport)
77   #else
78     #define LIBPROTOC_EXPORT   __declspec(dllimport)
79   #endif
80 #else
81   #define LIBPROTOBUF_EXPORT
82   #define LIBPROTOC_EXPORT
83 #endif
84 
85 // These #includes are for the byte swap functions declared later on.
86 #ifdef _MSC_VER
87 #include <stdlib.h>  // NOLINT(build/include)
88 #elif defined(__APPLE__)
89 #include <libkern/OSByteOrder.h>
90 #elif defined(__GLIBC__) || defined(__CYGWIN__)
91 #include <byteswap.h>  // IWYU pragma: export
92 #endif
93 
94 // ===================================================================
95 // from google3/base/port.h
96 namespace google {
97 namespace protobuf {
98 
99 typedef unsigned int uint;
100 
101 #ifdef _MSC_VER
102 typedef signed __int8  int8;
103 typedef __int16 int16;
104 typedef __int32 int32;
105 typedef __int64 int64;
106 
107 typedef unsigned __int8  uint8;
108 typedef unsigned __int16 uint16;
109 typedef unsigned __int32 uint32;
110 typedef unsigned __int64 uint64;
111 #else
112 typedef int8_t int8;
113 typedef int16_t int16;
114 typedef int32_t int32;
115 typedef int64_t int64;
116 
117 typedef uint8_t uint8;
118 typedef uint16_t uint16;
119 typedef uint32_t uint32;
120 typedef uint64_t uint64;
121 #endif
122 
123 // long long macros to be used because gcc and vc++ use different suffixes,
124 // and different size specifiers in format strings
125 #undef GOOGLE_LONGLONG
126 #undef GOOGLE_ULONGLONG
127 #undef GOOGLE_LL_FORMAT
128 
129 #ifdef _MSC_VER
130 #define GOOGLE_LONGLONG(x) x##I64
131 #define GOOGLE_ULONGLONG(x) x##UI64
132 #define GOOGLE_LL_FORMAT "I64"  // As in printf("%I64d", ...)
133 #else
134 // By long long, we actually mean int64.
135 #define GOOGLE_LONGLONG(x) x##LL
136 #define GOOGLE_ULONGLONG(x) x##ULL
137 // Used to format real long long integers.
138 #define GOOGLE_LL_FORMAT "ll"  // As in "%lld". Note that "q" is poor form also.
139 #endif
140 
141 static const int32 kint32max = 0x7FFFFFFF;
142 static const int32 kint32min = -kint32max - 1;
143 static const int64 kint64max = GOOGLE_LONGLONG(0x7FFFFFFFFFFFFFFF);
144 static const int64 kint64min = -kint64max - 1;
145 static const uint32 kuint32max = 0xFFFFFFFFu;
146 static const uint64 kuint64max = GOOGLE_ULONGLONG(0xFFFFFFFFFFFFFFFF);
147 
148 // -------------------------------------------------------------------
149 // Annotations:  Some parts of the code have been annotated in ways that might
150 //   be useful to some compilers or tools, but are not supported universally.
151 //   You can #define these annotations yourself if the default implementation
152 //   is not right for you.
153 
154 #ifndef GOOGLE_ATTRIBUTE_ALWAYS_INLINE
155 #if defined(__GNUC__) && (__GNUC__ > 3 ||(__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
156 // For functions we want to force inline.
157 // Introduced in gcc 3.1.
158 #define GOOGLE_ATTRIBUTE_ALWAYS_INLINE __attribute__ ((always_inline))
159 #else
160 // Other compilers will have to figure it out for themselves.
161 #define GOOGLE_ATTRIBUTE_ALWAYS_INLINE
162 #endif
163 #endif
164 
165 #ifndef GOOGLE_ATTRIBUTE_NOINLINE
166 #if defined(__GNUC__) && (__GNUC__ > 3 ||(__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
167 // For functions we want to force not inline.
168 // Introduced in gcc 3.1.
169 #define GOOGLE_ATTRIBUTE_NOINLINE __attribute__ ((noinline))
170 #elif defined(_MSC_VER) && (_MSC_VER >= 1400)
171 // Seems to have been around since at least Visual Studio 2005
172 #define GOOGLE_ATTRIBUTE_NOINLINE __declspec(noinline)
173 #else
174 // Other compilers will have to figure it out for themselves.
175 #define GOOGLE_ATTRIBUTE_NOINLINE
176 #endif
177 #endif
178 
179 #ifndef GOOGLE_ATTRIBUTE_NORETURN
180 #ifdef __GNUC__
181 // Tell the compiler that a given function never returns.
182 #define GOOGLE_ATTRIBUTE_NORETURN __attribute__((noreturn))
183 #else
184 #define GOOGLE_ATTRIBUTE_NORETURN
185 #endif
186 #endif
187 
188 #ifndef GOOGLE_ATTRIBUTE_DEPRECATED
189 #ifdef __GNUC__
190 // If the method/variable/type is used anywhere, produce a warning.
191 #define GOOGLE_ATTRIBUTE_DEPRECATED __attribute__((deprecated))
192 #else
193 #define GOOGLE_ATTRIBUTE_DEPRECATED
194 #endif
195 #endif
196 
197 #ifndef GOOGLE_PREDICT_TRUE
198 #ifdef __GNUC__
199 // Provided at least since GCC 3.0.
200 #define GOOGLE_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
201 #else
202 #define GOOGLE_PREDICT_TRUE(x) (x)
203 #endif
204 #endif
205 
206 #ifndef GOOGLE_PREDICT_FALSE
207 #ifdef __GNUC__
208 // Provided at least since GCC 3.0.
209 #define GOOGLE_PREDICT_FALSE(x) (__builtin_expect(x, 0))
210 #else
211 #define GOOGLE_PREDICT_FALSE(x) (x)
212 #endif
213 #endif
214 
215 // Delimits a block of code which may write to memory which is simultaneously
216 // written by other threads, but which has been determined to be thread-safe
217 // (e.g. because it is an idempotent write).
218 #ifndef GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN
219 #define GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN()
220 #endif
221 #ifndef GOOGLE_SAFE_CONCURRENT_WRITES_END
222 #define GOOGLE_SAFE_CONCURRENT_WRITES_END()
223 #endif
224 
225 #if defined(__clang__) && defined(__has_cpp_attribute) \
226     && !defined(GOOGLE_PROTOBUF_OS_APPLE)
227 # if defined(GOOGLE_PROTOBUF_OS_NACL) || defined(EMSCRIPTEN) || \
228      __has_cpp_attribute(clang::fallthrough)
229 #  define GOOGLE_FALLTHROUGH_INTENDED [[clang::fallthrough]]
230 # endif
231 #endif
232 
233 #ifndef GOOGLE_FALLTHROUGH_INTENDED
234 # define GOOGLE_FALLTHROUGH_INTENDED
235 #endif
236 
237 #define GOOGLE_GUARDED_BY(x)
238 #define GOOGLE_ATTRIBUTE_COLD
239 
240 // x86 and x86-64 can perform unaligned loads/stores directly.
241 #if defined(_M_X64) || defined(__x86_64__) || \
242     defined(_M_IX86) || defined(__i386__)
243 
244 #define GOOGLE_UNALIGNED_LOAD16(_p) (*reinterpret_cast<const uint16 *>(_p))
245 #define GOOGLE_UNALIGNED_LOAD32(_p) (*reinterpret_cast<const uint32 *>(_p))
246 #define GOOGLE_UNALIGNED_LOAD64(_p) (*reinterpret_cast<const uint64 *>(_p))
247 
248 #define GOOGLE_UNALIGNED_STORE16(_p, _val) (*reinterpret_cast<uint16 *>(_p) = (_val))
249 #define GOOGLE_UNALIGNED_STORE32(_p, _val) (*reinterpret_cast<uint32 *>(_p) = (_val))
250 #define GOOGLE_UNALIGNED_STORE64(_p, _val) (*reinterpret_cast<uint64 *>(_p) = (_val))
251 
252 #else
GOOGLE_UNALIGNED_LOAD16(const void * p)253 inline uint16 GOOGLE_UNALIGNED_LOAD16(const void *p) {
254   uint16 t;
255   memcpy(&t, p, sizeof t);
256   return t;
257 }
258 
GOOGLE_UNALIGNED_LOAD32(const void * p)259 inline uint32 GOOGLE_UNALIGNED_LOAD32(const void *p) {
260   uint32 t;
261   memcpy(&t, p, sizeof t);
262   return t;
263 }
264 
GOOGLE_UNALIGNED_LOAD64(const void * p)265 inline uint64 GOOGLE_UNALIGNED_LOAD64(const void *p) {
266   uint64 t;
267   memcpy(&t, p, sizeof t);
268   return t;
269 }
270 
GOOGLE_UNALIGNED_STORE16(void * p,uint16 v)271 inline void GOOGLE_UNALIGNED_STORE16(void *p, uint16 v) {
272   memcpy(p, &v, sizeof v);
273 }
274 
GOOGLE_UNALIGNED_STORE32(void * p,uint32 v)275 inline void GOOGLE_UNALIGNED_STORE32(void *p, uint32 v) {
276   memcpy(p, &v, sizeof v);
277 }
278 
GOOGLE_UNALIGNED_STORE64(void * p,uint64 v)279 inline void GOOGLE_UNALIGNED_STORE64(void *p, uint64 v) {
280   memcpy(p, &v, sizeof v);
281 }
282 #endif
283 
284 #if defined(_MSC_VER)
285 #define GOOGLE_THREAD_LOCAL __declspec(thread)
286 #else
287 #define GOOGLE_THREAD_LOCAL __thread
288 #endif
289 
290 // The following guarantees declaration of the byte swap functions.
291 #ifdef _MSC_VER
292 #define bswap_16(x) _byteswap_ushort(x)
293 #define bswap_32(x) _byteswap_ulong(x)
294 #define bswap_64(x) _byteswap_uint64(x)
295 
296 #elif defined(__APPLE__)
297 // Mac OS X / Darwin features
298 #define bswap_16(x) OSSwapInt16(x)
299 #define bswap_32(x) OSSwapInt32(x)
300 #define bswap_64(x) OSSwapInt64(x)
301 
302 #elif !defined(__GLIBC__) && !defined(__CYGWIN__)
303 
bswap_16(uint16 x)304 static inline uint16 bswap_16(uint16 x) {
305   return static_cast<uint16>(((x & 0xFF) << 8) | ((x & 0xFF00) >> 8));
306 }
307 #define bswap_16(x) bswap_16(x)
bswap_32(uint32 x)308 static inline uint32 bswap_32(uint32 x) {
309   return (((x & 0xFF) << 24) |
310           ((x & 0xFF00) << 8) |
311           ((x & 0xFF0000) >> 8) |
312           ((x & 0xFF000000) >> 24));
313 }
314 #define bswap_32(x) bswap_32(x)
bswap_64(uint64 x)315 static inline uint64 bswap_64(uint64 x) {
316   return (((x & GOOGLE_ULONGLONG(0xFF)) << 56) |
317           ((x & GOOGLE_ULONGLONG(0xFF00)) << 40) |
318           ((x & GOOGLE_ULONGLONG(0xFF0000)) << 24) |
319           ((x & GOOGLE_ULONGLONG(0xFF000000)) << 8) |
320           ((x & GOOGLE_ULONGLONG(0xFF00000000)) >> 8) |
321           ((x & GOOGLE_ULONGLONG(0xFF0000000000)) >> 24) |
322           ((x & GOOGLE_ULONGLONG(0xFF000000000000)) >> 40) |
323           ((x & GOOGLE_ULONGLONG(0xFF00000000000000)) >> 56));
324 }
325 #define bswap_64(x) bswap_64(x)
326 
327 #endif
328 
329 // ===================================================================
330 // from google3/util/endian/endian.h
331 LIBPROTOBUF_EXPORT uint32 ghtonl(uint32 x);
332 
333 class BigEndian {
334  public:
335 #ifdef PROTOBUF_LITTLE_ENDIAN
336 
FromHost16(uint16 x)337   static uint16 FromHost16(uint16 x) { return bswap_16(x); }
ToHost16(uint16 x)338   static uint16 ToHost16(uint16 x) { return bswap_16(x); }
339 
FromHost32(uint32 x)340   static uint32 FromHost32(uint32 x) { return bswap_32(x); }
ToHost32(uint32 x)341   static uint32 ToHost32(uint32 x) { return bswap_32(x); }
342 
FromHost64(uint64 x)343   static uint64 FromHost64(uint64 x) { return bswap_64(x); }
ToHost64(uint64 x)344   static uint64 ToHost64(uint64 x) { return bswap_64(x); }
345 
IsLittleEndian()346   static bool IsLittleEndian() { return true; }
347 
348 #else
349 
350   static uint16 FromHost16(uint16 x) { return x; }
351   static uint16 ToHost16(uint16 x) { return x; }
352 
353   static uint32 FromHost32(uint32 x) { return x; }
354   static uint32 ToHost32(uint32 x) { return x; }
355 
356   static uint64 FromHost64(uint64 x) { return x; }
357   static uint64 ToHost64(uint64 x) { return x; }
358 
359   static bool IsLittleEndian() { return false; }
360 
361 #endif /* ENDIAN */
362 
363   // Functions to do unaligned loads and stores in big-endian order.
Load16(const void * p)364   static uint16 Load16(const void *p) {
365     return ToHost16(GOOGLE_UNALIGNED_LOAD16(p));
366   }
367 
Store16(void * p,uint16 v)368   static void Store16(void *p, uint16 v) {
369     GOOGLE_UNALIGNED_STORE16(p, FromHost16(v));
370   }
371 
Load32(const void * p)372   static uint32 Load32(const void *p) {
373     return ToHost32(GOOGLE_UNALIGNED_LOAD32(p));
374   }
375 
Store32(void * p,uint32 v)376   static void Store32(void *p, uint32 v) {
377     GOOGLE_UNALIGNED_STORE32(p, FromHost32(v));
378   }
379 
Load64(const void * p)380   static uint64 Load64(const void *p) {
381     return ToHost64(GOOGLE_UNALIGNED_LOAD64(p));
382   }
383 
Store64(void * p,uint64 v)384   static void Store64(void *p, uint64 v) {
385     GOOGLE_UNALIGNED_STORE64(p, FromHost64(v));
386   }
387 };
388 
389 
390 }  // namespace protobuf
391 }  // namespace google
392 
393 #endif  // GOOGLE_PROTOBUF_STUBS_PORT_H_
394