BlobArray.hpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef _ZT_BLOBARRAY_HPP
  28. #define _ZT_BLOBARRAY_HPP
  29. #include <vector>
  30. #include <string>
  31. #include <algorithm>
  32. namespace ZeroTier {
  33. /**
  34. * A vector of binary strings serializable in a packed format
  35. *
  36. * The format uses variable-length integers to indicate the length of each
  37. * field. Each byte of the length has another byte with seven more significant
  38. * bits if its 8th bit is set. Fields can be up to 2^28 in length.
  39. */
  40. class BlobArray : public std::vector<std::string>
  41. {
  42. public:
  43. inline std::string serialize() const
  44. {
  45. std::string r;
  46. for(BlobArray::const_iterator i=begin();i!=end();++i) {
  47. unsigned int flen = (unsigned int)i->length();
  48. do {
  49. unsigned char flenb = (unsigned char)(flen & 0x7f);
  50. flen >>= 7;
  51. flenb |= (flen) ? 0x80 : 0;
  52. r.push_back((char)flenb);
  53. } while (flen);
  54. r.append(*i);
  55. }
  56. return r;
  57. }
  58. /**
  59. * Deserialize, replacing the current contents of this array
  60. *
  61. * @param data Serialized binary data
  62. * @param len Length of serialized data
  63. */
  64. inline void deserialize(const void *data,unsigned int len)
  65. {
  66. clear();
  67. for(unsigned int i=0;i<len;) {
  68. unsigned int flen = 0;
  69. unsigned int chunk = 0;
  70. while (i < len) {
  71. flen |= ((unsigned int)(((const unsigned char *)data)[i] & 0x7f)) << (7 * chunk++);
  72. if (!(((const unsigned char *)data)[i++] & 0x80))
  73. break;
  74. }
  75. flen = std::min(flen,len - i);
  76. push_back(std::string(((const char *)data) + i,flen));
  77. i += flen;
  78. }
  79. }
  80. inline void deserialize(const std::string &data)
  81. {
  82. deserialize(data.data(),(unsigned int)data.length());
  83. }
  84. };
  85. } // namespace ZeroTier
  86. #endif