Dictionary.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. Dictionary::~Dictionary()
  18. {}
  19. Vector< uint8_t > &Dictionary::operator[](const char *k)
  20. {
  21. if (k)
  22. return m_entries[s_key(k)];
  23. else return m_entries[""];
  24. }
  25. const Vector< uint8_t > &Dictionary::operator[](const char *k) const
  26. {
  27. static const Vector< uint8_t > s_emptyEntry;
  28. if (k) {
  29. SortedMap< String, Vector< uint8_t > >::const_iterator e(m_entries.find(s_key(k)));
  30. return (e == m_entries.end()) ? s_emptyEntry : e->second;
  31. } else {
  32. SortedMap< String, Vector< uint8_t > >::const_iterator e(m_entries.find(""));
  33. return (e == m_entries.end()) ? s_emptyEntry : e->second;
  34. }
  35. }
  36. void Dictionary::add(const char *k, bool v)
  37. {
  38. Vector< uint8_t > &e = (*this)[k];
  39. e.resize(2);
  40. e[0] = (uint8_t)(v ? '1' : '0');
  41. e[1] = 0;
  42. }
  43. void Dictionary::add(const char *k, const Address &v)
  44. {
  45. Vector< uint8_t > &e = (*this)[k];
  46. e.resize(ZT_ADDRESS_STRING_SIZE_MAX);
  47. v.toString((char *)e.data());
  48. }
  49. void Dictionary::add(const char *k, const char *v)
  50. {
  51. if ((v) && (*v)) {
  52. Vector< uint8_t > &e = (*this)[k];
  53. e.clear();
  54. while (*v)
  55. e.push_back((uint8_t)*(v++));
  56. }
  57. }
  58. void Dictionary::add(const char *k, const void *data, unsigned int len)
  59. {
  60. Vector< uint8_t > &e = (*this)[k];
  61. if (len != 0) {
  62. e.assign((const uint8_t *)data, (const uint8_t *)data + len);
  63. } else {
  64. e.clear();
  65. }
  66. }
  67. bool Dictionary::getB(const char *k, bool dfl) const
  68. {
  69. const Vector< uint8_t > &e = (*this)[k];
  70. if (!e.empty()) {
  71. switch ((char)e[0]) {
  72. case '1':
  73. case 't':
  74. case 'T':
  75. case 'y':
  76. case 'Y':
  77. return true;
  78. default:
  79. return false;
  80. }
  81. }
  82. return dfl;
  83. }
  84. uint64_t Dictionary::getUI(const char *k, uint64_t dfl) const
  85. {
  86. uint8_t tmp[18];
  87. uint64_t v = dfl;
  88. const Vector< uint8_t > &e = (*this)[k];
  89. if (!e.empty()) {
  90. if (e.back() != 0) {
  91. const unsigned long sl = e.size();
  92. Utils::copy(tmp, e.data(), (sl > 17) ? 17 : sl);
  93. tmp[17] = 0;
  94. return Utils::unhex((const char *)tmp);
  95. }
  96. return Utils::unhex((const char *)e.data());
  97. }
  98. return v;
  99. }
  100. char *Dictionary::getS(const char *k, char *v, const unsigned int cap) const
  101. {
  102. if (cap == 0) // sanity check
  103. return v;
  104. const Vector< uint8_t > &e = (*this)[k];
  105. if (e.empty()) {
  106. v[0] = 0;
  107. return v;
  108. }
  109. unsigned int i = 0;
  110. const unsigned int last = cap - 1;
  111. for (;;) {
  112. if ((i >= last) || (i >= (unsigned int)e.size())) {
  113. v[i] = 0;
  114. break;
  115. }
  116. if ((v[i] = (char)e[i]) == 0) {
  117. break;
  118. }
  119. ++i;
  120. }
  121. return v;
  122. }
  123. void Dictionary::clear()
  124. {
  125. m_entries.clear();
  126. }
  127. void Dictionary::encode(Vector< uint8_t > &out) const
  128. {
  129. out.clear();
  130. for (SortedMap< String, Vector< uint8_t > >::const_iterator ti(m_entries.begin()); ti != m_entries.end(); ++ti) {
  131. s_appendKey(out, ti->first.data());
  132. for (Vector< uint8_t >::const_iterator i(ti->second.begin()); i != ti->second.end(); ++i)
  133. s_appendValueByte(out, *i);
  134. out.push_back((uint8_t)'\n');
  135. }
  136. out.push_back(0);
  137. }
  138. bool Dictionary::decode(const void *data, unsigned int len)
  139. {
  140. clear();
  141. String k;
  142. Vector< uint8_t > *v = nullptr;
  143. bool escape = false;
  144. for (unsigned int di = 0; di < len; ++di) {
  145. uint8_t c = reinterpret_cast<const uint8_t *>(data)[di];
  146. if (!c) break;
  147. if (v) {
  148. if (escape) {
  149. escape = false;
  150. switch (c) {
  151. case 48:
  152. v->push_back(0);
  153. break;
  154. case 101:
  155. v->push_back(61);
  156. break;
  157. case 110:
  158. v->push_back(10);
  159. break;
  160. case 114:
  161. v->push_back(13);
  162. break;
  163. default:
  164. v->push_back(c);
  165. break;
  166. }
  167. } else {
  168. if (c == (uint8_t)'\n') {
  169. k.clear();
  170. v = nullptr;
  171. } else if (c == 92) { // backslash
  172. escape = true;
  173. } else {
  174. v->push_back(c);
  175. }
  176. }
  177. } else {
  178. if ((c < 33) || (c > 126) || (c == 92)) {
  179. return false;
  180. } else if (c == (uint8_t)'=') {
  181. k.push_back(0);
  182. v = &m_entries[k];
  183. } else if (k.size() < 7) {
  184. k.push_back(c);
  185. } else {
  186. return false;
  187. }
  188. }
  189. }
  190. return true;
  191. }
  192. char *Dictionary::arraySubscript(char buf[256],const char *name,const unsigned long sub) noexcept
  193. {
  194. for(unsigned int i=0;i<(256 - 17);++i) {
  195. if ((buf[i] = name[i]) == 0) {
  196. buf[i++] = '#';
  197. Utils::hex(sub, buf + i);
  198. return buf;
  199. }
  200. }
  201. buf[0] = 0;
  202. return buf;
  203. }
  204. } // namespace ZeroTier