Utils.hpp 21 KB

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