Dictionary.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_DICTIONARY_HPP
  14. #define ZT_DICTIONARY_HPP
  15. #include "Address.hpp"
  16. #include "Buffer.hpp"
  17. #include "Constants.hpp"
  18. #include "Utils.hpp"
  19. #include <stdint.h>
  20. namespace ZeroTier {
  21. /**
  22. * A small (in code and data) packed key=value store
  23. *
  24. * This stores data in the form of a compact blob that is sort of human
  25. * readable (depending on whether you put binary data in it) and is backward
  26. * compatible with older versions. Binary data is escaped such that the
  27. * serialized form of a Dictionary is always a valid null-terminated C string.
  28. *
  29. * Keys are restricted: no binary data, no CR/LF, and no equals (=). If a key
  30. * contains these characters it may not be retrievable. This is not checked.
  31. *
  32. * Lookup is via linear search and will be slow with a lot of keys. It's
  33. * designed for small things.
  34. *
  35. * There is code to test and fuzz this in selftest.cpp. Fuzzing a blob of
  36. * pointer tricks like this is important after any modifications.
  37. *
  38. * This is used for network configurations and for saving some things on disk
  39. * in the ZeroTier One service code.
  40. *
  41. * @tparam C Dictionary max capacity in bytes
  42. */
  43. template <unsigned int C> class Dictionary {
  44. public:
  45. Dictionary()
  46. {
  47. memset(_d, 0, sizeof(_d));
  48. }
  49. Dictionary(const char* s)
  50. {
  51. this->load(s);
  52. }
  53. Dictionary(const char* s, unsigned int len)
  54. {
  55. for (unsigned int i = 0; i < C; ++i) {
  56. if ((s) && (i < len)) {
  57. if (! (_d[i] = *s)) {
  58. s = (const char*)0;
  59. }
  60. else {
  61. ++s;
  62. }
  63. }
  64. else {
  65. _d[i] = (char)0;
  66. }
  67. }
  68. _d[C - 1] = (char)0;
  69. }
  70. Dictionary(const Dictionary& d)
  71. {
  72. memcpy(_d, d._d, C);
  73. }
  74. inline Dictionary& operator=(const Dictionary& d)
  75. {
  76. memcpy(_d, d._d, C);
  77. return *this;
  78. }
  79. inline operator bool() const
  80. {
  81. return (_d[0] != 0);
  82. }
  83. /**
  84. * Load a dictionary from a C-string
  85. *
  86. * @param s Dictionary in string form
  87. * @return False if 's' was longer than our capacity
  88. */
  89. inline bool load(const char* s)
  90. {
  91. for (unsigned int i = 0; i < C; ++i) {
  92. if (s) {
  93. if (! (_d[i] = *s)) {
  94. s = (const char*)0;
  95. }
  96. else {
  97. ++s;
  98. }
  99. }
  100. else {
  101. _d[i] = (char)0;
  102. }
  103. }
  104. _d[C - 1] = (char)0;
  105. return (! s);
  106. }
  107. /**
  108. * Delete all entries
  109. */
  110. inline void clear()
  111. {
  112. memset(_d, 0, sizeof(_d));
  113. }
  114. /**
  115. * @return Size of dictionary in bytes not including terminating NULL
  116. */
  117. inline unsigned int sizeBytes() const
  118. {
  119. for (unsigned int i = 0; i < C; ++i) {
  120. if (! _d[i]) {
  121. return i;
  122. }
  123. }
  124. return C - 1;
  125. }
  126. /**
  127. * Get an entry
  128. *
  129. * Note that to get binary values, dest[] should be at least one more than
  130. * the maximum size of the value being retrieved. That's because even if
  131. * the data is binary a terminating 0 is still appended to dest[] after it.
  132. *
  133. * If the key is not found, dest[0] is set to 0 to make dest[] an empty
  134. * C string in that case. The dest[] array will *never* be unterminated
  135. * after this call.
  136. *
  137. * Security note: if 'key' is ever directly based on anything that is not
  138. * a hard-code or internally-generated name, it must be checked to ensure
  139. * that the buffer is NULL-terminated since key[] does not take a secondary
  140. * size parameter. In NetworkConfig all keys are hard-coded strings so this
  141. * isn't a problem in the core.
  142. *
  143. * @param key Key to look up
  144. * @param dest Destination buffer
  145. * @param destlen Size of destination buffer
  146. * @return -1 if not found, or actual number of bytes stored in dest[] minus trailing 0
  147. */
  148. inline int get(const char* key, char* dest, unsigned int destlen) const
  149. {
  150. const char* p = _d;
  151. const char* const eof = p + C;
  152. const char* k;
  153. bool esc;
  154. int j;
  155. if (! destlen) { // sanity check
  156. return -1;
  157. }
  158. while (*p) {
  159. k = key;
  160. while ((*k) && (*p)) {
  161. if (*p != *k) {
  162. break;
  163. }
  164. ++k;
  165. if (++p == eof) {
  166. dest[0] = (char)0;
  167. return -1;
  168. }
  169. }
  170. if ((! *k) && (*p == '=')) {
  171. j = 0;
  172. esc = false;
  173. ++p;
  174. while ((*p != 0) && (*p != 13) && (*p != 10)) {
  175. if (esc) {
  176. esc = false;
  177. switch (*p) {
  178. case 'r':
  179. dest[j++] = 13;
  180. break;
  181. case 'n':
  182. dest[j++] = 10;
  183. break;
  184. case '0':
  185. dest[j++] = (char)0;
  186. break;
  187. case 'e':
  188. dest[j++] = '=';
  189. break;
  190. default:
  191. dest[j++] = *p;
  192. break;
  193. }
  194. if (j == (int)destlen) {
  195. dest[j - 1] = (char)0;
  196. return j - 1;
  197. }
  198. }
  199. else if (*p == '\\') {
  200. esc = true;
  201. }
  202. else {
  203. dest[j++] = *p;
  204. if (j == (int)destlen) {
  205. dest[j - 1] = (char)0;
  206. return j - 1;
  207. }
  208. }
  209. if (++p == eof) {
  210. dest[0] = (char)0;
  211. return -1;
  212. }
  213. }
  214. dest[j] = (char)0;
  215. return j;
  216. }
  217. else {
  218. while ((*p) && (*p != 13) && (*p != 10)) {
  219. if (++p == eof) {
  220. dest[0] = (char)0;
  221. return -1;
  222. }
  223. }
  224. if (*p) {
  225. if (++p == eof) {
  226. dest[0] = (char)0;
  227. return -1;
  228. }
  229. }
  230. else {
  231. break;
  232. }
  233. }
  234. }
  235. dest[0] = (char)0;
  236. return -1;
  237. }
  238. /**
  239. * Get the contents of a key into a buffer
  240. *
  241. * @param key Key to get
  242. * @param dest Destination buffer
  243. * @return True if key was found (if false, dest will be empty)
  244. * @tparam BC Buffer capacity (usually inferred)
  245. */
  246. template <unsigned int BC> inline bool get(const char* key, Buffer<BC>& dest) const
  247. {
  248. const int r = this->get(key, const_cast<char*>(reinterpret_cast<const char*>(dest.data())), BC);
  249. if (r >= 0) {
  250. dest.setSize((unsigned int)r);
  251. return true;
  252. }
  253. else {
  254. dest.clear();
  255. return false;
  256. }
  257. }
  258. /**
  259. * Get a boolean value
  260. *
  261. * @param key Key to look up
  262. * @param dfl Default value if not found in dictionary
  263. * @return Boolean value of key or 'dfl' if not found
  264. */
  265. bool getB(const char* key, bool dfl = false) const
  266. {
  267. char tmp[4];
  268. if (this->get(key, tmp, sizeof(tmp)) >= 0) {
  269. return ((*tmp == '1') || (*tmp == 't') || (*tmp == 'T'));
  270. }
  271. return dfl;
  272. }
  273. /**
  274. * Get an unsigned int64 stored as hex in the dictionary
  275. *
  276. * @param key Key to look up
  277. * @param dfl Default value or 0 if unspecified
  278. * @return Decoded hex UInt value or 'dfl' if not found
  279. */
  280. inline uint64_t getUI(const char* key, uint64_t dfl = 0) const
  281. {
  282. char tmp[128];
  283. if (this->get(key, tmp, sizeof(tmp)) >= 1) {
  284. return Utils::hexStrToU64(tmp);
  285. }
  286. return dfl;
  287. }
  288. /**
  289. * Get an unsigned int64 stored as hex in the dictionary
  290. *
  291. * @param key Key to look up
  292. * @param dfl Default value or 0 if unspecified
  293. * @return Decoded hex UInt value or 'dfl' if not found
  294. */
  295. inline int64_t getI(const char* key, int64_t dfl = 0) const
  296. {
  297. char tmp[128];
  298. if (this->get(key, tmp, sizeof(tmp)) >= 1) {
  299. return Utils::hexStrTo64(tmp);
  300. }
  301. return dfl;
  302. }
  303. /**
  304. * Add a new key=value pair
  305. *
  306. * If the key is already present this will append another, but the first
  307. * will always be returned by get(). This is not checked. If you want to
  308. * ensure a key is not present use erase() first.
  309. *
  310. * Use the vlen parameter to add binary values. Nulls will be escaped.
  311. *
  312. * @param key Key -- nulls, CR/LF, and equals (=) are illegal characters
  313. * @param value Value to set
  314. * @param vlen Length of value in bytes or -1 to treat value[] as a C-string and look for terminating 0
  315. * @return True if there was enough room to add this key=value pair
  316. */
  317. inline bool add(const char* key, const char* value, int vlen = -1)
  318. {
  319. for (unsigned int i = 0; i < C; ++i) {
  320. if (! _d[i]) {
  321. unsigned int j = i;
  322. if (j > 0) {
  323. _d[j++] = (char)10;
  324. if (j == C) {
  325. _d[i] = (char)0;
  326. return false;
  327. }
  328. }
  329. const char* p = key;
  330. while (*p) {
  331. _d[j++] = *(p++);
  332. if (j == C) {
  333. _d[i] = (char)0;
  334. return false;
  335. }
  336. }
  337. _d[j++] = '=';
  338. if (j == C) {
  339. _d[i] = (char)0;
  340. return false;
  341. }
  342. p = value;
  343. int k = 0;
  344. while (((vlen < 0) && (*p)) || (k < vlen)) {
  345. switch (*p) {
  346. case 0:
  347. case 13:
  348. case 10:
  349. case '\\':
  350. case '=':
  351. _d[j++] = '\\';
  352. if (j == C) {
  353. _d[i] = (char)0;
  354. return false;
  355. }
  356. switch (*p) {
  357. case 0:
  358. _d[j++] = '0';
  359. break;
  360. case 13:
  361. _d[j++] = 'r';
  362. break;
  363. case 10:
  364. _d[j++] = 'n';
  365. break;
  366. case '\\':
  367. _d[j++] = '\\';
  368. break;
  369. case '=':
  370. _d[j++] = 'e';
  371. break;
  372. }
  373. if (j == C) {
  374. _d[i] = (char)0;
  375. return false;
  376. }
  377. break;
  378. default:
  379. _d[j++] = *p;
  380. if (j == C) {
  381. _d[i] = (char)0;
  382. return false;
  383. }
  384. break;
  385. }
  386. ++p;
  387. ++k;
  388. }
  389. _d[j] = (char)0;
  390. return true;
  391. }
  392. }
  393. return false;
  394. }
  395. /**
  396. * Add a boolean as a '1' or a '0'
  397. */
  398. inline bool add(const char* key, bool value)
  399. {
  400. return this->add(key, (value) ? "1" : "0", 1);
  401. }
  402. /**
  403. * Add a 64-bit integer (unsigned) as a hex value
  404. */
  405. inline bool add(const char* key, uint64_t value)
  406. {
  407. char tmp[32];
  408. return this->add(key, Utils::hex(value, tmp), -1);
  409. }
  410. /**
  411. * Add a 64-bit integer (unsigned) as a hex value
  412. */
  413. inline bool add(const char* key, int64_t value)
  414. {
  415. char tmp[32];
  416. if (value >= 0) {
  417. return this->add(key, Utils::hex((uint64_t)value, tmp), -1);
  418. }
  419. else {
  420. tmp[0] = '-';
  421. return this->add(key, Utils::hex((uint64_t)(value * -1), tmp + 1), -1);
  422. }
  423. }
  424. /**
  425. * Add a 64-bit integer (unsigned) as a hex value
  426. */
  427. inline bool add(const char* key, const Address& a)
  428. {
  429. char tmp[32];
  430. return this->add(key, Utils::hex(a.toInt(), tmp), -1);
  431. }
  432. /**
  433. * Add a binary buffer's contents as a value
  434. *
  435. * @tparam BC Buffer capacity (usually inferred)
  436. */
  437. template <unsigned int BC> inline bool add(const char* key, const Buffer<BC>& value)
  438. {
  439. return this->add(key, (const char*)value.data(), (int)value.size());
  440. }
  441. /**
  442. * @param key Key to check
  443. * @return True if key is present
  444. */
  445. inline bool contains(const char* key) const
  446. {
  447. char tmp[2];
  448. return (this->get(key, tmp, 2) >= 0);
  449. }
  450. /**
  451. * @return Value of C template parameter
  452. */
  453. inline unsigned int capacity() const
  454. {
  455. return C;
  456. }
  457. inline const char* data() const
  458. {
  459. return _d;
  460. }
  461. inline char* unsafeData()
  462. {
  463. return _d;
  464. }
  465. private:
  466. char _d[C];
  467. };
  468. } // namespace ZeroTier
  469. #endif