Dictionary.cpp 4.2 KB

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