Dictionary.cpp 4.0 KB

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