Utils.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /*
  2. * Copyright (c)2019 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: 2025-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 <stdio.h>
  16. #include <stdlib.h>
  17. #include <stdint.h>
  18. #include <string.h>
  19. #include <time.h>
  20. #include <string>
  21. #include <stdexcept>
  22. #include <vector>
  23. #include <map>
  24. #if defined(__FreeBSD__)
  25. #include <sys/endian.h>
  26. #endif
  27. #include "Constants.hpp"
  28. #if __BYTE_ORDER == __LITTLE_ENDIAN
  29. #define ZT_CONST_TO_BE_UINT16(x) ((uint16_t)((uint16_t)((uint16_t)(x) << 8U) | (uint16_t)((uint16_t)(x) >> 8U)))
  30. #define ZT_CONST_TO_BE_UINT64(x) ( \
  31. (((uint64_t)(x) & 0x00000000000000ffULL) << 56U) | \
  32. (((uint64_t)(x) & 0x000000000000ff00ULL) << 40U) | \
  33. (((uint64_t)(x) & 0x0000000000ff0000ULL) << 24U) | \
  34. (((uint64_t)(x) & 0x00000000ff000000ULL) << 8U) | \
  35. (((uint64_t)(x) & 0x000000ff00000000ULL) >> 8U) | \
  36. (((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24U) | \
  37. (((uint64_t)(x) & 0x00ff000000000000ULL) >> 40U) | \
  38. (((uint64_t)(x) & 0xff00000000000000ULL) >> 56U))
  39. #else
  40. #define ZT_CONST_TO_BE_UINT16(x) ((uint16_t)(x))
  41. #define ZT_CONST_TO_BE_UINT64(x) ((uint64_t)(x))
  42. #endif
  43. #define ZT_ROR64(x, r) (((x) >> (r)) | ((x) << (64 - (r))))
  44. #define ZT_ROL64(x, r) (((x) << (r)) | ((x) >> (64 - (r))))
  45. #define ZT_ROR32(x, r) (((x) >> (r)) | ((x) << (32 - (r))))
  46. #define ZT_ROL32(x, r) (((x) << (r)) | ((x) >> (32 - (r))))
  47. namespace ZeroTier {
  48. /**
  49. * Miscellaneous utility functions and global constants
  50. */
  51. class Utils
  52. {
  53. public:
  54. static const uint64_t ZERO256[4];
  55. #ifdef ZT_ARCH_ARM_HAS_NEON
  56. struct ARMCapabilities
  57. {
  58. ARMCapabilities() noexcept;
  59. bool aes;
  60. bool crc32;
  61. bool pmull;
  62. bool sha1;
  63. bool sha2;
  64. };
  65. static const ARMCapabilities ARMCAP;
  66. #endif
  67. #ifdef ZT_ARCH_X64
  68. struct CPUIDRegisters
  69. {
  70. CPUIDRegisters() noexcept;
  71. bool rdrand;
  72. bool aes;
  73. bool avx;
  74. bool vaes; // implies AVX
  75. bool vpclmulqdq; // implies AVX
  76. bool avx2;
  77. bool avx512f;
  78. bool sha;
  79. bool fsrm;
  80. };
  81. static const CPUIDRegisters CPUID;
  82. #endif
  83. /**
  84. * Perform a time-invariant binary comparison
  85. *
  86. * @param a First binary string
  87. * @param b Second binary string
  88. * @param len Length of strings
  89. * @return True if strings are equal
  90. */
  91. static inline bool secureEq(const void *a,const void *b,unsigned int len)
  92. {
  93. uint8_t diff = 0;
  94. for(unsigned int i=0;i<len;++i)
  95. diff |= ( (reinterpret_cast<const uint8_t *>(a))[i] ^ (reinterpret_cast<const uint8_t *>(b))[i] );
  96. return (diff == 0);
  97. }
  98. /**
  99. * Securely zero memory, avoiding compiler optimizations and such
  100. */
  101. static void burn(void *ptr,unsigned int len);
  102. /**
  103. * @param n Number to convert
  104. * @param s Buffer, at least 24 bytes in size
  105. * @return String containing 'n' in base 10 form
  106. */
  107. static char *decimal(unsigned long n,char s[24]);
  108. static inline char *hex(uint64_t i,char s[17])
  109. {
  110. s[0] = HEXCHARS[(i >> 60) & 0xf];
  111. s[1] = HEXCHARS[(i >> 56) & 0xf];
  112. s[2] = HEXCHARS[(i >> 52) & 0xf];
  113. s[3] = HEXCHARS[(i >> 48) & 0xf];
  114. s[4] = HEXCHARS[(i >> 44) & 0xf];
  115. s[5] = HEXCHARS[(i >> 40) & 0xf];
  116. s[6] = HEXCHARS[(i >> 36) & 0xf];
  117. s[7] = HEXCHARS[(i >> 32) & 0xf];
  118. s[8] = HEXCHARS[(i >> 28) & 0xf];
  119. s[9] = HEXCHARS[(i >> 24) & 0xf];
  120. s[10] = HEXCHARS[(i >> 20) & 0xf];
  121. s[11] = HEXCHARS[(i >> 16) & 0xf];
  122. s[12] = HEXCHARS[(i >> 12) & 0xf];
  123. s[13] = HEXCHARS[(i >> 8) & 0xf];
  124. s[14] = HEXCHARS[(i >> 4) & 0xf];
  125. s[15] = HEXCHARS[i & 0xf];
  126. s[16] = (char)0;
  127. return s;
  128. }
  129. static inline char *hex10(uint64_t i,char s[11])
  130. {
  131. s[0] = HEXCHARS[(i >> 36) & 0xf];
  132. s[1] = HEXCHARS[(i >> 32) & 0xf];
  133. s[2] = HEXCHARS[(i >> 28) & 0xf];
  134. s[3] = HEXCHARS[(i >> 24) & 0xf];
  135. s[4] = HEXCHARS[(i >> 20) & 0xf];
  136. s[5] = HEXCHARS[(i >> 16) & 0xf];
  137. s[6] = HEXCHARS[(i >> 12) & 0xf];
  138. s[7] = HEXCHARS[(i >> 8) & 0xf];
  139. s[8] = HEXCHARS[(i >> 4) & 0xf];
  140. s[9] = HEXCHARS[i & 0xf];
  141. s[10] = (char)0;
  142. return s;
  143. }
  144. static inline char *hex(uint32_t i,char s[9])
  145. {
  146. s[0] = HEXCHARS[(i >> 28) & 0xf];
  147. s[1] = HEXCHARS[(i >> 24) & 0xf];
  148. s[2] = HEXCHARS[(i >> 20) & 0xf];
  149. s[3] = HEXCHARS[(i >> 16) & 0xf];
  150. s[4] = HEXCHARS[(i >> 12) & 0xf];
  151. s[5] = HEXCHARS[(i >> 8) & 0xf];
  152. s[6] = HEXCHARS[(i >> 4) & 0xf];
  153. s[7] = HEXCHARS[i & 0xf];
  154. s[8] = (char)0;
  155. return s;
  156. }
  157. static inline char *hex(uint16_t i,char s[5])
  158. {
  159. s[0] = HEXCHARS[(i >> 12) & 0xf];
  160. s[1] = HEXCHARS[(i >> 8) & 0xf];
  161. s[2] = HEXCHARS[(i >> 4) & 0xf];
  162. s[3] = HEXCHARS[i & 0xf];
  163. s[4] = (char)0;
  164. return s;
  165. }
  166. static inline char *hex(uint8_t i,char s[3])
  167. {
  168. s[0] = HEXCHARS[(i >> 4) & 0xf];
  169. s[1] = HEXCHARS[i & 0xf];
  170. s[2] = (char)0;
  171. return s;
  172. }
  173. static inline char *hex(const void *d,unsigned int l,char *s)
  174. {
  175. char *const save = s;
  176. for(unsigned int i=0;i<l;++i) {
  177. const unsigned int b = reinterpret_cast<const uint8_t *>(d)[i];
  178. *(s++) = HEXCHARS[b >> 4];
  179. *(s++) = HEXCHARS[b & 0xf];
  180. }
  181. *s = (char)0;
  182. return save;
  183. }
  184. static inline unsigned int unhex(const char *h,void *buf,unsigned int buflen)
  185. {
  186. unsigned int l = 0;
  187. while (l < buflen) {
  188. uint8_t hc = *(reinterpret_cast<const uint8_t *>(h++));
  189. if (!hc) break;
  190. uint8_t c = 0;
  191. if ((hc >= 48)&&(hc <= 57)) // 0..9
  192. c = hc - 48;
  193. else if ((hc >= 97)&&(hc <= 102)) // a..f
  194. c = hc - 87;
  195. else if ((hc >= 65)&&(hc <= 70)) // A..F
  196. c = hc - 55;
  197. hc = *(reinterpret_cast<const uint8_t *>(h++));
  198. if (!hc) break;
  199. c <<= 4;
  200. if ((hc >= 48)&&(hc <= 57))
  201. c |= hc - 48;
  202. else if ((hc >= 97)&&(hc <= 102))
  203. c |= hc - 87;
  204. else if ((hc >= 65)&&(hc <= 70))
  205. c |= hc - 55;
  206. reinterpret_cast<uint8_t *>(buf)[l++] = c;
  207. }
  208. return l;
  209. }
  210. static inline unsigned int unhex(const char *h,unsigned int hlen,void *buf,unsigned int buflen)
  211. {
  212. unsigned int l = 0;
  213. const char *hend = h + hlen;
  214. while (l < buflen) {
  215. if (h == hend) break;
  216. uint8_t hc = *(reinterpret_cast<const uint8_t *>(h++));
  217. if (!hc) break;
  218. uint8_t c = 0;
  219. if ((hc >= 48)&&(hc <= 57))
  220. c = hc - 48;
  221. else if ((hc >= 97)&&(hc <= 102))
  222. c = hc - 87;
  223. else if ((hc >= 65)&&(hc <= 70))
  224. c = hc - 55;
  225. if (h == hend) break;
  226. hc = *(reinterpret_cast<const uint8_t *>(h++));
  227. if (!hc) break;
  228. c <<= 4;
  229. if ((hc >= 48)&&(hc <= 57))
  230. c |= hc - 48;
  231. else if ((hc >= 97)&&(hc <= 102))
  232. c |= hc - 87;
  233. else if ((hc >= 65)&&(hc <= 70))
  234. c |= hc - 55;
  235. reinterpret_cast<uint8_t *>(buf)[l++] = c;
  236. }
  237. return l;
  238. }
  239. static inline float normalize(float value, float bigMin, float bigMax, float targetMin, float targetMax)
  240. {
  241. float bigSpan = bigMax - bigMin;
  242. float smallSpan = targetMax - targetMin;
  243. float valueScaled = (value - bigMin) / bigSpan;
  244. return targetMin + valueScaled * smallSpan;
  245. }
  246. /**
  247. * Generate secure random bytes
  248. *
  249. * This will try to use whatever OS sources of entropy are available. It's
  250. * guarded by an internal mutex so it's thread-safe.
  251. *
  252. * @param buf Buffer to fill
  253. * @param bytes Number of random bytes to generate
  254. */
  255. static void getSecureRandom(void *buf,unsigned int bytes);
  256. /**
  257. * Tokenize a string (alias for strtok_r or strtok_s depending on platform)
  258. *
  259. * @param str String to split
  260. * @param delim Delimiters
  261. * @param saveptr Pointer to a char * for temporary reentrant storage
  262. */
  263. static inline char *stok(char *str,const char *delim,char **saveptr)
  264. {
  265. #ifdef __WINDOWS__
  266. return strtok_s(str,delim,saveptr);
  267. #else
  268. return strtok_r(str,delim,saveptr);
  269. #endif
  270. }
  271. static inline unsigned int strToUInt(const char *s) { return (unsigned int)strtoul(s,(char **)0,10); }
  272. static inline int strToInt(const char *s) { return (int)strtol(s,(char **)0,10); }
  273. static inline unsigned long strToULong(const char *s) { return strtoul(s,(char **)0,10); }
  274. static inline long strToLong(const char *s) { return strtol(s,(char **)0,10); }
  275. static inline double strToDouble(const char *s) { return strtod(s,NULL); }
  276. static inline unsigned long long strToU64(const char *s)
  277. {
  278. #ifdef __WINDOWS__
  279. return (unsigned long long)_strtoui64(s,(char **)0,10);
  280. #else
  281. return strtoull(s,(char **)0,10);
  282. #endif
  283. }
  284. static inline long long strTo64(const char *s)
  285. {
  286. #ifdef __WINDOWS__
  287. return (long long)_strtoi64(s,(char **)0,10);
  288. #else
  289. return strtoll(s,(char **)0,10);
  290. #endif
  291. }
  292. static inline unsigned int hexStrToUInt(const char *s) { return (unsigned int)strtoul(s,(char **)0,16); }
  293. static inline int hexStrToInt(const char *s) { return (int)strtol(s,(char **)0,16); }
  294. static inline unsigned long hexStrToULong(const char *s) { return strtoul(s,(char **)0,16); }
  295. static inline long hexStrToLong(const char *s) { return strtol(s,(char **)0,16); }
  296. static inline unsigned long long hexStrToU64(const char *s)
  297. {
  298. #ifdef __WINDOWS__
  299. return (unsigned long long)_strtoui64(s,(char **)0,16);
  300. #else
  301. return strtoull(s,(char **)0,16);
  302. #endif
  303. }
  304. static inline long long hexStrTo64(const char *s)
  305. {
  306. #ifdef __WINDOWS__
  307. return (long long)_strtoi64(s,(char **)0,16);
  308. #else
  309. return strtoll(s,(char **)0,16);
  310. #endif
  311. }
  312. /**
  313. * Perform a safe C string copy, ALWAYS null-terminating the result
  314. *
  315. * This will never ever EVER result in dest[] not being null-terminated
  316. * regardless of any input parameter (other than len==0 which is invalid).
  317. *
  318. * @param dest Destination buffer (must not be NULL)
  319. * @param len Length of dest[] (if zero, false is returned and nothing happens)
  320. * @param src Source string (if NULL, dest will receive a zero-length string and true is returned)
  321. * @return True on success, false on overflow (buffer will still be 0-terminated)
  322. */
  323. static inline bool scopy(char *dest,unsigned int len,const char *src)
  324. {
  325. if (!len)
  326. return false; // sanity check
  327. if (!src) {
  328. *dest = (char)0;
  329. return true;
  330. }
  331. char *end = dest + len;
  332. while ((*dest++ = *src++)) {
  333. if (dest == end) {
  334. *(--dest) = (char)0;
  335. return false;
  336. }
  337. }
  338. return true;
  339. }
  340. /**
  341. * Count the number of bits set in an integer
  342. *
  343. * @param v 32-bit integer
  344. * @return Number of bits set in this integer (0-32)
  345. */
  346. static inline uint32_t countBits(uint32_t v)
  347. {
  348. v = v - ((v >> 1) & (uint32_t)0x55555555);
  349. v = (v & (uint32_t)0x33333333) + ((v >> 2) & (uint32_t)0x33333333);
  350. return ((((v + (v >> 4)) & (uint32_t)0xF0F0F0F) * (uint32_t)0x1010101) >> 24);
  351. }
  352. /**
  353. * Count the number of bits set in an integer
  354. *
  355. * @param v 64-bit integer
  356. * @return Number of bits set in this integer (0-64)
  357. */
  358. static inline uint64_t countBits(uint64_t v)
  359. {
  360. v = v - ((v >> 1) & (uint64_t)~(uint64_t)0/3);
  361. v = (v & (uint64_t)~(uint64_t)0/15*3) + ((v >> 2) & (uint64_t)~(uint64_t)0/15*3);
  362. v = (v + (v >> 4)) & (uint64_t)~(uint64_t)0/255*15;
  363. return (uint64_t)(v * ((uint64_t)~(uint64_t)0/255)) >> 56;
  364. }
  365. /**
  366. * Check if a memory buffer is all-zero
  367. *
  368. * @param p Memory to scan
  369. * @param len Length of memory
  370. * @return True if memory is all zero
  371. */
  372. static inline bool isZero(const void *p,unsigned int len)
  373. {
  374. for(unsigned int i=0;i<len;++i) {
  375. if (((const unsigned char *)p)[i])
  376. return false;
  377. }
  378. return true;
  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 uint64_t swapBytes(const uint64_t n) noexcept
  387. {
  388. #ifdef __GNUC__
  389. return __builtin_bswap64(n);
  390. #else
  391. #ifdef _MSC_VER
  392. return (uint64_t)_byteswap_uint64((unsigned __int64)n);
  393. #else
  394. return (
  395. ((n & 0x00000000000000ffULL) << 56) |
  396. ((n & 0x000000000000ff00ULL) << 40) |
  397. ((n & 0x0000000000ff0000ULL) << 24) |
  398. ((n & 0x00000000ff000000ULL) << 8) |
  399. ((n & 0x000000ff00000000ULL) >> 8) |
  400. ((n & 0x0000ff0000000000ULL) >> 24) |
  401. ((n & 0x00ff000000000000ULL) >> 40) |
  402. ((n & 0xff00000000000000ULL) >> 56)
  403. );
  404. #endif
  405. #endif
  406. }
  407. /**
  408. * Unconditionally swap bytes regardless of host byte order
  409. *
  410. * @param n Integer to swap
  411. * @return Integer with bytes reversed
  412. */
  413. static ZT_INLINE uint32_t swapBytes(const uint32_t n) noexcept
  414. {
  415. #if defined(__GNUC__)
  416. return __builtin_bswap32(n);
  417. #else
  418. #ifdef _MSC_VER
  419. return (uint32_t)_byteswap_ulong((unsigned long)n);
  420. #else
  421. return htonl(n);
  422. #endif
  423. #endif
  424. }
  425. /**
  426. * Unconditionally swap bytes regardless of host byte order
  427. *
  428. * @param n Integer to swap
  429. * @return Integer with bytes reversed
  430. */
  431. static ZT_INLINE uint16_t swapBytes(const uint16_t n) noexcept
  432. {
  433. #if defined(__GNUC__)
  434. return __builtin_bswap16(n);
  435. #else
  436. #ifdef _MSC_VER
  437. return (uint16_t)_byteswap_ushort((unsigned short)n);
  438. #else
  439. return htons(n);
  440. #endif
  441. #endif
  442. }
  443. // These are helper adapters to load and swap integer types special cased by size
  444. // to work with all typedef'd variants, signed/unsigned, etc.
  445. template< typename I, unsigned int S >
  446. class _swap_bytes_bysize;
  447. template< typename I >
  448. class _swap_bytes_bysize< I, 1 >
  449. {
  450. public:
  451. static ZT_INLINE I s(const I n) noexcept
  452. { return n; }
  453. };
  454. template< typename I >
  455. class _swap_bytes_bysize< I, 2 >
  456. {
  457. public:
  458. static ZT_INLINE I s(const I n) noexcept
  459. { return (I)swapBytes((uint16_t)n); }
  460. };
  461. template< typename I >
  462. class _swap_bytes_bysize< I, 4 >
  463. {
  464. public:
  465. static ZT_INLINE I s(const I n) noexcept
  466. { return (I)swapBytes((uint32_t)n); }
  467. };
  468. template< typename I >
  469. class _swap_bytes_bysize< I, 8 >
  470. {
  471. public:
  472. static ZT_INLINE I s(const I n) noexcept
  473. { return (I)swapBytes((uint64_t)n); }
  474. };
  475. template< typename I, unsigned int S >
  476. class _load_be_bysize;
  477. template< typename I >
  478. class _load_be_bysize< I, 1 >
  479. {
  480. public:
  481. static ZT_INLINE I l(const uint8_t *const p) noexcept
  482. { return p[0]; }
  483. };
  484. template< typename I >
  485. class _load_be_bysize< I, 2 >
  486. {
  487. public:
  488. static ZT_INLINE I l(const uint8_t *const p) noexcept
  489. { return (I)(((unsigned int)p[0] << 8U) | (unsigned int)p[1]); }
  490. };
  491. template< typename I >
  492. class _load_be_bysize< I, 4 >
  493. {
  494. public:
  495. static ZT_INLINE I l(const uint8_t *const p) noexcept
  496. { return (I)(((uint32_t)p[0] << 24U) | ((uint32_t)p[1] << 16U) | ((uint32_t)p[2] << 8U) | (uint32_t)p[3]); }
  497. };
  498. template< typename I >
  499. class _load_be_bysize< I, 8 >
  500. {
  501. public:
  502. static ZT_INLINE I l(const uint8_t *const p) noexcept
  503. { 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]); }
  504. };
  505. template< typename I, unsigned int S >
  506. class _load_le_bysize;
  507. template< typename I >
  508. class _load_le_bysize< I, 1 >
  509. {
  510. public:
  511. static ZT_INLINE I l(const uint8_t *const p) noexcept
  512. { return p[0]; }
  513. };
  514. template< typename I >
  515. class _load_le_bysize< I, 2 >
  516. {
  517. public:
  518. static ZT_INLINE I l(const uint8_t *const p) noexcept
  519. { return (I)((unsigned int)p[0] | ((unsigned int)p[1] << 8U)); }
  520. };
  521. template< typename I >
  522. class _load_le_bysize< I, 4 >
  523. {
  524. public:
  525. static ZT_INLINE I l(const uint8_t *const p) noexcept
  526. { return (I)((uint32_t)p[0] | ((uint32_t)p[1] << 8U) | ((uint32_t)p[2] << 16U) | ((uint32_t)p[3] << 24U)); }
  527. };
  528. template< typename I >
  529. class _load_le_bysize< I, 8 >
  530. {
  531. public:
  532. static ZT_INLINE I l(const uint8_t *const p) noexcept
  533. { 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); }
  534. };
  535. /**
  536. * Convert any signed or unsigned integer type to big-endian ("network") byte order
  537. *
  538. * @tparam I Integer type (usually inferred)
  539. * @param n Value to convert
  540. * @return Value in big-endian order
  541. */
  542. template< typename I >
  543. static ZT_INLINE I hton(const I n) noexcept
  544. {
  545. #if __BYTE_ORDER == __LITTLE_ENDIAN
  546. return _swap_bytes_bysize< I, sizeof(I) >::s(n);
  547. #else
  548. return n;
  549. #endif
  550. }
  551. /**
  552. * Convert any signed or unsigned integer type to host byte order from big-endian ("network") byte order
  553. *
  554. * @tparam I Integer type (usually inferred)
  555. * @param n Value to convert
  556. * @return Value in host byte order
  557. */
  558. template< typename I >
  559. static ZT_INLINE I ntoh(const I n) noexcept
  560. {
  561. #if __BYTE_ORDER == __LITTLE_ENDIAN
  562. return _swap_bytes_bysize< I, sizeof(I) >::s(n);
  563. #else
  564. return n;
  565. #endif
  566. }
  567. /**
  568. * Copy bits from memory into an integer type without modifying their order
  569. *
  570. * @tparam I Type to load
  571. * @param p Byte stream, must be at least sizeof(I) in size
  572. * @return Loaded raw integer
  573. */
  574. template< typename I >
  575. static ZT_INLINE I loadMachineEndian(const void *const p) noexcept
  576. {
  577. #ifdef ZT_NO_UNALIGNED_ACCESS
  578. I tmp;
  579. for(int i=0;i<(int)sizeof(I);++i)
  580. reinterpret_cast<uint8_t *>(&tmp)[i] = reinterpret_cast<const uint8_t *>(p)[i];
  581. return tmp;
  582. #else
  583. return *reinterpret_cast<const I *>(p);
  584. #endif
  585. }
  586. /**
  587. * Copy bits from memory into an integer type without modifying their order
  588. *
  589. * @tparam I Type to store
  590. * @param p Byte array (must be at least sizeof(I))
  591. * @param i Integer to store
  592. */
  593. template< typename I >
  594. static ZT_INLINE void storeMachineEndian(void *const p, const I i) noexcept
  595. {
  596. #ifdef ZT_NO_UNALIGNED_ACCESS
  597. for(unsigned int k=0;k<sizeof(I);++k)
  598. reinterpret_cast<uint8_t *>(p)[k] = reinterpret_cast<const uint8_t *>(&i)[k];
  599. #else
  600. *reinterpret_cast<I *>(p) = i;
  601. #endif
  602. }
  603. /**
  604. * Decode a big-endian value from a byte stream
  605. *
  606. * @tparam I Type to decode (should be unsigned e.g. uint32_t or uint64_t)
  607. * @param p Byte stream, must be at least sizeof(I) in size
  608. * @return Decoded integer
  609. */
  610. template< typename I >
  611. static ZT_INLINE I loadBigEndian(const void *const p) noexcept
  612. {
  613. #ifdef ZT_NO_UNALIGNED_ACCESS
  614. return _load_be_bysize<I,sizeof(I)>::l(reinterpret_cast<const uint8_t *>(p));
  615. #else
  616. return ntoh(*reinterpret_cast<const I *>(p));
  617. #endif
  618. }
  619. /**
  620. * Save an integer in big-endian format
  621. *
  622. * @tparam I Integer type to store (usually inferred)
  623. * @param p Byte stream to write (must be at least sizeof(I))
  624. * #param i Integer to write
  625. */
  626. template< typename I >
  627. static ZT_INLINE void storeBigEndian(void *const p, I i) noexcept
  628. {
  629. #ifdef ZT_NO_UNALIGNED_ACCESS
  630. storeMachineEndian(p,hton(i));
  631. #else
  632. *reinterpret_cast<I *>(p) = hton(i);
  633. #endif
  634. }
  635. /**
  636. * Decode a little-endian value from a byte stream
  637. *
  638. * @tparam I Type to decode
  639. * @param p Byte stream, must be at least sizeof(I) in size
  640. * @return Decoded integer
  641. */
  642. template< typename I >
  643. static ZT_INLINE I loadLittleEndian(const void *const p) noexcept
  644. {
  645. #if __BYTE_ORDER == __BIG_ENDIAN || defined(ZT_NO_UNALIGNED_ACCESS)
  646. return _load_le_bysize<I,sizeof(I)>::l(reinterpret_cast<const uint8_t *>(p));
  647. #else
  648. return *reinterpret_cast<const I *>(p);
  649. #endif
  650. }
  651. /**
  652. * Save an integer in little-endian format
  653. *
  654. * @tparam I Integer type to store (usually inferred)
  655. * @param p Byte stream to write (must be at least sizeof(I))
  656. * #param i Integer to write
  657. */
  658. template< typename I >
  659. static ZT_INLINE void storeLittleEndian(void *const p, const I i) noexcept
  660. {
  661. #if __BYTE_ORDER == __BIG_ENDIAN
  662. storeMachineEndian(p,_swap_bytes_bysize<I,sizeof(I)>::s(i));
  663. #else
  664. #ifdef ZT_NO_UNALIGNED_ACCESS
  665. storeMachineEndian(p,i);
  666. #else
  667. *reinterpret_cast<I *>(p) = i;
  668. #endif
  669. #endif
  670. }
  671. /**
  672. * Copy memory block whose size is known at compile time.
  673. *
  674. * @tparam L Size of memory
  675. * @param dest Destination memory
  676. * @param src Source memory
  677. */
  678. template< unsigned long L >
  679. static ZT_INLINE void copy(void *dest, const void *src) noexcept
  680. {
  681. #if defined(ZT_ARCH_X64) && defined(__GNUC__)
  682. uintptr_t l = L;
  683. __asm__ __volatile__ ("cld ; rep movsb" : "+c"(l), "+S"(src), "+D"(dest) :: "memory");
  684. #else
  685. memcpy(dest, src, L);
  686. #endif
  687. }
  688. /**
  689. * Copy memory block whose size is known at run time
  690. *
  691. * @param dest Destination memory
  692. * @param src Source memory
  693. * @param len Bytes to copy
  694. */
  695. static ZT_INLINE void copy(void *dest, const void *src, unsigned long len) noexcept
  696. {
  697. #if defined(ZT_ARCH_X64) && defined(__GNUC__)
  698. __asm__ __volatile__ ("cld ; rep movsb" : "+c"(len), "+S"(src), "+D"(dest) :: "memory");
  699. #else
  700. memcpy(dest, src, len);
  701. #endif
  702. }
  703. /**
  704. * Zero memory block whose size is known at compile time
  705. *
  706. * @tparam L Size in bytes
  707. * @param dest Memory to zero
  708. */
  709. template< unsigned long L >
  710. static ZT_INLINE void zero(void *dest) noexcept
  711. {
  712. #if defined(ZT_ARCH_X64) && defined(__GNUC__)
  713. uintptr_t l = L;
  714. __asm__ __volatile__ ("cld ; rep stosb" :"+c" (l), "+D" (dest) : "a" (0) : "memory");
  715. #else
  716. memset(dest, 0, L);
  717. #endif
  718. }
  719. /**
  720. * Zero memory block whose size is known at run time
  721. *
  722. * @param dest Memory to zero
  723. * @param len Size in bytes
  724. */
  725. static ZT_INLINE void zero(void *dest, unsigned long len) noexcept
  726. {
  727. #if defined(ZT_ARCH_X64) && defined(__GNUC__)
  728. __asm__ __volatile__ ("cld ; rep stosb" :"+c" (len), "+D" (dest) : "a" (0) : "memory");
  729. #else
  730. memset(dest, 0, len);
  731. #endif
  732. }
  733. /**
  734. * Hexadecimal characters 0-f
  735. */
  736. static const char HEXCHARS[16];
  737. };
  738. } // namespace ZeroTier
  739. #endif