2013-07-27 19:09:51 +00:00
|
|
|
/*
|
2015-02-17 21:11:34 +00:00
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
2016-01-12 22:04:55 +00:00
|
|
|
* Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
|
2013-07-27 19:09:51 +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/>.
|
|
|
|
*/
|
|
|
|
|
2013-12-07 00:49:20 +00:00
|
|
|
#ifndef ZT_DICTIONARY_HPP
|
|
|
|
#define ZT_DICTIONARY_HPP
|
2013-07-27 19:09:51 +00:00
|
|
|
|
2016-04-26 15:53:49 +00:00
|
|
|
#include "Constants.hpp"
|
2016-06-16 01:47:35 +00:00
|
|
|
#include "Utils.hpp"
|
2016-06-16 19:28:43 +00:00
|
|
|
#include "Buffer.hpp"
|
|
|
|
#include "Address.hpp"
|
2016-04-26 15:53:49 +00:00
|
|
|
|
2014-08-14 23:52:22 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2013-07-27 19:09:51 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
/**
|
2016-06-21 14:49:46 +00:00
|
|
|
* A small (in code and data) packed key=value store
|
2016-06-16 01:47:35 +00:00
|
|
|
*
|
2016-06-21 14:49:46 +00:00
|
|
|
* This stores data in the form of a compact blob that is sort of human
|
|
|
|
* readable (depending on whether you put binary data in it) and is backward
|
|
|
|
* compatible with older versions. Binary data is escaped such that the
|
|
|
|
* serialized form of a Dictionary is always a valid null-terminated C string.
|
2013-07-27 19:09:51 +00:00
|
|
|
*
|
2016-06-21 14:49:46 +00:00
|
|
|
* Keys are restricted: no binary data, no CR/LF, and no equals (=). If a key
|
|
|
|
* contains these characters it may not be retrievable. This is not checked.
|
2014-08-05 16:56:49 +00:00
|
|
|
*
|
2016-06-21 14:49:46 +00:00
|
|
|
* Lookup is via linear search and will be slow with a lot of keys. It's
|
|
|
|
* designed for small things.
|
2014-08-05 16:56:49 +00:00
|
|
|
*
|
2016-06-16 19:28:43 +00:00
|
|
|
* There is code to test and fuzz this in selftest.cpp. Fuzzing a blob of
|
|
|
|
* pointer tricks like this is important after any modifications.
|
2016-06-21 14:32:58 +00:00
|
|
|
*
|
2016-06-21 14:49:46 +00:00
|
|
|
* This is used for network configurations and for saving some things on disk
|
|
|
|
* in the ZeroTier One service code.
|
|
|
|
*
|
2016-06-21 14:32:58 +00:00
|
|
|
* @tparam C Dictionary max capacity in bytes
|
2013-07-27 19:09:51 +00:00
|
|
|
*/
|
2016-06-21 14:32:58 +00:00
|
|
|
template<unsigned int C>
|
2016-06-16 01:47:35 +00:00
|
|
|
class Dictionary
|
2013-07-27 19:09:51 +00:00
|
|
|
{
|
|
|
|
public:
|
2016-06-16 01:47:35 +00:00
|
|
|
Dictionary()
|
2013-07-29 17:56:20 +00:00
|
|
|
{
|
2016-06-16 01:47:35 +00:00
|
|
|
_d[0] = (char)0;
|
2013-07-29 17:56:20 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 01:47:35 +00:00
|
|
|
Dictionary(const char *s)
|
|
|
|
{
|
|
|
|
Utils::scopy(_d,sizeof(_d),s);
|
|
|
|
}
|
2015-01-07 01:16:54 +00:00
|
|
|
|
2016-06-16 19:28:43 +00:00
|
|
|
Dictionary(const char *s,unsigned int len)
|
2015-01-07 01:16:54 +00:00
|
|
|
{
|
2016-06-21 14:32:58 +00:00
|
|
|
memcpy(_d,s,(len > C) ? (unsigned int)C : len);
|
|
|
|
_d[C-1] = (char)0;
|
2016-06-16 19:28:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary(const Dictionary &d)
|
|
|
|
{
|
|
|
|
Utils::scopy(_d,sizeof(_d),d._d);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Dictionary &operator=(const Dictionary &d)
|
|
|
|
{
|
|
|
|
Utils::scopy(_d,sizeof(_d),d._d);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a dictionary from a C-string
|
|
|
|
*
|
|
|
|
* @param s Dictionary in string form
|
2016-06-21 14:32:58 +00:00
|
|
|
* @return False if 's' was longer than our capacity
|
2016-06-16 19:28:43 +00:00
|
|
|
*/
|
|
|
|
inline bool load(const char *s)
|
|
|
|
{
|
|
|
|
return Utils::scopy(_d,sizeof(_d),s);
|
2015-01-07 01:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-16 01:47:35 +00:00
|
|
|
* Delete all entries
|
2015-01-07 01:16:54 +00:00
|
|
|
*/
|
2016-06-16 01:47:35 +00:00
|
|
|
inline void clear()
|
2015-01-07 01:16:54 +00:00
|
|
|
{
|
2016-06-16 01:47:35 +00:00
|
|
|
_d[0] = (char)0;
|
2015-01-07 01:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-16 01:47:35 +00:00
|
|
|
* @return Size of dictionary in bytes not including terminating NULL
|
2015-01-07 01:16:54 +00:00
|
|
|
*/
|
2016-06-16 01:47:35 +00:00
|
|
|
inline unsigned int sizeBytes() const
|
2015-01-07 01:16:54 +00:00
|
|
|
{
|
2016-06-21 14:32:58 +00:00
|
|
|
for(unsigned int i=0;i<C;++i) {
|
2016-06-16 01:47:35 +00:00
|
|
|
if (!_d[i])
|
|
|
|
return i;
|
|
|
|
}
|
2016-06-21 14:49:46 +00:00
|
|
|
return C-1;
|
2015-01-07 01:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-16 01:47:35 +00:00
|
|
|
* Get an entry
|
|
|
|
*
|
|
|
|
* Note that to get binary values, dest[] should be at least one more than
|
|
|
|
* the maximum size of the value being retrieved. That's because even if
|
2016-06-21 14:49:46 +00:00
|
|
|
* the data is binary a terminating 0 is still appended to dest[] after it.
|
2016-06-16 01:47:35 +00:00
|
|
|
*
|
|
|
|
* If the key is not found, dest[0] is set to 0 to make dest[] an empty
|
2016-06-21 14:49:46 +00:00
|
|
|
* C string in that case. The dest[] array will *never* be unterminated
|
|
|
|
* after this call.
|
2016-06-16 01:47:35 +00:00
|
|
|
*
|
2016-06-21 14:59:42 +00:00
|
|
|
* Security note: if 'key' is ever directly based on anything that is not
|
|
|
|
* a hard-code or internally-generated name, it must be checked to ensure
|
|
|
|
* that the buffer is NULL-terminated since key[] does not take a secondary
|
|
|
|
* size parameter. In NetworkConfig all keys are hard-coded strings so this
|
|
|
|
* isn't a problem in the core.
|
|
|
|
*
|
2016-06-16 01:47:35 +00:00
|
|
|
* @param key Key to look up
|
|
|
|
* @param dest Destination buffer
|
|
|
|
* @param destlen Size of destination buffer
|
|
|
|
* @return -1 if not found, or actual number of bytes stored in dest[] minus trailing 0
|
2015-01-07 01:16:54 +00:00
|
|
|
*/
|
2016-06-16 01:47:35 +00:00
|
|
|
inline int get(const char *key,char *dest,unsigned int destlen) const
|
2015-01-07 01:16:54 +00:00
|
|
|
{
|
2016-06-16 01:47:35 +00:00
|
|
|
const char *p = _d;
|
2016-06-21 14:59:42 +00:00
|
|
|
const char *const eof = p + C;
|
2016-06-16 22:48:58 +00:00
|
|
|
const char *k;
|
2016-06-16 01:47:35 +00:00
|
|
|
bool esc;
|
|
|
|
int j;
|
|
|
|
|
2016-06-16 22:48:58 +00:00
|
|
|
if (!destlen) // sanity check
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
while (*p) {
|
|
|
|
k = key;
|
2016-06-21 14:59:42 +00:00
|
|
|
while ((*k)&&(*p)) {
|
2016-06-16 22:48:58 +00:00
|
|
|
if (*p != *k)
|
|
|
|
break;
|
|
|
|
++k;
|
2016-06-21 14:59:42 +00:00
|
|
|
if (++p == eof) {
|
|
|
|
dest[0] = (char)0;
|
|
|
|
return -1;
|
|
|
|
}
|
2016-06-16 22:48:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((!*k)&&(*p == '=')) {
|
|
|
|
j = 0;
|
|
|
|
esc = false;
|
|
|
|
++p;
|
|
|
|
while ((*p)&&(*p != '\r')&&(*p != '\n')) {
|
|
|
|
if (esc) {
|
2016-06-16 01:47:35 +00:00
|
|
|
esc = false;
|
2016-06-16 22:48:58 +00:00
|
|
|
switch(*p) {
|
|
|
|
case 'r': dest[j++] = '\r'; break;
|
|
|
|
case 'n': dest[j++] = '\n'; break;
|
|
|
|
case '0': dest[j++] = (char)0; break;
|
|
|
|
case 'e': dest[j++] = '='; break;
|
|
|
|
default: dest[j++] = *p; break;
|
2016-06-16 01:47:35 +00:00
|
|
|
}
|
2016-06-16 22:48:58 +00:00
|
|
|
if (j == (int)destlen) {
|
|
|
|
dest[j-1] = (char)0;
|
|
|
|
return j-1;
|
|
|
|
}
|
|
|
|
} else if (*p == '\\') {
|
|
|
|
esc = true;
|
2016-06-16 01:47:35 +00:00
|
|
|
} else {
|
2016-06-16 22:48:58 +00:00
|
|
|
dest[j++] = *p;
|
|
|
|
if (j == (int)destlen) {
|
|
|
|
dest[j-1] = (char)0;
|
|
|
|
return j-1;
|
2016-06-16 01:47:35 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-21 14:59:42 +00:00
|
|
|
if (++p == eof) {
|
|
|
|
dest[0] = (char)0;
|
|
|
|
return -1;
|
|
|
|
}
|
2016-06-16 01:47:35 +00:00
|
|
|
}
|
2016-06-16 22:48:58 +00:00
|
|
|
dest[j] = (char)0;
|
|
|
|
return j;
|
|
|
|
} else {
|
2016-06-21 14:59:42 +00:00
|
|
|
while ((*p)&&(*p != '\r')&&(*p != '\n')) {
|
|
|
|
if (++p == eof) {
|
|
|
|
dest[0] = (char)0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*p) {
|
|
|
|
if (++p == eof) {
|
|
|
|
dest[0] = (char)0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2016-06-16 22:48:58 +00:00
|
|
|
else break;
|
2016-06-16 01:47:35 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-16 22:48:58 +00:00
|
|
|
|
|
|
|
dest[0] = (char)0;
|
|
|
|
return -1;
|
2015-01-07 01:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-16 19:28:43 +00:00
|
|
|
* Get the contents of a key into a buffer
|
|
|
|
*
|
|
|
|
* @param key Key to get
|
|
|
|
* @param dest Destination buffer
|
|
|
|
* @return True if key was found (if false, dest will be empty)
|
2016-06-21 14:32:58 +00:00
|
|
|
* @tparam BC Buffer capacity (usually inferred)
|
2016-06-16 19:28:43 +00:00
|
|
|
*/
|
2016-06-21 14:32:58 +00:00
|
|
|
template<unsigned int BC>
|
|
|
|
inline bool get(const char *key,Buffer<BC> &dest) const
|
2016-06-16 19:28:43 +00:00
|
|
|
{
|
2016-06-21 15:00:40 +00:00
|
|
|
const int r = this->get(key,const_cast<char *>(reinterpret_cast<const char *>(dest.data())),BC);
|
2016-06-16 19:28:43 +00:00
|
|
|
if (r >= 0) {
|
|
|
|
dest.setSize((unsigned int)r);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
dest.clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a boolean value
|
|
|
|
*
|
2016-06-16 01:47:35 +00:00
|
|
|
* @param key Key to look up
|
2016-06-16 19:28:43 +00:00
|
|
|
* @param dfl Default value if not found in dictionary
|
2016-06-16 01:47:35 +00:00
|
|
|
* @return Boolean value of key or 'dfl' if not found
|
2015-01-07 01:16:54 +00:00
|
|
|
*/
|
2016-06-16 19:28:43 +00:00
|
|
|
bool getB(const char *key,bool dfl = false) const
|
2015-01-07 01:16:54 +00:00
|
|
|
{
|
2016-06-16 19:28:43 +00:00
|
|
|
char tmp[4];
|
|
|
|
if (this->get(key,tmp,sizeof(tmp)) >= 0)
|
|
|
|
return ((*tmp == '1')||(*tmp == 't')||(*tmp == 'T'));
|
2016-06-16 01:47:35 +00:00
|
|
|
return dfl;
|
2015-01-07 01:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-16 19:28:43 +00:00
|
|
|
* Get an unsigned int64 stored as hex in the dictionary
|
|
|
|
*
|
2016-06-16 01:47:35 +00:00
|
|
|
* @param key Key to look up
|
|
|
|
* @param dfl Default value or 0 if unspecified
|
|
|
|
* @return Decoded hex UInt value or 'dfl' if not found
|
2015-01-07 01:16:54 +00:00
|
|
|
*/
|
2016-06-16 19:28:43 +00:00
|
|
|
inline uint64_t getUI(const char *key,uint64_t dfl = 0) const
|
2015-01-07 01:16:54 +00:00
|
|
|
{
|
2016-06-16 01:47:35 +00:00
|
|
|
char tmp[128];
|
|
|
|
if (this->get(key,tmp,sizeof(tmp)) >= 1)
|
|
|
|
return Utils::hexStrToU64(tmp);
|
|
|
|
return dfl;
|
2015-01-07 01:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-16 01:47:35 +00:00
|
|
|
* Add a new key=value pair
|
|
|
|
*
|
|
|
|
* If the key is already present this will append another, but the first
|
2016-06-16 21:09:09 +00:00
|
|
|
* will always be returned by get(). This is not checked. If you want to
|
|
|
|
* ensure a key is not present use erase() first.
|
2016-06-16 01:47:35 +00:00
|
|
|
*
|
2016-06-16 19:28:43 +00:00
|
|
|
* Use the vlen parameter to add binary values. Nulls will be escaped.
|
|
|
|
*
|
2016-06-16 01:47:35 +00:00
|
|
|
* @param key Key -- nulls, CR/LF, and equals (=) are illegal characters
|
|
|
|
* @param value Value to set
|
|
|
|
* @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
|
2015-01-07 01:16:54 +00:00
|
|
|
*/
|
2016-06-16 01:47:35 +00:00
|
|
|
inline bool add(const char *key,const char *value,int vlen = -1)
|
2015-01-07 01:16:54 +00:00
|
|
|
{
|
2016-06-21 14:32:58 +00:00
|
|
|
for(unsigned int i=0;i<C;++i) {
|
2016-06-16 01:47:35 +00:00
|
|
|
if (!_d[i]) {
|
|
|
|
unsigned int j = i;
|
2016-06-16 21:45:36 +00:00
|
|
|
|
|
|
|
if (j > 0) {
|
|
|
|
_d[j++] = '\n';
|
2016-06-21 14:32:58 +00:00
|
|
|
if (j == C) {
|
2016-06-16 21:45:36 +00:00
|
|
|
_d[i] = (char)0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-16 01:47:35 +00:00
|
|
|
const char *p = key;
|
|
|
|
while (*p) {
|
|
|
|
_d[j++] = *(p++);
|
2016-06-21 14:32:58 +00:00
|
|
|
if (j == C) {
|
2016-06-16 01:47:35 +00:00
|
|
|
_d[i] = (char)0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-06-16 21:45:36 +00:00
|
|
|
|
|
|
|
_d[j++] = '=';
|
2016-06-21 14:32:58 +00:00
|
|
|
if (j == C) {
|
2016-06-16 21:45:36 +00:00
|
|
|
_d[i] = (char)0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-16 01:47:35 +00:00
|
|
|
p = value;
|
|
|
|
int k = 0;
|
2016-06-16 21:45:36 +00:00
|
|
|
while ( ((*p)&&(vlen < 0)) || (k < vlen) ) {
|
2016-06-16 01:47:35 +00:00
|
|
|
switch(*p) {
|
2016-06-16 19:28:43 +00:00
|
|
|
case 0:
|
2016-06-16 01:47:35 +00:00
|
|
|
case '\r':
|
|
|
|
case '\n':
|
2016-06-16 19:28:43 +00:00
|
|
|
case '\\':
|
2016-06-16 21:09:09 +00:00
|
|
|
case '=':
|
2016-06-16 01:47:35 +00:00
|
|
|
_d[j++] = '\\';
|
2016-06-21 14:32:58 +00:00
|
|
|
if (j == C) {
|
2016-06-16 01:47:35 +00:00
|
|
|
_d[i] = (char)0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
switch(*p) {
|
2016-06-16 19:28:43 +00:00
|
|
|
case 0: _d[j++] = '0'; break;
|
2016-06-16 01:47:35 +00:00
|
|
|
case '\r': _d[j++] = 'r'; break;
|
|
|
|
case '\n': _d[j++] = 'n'; break;
|
2016-06-16 19:28:43 +00:00
|
|
|
case '\\': _d[j++] = '\\'; break;
|
2016-06-16 21:09:09 +00:00
|
|
|
case '=': _d[j++] = 'e'; break;
|
2016-06-16 01:47:35 +00:00
|
|
|
}
|
2016-06-21 14:32:58 +00:00
|
|
|
if (j == C) {
|
2016-06-16 01:47:35 +00:00
|
|
|
_d[i] = (char)0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
_d[j++] = *p;
|
2016-06-21 14:32:58 +00:00
|
|
|
if (j == C) {
|
2016-06-16 01:47:35 +00:00
|
|
|
_d[i] = (char)0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++p;
|
|
|
|
++k;
|
|
|
|
}
|
2016-06-16 21:45:36 +00:00
|
|
|
|
|
|
|
_d[j] = (char)0;
|
|
|
|
|
2016-06-16 01:47:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2015-01-07 01:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-16 01:47:35 +00:00
|
|
|
* Add a boolean as a '1' or a '0'
|
2015-01-07 01:16:54 +00:00
|
|
|
*/
|
2016-06-16 19:28:43 +00:00
|
|
|
inline bool add(const char *key,bool value)
|
2015-01-07 01:16:54 +00:00
|
|
|
{
|
2016-06-16 19:28:43 +00:00
|
|
|
return this->add(key,(value) ? "1" : "0",1);
|
2015-01-07 01:16:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 01:47:35 +00:00
|
|
|
/**
|
|
|
|
* Add a 64-bit integer (unsigned) as a hex value
|
2015-01-07 01:16:54 +00:00
|
|
|
*/
|
2016-06-16 19:28:43 +00:00
|
|
|
inline bool add(const char *key,uint64_t value)
|
2015-01-07 01:16:54 +00:00
|
|
|
{
|
2016-06-16 19:28:43 +00:00
|
|
|
char tmp[32];
|
2015-01-07 01:16:54 +00:00
|
|
|
Utils::snprintf(tmp,sizeof(tmp),"%llx",(unsigned long long)value);
|
2016-06-16 19:28:43 +00:00
|
|
|
return this->add(key,tmp,-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a 64-bit integer (unsigned) as a hex value
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-21 14:32:58 +00:00
|
|
|
* Add a binary buffer's contents as a value
|
|
|
|
*
|
|
|
|
* @tparam BC Buffer capacity (usually inferred)
|
2016-06-16 19:28:43 +00:00
|
|
|
*/
|
2016-06-21 14:32:58 +00:00
|
|
|
template<unsigned int BC>
|
|
|
|
inline bool add(const char *key,const Buffer<BC> &value)
|
2016-06-16 19:28:43 +00:00
|
|
|
{
|
|
|
|
return this->add(key,(const char *)value.data(),(int)value.size());
|
2015-01-07 01:16:54 +00:00
|
|
|
}
|
|
|
|
|
2013-07-29 17:56:20 +00:00
|
|
|
/**
|
|
|
|
* @param key Key to check
|
2016-06-16 01:47:35 +00:00
|
|
|
* @return True if key is present
|
2014-08-14 23:52:22 +00:00
|
|
|
*/
|
2016-06-16 01:47:35 +00:00
|
|
|
inline bool contains(const char *key) const
|
2013-07-27 19:09:51 +00:00
|
|
|
{
|
2016-06-16 01:47:35 +00:00
|
|
|
char tmp[2];
|
|
|
|
return (this->get(key,tmp,2) >= 0);
|
2013-07-27 19:09:51 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 21:09:09 +00:00
|
|
|
/**
|
|
|
|
* Erase a key from this dictionary
|
|
|
|
*
|
|
|
|
* Use this before add() to ensure that a key is replaced if it might
|
|
|
|
* already be present.
|
|
|
|
*
|
|
|
|
* @param key Key to erase
|
|
|
|
* @return True if key was found and erased
|
|
|
|
*/
|
|
|
|
inline bool erase(const char *key)
|
|
|
|
{
|
2016-06-21 14:32:58 +00:00
|
|
|
char d2[C];
|
2016-06-16 21:09:09 +00:00
|
|
|
char *saveptr = (char *)0;
|
|
|
|
unsigned int d2ptr = 0;
|
|
|
|
bool found = false;
|
|
|
|
for(char *f=Utils::stok(_d,"\r\n",&saveptr);(f);f=Utils::stok((char *)0,"\r\n",&saveptr)) {
|
|
|
|
if (*f) {
|
|
|
|
const char *p = f;
|
|
|
|
const char *k = key;
|
|
|
|
while ((*k)&&(*p)) {
|
|
|
|
if (*k != *p)
|
|
|
|
break;
|
|
|
|
++k;
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
if (*k) {
|
|
|
|
p = f;
|
|
|
|
while (*p)
|
|
|
|
d2[d2ptr++] = *(p++);
|
|
|
|
d2[d2ptr++] = '\n';
|
|
|
|
} else {
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d2[d2ptr++] = (char)0;
|
|
|
|
memcpy(_d,d2,d2ptr);
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2014-08-05 16:56:49 +00:00
|
|
|
/**
|
2016-06-16 01:47:35 +00:00
|
|
|
* @return Dictionary data as a 0-terminated C-string
|
2014-08-05 16:56:49 +00:00
|
|
|
*/
|
2016-06-16 01:47:35 +00:00
|
|
|
inline const char *data() const { return _d; }
|
2014-08-05 16:56:49 +00:00
|
|
|
|
2016-06-21 14:32:58 +00:00
|
|
|
/**
|
|
|
|
* @return Value of C template parameter
|
|
|
|
*/
|
|
|
|
inline unsigned int capacity() const { return C; }
|
|
|
|
|
2013-07-27 19:09:51 +00:00
|
|
|
private:
|
2016-06-21 14:32:58 +00:00
|
|
|
char _d[C];
|
2013-07-27 19:09:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|