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
|
@ -14,10 +14,10 @@
|
|||
#ifndef ZT_DICTIONARY_HPP
|
||||
#define ZT_DICTIONARY_HPP
|
||||
|
||||
#include "Address.hpp"
|
||||
#include "Buffer.hpp"
|
||||
#include "Constants.hpp"
|
||||
#include "Utils.hpp"
|
||||
#include "Buffer.hpp"
|
||||
#include "Address.hpp"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
@ -45,36 +45,48 @@ namespace ZeroTier {
|
|||
*
|
||||
* @tparam C Dictionary max capacity in bytes
|
||||
*/
|
||||
template<unsigned int C>
|
||||
class Dictionary
|
||||
{
|
||||
public:
|
||||
Dictionary() { memset(_d,0,sizeof(_d)); }
|
||||
Dictionary(const char *s) { this->load(s); }
|
||||
Dictionary(const char *s,unsigned int len)
|
||||
template <unsigned int C> class Dictionary {
|
||||
public:
|
||||
Dictionary()
|
||||
{
|
||||
for(unsigned int i=0;i<C;++i) {
|
||||
if ((s)&&(i < len)) {
|
||||
if (!(_d[i] = *s)) {
|
||||
s = (const char *)0;
|
||||
} else {
|
||||
memset(_d, 0, sizeof(_d));
|
||||
}
|
||||
Dictionary(const char* s)
|
||||
{
|
||||
this->load(s);
|
||||
}
|
||||
Dictionary(const char* s, unsigned int len)
|
||||
{
|
||||
for (unsigned int i = 0; i < C; ++i) {
|
||||
if ((s) && (i < len)) {
|
||||
if (! (_d[i] = *s)) {
|
||||
s = (const char*)0;
|
||||
}
|
||||
else {
|
||||
++s;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
_d[i] = (char)0;
|
||||
}
|
||||
}
|
||||
_d[C - 1] = (char)0;
|
||||
}
|
||||
Dictionary(const Dictionary &d) { memcpy(_d,d._d,C); }
|
||||
|
||||
inline Dictionary &operator=(const Dictionary &d)
|
||||
Dictionary(const Dictionary& d)
|
||||
{
|
||||
memcpy(_d,d._d,C);
|
||||
memcpy(_d, d._d, C);
|
||||
}
|
||||
|
||||
inline Dictionary& operator=(const Dictionary& d)
|
||||
{
|
||||
memcpy(_d, d._d, C);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline operator bool() const { return (_d[0] != 0); }
|
||||
inline operator bool() const
|
||||
{
|
||||
return (_d[0] != 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a dictionary from a C-string
|
||||
|
@ -82,21 +94,23 @@ public:
|
|||
* @param s Dictionary in string form
|
||||
* @return False if 's' was longer than our capacity
|
||||
*/
|
||||
inline bool load(const char *s)
|
||||
inline bool load(const char* s)
|
||||
{
|
||||
for(unsigned int i=0;i<C;++i) {
|
||||
for (unsigned int i = 0; i < C; ++i) {
|
||||
if (s) {
|
||||
if (!(_d[i] = *s)) {
|
||||
s = (const char *)0;
|
||||
} else {
|
||||
if (! (_d[i] = *s)) {
|
||||
s = (const char*)0;
|
||||
}
|
||||
else {
|
||||
++s;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
_d[i] = (char)0;
|
||||
}
|
||||
}
|
||||
_d[C - 1] = (char)0;
|
||||
return (!s);
|
||||
return (! s);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -104,7 +118,7 @@ public:
|
|||
*/
|
||||
inline void clear()
|
||||
{
|
||||
memset(_d,0,sizeof(_d));
|
||||
memset(_d, 0, sizeof(_d));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -112,12 +126,12 @@ public:
|
|||
*/
|
||||
inline unsigned int sizeBytes() const
|
||||
{
|
||||
for(unsigned int i=0;i<C;++i) {
|
||||
if (!_d[i]) {
|
||||
for (unsigned int i = 0; i < C; ++i) {
|
||||
if (! _d[i]) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return C-1;
|
||||
return C - 1;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -142,21 +156,21 @@ public:
|
|||
* @param destlen Size of destination buffer
|
||||
* @return -1 if not found, or actual number of bytes stored in dest[] minus trailing 0
|
||||
*/
|
||||
inline int get(const char *key,char *dest,unsigned int destlen) const
|
||||
inline int get(const char* key, char* dest, unsigned int destlen) const
|
||||
{
|
||||
const char *p = _d;
|
||||
const char *const eof = p + C;
|
||||
const char *k;
|
||||
const char* p = _d;
|
||||
const char* const eof = p + C;
|
||||
const char* k;
|
||||
bool esc;
|
||||
int j;
|
||||
|
||||
if (!destlen) { // sanity check
|
||||
if (! destlen) { // sanity check
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (*p) {
|
||||
k = key;
|
||||
while ((*k)&&(*p)) {
|
||||
while ((*k) && (*p)) {
|
||||
if (*p != *k) {
|
||||
break;
|
||||
}
|
||||
|
@ -167,14 +181,14 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
if ((!*k)&&(*p == '=')) {
|
||||
if ((! *k) && (*p == '=')) {
|
||||
j = 0;
|
||||
esc = false;
|
||||
++p;
|
||||
while ((*p != 0)&&(*p != 13)&&(*p != 10)) {
|
||||
while ((*p != 0) && (*p != 13) && (*p != 10)) {
|
||||
if (esc) {
|
||||
esc = false;
|
||||
switch(*p) {
|
||||
switch (*p) {
|
||||
case 'r':
|
||||
dest[j++] = 13;
|
||||
break;
|
||||
|
@ -192,16 +206,18 @@ public:
|
|||
break;
|
||||
}
|
||||
if (j == (int)destlen) {
|
||||
dest[j-1] = (char)0;
|
||||
return j-1;
|
||||
dest[j - 1] = (char)0;
|
||||
return j - 1;
|
||||
}
|
||||
} else if (*p == '\\') {
|
||||
}
|
||||
else if (*p == '\\') {
|
||||
esc = true;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
dest[j++] = *p;
|
||||
if (j == (int)destlen) {
|
||||
dest[j-1] = (char)0;
|
||||
return j-1;
|
||||
dest[j - 1] = (char)0;
|
||||
return j - 1;
|
||||
}
|
||||
}
|
||||
if (++p == eof) {
|
||||
|
@ -211,8 +227,9 @@ public:
|
|||
}
|
||||
dest[j] = (char)0;
|
||||
return j;
|
||||
} else {
|
||||
while ((*p)&&(*p != 13)&&(*p != 10)) {
|
||||
}
|
||||
else {
|
||||
while ((*p) && (*p != 13) && (*p != 10)) {
|
||||
if (++p == eof) {
|
||||
dest[0] = (char)0;
|
||||
return -1;
|
||||
|
@ -223,7 +240,8 @@ public:
|
|||
dest[0] = (char)0;
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -241,14 +259,14 @@ public:
|
|||
* @return True if key was found (if false, dest will be empty)
|
||||
* @tparam BC Buffer capacity (usually inferred)
|
||||
*/
|
||||
template<unsigned int BC>
|
||||
inline bool get(const char *key,Buffer<BC> &dest) const
|
||||
template <unsigned int BC> inline bool get(const char* key, Buffer<BC>& dest) const
|
||||
{
|
||||
const int r = this->get(key,const_cast<char *>(reinterpret_cast<const char *>(dest.data())),BC);
|
||||
const int r = this->get(key, const_cast<char*>(reinterpret_cast<const char*>(dest.data())), BC);
|
||||
if (r >= 0) {
|
||||
dest.setSize((unsigned int)r);
|
||||
return true;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
dest.clear();
|
||||
return false;
|
||||
}
|
||||
|
@ -261,11 +279,11 @@ public:
|
|||
* @param dfl Default value if not found in dictionary
|
||||
* @return Boolean value of key or 'dfl' if not found
|
||||
*/
|
||||
bool getB(const char *key,bool dfl = false) const
|
||||
bool getB(const char* key, bool dfl = false) const
|
||||
{
|
||||
char tmp[4];
|
||||
if (this->get(key,tmp,sizeof(tmp)) >= 0) {
|
||||
return ((*tmp == '1')||(*tmp == 't')||(*tmp == 'T'));
|
||||
if (this->get(key, tmp, sizeof(tmp)) >= 0) {
|
||||
return ((*tmp == '1') || (*tmp == 't') || (*tmp == 'T'));
|
||||
}
|
||||
return dfl;
|
||||
}
|
||||
|
@ -277,10 +295,10 @@ public:
|
|||
* @param dfl Default value or 0 if unspecified
|
||||
* @return Decoded hex UInt value or 'dfl' if not found
|
||||
*/
|
||||
inline uint64_t getUI(const char *key,uint64_t dfl = 0) const
|
||||
inline uint64_t getUI(const char* key, uint64_t dfl = 0) const
|
||||
{
|
||||
char tmp[128];
|
||||
if (this->get(key,tmp,sizeof(tmp)) >= 1) {
|
||||
if (this->get(key, tmp, sizeof(tmp)) >= 1) {
|
||||
return Utils::hexStrToU64(tmp);
|
||||
}
|
||||
return dfl;
|
||||
|
@ -293,10 +311,10 @@ public:
|
|||
* @param dfl Default value or 0 if unspecified
|
||||
* @return Decoded hex UInt value or 'dfl' if not found
|
||||
*/
|
||||
inline int64_t getI(const char *key,int64_t dfl = 0) const
|
||||
inline int64_t getI(const char* key, int64_t dfl = 0) const
|
||||
{
|
||||
char tmp[128];
|
||||
if (this->get(key,tmp,sizeof(tmp)) >= 1) {
|
||||
if (this->get(key, tmp, sizeof(tmp)) >= 1) {
|
||||
return Utils::hexStrTo64(tmp);
|
||||
}
|
||||
return dfl;
|
||||
|
@ -316,10 +334,10 @@ public:
|
|||
* @param vlen Length of value in bytes or -1 to treat value[] as a C-string and look for terminating 0
|
||||
* @return True if there was enough room to add this key=value pair
|
||||
*/
|
||||
inline bool add(const char *key,const char *value,int vlen = -1)
|
||||
inline bool add(const char* key, const char* value, int vlen = -1)
|
||||
{
|
||||
for(unsigned int i=0;i<C;++i) {
|
||||
if (!_d[i]) {
|
||||
for (unsigned int i = 0; i < C; ++i) {
|
||||
if (! _d[i]) {
|
||||
unsigned int j = i;
|
||||
|
||||
if (j > 0) {
|
||||
|
@ -330,7 +348,7 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
const char *p = key;
|
||||
const char* p = key;
|
||||
while (*p) {
|
||||
_d[j++] = *(p++);
|
||||
if (j == C) {
|
||||
|
@ -347,8 +365,8 @@ public:
|
|||
|
||||
p = value;
|
||||
int k = 0;
|
||||
while ( ((vlen < 0)&&(*p)) || (k < vlen) ) {
|
||||
switch(*p) {
|
||||
while (((vlen < 0) && (*p)) || (k < vlen)) {
|
||||
switch (*p) {
|
||||
case 0:
|
||||
case 13:
|
||||
case 10:
|
||||
|
@ -359,7 +377,7 @@ public:
|
|||
_d[i] = (char)0;
|
||||
return false;
|
||||
}
|
||||
switch(*p) {
|
||||
switch (*p) {
|
||||
case 0:
|
||||
_d[j++] = '0';
|
||||
break;
|
||||
|
@ -404,41 +422,42 @@ public:
|
|||
/**
|
||||
* Add a boolean as a '1' or a '0'
|
||||
*/
|
||||
inline bool add(const char *key,bool value)
|
||||
inline bool add(const char* key, bool value)
|
||||
{
|
||||
return this->add(key,(value) ? "1" : "0",1);
|
||||
return this->add(key, (value) ? "1" : "0", 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a 64-bit integer (unsigned) as a hex value
|
||||
*/
|
||||
inline bool add(const char *key,uint64_t value)
|
||||
inline bool add(const char* key, uint64_t value)
|
||||
{
|
||||
char tmp[32];
|
||||
return this->add(key,Utils::hex(value,tmp),-1);
|
||||
return this->add(key, Utils::hex(value, tmp), -1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a 64-bit integer (unsigned) as a hex value
|
||||
*/
|
||||
inline bool add(const char *key,int64_t value)
|
||||
inline bool add(const char* key, int64_t value)
|
||||
{
|
||||
char tmp[32];
|
||||
if (value >= 0) {
|
||||
return this->add(key,Utils::hex((uint64_t)value,tmp),-1);
|
||||
} else {
|
||||
return this->add(key, Utils::hex((uint64_t)value, tmp), -1);
|
||||
}
|
||||
else {
|
||||
tmp[0] = '-';
|
||||
return this->add(key,Utils::hex((uint64_t)(value * -1),tmp+1),-1);
|
||||
return this->add(key, Utils::hex((uint64_t)(value * -1), tmp + 1), -1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a 64-bit integer (unsigned) as a hex value
|
||||
*/
|
||||
inline bool add(const char *key,const Address &a)
|
||||
inline bool add(const char* key, const Address& a)
|
||||
{
|
||||
char tmp[32];
|
||||
return this->add(key,Utils::hex(a.toInt(),tmp),-1);
|
||||
return this->add(key, Utils::hex(a.toInt(), tmp), -1);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -446,34 +465,42 @@ public:
|
|||
*
|
||||
* @tparam BC Buffer capacity (usually inferred)
|
||||
*/
|
||||
template<unsigned int BC>
|
||||
inline bool add(const char *key,const Buffer<BC> &value)
|
||||
template <unsigned int BC> inline bool add(const char* key, const Buffer<BC>& value)
|
||||
{
|
||||
return this->add(key,(const char *)value.data(),(int)value.size());
|
||||
return this->add(key, (const char*)value.data(), (int)value.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key Key to check
|
||||
* @return True if key is present
|
||||
*/
|
||||
inline bool contains(const char *key) const
|
||||
inline bool contains(const char* key) const
|
||||
{
|
||||
char tmp[2];
|
||||
return (this->get(key,tmp,2) >= 0);
|
||||
return (this->get(key, tmp, 2) >= 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Value of C template parameter
|
||||
*/
|
||||
inline unsigned int capacity() const { return C; }
|
||||
inline unsigned int capacity() const
|
||||
{
|
||||
return C;
|
||||
}
|
||||
|
||||
inline const char *data() const { return _d; }
|
||||
inline char *unsafeData() { return _d; }
|
||||
inline const char* data() const
|
||||
{
|
||||
return _d;
|
||||
}
|
||||
inline char* unsafeData()
|
||||
{
|
||||
return _d;
|
||||
}
|
||||
|
||||
private:
|
||||
private:
|
||||
char _d[C];
|
||||
};
|
||||
|
||||
} // namespace ZeroTier
|
||||
} // namespace ZeroTier
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue