Utils.hpp 21 KB

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