Utils.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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 <cstdlib>
  17. #include <cstring>
  18. namespace ZeroTier {
  19. namespace Utils {
  20. #ifdef ZT_ARCH_X64
  21. struct CPUIDRegisters
  22. {
  23. uint32_t eax,ebx,ecx,edx;
  24. bool rdrand;
  25. bool aes;
  26. CPUIDRegisters();
  27. };
  28. extern const CPUIDRegisters CPUID;
  29. #endif
  30. /**
  31. * 256 zero bits / 32 zero bytes
  32. */
  33. extern const uint64_t ZERO256[4];
  34. /**
  35. * Hexadecimal characters 0-f
  36. */
  37. extern const char HEXCHARS[16];
  38. /**
  39. * Perform a time-invariant binary comparison
  40. *
  41. * @param a First binary string
  42. * @param b Second binary string
  43. * @param len Length of strings
  44. * @return True if strings are equal
  45. */
  46. bool secureEq(const void *a,const void *b,unsigned int len) noexcept;
  47. /**
  48. * Be absolutely sure to zero memory
  49. *
  50. * This uses some hacks to be totally sure the compiler does not optimize it out.
  51. *
  52. * @param ptr Memory to zero
  53. * @param len Length of memory in bytes
  54. */
  55. void burn(void *ptr,unsigned int len);
  56. /**
  57. * @param n Number to convert
  58. * @param s Buffer, at least 24 bytes in size
  59. * @return String containing 'n' in base 10 form
  60. */
  61. char *decimal(unsigned long n,char s[24]) noexcept;
  62. /**
  63. * Convert an unsigned integer into hex
  64. *
  65. * @param i Any unsigned integer
  66. * @param s Buffer to receive hex, must be at least (2*sizeof(i))+1 in size or overflow will occur.
  67. * @return Pointer to s containing hex string with trailing zero byte
  68. */
  69. char *hex(uint8_t i,char s[3]) noexcept;
  70. /**
  71. * Convert an unsigned integer into hex
  72. *
  73. * @param i Any unsigned integer
  74. * @param s Buffer to receive hex, must be at least (2*sizeof(i))+1 in size or overflow will occur.
  75. * @return Pointer to s containing hex string with trailing zero byte
  76. */
  77. char *hex(uint16_t i,char s[5]) noexcept;
  78. /**
  79. * Convert an unsigned integer into hex
  80. *
  81. * @param i Any unsigned integer
  82. * @param s Buffer to receive hex, must be at least (2*sizeof(i))+1 in size or overflow will occur.
  83. * @return Pointer to s containing hex string with trailing zero byte
  84. */
  85. char *hex(uint32_t i,char s[9]) noexcept;
  86. /**
  87. * Convert an unsigned integer into hex
  88. *
  89. * @param i Any unsigned integer
  90. * @param s Buffer to receive hex, must be at least (2*sizeof(i))+1 in size or overflow will occur.
  91. * @return Pointer to s containing hex string with trailing zero byte
  92. */
  93. char *hex(uint64_t i,char s[17]) noexcept;
  94. /**
  95. * Decode an unsigned integer in hex format
  96. *
  97. * @param s String to decode, non-hex chars are ignored
  98. * @return Unsigned integer
  99. */
  100. uint64_t unhex(const char *s) noexcept;
  101. /**
  102. * Convert a byte array into hex
  103. *
  104. * @param d Bytes
  105. * @param l Length of bytes
  106. * @param s String buffer, must be at least (l*2)+1 in size or overflow will occur
  107. * @return Pointer to filled string buffer
  108. */
  109. char *hex(const void *d,unsigned int l,char *s) noexcept;
  110. /**
  111. * Decode a hex string
  112. *
  113. * @param h Hex C-string (non hex chars are ignored)
  114. * @param hlen Maximum length of string (will stop at terminating zero)
  115. * @param buf Output buffer
  116. * @param buflen Length of output buffer
  117. * @return Number of written bytes
  118. */
  119. unsigned int unhex(const char *h,unsigned int hlen,void *buf,unsigned int buflen) noexcept;
  120. /**
  121. * Generate secure random bytes
  122. *
  123. * This will try to use whatever OS sources of entropy are available. It's
  124. * guarded by an internal mutex so it's thread-safe.
  125. *
  126. * @param buf Buffer to fill
  127. * @param bytes Number of random bytes to generate
  128. */
  129. void getSecureRandom(void *buf,unsigned int bytes) noexcept;
  130. /**
  131. * @return Secure random 64-bit integer
  132. */
  133. uint64_t getSecureRandomU64() noexcept;
  134. /**
  135. * Encode string to base32
  136. *
  137. * @param data Binary data to encode
  138. * @param length Length of data in bytes
  139. * @param result Result buffer
  140. * @param bufSize Size of result buffer
  141. * @return Number of bytes written
  142. */
  143. int b32e(const uint8_t *data,int length,char *result,int bufSize) noexcept;
  144. /**
  145. * Decode base32 string
  146. *
  147. * @param encoded C-string in base32 format (non-base32 characters are ignored)
  148. * @param result Result buffer
  149. * @param bufSize Size of result buffer
  150. * @return Number of bytes written or -1 on error
  151. */
  152. int b32d(const char *encoded, uint8_t *result, int bufSize) noexcept;
  153. /**
  154. * Get a non-cryptographic random integer
  155. */
  156. uint64_t random() noexcept;
  157. /**
  158. * Perform a safe C string copy, ALWAYS null-terminating the result
  159. *
  160. * This will never ever EVER result in dest[] not being null-terminated
  161. * regardless of any input parameter (other than len==0 which is invalid).
  162. *
  163. * @param dest Destination buffer (must not be NULL)
  164. * @param len Length of dest[] (if zero, false is returned and nothing happens)
  165. * @param src Source string (if NULL, dest will receive a zero-length string and true is returned)
  166. * @return True on success, false on overflow (buffer will still be 0-terminated)
  167. */
  168. bool scopy(char *dest,unsigned int len,const char *src) noexcept;
  169. /**
  170. * Mix bits in a 64-bit integer
  171. *
  172. * https://nullprogram.com/blog/2018/07/31/
  173. *
  174. * @param x Integer to mix
  175. * @return Hashed value
  176. */
  177. static ZT_ALWAYS_INLINE uint64_t hash64(uint64_t x) noexcept
  178. {
  179. x ^= x >> 30U;
  180. x *= 0xbf58476d1ce4e5b9ULL;
  181. x ^= x >> 27U;
  182. x *= 0x94d049bb133111ebULL;
  183. x ^= x >> 31U;
  184. return x;
  185. }
  186. /**
  187. * @param b Buffer to check
  188. * @param l Length of buffer
  189. * @return True if buffer is all zero
  190. */
  191. static ZT_ALWAYS_INLINE bool allZero(const void *const b,const unsigned int l) noexcept
  192. {
  193. const uint8_t *x = reinterpret_cast<const uint8_t *>(b);
  194. const uint8_t *const y = x + l;
  195. while (x != y) {
  196. if (*x != 0)
  197. return false;
  198. ++x;
  199. }
  200. return true;
  201. }
  202. /**
  203. * Wrapper around reentrant strtok functions, which differ in name by platform
  204. *
  205. * @param str String to tokenize or NULL for subsequent calls
  206. * @param delim Delimiter
  207. * @param saveptr Pointer to pointer where function can save state
  208. * @return Next token or NULL if none
  209. */
  210. static ZT_ALWAYS_INLINE char *stok(char *str,const char *delim,char **saveptr) noexcept
  211. {
  212. #ifdef __WINDOWS__
  213. return strtok_s(str,delim,saveptr);
  214. #else
  215. return strtok_r(str,delim,saveptr);
  216. #endif
  217. }
  218. static ZT_ALWAYS_INLINE unsigned int strToUInt(const char *s) noexcept
  219. {
  220. return (unsigned int)strtoul(s,nullptr,10);
  221. }
  222. static ZT_ALWAYS_INLINE unsigned long long hexStrToU64(const char *s) noexcept
  223. {
  224. #ifdef __WINDOWS__
  225. return (unsigned long long)_strtoui64(s,nullptr,16);
  226. #else
  227. return strtoull(s,nullptr,16);
  228. #endif
  229. }
  230. /**
  231. * Calculate a non-cryptographic hash of a byte string
  232. *
  233. * @param key Key to hash
  234. * @param len Length in bytes
  235. * @return Non-cryptographic hash suitable for use in a hash table
  236. */
  237. static ZT_ALWAYS_INLINE unsigned long hashString(const void *restrict key,const unsigned int len) noexcept
  238. {
  239. const uint8_t *p = reinterpret_cast<const uint8_t *>(key);
  240. unsigned long h = 0;
  241. for (unsigned int i=0;i<len;++i) {
  242. h += p[i];
  243. h += (h << 10U);
  244. h ^= (h >> 6U);
  245. }
  246. h += (h << 3U);
  247. h ^= (h >> 11U);
  248. h += (h << 15U);
  249. return h;
  250. }
  251. #ifdef __GNUC__
  252. static ZT_ALWAYS_INLINE unsigned int countBits(const uint8_t v) noexcept { return (unsigned int)__builtin_popcount((unsigned int)v); }
  253. static ZT_ALWAYS_INLINE unsigned int countBits(const uint16_t v) noexcept { return (unsigned int)__builtin_popcount((unsigned int)v); }
  254. static ZT_ALWAYS_INLINE unsigned int countBits(const uint32_t v) noexcept { return (unsigned int)__builtin_popcountl((unsigned long)v); }
  255. static ZT_ALWAYS_INLINE unsigned int countBits(const uint64_t v) noexcept{ return (unsigned int)__builtin_popcountll((unsigned long long)v); }
  256. #else
  257. template<typename T>
  258. static ZT_ALWAYS_INLINE unsigned int countBits(T v) noexcept
  259. {
  260. v = v - ((v >> 1) & (T)~(T)0/3);
  261. v = (v & (T)~(T)0/15*3) + ((v >> 2) & (T)~(T)0/15*3);
  262. v = (v + (v >> 4)) & (T)~(T)0/255*15;
  263. return (unsigned int)((v * ((~((T)0))/((T)255))) >> ((sizeof(T) - 1) * 8));
  264. }
  265. #endif
  266. /**
  267. * Unconditionally swap bytes regardless of host byte order
  268. *
  269. * @param n Integer to swap
  270. * @return Integer with bytes reversed
  271. */
  272. static ZT_ALWAYS_INLINE uint64_t swapBytes(const uint64_t n) noexcept
  273. {
  274. #ifdef __GNUC__
  275. return __builtin_bswap64(n);
  276. #else
  277. #ifdef _MSC_VER
  278. return (uint64_t)_byteswap_uint64((unsigned __int64)n);
  279. #else
  280. return (
  281. ((n & 0x00000000000000ffULL) << 56) |
  282. ((n & 0x000000000000ff00ULL) << 40) |
  283. ((n & 0x0000000000ff0000ULL) << 24) |
  284. ((n & 0x00000000ff000000ULL) << 8) |
  285. ((n & 0x000000ff00000000ULL) >> 8) |
  286. ((n & 0x0000ff0000000000ULL) >> 24) |
  287. ((n & 0x00ff000000000000ULL) >> 40) |
  288. ((n & 0xff00000000000000ULL) >> 56)
  289. );
  290. #endif
  291. #endif
  292. }
  293. /**
  294. * Unconditionally swap bytes regardless of host byte order
  295. *
  296. * @param n Integer to swap
  297. * @return Integer with bytes reversed
  298. */
  299. static ZT_ALWAYS_INLINE uint32_t swapBytes(const uint32_t n) noexcept
  300. {
  301. #if defined(__GCC__)
  302. return __builtin_bswap32(n);
  303. #else
  304. #ifdef _MSC_VER
  305. return (uint32_t)_byteswap_ulong((unsigned long)n);
  306. #else
  307. return htonl(n);
  308. #endif
  309. #endif
  310. }
  311. /**
  312. * Unconditionally swap bytes regardless of host byte order
  313. *
  314. * @param n Integer to swap
  315. * @return Integer with bytes reversed
  316. */
  317. static ZT_ALWAYS_INLINE uint16_t swapBytes(const uint16_t n) noexcept
  318. {
  319. #if defined(__GCC__)
  320. return __builtin_bswap16(n);
  321. #else
  322. #ifdef _MSC_VER
  323. return (uint16_t)_byteswap_ushort((unsigned short)n);
  324. #else
  325. return htons(n);
  326. #endif
  327. #endif
  328. }
  329. // ZZvvvzvzvvzzz... moooooo... http://www.catb.org/~esr/jargon/html/Y/yak-shaving.html
  330. #if __BYTE_ORDER == __LITTLE_ENDIAN
  331. template<typename I,unsigned int S>
  332. class _hton_impl { public: static ZT_ALWAYS_INLINE I hton(const I n) { return n; } };
  333. template<typename I>
  334. class _hton_impl<I,2> { public: static ZT_ALWAYS_INLINE I hton(const I n) { return (I)swapBytes((uint16_t)n); } };
  335. template<typename I>
  336. class _hton_impl<I,4> { public: static ZT_ALWAYS_INLINE I hton(const I n) { return (I)swapBytes((uint32_t)n); } };
  337. template<typename I>
  338. class _hton_impl<I,8> { public: static ZT_ALWAYS_INLINE I hton(const I n) { return (I)swapBytes((uint64_t)n); } };
  339. #endif
  340. /**
  341. * Convert any signed or unsigned integer type to big-endian ("network") byte order
  342. *
  343. * @tparam I Integer type (usually inferred)
  344. * @param n Value to convert
  345. * @return Value in big-endian order
  346. */
  347. template<typename I>
  348. static ZT_ALWAYS_INLINE I hton(const I n) noexcept
  349. {
  350. #if __BYTE_ORDER == __LITTLE_ENDIAN
  351. return _hton_impl<I,sizeof(I)>::hton(n);
  352. #else
  353. return n;
  354. #endif
  355. }
  356. /**
  357. * Convert any signed or unsigned integer type to host byte order from big-endian ("network") byte order
  358. *
  359. * @tparam I Integer type (usually inferred)
  360. * @param n Value to convert
  361. * @return Value in host byte order
  362. */
  363. template<typename I>
  364. static ZT_ALWAYS_INLINE I ntoh(const I n) noexcept
  365. {
  366. #if __BYTE_ORDER == __LITTLE_ENDIAN
  367. return _hton_impl<I,sizeof(I)>::hton(n);
  368. #else
  369. return n;
  370. #endif
  371. }
  372. /**
  373. * Decode a big-endian value from a byte stream
  374. *
  375. * @tparam I Type to decode (should be unsigned e.g. uint32_t or uint64_t)
  376. * @param p Byte stream, must be at least sizeof(I) in size
  377. * @return Decoded integer
  378. */
  379. template<typename I>
  380. static ZT_ALWAYS_INLINE I loadBigEndian(const void *const p) noexcept
  381. {
  382. #ifdef ZT_NO_UNALIGNED_ACCESS
  383. if (sizeof(I) == 8) {
  384. return (I)(
  385. ((uint64_t)reinterpret_cast<const uint8_t *>(p)[0] << 56U) |
  386. ((uint64_t)reinterpret_cast<const uint8_t *>(p)[1] << 48U) |
  387. ((uint64_t)reinterpret_cast<const uint8_t *>(p)[2] << 40U) |
  388. ((uint64_t)reinterpret_cast<const uint8_t *>(p)[3] << 32U) |
  389. ((uint64_t)reinterpret_cast<const uint8_t *>(p)[4] << 24U) |
  390. ((uint64_t)reinterpret_cast<const uint8_t *>(p)[5] << 16U) |
  391. ((uint64_t)reinterpret_cast<const uint8_t *>(p)[6] << 8U) |
  392. (uint64_t)reinterpret_cast<const uint8_t *>(p)[7]
  393. );
  394. } else if (sizeof(I) == 4) {
  395. return (I)(
  396. ((uint32_t)reinterpret_cast<const uint8_t *>(p)[0] << 24U) |
  397. ((uint32_t)reinterpret_cast<const uint8_t *>(p)[1] << 16U) |
  398. ((uint32_t)reinterpret_cast<const uint8_t *>(p)[2] << 8U) |
  399. (uint32_t)reinterpret_cast<const uint8_t *>(p)[3]
  400. );
  401. } else if (sizeof(I) == 2) {
  402. return (I)(
  403. ((unsigned int)reinterpret_cast<const uint8_t *>(p)[0] << 8U) |
  404. (unsigned int)reinterpret_cast<const uint8_t *>(p)[1]
  405. );
  406. } else {
  407. return (I)reinterpret_cast<const uint8_t *>(p)[0];
  408. }
  409. #else
  410. return ntoh(*reinterpret_cast<const I *>(p));
  411. #endif
  412. }
  413. /**
  414. * Save an integer in big-endian format
  415. *
  416. * @tparam I Integer type to store (usually inferred)
  417. * @param p Byte stream to write (must be at least sizeof(I))
  418. * #param i Integer to write
  419. */
  420. template<typename I>
  421. static ZT_ALWAYS_INLINE void storeBigEndian(void *const p,I i) noexcept
  422. {
  423. #ifdef ZT_NO_UNALIGNED_ACCESS
  424. if (sizeof(I) == 8) {
  425. reinterpret_cast<uint8_t *>(p)[0] = (uint8_t)(reinterpret_cast<uint64_t>(i) >> 56U);
  426. reinterpret_cast<uint8_t *>(p)[1] = (uint8_t)(reinterpret_cast<uint64_t>(i) >> 48U);
  427. reinterpret_cast<uint8_t *>(p)[2] = (uint8_t)(reinterpret_cast<uint64_t>(i) >> 40U);
  428. reinterpret_cast<uint8_t *>(p)[3] = (uint8_t)(reinterpret_cast<uint64_t>(i) >> 32U);
  429. reinterpret_cast<uint8_t *>(p)[4] = (uint8_t)(reinterpret_cast<uint64_t>(i) >> 24U);
  430. reinterpret_cast<uint8_t *>(p)[5] = (uint8_t)(reinterpret_cast<uint64_t>(i) >> 16U);
  431. reinterpret_cast<uint8_t *>(p)[6] = (uint8_t)(reinterpret_cast<uint64_t>(i) >> 8U);
  432. reinterpret_cast<uint8_t *>(p)[7] = (uint8_t)reinterpret_cast<uint64_t>(i);
  433. } else if (sizeof(I) == 4) {
  434. reinterpret_cast<uint8_t *>(p)[0] = (uint8_t)(reinterpret_cast<uint32_t>(i) >> 24U);
  435. reinterpret_cast<uint8_t *>(p)[1] = (uint8_t)(reinterpret_cast<uint32_t>(i) >> 16U);
  436. reinterpret_cast<uint8_t *>(p)[2] = (uint8_t)(reinterpret_cast<uint32_t>(i) >> 8U);
  437. reinterpret_cast<uint8_t *>(p)[3] = (uint8_t)reinterpret_cast<uint32_t>(i);
  438. } else if (sizeof(I) == 2) {
  439. reinterpret_cast<uint8_t *>(p)[0] = (uint8_t)(reinterpret_cast<uint16_t>(i) >> 8U);
  440. reinterpret_cast<uint8_t *>(p)[1] = (uint8_t)reinterpret_cast<uint16_t>(i);
  441. } else {
  442. reinterpret_cast<uint8_t *>(p)[0] = (uint8_t)i;
  443. }
  444. #else
  445. *reinterpret_cast<I *>(p) = hton(i);
  446. #endif
  447. }
  448. /**
  449. * Decode a little-endian value from a byte stream
  450. *
  451. * @tparam I Type to decode
  452. * @param p Byte stream, must be at least sizeof(I) in size
  453. * @return Decoded integer
  454. */
  455. template<typename I>
  456. static ZT_ALWAYS_INLINE I loadLittleEndian(const void *const p) noexcept
  457. {
  458. #if __BYTE_ORDER == __BIG_ENDIAN || defined(ZT_NO_UNALIGNED_ACCESS)
  459. if (sizeof(I) == 8) {
  460. return (I)(
  461. (uint64_t)reinterpret_cast<const uint8_t *>(p)[0] |
  462. ((uint64_t)reinterpret_cast<const uint8_t *>(p)[1] << 8U) |
  463. ((uint64_t)reinterpret_cast<const uint8_t *>(p)[2] << 16U) |
  464. ((uint64_t)reinterpret_cast<const uint8_t *>(p)[3] << 24U) |
  465. ((uint64_t)reinterpret_cast<const uint8_t *>(p)[4] << 32U) |
  466. ((uint64_t)reinterpret_cast<const uint8_t *>(p)[5] << 40U) |
  467. ((uint64_t)reinterpret_cast<const uint8_t *>(p)[6] << 48U) |
  468. ((uint64_t)reinterpret_cast<const uint8_t *>(p)[7] << 56U)
  469. );
  470. } else if (sizeof(I) == 4) {
  471. return (I)(
  472. (uint32_t)reinterpret_cast<const uint8_t *>(p)[0] |
  473. ((uint32_t)reinterpret_cast<const uint8_t *>(p)[1] << 8U) |
  474. ((uint32_t)reinterpret_cast<const uint8_t *>(p)[2] << 16U) |
  475. ((uint32_t)reinterpret_cast<const uint8_t *>(p)[3] << 24U)
  476. );
  477. } else if (sizeof(I) == 2) {
  478. return (I)(
  479. (unsigned int)reinterpret_cast<const uint8_t *>(p)[0] |
  480. ((unsigned int)reinterpret_cast<const uint8_t *>(p)[1] << 8U)
  481. );
  482. } else {
  483. return (I)reinterpret_cast<const uint8_t *>(p)[0];
  484. }
  485. #else
  486. return *reinterpret_cast<const I *>(p);
  487. #endif
  488. }
  489. /**
  490. * Save an integer in little-endian format
  491. *
  492. * @tparam I Integer type to store (usually inferred)
  493. * @param p Byte stream to write (must be at least sizeof(I))
  494. * #param i Integer to write
  495. */
  496. template<typename I>
  497. static ZT_ALWAYS_INLINE void storeLittleEndian(void *const p,const I i) noexcept
  498. {
  499. #if __BYTE_ORDER == __BIG_ENDIAN || defined(ZT_NO_UNALIGNED_ACCESS)
  500. if (sizeof(I) == 8) {
  501. reinterpret_cast<uint8_t *>(p)[0] = (uint8_t)reinterpret_cast<uint64_t>(i);
  502. reinterpret_cast<uint8_t *>(p)[1] = (uint8_t)(reinterpret_cast<uint64_t>(i) >> 8U);
  503. reinterpret_cast<uint8_t *>(p)[2] = (uint8_t)(reinterpret_cast<uint64_t>(i) >> 16U);
  504. reinterpret_cast<uint8_t *>(p)[3] = (uint8_t)(reinterpret_cast<uint64_t>(i) >> 24U);
  505. reinterpret_cast<uint8_t *>(p)[4] = (uint8_t)(reinterpret_cast<uint64_t>(i) >> 32U);
  506. reinterpret_cast<uint8_t *>(p)[5] = (uint8_t)(reinterpret_cast<uint64_t>(i) >> 40U);
  507. reinterpret_cast<uint8_t *>(p)[6] = (uint8_t)(reinterpret_cast<uint64_t>(i) >> 48U);
  508. reinterpret_cast<uint8_t *>(p)[7] = (uint8_t)(reinterpret_cast<uint64_t>(i) >> 56U);
  509. } else if (sizeof(I) == 4) {
  510. reinterpret_cast<uint8_t *>(p)[0] = (uint8_t)reinterpret_cast<uint32_t>(i);
  511. reinterpret_cast<uint8_t *>(p)[1] = (uint8_t)(reinterpret_cast<uint32_t>(i) >> 8U);
  512. reinterpret_cast<uint8_t *>(p)[2] = (uint8_t)(reinterpret_cast<uint32_t>(i) >> 16U);
  513. reinterpret_cast<uint8_t *>(p)[3] = (uint8_t)(reinterpret_cast<uint32_t>(i) >> 24U);
  514. } else if (sizeof(I) == 2) {
  515. reinterpret_cast<uint8_t *>(p)[0] = (uint8_t)reinterpret_cast<uint16_t>(i);
  516. reinterpret_cast<uint8_t *>(p)[1] = (uint8_t)(reinterpret_cast<uint16_t>(i) >> 8U);
  517. } else {
  518. reinterpret_cast<uint8_t *>(p)[0] = (uint8_t)i;
  519. }
  520. #else
  521. *reinterpret_cast<I *>(p) = i;
  522. #endif
  523. }
  524. /**
  525. * Copy bits from memory into an integer type without modifying their order
  526. *
  527. * @tparam I Type to load
  528. * @param p Byte stream, must be at least sizeof(I) in size
  529. * @return Loaded raw integer
  530. */
  531. template<typename I>
  532. static ZT_ALWAYS_INLINE I loadAsIsEndian(const void *const p) noexcept
  533. {
  534. #ifdef ZT_NO_UNALIGNED_ACCESS
  535. I x = (I)0;
  536. for(unsigned int k=0;k<sizeof(I);++k)
  537. reinterpret_cast<uint8_t *>(&x)[k] = reinterpret_cast<const uint8_t *>(p)[k];
  538. return x;
  539. #else
  540. return *reinterpret_cast<const I *>(p);
  541. #endif
  542. }
  543. /**
  544. * Copy bits from memory into an integer type without modifying their order
  545. *
  546. * @tparam I Type to store
  547. * @param p Byte array (must be at least sizeof(I))
  548. * @param i Integer to store
  549. */
  550. template<typename I>
  551. static ZT_ALWAYS_INLINE void storeAsIsEndian(void *const p,const I i) noexcept
  552. {
  553. #ifdef ZT_NO_UNALIGNED_ACCESS
  554. for(unsigned int k=0;k<sizeof(I);++k)
  555. reinterpret_cast<uint8_t *>(p)[k] = reinterpret_cast<const uint8_t *>(&i)[k];
  556. #else
  557. *reinterpret_cast<I *>(p) = i;
  558. #endif
  559. }
  560. } // namespace Utils
  561. } // namespace ZeroTier
  562. #endif