Utils.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef _ZT_UTILS_HPP
  28. #define _ZT_UTILS_HPP
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <stdint.h>
  32. #include <string.h>
  33. #include <time.h>
  34. #include <string>
  35. #include <stdexcept>
  36. #include <vector>
  37. #include <map>
  38. #include "Constants.hpp"
  39. #include "../ext/lz4/lz4.h"
  40. #include "../ext/lz4/lz4hc.h"
  41. #ifdef __WINDOWS__
  42. #include <WinSock2.h>
  43. #include <Windows.h>
  44. #else
  45. #include <unistd.h>
  46. #include <sys/time.h>
  47. #include <arpa/inet.h>
  48. #endif
  49. /**
  50. * Maximum compression/decompression block size (do not change)
  51. */
  52. #define ZT_COMPRESSION_BLOCK_SIZE 16777216
  53. namespace ZeroTier {
  54. /**
  55. * Miscellaneous utility functions and global constants
  56. */
  57. class Utils
  58. {
  59. public:
  60. /**
  61. * Delete a file
  62. *
  63. * @param path Path to delete
  64. * @return True if delete was successful
  65. */
  66. static inline bool rm(const char *path)
  67. throw()
  68. {
  69. #ifdef __WINDOWS__
  70. return (DeleteFileA(path) != FALSE);
  71. #else
  72. return (unlink(path) == 0);
  73. #endif
  74. }
  75. static inline bool rm(const std::string &path)
  76. throw()
  77. {
  78. return rm(path.c_str());
  79. }
  80. /**
  81. * List a directory's contents
  82. *
  83. * @param path Path to list
  84. * @param files Set to fill with files
  85. * @param directories Set to fill with directories
  86. * @return Map of entries and whether or not they are also directories (empty on failure)
  87. */
  88. static std::map<std::string,bool> listDirectory(const char *path);
  89. /**
  90. * @param data Data to convert to hex
  91. * @param len Length of data
  92. * @return Hexadecimal string
  93. */
  94. static std::string hex(const void *data,unsigned int len);
  95. static inline std::string hex(const std::string &data) { return hex(data.data(),(unsigned int)data.length()); }
  96. /**
  97. * @param hex Hexadecimal ASCII code (non-hex chars are ignored)
  98. * @return Binary data
  99. */
  100. static std::string unhex(const char *hex);
  101. static inline std::string unhex(const std::string &hex) { return unhex(hex.c_str()); }
  102. /**
  103. * @param hex Hexadecimal ASCII
  104. * @param buf Buffer to fill
  105. * @param len Length of buffer
  106. * @return Number of characters actually written
  107. */
  108. static unsigned int unhex(const char *hex,void *buf,unsigned int len);
  109. static inline unsigned int unhex(const std::string &hex,void *buf,unsigned int len) { return unhex(hex.c_str(),buf,len); }
  110. /**
  111. * @param buf Buffer to fill
  112. * @param bytes Number of random bytes to generate
  113. */
  114. static void getSecureRandom(void *buf,unsigned int bytes);
  115. /**
  116. * Set modes on a file to something secure
  117. *
  118. * This locks a file so that only the owner can access it. What it actually
  119. * does varies by platform.
  120. *
  121. * @param path Path to lock
  122. * @param isDir True if this is a directory
  123. */
  124. static void lockDownFile(const char *path,bool isDir);
  125. /**
  126. * Get file last modification time
  127. *
  128. * Resolution is often only second, not millisecond, but the return is
  129. * always in ms for comparison against now().
  130. *
  131. * @param path Path to file to get time
  132. * @return Last modification time in ms since epoch or 0 if not found
  133. */
  134. static uint64_t getLastModified(const char *path);
  135. /**
  136. * @param path Path to check
  137. * @return True if file or directory exists at path location
  138. */
  139. static inline bool fileExists(const char *path)
  140. {
  141. return (getLastModified(path) != 0);
  142. }
  143. /**
  144. * @param t64 Time in ms since epoch
  145. * @return RFC1123 date string
  146. */
  147. static std::string toRfc1123(uint64_t t64);
  148. /**
  149. * @param tstr Time in RFC1123 string format
  150. * @return Time in ms since epoch
  151. */
  152. static uint64_t fromRfc1123(const char *tstr);
  153. static inline uint64_t fromRfc1123(const std::string &tstr) { return fromRfc1123(tstr.c_str()); }
  154. /**
  155. * String append output function object for use with compress/decompress
  156. */
  157. class StringAppendOutput
  158. {
  159. public:
  160. StringAppendOutput(std::string &s) : _s(s) {}
  161. inline void operator()(const void *data,unsigned int len) { _s.append((const char *)data,len); }
  162. private:
  163. std::string &_s;
  164. };
  165. /**
  166. * STDIO FILE append output function object for compress/decompress
  167. *
  168. * Throws std::runtime_error on write error.
  169. */
  170. class FILEAppendOutput
  171. {
  172. public:
  173. FILEAppendOutput(FILE *f) : _f(f) {}
  174. inline void operator()(const void *data,unsigned int len)
  175. throw(std::runtime_error)
  176. {
  177. if ((int)fwrite(data,1,len,_f) != (int)len)
  178. throw std::runtime_error("write failed");
  179. }
  180. private:
  181. FILE *_f;
  182. };
  183. /**
  184. * Compress data
  185. *
  186. * O must be a function or function object that takes the following
  187. * arguments: (const void *data,unsigned int len)
  188. *
  189. * @param in Input iterator that reads bytes (char, uint8_t, etc.)
  190. * @param out Output iterator that writes bytes
  191. */
  192. template<typename I,typename O>
  193. static inline void compress(I begin,I end,O out)
  194. {
  195. unsigned int bufLen = LZ4_compressBound(ZT_COMPRESSION_BLOCK_SIZE);
  196. char *buf = new char[bufLen * 2];
  197. char *buf2 = buf + bufLen;
  198. try {
  199. I inp(begin);
  200. for(;;) {
  201. unsigned int readLen = 0;
  202. while ((readLen < ZT_COMPRESSION_BLOCK_SIZE)&&(inp != end)) {
  203. buf[readLen++] = (char)*inp;
  204. ++inp;
  205. }
  206. if (!readLen)
  207. break;
  208. uint32_t l = hton((uint32_t)readLen);
  209. out((const void *)&l,4); // original size
  210. if (readLen < 32) { // don't bother compressing itty bitty blocks
  211. l = 0; // stored
  212. out((const void *)&l,4);
  213. out((const void *)buf,readLen);
  214. continue;
  215. }
  216. int lz4CompressedLen = LZ4_compressHC(buf,buf2,(int)readLen);
  217. if ((lz4CompressedLen <= 0)||(lz4CompressedLen >= (int)readLen)) {
  218. l = 0; // stored
  219. out((const void *)&l,4);
  220. out((const void *)buf,readLen);
  221. continue;
  222. }
  223. l = hton((uint32_t)lz4CompressedLen); // lz4 only
  224. out((const void *)&l,4);
  225. out((const void *)buf2,(unsigned int)lz4CompressedLen);
  226. }
  227. delete [] buf;
  228. } catch ( ... ) {
  229. delete [] buf;
  230. throw;
  231. }
  232. }
  233. /**
  234. * Decompress data
  235. *
  236. * O must be a function or function object that takes the following
  237. * arguments: (const void *data,unsigned int len)
  238. *
  239. * @param in Input iterator that reads bytes (char, uint8_t, etc.)
  240. * @param out Output iterator that writes bytes
  241. * @return False on decompression error
  242. */
  243. template<typename I,typename O>
  244. static inline bool decompress(I begin,I end,O out)
  245. {
  246. volatile char i32c[4];
  247. void *const i32cp = (void *)i32c;
  248. unsigned int bufLen = LZ4_compressBound(ZT_COMPRESSION_BLOCK_SIZE);
  249. char *buf = new char[bufLen * 2];
  250. char *buf2 = buf + bufLen;
  251. try {
  252. I inp(begin);
  253. while (inp != end) {
  254. i32c[0] = (char)*inp; if (++inp == end) { delete [] buf; return false; }
  255. i32c[1] = (char)*inp; if (++inp == end) { delete [] buf; return false; }
  256. i32c[2] = (char)*inp; if (++inp == end) { delete [] buf; return false; }
  257. i32c[3] = (char)*inp; if (++inp == end) { delete [] buf; return false; }
  258. unsigned int originalSize = ntoh(*((const uint32_t *)i32cp));
  259. i32c[0] = (char)*inp; if (++inp == end) { delete [] buf; return false; }
  260. i32c[1] = (char)*inp; if (++inp == end) { delete [] buf; return false; }
  261. i32c[2] = (char)*inp; if (++inp == end) { delete [] buf; return false; }
  262. i32c[3] = (char)*inp; if (++inp == end) { delete [] buf; return false; }
  263. uint32_t _compressedSize = ntoh(*((const uint32_t *)i32cp));
  264. unsigned int compressedSize = _compressedSize & 0x7fffffff;
  265. if (compressedSize) {
  266. if (compressedSize > bufLen) {
  267. delete [] buf;
  268. return false;
  269. }
  270. unsigned int readLen = 0;
  271. while ((readLen < compressedSize)&&(inp != end)) {
  272. buf[readLen++] = (char)*inp;
  273. ++inp;
  274. }
  275. if (readLen != compressedSize) {
  276. delete [] buf;
  277. return false;
  278. }
  279. if (LZ4_uncompress_unknownOutputSize(buf,buf2,compressedSize,bufLen) != (int)originalSize) {
  280. delete [] buf;
  281. return false;
  282. } else out((const void *)buf2,(unsigned int)originalSize);
  283. } else { // stored
  284. if (originalSize > bufLen) {
  285. delete [] buf;
  286. return false;
  287. }
  288. unsigned int readLen = 0;
  289. while ((readLen < originalSize)&&(inp != end)) {
  290. buf[readLen++] = (char)*inp;
  291. ++inp;
  292. }
  293. if (readLen != originalSize) {
  294. delete [] buf;
  295. return false;
  296. }
  297. out((const void *)buf,(unsigned int)originalSize);
  298. }
  299. }
  300. delete [] buf;
  301. return true;
  302. } catch ( ... ) {
  303. delete [] buf;
  304. throw;
  305. }
  306. }
  307. /**
  308. * @return Current time in milliseconds since epoch
  309. */
  310. static inline uint64_t now()
  311. throw()
  312. {
  313. #ifdef __WINDOWS__
  314. FILETIME ft;
  315. SYSTEMTIME st;
  316. ULARGE_INTEGER tmp;
  317. GetSystemTime(&st);
  318. SystemTimeToFileTime(&st,&ft);
  319. tmp.LowPart = ft.dwLowDateTime;
  320. tmp.HighPart = ft.dwHighDateTime;
  321. return ( ((tmp.QuadPart - 116444736000000000ULL) / 10000L) + st.wMilliseconds );
  322. #else
  323. struct timeval tv;
  324. gettimeofday(&tv,(struct timezone *)0);
  325. return ( (1000ULL * (uint64_t)tv.tv_sec) + (uint64_t)(tv.tv_usec / 1000) );
  326. #endif
  327. };
  328. /**
  329. * @return Current time in seconds since epoch, to the highest available resolution
  330. */
  331. static inline double nowf()
  332. throw()
  333. {
  334. #ifdef __WINDOWS__
  335. FILETIME ft;
  336. SYSTEMTIME st;
  337. ULARGE_INTEGER tmp;
  338. GetSystemTime(&st);
  339. SystemTimeToFileTime(&st,&ft);
  340. tmp.LowPart = ft.dwLowDateTime;
  341. tmp.HighPart = ft.dwHighDateTime;
  342. return (((double)(tmp.QuadPart - 116444736000000000ULL)) / 10000000.0);
  343. #else
  344. struct timeval tv;
  345. gettimeofday(&tv,(struct timezone *)0);
  346. return ( ((double)tv.tv_sec) + (((double)tv.tv_usec) / 1000000.0) );
  347. #endif
  348. }
  349. /**
  350. * Read the full contents of a file into a string buffer
  351. *
  352. * The buffer isn't cleared, so if it already contains data the file's data will
  353. * be appended.
  354. *
  355. * @param path Path of file to read
  356. * @param buf Buffer to fill
  357. * @return True if open and read successful
  358. */
  359. static bool readFile(const char *path,std::string &buf);
  360. /**
  361. * Write a block of data to disk, replacing any current file contents
  362. *
  363. * @param path Path to write
  364. * @param buf Buffer containing data
  365. * @param len Length of buffer
  366. * @return True if entire file was successfully written
  367. */
  368. static bool writeFile(const char *path,const void *buf,unsigned int len);
  369. /**
  370. * Write a block of data to disk, replacing any current file contents
  371. *
  372. * @param path Path to write
  373. * @param s Data to write
  374. * @return True if entire file was successfully written
  375. */
  376. static inline bool writeFile(const char *path,const std::string &s)
  377. {
  378. return writeFile(path,s.data(),(unsigned int)s.length());
  379. }
  380. /**
  381. * @param data Binary data to encode
  382. * @param len Length of data
  383. * @return Base64-encoded string
  384. */
  385. static std::string base64Encode(const void *data,unsigned int len);
  386. inline static std::string base64Encode(const std::string &data) { return base64Encode(data.data(),(unsigned int)data.length()); }
  387. /**
  388. * @param data Base64-encoded string
  389. * @param len Length of encoded string
  390. * @return Decoded binary date
  391. */
  392. static std::string base64Decode(const char *data,unsigned int len);
  393. inline static std::string base64Decode(const std::string &data) { return base64Decode(data.data(),(unsigned int)data.length()); }
  394. /**
  395. * Split a string by delimiter, with optional escape and quote characters
  396. *
  397. * @param s String to split
  398. * @param sep One or more separators
  399. * @param esc Zero or more escape characters
  400. * @param quot Zero or more quote characters
  401. * @return Vector of tokens
  402. */
  403. static std::vector<std::string> split(const char *s,const char *const sep,const char *esc,const char *quot);
  404. /**
  405. * Tokenize a string
  406. *
  407. * @param str String to split
  408. * @param delim Delimiters
  409. * @param saveptr Pointer to a char * for temporary reentrant storage
  410. */
  411. static inline char *stok(char *str,const char *delim,char **saveptr)
  412. throw()
  413. {
  414. #ifdef __WINDOWS__
  415. return strtok_s(str,delim,saveptr);
  416. #else
  417. return strtok_r(str,delim,saveptr);
  418. #endif
  419. }
  420. // String to int converters (and hex string to int)
  421. static inline unsigned int stoui(const char *s)
  422. throw()
  423. {
  424. return (unsigned int)strtoul(s,(char **)0,10);
  425. }
  426. static inline unsigned long stoul(const char *s)
  427. throw()
  428. {
  429. return strtoul(s,(char **)0,10);
  430. }
  431. static inline unsigned long long stoull(const char *s)
  432. throw()
  433. {
  434. return strtoull(s,(char **)0,10);
  435. }
  436. static inline unsigned int hstoui(const char *s)
  437. throw()
  438. {
  439. return (unsigned int)strtoul(s,(char **)0,16);
  440. }
  441. static inline unsigned long hstoul(const char *s)
  442. throw()
  443. {
  444. return strtoul(s,(char **)0,16);
  445. }
  446. static inline unsigned long long hstoull(const char *s)
  447. throw()
  448. {
  449. return strtoull(s,(char **)0,16);
  450. }
  451. /**
  452. * Perform a safe C string copy
  453. *
  454. * @param dest Destination buffer
  455. * @param len Length of buffer
  456. * @param src Source string
  457. * @return True on success, false on overflow (buffer will still be 0-terminated)
  458. */
  459. static inline bool scopy(char *dest,unsigned int len,const char *src)
  460. throw()
  461. {
  462. if (!len)
  463. return false; // sanity check
  464. char *end = dest + len;
  465. while ((*dest++ = *src++)) {
  466. if (dest == end) {
  467. dest[len - 1] = (char)0;
  468. return false;
  469. }
  470. }
  471. return true;
  472. }
  473. /**
  474. * Trim whitespace from the start and end of a string
  475. *
  476. * @param s String to trim
  477. * @return Trimmed string
  478. */
  479. static std::string trim(const std::string &s);
  480. /**
  481. * Like sprintf, but appends to std::string
  482. *
  483. * @param s String to append to
  484. * @param fmt Printf format string
  485. * @param ... Format arguments
  486. * @throws std::bad_alloc Memory allocation failure
  487. * @throws std::length_error Format + args exceeds internal buffer maximum
  488. */
  489. static void stdsprintf(std::string &s,const char *fmt,...)
  490. throw(std::bad_alloc,std::length_error);
  491. /**
  492. * Variant of snprintf that is portable and throws an exception
  493. *
  494. * @param buf Buffer to write to
  495. * @param len Length of buffer in bytes
  496. * @param fmt Format string
  497. * @param ... Format arguments
  498. * @throws std::length_error buf[] too short (buf[] will still be left null-terminated)
  499. */
  500. static unsigned int snprintf(char *buf,unsigned int len,const char *fmt,...)
  501. throw(std::length_error);
  502. /**
  503. * Count the number of bits set in an integer
  504. *
  505. * @param v 32-bit integer
  506. * @return Number of bits set in this integer (0-32)
  507. */
  508. static inline uint32_t countBits(uint32_t v)
  509. throw()
  510. {
  511. v = v - ((v >> 1) & (uint32_t)0x55555555);
  512. v = (v & (uint32_t)0x33333333) + ((v >> 2) & (uint32_t)0x33333333);
  513. return ((((v + (v >> 4)) & (uint32_t)0xF0F0F0F) * (uint32_t)0x1010101) >> 24);
  514. }
  515. /**
  516. * Check if a memory buffer is all-zero
  517. *
  518. * @param p Memory to scan
  519. * @param len Length of memory
  520. * @return True if memory is all zero
  521. */
  522. static inline bool isZero(const void *p,unsigned int len)
  523. throw()
  524. {
  525. for(unsigned int i=0;i<len;++i) {
  526. if (((const unsigned char *)p)[i])
  527. return false;
  528. }
  529. return true;
  530. }
  531. /**
  532. * Match two strings with bits masked netmask-style
  533. *
  534. * @param a First string
  535. * @param abits Number of bits in first string
  536. * @param b Second string
  537. * @param bbits Number of bits in second string
  538. * @return True if min(abits,bbits) match between a and b
  539. */
  540. static inline bool matchNetmask(const void *a,unsigned int abits,const void *b,unsigned int bbits)
  541. throw()
  542. {
  543. const unsigned char *aptr = (const unsigned char *)a;
  544. const unsigned char *bptr = (const unsigned char *)b;
  545. while ((abits >= 8)&&(bbits >= 8)) {
  546. if (*aptr++ != *bptr++)
  547. return false;
  548. abits -= 8;
  549. bbits -= 8;
  550. }
  551. unsigned char mask = 0xff << (8 - ((abits > bbits) ? bbits : abits));
  552. return ((*aptr & mask) == (*aptr & mask));
  553. }
  554. /**
  555. * Compute CRC64
  556. *
  557. * @param crc Previous CRC (0 to start)
  558. * @param s String to add to crc
  559. * @param l Length of string in bytes
  560. * @return New CRC
  561. */
  562. static inline uint64_t crc64(uint64_t crc,const void *s,unsigned int l)
  563. throw()
  564. {
  565. for(unsigned int i=0;i<l;++i)
  566. crc = crc64Table[(uint8_t)crc ^ ((const uint8_t *)s)[i]] ^ (crc >> 8);
  567. return crc;
  568. }
  569. static inline uint8_t hton(uint8_t n) throw() { return n; }
  570. static inline int8_t hton(int8_t n) throw() { return n; }
  571. static inline uint16_t hton(uint16_t n) throw() { return htons(n); }
  572. static inline int16_t hton(int16_t n) throw() { return (int16_t)htons((uint16_t)n); }
  573. static inline uint32_t hton(uint32_t n) throw() { return htonl(n); }
  574. static inline int32_t hton(int32_t n) throw() { return (int32_t)htonl((uint32_t)n); }
  575. static inline uint64_t hton(uint64_t n)
  576. throw()
  577. {
  578. #if __BYTE_ORDER == __LITTLE_ENDIAN
  579. #ifdef __GNUC__
  580. return __builtin_bswap64(n);
  581. #else
  582. return (
  583. ((n & 0x00000000000000FFULL) << 56) |
  584. ((n & 0x000000000000FF00ULL) << 40) |
  585. ((n & 0x0000000000FF0000ULL) << 24) |
  586. ((n & 0x00000000FF000000ULL) << 8) |
  587. ((n & 0x000000FF00000000ULL) >> 8) |
  588. ((n & 0x0000FF0000000000ULL) >> 24) |
  589. ((n & 0x00FF000000000000ULL) >> 40) |
  590. ((n & 0xFF00000000000000ULL) >> 56)
  591. );
  592. #endif
  593. #else
  594. return n;
  595. #endif
  596. }
  597. static inline int64_t hton(int64_t n) throw() { return (int64_t)hton((uint64_t)n); }
  598. static inline uint8_t ntoh(uint8_t n) throw() { return n; }
  599. static inline int8_t ntoh(int8_t n) throw() { return n; }
  600. static inline uint16_t ntoh(uint16_t n) throw() { return ntohs(n); }
  601. static inline int16_t ntoh(int16_t n) throw() { return (int16_t)ntohs((uint16_t)n); }
  602. static inline uint32_t ntoh(uint32_t n) throw() { return ntohl(n); }
  603. static inline int32_t ntoh(int32_t n) throw() { return (int32_t)ntohl((uint32_t)n); }
  604. static inline uint64_t ntoh(uint64_t n)
  605. throw()
  606. {
  607. #if __BYTE_ORDER == __LITTLE_ENDIAN
  608. #ifdef __GNUC__
  609. return __builtin_bswap64(n);
  610. #else
  611. return (
  612. ((n & 0x00000000000000FFULL) << 56) |
  613. ((n & 0x000000000000FF00ULL) << 40) |
  614. ((n & 0x0000000000FF0000ULL) << 24) |
  615. ((n & 0x00000000FF000000ULL) << 8) |
  616. ((n & 0x000000FF00000000ULL) >> 8) |
  617. ((n & 0x0000FF0000000000ULL) >> 24) |
  618. ((n & 0x00FF000000000000ULL) >> 40) |
  619. ((n & 0xFF00000000000000ULL) >> 56)
  620. );
  621. #endif
  622. #else
  623. return n;
  624. #endif
  625. }
  626. static inline int64_t ntoh(int64_t n) throw() { return (int64_t)ntoh((uint64_t)n); }
  627. /**
  628. * Hexadecimal characters 0-f
  629. */
  630. static const char HEXCHARS[16];
  631. private:
  632. static const uint64_t crc64Table[256];
  633. static const char base64EncMap[64];
  634. static const char base64DecMap[128];
  635. };
  636. } // namespace ZeroTier
  637. #endif