Utils.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_UTILS_HPP
  14. #define ZT_UTILS_HPP
  15. #include "Constants.hpp"
  16. #include <utility>
  17. #include <algorithm>
  18. #include <memory>
  19. namespace ZeroTier {
  20. namespace Utils {
  21. #ifndef __WINDOWS__
  22. #include <sys/mman.h>
  23. #endif
  24. // Macros to convert endian-ness at compile time for constants.
  25. #if __BYTE_ORDER == __LITTLE_ENDIAN
  26. #define ZT_CONST_TO_BE_UINT16(x) ((uint16_t)((uint16_t)((uint16_t)(x) << 8U) | (uint16_t)((uint16_t)(x) >> 8U)))
  27. #define ZT_CONST_TO_BE_UINT64(x) ( \
  28. (((uint64_t)(x) & 0x00000000000000ffULL) << 56U) | \
  29. (((uint64_t)(x) & 0x000000000000ff00ULL) << 40U) | \
  30. (((uint64_t)(x) & 0x0000000000ff0000ULL) << 24U) | \
  31. (((uint64_t)(x) & 0x00000000ff000000ULL) << 8U) | \
  32. (((uint64_t)(x) & 0x000000ff00000000ULL) >> 8U) | \
  33. (((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24U) | \
  34. (((uint64_t)(x) & 0x00ff000000000000ULL) >> 40U) | \
  35. (((uint64_t)(x) & 0xff00000000000000ULL) >> 56U))
  36. #else
  37. #define ZT_CONST_TO_BE_UINT16(x) ((uint16_t)(x))
  38. #define ZT_CONST_TO_BE_UINT64(x) ((uint64_t)(x))
  39. #endif
  40. #define ZT_ROR64(x, r) (((x) >> (r)) | ((x) << (64 - (r))))
  41. #define ZT_ROL64(x, r) (((x) << (r)) | ((x) >> (64 - (r))))
  42. #define ZT_ROR32(x, r) (((x) >> (r)) | ((x) << (32 - (r))))
  43. #define ZT_ROL32(x, r) (((x) << (r)) | ((x) >> (32 - (r))))
  44. #ifdef ZT_ARCH_X64
  45. struct CPUIDRegisters
  46. {
  47. CPUIDRegisters() noexcept;
  48. bool rdrand;
  49. bool aes;
  50. bool avx;
  51. bool vaes; // implies AVX
  52. bool vpclmulqdq; // implies AVX
  53. bool avx2;
  54. bool avx512f;
  55. bool sha;
  56. bool fsrm;
  57. };
  58. extern const CPUIDRegisters CPUID;
  59. #endif
  60. extern const std::bad_alloc BadAllocException;
  61. extern const std::out_of_range OutOfRangeException;
  62. /**
  63. * 256 zero bits / 32 zero bytes
  64. */
  65. extern const uint64_t ZERO256[4];
  66. /**
  67. * Hexadecimal characters 0-f
  68. */
  69. extern const char HEXCHARS[16];
  70. /**
  71. * A random integer generated at startup for Map's hash bucket calculation.
  72. */
  73. extern const uint64_t s_mapNonce;
  74. /**
  75. * Lock memory to prevent swapping out to secondary storage (if possible)
  76. *
  77. * This is used to attempt to prevent the swapping out of long-term stored secure
  78. * credentials like secret keys. It isn't supported on all platforms and may not
  79. * be absolutely guaranteed to work, but it's a countermeasure.
  80. *
  81. * @param p Memory to lock
  82. * @param l Size of memory
  83. */
  84. static ZT_INLINE void memoryLock(const void *const p, const unsigned int l) noexcept
  85. {
  86. #ifndef __WINDOWS__
  87. mlock(p, l);
  88. #endif
  89. }
  90. /**
  91. * Unlock memory locked with memoryLock()
  92. *
  93. * @param p Memory to unlock
  94. * @param l Size of memory
  95. */
  96. static ZT_INLINE void memoryUnlock(const void *const p, const unsigned int l) noexcept
  97. {
  98. #ifndef __WINDOWS__
  99. munlock(p, l);
  100. #endif
  101. }
  102. /**
  103. * Perform a time-invariant binary comparison
  104. *
  105. * @param a First binary string
  106. * @param b Second binary string
  107. * @param len Length of strings
  108. * @return True if strings are equal
  109. */
  110. bool secureEq(const void *a, const void *b, unsigned int len) noexcept;
  111. /**
  112. * Be absolutely sure to zero memory
  113. *
  114. * This uses some hacks to be totally sure the compiler does not optimize it out.
  115. *
  116. * @param ptr Memory to zero
  117. * @param len Length of memory in bytes
  118. */
  119. void burn(void *ptr, unsigned int len);
  120. /**
  121. * @param n Number to convert
  122. * @param s Buffer, at least 24 bytes in size
  123. * @return String containing 'n' in base 10 form
  124. */
  125. char *decimal(unsigned long n, char s[24]) noexcept;
  126. /**
  127. * Convert an unsigned integer into hex
  128. *
  129. * @param i Any unsigned integer
  130. * @param s Buffer to receive hex, must be at least (2*sizeof(i))+1 in size or overflow will occur.
  131. * @return Pointer to s containing hex string with trailing zero byte
  132. */
  133. char *hex(uint64_t i, char buf[17]) noexcept;
  134. /**
  135. * Decode an unsigned integer in hex format
  136. *
  137. * @param s String to decode, non-hex chars are ignored
  138. * @return Unsigned integer
  139. */
  140. uint64_t unhex(const char *s) noexcept;
  141. /**
  142. * Convert a byte array into hex
  143. *
  144. * @param d Bytes
  145. * @param l Length of bytes
  146. * @param s String buffer, must be at least (l*2)+1 in size or overflow will occur
  147. * @return Pointer to filled string buffer
  148. */
  149. char *hex(const void *d, unsigned int l, char *s) noexcept;
  150. /**
  151. * Decode a hex string
  152. *
  153. * @param h Hex C-string (non hex chars are ignored)
  154. * @param hlen Maximum length of string (will stop at terminating zero)
  155. * @param buf Output buffer
  156. * @param buflen Length of output buffer
  157. * @return Number of written bytes
  158. */
  159. unsigned int unhex(const char *h, unsigned int hlen, void *buf, unsigned int buflen) noexcept;
  160. /**
  161. * Generate secure random bytes
  162. *
  163. * This will try to use whatever OS sources of entropy are available. It's
  164. * guarded by an internal mutex so it's thread-safe.
  165. *
  166. * @param buf Buffer to fill
  167. * @param bytes Number of random bytes to generate
  168. */
  169. void getSecureRandom(void *buf, unsigned int bytes) noexcept;
  170. /**
  171. * @return Secure random 64-bit integer
  172. */
  173. uint64_t getSecureRandomU64() noexcept;
  174. /**
  175. * Encode string to base32
  176. *
  177. * @param data Binary data to encode
  178. * @param length Length of data in bytes
  179. * @param result Result buffer
  180. * @param bufSize Size of result buffer
  181. * @return Number of bytes written
  182. */
  183. int b32e(const uint8_t *data, int length, char *result, int bufSize) noexcept;
  184. /**
  185. * Decode base32 string
  186. *
  187. * @param encoded C-string in base32 format (non-base32 characters are ignored)
  188. * @param result Result buffer
  189. * @param bufSize Size of result buffer
  190. * @return Number of bytes written or -1 on error
  191. */
  192. int b32d(const char *encoded, uint8_t *result, int bufSize) noexcept;
  193. /**
  194. * Get a non-cryptographic random integer.
  195. *
  196. * This should never be used for cryptographic use cases, not even for choosing
  197. * message nonce/IV values if they should not repeat. It should only be used when
  198. * a fast and potentially "dirty" random source is needed.
  199. */
  200. uint64_t random() noexcept;
  201. /**
  202. * Perform a safe C string copy, ALWAYS null-terminating the result
  203. *
  204. * This will never ever EVER result in dest[] not being null-terminated
  205. * regardless of any input parameter (other than len==0 which is invalid).
  206. *
  207. * @param dest Destination buffer (must not be NULL)
  208. * @param len Length of dest[] (if zero, false is returned and nothing happens)
  209. * @param src Source string (if NULL, dest will receive a zero-length string and true is returned)
  210. * @return True on success, false on overflow (buffer will still be 0-terminated)
  211. */
  212. bool scopy(char *dest, unsigned int len, const char *src) noexcept;
  213. /**
  214. * Mix bits in a 64-bit integer (non-cryptographic, for hash tables)
  215. *
  216. * https://nullprogram.com/blog/2018/07/31/
  217. *
  218. * @param x Integer to mix
  219. * @return Hashed value
  220. */
  221. static ZT_INLINE uint64_t hash64(uint64_t x) noexcept
  222. {
  223. x ^= x >> 30U;
  224. x *= 0xbf58476d1ce4e5b9ULL;
  225. x ^= x >> 27U;
  226. x *= 0x94d049bb133111ebULL;
  227. x ^= x >> 31U;
  228. return x;
  229. }
  230. /**
  231. * Mix bits in a 32-bit integer (non-cryptographic, for hash tables)
  232. *
  233. * https://nullprogram.com/blog/2018/07/31/
  234. *
  235. * @param x Integer to mix
  236. * @return Hashed value
  237. */
  238. static ZT_INLINE uint32_t hash32(uint32_t x) noexcept
  239. {
  240. x ^= x >> 16U;
  241. x *= 0x7feb352dU;
  242. x ^= x >> 15U;
  243. x *= 0x846ca68bU;
  244. x ^= x >> 16U;
  245. return x;
  246. }
  247. /**
  248. * Check if a buffer's contents are all zero
  249. */
  250. static ZT_INLINE bool allZero(const void *const b, unsigned int l) noexcept
  251. {
  252. const uint8_t *p = reinterpret_cast<const uint8_t *>(b);
  253. #ifndef ZT_NO_UNALIGNED_ACCESS
  254. while (l >= 8) {
  255. if (*reinterpret_cast<const uint64_t *>(p) != 0)
  256. return false;
  257. p += 8;
  258. l -= 8;
  259. }
  260. #endif
  261. for (unsigned int i = 0; i < l; ++i) {
  262. if (reinterpret_cast<const uint8_t *>(p)[i] != 0)
  263. return false;
  264. }
  265. return true;
  266. }
  267. /**
  268. * Wrapper around reentrant strtok functions, which differ in name by platform
  269. *
  270. * @param str String to tokenize or NULL for subsequent calls
  271. * @param delim Delimiter
  272. * @param saveptr Pointer to pointer where function can save state
  273. * @return Next token or NULL if none
  274. */
  275. static ZT_INLINE char *stok(char *str, const char *delim, char **saveptr) noexcept
  276. {
  277. #ifdef __WINDOWS__
  278. return strtok_s(str,delim,saveptr);
  279. #else
  280. return strtok_r(str, delim, saveptr);
  281. #endif
  282. }
  283. static ZT_INLINE unsigned int strToUInt(const char *s) noexcept
  284. {
  285. return (unsigned int)strtoul(s, nullptr, 10);
  286. }
  287. static ZT_INLINE unsigned long long hexStrToU64(const char *s) noexcept
  288. {
  289. #ifdef __WINDOWS__
  290. return (unsigned long long)_strtoui64(s,nullptr,16);
  291. #else
  292. return strtoull(s, nullptr, 16);
  293. #endif
  294. }
  295. /**
  296. * Compute 32-bit FNV-1a checksum
  297. *
  298. * See: http://www.isthe.com/chongo/tech/comp/fnv/
  299. *
  300. * @param data Data to checksum
  301. * @param len Length of data
  302. * @return FNV1a checksum
  303. */
  304. static ZT_INLINE uint32_t fnv1a32(const void *const data, const unsigned int len) noexcept
  305. {
  306. uint32_t h = 0x811c9dc5;
  307. const uint32_t p = 0x01000193;
  308. for (unsigned int i = 0; i < len; ++i)
  309. h = (h ^ (uint32_t)reinterpret_cast<const uint8_t *>(data)[i]) * p;
  310. return h;
  311. }
  312. #ifdef __GNUC__
  313. static ZT_INLINE unsigned int countBits(const uint8_t v) noexcept
  314. { return (unsigned int)__builtin_popcount((unsigned int)v); }
  315. static ZT_INLINE unsigned int countBits(const uint16_t v) noexcept
  316. { return (unsigned int)__builtin_popcount((unsigned int)v); }
  317. static ZT_INLINE unsigned int countBits(const uint32_t v) noexcept
  318. { return (unsigned int)__builtin_popcountl((unsigned long)v); }
  319. static ZT_INLINE unsigned int countBits(const uint64_t v) noexcept
  320. { return (unsigned int)__builtin_popcountll((unsigned long long)v); }
  321. #else
  322. template<typename T>
  323. static ZT_INLINE unsigned int countBits(T v) noexcept
  324. {
  325. v = v - ((v >> 1) & (T)~(T)0/3);
  326. v = (v & (T)~(T)0/15*3) + ((v >> 2) & (T)~(T)0/15*3);
  327. v = (v + (v >> 4)) & (T)~(T)0/255*15;
  328. return (unsigned int)((v * ((~((T)0))/((T)255))) >> ((sizeof(T) - 1) * 8));
  329. }
  330. #endif
  331. /**
  332. * Unconditionally swap bytes regardless of host byte order
  333. *
  334. * @param n Integer to swap
  335. * @return Integer with bytes reversed
  336. */
  337. static ZT_INLINE uint64_t swapBytes(const uint64_t n) noexcept
  338. {
  339. #ifdef __GNUC__
  340. return __builtin_bswap64(n);
  341. #else
  342. #ifdef _MSC_VER
  343. return (uint64_t)_byteswap_uint64((unsigned __int64)n);
  344. #else
  345. return (
  346. ((n & 0x00000000000000ffULL) << 56) |
  347. ((n & 0x000000000000ff00ULL) << 40) |
  348. ((n & 0x0000000000ff0000ULL) << 24) |
  349. ((n & 0x00000000ff000000ULL) << 8) |
  350. ((n & 0x000000ff00000000ULL) >> 8) |
  351. ((n & 0x0000ff0000000000ULL) >> 24) |
  352. ((n & 0x00ff000000000000ULL) >> 40) |
  353. ((n & 0xff00000000000000ULL) >> 56)
  354. );
  355. #endif
  356. #endif
  357. }
  358. /**
  359. * Unconditionally swap bytes regardless of host byte order
  360. *
  361. * @param n Integer to swap
  362. * @return Integer with bytes reversed
  363. */
  364. static ZT_INLINE uint32_t swapBytes(const uint32_t n) noexcept
  365. {
  366. #if defined(__GNUC__)
  367. return __builtin_bswap32(n);
  368. #else
  369. #ifdef _MSC_VER
  370. return (uint32_t)_byteswap_ulong((unsigned long)n);
  371. #else
  372. return htonl(n);
  373. #endif
  374. #endif
  375. }
  376. /**
  377. * Unconditionally swap bytes regardless of host byte order
  378. *
  379. * @param n Integer to swap
  380. * @return Integer with bytes reversed
  381. */
  382. static ZT_INLINE uint16_t swapBytes(const uint16_t n) noexcept
  383. {
  384. #if defined(__GNUC__)
  385. return __builtin_bswap16(n);
  386. #else
  387. #ifdef _MSC_VER
  388. return (uint16_t)_byteswap_ushort((unsigned short)n);
  389. #else
  390. return htons(n);
  391. #endif
  392. #endif
  393. }
  394. // These are helper adapters to load and swap integer types special cased by size
  395. // to work with all typedef'd variants, signed/unsigned, etc.
  396. template< typename I, unsigned int S >
  397. class _swap_bytes_bysize;
  398. template< typename I >
  399. class _swap_bytes_bysize< I, 1 >
  400. {
  401. public:
  402. static ZT_INLINE I s(const I n) noexcept
  403. { return n; }
  404. };
  405. template< typename I >
  406. class _swap_bytes_bysize< I, 2 >
  407. {
  408. public:
  409. static ZT_INLINE I s(const I n) noexcept
  410. { return (I)swapBytes((uint16_t)n); }
  411. };
  412. template< typename I >
  413. class _swap_bytes_bysize< I, 4 >
  414. {
  415. public:
  416. static ZT_INLINE I s(const I n) noexcept
  417. { return (I)swapBytes((uint32_t)n); }
  418. };
  419. template< typename I >
  420. class _swap_bytes_bysize< I, 8 >
  421. {
  422. public:
  423. static ZT_INLINE I s(const I n) noexcept
  424. { return (I)swapBytes((uint64_t)n); }
  425. };
  426. template< typename I, unsigned int S >
  427. class _load_be_bysize;
  428. template< typename I >
  429. class _load_be_bysize< I, 1 >
  430. {
  431. public:
  432. static ZT_INLINE I l(const uint8_t *const p) noexcept
  433. { return p[0]; }
  434. };
  435. template< typename I >
  436. class _load_be_bysize< I, 2 >
  437. {
  438. public:
  439. static ZT_INLINE I l(const uint8_t *const p) noexcept
  440. { return (I)(((unsigned int)p[0] << 8U) | (unsigned int)p[1]); }
  441. };
  442. template< typename I >
  443. class _load_be_bysize< I, 4 >
  444. {
  445. public:
  446. static ZT_INLINE I l(const uint8_t *const p) noexcept
  447. { return (I)(((uint32_t)p[0] << 24U) | ((uint32_t)p[1] << 16U) | ((uint32_t)p[2] << 8U) | (uint32_t)p[3]); }
  448. };
  449. template< typename I >
  450. class _load_be_bysize< I, 8 >
  451. {
  452. public:
  453. static ZT_INLINE I l(const uint8_t *const p) noexcept
  454. { return (I)(((uint64_t)p[0] << 56U) | ((uint64_t)p[1] << 48U) | ((uint64_t)p[2] << 40U) | ((uint64_t)p[3] << 32U) | ((uint64_t)p[4] << 24U) | ((uint64_t)p[5] << 16U) | ((uint64_t)p[6] << 8U) | (uint64_t)p[7]); }
  455. };
  456. template< typename I, unsigned int S >
  457. class _load_le_bysize;
  458. template< typename I >
  459. class _load_le_bysize< I, 1 >
  460. {
  461. public:
  462. static ZT_INLINE I l(const uint8_t *const p) noexcept
  463. { return p[0]; }
  464. };
  465. template< typename I >
  466. class _load_le_bysize< I, 2 >
  467. {
  468. public:
  469. static ZT_INLINE I l(const uint8_t *const p) noexcept
  470. { return (I)((unsigned int)p[0] | ((unsigned int)p[1] << 8U)); }
  471. };
  472. template< typename I >
  473. class _load_le_bysize< I, 4 >
  474. {
  475. public:
  476. static ZT_INLINE I l(const uint8_t *const p) noexcept
  477. { return (I)((uint32_t)p[0] | ((uint32_t)p[1] << 8U) | ((uint32_t)p[2] << 16U) | ((uint32_t)p[3] << 24U)); }
  478. };
  479. template< typename I >
  480. class _load_le_bysize< I, 8 >
  481. {
  482. public:
  483. static ZT_INLINE I l(const uint8_t *const p) noexcept
  484. { return (I)((uint64_t)p[0] | ((uint64_t)p[1] << 8U) | ((uint64_t)p[2] << 16U) | ((uint64_t)p[3] << 24U) | ((uint64_t)p[4] << 32U) | ((uint64_t)p[5] << 40U) | ((uint64_t)p[6] << 48U) | ((uint64_t)p[7]) << 56U); }
  485. };
  486. /**
  487. * Convert any signed or unsigned integer type to big-endian ("network") byte order
  488. *
  489. * @tparam I Integer type (usually inferred)
  490. * @param n Value to convert
  491. * @return Value in big-endian order
  492. */
  493. template< typename I >
  494. static ZT_INLINE I hton(const I n) noexcept
  495. {
  496. #if __BYTE_ORDER == __LITTLE_ENDIAN
  497. return _swap_bytes_bysize< I, sizeof(I) >::s(n);
  498. #else
  499. return n;
  500. #endif
  501. }
  502. /**
  503. * Convert any signed or unsigned integer type to host byte order from big-endian ("network") byte order
  504. *
  505. * @tparam I Integer type (usually inferred)
  506. * @param n Value to convert
  507. * @return Value in host byte order
  508. */
  509. template< typename I >
  510. static ZT_INLINE I ntoh(const I n) noexcept
  511. {
  512. #if __BYTE_ORDER == __LITTLE_ENDIAN
  513. return _swap_bytes_bysize< I, sizeof(I) >::s(n);
  514. #else
  515. return n;
  516. #endif
  517. }
  518. /**
  519. * Copy bits from memory into an integer type without modifying their order
  520. *
  521. * @tparam I Type to load
  522. * @param p Byte stream, must be at least sizeof(I) in size
  523. * @return Loaded raw integer
  524. */
  525. template< typename I >
  526. static ZT_INLINE I loadAsIsEndian(const void *const p) noexcept
  527. {
  528. #ifdef ZT_NO_UNALIGNED_ACCESS
  529. I tmp;
  530. for(int i=0;i<(int)sizeof(I);++i)
  531. reinterpret_cast<uint8_t *>(&tmp)[i] = reinterpret_cast<const uint8_t *>(p)[i];
  532. return tmp;
  533. #else
  534. return *reinterpret_cast<const I *>(p);
  535. #endif
  536. }
  537. /**
  538. * Copy bits from memory into an integer type without modifying their order
  539. *
  540. * @tparam I Type to store
  541. * @param p Byte array (must be at least sizeof(I))
  542. * @param i Integer to store
  543. */
  544. template< typename I >
  545. static ZT_INLINE void storeAsIsEndian(void *const p, const I i) noexcept
  546. {
  547. #ifdef ZT_NO_UNALIGNED_ACCESS
  548. for(unsigned int k=0;k<sizeof(I);++k)
  549. reinterpret_cast<uint8_t *>(p)[k] = reinterpret_cast<const uint8_t *>(&i)[k];
  550. #else
  551. *reinterpret_cast<I *>(p) = i;
  552. #endif
  553. }
  554. /**
  555. * Decode a big-endian value from a byte stream
  556. *
  557. * @tparam I Type to decode (should be unsigned e.g. uint32_t or uint64_t)
  558. * @param p Byte stream, must be at least sizeof(I) in size
  559. * @return Decoded integer
  560. */
  561. template< typename I >
  562. static ZT_INLINE I loadBigEndian(const void *const p) noexcept
  563. {
  564. #ifdef ZT_NO_UNALIGNED_ACCESS
  565. return _load_be_bysize<I,sizeof(I)>::l(reinterpret_cast<const uint8_t *>(p));
  566. #else
  567. return ntoh(*reinterpret_cast<const I *>(p));
  568. #endif
  569. }
  570. /**
  571. * Save an integer in big-endian format
  572. *
  573. * @tparam I Integer type to store (usually inferred)
  574. * @param p Byte stream to write (must be at least sizeof(I))
  575. * #param i Integer to write
  576. */
  577. template< typename I >
  578. static ZT_INLINE void storeBigEndian(void *const p, I i) noexcept
  579. {
  580. #ifdef ZT_NO_UNALIGNED_ACCESS
  581. storeAsIsEndian(p,hton(i));
  582. #else
  583. *reinterpret_cast<I *>(p) = hton(i);
  584. #endif
  585. }
  586. /**
  587. * Decode a little-endian value from a byte stream
  588. *
  589. * @tparam I Type to decode
  590. * @param p Byte stream, must be at least sizeof(I) in size
  591. * @return Decoded integer
  592. */
  593. template< typename I >
  594. static ZT_INLINE I loadLittleEndian(const void *const p) noexcept
  595. {
  596. #if __BYTE_ORDER == __BIG_ENDIAN || defined(ZT_NO_UNALIGNED_ACCESS)
  597. return _load_le_bysize<I,sizeof(I)>::l(reinterpret_cast<const uint8_t *>(p));
  598. #else
  599. return *reinterpret_cast<const I *>(p);
  600. #endif
  601. }
  602. /**
  603. * Save an integer in little-endian format
  604. *
  605. * @tparam I Integer type to store (usually inferred)
  606. * @param p Byte stream to write (must be at least sizeof(I))
  607. * #param i Integer to write
  608. */
  609. template< typename I >
  610. static ZT_INLINE void storeLittleEndian(void *const p, const I i) noexcept
  611. {
  612. #if __BYTE_ORDER == __BIG_ENDIAN
  613. storeAsIsEndian(p,_swap_bytes_bysize<I,sizeof(I)>::s(i));
  614. #else
  615. #ifdef ZT_NO_UNALIGNED_ACCESS
  616. storeAsIsEndian(p,i);
  617. #else
  618. *reinterpret_cast<I *>(p) = i;
  619. #endif
  620. #endif
  621. }
  622. /**
  623. * Copy memory block whose size is known at compile time.
  624. *
  625. * @tparam L Size of memory
  626. * @param dest Destination memory
  627. * @param src Source memory
  628. */
  629. template< unsigned int L >
  630. static ZT_INLINE void copy(void *const dest, const void *const src) noexcept
  631. {
  632. #ifdef ZT_ARCH_X64
  633. uint8_t *volatile d = reinterpret_cast<uint8_t *>(dest);
  634. const uint8_t *s = reinterpret_cast<const uint8_t *>(src);
  635. for (unsigned int i = 0; i < (L >> 6U); ++i) {
  636. __m128i x0 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(s));
  637. __m128i x1 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(s + 16));
  638. __m128i x2 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(s + 32));
  639. __m128i x3 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(s + 48));
  640. s += 64;
  641. _mm_storeu_si128(reinterpret_cast<__m128i *>(d), x0);
  642. _mm_storeu_si128(reinterpret_cast<__m128i *>(d + 16), x1);
  643. _mm_storeu_si128(reinterpret_cast<__m128i *>(d + 32), x2);
  644. _mm_storeu_si128(reinterpret_cast<__m128i *>(d + 48), x3);
  645. d += 64;
  646. }
  647. if ((L & 32U) != 0) {
  648. __m128i x0 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(s));
  649. __m128i x1 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(s + 16));
  650. s += 32;
  651. _mm_storeu_si128(reinterpret_cast<__m128i *>(d), x0);
  652. _mm_storeu_si128(reinterpret_cast<__m128i *>(d + 16), x1);
  653. d += 32;
  654. }
  655. if ((L & 16U) != 0) {
  656. __m128i x0 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(s));
  657. s += 16;
  658. _mm_storeu_si128(reinterpret_cast<__m128i *>(d), x0);
  659. d += 16;
  660. }
  661. if ((L & 8U) != 0) {
  662. *reinterpret_cast<volatile uint64_t *>(d) = *reinterpret_cast<const uint64_t *>(s);
  663. s += 8;
  664. d += 8;
  665. }
  666. if ((L & 4U) != 0) {
  667. *reinterpret_cast<volatile uint32_t *>(d) = *reinterpret_cast<const uint32_t *>(s);
  668. s += 4;
  669. d += 4;
  670. }
  671. if ((L & 2U) != 0) {
  672. *reinterpret_cast<volatile uint16_t *>(d) = *reinterpret_cast<const uint16_t *>(s);
  673. s += 2;
  674. d += 2;
  675. }
  676. if ((L & 1U) != 0) {
  677. *d = *s;
  678. }
  679. #else
  680. memcpy(dest,src,L);
  681. #endif
  682. }
  683. /**
  684. * Copy memory block whose size is known at run time
  685. *
  686. * @param dest Destination memory
  687. * @param src Source memory
  688. * @param len Bytes to copy
  689. */
  690. static ZT_INLINE void copy(void *const dest, const void *const src, unsigned int len) noexcept
  691. { memcpy(dest, src, len); }
  692. /**
  693. * Zero memory block whose size is known at compile time
  694. *
  695. * @tparam L Size in bytes
  696. * @param dest Memory to zero
  697. */
  698. template< unsigned int L >
  699. static ZT_INLINE void zero(void *const dest) noexcept
  700. {
  701. #ifdef ZT_ARCH_X64
  702. uint8_t *volatile d = reinterpret_cast<uint8_t *>(dest);
  703. __m128i z = _mm_setzero_si128();
  704. for (unsigned int i = 0; i < (L >> 6U); ++i) {
  705. _mm_storeu_si128(reinterpret_cast<__m128i *>(d), z);
  706. _mm_storeu_si128(reinterpret_cast<__m128i *>(d + 16), z);
  707. _mm_storeu_si128(reinterpret_cast<__m128i *>(d + 32), z);
  708. _mm_storeu_si128(reinterpret_cast<__m128i *>(d + 48), z);
  709. d += 64;
  710. }
  711. if ((L & 32U) != 0) {
  712. _mm_storeu_si128(reinterpret_cast<__m128i *>(d), z);
  713. _mm_storeu_si128(reinterpret_cast<__m128i *>(d + 16), z);
  714. d += 32;
  715. }
  716. if ((L & 16U) != 0) {
  717. _mm_storeu_si128(reinterpret_cast<__m128i *>(d), z);
  718. d += 16;
  719. }
  720. if ((L & 8U) != 0) {
  721. *reinterpret_cast<volatile uint64_t *>(d) = 0;
  722. d += 8;
  723. }
  724. if ((L & 4U) != 0) {
  725. *reinterpret_cast<volatile uint32_t *>(d) = 0;
  726. d += 4;
  727. }
  728. if ((L & 2U) != 0) {
  729. *reinterpret_cast<volatile uint16_t *>(d) = 0;
  730. d += 2;
  731. }
  732. if ((L & 1U) != 0) {
  733. *d = 0;
  734. }
  735. #else
  736. memset(dest,0,L);
  737. #endif
  738. }
  739. /**
  740. * Zero memory block whose size is known at run time
  741. *
  742. * @param dest Memory to zero
  743. * @param len Size in bytes
  744. */
  745. static ZT_INLINE void zero(void *const dest, const unsigned int len) noexcept
  746. { memset(dest, 0, len); }
  747. /**
  748. * Simple malloc/free based C++ STL allocator.
  749. *
  750. * This is used to make sure our containers don't use weird libc++
  751. * allocators but instead use whatever malloc() is, which in turn
  752. * can be overridden by things like jemaclloc or tcmalloc.
  753. *
  754. * @tparam T Allocated type
  755. */
  756. template< typename T >
  757. struct Mallocator
  758. {
  759. typedef size_t size_type;
  760. typedef ptrdiff_t difference_type;
  761. typedef T *pointer;
  762. typedef const T *const_pointer;
  763. typedef T &reference;
  764. typedef const T &const_reference;
  765. typedef T value_type;
  766. template< class U >
  767. struct rebind
  768. {
  769. typedef Mallocator< U > other;
  770. };
  771. ZT_INLINE Mallocator() noexcept
  772. {}
  773. ZT_INLINE Mallocator(const Mallocator &) noexcept
  774. {}
  775. template< class U >
  776. ZT_INLINE Mallocator(const Mallocator< U > &) noexcept
  777. {}
  778. ZT_INLINE ~Mallocator() noexcept
  779. {}
  780. ZT_INLINE pointer allocate(size_type s, void const * = nullptr)
  781. {
  782. if (0 == s)
  783. return nullptr;
  784. pointer temp = (pointer)malloc(s * sizeof(T));
  785. if (temp == nullptr)
  786. throw BadAllocException;
  787. return temp;
  788. }
  789. ZT_INLINE pointer address(reference x) const
  790. { return &x; }
  791. ZT_INLINE const_pointer address(const_reference x) const
  792. { return &x; }
  793. ZT_INLINE void deallocate(pointer p, size_type)
  794. { free(p); }
  795. ZT_INLINE size_type max_size() const noexcept
  796. { return std::numeric_limits< size_t >::max() / sizeof(T); }
  797. ZT_INLINE void construct(pointer p, const T &val)
  798. { new((void *)p) T(val); }
  799. ZT_INLINE void destroy(pointer p)
  800. { p->~T(); }
  801. constexpr bool operator==(const Mallocator &) const noexcept
  802. { return true; }
  803. constexpr bool operator!=(const Mallocator &) const noexcept
  804. { return false; }
  805. };
  806. } // namespace Utils
  807. } // namespace ZeroTier
  808. #endif