Utils.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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(reinterpret_cast<LPVOID>(const_cast<void*>(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(reinterpret_cast<LPVOID>(const_cast<void*>(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 a few tricks to make sure the compiler doesn't optimize it
  121. * out, including passing the memory as volatile.
  122. *
  123. * @param ptr Memory to zero
  124. * @param len Length of memory in bytes
  125. */
  126. void burn(volatile void *ptr, unsigned int len);
  127. /**
  128. * @param n Number to convert
  129. * @param s Buffer, at least 24 bytes in size
  130. * @return String containing 'n' in base 10 form
  131. */
  132. char *decimal(unsigned long n, char s[24]) noexcept;
  133. /**
  134. * Convert an unsigned integer into hex
  135. *
  136. * @param i Any unsigned integer
  137. * @param s Buffer to receive hex, must be at least (2*sizeof(i))+1 in size or overflow will occur.
  138. * @return Pointer to s containing hex string with trailing zero byte
  139. */
  140. char *hex(uint64_t i, char buf[17]) noexcept;
  141. /**
  142. * Decode an unsigned integer in hex format
  143. *
  144. * @param s String to decode, non-hex chars are ignored
  145. * @return Unsigned integer
  146. */
  147. uint64_t unhex(const char *s) noexcept;
  148. /**
  149. * Convert a byte array into hex
  150. *
  151. * @param d Bytes
  152. * @param l Length of bytes
  153. * @param s String buffer, must be at least (l*2)+1 in size or overflow will occur
  154. * @return Pointer to filled string buffer
  155. */
  156. char *hex(const void *d, unsigned int l, char *s) noexcept;
  157. /**
  158. * Decode a hex string
  159. *
  160. * @param h Hex C-string (non hex chars are ignored)
  161. * @param hlen Maximum length of string (will stop at terminating zero)
  162. * @param buf Output buffer
  163. * @param buflen Length of output buffer
  164. * @return Number of written bytes
  165. */
  166. unsigned int unhex(const char *h, unsigned int hlen, void *buf, unsigned int buflen) noexcept;
  167. /**
  168. * Generate secure random bytes
  169. *
  170. * This will try to use whatever OS sources of entropy are available. It's
  171. * guarded by an internal mutex so it's thread-safe.
  172. *
  173. * @param buf Buffer to fill
  174. * @param bytes Number of random bytes to generate
  175. */
  176. void getSecureRandom(void *buf, unsigned int bytes) noexcept;
  177. /**
  178. * @return Secure random 64-bit integer
  179. */
  180. uint64_t getSecureRandomU64() noexcept;
  181. /**
  182. * Encode string to base32
  183. *
  184. * @param data Binary data to encode
  185. * @param length Length of data in bytes
  186. * @param result Result buffer
  187. * @param bufSize Size of result buffer
  188. * @return Number of bytes written
  189. */
  190. int b32e(const uint8_t *data, int length, char *result, int bufSize) noexcept;
  191. /**
  192. * Decode base32 string
  193. *
  194. * @param encoded C-string in base32 format (non-base32 characters are ignored)
  195. * @param result Result buffer
  196. * @param bufSize Size of result buffer
  197. * @return Number of bytes written or -1 on error
  198. */
  199. int b32d(const char *encoded, uint8_t *result, int bufSize) noexcept;
  200. /**
  201. * Get a non-cryptographic random integer.
  202. *
  203. * This should never be used for cryptographic use cases, not even for choosing
  204. * message nonce/IV values if they should not repeat. It should only be used when
  205. * a fast and potentially "dirty" random source is needed.
  206. */
  207. uint64_t random() noexcept;
  208. /**
  209. * Perform a safe C string copy, ALWAYS null-terminating the result
  210. *
  211. * This will never ever EVER result in dest[] not being null-terminated
  212. * regardless of any input parameter (other than len==0 which is invalid).
  213. *
  214. * @param dest Destination buffer (must not be NULL)
  215. * @param len Length of dest[] (if zero, false is returned and nothing happens)
  216. * @param src Source string (if NULL, dest will receive a zero-length string and true is returned)
  217. * @return True on success, false on overflow (buffer will still be 0-terminated)
  218. */
  219. bool scopy(char *dest, unsigned int len, const char *src) noexcept;
  220. /**
  221. * Mix bits in a 64-bit integer (non-cryptographic, for hash tables)
  222. *
  223. * https://nullprogram.com/blog/2018/07/31/
  224. *
  225. * @param x Integer to mix
  226. * @return Hashed value
  227. */
  228. static ZT_INLINE uint64_t hash64(uint64_t x) noexcept
  229. {
  230. x ^= x >> 30U;
  231. x *= 0xbf58476d1ce4e5b9ULL;
  232. x ^= x >> 27U;
  233. x *= 0x94d049bb133111ebULL;
  234. x ^= x >> 31U;
  235. return x;
  236. }
  237. /**
  238. * Mix bits in a 32-bit integer (non-cryptographic, for hash tables)
  239. *
  240. * https://nullprogram.com/blog/2018/07/31/
  241. *
  242. * @param x Integer to mix
  243. * @return Hashed value
  244. */
  245. static ZT_INLINE uint32_t hash32(uint32_t x) noexcept
  246. {
  247. x ^= x >> 16U;
  248. x *= 0x7feb352dU;
  249. x ^= x >> 15U;
  250. x *= 0x846ca68bU;
  251. x ^= x >> 16U;
  252. return x;
  253. }
  254. /**
  255. * Check if a buffer's contents are all zero
  256. */
  257. static ZT_INLINE bool allZero(const void *const b, unsigned int l) noexcept
  258. {
  259. const uint8_t *p = reinterpret_cast<const uint8_t *>(b);
  260. #ifndef ZT_NO_UNALIGNED_ACCESS
  261. while (l >= 8) {
  262. if (*reinterpret_cast<const uint64_t *>(p) != 0)
  263. return false;
  264. p += 8;
  265. l -= 8;
  266. }
  267. #endif
  268. for (unsigned int i = 0; i < l; ++i) {
  269. if (reinterpret_cast<const uint8_t *>(p)[i] != 0)
  270. return false;
  271. }
  272. return true;
  273. }
  274. /**
  275. * Wrapper around reentrant strtok functions, which differ in name by platform
  276. *
  277. * @param str String to tokenize or NULL for subsequent calls
  278. * @param delim Delimiter
  279. * @param saveptr Pointer to pointer where function can save state
  280. * @return Next token or NULL if none
  281. */
  282. static ZT_INLINE char *stok(char *str, const char *delim, char **saveptr) noexcept
  283. {
  284. #ifdef __WINDOWS__
  285. return strtok_s(str,delim,saveptr);
  286. #else
  287. return strtok_r(str, delim, saveptr);
  288. #endif
  289. }
  290. static ZT_INLINE unsigned int strToUInt(const char *s) noexcept
  291. { return (unsigned int)strtoul(s, nullptr, 10); }
  292. static ZT_INLINE unsigned long long hexStrToU64(const char *s) noexcept
  293. {
  294. #ifdef __WINDOWS__
  295. return (unsigned long long)_strtoui64(s,nullptr,16);
  296. #else
  297. return strtoull(s, nullptr, 16);
  298. #endif
  299. }
  300. /**
  301. * Compute 32-bit FNV-1a checksum
  302. *
  303. * See: http://www.isthe.com/chongo/tech/comp/fnv/
  304. *
  305. * @param data Data to checksum
  306. * @param len Length of data
  307. * @return FNV1a checksum
  308. */
  309. static ZT_INLINE uint32_t fnv1a32(const void *const data, const unsigned int len) noexcept
  310. {
  311. uint32_t h = 0x811c9dc5;
  312. const uint32_t p = 0x01000193;
  313. for (unsigned int i = 0; i < len; ++i)
  314. h = (h ^ (uint32_t)reinterpret_cast<const uint8_t *>(data)[i]) * p;
  315. return h;
  316. }
  317. #ifdef __GNUC__
  318. static ZT_INLINE unsigned int countBits(const uint8_t v) noexcept
  319. { return (unsigned int)__builtin_popcount((unsigned int)v); }
  320. static ZT_INLINE unsigned int countBits(const uint16_t v) noexcept
  321. { return (unsigned int)__builtin_popcount((unsigned int)v); }
  322. static ZT_INLINE unsigned int countBits(const uint32_t v) noexcept
  323. { return (unsigned int)__builtin_popcountl((unsigned long)v); }
  324. static ZT_INLINE unsigned int countBits(const uint64_t v) noexcept
  325. { return (unsigned int)__builtin_popcountll((unsigned long long)v); }
  326. #else
  327. template<typename T>
  328. static ZT_INLINE unsigned int countBits(T v) noexcept
  329. {
  330. v = v - ((v >> 1) & (T)~(T)0/3);
  331. v = (v & (T)~(T)0/15*3) + ((v >> 2) & (T)~(T)0/15*3);
  332. v = (v + (v >> 4)) & (T)~(T)0/255*15;
  333. return (unsigned int)((v * ((~((T)0))/((T)255))) >> ((sizeof(T) - 1) * 8));
  334. }
  335. #endif
  336. /**
  337. * Unconditionally swap bytes regardless of host byte order
  338. *
  339. * @param n Integer to swap
  340. * @return Integer with bytes reversed
  341. */
  342. static ZT_INLINE uint64_t swapBytes(const uint64_t n) noexcept
  343. {
  344. #ifdef __GNUC__
  345. return __builtin_bswap64(n);
  346. #else
  347. #ifdef _MSC_VER
  348. return (uint64_t)_byteswap_uint64((unsigned __int64)n);
  349. #else
  350. return (
  351. ((n & 0x00000000000000ffULL) << 56) |
  352. ((n & 0x000000000000ff00ULL) << 40) |
  353. ((n & 0x0000000000ff0000ULL) << 24) |
  354. ((n & 0x00000000ff000000ULL) << 8) |
  355. ((n & 0x000000ff00000000ULL) >> 8) |
  356. ((n & 0x0000ff0000000000ULL) >> 24) |
  357. ((n & 0x00ff000000000000ULL) >> 40) |
  358. ((n & 0xff00000000000000ULL) >> 56)
  359. );
  360. #endif
  361. #endif
  362. }
  363. /**
  364. * Unconditionally swap bytes regardless of host byte order
  365. *
  366. * @param n Integer to swap
  367. * @return Integer with bytes reversed
  368. */
  369. static ZT_INLINE uint32_t swapBytes(const uint32_t n) noexcept
  370. {
  371. #if defined(__GNUC__)
  372. return __builtin_bswap32(n);
  373. #else
  374. #ifdef _MSC_VER
  375. return (uint32_t)_byteswap_ulong((unsigned long)n);
  376. #else
  377. return htonl(n);
  378. #endif
  379. #endif
  380. }
  381. /**
  382. * Unconditionally swap bytes regardless of host byte order
  383. *
  384. * @param n Integer to swap
  385. * @return Integer with bytes reversed
  386. */
  387. static ZT_INLINE uint16_t swapBytes(const uint16_t n) noexcept
  388. {
  389. #if defined(__GNUC__)
  390. return __builtin_bswap16(n);
  391. #else
  392. #ifdef _MSC_VER
  393. return (uint16_t)_byteswap_ushort((unsigned short)n);
  394. #else
  395. return htons(n);
  396. #endif
  397. #endif
  398. }
  399. // These are helper adapters to load and swap integer types special cased by size
  400. // to work with all typedef'd variants, signed/unsigned, etc.
  401. template< typename I, unsigned int S >
  402. class _swap_bytes_bysize;
  403. template< typename I >
  404. class _swap_bytes_bysize< I, 1 >
  405. {
  406. public:
  407. static ZT_INLINE I s(const I n) noexcept
  408. { return n; }
  409. };
  410. template< typename I >
  411. class _swap_bytes_bysize< I, 2 >
  412. {
  413. public:
  414. static ZT_INLINE I s(const I n) noexcept
  415. { return (I)swapBytes((uint16_t)n); }
  416. };
  417. template< typename I >
  418. class _swap_bytes_bysize< I, 4 >
  419. {
  420. public:
  421. static ZT_INLINE I s(const I n) noexcept
  422. { return (I)swapBytes((uint32_t)n); }
  423. };
  424. template< typename I >
  425. class _swap_bytes_bysize< I, 8 >
  426. {
  427. public:
  428. static ZT_INLINE I s(const I n) noexcept
  429. { return (I)swapBytes((uint64_t)n); }
  430. };
  431. template< typename I, unsigned int S >
  432. class _load_be_bysize;
  433. template< typename I >
  434. class _load_be_bysize< I, 1 >
  435. {
  436. public:
  437. static ZT_INLINE I l(const uint8_t *const p) noexcept
  438. { return p[0]; }
  439. };
  440. template< typename I >
  441. class _load_be_bysize< I, 2 >
  442. {
  443. public:
  444. static ZT_INLINE I l(const uint8_t *const p) noexcept
  445. { return (I)(((unsigned int)p[0] << 8U) | (unsigned int)p[1]); }
  446. };
  447. template< typename I >
  448. class _load_be_bysize< I, 4 >
  449. {
  450. public:
  451. static ZT_INLINE I l(const uint8_t *const p) noexcept
  452. { return (I)(((uint32_t)p[0] << 24U) | ((uint32_t)p[1] << 16U) | ((uint32_t)p[2] << 8U) | (uint32_t)p[3]); }
  453. };
  454. template< typename I >
  455. class _load_be_bysize< I, 8 >
  456. {
  457. public:
  458. static ZT_INLINE I l(const uint8_t *const p) noexcept
  459. { 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]); }
  460. };
  461. template< typename I, unsigned int S >
  462. class _load_le_bysize;
  463. template< typename I >
  464. class _load_le_bysize< I, 1 >
  465. {
  466. public:
  467. static ZT_INLINE I l(const uint8_t *const p) noexcept
  468. { return p[0]; }
  469. };
  470. template< typename I >
  471. class _load_le_bysize< I, 2 >
  472. {
  473. public:
  474. static ZT_INLINE I l(const uint8_t *const p) noexcept
  475. { return (I)((unsigned int)p[0] | ((unsigned int)p[1] << 8U)); }
  476. };
  477. template< typename I >
  478. class _load_le_bysize< I, 4 >
  479. {
  480. public:
  481. static ZT_INLINE I l(const uint8_t *const p) noexcept
  482. { return (I)((uint32_t)p[0] | ((uint32_t)p[1] << 8U) | ((uint32_t)p[2] << 16U) | ((uint32_t)p[3] << 24U)); }
  483. };
  484. template< typename I >
  485. class _load_le_bysize< I, 8 >
  486. {
  487. public:
  488. static ZT_INLINE I l(const uint8_t *const p) noexcept
  489. { 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); }
  490. };
  491. /**
  492. * Convert any signed or unsigned integer type to big-endian ("network") byte order
  493. *
  494. * @tparam I Integer type (usually inferred)
  495. * @param n Value to convert
  496. * @return Value in big-endian order
  497. */
  498. template< typename I >
  499. static ZT_INLINE I hton(const I n) noexcept
  500. {
  501. #if __BYTE_ORDER == __LITTLE_ENDIAN
  502. return _swap_bytes_bysize< I, sizeof(I) >::s(n);
  503. #else
  504. return n;
  505. #endif
  506. }
  507. /**
  508. * Convert any signed or unsigned integer type to host byte order from big-endian ("network") byte order
  509. *
  510. * @tparam I Integer type (usually inferred)
  511. * @param n Value to convert
  512. * @return Value in host byte order
  513. */
  514. template< typename I >
  515. static ZT_INLINE I ntoh(const I n) noexcept
  516. {
  517. #if __BYTE_ORDER == __LITTLE_ENDIAN
  518. return _swap_bytes_bysize< I, sizeof(I) >::s(n);
  519. #else
  520. return n;
  521. #endif
  522. }
  523. /**
  524. * Copy bits from memory into an integer type without modifying their order
  525. *
  526. * @tparam I Type to load
  527. * @param p Byte stream, must be at least sizeof(I) in size
  528. * @return Loaded raw integer
  529. */
  530. template< typename I >
  531. static ZT_INLINE I loadMachineEndian(const void *const p) noexcept
  532. {
  533. #ifdef ZT_NO_UNALIGNED_ACCESS
  534. I tmp;
  535. for(int i=0;i<(int)sizeof(I);++i)
  536. reinterpret_cast<uint8_t *>(&tmp)[i] = reinterpret_cast<const uint8_t *>(p)[i];
  537. return tmp;
  538. #else
  539. return *reinterpret_cast<const I *>(p);
  540. #endif
  541. }
  542. /**
  543. * Copy bits from memory into an integer type without modifying their order
  544. *
  545. * @tparam I Type to store
  546. * @param p Byte array (must be at least sizeof(I))
  547. * @param i Integer to store
  548. */
  549. template< typename I >
  550. static ZT_INLINE void storeMachineEndian(void *const p, const I i) noexcept
  551. {
  552. #ifdef ZT_NO_UNALIGNED_ACCESS
  553. for(unsigned int k=0;k<sizeof(I);++k)
  554. reinterpret_cast<uint8_t *>(p)[k] = reinterpret_cast<const uint8_t *>(&i)[k];
  555. #else
  556. *reinterpret_cast<I *>(p) = i;
  557. #endif
  558. }
  559. /**
  560. * Decode a big-endian value from a byte stream
  561. *
  562. * @tparam I Type to decode (should be unsigned e.g. uint32_t or uint64_t)
  563. * @param p Byte stream, must be at least sizeof(I) in size
  564. * @return Decoded integer
  565. */
  566. template< typename I >
  567. static ZT_INLINE I loadBigEndian(const void *const p) noexcept
  568. {
  569. #ifdef ZT_NO_UNALIGNED_ACCESS
  570. return _load_be_bysize<I,sizeof(I)>::l(reinterpret_cast<const uint8_t *>(p));
  571. #else
  572. return ntoh(*reinterpret_cast<const I *>(p));
  573. #endif
  574. }
  575. /**
  576. * Save an integer in big-endian format
  577. *
  578. * @tparam I Integer type to store (usually inferred)
  579. * @param p Byte stream to write (must be at least sizeof(I))
  580. * #param i Integer to write
  581. */
  582. template< typename I >
  583. static ZT_INLINE void storeBigEndian(void *const p, I i) noexcept
  584. {
  585. #ifdef ZT_NO_UNALIGNED_ACCESS
  586. storeMachineEndian(p,hton(i));
  587. #else
  588. *reinterpret_cast<I *>(p) = hton(i);
  589. #endif
  590. }
  591. /**
  592. * Decode a little-endian value from a byte stream
  593. *
  594. * @tparam I Type to decode
  595. * @param p Byte stream, must be at least sizeof(I) in size
  596. * @return Decoded integer
  597. */
  598. template< typename I >
  599. static ZT_INLINE I loadLittleEndian(const void *const p) noexcept
  600. {
  601. #if __BYTE_ORDER == __BIG_ENDIAN || defined(ZT_NO_UNALIGNED_ACCESS)
  602. return _load_le_bysize<I,sizeof(I)>::l(reinterpret_cast<const uint8_t *>(p));
  603. #else
  604. return *reinterpret_cast<const I *>(p);
  605. #endif
  606. }
  607. /**
  608. * Save an integer in little-endian format
  609. *
  610. * @tparam I Integer type to store (usually inferred)
  611. * @param p Byte stream to write (must be at least sizeof(I))
  612. * #param i Integer to write
  613. */
  614. template< typename I >
  615. static ZT_INLINE void storeLittleEndian(void *const p, const I i) noexcept
  616. {
  617. #if __BYTE_ORDER == __BIG_ENDIAN
  618. storeMachineEndian(p,_swap_bytes_bysize<I,sizeof(I)>::s(i));
  619. #else
  620. #ifdef ZT_NO_UNALIGNED_ACCESS
  621. storeMachineEndian(p,i);
  622. #else
  623. *reinterpret_cast<I *>(p) = i;
  624. #endif
  625. #endif
  626. }
  627. /*
  628. * Note on copy() and zero():
  629. *
  630. * On X64, rep/movsb and rep/stosb are almost always faster for small memory
  631. * regions on all but the oldest microarchitectures (and even there the
  632. * difference is not large). While more aggressive memcpy() implementations
  633. * may be faster in micro-benchmarks, these fail to account for real world
  634. * context such as instruction cache and pipeline pressure. A simple
  635. * instruction like rep/movsb takes up only a few spots in caches and pipelines
  636. * and requires no branching or function calls. Specialized memcpy() can still
  637. * be faster for large memory regions, but ZeroTier doesn't copy anything
  638. * much larger than 16KiB.
  639. *
  640. * A templated version for statically known sizes is provided since this can
  641. * allow some nice optimizations in some cases.
  642. */
  643. /**
  644. * Copy memory block whose size is known at compile time.
  645. *
  646. * @tparam L Size of memory
  647. * @param dest Destination memory
  648. * @param src Source memory
  649. */
  650. template< unsigned long L >
  651. static ZT_INLINE void copy(void *dest, const void *src) noexcept
  652. {
  653. #if defined(ZT_ARCH_X64) && defined(__GNUC__)
  654. uintptr_t l = L;
  655. asm volatile ("cld ; rep movsb" : "+c"(l), "+S"(src), "+D"(dest));
  656. #else
  657. memcpy(dest, src, L);
  658. #endif
  659. }
  660. // Avoid rep/movsb startup time for some small common sizes.
  661. template<>
  662. ZT_INLINE void copy<4>(void *dest, const void *src) noexcept
  663. {
  664. *reinterpret_cast<uint32_t *>(dest) = *reinterpret_cast<const uint32_t *>(src);
  665. }
  666. template<>
  667. ZT_INLINE void copy<8>(void *dest, const void *src) noexcept
  668. {
  669. *reinterpret_cast<uint64_t *>(dest) = *reinterpret_cast<const uint64_t *>(src);
  670. }
  671. template<>
  672. ZT_INLINE void copy<12>(void *dest, const void *src) noexcept
  673. {
  674. *reinterpret_cast<uint64_t *>(dest) = *reinterpret_cast<const uint64_t *>(src);
  675. *reinterpret_cast<uint32_t *>(reinterpret_cast<uint8_t *>(dest) + 8) = *reinterpret_cast<const uint32_t *>(reinterpret_cast<const uint8_t *>(src) + 8);
  676. }
  677. template<>
  678. ZT_INLINE void copy<16>(void *dest, const void *src) noexcept
  679. {
  680. *reinterpret_cast<uint64_t *>(dest) = *reinterpret_cast<const uint64_t *>(src);
  681. *reinterpret_cast<uint64_t *>(reinterpret_cast<uint8_t *>(dest) + 8) = *reinterpret_cast<const uint64_t *>(reinterpret_cast<const uint8_t *>(src) + 8);
  682. }
  683. template<>
  684. ZT_INLINE void copy<24>(void *dest, const void *src) noexcept
  685. {
  686. *reinterpret_cast<uint64_t *>(dest) = *reinterpret_cast<const uint64_t *>(src);
  687. *reinterpret_cast<uint64_t *>(reinterpret_cast<uint8_t *>(dest) + 8) = *reinterpret_cast<const uint64_t *>(reinterpret_cast<const uint8_t *>(src) + 8);
  688. *reinterpret_cast<uint64_t *>(reinterpret_cast<uint8_t *>(dest) + 16) = *reinterpret_cast<const uint64_t *>(reinterpret_cast<const uint8_t *>(src) + 16);
  689. }
  690. /**
  691. * Copy memory block whose size is known at run time
  692. *
  693. * @param dest Destination memory
  694. * @param src Source memory
  695. * @param len Bytes to copy
  696. */
  697. static ZT_INLINE void copy(void *dest, const void *src, unsigned long len) noexcept
  698. {
  699. #if defined(ZT_ARCH_X64) && defined(__GNUC__)
  700. asm volatile ("cld ; rep movsb" : "+c"(len), "+S"(src), "+D"(dest));
  701. #else
  702. memcpy(dest, src, len);
  703. #endif
  704. }
  705. /**
  706. * Zero memory block whose size is known at compile time
  707. *
  708. * @tparam L Size in bytes
  709. * @param dest Memory to zero
  710. */
  711. template< unsigned long L >
  712. static ZT_INLINE void zero(void *dest) noexcept
  713. {
  714. #if defined(ZT_ARCH_X64) && defined(__GNUC__)
  715. uintptr_t l = L;
  716. asm volatile ("cld ; rep stosb" :"+c" (l), "+D" (dest) : "a" (0));
  717. #else
  718. memset(dest, 0, L);
  719. #endif
  720. }
  721. /**
  722. * Zero memory block whose size is known at run time
  723. *
  724. * @param dest Memory to zero
  725. * @param len Size in bytes
  726. */
  727. static ZT_INLINE void zero(void *dest, unsigned long len) noexcept
  728. {
  729. #if defined(ZT_ARCH_X64) && defined(__GNUC__)
  730. asm volatile ("cld ; rep stosb" :"+c" (len), "+D" (dest) : "a" (0));
  731. #else
  732. memset(dest, 0, len);
  733. #endif
  734. }
  735. } // namespace Utils
  736. } // namespace ZeroTier
  737. #endif