Buffer.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 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_BUFFER_HPP
  28. #define ZT_BUFFER_HPP
  29. #include <string.h>
  30. #include <stdint.h>
  31. #include <stdexcept>
  32. #include <string>
  33. #include <algorithm>
  34. #include <utility>
  35. #include "Constants.hpp"
  36. #include "Utils.hpp"
  37. #ifdef __GNUC__
  38. #define ZT_VAR_MAY_ALIAS __attribute__((__may_alias__))
  39. #else
  40. #define ZT_VAR_MAY_ALIAS
  41. #endif
  42. namespace ZeroTier {
  43. /**
  44. * A variable length but statically allocated buffer
  45. *
  46. * Bounds-checking is done everywhere, since this is used in security
  47. * critical code. This supports construction and assignment from buffers
  48. * of differing capacities, provided the data actually in them fits.
  49. * It throws std::out_of_range on any boundary violation.
  50. *
  51. * The at(), append(), etc. methods encode integers larger than 8-bit in
  52. * big-endian (network) byte order.
  53. *
  54. * @tparam C Total capacity
  55. */
  56. template<unsigned int C>
  57. class Buffer
  58. {
  59. // I love me!
  60. template <unsigned int C2> friend class Buffer;
  61. public:
  62. // STL container idioms
  63. typedef unsigned char value_type;
  64. typedef unsigned char * pointer;
  65. typedef const unsigned char * const_pointer;
  66. typedef unsigned char & reference;
  67. typedef const unsigned char & const_reference;
  68. typedef unsigned char * iterator;
  69. typedef const unsigned char * const_iterator;
  70. typedef unsigned int size_type;
  71. typedef int difference_type;
  72. typedef std::reverse_iterator<iterator> reverse_iterator;
  73. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  74. inline iterator begin() { return _b; }
  75. inline iterator end() { return (_b + _l); }
  76. inline const_iterator begin() const { return _b; }
  77. inline const_iterator end() const { return (_b + _l); }
  78. inline reverse_iterator rbegin() { return reverse_iterator(begin()); }
  79. inline reverse_iterator rend() { return reverse_iterator(end()); }
  80. inline const_reverse_iterator rbegin() const { return const_reverse_iterator(begin()); }
  81. inline const_reverse_iterator rend() const { return const_reverse_iterator(end()); }
  82. Buffer()
  83. throw() :
  84. _l(0)
  85. {
  86. }
  87. Buffer(unsigned int l)
  88. throw(std::out_of_range)
  89. {
  90. if (l > C)
  91. throw std::out_of_range("Buffer: construct with size larger than capacity");
  92. _l = l;
  93. }
  94. template<unsigned int C2>
  95. Buffer(const Buffer<C2> &b)
  96. throw(std::out_of_range)
  97. {
  98. *this = b;
  99. }
  100. Buffer(const void *b,unsigned int l)
  101. throw(std::out_of_range)
  102. {
  103. copyFrom(b,l);
  104. }
  105. Buffer(const std::string &s)
  106. throw(std::out_of_range)
  107. {
  108. copyFrom(s.data(),s.length());
  109. }
  110. template<unsigned int C2>
  111. inline Buffer &operator=(const Buffer<C2> &b)
  112. throw(std::out_of_range)
  113. {
  114. if (b._l > C)
  115. throw std::out_of_range("Buffer: assignment from buffer larger than capacity");
  116. memcpy(_b,b._b,_l = b._l);
  117. return *this;
  118. }
  119. inline Buffer &operator=(const std::string &s)
  120. throw(std::out_of_range)
  121. {
  122. copyFrom(s.data(),s.length());
  123. return *this;
  124. }
  125. inline void copyFrom(const void *b,unsigned int l)
  126. throw(std::out_of_range)
  127. {
  128. if (l > C)
  129. throw std::out_of_range("Buffer: set from C array larger than capacity");
  130. _l = l;
  131. memcpy(_b,b,l);
  132. }
  133. unsigned char operator[](const unsigned int i) const
  134. throw(std::out_of_range)
  135. {
  136. if (i >= _l)
  137. throw std::out_of_range("Buffer: [] beyond end of data");
  138. return (unsigned char)_b[i];
  139. }
  140. unsigned char &operator[](const unsigned int i)
  141. throw(std::out_of_range)
  142. {
  143. if (i >= _l)
  144. throw std::out_of_range("Buffer: [] beyond end of data");
  145. return ((unsigned char *)_b)[i];
  146. }
  147. unsigned char *data() throw() { return (unsigned char *)_b; }
  148. const unsigned char *data() const throw() { return (const unsigned char *)_b; }
  149. /**
  150. * Safe way to get a pointer to a field from data() with bounds checking
  151. *
  152. * @param i Index of field in buffer
  153. * @param l Length of field in bytes
  154. * @return Pointer to field data
  155. * @throws std::out_of_range Field extends beyond data size
  156. */
  157. unsigned char *field(unsigned int i,unsigned int l)
  158. throw(std::out_of_range)
  159. {
  160. if ((i + l) > _l)
  161. throw std::out_of_range("Buffer: field() beyond end of data");
  162. return (unsigned char *)(_b + i);
  163. }
  164. const unsigned char *field(unsigned int i,unsigned int l) const
  165. throw(std::out_of_range)
  166. {
  167. if ((i + l) > _l)
  168. throw std::out_of_range("Buffer: field() beyond end of data");
  169. return (const unsigned char *)(_b + i);
  170. }
  171. /**
  172. * Place a primitive integer value at a given position
  173. *
  174. * @param i Index to place value
  175. * @param v Value
  176. * @tparam T Integer type (e.g. uint16_t, int64_t)
  177. */
  178. template<typename T>
  179. inline void setAt(unsigned int i,const T v)
  180. throw(std::out_of_range)
  181. {
  182. if ((i + sizeof(T)) > _l)
  183. throw std::out_of_range("Buffer: set() beyond end of data");
  184. T *const ZT_VAR_MAY_ALIAS p = reinterpret_cast<T *>(_b + i);
  185. *p = Utils::hton(v);
  186. }
  187. /**
  188. * Get a primitive integer value at a given position
  189. *
  190. * This behaves like set() in reverse.
  191. *
  192. * @param i Index to get integer
  193. * @tparam T Integer type (e.g. uint16_t, int64_t)
  194. * @return Integer value
  195. */
  196. template<typename T>
  197. inline T at(unsigned int i) const
  198. throw(std::out_of_range)
  199. {
  200. if ((i + sizeof(T)) > _l)
  201. throw std::out_of_range("Buffer: at() beyond end of data");
  202. const T *const ZT_VAR_MAY_ALIAS p = reinterpret_cast<const T *>(_b + i);
  203. return Utils::ntoh(*p);
  204. }
  205. /**
  206. * Append an integer type to this buffer
  207. *
  208. * @param v Value to append
  209. * @tparam T Integer type (e.g. uint16_t, int64_t)
  210. * @throws std::out_of_range Attempt to append beyond capacity
  211. */
  212. template<typename T>
  213. inline void append(const T v)
  214. throw(std::out_of_range)
  215. {
  216. if ((_l + sizeof(T)) > C)
  217. throw std::out_of_range("Buffer: append beyond capacity");
  218. T *const ZT_VAR_MAY_ALIAS p = reinterpret_cast<T *>(_b + _l);
  219. *p = Utils::hton(v);
  220. _l += sizeof(T);
  221. }
  222. /**
  223. * Append a run of bytes
  224. *
  225. * @param c Character value to append
  226. * @param n Number of times to append
  227. * @throws std::out_of_range Attempt to append beyond capacity
  228. */
  229. inline void append(unsigned char c,unsigned int n)
  230. throw(std::out_of_range)
  231. {
  232. if ((_l + n) > C)
  233. throw std::out_of_range("Buffer: append beyond capacity");
  234. for(unsigned int i=0;i<n;++i)
  235. _b[_l++] = (char)c;
  236. }
  237. /**
  238. * Append a C-array of bytes
  239. *
  240. * @param b Data
  241. * @param l Length
  242. * @throws std::out_of_range Attempt to append beyond capacity
  243. */
  244. inline void append(const void *b,unsigned int l)
  245. throw(std::out_of_range)
  246. {
  247. if ((_l + l) > C)
  248. throw std::out_of_range("Buffer: append beyond capacity");
  249. memcpy(_b + _l,b,l);
  250. _l += l;
  251. }
  252. /**
  253. * Append a string
  254. *
  255. * @param s String to append
  256. * @throws std::out_of_range Attempt to append beyond capacity
  257. */
  258. inline void append(const std::string &s)
  259. throw(std::out_of_range)
  260. {
  261. append(s.data(),(unsigned int)s.length());
  262. }
  263. /**
  264. * Append a buffer
  265. *
  266. * @param b Buffer to append
  267. * @tparam C2 Capacity of second buffer (typically inferred)
  268. * @throws std::out_of_range Attempt to append beyond capacity
  269. */
  270. template<unsigned int C2>
  271. inline void append(const Buffer<C2> &b)
  272. throw(std::out_of_range)
  273. {
  274. append(b._b,b._l);
  275. }
  276. /**
  277. * Increment size and return pointer to field of specified size
  278. *
  279. * The memory isn't actually written, so this is a shortcut for a multi-step
  280. * process involving getting the current pointer and adding size.
  281. *
  282. * @param l Length of field to append
  283. * @return Pointer to beginning of appended field of length 'l'
  284. */
  285. inline char *appendField(unsigned int l)
  286. throw(std::out_of_range)
  287. {
  288. if ((_l + l) > C)
  289. throw std::out_of_range("Buffer: append beyond capacity");
  290. char *r = _b + _l;
  291. _l += l;
  292. return r;
  293. }
  294. /**
  295. * Increment size by a given number of bytes
  296. *
  297. * The contents of new space are undefined.
  298. *
  299. * @param i Bytes to increment
  300. * @throws std::out_of_range Capacity exceeded
  301. */
  302. inline void addSize(unsigned int i)
  303. throw(std::out_of_range)
  304. {
  305. if ((i + _l) > C)
  306. throw std::out_of_range("Buffer: setSize to larger than capacity");
  307. _l += i;
  308. }
  309. /**
  310. * Set size of data in buffer
  311. *
  312. * The contents of new space are undefined.
  313. *
  314. * @param i New size
  315. * @throws std::out_of_range Size larger than capacity
  316. */
  317. inline void setSize(const unsigned int i)
  318. throw(std::out_of_range)
  319. {
  320. if (i > C)
  321. throw std::out_of_range("Buffer: setSize to larger than capacity");
  322. _l = i;
  323. }
  324. /**
  325. * Set buffer data length to zero
  326. */
  327. inline void clear()
  328. throw()
  329. {
  330. _l = 0;
  331. }
  332. /**
  333. * Zero buffer up to size()
  334. */
  335. inline void zero()
  336. throw()
  337. {
  338. memset(_b,0,_l);
  339. }
  340. /**
  341. * Zero unused capacity area
  342. */
  343. inline void zeroUnused()
  344. throw()
  345. {
  346. memset(_b + _l,0,C - _l);
  347. }
  348. /**
  349. * Unconditionally zero buffer's underlying memory
  350. */
  351. inline void zeroAll()
  352. throw()
  353. {
  354. memset(_b,0,sizeof(_b));
  355. }
  356. /**
  357. * @return Size of data in buffer
  358. */
  359. inline unsigned int size() const throw() { return _l; }
  360. /**
  361. * @return Capacity of buffer
  362. */
  363. inline unsigned int capacity() const throw() { return C; }
  364. template<unsigned int C2>
  365. inline bool operator==(const Buffer<C2> &b) const
  366. throw()
  367. {
  368. return ((_l == b._l)&&(!memcmp(_b,b._b,_l)));
  369. }
  370. template<unsigned int C2>
  371. inline bool operator!=(const Buffer<C2> &b) const
  372. throw()
  373. {
  374. return ((_l != b._l)||(memcmp(_b,b._b,_l)));
  375. }
  376. template<unsigned int C2>
  377. inline bool operator<(const Buffer<C2> &b) const
  378. throw()
  379. {
  380. return (memcmp(_b,b._b,std::min(_l,b._l)) < 0);
  381. }
  382. template<unsigned int C2>
  383. inline bool operator>(const Buffer<C2> &b) const
  384. throw()
  385. {
  386. return (b < *this);
  387. }
  388. template<unsigned int C2>
  389. inline bool operator<=(const Buffer<C2> &b) const
  390. throw()
  391. {
  392. return !(b < *this);
  393. }
  394. template<unsigned int C2>
  395. inline bool operator>=(const Buffer<C2> &b) const
  396. throw()
  397. {
  398. return !(*this < b);
  399. }
  400. private:
  401. unsigned int _l;
  402. char ZT_VAR_MAY_ALIAS _b[C];
  403. };
  404. } // namespace ZeroTier
  405. #endif