Dictionary.cpp 5.1 KB

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