mirror of
https://github.com/ZeroTier/ZeroTierOne
synced 2025-07-05 20:41:44 -07:00
clang-format
This commit is contained in:
parent
d45f280cb7
commit
ba2a4a605c
140 changed files with 19214 additions and 17403 deletions
577
node/Utils.hpp
577
node/Utils.hpp
|
@ -14,17 +14,16 @@
|
|||
#ifndef ZT_UTILS_HPP
|
||||
#define ZT_UTILS_HPP
|
||||
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
#include <stdexcept>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
#include <time.h>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <algorithm>
|
||||
|
||||
#if defined(__FreeBSD__)
|
||||
#include <sys/endian.h>
|
||||
|
@ -34,15 +33,9 @@
|
|||
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
#define ZT_CONST_TO_BE_UINT16(x) ((uint16_t)((uint16_t)((uint16_t)(x) << 8U) | (uint16_t)((uint16_t)(x) >> 8U)))
|
||||
#define ZT_CONST_TO_BE_UINT64(x) ( \
|
||||
(((uint64_t)(x) & 0x00000000000000ffULL) << 56U) | \
|
||||
(((uint64_t)(x) & 0x000000000000ff00ULL) << 40U) | \
|
||||
(((uint64_t)(x) & 0x0000000000ff0000ULL) << 24U) | \
|
||||
(((uint64_t)(x) & 0x00000000ff000000ULL) << 8U) | \
|
||||
(((uint64_t)(x) & 0x000000ff00000000ULL) >> 8U) | \
|
||||
(((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24U) | \
|
||||
(((uint64_t)(x) & 0x00ff000000000000ULL) >> 40U) | \
|
||||
(((uint64_t)(x) & 0xff00000000000000ULL) >> 56U))
|
||||
#define ZT_CONST_TO_BE_UINT64(x) \
|
||||
((((uint64_t)(x) & 0x00000000000000ffULL) << 56U) | (((uint64_t)(x) & 0x000000000000ff00ULL) << 40U) | (((uint64_t)(x) & 0x0000000000ff0000ULL) << 24U) | (((uint64_t)(x) & 0x00000000ff000000ULL) << 8U) \
|
||||
| (((uint64_t)(x) & 0x000000ff00000000ULL) >> 8U) | (((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24U) | (((uint64_t)(x) & 0x00ff000000000000ULL) >> 40U) | (((uint64_t)(x) & 0xff00000000000000ULL) >> 56U))
|
||||
#else
|
||||
#define ZT_CONST_TO_BE_UINT16(x) ((uint16_t)(x))
|
||||
#define ZT_CONST_TO_BE_UINT64(x) ((uint64_t)(x))
|
||||
|
@ -58,14 +51,12 @@ namespace ZeroTier {
|
|||
/**
|
||||
* Miscellaneous utility functions and global constants
|
||||
*/
|
||||
class Utils
|
||||
{
|
||||
public:
|
||||
class Utils {
|
||||
public:
|
||||
static const uint64_t ZERO256[4];
|
||||
|
||||
#ifdef ZT_ARCH_ARM_HAS_NEON
|
||||
struct ARMCapabilities
|
||||
{
|
||||
struct ARMCapabilities {
|
||||
ARMCapabilities() noexcept;
|
||||
|
||||
bool aes;
|
||||
|
@ -78,15 +69,14 @@ public:
|
|||
#endif
|
||||
|
||||
#ifdef ZT_ARCH_X64
|
||||
struct CPUIDRegisters
|
||||
{
|
||||
struct CPUIDRegisters {
|
||||
CPUIDRegisters() noexcept;
|
||||
|
||||
bool rdrand;
|
||||
bool aes;
|
||||
bool avx;
|
||||
bool vaes; // implies AVX
|
||||
bool vpclmulqdq; // implies AVX
|
||||
bool vaes; // implies AVX
|
||||
bool vpclmulqdq; // implies AVX
|
||||
bool avx2;
|
||||
bool avx512f;
|
||||
bool sha;
|
||||
|
@ -97,7 +87,7 @@ public:
|
|||
|
||||
/**
|
||||
* Compute the log2 (most significant bit set) of a 32-bit integer
|
||||
*
|
||||
*
|
||||
* @param v Integer to compute
|
||||
* @return log2 or 0 if v is 0
|
||||
*/
|
||||
|
@ -126,11 +116,11 @@ public:
|
|||
* @param len Length of strings
|
||||
* @return True if strings are equal
|
||||
*/
|
||||
static inline bool secureEq(const void *a,const void *b,unsigned int len)
|
||||
static inline bool secureEq(const void* a, const void* b, unsigned int len)
|
||||
{
|
||||
uint8_t diff = 0;
|
||||
for(unsigned int i=0;i<len;++i) {
|
||||
diff |= ( (reinterpret_cast<const uint8_t *>(a))[i] ^ (reinterpret_cast<const uint8_t *>(b))[i] );
|
||||
for (unsigned int i = 0; i < len; ++i) {
|
||||
diff |= ((reinterpret_cast<const uint8_t*>(a))[i] ^ (reinterpret_cast<const uint8_t*>(b))[i]);
|
||||
}
|
||||
return (diff == 0);
|
||||
}
|
||||
|
@ -138,16 +128,16 @@ public:
|
|||
/**
|
||||
* Securely zero memory, avoiding compiler optimizations and such
|
||||
*/
|
||||
static void burn(void *ptr,unsigned int len);
|
||||
static void burn(void* ptr, unsigned int len);
|
||||
|
||||
/**
|
||||
* @param n Number to convert
|
||||
* @param s Buffer, at least 24 bytes in size
|
||||
* @return String containing 'n' in base 10 form
|
||||
*/
|
||||
static char *decimal(unsigned long n,char s[24]);
|
||||
static char* decimal(unsigned long n, char s[24]);
|
||||
|
||||
static inline char *hex(uint64_t i,char s[17])
|
||||
static inline char* hex(uint64_t i, char s[17])
|
||||
{
|
||||
s[0] = HEXCHARS[(i >> 60) & 0xf];
|
||||
s[1] = HEXCHARS[(i >> 56) & 0xf];
|
||||
|
@ -169,7 +159,7 @@ public:
|
|||
return s;
|
||||
}
|
||||
|
||||
static inline char *hex10(uint64_t i,char s[11])
|
||||
static inline char* hex10(uint64_t i, char s[11])
|
||||
{
|
||||
s[0] = HEXCHARS[(i >> 36) & 0xf];
|
||||
s[1] = HEXCHARS[(i >> 32) & 0xf];
|
||||
|
@ -185,7 +175,7 @@ public:
|
|||
return s;
|
||||
}
|
||||
|
||||
static inline char *hex(uint32_t i,char s[9])
|
||||
static inline char* hex(uint32_t i, char s[9])
|
||||
{
|
||||
s[0] = HEXCHARS[(i >> 28) & 0xf];
|
||||
s[1] = HEXCHARS[(i >> 24) & 0xf];
|
||||
|
@ -199,7 +189,7 @@ public:
|
|||
return s;
|
||||
}
|
||||
|
||||
static inline char *hex(uint16_t i,char s[5])
|
||||
static inline char* hex(uint16_t i, char s[5])
|
||||
{
|
||||
s[0] = HEXCHARS[(i >> 12) & 0xf];
|
||||
s[1] = HEXCHARS[(i >> 8) & 0xf];
|
||||
|
@ -209,7 +199,7 @@ public:
|
|||
return s;
|
||||
}
|
||||
|
||||
static inline char *hex(uint8_t i,char s[3])
|
||||
static inline char* hex(uint8_t i, char s[3])
|
||||
{
|
||||
s[0] = HEXCHARS[(i >> 4) & 0xf];
|
||||
s[1] = HEXCHARS[i & 0xf];
|
||||
|
@ -217,11 +207,11 @@ public:
|
|||
return s;
|
||||
}
|
||||
|
||||
static inline char *hex(const void *d,unsigned int l,char *s)
|
||||
static inline char* hex(const void* d, unsigned int l, char* s)
|
||||
{
|
||||
char *const save = s;
|
||||
for(unsigned int i=0;i<l;++i) {
|
||||
const unsigned int b = reinterpret_cast<const uint8_t *>(d)[i];
|
||||
char* const save = s;
|
||||
for (unsigned int i = 0; i < l; ++i) {
|
||||
const unsigned int b = reinterpret_cast<const uint8_t*>(d)[i];
|
||||
*(s++) = HEXCHARS[b >> 4];
|
||||
*(s++) = HEXCHARS[b & 0xf];
|
||||
}
|
||||
|
@ -229,83 +219,91 @@ public:
|
|||
return save;
|
||||
}
|
||||
|
||||
static inline unsigned int unhex(const char *h,void *buf,unsigned int buflen)
|
||||
static inline unsigned int unhex(const char* h, void* buf, unsigned int buflen)
|
||||
{
|
||||
unsigned int l = 0;
|
||||
while (l < buflen) {
|
||||
uint8_t hc = *(reinterpret_cast<const uint8_t *>(h++));
|
||||
if (!hc) {
|
||||
uint8_t hc = *(reinterpret_cast<const uint8_t*>(h++));
|
||||
if (! hc) {
|
||||
break;
|
||||
}
|
||||
|
||||
uint8_t c = 0;
|
||||
if ((hc >= 48)&&(hc <= 57)) { // 0..9
|
||||
if ((hc >= 48) && (hc <= 57)) { // 0..9
|
||||
c = hc - 48;
|
||||
} else if ((hc >= 97)&&(hc <= 102)) { // a..f
|
||||
}
|
||||
else if ((hc >= 97) && (hc <= 102)) { // a..f
|
||||
c = hc - 87;
|
||||
} else if ((hc >= 65)&&(hc <= 70)) { // A..F
|
||||
}
|
||||
else if ((hc >= 65) && (hc <= 70)) { // A..F
|
||||
c = hc - 55;
|
||||
}
|
||||
|
||||
hc = *(reinterpret_cast<const uint8_t *>(h++));
|
||||
if (!hc) {
|
||||
hc = *(reinterpret_cast<const uint8_t*>(h++));
|
||||
if (! hc) {
|
||||
break;
|
||||
}
|
||||
|
||||
c <<= 4;
|
||||
if ((hc >= 48)&&(hc <= 57)) {
|
||||
if ((hc >= 48) && (hc <= 57)) {
|
||||
c |= hc - 48;
|
||||
} else if ((hc >= 97)&&(hc <= 102)) {
|
||||
}
|
||||
else if ((hc >= 97) && (hc <= 102)) {
|
||||
c |= hc - 87;
|
||||
} else if ((hc >= 65)&&(hc <= 70)) {
|
||||
}
|
||||
else if ((hc >= 65) && (hc <= 70)) {
|
||||
c |= hc - 55;
|
||||
}
|
||||
|
||||
reinterpret_cast<uint8_t *>(buf)[l++] = c;
|
||||
reinterpret_cast<uint8_t*>(buf)[l++] = c;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
static inline unsigned int unhex(const char *h,unsigned int hlen,void *buf,unsigned int buflen)
|
||||
static inline unsigned int unhex(const char* h, unsigned int hlen, void* buf, unsigned int buflen)
|
||||
{
|
||||
unsigned int l = 0;
|
||||
const char *hend = h + hlen;
|
||||
const char* hend = h + hlen;
|
||||
while (l < buflen) {
|
||||
if (h == hend) {
|
||||
break;
|
||||
}
|
||||
uint8_t hc = *(reinterpret_cast<const uint8_t *>(h++));
|
||||
if (!hc) {
|
||||
uint8_t hc = *(reinterpret_cast<const uint8_t*>(h++));
|
||||
if (! hc) {
|
||||
break;
|
||||
}
|
||||
|
||||
uint8_t c = 0;
|
||||
if ((hc >= 48)&&(hc <= 57)) {
|
||||
if ((hc >= 48) && (hc <= 57)) {
|
||||
c = hc - 48;
|
||||
} else if ((hc >= 97)&&(hc <= 102)) {
|
||||
}
|
||||
else if ((hc >= 97) && (hc <= 102)) {
|
||||
c = hc - 87;
|
||||
} else if ((hc >= 65)&&(hc <= 70)) {
|
||||
}
|
||||
else if ((hc >= 65) && (hc <= 70)) {
|
||||
c = hc - 55;
|
||||
}
|
||||
|
||||
if (h == hend) {
|
||||
break;
|
||||
}
|
||||
hc = *(reinterpret_cast<const uint8_t *>(h++));
|
||||
if (!hc) {
|
||||
hc = *(reinterpret_cast<const uint8_t*>(h++));
|
||||
if (! hc) {
|
||||
break;
|
||||
}
|
||||
|
||||
c <<= 4;
|
||||
if ((hc >= 48)&&(hc <= 57)) {
|
||||
if ((hc >= 48) && (hc <= 57)) {
|
||||
c |= hc - 48;
|
||||
} else if ((hc >= 97)&&(hc <= 102)) {
|
||||
}
|
||||
else if ((hc >= 97) && (hc <= 102)) {
|
||||
c |= hc - 87;
|
||||
} else if ((hc >= 65)&&(hc <= 70)) {
|
||||
}
|
||||
else if ((hc >= 65) && (hc <= 70)) {
|
||||
c |= hc - 55;
|
||||
}
|
||||
|
||||
reinterpret_cast<uint8_t *>(buf)[l++] = c;
|
||||
reinterpret_cast<uint8_t*>(buf)[l++] = c;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
@ -327,7 +325,7 @@ public:
|
|||
* @param buf Buffer to fill
|
||||
* @param bytes Number of random bytes to generate
|
||||
*/
|
||||
static void getSecureRandom(void *buf,unsigned int bytes);
|
||||
static void getSecureRandom(void* buf, unsigned int bytes);
|
||||
|
||||
/**
|
||||
* Tokenize a string (alias for strtok_r or strtok_s depending on platform)
|
||||
|
@ -336,54 +334,81 @@ public:
|
|||
* @param delim Delimiters
|
||||
* @param saveptr Pointer to a char * for temporary reentrant storage
|
||||
*/
|
||||
static inline char *stok(char *str,const char *delim,char **saveptr)
|
||||
static inline char* stok(char* str, const char* delim, char** saveptr)
|
||||
{
|
||||
#ifdef __WINDOWS__
|
||||
return strtok_s(str,delim,saveptr);
|
||||
return strtok_s(str, delim, saveptr);
|
||||
#else
|
||||
return strtok_r(str,delim,saveptr);
|
||||
return strtok_r(str, delim, saveptr);
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline unsigned int strToUInt(const char *s) { return (unsigned int)strtoul(s,(char **)0,10); }
|
||||
static inline int strToInt(const char *s) { return (int)strtol(s,(char **)0,10); }
|
||||
static inline unsigned long strToULong(const char *s) { return strtoul(s,(char **)0,10); }
|
||||
static inline long strToLong(const char *s) { return strtol(s,(char **)0,10); }
|
||||
static inline double strToDouble(const char *s) { return strtod(s,NULL); }
|
||||
static inline unsigned long long strToU64(const char *s)
|
||||
static inline unsigned int strToUInt(const char* s)
|
||||
{
|
||||
return (unsigned int)strtoul(s, (char**)0, 10);
|
||||
}
|
||||
static inline int strToInt(const char* s)
|
||||
{
|
||||
return (int)strtol(s, (char**)0, 10);
|
||||
}
|
||||
static inline unsigned long strToULong(const char* s)
|
||||
{
|
||||
return strtoul(s, (char**)0, 10);
|
||||
}
|
||||
static inline long strToLong(const char* s)
|
||||
{
|
||||
return strtol(s, (char**)0, 10);
|
||||
}
|
||||
static inline double strToDouble(const char* s)
|
||||
{
|
||||
return strtod(s, NULL);
|
||||
}
|
||||
static inline unsigned long long strToU64(const char* s)
|
||||
{
|
||||
#ifdef __WINDOWS__
|
||||
return (unsigned long long)_strtoui64(s,(char **)0,10);
|
||||
return (unsigned long long)_strtoui64(s, (char**)0, 10);
|
||||
#else
|
||||
return strtoull(s,(char **)0,10);
|
||||
return strtoull(s, (char**)0, 10);
|
||||
#endif
|
||||
}
|
||||
static inline long long strTo64(const char *s)
|
||||
static inline long long strTo64(const char* s)
|
||||
{
|
||||
#ifdef __WINDOWS__
|
||||
return (long long)_strtoi64(s,(char **)0,10);
|
||||
return (long long)_strtoi64(s, (char**)0, 10);
|
||||
#else
|
||||
return strtoll(s,(char **)0,10);
|
||||
return strtoll(s, (char**)0, 10);
|
||||
#endif
|
||||
}
|
||||
static inline unsigned int hexStrToUInt(const char *s) { return (unsigned int)strtoul(s,(char **)0,16); }
|
||||
static inline int hexStrToInt(const char *s) { return (int)strtol(s,(char **)0,16); }
|
||||
static inline unsigned long hexStrToULong(const char *s) { return strtoul(s,(char **)0,16); }
|
||||
static inline long hexStrToLong(const char *s) { return strtol(s,(char **)0,16); }
|
||||
static inline unsigned long long hexStrToU64(const char *s)
|
||||
static inline unsigned int hexStrToUInt(const char* s)
|
||||
{
|
||||
return (unsigned int)strtoul(s, (char**)0, 16);
|
||||
}
|
||||
static inline int hexStrToInt(const char* s)
|
||||
{
|
||||
return (int)strtol(s, (char**)0, 16);
|
||||
}
|
||||
static inline unsigned long hexStrToULong(const char* s)
|
||||
{
|
||||
return strtoul(s, (char**)0, 16);
|
||||
}
|
||||
static inline long hexStrToLong(const char* s)
|
||||
{
|
||||
return strtol(s, (char**)0, 16);
|
||||
}
|
||||
static inline unsigned long long hexStrToU64(const char* s)
|
||||
{
|
||||
#ifdef __WINDOWS__
|
||||
return (unsigned long long)_strtoui64(s,(char **)0,16);
|
||||
return (unsigned long long)_strtoui64(s, (char**)0, 16);
|
||||
#else
|
||||
return strtoull(s,(char **)0,16);
|
||||
return strtoull(s, (char**)0, 16);
|
||||
#endif
|
||||
}
|
||||
static inline long long hexStrTo64(const char *s)
|
||||
static inline long long hexStrTo64(const char* s)
|
||||
{
|
||||
#ifdef __WINDOWS__
|
||||
return (long long)_strtoi64(s,(char **)0,16);
|
||||
return (long long)_strtoi64(s, (char**)0, 16);
|
||||
#else
|
||||
return strtoll(s,(char **)0,16);
|
||||
return strtoll(s, (char**)0, 16);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -398,16 +423,16 @@ public:
|
|||
* @param src Source string (if NULL, dest will receive a zero-length string and true is returned)
|
||||
* @return True on success, false on overflow (buffer will still be 0-terminated)
|
||||
*/
|
||||
static inline bool scopy(char *dest,unsigned int len,const char *src)
|
||||
static inline bool scopy(char* dest, unsigned int len, const char* src)
|
||||
{
|
||||
if (!len) {
|
||||
return false; // sanity check
|
||||
if (! len) {
|
||||
return false; // sanity check
|
||||
}
|
||||
if (!src) {
|
||||
if (! src) {
|
||||
*dest = (char)0;
|
||||
return true;
|
||||
}
|
||||
char *end = dest + len;
|
||||
char* end = dest + len;
|
||||
while ((*dest++ = *src++)) {
|
||||
if (dest == end) {
|
||||
*(--dest) = (char)0;
|
||||
|
@ -438,10 +463,10 @@ public:
|
|||
*/
|
||||
static inline uint64_t countBits(uint64_t v)
|
||||
{
|
||||
v = v - ((v >> 1) & (uint64_t)~(uint64_t)0/3);
|
||||
v = (v & (uint64_t)~(uint64_t)0/15*3) + ((v >> 2) & (uint64_t)~(uint64_t)0/15*3);
|
||||
v = (v + (v >> 4)) & (uint64_t)~(uint64_t)0/255*15;
|
||||
return (uint64_t)(v * ((uint64_t)~(uint64_t)0/255)) >> 56;
|
||||
v = v - ((v >> 1) & (uint64_t)~(uint64_t)0 / 3);
|
||||
v = (v & (uint64_t)~(uint64_t)0 / 15 * 3) + ((v >> 2) & (uint64_t)~(uint64_t)0 / 15 * 3);
|
||||
v = (v + (v >> 4)) & (uint64_t)~(uint64_t)0 / 255 * 15;
|
||||
return (uint64_t)(v * ((uint64_t)~(uint64_t)0 / 255)) >> 56;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -451,10 +476,10 @@ public:
|
|||
* @param len Length of memory
|
||||
* @return True if memory is all zero
|
||||
*/
|
||||
static inline bool isZero(const void *p,unsigned int len)
|
||||
static inline bool isZero(const void* p, unsigned int len)
|
||||
{
|
||||
for(unsigned int i=0;i<len;++i) {
|
||||
if (((const unsigned char *)p)[i]) {
|
||||
for (unsigned int i = 0; i < len; ++i) {
|
||||
if (((const unsigned char*)p)[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -469,24 +494,17 @@ public:
|
|||
*/
|
||||
static ZT_INLINE uint64_t swapBytes(const uint64_t n) noexcept
|
||||
{
|
||||
#ifdef __GNUC__
|
||||
#ifdef __GNUC__
|
||||
return __builtin_bswap64(n);
|
||||
#else
|
||||
#ifdef _MSC_VER
|
||||
#else
|
||||
#ifdef _MSC_VER
|
||||
return (uint64_t)_byteswap_uint64((unsigned __int64)n);
|
||||
#else
|
||||
#else
|
||||
return (
|
||||
((n & 0x00000000000000ffULL) << 56) |
|
||||
((n & 0x000000000000ff00ULL) << 40) |
|
||||
((n & 0x0000000000ff0000ULL) << 24) |
|
||||
((n & 0x00000000ff000000ULL) << 8) |
|
||||
((n & 0x000000ff00000000ULL) >> 8) |
|
||||
((n & 0x0000ff0000000000ULL) >> 24) |
|
||||
((n & 0x00ff000000000000ULL) >> 40) |
|
||||
((n & 0xff00000000000000ULL) >> 56)
|
||||
);
|
||||
#endif
|
||||
#endif
|
||||
((n & 0x00000000000000ffULL) << 56) | ((n & 0x000000000000ff00ULL) << 40) | ((n & 0x0000000000ff0000ULL) << 24) | ((n & 0x00000000ff000000ULL) << 8) | ((n & 0x000000ff00000000ULL) >> 8) | ((n & 0x0000ff0000000000ULL) >> 24)
|
||||
| ((n & 0x00ff000000000000ULL) >> 40) | ((n & 0xff00000000000000ULL) >> 56));
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -497,15 +515,15 @@ public:
|
|||
*/
|
||||
static ZT_INLINE uint32_t swapBytes(const uint32_t n) noexcept
|
||||
{
|
||||
#if defined(__GNUC__)
|
||||
#if defined(__GNUC__)
|
||||
return __builtin_bswap32(n);
|
||||
#else
|
||||
#ifdef _MSC_VER
|
||||
#else
|
||||
#ifdef _MSC_VER
|
||||
return (uint32_t)_byteswap_ulong((unsigned long)n);
|
||||
#else
|
||||
#else
|
||||
return htonl(n);
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -516,122 +534,119 @@ public:
|
|||
*/
|
||||
static ZT_INLINE uint16_t swapBytes(const uint16_t n) noexcept
|
||||
{
|
||||
#if defined(__GNUC__)
|
||||
#if defined(__GNUC__)
|
||||
return __builtin_bswap16(n);
|
||||
#else
|
||||
#ifdef _MSC_VER
|
||||
#else
|
||||
#ifdef _MSC_VER
|
||||
return (uint16_t)_byteswap_ushort((unsigned short)n);
|
||||
#else
|
||||
#else
|
||||
return htons(n);
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
// These are helper adapters to load and swap integer types special cased by size
|
||||
// to work with all typedef'd variants, signed/unsigned, etc.
|
||||
template< typename I, unsigned int S >
|
||||
class _swap_bytes_bysize;
|
||||
template <typename I, unsigned int S> class _swap_bytes_bysize;
|
||||
|
||||
template< typename I >
|
||||
class _swap_bytes_bysize< I, 1 >
|
||||
{
|
||||
public:
|
||||
template <typename I> class _swap_bytes_bysize<I, 1> {
|
||||
public:
|
||||
static ZT_INLINE I s(const I n) noexcept
|
||||
{ return n; }
|
||||
{
|
||||
return n;
|
||||
}
|
||||
};
|
||||
|
||||
template< typename I >
|
||||
class _swap_bytes_bysize< I, 2 >
|
||||
{
|
||||
public:
|
||||
template <typename I> class _swap_bytes_bysize<I, 2> {
|
||||
public:
|
||||
static ZT_INLINE I s(const I n) noexcept
|
||||
{ return (I)swapBytes((uint16_t)n); }
|
||||
{
|
||||
return (I)swapBytes((uint16_t)n);
|
||||
}
|
||||
};
|
||||
|
||||
template< typename I >
|
||||
class _swap_bytes_bysize< I, 4 >
|
||||
{
|
||||
public:
|
||||
template <typename I> class _swap_bytes_bysize<I, 4> {
|
||||
public:
|
||||
static ZT_INLINE I s(const I n) noexcept
|
||||
{ return (I)swapBytes((uint32_t)n); }
|
||||
{
|
||||
return (I)swapBytes((uint32_t)n);
|
||||
}
|
||||
};
|
||||
|
||||
template< typename I >
|
||||
class _swap_bytes_bysize< I, 8 >
|
||||
{
|
||||
public:
|
||||
template <typename I> class _swap_bytes_bysize<I, 8> {
|
||||
public:
|
||||
static ZT_INLINE I s(const I n) noexcept
|
||||
{ return (I)swapBytes((uint64_t)n); }
|
||||
{
|
||||
return (I)swapBytes((uint64_t)n);
|
||||
}
|
||||
};
|
||||
|
||||
template< typename I, unsigned int S >
|
||||
class _load_be_bysize;
|
||||
template <typename I, unsigned int S> class _load_be_bysize;
|
||||
|
||||
template< typename I >
|
||||
class _load_be_bysize< I, 1 >
|
||||
{
|
||||
public:
|
||||
static ZT_INLINE I l(const uint8_t *const p) noexcept
|
||||
{ return p[0]; }
|
||||
template <typename I> class _load_be_bysize<I, 1> {
|
||||
public:
|
||||
static ZT_INLINE I l(const uint8_t* const p) noexcept
|
||||
{
|
||||
return p[0];
|
||||
}
|
||||
};
|
||||
|
||||
template< typename I >
|
||||
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]); }
|
||||
template <typename I> 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]);
|
||||
}
|
||||
};
|
||||
|
||||
template< typename I >
|
||||
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]); }
|
||||
template <typename I> 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]);
|
||||
}
|
||||
};
|
||||
|
||||
template< typename I >
|
||||
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]); }
|
||||
template <typename I> 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]);
|
||||
}
|
||||
};
|
||||
|
||||
template< typename I, unsigned int S >
|
||||
class _load_le_bysize;
|
||||
template <typename I, unsigned int S> class _load_le_bysize;
|
||||
|
||||
template< typename I >
|
||||
class _load_le_bysize< I, 1 >
|
||||
{
|
||||
public:
|
||||
static ZT_INLINE I l(const uint8_t *const p) noexcept
|
||||
{ return p[0]; }
|
||||
template <typename I> class _load_le_bysize<I, 1> {
|
||||
public:
|
||||
static ZT_INLINE I l(const uint8_t* const p) noexcept
|
||||
{
|
||||
return p[0];
|
||||
}
|
||||
};
|
||||
|
||||
template< typename I >
|
||||
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)); }
|
||||
template <typename I> 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));
|
||||
}
|
||||
};
|
||||
|
||||
template< typename I >
|
||||
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)); }
|
||||
template <typename I> 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));
|
||||
}
|
||||
};
|
||||
|
||||
template< typename I >
|
||||
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); }
|
||||
template <typename I> 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);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -641,14 +656,13 @@ public:
|
|||
* @param n Value to convert
|
||||
* @return Value in big-endian order
|
||||
*/
|
||||
template< typename I >
|
||||
static ZT_INLINE I hton(const I n) noexcept
|
||||
template <typename I> static ZT_INLINE I hton(const I n) noexcept
|
||||
{
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
return _swap_bytes_bysize< I, sizeof(I) >::s(n);
|
||||
#else
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
return _swap_bytes_bysize<I, sizeof(I)>::s(n);
|
||||
#else
|
||||
return n;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -658,14 +672,13 @@ public:
|
|||
* @param n Value to convert
|
||||
* @return Value in host byte order
|
||||
*/
|
||||
template< typename I >
|
||||
static ZT_INLINE I ntoh(const I n) noexcept
|
||||
template <typename I> static ZT_INLINE I ntoh(const I n) noexcept
|
||||
{
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
return _swap_bytes_bysize< I, sizeof(I) >::s(n);
|
||||
#else
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
return _swap_bytes_bysize<I, sizeof(I)>::s(n);
|
||||
#else
|
||||
return n;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -675,18 +688,17 @@ public:
|
|||
* @param p Byte stream, must be at least sizeof(I) in size
|
||||
* @return Loaded raw integer
|
||||
*/
|
||||
template< typename I >
|
||||
static ZT_INLINE I loadMachineEndian(const void *const p) noexcept
|
||||
template <typename I> static ZT_INLINE I loadMachineEndian(const void* const p) noexcept
|
||||
{
|
||||
#ifdef ZT_NO_UNALIGNED_ACCESS
|
||||
#ifdef ZT_NO_UNALIGNED_ACCESS
|
||||
I tmp;
|
||||
for(int i=0;i<(int)sizeof(I);++i) {
|
||||
reinterpret_cast<uint8_t *>(&tmp)[i] = reinterpret_cast<const uint8_t *>(p)[i];
|
||||
for (int i = 0; i < (int)sizeof(I); ++i) {
|
||||
reinterpret_cast<uint8_t*>(&tmp)[i] = reinterpret_cast<const uint8_t*>(p)[i];
|
||||
}
|
||||
return tmp;
|
||||
#else
|
||||
return *reinterpret_cast<const I *>(p);
|
||||
#endif
|
||||
#else
|
||||
return *reinterpret_cast<const I*>(p);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -696,16 +708,15 @@ public:
|
|||
* @param p Byte array (must be at least sizeof(I))
|
||||
* @param i Integer to store
|
||||
*/
|
||||
template< typename I >
|
||||
static ZT_INLINE void storeMachineEndian(void *const p, const I i) noexcept
|
||||
template <typename I> static ZT_INLINE void storeMachineEndian(void* const p, const I i) noexcept
|
||||
{
|
||||
#ifdef ZT_NO_UNALIGNED_ACCESS
|
||||
for(unsigned int k=0;k<sizeof(I);++k) {
|
||||
reinterpret_cast<uint8_t *>(p)[k] = reinterpret_cast<const uint8_t *>(&i)[k];
|
||||
#ifdef ZT_NO_UNALIGNED_ACCESS
|
||||
for (unsigned int k = 0; k < sizeof(I); ++k) {
|
||||
reinterpret_cast<uint8_t*>(p)[k] = reinterpret_cast<const uint8_t*>(&i)[k];
|
||||
}
|
||||
#else
|
||||
*reinterpret_cast<I *>(p) = i;
|
||||
#endif
|
||||
#else
|
||||
*reinterpret_cast<I*>(p) = i;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -715,14 +726,13 @@ public:
|
|||
* @param p Byte stream, must be at least sizeof(I) in size
|
||||
* @return Decoded integer
|
||||
*/
|
||||
template< typename I >
|
||||
static ZT_INLINE I loadBigEndian(const void *const p) noexcept
|
||||
template <typename I> static ZT_INLINE I loadBigEndian(const void* const p) noexcept
|
||||
{
|
||||
#ifdef ZT_NO_UNALIGNED_ACCESS
|
||||
return _load_be_bysize<I,sizeof(I)>::l(reinterpret_cast<const uint8_t *>(p));
|
||||
#else
|
||||
return ntoh(*reinterpret_cast<const I *>(p));
|
||||
#endif
|
||||
#ifdef ZT_NO_UNALIGNED_ACCESS
|
||||
return _load_be_bysize<I, sizeof(I)>::l(reinterpret_cast<const uint8_t*>(p));
|
||||
#else
|
||||
return ntoh(*reinterpret_cast<const I*>(p));
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -732,14 +742,13 @@ public:
|
|||
* @param p Byte stream to write (must be at least sizeof(I))
|
||||
* #param i Integer to write
|
||||
*/
|
||||
template< typename I >
|
||||
static ZT_INLINE void storeBigEndian(void *const p, I i) noexcept
|
||||
template <typename I> static ZT_INLINE void storeBigEndian(void* const p, I i) noexcept
|
||||
{
|
||||
#ifdef ZT_NO_UNALIGNED_ACCESS
|
||||
storeMachineEndian(p,hton(i));
|
||||
#else
|
||||
*reinterpret_cast<I *>(p) = hton(i);
|
||||
#endif
|
||||
#ifdef ZT_NO_UNALIGNED_ACCESS
|
||||
storeMachineEndian(p, hton(i));
|
||||
#else
|
||||
*reinterpret_cast<I*>(p) = hton(i);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -749,14 +758,13 @@ public:
|
|||
* @param p Byte stream, must be at least sizeof(I) in size
|
||||
* @return Decoded integer
|
||||
*/
|
||||
template< typename I >
|
||||
static ZT_INLINE I loadLittleEndian(const void *const p) noexcept
|
||||
template <typename I> static ZT_INLINE I loadLittleEndian(const void* const p) noexcept
|
||||
{
|
||||
#if __BYTE_ORDER == __BIG_ENDIAN || defined(ZT_NO_UNALIGNED_ACCESS)
|
||||
return _load_le_bysize<I,sizeof(I)>::l(reinterpret_cast<const uint8_t *>(p));
|
||||
#else
|
||||
return *reinterpret_cast<const I *>(p);
|
||||
#endif
|
||||
#if __BYTE_ORDER == __BIG_ENDIAN || defined(ZT_NO_UNALIGNED_ACCESS)
|
||||
return _load_le_bysize<I, sizeof(I)>::l(reinterpret_cast<const uint8_t*>(p));
|
||||
#else
|
||||
return *reinterpret_cast<const I*>(p);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -766,18 +774,17 @@ public:
|
|||
* @param p Byte stream to write (must be at least sizeof(I))
|
||||
* #param i Integer to write
|
||||
*/
|
||||
template< typename I >
|
||||
static ZT_INLINE void storeLittleEndian(void *const p, const I i) noexcept
|
||||
template <typename I> static ZT_INLINE void storeLittleEndian(void* const p, const I i) noexcept
|
||||
{
|
||||
#if __BYTE_ORDER == __BIG_ENDIAN
|
||||
storeMachineEndian(p,_swap_bytes_bysize<I,sizeof(I)>::s(i));
|
||||
#else
|
||||
#ifdef ZT_NO_UNALIGNED_ACCESS
|
||||
storeMachineEndian(p,i);
|
||||
#else
|
||||
*reinterpret_cast<I *>(p) = i;
|
||||
#endif
|
||||
#endif
|
||||
#if __BYTE_ORDER == __BIG_ENDIAN
|
||||
storeMachineEndian(p, _swap_bytes_bysize<I, sizeof(I)>::s(i));
|
||||
#else
|
||||
#ifdef ZT_NO_UNALIGNED_ACCESS
|
||||
storeMachineEndian(p, i);
|
||||
#else
|
||||
*reinterpret_cast<I*>(p) = i;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -787,15 +794,14 @@ public:
|
|||
* @param dest Destination memory
|
||||
* @param src Source memory
|
||||
*/
|
||||
template< unsigned long L >
|
||||
static ZT_INLINE void copy(void *dest, const void *src) noexcept
|
||||
template <unsigned long L> static ZT_INLINE void copy(void* dest, const void* src) noexcept
|
||||
{
|
||||
#if defined(ZT_ARCH_X64) && defined(__GNUC__)
|
||||
#if defined(ZT_ARCH_X64) && defined(__GNUC__)
|
||||
uintptr_t l = L;
|
||||
__asm__ __volatile__ ("cld ; rep movsb" : "+c"(l), "+S"(src), "+D"(dest) :: "memory");
|
||||
#else
|
||||
__asm__ __volatile__("cld ; rep movsb" : "+c"(l), "+S"(src), "+D"(dest)::"memory");
|
||||
#else
|
||||
memcpy(dest, src, L);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -805,13 +811,13 @@ public:
|
|||
* @param src Source memory
|
||||
* @param len Bytes to copy
|
||||
*/
|
||||
static ZT_INLINE void copy(void *dest, const void *src, unsigned long len) noexcept
|
||||
static ZT_INLINE void copy(void* dest, const void* src, unsigned long len) noexcept
|
||||
{
|
||||
#if defined(ZT_ARCH_X64) && defined(__GNUC__)
|
||||
__asm__ __volatile__ ("cld ; rep movsb" : "+c"(len), "+S"(src), "+D"(dest) :: "memory");
|
||||
#else
|
||||
#if defined(ZT_ARCH_X64) && defined(__GNUC__)
|
||||
__asm__ __volatile__("cld ; rep movsb" : "+c"(len), "+S"(src), "+D"(dest)::"memory");
|
||||
#else
|
||||
memcpy(dest, src, len);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -820,15 +826,14 @@ public:
|
|||
* @tparam L Size in bytes
|
||||
* @param dest Memory to zero
|
||||
*/
|
||||
template< unsigned long L >
|
||||
static ZT_INLINE void zero(void *dest) noexcept
|
||||
template <unsigned long L> static ZT_INLINE void zero(void* dest) noexcept
|
||||
{
|
||||
#if defined(ZT_ARCH_X64) && defined(__GNUC__)
|
||||
#if defined(ZT_ARCH_X64) && defined(__GNUC__)
|
||||
uintptr_t l = L;
|
||||
__asm__ __volatile__ ("cld ; rep stosb" :"+c" (l), "+D" (dest) : "a" (0) : "memory");
|
||||
#else
|
||||
__asm__ __volatile__("cld ; rep stosb" : "+c"(l), "+D"(dest) : "a"(0) : "memory");
|
||||
#else
|
||||
memset(dest, 0, L);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -837,13 +842,13 @@ public:
|
|||
* @param dest Memory to zero
|
||||
* @param len Size in bytes
|
||||
*/
|
||||
static ZT_INLINE void zero(void *dest, unsigned long len) noexcept
|
||||
static ZT_INLINE void zero(void* dest, unsigned long len) noexcept
|
||||
{
|
||||
#if defined(ZT_ARCH_X64) && defined(__GNUC__)
|
||||
__asm__ __volatile__ ("cld ; rep stosb" :"+c" (len), "+D" (dest) : "a" (0) : "memory");
|
||||
#else
|
||||
#if defined(ZT_ARCH_X64) && defined(__GNUC__)
|
||||
__asm__ __volatile__("cld ; rep stosb" : "+c"(len), "+D"(dest) : "a"(0) : "memory");
|
||||
#else
|
||||
memset(dest, 0, len);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -855,7 +860,7 @@ public:
|
|||
* Remove `-` and `:` from a MAC address (in-place).
|
||||
*
|
||||
* @param mac The MAC address
|
||||
*/
|
||||
*/
|
||||
static inline void cleanMac(std::string& mac)
|
||||
{
|
||||
auto start = mac.begin();
|
||||
|
@ -865,6 +870,6 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
} // namespace ZeroTier
|
||||
} // namespace ZeroTier
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue