Dictionary.cpp 5.1 KB

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