Dictionary.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. unsigned int i = 0;
  106. const unsigned int last = cap - 1;
  107. for (;;) {
  108. if ((i == last) || (i >= (unsigned int)e.size()))
  109. break;
  110. v[i] = (char)e[i];
  111. ++i;
  112. }
  113. v[i] = 0;
  114. return v;
  115. }
  116. void Dictionary::clear()
  117. {
  118. m_entries.clear();
  119. }
  120. void Dictionary::encode(Vector< uint8_t > &out) const
  121. {
  122. out.clear();
  123. for (SortedMap< String, Vector< uint8_t > >::const_iterator ti(m_entries.begin()); ti != m_entries.end(); ++ti) {
  124. s_appendKey(out, ti->first.data());
  125. for (Vector< uint8_t >::const_iterator i(ti->second.begin()); i != ti->second.end(); ++i)
  126. s_appendValueByte(out, *i);
  127. out.push_back((uint8_t)'\n');
  128. }
  129. out.push_back(0);
  130. }
  131. bool Dictionary::decode(const void *data, unsigned int len)
  132. {
  133. clear();
  134. String k;
  135. Vector< uint8_t > *v = nullptr;
  136. bool escape = false;
  137. for (unsigned int di = 0; di < len; ++di) {
  138. uint8_t c = reinterpret_cast<const uint8_t *>(data)[di];
  139. if (!c) break;
  140. if (v) {
  141. if (escape) {
  142. escape = false;
  143. switch (c) {
  144. case 48:
  145. v->push_back(0);
  146. break;
  147. case 101:
  148. v->push_back(61);
  149. break;
  150. case 110:
  151. v->push_back(10);
  152. break;
  153. case 114:
  154. v->push_back(13);
  155. break;
  156. default:
  157. v->push_back(c);
  158. break;
  159. }
  160. } else {
  161. if (c == (uint8_t)'\n') {
  162. k.clear();
  163. v = nullptr;
  164. } else if (c == 92) { // backslash
  165. escape = true;
  166. } else {
  167. v->push_back(c);
  168. }
  169. }
  170. } else {
  171. if ((c < 33) || (c > 126) || (c == 92)) {
  172. return false;
  173. } else if (c == (uint8_t)'=') {
  174. k.push_back(0);
  175. v = &m_entries[k];
  176. } else if (k.size() < 7) {
  177. k.push_back(c);
  178. } else {
  179. return false;
  180. }
  181. }
  182. }
  183. return true;
  184. }
  185. } // namespace ZeroTier