Utils.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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. #ifdef ZT_ARCH_X64
  17. #include <xmmintrin.h>
  18. #include <emmintrin.h>
  19. #include <immintrin.h>
  20. #endif
  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. #ifdef ZT_ARCH_X64
  43. struct CPUIDRegisters
  44. {
  45. uint32_t eax,ebx,ecx,edx;
  46. bool rdrand;
  47. bool aes;
  48. CPUIDRegisters();
  49. };
  50. extern const CPUIDRegisters CPUID;
  51. #endif
  52. /**
  53. * 256 zero bits / 32 zero bytes
  54. */
  55. extern const uint64_t ZERO256[4];
  56. /**
  57. * Hexadecimal characters 0-f
  58. */
  59. extern const char HEXCHARS[16];
  60. /**
  61. * A random integer generated at startup for Map's hash bucket calculation.
  62. */
  63. extern const uint64_t s_mapNonce;
  64. /**
  65. * Lock memory to prevent swapping out to secondary storage (if possible)
  66. *
  67. * This is used to attempt to prevent the swapping out of long-term stored secure
  68. * credentials like secret keys. It isn't supported on all platforms and may not
  69. * be absolutely guaranteed to work, but it's a countermeasure.
  70. *
  71. * @param p Memory to lock
  72. * @param l Size of memory
  73. */
  74. static ZT_INLINE void memoryLock(const void *const p,const unsigned int l) noexcept
  75. {
  76. #ifndef __WINDOWS__
  77. mlock(p,l);
  78. #endif
  79. }
  80. /**
  81. * Unlock memory locked with memoryLock()
  82. *
  83. * @param p Memory to unlock
  84. * @param l Size of memory
  85. */
  86. static ZT_INLINE void memoryUnlock(const void *const p,const unsigned int l) noexcept
  87. {
  88. #ifndef __WINDOWS__
  89. munlock(p,l);
  90. #endif
  91. }
  92. /**
  93. * Perform a time-invariant binary comparison
  94. *
  95. * @param a First binary string
  96. * @param b Second binary string
  97. * @param len Length of strings
  98. * @return True if strings are equal
  99. */
  100. bool secureEq(const void *a,const void *b,unsigned int len) noexcept;
  101. /**
  102. * Be absolutely sure to zero memory
  103. *
  104. * This uses some hacks to be totally sure the compiler does not optimize it out.
  105. *
  106. * @param ptr Memory to zero
  107. * @param len Length of memory in bytes
  108. */
  109. void burn(void *ptr,unsigned int len);
  110. /**
  111. * @param n Number to convert
  112. * @param s Buffer, at least 24 bytes in size
  113. * @return String containing 'n' in base 10 form
  114. */
  115. char *decimal(unsigned long n,char s[24]) noexcept;
  116. /**
  117. * Convert an unsigned integer into hex
  118. *
  119. * @param i Any unsigned integer
  120. * @param s Buffer to receive hex, must be at least (2*sizeof(i))+1 in size or overflow will occur.
  121. * @return Pointer to s containing hex string with trailing zero byte
  122. */
  123. char *hex(uint8_t i,char s[3]) noexcept;
  124. /**
  125. * Convert an unsigned integer into hex
  126. *
  127. * @param i Any unsigned integer
  128. * @param s Buffer to receive hex, must be at least (2*sizeof(i))+1 in size or overflow will occur.
  129. * @return Pointer to s containing hex string with trailing zero byte
  130. */
  131. char *hex(uint16_t i,char s[5]) noexcept;
  132. /**
  133. * Convert an unsigned integer into hex
  134. *
  135. * @param i Any unsigned integer
  136. * @param s Buffer to receive hex, must be at least (2*sizeof(i))+1 in size or overflow will occur.
  137. * @return Pointer to s containing hex string with trailing zero byte
  138. */
  139. char *hex(uint32_t i,char s[9]) noexcept;
  140. /**
  141. * Convert an unsigned integer into hex
  142. *
  143. * @param i Any unsigned integer
  144. * @param s Buffer to receive hex, must be at least (2*sizeof(i))+1 in size or overflow will occur.
  145. * @return Pointer to s containing hex string with trailing zero byte
  146. */
  147. char *hex(uint64_t i,char s[17]) noexcept;
  148. /**
  149. * Decode an unsigned integer in hex format
  150. *
  151. * @param s String to decode, non-hex chars are ignored
  152. * @return Unsigned integer
  153. */
  154. uint64_t unhex(const char *s) noexcept;
  155. /**
  156. * Convert a byte array into hex
  157. *
  158. * @param d Bytes
  159. * @param l Length of bytes
  160. * @param s String buffer, must be at least (l*2)+1 in size or overflow will occur
  161. * @return Pointer to filled string buffer
  162. */
  163. char *hex(const void *d,unsigned int l,char *s) noexcept;
  164. /**
  165. * Decode a hex string
  166. *
  167. * @param h Hex C-string (non hex chars are ignored)
  168. * @param hlen Maximum length of string (will stop at terminating zero)
  169. * @param buf Output buffer
  170. * @param buflen Length of output buffer
  171. * @return Number of written bytes
  172. */
  173. unsigned int unhex(const char *h,unsigned int hlen,void *buf,unsigned int buflen) noexcept;
  174. /**
  175. * Generate secure random bytes
  176. *
  177. * This will try to use whatever OS sources of entropy are available. It's
  178. * guarded by an internal mutex so it's thread-safe.
  179. *
  180. * @param buf Buffer to fill
  181. * @param bytes Number of random bytes to generate
  182. */
  183. void getSecureRandom(void *buf,unsigned int bytes) noexcept;
  184. /**
  185. * @return Secure random 64-bit integer
  186. */
  187. uint64_t getSecureRandomU64() noexcept;
  188. /**
  189. * Encode string to base32
  190. *
  191. * @param data Binary data to encode
  192. * @param length Length of data in bytes
  193. * @param result Result buffer
  194. * @param bufSize Size of result buffer
  195. * @return Number of bytes written
  196. */
  197. int b32e(const uint8_t *data,int length,char *result,int bufSize) noexcept;
  198. /**
  199. * Decode base32 string
  200. *
  201. * @param encoded C-string in base32 format (non-base32 characters are ignored)
  202. * @param result Result buffer
  203. * @param bufSize Size of result buffer
  204. * @return Number of bytes written or -1 on error
  205. */
  206. int b32d(const char *encoded, uint8_t *result, int bufSize) noexcept;
  207. /**
  208. * Get a non-cryptographic random integer.
  209. *
  210. * This should never be used for cryptographic use cases, not even for choosing
  211. * message nonce/IV values if they should not repeat. It should only be used when
  212. * a fast and potentially "dirty" random source is needed.
  213. */
  214. uint64_t random() noexcept;
  215. /**
  216. * Perform a safe C string copy, ALWAYS null-terminating the result
  217. *
  218. * This will never ever EVER result in dest[] not being null-terminated
  219. * regardless of any input parameter (other than len==0 which is invalid).
  220. *
  221. * @param dest Destination buffer (must not be NULL)
  222. * @param len Length of dest[] (if zero, false is returned and nothing happens)
  223. * @param src Source string (if NULL, dest will receive a zero-length string and true is returned)
  224. * @return True on success, false on overflow (buffer will still be 0-terminated)
  225. */
  226. bool scopy(char *dest,unsigned int len,const char *src) noexcept;
  227. /**
  228. * Mix bits in a 64-bit integer (non-cryptographic, for hash tables)
  229. *
  230. * https://nullprogram.com/blog/2018/07/31/
  231. *
  232. * @param x Integer to mix
  233. * @return Hashed value
  234. */
  235. static ZT_INLINE uint64_t hash64(uint64_t x) noexcept
  236. {
  237. x ^= x >> 30U;
  238. x *= 0xbf58476d1ce4e5b9ULL;
  239. x ^= x >> 27U;
  240. x *= 0x94d049bb133111ebULL;
  241. x ^= x >> 31U;
  242. return x;
  243. }
  244. /**
  245. * Mix bits in a 32-bit integer (non-cryptographic, for hash tables)
  246. *
  247. * https://nullprogram.com/blog/2018/07/31/
  248. *
  249. * @param x Integer to mix
  250. * @return Hashed value
  251. */
  252. static ZT_INLINE uint32_t hash32(uint32_t x) noexcept
  253. {
  254. x ^= x >> 16U;
  255. x *= 0x7feb352dU;
  256. x ^= x >> 15U;
  257. x *= 0x846ca68bU;
  258. x ^= x >> 16U;
  259. return x;
  260. }
  261. /**
  262. * Check if a buffer's contents are all zero
  263. */
  264. static ZT_INLINE bool allZero(const void *const b,unsigned int l) noexcept
  265. {
  266. for(unsigned int i=0;i<l;++i) {
  267. if (reinterpret_cast<const uint8_t *>(b)[i] != 0)
  268. return false;
  269. }
  270. return true;
  271. }
  272. /**
  273. * Wrapper around reentrant strtok functions, which differ in name by platform
  274. *
  275. * @param str String to tokenize or NULL for subsequent calls
  276. * @param delim Delimiter
  277. * @param saveptr Pointer to pointer where function can save state
  278. * @return Next token or NULL if none
  279. */
  280. static ZT_INLINE char *stok(char *str,const char *delim,char **saveptr) noexcept
  281. {
  282. #ifdef __WINDOWS__
  283. return strtok_s(str,delim,saveptr);
  284. #else
  285. return strtok_r(str,delim,saveptr);
  286. #endif
  287. }
  288. static ZT_INLINE unsigned int strToUInt(const char *s) noexcept
  289. {
  290. return (unsigned int)strtoul(s,nullptr,10);
  291. }
  292. static ZT_INLINE unsigned long long hexStrToU64(const char *s) noexcept
  293. {
  294. #ifdef __WINDOWS__
  295. return (unsigned long long)_strtoui64(s,nullptr,16);
  296. #else
  297. return strtoull(s,nullptr,16);
  298. #endif
  299. }
  300. /**
  301. * Compute 32-bit FNV-1a checksum
  302. *
  303. * See: http://www.isthe.com/chongo/tech/comp/fnv/
  304. *
  305. * @param data Data to checksum
  306. * @param len Length of data
  307. * @return FNV1a checksum
  308. */
  309. static ZT_INLINE uint32_t fnv1a32(const void *const data,const unsigned int len) noexcept
  310. {
  311. uint32_t h = 0x811c9dc5;
  312. const uint32_t p = 0x01000193;
  313. for(unsigned int i=0;i<len;++i)
  314. h = (h ^ (uint32_t)reinterpret_cast<const uint8_t *>(data)[i]) * p;
  315. return h;
  316. }
  317. #ifdef __GNUC__
  318. static ZT_INLINE unsigned int countBits(const uint8_t v) noexcept { return (unsigned int)__builtin_popcount((unsigned int)v); }
  319. static ZT_INLINE unsigned int countBits(const uint16_t v) noexcept { return (unsigned int)__builtin_popcount((unsigned int)v); }
  320. static ZT_INLINE unsigned int countBits(const uint32_t v) noexcept { return (unsigned int)__builtin_popcountl((unsigned long)v); }
  321. static ZT_INLINE unsigned int countBits(const uint64_t v) noexcept{ return (unsigned int)__builtin_popcountll((unsigned long long)v); }
  322. #else
  323. template<typename T>
  324. static ZT_INLINE unsigned int countBits(T v) noexcept
  325. {
  326. v = v - ((v >> 1) & (T)~(T)0/3);
  327. v = (v & (T)~(T)0/15*3) + ((v >> 2) & (T)~(T)0/15*3);
  328. v = (v + (v >> 4)) & (T)~(T)0/255*15;
  329. return (unsigned int)((v * ((~((T)0))/((T)255))) >> ((sizeof(T) - 1) * 8));
  330. }
  331. #endif
  332. /**
  333. * Unconditionally swap bytes regardless of host byte order
  334. *
  335. * @param n Integer to swap
  336. * @return Integer with bytes reversed
  337. */
  338. static ZT_INLINE uint64_t swapBytes(const uint64_t n) noexcept
  339. {
  340. #ifdef __GNUC__
  341. return __builtin_bswap64(n);
  342. #else
  343. #ifdef _MSC_VER
  344. return (uint64_t)_byteswap_uint64((unsigned __int64)n);
  345. #else
  346. return (
  347. ((n & 0x00000000000000ffULL) << 56) |
  348. ((n & 0x000000000000ff00ULL) << 40) |
  349. ((n & 0x0000000000ff0000ULL) << 24) |
  350. ((n & 0x00000000ff000000ULL) << 8) |
  351. ((n & 0x000000ff00000000ULL) >> 8) |
  352. ((n & 0x0000ff0000000000ULL) >> 24) |
  353. ((n & 0x00ff000000000000ULL) >> 40) |
  354. ((n & 0xff00000000000000ULL) >> 56)
  355. );
  356. #endif
  357. #endif
  358. }
  359. /**
  360. * Unconditionally swap bytes regardless of host byte order
  361. *
  362. * @param n Integer to swap
  363. * @return Integer with bytes reversed
  364. */
  365. static ZT_INLINE uint32_t swapBytes(const uint32_t n) noexcept
  366. {
  367. #if defined(__GNUC__)
  368. return __builtin_bswap32(n);
  369. #else
  370. #ifdef _MSC_VER
  371. return (uint32_t)_byteswap_ulong((unsigned long)n);
  372. #else
  373. return htonl(n);
  374. #endif
  375. #endif
  376. }
  377. /**
  378. * Unconditionally swap bytes regardless of host byte order
  379. *
  380. * @param n Integer to swap
  381. * @return Integer with bytes reversed
  382. */
  383. static ZT_INLINE uint16_t swapBytes(const uint16_t n) noexcept
  384. {
  385. #if defined(__GNUC__)
  386. return __builtin_bswap16(n);
  387. #else
  388. #ifdef _MSC_VER
  389. return (uint16_t)_byteswap_ushort((unsigned short)n);
  390. #else
  391. return htons(n);
  392. #endif
  393. #endif
  394. }
  395. // These are helper adapters to load and swap integer types special cased by size
  396. // to work with all typedef'd variants, signed/unsigned, etc.
  397. template<typename I,unsigned int S>
  398. class _swap_bytes_bysize;
  399. template<typename I>
  400. class _swap_bytes_bysize<I,1> { public: static ZT_INLINE I s(const I n) noexcept { return n; } };
  401. template<typename I>
  402. class _swap_bytes_bysize<I,2> { public: static ZT_INLINE I s(const I n) noexcept { return (I)swapBytes((uint16_t)n); } };
  403. template<typename I>
  404. class _swap_bytes_bysize<I,4> { public: static ZT_INLINE I s(const I n) noexcept { return (I)swapBytes((uint32_t)n); } };
  405. template<typename I>
  406. class _swap_bytes_bysize<I,8> { public: static ZT_INLINE I s(const I n) noexcept { return (I)swapBytes((uint64_t)n); } };
  407. template<typename I,unsigned int S>
  408. class _load_be_bysize;
  409. template<typename I>
  410. class _load_be_bysize<I,1> { public: static ZT_INLINE I l(const uint8_t *const p) noexcept { return p[0]; }};
  411. template<typename I>
  412. class _load_be_bysize<I,2> { public: static ZT_INLINE I l(const uint8_t *const p) noexcept { return (I)(((unsigned int)p[0] << 8U) | (unsigned int)p[1]); }};
  413. template<typename I>
  414. class _load_be_bysize<I,4> { public: static ZT_INLINE I l(const uint8_t *const p) noexcept { return (I)(((uint32_t)p[0] << 24U) | ((uint32_t)p[1] << 16U) | ((uint32_t)p[2] << 8U) | (uint32_t)p[3]); }};
  415. template<typename I>
  416. class _load_be_bysize<I,8> { public: static ZT_INLINE I l(const uint8_t *const p) noexcept { 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]); }};
  417. template<typename I,unsigned int S>
  418. class _load_le_bysize;
  419. template<typename I>
  420. class _load_le_bysize<I,1> { public: static ZT_INLINE I l(const uint8_t *const p) noexcept { return p[0]; }};
  421. template<typename I>
  422. class _load_le_bysize<I,2> { public: static ZT_INLINE I l(const uint8_t *const p) noexcept { return (I)((unsigned int)p[0] | ((unsigned int)p[1] << 8U)); }};
  423. template<typename I>
  424. class _load_le_bysize<I,4> { public: static ZT_INLINE I l(const uint8_t *const p) noexcept { return (I)((uint32_t)p[0] | ((uint32_t)p[1] << 8U) | ((uint32_t)p[2] << 16U) | ((uint32_t)p[3] << 24U)); }};
  425. template<typename I>
  426. class _load_le_bysize<I,8> { public: static ZT_INLINE I l(const uint8_t *const p) noexcept { 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); }};
  427. /**
  428. * Convert any signed or unsigned integer type to big-endian ("network") byte order
  429. *
  430. * @tparam I Integer type (usually inferred)
  431. * @param n Value to convert
  432. * @return Value in big-endian order
  433. */
  434. template<typename I>
  435. static ZT_INLINE I hton(const I n) noexcept
  436. {
  437. #if __BYTE_ORDER == __LITTLE_ENDIAN
  438. return _swap_bytes_bysize<I,sizeof(I)>::s(n);
  439. #else
  440. return n;
  441. #endif
  442. }
  443. /**
  444. * Convert any signed or unsigned integer type to host byte order from big-endian ("network") byte order
  445. *
  446. * @tparam I Integer type (usually inferred)
  447. * @param n Value to convert
  448. * @return Value in host byte order
  449. */
  450. template<typename I>
  451. static ZT_INLINE I ntoh(const I n) noexcept
  452. {
  453. #if __BYTE_ORDER == __LITTLE_ENDIAN
  454. return _swap_bytes_bysize<I,sizeof(I)>::s(n);
  455. #else
  456. return n;
  457. #endif
  458. }
  459. /**
  460. * Copy bits from memory into an integer type without modifying their order
  461. *
  462. * @tparam I Type to load
  463. * @param p Byte stream, must be at least sizeof(I) in size
  464. * @return Loaded raw integer
  465. */
  466. template<typename I>
  467. static ZT_INLINE I loadAsIsEndian(const void *const p) noexcept
  468. {
  469. #ifdef ZT_NO_UNALIGNED_ACCESS
  470. I tmp;
  471. for(int i=0;i<(int)sizeof(I);++i)
  472. reinterpret_cast<uint8_t *>(&tmp)[i] = reinterpret_cast<const uint8_t *>(p)[i];
  473. return tmp;
  474. #else
  475. return *reinterpret_cast<const I *>(p);
  476. #endif
  477. }
  478. /**
  479. * Copy bits from memory into an integer type without modifying their order
  480. *
  481. * @tparam I Type to store
  482. * @param p Byte array (must be at least sizeof(I))
  483. * @param i Integer to store
  484. */
  485. template<typename I>
  486. static ZT_INLINE void storeAsIsEndian(void *const p,const I i) noexcept
  487. {
  488. #ifdef ZT_NO_UNALIGNED_ACCESS
  489. for(unsigned int k=0;k<sizeof(I);++k)
  490. reinterpret_cast<uint8_t *>(p)[k] = reinterpret_cast<const uint8_t *>(&i)[k];
  491. #else
  492. *reinterpret_cast<I *>(p) = i;
  493. #endif
  494. }
  495. /**
  496. * Decode a big-endian value from a byte stream
  497. *
  498. * @tparam I Type to decode (should be unsigned e.g. uint32_t or uint64_t)
  499. * @param p Byte stream, must be at least sizeof(I) in size
  500. * @return Decoded integer
  501. */
  502. template<typename I>
  503. static ZT_INLINE I loadBigEndian(const void *const p) noexcept
  504. {
  505. #ifdef ZT_NO_UNALIGNED_ACCESS
  506. return _load_be_bysize<I,sizeof(I)>::l(reinterpret_cast<const uint8_t *>(p));
  507. #else
  508. return ntoh(*reinterpret_cast<const I *>(p));
  509. #endif
  510. }
  511. /**
  512. * Save an integer in big-endian format
  513. *
  514. * @tparam I Integer type to store (usually inferred)
  515. * @param p Byte stream to write (must be at least sizeof(I))
  516. * #param i Integer to write
  517. */
  518. template<typename I>
  519. static ZT_INLINE void storeBigEndian(void *const p,I i) noexcept
  520. {
  521. #ifdef ZT_NO_UNALIGNED_ACCESS
  522. storeAsIsEndian(p,hton(i));
  523. #else
  524. *reinterpret_cast<I *>(p) = hton(i);
  525. #endif
  526. }
  527. /**
  528. * Decode a little-endian value from a byte stream
  529. *
  530. * @tparam I Type to decode
  531. * @param p Byte stream, must be at least sizeof(I) in size
  532. * @return Decoded integer
  533. */
  534. template<typename I>
  535. static ZT_INLINE I loadLittleEndian(const void *const p) noexcept
  536. {
  537. #if __BYTE_ORDER == __BIG_ENDIAN || defined(ZT_NO_UNALIGNED_ACCESS)
  538. return _load_le_bysize<I,sizeof(I)>::l(reinterpret_cast<const uint8_t *>(p));
  539. #else
  540. return *reinterpret_cast<const I *>(p);
  541. #endif
  542. }
  543. /**
  544. * Save an integer in little-endian format
  545. *
  546. * @tparam I Integer type to store (usually inferred)
  547. * @param p Byte stream to write (must be at least sizeof(I))
  548. * #param i Integer to write
  549. */
  550. template<typename I>
  551. static ZT_INLINE void storeLittleEndian(void *const p,const I i) noexcept
  552. {
  553. #if __BYTE_ORDER == __BIG_ENDIAN
  554. storeAsIsEndian(p,_swap_bytes_bysize<I,sizeof(I)>::s(i));
  555. #else
  556. #ifdef ZT_NO_UNALIGNED_ACCESS
  557. storeAsIsEndian(p,i);
  558. #else
  559. *reinterpret_cast<I *>(p) = i;
  560. #endif
  561. #endif
  562. }
  563. /**
  564. * Copy memory block whose size is known at compile time.
  565. *
  566. * @tparam L Size of memory
  567. * @param dest Destination memory
  568. * @param src Source memory
  569. */
  570. template<unsigned int L>
  571. static ZT_INLINE void copy(void *const dest,const void *const src) noexcept
  572. {
  573. #ifdef ZT_ARCH_X64
  574. uint8_t *volatile d = reinterpret_cast<uint8_t *>(dest);
  575. const uint8_t *s = reinterpret_cast<const uint8_t *>(src);
  576. for(unsigned int i=0;i<(L >> 6U);++i) {
  577. __m128i x0 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(s));
  578. __m128i x1 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(s + 16));
  579. __m128i x2 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(s + 32));
  580. __m128i x3 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(s + 48));
  581. s += 64;
  582. _mm_storeu_si128(reinterpret_cast<__m128i *>(d),x0);
  583. _mm_storeu_si128(reinterpret_cast<__m128i *>(d + 16),x1);
  584. _mm_storeu_si128(reinterpret_cast<__m128i *>(d + 32),x2);
  585. _mm_storeu_si128(reinterpret_cast<__m128i *>(d + 48),x3);
  586. d += 64;
  587. }
  588. if ((L & 32U) != 0) {
  589. __m128i x0 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(s));
  590. __m128i x1 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(s + 16));
  591. s += 32;
  592. _mm_storeu_si128(reinterpret_cast<__m128i *>(d),x0);
  593. _mm_storeu_si128(reinterpret_cast<__m128i *>(d + 16),x1);
  594. d += 32;
  595. }
  596. if ((L & 16U) != 0) {
  597. __m128i x0 = _mm_loadu_si128(reinterpret_cast<const __m128i *>(s));
  598. s += 16;
  599. _mm_storeu_si128(reinterpret_cast<__m128i *>(d),x0);
  600. d += 16;
  601. }
  602. if ((L & 8U) != 0) {
  603. *reinterpret_cast<volatile uint64_t *>(d) = *reinterpret_cast<const uint64_t *>(s);
  604. s += 8;
  605. d += 8;
  606. }
  607. if ((L & 4U) != 0) {
  608. *reinterpret_cast<volatile uint32_t *>(d) = *reinterpret_cast<const uint32_t *>(s);
  609. s += 4;
  610. d += 4;
  611. }
  612. if ((L & 2U) != 0) {
  613. *reinterpret_cast<volatile uint16_t *>(d) = *reinterpret_cast<const uint16_t *>(s);
  614. s += 2;
  615. d += 2;
  616. }
  617. if ((L & 1U) != 0) {
  618. *d = *s;
  619. }
  620. #else
  621. memcpy(dest,src,L);
  622. #endif
  623. }
  624. /**
  625. * Copy memory block whose size is known at run time
  626. *
  627. * @param dest Destination memory
  628. * @param src Source memory
  629. * @param len Bytes to copy
  630. */
  631. static ZT_INLINE void copy(void *const dest,const void *const src,unsigned int len) noexcept
  632. {
  633. memcpy(dest,src,len);
  634. }
  635. /**
  636. * Zero memory block whose size is known at compile time
  637. *
  638. * @tparam L Size in bytes
  639. * @param dest Memory to zero
  640. */
  641. template<unsigned int L>
  642. static ZT_INLINE void zero(void *const dest) noexcept
  643. {
  644. #ifdef ZT_ARCH_X64
  645. uint8_t *volatile d = reinterpret_cast<uint8_t *>(dest);
  646. __m128i z = _mm_setzero_si128();
  647. for(unsigned int i=0;i<(L >> 6U);++i) {
  648. _mm_storeu_si128(reinterpret_cast<__m128i *>(d),z);
  649. _mm_storeu_si128(reinterpret_cast<__m128i *>(d + 16),z);
  650. _mm_storeu_si128(reinterpret_cast<__m128i *>(d + 32),z);
  651. _mm_storeu_si128(reinterpret_cast<__m128i *>(d + 48),z);
  652. d += 64;
  653. }
  654. if ((L & 32U) != 0) {
  655. _mm_storeu_si128(reinterpret_cast<__m128i *>(d),z);
  656. _mm_storeu_si128(reinterpret_cast<__m128i *>(d + 16),z);
  657. d += 32;
  658. }
  659. if ((L & 16U) != 0) {
  660. _mm_storeu_si128(reinterpret_cast<__m128i *>(d),z);
  661. d += 16;
  662. }
  663. if ((L & 8U) != 0) {
  664. *reinterpret_cast<volatile uint64_t *>(d) = 0;
  665. d += 8;
  666. }
  667. if ((L & 4U) != 0) {
  668. *reinterpret_cast<volatile uint32_t *>(d) = 0;
  669. d += 4;
  670. }
  671. if ((L & 2U) != 0) {
  672. *reinterpret_cast<volatile uint16_t *>(d) = 0;
  673. d += 2;
  674. }
  675. if ((L & 1U) != 0) {
  676. *d = 0;
  677. }
  678. #else
  679. memset(dest,0,L);
  680. #endif
  681. }
  682. /**
  683. * Zero memory block whose size is known at run time
  684. *
  685. * @param dest Memory to zero
  686. * @param len Size in bytes
  687. */
  688. static ZT_INLINE void zero(void *const dest,const unsigned int len) noexcept
  689. {
  690. memset(dest,0,len);
  691. }
  692. } // namespace Utils
  693. } // namespace ZeroTier
  694. #endif