Utils.hpp 23 KB

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