Utils.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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. #ifndef __WINDOWS__
  89. mlock(p, l);
  90. #endif
  91. }
  92. /**
  93. * Unlock memory locked with memoryLock()
  94. *
  95. * @param p Memory to unlock
  96. * @param l Size of memory
  97. */
  98. static ZT_INLINE void memoryUnlock(const void *const p, const unsigned int l) noexcept
  99. {
  100. #ifndef __WINDOWS__
  101. munlock(p, l);
  102. #endif
  103. }
  104. /**
  105. * Perform a time-invariant binary comparison
  106. *
  107. * @param a First binary string
  108. * @param b Second binary string
  109. * @param len Length of strings
  110. * @return True if strings are equal
  111. */
  112. bool secureEq(const void *a, const void *b, unsigned int len) noexcept;
  113. /**
  114. * Be absolutely sure to zero memory
  115. *
  116. * This uses some hacks to be totally sure the compiler does not optimize it out.
  117. *
  118. * @param ptr Memory to zero
  119. * @param len Length of memory in bytes
  120. */
  121. void burn(void *ptr, unsigned int len);
  122. /**
  123. * @param n Number to convert
  124. * @param s Buffer, at least 24 bytes in size
  125. * @return String containing 'n' in base 10 form
  126. */
  127. char *decimal(unsigned long n, char s[24]) noexcept;
  128. /**
  129. * Convert an unsigned integer into hex
  130. *
  131. * @param i Any unsigned integer
  132. * @param s Buffer to receive hex, must be at least (2*sizeof(i))+1 in size or overflow will occur.
  133. * @return Pointer to s containing hex string with trailing zero byte
  134. */
  135. char *hex(uint64_t i, char buf[17]) noexcept;
  136. /**
  137. * Decode an unsigned integer in hex format
  138. *
  139. * @param s String to decode, non-hex chars are ignored
  140. * @return Unsigned integer
  141. */
  142. uint64_t unhex(const char *s) noexcept;
  143. /**
  144. * Convert a byte array into hex
  145. *
  146. * @param d Bytes
  147. * @param l Length of bytes
  148. * @param s String buffer, must be at least (l*2)+1 in size or overflow will occur
  149. * @return Pointer to filled string buffer
  150. */
  151. char *hex(const void *d, unsigned int l, char *s) noexcept;
  152. /**
  153. * Decode a hex string
  154. *
  155. * @param h Hex C-string (non hex chars are ignored)
  156. * @param hlen Maximum length of string (will stop at terminating zero)
  157. * @param buf Output buffer
  158. * @param buflen Length of output buffer
  159. * @return Number of written bytes
  160. */
  161. unsigned int unhex(const char *h, unsigned int hlen, void *buf, unsigned int buflen) noexcept;
  162. /**
  163. * Generate secure random bytes
  164. *
  165. * This will try to use whatever OS sources of entropy are available. It's
  166. * guarded by an internal mutex so it's thread-safe.
  167. *
  168. * @param buf Buffer to fill
  169. * @param bytes Number of random bytes to generate
  170. */
  171. void getSecureRandom(void *buf, unsigned int bytes) noexcept;
  172. /**
  173. * @return Secure random 64-bit integer
  174. */
  175. uint64_t getSecureRandomU64() noexcept;
  176. /**
  177. * Encode string to base32
  178. *
  179. * @param data Binary data to encode
  180. * @param length Length of data in bytes
  181. * @param result Result buffer
  182. * @param bufSize Size of result buffer
  183. * @return Number of bytes written
  184. */
  185. int b32e(const uint8_t *data, int length, char *result, int bufSize) noexcept;
  186. /**
  187. * Decode base32 string
  188. *
  189. * @param encoded C-string in base32 format (non-base32 characters are ignored)
  190. * @param result Result buffer
  191. * @param bufSize Size of result buffer
  192. * @return Number of bytes written or -1 on error
  193. */
  194. int b32d(const char *encoded, uint8_t *result, int bufSize) noexcept;
  195. /**
  196. * Get a non-cryptographic random integer.
  197. *
  198. * This should never be used for cryptographic use cases, not even for choosing
  199. * message nonce/IV values if they should not repeat. It should only be used when
  200. * a fast and potentially "dirty" random source is needed.
  201. */
  202. uint64_t random() noexcept;
  203. /**
  204. * Perform a safe C string copy, ALWAYS null-terminating the result
  205. *
  206. * This will never ever EVER result in dest[] not being null-terminated
  207. * regardless of any input parameter (other than len==0 which is invalid).
  208. *
  209. * @param dest Destination buffer (must not be NULL)
  210. * @param len Length of dest[] (if zero, false is returned and nothing happens)
  211. * @param src Source string (if NULL, dest will receive a zero-length string and true is returned)
  212. * @return True on success, false on overflow (buffer will still be 0-terminated)
  213. */
  214. bool scopy(char *dest, unsigned int len, const char *src) noexcept;
  215. /**
  216. * Mix bits in a 64-bit integer (non-cryptographic, for hash tables)
  217. *
  218. * https://nullprogram.com/blog/2018/07/31/
  219. *
  220. * @param x Integer to mix
  221. * @return Hashed value
  222. */
  223. static ZT_INLINE uint64_t hash64(uint64_t x) noexcept
  224. {
  225. x ^= x >> 30U;
  226. x *= 0xbf58476d1ce4e5b9ULL;
  227. x ^= x >> 27U;
  228. x *= 0x94d049bb133111ebULL;
  229. x ^= x >> 31U;
  230. return x;
  231. }
  232. /**
  233. * Mix bits in a 32-bit integer (non-cryptographic, for hash tables)
  234. *
  235. * https://nullprogram.com/blog/2018/07/31/
  236. *
  237. * @param x Integer to mix
  238. * @return Hashed value
  239. */
  240. static ZT_INLINE uint32_t hash32(uint32_t x) noexcept
  241. {
  242. x ^= x >> 16U;
  243. x *= 0x7feb352dU;
  244. x ^= x >> 15U;
  245. x *= 0x846ca68bU;
  246. x ^= x >> 16U;
  247. return x;
  248. }
  249. /**
  250. * Check if a buffer's contents are all zero
  251. */
  252. static ZT_INLINE bool allZero(const void *const b, unsigned int l) noexcept
  253. {
  254. const uint8_t *p = reinterpret_cast<const uint8_t *>(b);
  255. #ifndef ZT_NO_UNALIGNED_ACCESS
  256. while (l >= 8) {
  257. if (*reinterpret_cast<const uint64_t *>(p) != 0)
  258. return false;
  259. p += 8;
  260. l -= 8;
  261. }
  262. #endif
  263. for (unsigned int i = 0; i < l; ++i) {
  264. if (reinterpret_cast<const uint8_t *>(p)[i] != 0)
  265. return false;
  266. }
  267. return true;
  268. }
  269. /**
  270. * Wrapper around reentrant strtok functions, which differ in name by platform
  271. *
  272. * @param str String to tokenize or NULL for subsequent calls
  273. * @param delim Delimiter
  274. * @param saveptr Pointer to pointer where function can save state
  275. * @return Next token or NULL if none
  276. */
  277. static ZT_INLINE char *stok(char *str, const char *delim, char **saveptr) noexcept
  278. {
  279. #ifdef __WINDOWS__
  280. return strtok_s(str,delim,saveptr);
  281. #else
  282. return strtok_r(str, delim, saveptr);
  283. #endif
  284. }
  285. static ZT_INLINE unsigned int strToUInt(const char *s) noexcept
  286. { return (unsigned int)strtoul(s, nullptr, 10); }
  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 loadMachineEndian(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 storeMachineEndian(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 long L >
  630. static ZT_INLINE void copy(void *dest, const void *src) noexcept
  631. {
  632. #if defined(ZT_ARCH_X64) && defined(__GNUC__)
  633. unsigned long l = L;
  634. asm volatile ("rep movsb"
  635. : "=D" (dest),
  636. "=S" (src),
  637. "=c" (l)
  638. : "0" (dest),
  639. "1" (src),
  640. "2" (l)
  641. : "memory");
  642. #else
  643. memcpy(dest, src, L);
  644. #endif
  645. }
  646. /**
  647. * Copy memory block whose size is known at run time
  648. *
  649. * @param dest Destination memory
  650. * @param src Source memory
  651. * @param len Bytes to copy
  652. */
  653. static ZT_INLINE void copy(void *dest, const void *src, unsigned long len) noexcept
  654. {
  655. #if defined(ZT_ARCH_X64) && defined(__GNUC__)
  656. asm volatile ("rep movsb"
  657. : "=D" (dest),
  658. "=S" (src),
  659. "=c" (len)
  660. : "0" (dest),
  661. "1" (src),
  662. "2" (len)
  663. : "memory");
  664. #else
  665. memcpy(dest, src, len);
  666. #endif
  667. }
  668. /**
  669. * Zero memory block whose size is known at compile time
  670. *
  671. * @tparam L Size in bytes
  672. * @param dest Memory to zero
  673. */
  674. template< unsigned long L >
  675. static ZT_INLINE void zero(void *const dest) noexcept
  676. { memset(dest, 0, L); }
  677. /**
  678. * Zero memory block whose size is known at run time
  679. *
  680. * @param dest Memory to zero
  681. * @param len Size in bytes
  682. */
  683. static ZT_INLINE void zero(void *const dest, const unsigned long len) noexcept
  684. { memset(dest, 0, len); }
  685. } // namespace Utils
  686. } // namespace ZeroTier
  687. #endif