diff options
Diffstat (limited to 'node/Dictionary.hpp')
-rw-r--r-- | node/Dictionary.hpp | 100 |
1 files changed, 62 insertions, 38 deletions
diff --git a/node/Dictionary.hpp b/node/Dictionary.hpp index 0db13b63..f89b6ffc 100644 --- a/node/Dictionary.hpp +++ b/node/Dictionary.hpp @@ -1,6 +1,6 @@ /* * ZeroTier One - Network Virtualization Everywhere - * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/ + * Copyright (C) 2011-2018 ZeroTier, Inc. https://www.zerotier.com/ * * 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 @@ -14,6 +14,14 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * -- + * + * You can be released from the requirements of the license by purchasing + * a commercial license. Buying such a license is mandatory as soon as you + * develop commercial closed-source software that incorporates or links + * directly against ZeroTier software without disclosing the source code + * of your own application. */ #ifndef ZT_DICTIONARY_HPP @@ -54,43 +62,29 @@ template<unsigned int C> class Dictionary { public: - Dictionary() - { - _d[0] = (char)0; - } - - Dictionary(const char *s) - { - if (s) { - Utils::scopy(_d,sizeof(_d),s); - } else { - _d[0] = (char)0; - } - } - + Dictionary() { memset(_d,0,sizeof(_d)); } + Dictionary(const char *s) { this->load(s); } Dictionary(const char *s,unsigned int len) { - if (s) { - if (len > (C-1)) - len = C-1; - memcpy(_d,s,len); - _d[len] = (char)0; - } else { - _d[0] = (char)0; + for(unsigned int i=0;i<C;++i) { + if ((s)&&(i < len)) { + if (!(_d[i] = *s)) + s = (const char *)0; + else ++s; + } else _d[i] = (char)0; } + _d[C - 1] = (char)0; } - - Dictionary(const Dictionary &d) - { - Utils::scopy(_d,sizeof(_d),d._d); - } + Dictionary(const Dictionary &d) { memcpy(_d,d._d,C); } inline Dictionary &operator=(const Dictionary &d) { - Utils::scopy(_d,sizeof(_d),d._d); + memcpy(_d,d._d,C); return *this; } + inline operator bool() const { return (_d[0] != 0); } + /** * Load a dictionary from a C-string * @@ -99,12 +93,15 @@ public: */ inline bool load(const char *s) { - if (s) { - return Utils::scopy(_d,sizeof(_d),s); - } else { - _d[0] = (char)0; - return true; + for(unsigned int i=0;i<C;++i) { + if (s) { + if (!(_d[i] = *s)) + s = (const char *)0; + else ++s; + } else _d[i] = (char)0; } + _d[C - 1] = (char)0; + return (!s); } /** @@ -112,7 +109,7 @@ public: */ inline void clear() { - _d[0] = (char)0; + memset(_d,0,sizeof(_d)); } /** @@ -279,6 +276,21 @@ public: } /** + * Get an unsigned int64 stored as hex in the dictionary + * + * @param key Key to look up + * @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 + { + char tmp[128]; + if (this->get(key,tmp,sizeof(tmp)) >= 1) + return Utils::hexStrTo64(tmp); + return dfl; + } + + /** * Add a new key=value pair * * If the key is already present this will append another, but the first @@ -381,8 +393,21 @@ public: inline bool add(const char *key,uint64_t value) { char tmp[32]; - Utils::snprintf(tmp,sizeof(tmp),"%llx",(unsigned long long)value); - return this->add(key,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) + { + char tmp[32]; + if (value >= 0) { + 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); + } } /** @@ -391,8 +416,7 @@ public: inline bool add(const char *key,const Address &a) { char tmp[32]; - Utils::snprintf(tmp,sizeof(tmp),"%.10llx",(unsigned long long)a.toInt()); - return this->add(key,tmp,-1); + return this->add(key,Utils::hex(a.toInt(),tmp),-1); } /** |