2013-07-04 20:56:19 +00:00
|
|
|
/*
|
|
|
|
* ZeroTier One - Global Peer to Peer Ethernet
|
2014-02-16 20:40:22 +00:00
|
|
|
* Copyright (C) 2011-2014 ZeroTier Networks LLC
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* --
|
|
|
|
*
|
|
|
|
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
|
|
|
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
*
|
|
|
|
* If you would like to embed ZeroTier into a commercial application or
|
|
|
|
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
|
|
|
* LLC. Start here: http://www.zerotier.com/
|
|
|
|
*/
|
|
|
|
|
2013-12-07 00:49:20 +00:00
|
|
|
#ifndef ZT_UTILS_HPP
|
|
|
|
#define ZT_UTILS_HPP
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
2013-08-28 20:01:27 +00:00
|
|
|
#include <string.h>
|
2013-07-04 20:56:19 +00:00
|
|
|
#include <time.h>
|
2013-07-17 18:10:44 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#include <string>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <vector>
|
2013-07-25 21:53:57 +00:00
|
|
|
#include <map>
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-08-13 01:25:36 +00:00
|
|
|
#include "Constants.hpp"
|
|
|
|
|
2013-08-08 14:06:39 +00:00
|
|
|
#ifdef __WINDOWS__
|
2013-08-12 20:57:34 +00:00
|
|
|
#include <WinSock2.h>
|
2013-08-13 01:25:36 +00:00
|
|
|
#include <Windows.h>
|
2013-08-08 14:06:39 +00:00
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
2013-08-12 20:57:34 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <arpa/inet.h>
|
2013-08-08 14:06:39 +00:00
|
|
|
#endif
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Miscellaneous utility functions and global constants
|
|
|
|
*/
|
|
|
|
class Utils
|
|
|
|
{
|
|
|
|
public:
|
2014-08-07 13:35:54 +00:00
|
|
|
#ifdef __UNIX_LIKE__
|
|
|
|
/**
|
|
|
|
* Close STDOUT_FILENO and STDERR_FILENO and replace them with output to given path
|
|
|
|
*
|
|
|
|
* This can be called after fork() and prior to exec() to suppress output
|
|
|
|
* from a subprocess, such as auto-update.
|
|
|
|
*
|
|
|
|
* @param stdoutPath Path to file to use for stdout
|
|
|
|
* @param stderrPath Path to file to use for stderr, or NULL for same as stdout (default)
|
|
|
|
* @return True on success
|
|
|
|
*/
|
|
|
|
static bool redirectUnixOutputs(const char *stdoutPath,const char *stderrPath = (const char *)0)
|
|
|
|
throw();
|
2014-08-07 13:36:46 +00:00
|
|
|
#endif // __UNIX_LIKE__
|
2014-08-07 13:35:54 +00:00
|
|
|
|
2013-09-13 20:53:47 +00:00
|
|
|
/**
|
|
|
|
* Perform a time-invariant binary comparison
|
|
|
|
*
|
|
|
|
* @param a First binary string
|
|
|
|
* @param b Second binary string
|
|
|
|
* @param len Length of strings
|
|
|
|
* @return True if strings are equal
|
|
|
|
*/
|
|
|
|
static inline bool secureEq(const void *a,const void *b,unsigned int len)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
const char *p1 = (const char *)a;
|
|
|
|
const char *p2 = (const char *)b;
|
|
|
|
uint64_t diff = 0;
|
|
|
|
|
|
|
|
while (len >= 8) {
|
|
|
|
diff |= (*((const uint64_t *)p1) ^ *((const uint64_t *)p2));
|
|
|
|
p1 += 8;
|
|
|
|
p2 += 8;
|
|
|
|
len -= 8;
|
|
|
|
}
|
2013-09-16 17:02:10 +00:00
|
|
|
while (len--)
|
|
|
|
diff |= (uint64_t)(*p1++ ^ *p2++);
|
2013-09-13 20:53:47 +00:00
|
|
|
|
|
|
|
return (diff == 0ULL);
|
|
|
|
}
|
|
|
|
|
2014-04-18 07:14:12 +00:00
|
|
|
/**
|
|
|
|
* Securely zero memory
|
|
|
|
*
|
|
|
|
* This just uses volatile to ensure that it's never optimized out.
|
|
|
|
*/
|
|
|
|
static inline void burn(void *ptr,unsigned int len)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
volatile unsigned char *p = (unsigned char *)ptr;
|
|
|
|
volatile unsigned char *e = p + len;
|
|
|
|
while (p != e)
|
|
|
|
*(p++) = (unsigned char)0;
|
|
|
|
}
|
|
|
|
|
2013-08-08 14:06:39 +00:00
|
|
|
/**
|
|
|
|
* Delete a file
|
|
|
|
*
|
|
|
|
* @param path Path to delete
|
|
|
|
* @return True if delete was successful
|
|
|
|
*/
|
|
|
|
static inline bool rm(const char *path)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
#ifdef __WINDOWS__
|
2013-08-14 15:19:21 +00:00
|
|
|
return (DeleteFileA(path) != FALSE);
|
2013-08-08 14:06:39 +00:00
|
|
|
#else
|
|
|
|
return (unlink(path) == 0);
|
|
|
|
#endif
|
|
|
|
}
|
2013-11-06 15:38:19 +00:00
|
|
|
static inline bool rm(const std::string &path) throw() { return rm(path.c_str()); }
|
2013-08-08 14:06:39 +00:00
|
|
|
|
2013-07-25 21:53:57 +00:00
|
|
|
/**
|
|
|
|
* List a directory's contents
|
2013-11-06 15:38:19 +00:00
|
|
|
*
|
|
|
|
* Keys in returned map are filenames only and don't include the leading
|
2013-12-12 19:33:41 +00:00
|
|
|
* path. Pseudo-paths like . and .. are not returned. Values are true if
|
|
|
|
* the item is a directory, false if it's a file. More detailed attributes
|
|
|
|
* aren't supported since the code that uses this doesn't need them.
|
2013-07-25 21:53:57 +00:00
|
|
|
*
|
|
|
|
* @param path Path to list
|
|
|
|
* @return Map of entries and whether or not they are also directories (empty on failure)
|
|
|
|
*/
|
|
|
|
static std::map<std::string,bool> listDirectory(const char *path);
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
2013-12-12 19:33:41 +00:00
|
|
|
* Convert binary data to hexadecimal
|
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param data Data to convert to hex
|
|
|
|
* @param len Length of data
|
|
|
|
* @return Hexadecimal string
|
|
|
|
*/
|
|
|
|
static std::string hex(const void *data,unsigned int len);
|
2013-08-26 21:22:20 +00:00
|
|
|
static inline std::string hex(const std::string &data) { return hex(data.data(),(unsigned int)data.length()); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
2013-12-12 19:33:41 +00:00
|
|
|
* Convert hexadecimal to binary data
|
|
|
|
*
|
|
|
|
* This ignores all non-hex characters, just stepping over them and
|
|
|
|
* continuing. Upper and lower case are supported for letters a-f.
|
|
|
|
*
|
2014-08-05 16:56:49 +00:00
|
|
|
* @param hex Hexadecimal ASCII code (non-hex chars are ignored, stops at zero or maxlen)
|
|
|
|
* @param maxlen Maximum length of hex string buffer
|
2013-07-04 20:56:19 +00:00
|
|
|
* @return Binary data
|
|
|
|
*/
|
2014-08-05 16:56:49 +00:00
|
|
|
static std::string unhex(const char *hex,unsigned int maxlen);
|
|
|
|
static inline std::string unhex(const std::string &hex) { return unhex(hex.c_str(),(unsigned int)hex.length()); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
2013-12-12 19:33:41 +00:00
|
|
|
* Convert hexadecimal to binary data
|
|
|
|
*
|
|
|
|
* This ignores all non-hex characters, just stepping over them and
|
|
|
|
* continuing. Upper and lower case are supported for letters a-f.
|
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param hex Hexadecimal ASCII
|
2014-08-05 16:56:49 +00:00
|
|
|
* @param maxlen Maximum length of hex string buffer
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param buf Buffer to fill
|
|
|
|
* @param len Length of buffer
|
|
|
|
* @return Number of characters actually written
|
|
|
|
*/
|
2014-08-05 16:56:49 +00:00
|
|
|
static unsigned int unhex(const char *hex,unsigned int maxlen,void *buf,unsigned int len);
|
2014-08-08 02:08:41 +00:00
|
|
|
static inline unsigned int unhex(const std::string &hex,void *buf,unsigned int len) { return unhex(hex.c_str(),(unsigned int)hex.length(),buf,len); }
|
2013-10-04 16:24:21 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
2013-12-12 19:33:41 +00:00
|
|
|
* Generate secure random bytes
|
|
|
|
*
|
|
|
|
* This will try to use whatever OS sources of entropy are available. It's
|
|
|
|
* guarded by an internal mutex so it's thread-safe.
|
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param buf Buffer to fill
|
|
|
|
* @param bytes Number of random bytes to generate
|
|
|
|
*/
|
|
|
|
static void getSecureRandom(void *buf,unsigned int bytes);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set modes on a file to something secure
|
|
|
|
*
|
|
|
|
* This locks a file so that only the owner can access it. What it actually
|
|
|
|
* does varies by platform.
|
|
|
|
*
|
|
|
|
* @param path Path to lock
|
|
|
|
* @param isDir True if this is a directory
|
|
|
|
*/
|
|
|
|
static void lockDownFile(const char *path,bool isDir);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get file last modification time
|
|
|
|
*
|
|
|
|
* Resolution is often only second, not millisecond, but the return is
|
|
|
|
* always in ms for comparison against now().
|
|
|
|
*
|
|
|
|
* @param path Path to file to get time
|
|
|
|
* @return Last modification time in ms since epoch or 0 if not found
|
|
|
|
*/
|
|
|
|
static uint64_t getLastModified(const char *path);
|
|
|
|
|
2013-08-01 21:32:37 +00:00
|
|
|
/**
|
|
|
|
* @param path Path to check
|
2013-11-21 21:34:27 +00:00
|
|
|
* @param followLinks Follow links (on platforms with that concept)
|
2013-08-01 21:32:37 +00:00
|
|
|
* @return True if file or directory exists at path location
|
|
|
|
*/
|
2013-11-21 21:34:27 +00:00
|
|
|
static bool fileExists(const char *path,bool followLinks = true);
|
2013-08-01 21:32:37 +00:00
|
|
|
|
2013-11-01 16:38:38 +00:00
|
|
|
/**
|
|
|
|
* @param path Path to file
|
|
|
|
* @return File size or -1 if nonexistent or other failure
|
|
|
|
*/
|
|
|
|
static int64_t getFileSize(const char *path);
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* @return Current time in milliseconds since epoch
|
|
|
|
*/
|
|
|
|
static inline uint64_t now()
|
|
|
|
throw()
|
|
|
|
{
|
2013-08-12 20:57:34 +00:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
FILETIME ft;
|
|
|
|
SYSTEMTIME st;
|
|
|
|
ULARGE_INTEGER tmp;
|
|
|
|
GetSystemTime(&st);
|
|
|
|
SystemTimeToFileTime(&st,&ft);
|
|
|
|
tmp.LowPart = ft.dwLowDateTime;
|
|
|
|
tmp.HighPart = ft.dwHighDateTime;
|
|
|
|
return ( ((tmp.QuadPart - 116444736000000000ULL) / 10000L) + st.wMilliseconds );
|
|
|
|
#else
|
2013-07-04 20:56:19 +00:00
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv,(struct timezone *)0);
|
|
|
|
return ( (1000ULL * (uint64_t)tv.tv_sec) + (uint64_t)(tv.tv_usec / 1000) );
|
2013-08-12 20:57:34 +00:00
|
|
|
#endif
|
2013-07-04 20:56:19 +00:00
|
|
|
};
|
|
|
|
|
2013-08-08 21:20:35 +00:00
|
|
|
/**
|
|
|
|
* @return Current time in seconds since epoch, to the highest available resolution
|
|
|
|
*/
|
|
|
|
static inline double nowf()
|
|
|
|
throw()
|
|
|
|
{
|
2013-08-12 20:57:34 +00:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
FILETIME ft;
|
|
|
|
SYSTEMTIME st;
|
|
|
|
ULARGE_INTEGER tmp;
|
|
|
|
GetSystemTime(&st);
|
|
|
|
SystemTimeToFileTime(&st,&ft);
|
|
|
|
tmp.LowPart = ft.dwLowDateTime;
|
|
|
|
tmp.HighPart = ft.dwHighDateTime;
|
|
|
|
return (((double)(tmp.QuadPart - 116444736000000000ULL)) / 10000000.0);
|
|
|
|
#else
|
2013-08-08 21:20:35 +00:00
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv,(struct timezone *)0);
|
|
|
|
return ( ((double)tv.tv_sec) + (((double)tv.tv_usec) / 1000000.0) );
|
2013-08-12 20:57:34 +00:00
|
|
|
#endif
|
2013-08-08 21:20:35 +00:00
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* Read the full contents of a file into a string buffer
|
|
|
|
*
|
|
|
|
* The buffer isn't cleared, so if it already contains data the file's data will
|
|
|
|
* be appended.
|
|
|
|
*
|
|
|
|
* @param path Path of file to read
|
|
|
|
* @param buf Buffer to fill
|
|
|
|
* @return True if open and read successful
|
|
|
|
*/
|
|
|
|
static bool readFile(const char *path,std::string &buf);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write a block of data to disk, replacing any current file contents
|
|
|
|
*
|
|
|
|
* @param path Path to write
|
|
|
|
* @param buf Buffer containing data
|
|
|
|
* @param len Length of buffer
|
|
|
|
* @return True if entire file was successfully written
|
|
|
|
*/
|
|
|
|
static bool writeFile(const char *path,const void *buf,unsigned int len);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write a block of data to disk, replacing any current file contents
|
|
|
|
*
|
|
|
|
* @param path Path to write
|
|
|
|
* @param s Data to write
|
|
|
|
* @return True if entire file was successfully written
|
|
|
|
*/
|
|
|
|
static inline bool writeFile(const char *path,const std::string &s)
|
|
|
|
{
|
2013-08-26 21:22:20 +00:00
|
|
|
return writeFile(path,s.data(),(unsigned int)s.length());
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Split a string by delimiter, with optional escape and quote characters
|
|
|
|
*
|
|
|
|
* @param s String to split
|
|
|
|
* @param sep One or more separators
|
|
|
|
* @param esc Zero or more escape characters
|
|
|
|
* @param quot Zero or more quote characters
|
|
|
|
* @return Vector of tokens
|
|
|
|
*/
|
|
|
|
static std::vector<std::string> split(const char *s,const char *const sep,const char *esc,const char *quot);
|
|
|
|
|
2013-08-28 19:09:49 +00:00
|
|
|
/**
|
2013-09-16 17:02:10 +00:00
|
|
|
* Tokenize a string (alias for strtok_r or strtok_s depending on platform)
|
2013-08-28 19:09:49 +00:00
|
|
|
*
|
|
|
|
* @param str String to split
|
|
|
|
* @param delim Delimiters
|
|
|
|
* @param saveptr Pointer to a char * for temporary reentrant storage
|
|
|
|
*/
|
|
|
|
static inline char *stok(char *str,const char *delim,char **saveptr)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
return strtok_s(str,delim,saveptr);
|
|
|
|
#else
|
|
|
|
return strtok_r(str,delim,saveptr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-09-07 16:23:53 +00:00
|
|
|
// String to number converters -- defined here to permit portability
|
|
|
|
// ifdefs for platforms that lack some of the strtoXX functions.
|
2013-09-04 13:27:56 +00:00
|
|
|
static inline unsigned int strToUInt(const char *s)
|
2013-08-28 20:01:27 +00:00
|
|
|
throw()
|
|
|
|
{
|
|
|
|
return (unsigned int)strtoul(s,(char **)0,10);
|
|
|
|
}
|
2013-09-07 16:23:53 +00:00
|
|
|
static inline int strToInt(const char *s)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
return (int)strtol(s,(char **)0,10);
|
|
|
|
}
|
2013-09-04 13:27:56 +00:00
|
|
|
static inline unsigned long strToULong(const char *s)
|
2013-08-28 20:01:27 +00:00
|
|
|
throw()
|
|
|
|
{
|
|
|
|
return strtoul(s,(char **)0,10);
|
|
|
|
}
|
2013-09-07 16:23:53 +00:00
|
|
|
static inline long strToLong(const char *s)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
return strtol(s,(char **)0,10);
|
|
|
|
}
|
2013-09-04 13:27:56 +00:00
|
|
|
static inline unsigned long long strToU64(const char *s)
|
2013-08-28 20:01:27 +00:00
|
|
|
throw()
|
|
|
|
{
|
2013-09-04 13:27:56 +00:00
|
|
|
#ifdef __WINDOWS__
|
2013-09-07 16:23:53 +00:00
|
|
|
return (unsigned long long)_strtoui64(s,(char **)0,10);
|
2013-09-04 13:27:56 +00:00
|
|
|
#else
|
2013-08-28 20:01:27 +00:00
|
|
|
return strtoull(s,(char **)0,10);
|
2013-09-07 16:23:53 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
static inline long long strTo64(const char *s)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
return (long long)_strtoi64(s,(char **)0,10);
|
|
|
|
#else
|
|
|
|
return strtoll(s,(char **)0,10);
|
2013-09-04 13:27:56 +00:00
|
|
|
#endif
|
2013-08-28 20:01:27 +00:00
|
|
|
}
|
2013-09-04 13:27:56 +00:00
|
|
|
static inline unsigned int hexStrToUInt(const char *s)
|
2013-08-28 20:01:27 +00:00
|
|
|
throw()
|
|
|
|
{
|
|
|
|
return (unsigned int)strtoul(s,(char **)0,16);
|
|
|
|
}
|
2013-09-07 16:23:53 +00:00
|
|
|
static inline int hexStrToInt(const char *s)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
return (int)strtol(s,(char **)0,16);
|
|
|
|
}
|
2013-09-04 13:27:56 +00:00
|
|
|
static inline unsigned long hexStrToULong(const char *s)
|
2013-08-28 20:01:27 +00:00
|
|
|
throw()
|
|
|
|
{
|
|
|
|
return strtoul(s,(char **)0,16);
|
|
|
|
}
|
2013-09-07 16:23:53 +00:00
|
|
|
static inline long hexStrToLong(const char *s)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
return strtol(s,(char **)0,16);
|
|
|
|
}
|
2013-09-04 13:27:56 +00:00
|
|
|
static inline unsigned long long hexStrToU64(const char *s)
|
2013-08-28 20:01:27 +00:00
|
|
|
throw()
|
|
|
|
{
|
2013-09-04 13:27:56 +00:00
|
|
|
#ifdef __WINDOWS__
|
2013-09-07 16:23:53 +00:00
|
|
|
return (unsigned long long)_strtoui64(s,(char **)0,16);
|
2013-09-04 13:27:56 +00:00
|
|
|
#else
|
2013-08-28 20:01:27 +00:00
|
|
|
return strtoull(s,(char **)0,16);
|
2013-09-07 16:23:53 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
static inline long long hexStrTo64(const char *s)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
return (long long)_strtoi64(s,(char **)0,16);
|
|
|
|
#else
|
|
|
|
return strtoll(s,(char **)0,16);
|
2013-09-04 13:27:56 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
static inline double strToDouble(const char *s)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
return strtod(s,(char **)0);
|
2013-08-28 20:01:27 +00:00
|
|
|
}
|
|
|
|
|
2013-08-28 19:09:49 +00:00
|
|
|
/**
|
|
|
|
* Perform a safe C string copy
|
|
|
|
*
|
|
|
|
* @param dest Destination buffer
|
|
|
|
* @param len Length of buffer
|
|
|
|
* @param src Source string
|
|
|
|
* @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)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
if (!len)
|
|
|
|
return false; // sanity check
|
|
|
|
char *end = dest + len;
|
|
|
|
while ((*dest++ = *src++)) {
|
|
|
|
if (dest == end) {
|
2013-09-30 15:05:35 +00:00
|
|
|
*(--dest) = (char)0;
|
2013-08-28 19:09:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* Trim whitespace from the start and end of a string
|
|
|
|
*
|
|
|
|
* @param s String to trim
|
|
|
|
* @return Trimmed string
|
|
|
|
*/
|
|
|
|
static std::string trim(const std::string &s);
|
|
|
|
|
2013-08-30 21:05:43 +00:00
|
|
|
/**
|
|
|
|
* Variant of snprintf that is portable and throws an exception
|
|
|
|
*
|
2014-04-18 07:14:12 +00:00
|
|
|
* This just wraps the local implementation whatever it's called, while
|
|
|
|
* performing a few other checks and adding exceptions for overflow.
|
|
|
|
*
|
2013-08-30 21:05:43 +00:00
|
|
|
* @param buf Buffer to write to
|
|
|
|
* @param len Length of buffer in bytes
|
|
|
|
* @param fmt Format string
|
|
|
|
* @param ... Format arguments
|
|
|
|
* @throws std::length_error buf[] too short (buf[] will still be left null-terminated)
|
|
|
|
*/
|
|
|
|
static unsigned int snprintf(char *buf,unsigned int len,const char *fmt,...)
|
|
|
|
throw(std::length_error);
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* Count the number of bits set in an integer
|
|
|
|
*
|
|
|
|
* @param v 32-bit integer
|
|
|
|
* @return Number of bits set in this integer (0-32)
|
|
|
|
*/
|
|
|
|
static inline uint32_t countBits(uint32_t v)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
v = v - ((v >> 1) & (uint32_t)0x55555555);
|
|
|
|
v = (v & (uint32_t)0x33333333) + ((v >> 2) & (uint32_t)0x33333333);
|
|
|
|
return ((((v + (v >> 4)) & (uint32_t)0xF0F0F0F) * (uint32_t)0x1010101) >> 24);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a memory buffer is all-zero
|
|
|
|
*
|
|
|
|
* @param p Memory to scan
|
|
|
|
* @param len Length of memory
|
|
|
|
* @return True if memory is all zero
|
|
|
|
*/
|
|
|
|
static inline bool isZero(const void *p,unsigned int len)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
for(unsigned int i=0;i<len;++i) {
|
|
|
|
if (((const unsigned char *)p)[i])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-15 19:32:06 +00:00
|
|
|
/**
|
|
|
|
* Compute SDBM hash of a binary string
|
|
|
|
*
|
|
|
|
* See: http://www.cse.yorku.ca/~oz/hash.html
|
|
|
|
*
|
|
|
|
* @param s Data to hash
|
|
|
|
* @param l Length in bytes
|
|
|
|
* @param h Previous hash value (use 0 initially)
|
|
|
|
* @tparam H Hash integer type -- should be unsigned
|
|
|
|
* @return New hash value
|
|
|
|
*/
|
|
|
|
template<typename H>
|
|
|
|
static inline H sdbmHash(const void *s,unsigned int l,H h)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
for(unsigned int i=0;i<l;++i)
|
|
|
|
h = ((H)(((const unsigned char *)s)[i])) + (h << 6) + (h << 16) - h;
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute SDBM hash of a 0-terminated C string
|
|
|
|
*
|
|
|
|
* See: http://www.cse.yorku.ca/~oz/hash.html
|
|
|
|
*
|
|
|
|
* @param s C-string to hash
|
|
|
|
* @param h Previous hash value (use 0 initially)
|
|
|
|
* @tparam H Hash integer type -- should be unsigned
|
|
|
|
* @return New hash value
|
|
|
|
*/
|
|
|
|
template<typename H>
|
|
|
|
static inline H sdbmHash(const char *s,H h)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
while ((c = *(s++)))
|
|
|
|
h = ((H)c) + (h << 6) + (h << 16) - h;
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute SDBM hash of an integer's bytes in little-endian byte order
|
|
|
|
*
|
|
|
|
* See: http://www.cse.yorku.ca/~oz/hash.html
|
|
|
|
*
|
|
|
|
* @param n Integer to hash in LE byte order
|
|
|
|
* @param h Previous hash value (use 0 initially)
|
|
|
|
* @tparam I Integer type -- should be unsigned
|
|
|
|
* @tparam H Hash integer type -- should be unsigned
|
|
|
|
* @return New hash value
|
|
|
|
*/
|
|
|
|
template<typename I,typename H>
|
|
|
|
static inline H sdbmHash(I n,H h)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
for(unsigned int i=0;i<(unsigned int)sizeof(n);++i) {
|
|
|
|
h = ((H)(n & 0xff)) + (h << 6) + (h << 16) - h;
|
|
|
|
n >>= 8;
|
|
|
|
}
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2013-11-06 15:38:19 +00:00
|
|
|
// Byte swappers for big/little endian conversion
|
2013-07-04 20:56:19 +00:00
|
|
|
static inline uint8_t hton(uint8_t n) throw() { return n; }
|
|
|
|
static inline int8_t hton(int8_t n) throw() { return n; }
|
|
|
|
static inline uint16_t hton(uint16_t n) throw() { return htons(n); }
|
|
|
|
static inline int16_t hton(int16_t n) throw() { return (int16_t)htons((uint16_t)n); }
|
|
|
|
static inline uint32_t hton(uint32_t n) throw() { return htonl(n); }
|
|
|
|
static inline int32_t hton(int32_t n) throw() { return (int32_t)htonl((uint32_t)n); }
|
|
|
|
static inline uint64_t hton(uint64_t n)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
|
|
|
#ifdef __GNUC__
|
|
|
|
return __builtin_bswap64(n);
|
|
|
|
#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
|
|
|
|
#else
|
|
|
|
return n;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
static inline int64_t hton(int64_t n) throw() { return (int64_t)hton((uint64_t)n); }
|
|
|
|
|
|
|
|
static inline uint8_t ntoh(uint8_t n) throw() { return n; }
|
|
|
|
static inline int8_t ntoh(int8_t n) throw() { return n; }
|
|
|
|
static inline uint16_t ntoh(uint16_t n) throw() { return ntohs(n); }
|
|
|
|
static inline int16_t ntoh(int16_t n) throw() { return (int16_t)ntohs((uint16_t)n); }
|
|
|
|
static inline uint32_t ntoh(uint32_t n) throw() { return ntohl(n); }
|
|
|
|
static inline int32_t ntoh(int32_t n) throw() { return (int32_t)ntohl((uint32_t)n); }
|
|
|
|
static inline uint64_t ntoh(uint64_t n)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
|
|
|
#ifdef __GNUC__
|
|
|
|
return __builtin_bswap64(n);
|
|
|
|
#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
|
|
|
|
#else
|
|
|
|
return n;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
static inline int64_t ntoh(int64_t n) throw() { return (int64_t)ntoh((uint64_t)n); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hexadecimal characters 0-f
|
|
|
|
*/
|
|
|
|
static const char HEXCHARS[16];
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|