Dictionary.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. char tmp[32];
  87. getS(k, tmp, sizeof(tmp));
  88. if (tmp[0])
  89. return Utils::unhex(tmp);
  90. return dfl;
  91. }
  92. char *Dictionary::getS(const char *k, char *v, const unsigned int cap) const
  93. {
  94. if (cap == 0) // sanity check
  95. return v;
  96. const Vector< uint8_t > &e = (*this)[k];
  97. if (e.empty()) {
  98. v[0] = 0;
  99. return v;
  100. }
  101. unsigned int i = 0;
  102. const unsigned int last = cap - 1;
  103. for (;;) {
  104. if ((i >= last) || (i >= (unsigned int)e.size())) {
  105. v[i] = 0;
  106. break;
  107. }
  108. if ((v[i] = (char)e[i]) == 0) {
  109. break;
  110. }
  111. ++i;
  112. }
  113. return v;
  114. }
  115. void Dictionary::clear()
  116. {
  117. m_entries.clear();
  118. }
  119. void Dictionary::encode(Vector< uint8_t > &out) const
  120. {
  121. out.clear();
  122. for (SortedMap< String, Vector< uint8_t > >::const_iterator ti(m_entries.begin()); ti != m_entries.end(); ++ti) {
  123. s_appendKey(out, ti->first.data());
  124. for (Vector< uint8_t >::const_iterator i(ti->second.begin()); i != ti->second.end(); ++i)
  125. s_appendValueByte(out, *i);
  126. out.push_back((uint8_t)'\n');
  127. }
  128. out.push_back(0);
  129. }
  130. bool Dictionary::decode(const void *data, unsigned int len)
  131. {
  132. clear();
  133. String k;
  134. Vector< uint8_t > *v = nullptr;
  135. bool escape = false;
  136. for (unsigned int di = 0; di < len; ++di) {
  137. uint8_t c = reinterpret_cast<const uint8_t *>(data)[di];
  138. if (!c) break;
  139. if (v) {
  140. if (escape) {
  141. escape = false;
  142. switch (c) {
  143. case 48:
  144. v->push_back(0);
  145. break;
  146. case 101:
  147. v->push_back(61);
  148. break;
  149. case 110:
  150. v->push_back(10);
  151. break;
  152. case 114:
  153. v->push_back(13);
  154. break;
  155. default:
  156. v->push_back(c);
  157. break;
  158. }
  159. } else {
  160. if (c == (uint8_t)'\n') {
  161. k.clear();
  162. v = nullptr;
  163. } else if (c == 92) { // backslash
  164. escape = true;
  165. } else {
  166. v->push_back(c);
  167. }
  168. }
  169. } else {
  170. if ((c < 33) || (c > 126) || (c == 92)) {
  171. return false;
  172. } else if (c == (uint8_t)'=') {
  173. k.push_back(0);
  174. v = &m_entries[k];
  175. } else {
  176. k.push_back(c);
  177. }
  178. }
  179. }
  180. return true;
  181. }
  182. char *Dictionary::arraySubscript(char buf[256],const char *name,const unsigned long sub) noexcept
  183. {
  184. for(unsigned int i=0;i<(256 - 17);++i) {
  185. if ((buf[i] = name[i]) == 0) {
  186. buf[i++] = '#';
  187. Utils::hex(sub, buf + i);
  188. return buf;
  189. }
  190. }
  191. buf[0] = 0;
  192. return buf;
  193. }
  194. } // namespace ZeroTier