port.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. #ifndef GOOGLE_PROTOBUF_STUBS_PORT_H_
  31. #define GOOGLE_PROTOBUF_STUBS_PORT_H_
  32. #include <assert.h>
  33. #include <cstdint>
  34. #include <stdlib.h>
  35. #include <cstddef>
  36. #include <string>
  37. #include <string.h>
  38. #include <google/protobuf/stubs/platform_macros.h>
  39. #include <google/protobuf/port_def.inc>
  40. #undef PROTOBUF_LITTLE_ENDIAN
  41. #ifdef _WIN32
  42. // Assuming windows is always little-endian.
  43. // TODO(xiaofeng): The PROTOBUF_LITTLE_ENDIAN is not only used for
  44. // optimization but also for correctness. We should define an
  45. // different macro to test the big-endian code path in coded_stream.
  46. #if !defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
  47. #define PROTOBUF_LITTLE_ENDIAN 1
  48. #endif
  49. #if defined(_MSC_VER) && _MSC_VER >= 1300 && !defined(__INTEL_COMPILER)
  50. // If MSVC has "/RTCc" set, it will complain about truncating casts at
  51. // runtime. This file contains some intentional truncating casts.
  52. #pragma runtime_checks("c", off)
  53. #endif
  54. #else
  55. #ifdef __APPLE__
  56. #include <machine/endian.h> // __BYTE_ORDER
  57. #elif defined(__FreeBSD__)
  58. #include <sys/endian.h> // __BYTE_ORDER
  59. #else
  60. #if !defined(__QNX__)
  61. #include <endian.h> // __BYTE_ORDER
  62. #endif
  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. // These #includes are for the byte swap functions declared later on.
  72. #ifdef _MSC_VER
  73. #include <stdlib.h> // NOLINT(build/include)
  74. #include <intrin.h>
  75. #elif defined(__APPLE__)
  76. #include <libkern/OSByteOrder.h>
  77. #elif defined(__linux__) || defined(__ANDROID__) || defined(__CYGWIN__)
  78. #include <byteswap.h> // IWYU pragma: export
  79. #endif
  80. // Legacy: some users reference these (internal-only) macros even though we
  81. // don't need them any more.
  82. #if defined(_MSC_VER) && defined(PROTOBUF_USE_DLLS)
  83. #ifdef LIBPROTOBUF_EXPORTS
  84. #define LIBPROTOBUF_EXPORT __declspec(dllexport)
  85. #else
  86. #define LIBPROTOBUF_EXPORT __declspec(dllimport)
  87. #endif
  88. #ifdef LIBPROTOC_EXPORTS
  89. #define LIBPROTOC_EXPORT __declspec(dllexport)
  90. #else
  91. #define LIBPROTOC_EXPORT __declspec(dllimport)
  92. #endif
  93. #else
  94. #define LIBPROTOBUF_EXPORT
  95. #define LIBPROTOC_EXPORT
  96. #endif
  97. #define PROTOBUF_RUNTIME_DEPRECATED(message) PROTOBUF_DEPRECATED_MSG(message)
  98. #define GOOGLE_PROTOBUF_RUNTIME_DEPRECATED(message) \
  99. PROTOBUF_DEPRECATED_MSG(message)
  100. // ===================================================================
  101. // from google3/base/port.h
  102. #if (defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L || \
  103. (defined(_MSC_VER) && _MSC_VER >= 1900))
  104. // Define this to 1 if the code is compiled in C++11 mode; leave it
  105. // undefined otherwise. Do NOT define it to 0 -- that causes
  106. // '#ifdef LANG_CXX11' to behave differently from '#if LANG_CXX11'.
  107. #define LANG_CXX11 1
  108. #else
  109. #error "Protobuf requires at least C++11."
  110. #endif
  111. namespace google {
  112. namespace protobuf {
  113. using ConstStringParam = const std::string &;
  114. typedef unsigned int uint;
  115. typedef int8_t int8;
  116. typedef int16_t int16;
  117. typedef int32_t int32;
  118. typedef int64_t int64;
  119. typedef uint8_t uint8;
  120. typedef uint16_t uint16;
  121. typedef uint32_t uint32;
  122. typedef uint64_t uint64;
  123. static const int32 kint32max = 0x7FFFFFFF;
  124. static const int32 kint32min = -kint32max - 1;
  125. static const int64 kint64max = int64_t{0x7FFFFFFFFFFFFFFF};
  126. static const int64 kint64min = -kint64max - 1;
  127. static const uint32 kuint32max = 0xFFFFFFFFu;
  128. static const uint64 kuint64max = uint64_t{0xFFFFFFFFFFFFFFFFu};
  129. #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) ||\
  130. defined(MEMORY_SANITIZER)
  131. #ifdef __cplusplus
  132. extern "C" {
  133. #endif // __cplusplus
  134. uint16_t __sanitizer_unaligned_load16(const void *p);
  135. uint32_t __sanitizer_unaligned_load32(const void *p);
  136. uint64_t __sanitizer_unaligned_load64(const void *p);
  137. void __sanitizer_unaligned_store16(void *p, uint16_t v);
  138. void __sanitizer_unaligned_store32(void *p, uint32_t v);
  139. void __sanitizer_unaligned_store64(void *p, uint64_t v);
  140. #ifdef __cplusplus
  141. } // extern "C"
  142. #endif // __cplusplus
  143. inline uint16 GOOGLE_UNALIGNED_LOAD16(const void *p) {
  144. return __sanitizer_unaligned_load16(p);
  145. }
  146. inline uint32 GOOGLE_UNALIGNED_LOAD32(const void *p) {
  147. return __sanitizer_unaligned_load32(p);
  148. }
  149. inline uint64 GOOGLE_UNALIGNED_LOAD64(const void *p) {
  150. return __sanitizer_unaligned_load64(p);
  151. }
  152. inline void GOOGLE_UNALIGNED_STORE16(void *p, uint16 v) {
  153. __sanitizer_unaligned_store16(p, v);
  154. }
  155. inline void GOOGLE_UNALIGNED_STORE32(void *p, uint32 v) {
  156. __sanitizer_unaligned_store32(p, v);
  157. }
  158. inline void GOOGLE_UNALIGNED_STORE64(void *p, uint64 v) {
  159. __sanitizer_unaligned_store64(p, v);
  160. }
  161. #elif defined(GOOGLE_PROTOBUF_USE_UNALIGNED) && GOOGLE_PROTOBUF_USE_UNALIGNED
  162. #define GOOGLE_UNALIGNED_LOAD16(_p) (*reinterpret_cast<const uint16 *>(_p))
  163. #define GOOGLE_UNALIGNED_LOAD32(_p) (*reinterpret_cast<const uint32 *>(_p))
  164. #define GOOGLE_UNALIGNED_LOAD64(_p) (*reinterpret_cast<const uint64 *>(_p))
  165. #define GOOGLE_UNALIGNED_STORE16(_p, _val) (*reinterpret_cast<uint16 *>(_p) = (_val))
  166. #define GOOGLE_UNALIGNED_STORE32(_p, _val) (*reinterpret_cast<uint32 *>(_p) = (_val))
  167. #define GOOGLE_UNALIGNED_STORE64(_p, _val) (*reinterpret_cast<uint64 *>(_p) = (_val))
  168. #else
  169. inline uint16 GOOGLE_UNALIGNED_LOAD16(const void *p) {
  170. uint16 t;
  171. memcpy(&t, p, sizeof t);
  172. return t;
  173. }
  174. inline uint32 GOOGLE_UNALIGNED_LOAD32(const void *p) {
  175. uint32 t;
  176. memcpy(&t, p, sizeof t);
  177. return t;
  178. }
  179. inline uint64 GOOGLE_UNALIGNED_LOAD64(const void *p) {
  180. uint64 t;
  181. memcpy(&t, p, sizeof t);
  182. return t;
  183. }
  184. inline void GOOGLE_UNALIGNED_STORE16(void *p, uint16 v) {
  185. memcpy(p, &v, sizeof v);
  186. }
  187. inline void GOOGLE_UNALIGNED_STORE32(void *p, uint32 v) {
  188. memcpy(p, &v, sizeof v);
  189. }
  190. inline void GOOGLE_UNALIGNED_STORE64(void *p, uint64 v) {
  191. memcpy(p, &v, sizeof v);
  192. }
  193. #endif
  194. #if defined(GOOGLE_PROTOBUF_OS_NACL) \
  195. || (defined(__ANDROID__) && defined(__clang__) \
  196. && (__clang_major__ == 3 && __clang_minor__ == 8) \
  197. && (__clang_patchlevel__ < 275480))
  198. # define GOOGLE_PROTOBUF_USE_PORTABLE_LOG2
  199. #endif
  200. // The following guarantees declaration of the byte swap functions.
  201. #ifdef _MSC_VER
  202. #define bswap_16(x) _byteswap_ushort(x)
  203. #define bswap_32(x) _byteswap_ulong(x)
  204. #define bswap_64(x) _byteswap_uint64(x)
  205. #elif defined(__APPLE__)
  206. // Mac OS X / Darwin features
  207. #define bswap_16(x) OSSwapInt16(x)
  208. #define bswap_32(x) OSSwapInt32(x)
  209. #define bswap_64(x) OSSwapInt64(x)
  210. #elif !defined(__linux__) && !defined(__ANDROID__) && !defined(__CYGWIN__)
  211. #ifndef bswap_16
  212. static inline uint16 bswap_16(uint16 x) {
  213. return static_cast<uint16>(((x & 0xFF) << 8) | ((x & 0xFF00) >> 8));
  214. }
  215. #define bswap_16(x) bswap_16(x)
  216. #endif
  217. #ifndef bswap_32
  218. static inline uint32 bswap_32(uint32 x) {
  219. return (((x & 0xFF) << 24) |
  220. ((x & 0xFF00) << 8) |
  221. ((x & 0xFF0000) >> 8) |
  222. ((x & 0xFF000000) >> 24));
  223. }
  224. #define bswap_32(x) bswap_32(x)
  225. #endif
  226. #ifndef bswap_64
  227. static inline uint64 bswap_64(uint64 x) {
  228. return (((x & uint64_t{0xFFu}) << 56) |
  229. ((x & uint64_t{0xFF00u}) << 40) |
  230. ((x & uint64_t{0xFF0000u}) << 24) |
  231. ((x & uint64_t{0xFF000000u}) << 8) |
  232. ((x & uint64_t{0xFF00000000u}) >> 8) |
  233. ((x & uint64_t{0xFF0000000000u}) >> 24) |
  234. ((x & uint64_t{0xFF000000000000u}) >> 40) |
  235. ((x & uint64_t{0xFF00000000000000u}) >> 56));
  236. }
  237. #define bswap_64(x) bswap_64(x)
  238. #endif
  239. #endif
  240. // ===================================================================
  241. // from google3/util/bits/bits.h
  242. class Bits {
  243. public:
  244. static uint32 Log2FloorNonZero(uint32 n) {
  245. #if defined(__GNUC__)
  246. return 31 ^ static_cast<uint32>(__builtin_clz(n));
  247. #elif defined(_MSC_VER)
  248. unsigned long where;
  249. _BitScanReverse(&where, n);
  250. return where;
  251. #else
  252. return Log2FloorNonZero_Portable(n);
  253. #endif
  254. }
  255. static uint32 Log2FloorNonZero64(uint64 n) {
  256. // Older versions of clang run into an instruction-selection failure when
  257. // it encounters __builtin_clzll:
  258. // https://bugs.chromium.org/p/nativeclient/issues/detail?id=4395
  259. // This includes arm-nacl-clang and clang in older Android NDK versions.
  260. // To work around this, when we build with those we use the portable
  261. // implementation instead.
  262. #if defined(__GNUC__) && !defined(GOOGLE_PROTOBUF_USE_PORTABLE_LOG2)
  263. return 63 ^ static_cast<uint32>(__builtin_clzll(n));
  264. #elif defined(_MSC_VER) && defined(_M_X64)
  265. unsigned long where;
  266. _BitScanReverse64(&where, n);
  267. return where;
  268. #else
  269. return Log2FloorNonZero64_Portable(n);
  270. #endif
  271. }
  272. private:
  273. static int Log2FloorNonZero_Portable(uint32 n) {
  274. if (n == 0)
  275. return -1;
  276. int log = 0;
  277. uint32 value = n;
  278. for (int i = 4; i >= 0; --i) {
  279. int shift = (1 << i);
  280. uint32 x = value >> shift;
  281. if (x != 0) {
  282. value = x;
  283. log += shift;
  284. }
  285. }
  286. assert(value == 1);
  287. return log;
  288. }
  289. static int Log2FloorNonZero64_Portable(uint64 n) {
  290. const uint32 topbits = static_cast<uint32>(n >> 32);
  291. if (topbits == 0) {
  292. // Top bits are zero, so scan in bottom bits
  293. return static_cast<int>(Log2FloorNonZero(static_cast<uint32>(n)));
  294. } else {
  295. return 32 + static_cast<int>(Log2FloorNonZero(topbits));
  296. }
  297. }
  298. };
  299. // ===================================================================
  300. // from google3/util/endian/endian.h
  301. PROTOBUF_EXPORT uint32 ghtonl(uint32 x);
  302. class BigEndian {
  303. public:
  304. #ifdef PROTOBUF_LITTLE_ENDIAN
  305. static uint16 FromHost16(uint16 x) { return bswap_16(x); }
  306. static uint16 ToHost16(uint16 x) { return bswap_16(x); }
  307. static uint32 FromHost32(uint32 x) { return bswap_32(x); }
  308. static uint32 ToHost32(uint32 x) { return bswap_32(x); }
  309. static uint64 FromHost64(uint64 x) { return bswap_64(x); }
  310. static uint64 ToHost64(uint64 x) { return bswap_64(x); }
  311. static bool IsLittleEndian() { return true; }
  312. #else
  313. static uint16 FromHost16(uint16 x) { return x; }
  314. static uint16 ToHost16(uint16 x) { return x; }
  315. static uint32 FromHost32(uint32 x) { return x; }
  316. static uint32 ToHost32(uint32 x) { return x; }
  317. static uint64 FromHost64(uint64 x) { return x; }
  318. static uint64 ToHost64(uint64 x) { return x; }
  319. static bool IsLittleEndian() { return false; }
  320. #endif /* ENDIAN */
  321. // Functions to do unaligned loads and stores in big-endian order.
  322. static uint16 Load16(const void *p) {
  323. return ToHost16(GOOGLE_UNALIGNED_LOAD16(p));
  324. }
  325. static void Store16(void *p, uint16 v) {
  326. GOOGLE_UNALIGNED_STORE16(p, FromHost16(v));
  327. }
  328. static uint32 Load32(const void *p) {
  329. return ToHost32(GOOGLE_UNALIGNED_LOAD32(p));
  330. }
  331. static void Store32(void *p, uint32 v) {
  332. GOOGLE_UNALIGNED_STORE32(p, FromHost32(v));
  333. }
  334. static uint64 Load64(const void *p) {
  335. return ToHost64(GOOGLE_UNALIGNED_LOAD64(p));
  336. }
  337. static void Store64(void *p, uint64 v) {
  338. GOOGLE_UNALIGNED_STORE64(p, FromHost64(v));
  339. }
  340. };
  341. } // namespace protobuf
  342. } // namespace google
  343. #include <google/protobuf/port_undef.inc>
  344. #endif // GOOGLE_PROTOBUF_STUBS_PORT_H_