Dictionary.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright (c)2013-2020 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: 2024-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. #include "Dictionary.hpp"
  14. namespace ZeroTier {
  15. static uint64_t _toKey(const char *k)
  16. {
  17. uint64_t n = 0;
  18. unsigned int i = 0;
  19. for(;;) {
  20. char c = k[i];
  21. if (!c) break;
  22. reinterpret_cast<uint8_t *>(&n)[i & 7U] ^= (uint8_t)c;
  23. ++i;
  24. }
  25. return n;
  26. }
  27. Dictionary::Dictionary()
  28. {
  29. }
  30. Dictionary::~Dictionary()
  31. {
  32. }
  33. std::vector<uint8_t> &Dictionary::operator[](const char *k)
  34. {
  35. return _t[_toKey(k)];
  36. }
  37. const std::vector<uint8_t> &Dictionary::operator[](const char *k) const
  38. {
  39. static const std::vector<uint8_t> emptyEntry;
  40. const std::vector<uint8_t> *const e = _t.get(_toKey(k));
  41. return (e) ? *e : emptyEntry;
  42. }
  43. void Dictionary::add(const char *k,bool v)
  44. {
  45. std::vector<uint8_t> &e = (*this)[k];
  46. e.resize(2);
  47. e[0] = (uint8_t)(v ? '1' : '0');
  48. e[1] = 0;
  49. }
  50. void Dictionary::add(const char *k,uint16_t v)
  51. {
  52. std::vector<uint8_t> &e = (*this)[k];
  53. e.resize(5);
  54. Utils::hex(v,(char *)e.data());
  55. }
  56. void Dictionary::add(const char *k,uint32_t v)
  57. {
  58. std::vector<uint8_t> &e = (*this)[k];
  59. e.resize(9);
  60. Utils::hex(v,(char *)e.data());
  61. }
  62. void Dictionary::add(const char *k,uint64_t v)
  63. {
  64. std::vector<uint8_t> &e = (*this)[k];
  65. e.resize(17);
  66. Utils::hex(v,(char *)e.data());
  67. }
  68. void Dictionary::add(const char *k,const Address &v)
  69. {
  70. std::vector<uint8_t> &e = (*this)[k];
  71. e.resize(11);
  72. v.toString((char *)e.data());
  73. }
  74. void Dictionary::add(const char *k,const char *v)
  75. {
  76. std::vector<uint8_t> &e = (*this)[k];
  77. e.clear();
  78. if (v) {
  79. for(;;) {
  80. const uint8_t c = (uint8_t)*(v++);
  81. e.push_back(c);
  82. if (!c) break;
  83. }
  84. }
  85. }
  86. void Dictionary::add(const char *k,const void *data,unsigned int len)
  87. {
  88. std::vector<uint8_t> &e = (*this)[k];
  89. if (len != 0) {
  90. e.assign((const uint8_t *)data,(const uint8_t *)data + len);
  91. } else {
  92. e.clear();
  93. }
  94. }
  95. bool Dictionary::getB(const char *k,bool dfl) const
  96. {
  97. bool v = dfl;
  98. const std::vector<uint8_t> &e = (*this)[k];
  99. if (!e.empty()) {
  100. switch ((char)e[0]) {
  101. case '1':
  102. case 't':
  103. case 'T':
  104. case 'y':
  105. case 'Y':
  106. return true;
  107. default:
  108. return false;
  109. }
  110. }
  111. return v;
  112. }
  113. uint64_t Dictionary::getUI(const char *k,uint64_t dfl) const
  114. {
  115. uint8_t tmp[18];
  116. uint64_t v = dfl;
  117. const std::vector<uint8_t> &e = (*this)[k];
  118. if (!e.empty()) {
  119. if (e.back() != 0) {
  120. const unsigned long sl = e.size();
  121. memcpy(tmp,e.data(),(sl > 17) ? 17 : sl);
  122. tmp[17] = 0;
  123. return Utils::unhex((const char *)tmp);
  124. }
  125. return Utils::unhex((const char *)e.data());
  126. }
  127. return v;
  128. }
  129. void Dictionary::getS(const char *k,char *v,unsigned int cap) const
  130. {
  131. if (cap == 0) // sanity check
  132. return;
  133. const std::vector<uint8_t> &e = (*this)[k];
  134. unsigned int i = 0;
  135. const unsigned int last = cap - 1;
  136. for(;;) {
  137. if ((i == last)||(i >= (unsigned int)e.size()))
  138. break;
  139. v[i] = (char)e[i];
  140. ++i;
  141. }
  142. v[i] = 0;
  143. }
  144. void Dictionary::clear()
  145. {
  146. _t.clear();
  147. }
  148. void Dictionary::encode(std::vector<uint8_t> &out) const
  149. {
  150. uint64_t str[2] = { 0,0 }; // second entry causes all strings to be null-terminated even if 8 chars in length
  151. out.clear();
  152. Hashtable< uint64_t,std::vector<uint8_t> >::Iterator ti(const_cast<Dictionary *>(this)->_t);
  153. uint64_t *kk = nullptr;
  154. std::vector<uint8_t> *vv = nullptr;
  155. while (ti.next(kk,vv)) {
  156. str[0] = *kk;
  157. const char *k = (const char *)str;
  158. for(;;) {
  159. char kc = *(k++);
  160. if (!kc) break;
  161. if ((kc >= 33)&&(kc <= 126)&&(kc != 61)&&(kc != 92)) // printable ASCII with no spaces, equals, or backslash
  162. out.push_back((uint8_t)kc);
  163. }
  164. out.push_back(61); // =
  165. for(std::vector<uint8_t>::const_iterator i(vv->begin());i!=vv->end();++i) {
  166. uint8_t c = *i;
  167. switch(c) {
  168. case 0:
  169. out.push_back(92);
  170. out.push_back(48);
  171. break;
  172. case 10:
  173. out.push_back(92);
  174. out.push_back(110);
  175. break;
  176. case 13:
  177. out.push_back(92);
  178. out.push_back(114);
  179. break;
  180. case 61:
  181. out.push_back(92);
  182. out.push_back(101);
  183. break;
  184. case 92:
  185. out.push_back(92);
  186. out.push_back(92);
  187. break;
  188. default:
  189. out.push_back(c);
  190. break;
  191. }
  192. }
  193. out.push_back(10);
  194. }
  195. }
  196. bool Dictionary::decode(const void *data,unsigned int len)
  197. {
  198. clear();
  199. uint64_t k = 0;
  200. unsigned int ki = 0;
  201. std::vector<uint8_t> *v = nullptr;
  202. bool escape = false;
  203. for(unsigned int di=0;di<len;++di) {
  204. uint8_t c = reinterpret_cast<const uint8_t *>(data)[di];
  205. if (!c) break;
  206. if (v) {
  207. if (escape) {
  208. escape = false;
  209. switch(c) {
  210. case 48:
  211. v->push_back(0);
  212. break;
  213. case 101:
  214. v->push_back(61);
  215. break;
  216. case 110:
  217. v->push_back(10);
  218. break;
  219. case 114:
  220. v->push_back(13);
  221. break;
  222. default:
  223. v->push_back(c);
  224. break;
  225. }
  226. } else {
  227. if (c == 10) {
  228. k = 0;
  229. ki = 0;
  230. v = nullptr;
  231. } else if (c == 92) {
  232. escape = true;
  233. } else {
  234. v->push_back(c);
  235. }
  236. }
  237. } else {
  238. if ((c < 33)||(c > 126)||(c == 92)) {
  239. return false;
  240. } else if (c == 61) {
  241. v = &_t[k];
  242. } else {
  243. reinterpret_cast<uint8_t *>(&k)[ki & 7U] ^= c;
  244. }
  245. }
  246. }
  247. return true;
  248. }
  249. } // namespace ZeroTier