summaryrefslogtreecommitdiff
path: root/node/Buffer.hpp
blob: a4921b845602e9907970f9e68f54c24aae392af5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
 * ZeroTier One - Network Virtualization Everywhere
 * Copyright (C) 2011-2015  ZeroTier, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * --
 *
 * ZeroTier may be used and distributed under the terms of the GPLv3, which
 * are available at: http://www.gnu.org/licenses/gpl-3.0.html
 *
 * If you would like to embed ZeroTier into a commercial application or
 * redistribute it in a modified binary form, please contact ZeroTier Networks
 * LLC. Start here: http://www.zerotier.com/
 */

#ifndef ZT_BUFFER_HPP
#define ZT_BUFFER_HPP

#include <string.h>
#include <stdint.h>

#include <stdexcept>
#include <string>
#include <algorithm>
#include <utility>

#include "Constants.hpp"
#include "Utils.hpp"

#ifdef __GNUC__
#define ZT_VAR_MAY_ALIAS __attribute__((__may_alias__))
#else
#define ZT_VAR_MAY_ALIAS
#endif

namespace ZeroTier {

/**
 * A variable length but statically allocated buffer
 *
 * Bounds-checking is done everywhere, since this is used in security
 * critical code. This supports construction and assignment from buffers
 * of differing capacities, provided the data actually in them fits.
 * It throws std::out_of_range on any boundary violation.
 *
 * The at(), append(), etc. methods encode integers larger than 8-bit in
 * big-endian (network) byte order.
 *
 * @tparam C Total capacity
 */
template<unsigned int C>
class Buffer
{
	// I love me!
	template <unsigned int C2> friend class Buffer;

public:
	// STL container idioms
	typedef unsigned char value_type;
	typedef unsigned char * pointer;
	typedef const unsigned char * const_pointer;
	typedef unsigned char & reference;
	typedef const unsigned char & const_reference;
	typedef unsigned char * iterator;
	typedef const unsigned char * const_iterator;
	typedef unsigned int size_type;
	typedef int difference_type;
	typedef std::reverse_iterator<iterator> reverse_iterator;
	typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
	inline iterator begin() { return _b; }
	inline iterator end() { return (_b + _l); }
	inline const_iterator begin() const { return _b; }
	inline const_iterator end() const { return (_b + _l); }
	inline reverse_iterator rbegin() { return reverse_iterator(begin()); }
	inline reverse_iterator rend() { return reverse_iterator(end()); }
	inline const_reverse_iterator rbegin() const { return const_reverse_iterator(begin()); }
	inline const_reverse_iterator rend() const { return const_reverse_iterator(end()); }

	Buffer()
		throw() :
		_l(0)
	{
	}

	Buffer(unsigned int l)
		throw(std::out_of_range)
	{
		if (l > C)
			throw std::out_of_range("Buffer: construct with size larger than capacity");
		_l = l;
	}

	template<unsigned int C2>
	Buffer(const Buffer<C2> &b)
		throw(std::out_of_range)
	{
		*this = b;
	}

	Buffer(const void *b,unsigned int l)
		throw(std::out_of_range)
	{
		copyFrom(b,l);
	}

	Buffer(const std::string &s)
		throw(std::out_of_range)
	{
		copyFrom(s.data(),s.length());
	}

	template<unsigned int C2>
	inline Buffer &operator=(const Buffer<C2> &b)
		throw(std::out_of_range)
	{
		if (b._l > C)
			throw std::out_of_range("Buffer: assignment from buffer larger than capacity");
		memcpy(_b,b._b,_l = b._l);
		return *this;
	}

	inline Buffer &operator=(const std::string &s)
		throw(std::out_of_range)
	{
		copyFrom(s.data(),s.length());
		return *this;
	}

	inline void copyFrom(const void *b,unsigned int l)
		throw(std::out_of_range)
	{
		if (l > C)
			throw std::out_of_range("Buffer: set from C array larger than capacity");
		_l = l;
		memcpy(_b,b,l);
	}

	unsigned char operator[](const unsigned int i) const
		throw(std::out_of_range)
	{
		if (i >= _l)
			throw std::out_of_range("Buffer: [] beyond end of data");
		return (unsigned char)_b[i];
	}

	unsigned char &operator[](const unsigned int i)
		throw(std::out_of_range)
	{
		if (i >= _l)
			throw std::out_of_range("Buffer: [] beyond end of data");
		return ((unsigned char *)_b)[i];
	}

	/**
	 * Get a raw pointer to a field with bounds checking
	 *
	 * This isn't perfectly safe in that the caller could still overflow
	 * the pointer, but its use provides both a sanity check and
	 * documentation / reminder to the calling code to treat the returned
	 * pointer as being of size [l].
	 * 
	 * @param i Index of field in buffer
	 * @param l Length of field in bytes
	 * @return Pointer to field data
	 * @throws std::out_of_range Field extends beyond data size
	 */
	unsigned char *field(unsigned int i,unsigned int l)
		throw(std::out_of_range)
	{
		if ((i + l) > _l)
			throw std::out_of_range("Buffer: field() beyond end of data");
		return (unsigned char *)(_b + i);
	}
	const unsigned char *field(unsigned int i,unsigned int l) const
		throw(std::out_of_range)
	{
		if ((i + l) > _l)
			throw std::out_of_range("Buffer: field() beyond end of data");
		return (const unsigned char *)(_b + i);
	}

	/**
	 * Place a primitive integer value at a given position
	 * 
	 * @param i Index to place value
	 * @param v Value
	 * @tparam T Integer type (e.g. uint16_t, int64_t)
	 */
	template<typename T>
	inline void setAt(unsigned int i,const T v)
		throw(std::out_of_range)
	{
		if ((i + sizeof(T)) > _l)
			throw std::out_of_range("Buffer: setAt() beyond end of data");
		T *const ZT_VAR_MAY_ALIAS p = reinterpret_cast<T *>(_b + i);
		*p = Utils::hton(v);
	}

	/**
	 * Get a primitive integer value at a given position
	 * 
	 * This behaves like set() in reverse.
	 * 
	 * @param i Index to get integer
	 * @tparam T Integer type (e.g. uint16_t, int64_t)
	 * @return Integer value
	 */
	template<typename T>
	inline T at(unsigned int i) const
		throw(std::out_of_range)
	{
		if ((i + sizeof(T)) > _l)
			throw std::out_of_range("Buffer: at() beyond end of data");
		const T *const ZT_VAR_MAY_ALIAS p = reinterpret_cast<const T *>(_b + i);
		return Utils::ntoh(*p);
	}

	/**
	 * Append an integer type to this buffer
	 * 
	 * @param v Value to append
	 * @tparam T Integer type (e.g. uint16_t, int64_t)
	 * @throws std::out_of_range Attempt to append beyond capacity
	 */
	template<typename T>
	inline void append(const T v)
		throw(std::out_of_range)
	{
		if ((_l + sizeof(T)) > C)
			throw std::out_of_range("Buffer: append beyond capacity");
		T *const ZT_VAR_MAY_ALIAS p = reinterpret_cast<T *>(_b + _l);
		*p = Utils::hton(v);
		_l += sizeof(T);
	}

	/**
	 * Append a run of bytes
	 *
	 * @param c Character value to append
	 * @param n Number of times to append
	 * @throws std::out_of_range Attempt to append beyond capacity
	 */
	inline void append(unsigned char c,unsigned int n)
		throw(std::out_of_range)
	{
		if ((_l + n) > C)
			throw std::out_of_range("Buffer: append beyond capacity");
		for(unsigned int i=0;i<n;++i)
			_b[_l++] = (char)c;
	}

	/**
	 * Append a C-array of bytes
	 * 
	 * @param b Data
	 * @param l Length
	 * @throws std::out_of_range Attempt to append beyond capacity
	 */
	inline void append(const void *b,unsigned int l)
		throw(std::out_of_range)
	{
		if ((_l + l) > C)
			throw std::out_of_range("Buffer: append beyond capacity");
		memcpy(_b + _l,b,l);
		_l += l;
	}

	/**
	 * Append a string
	 *
	 * @param s String to append
	 * @throws std::out_of_range Attempt to append beyond capacity
	 */
	inline void append(const std::string &s)
		throw(std::out_of_range)
	{
		append(s.data(),(unsigned int)s.length());
	}

	/**
	 * Append a buffer
	 *
	 * @param b Buffer to append
	 * @tparam C2 Capacity of second buffer (typically inferred)
	 * @throws std::out_of_range Attempt to append beyond capacity
	 */
	template<unsigned int C2>
	inline void append(const Buffer<C2> &b)
		throw(std::out_of_range)
	{
		append(b._b,b._l);
	}

	/**
	 * Increment size and return pointer to field of specified size
	 *
	 * Nothing is actually written to the memory. This is a shortcut
	 * for addSize() followed by field() to reference the previous
	 * position and the new size.
	 *
	 * @param l Length of field to append
	 * @return Pointer to beginning of appended field of length 'l'
	 */
	inline char *appendField(unsigned int l)
		throw(std::out_of_range)
	{
		if ((_l + l) > C)
			throw std::out_of_range("Buffer: append beyond capacity");
		char *r = _b + _l;
		_l += l;
		return r;
	}

	/**
	 * Increment size by a given number of bytes
	 * 
	 * The contents of new space are undefined.
	 * 
	 * @param i Bytes to increment
	 * @throws std::out_of_range Capacity exceeded
	 */
	inline void addSize(unsigned int i)
		throw(std::out_of_range)
	{
		if ((i + _l) > C)
			throw std::out_of_range("Buffer: setSize to larger than capacity");
		_l += i;
	}

	/**
	 * Set size of data in buffer
	 * 
	 * The contents of new space are undefined.
	 * 
	 * @param i New size
	 * @throws std::out_of_range Size larger than capacity
	 */
	inline void setSize(const unsigned int i)
		throw(std::out_of_range)
	{
		if (i > C)
			throw std::out_of_range("Buffer: setSize to larger than capacity");
		_l = i;
	}

	/**
	 * Move everything after 'at' to the buffer's front and truncate
	 *
	 * @param at Truncate before this position
	 * @throw std::out_of_range Position is beyond size of buffer
	 */
	inline void behead(const unsigned int at)
		throw(std::out_of_range)
	{
		if (!at)
			return;
		if (at > _l)
			throw std::out_of_range("Buffer: behead() beyond capacity");
		::memmove(_b,_b + at,_l -= at);
	}

	/**
	 * Set buffer data length to zero
	 */
	inline void clear()
		throw()
	{
		_l = 0;
	}

	/**
	 * Zero buffer up to size()
	 */
	inline void zero()
		throw()
	{
		memset(_b,0,_l);
	}

	/**
	 * Zero unused capacity area
	 */
	inline void zeroUnused()
		throw()
	{
		memset(_b + _l,0,C - _l);
	}

	/**
	 * Unconditionally and securely zero buffer's underlying memory
	 */
	inline void burn()
		throw()
	{
		Utils::burn(_b,sizeof(_b));
	}

	/**
	 * @return Constant pointer to data in buffer
	 */
	inline const void *data() const throw() { return _b; }

	/**
	 * @return Size of data in buffer
	 */
	inline unsigned int size() const throw() { return _l; }

	/**
	 * @return Capacity of buffer
	 */
	inline unsigned int capacity() const throw() { return C; }

	template<unsigned int C2>
	inline bool operator==(const Buffer<C2> &b) const
		throw()
	{
		return ((_l == b._l)&&(!memcmp(_b,b._b,_l)));
	}
	template<unsigned int C2>
	inline bool operator!=(const Buffer<C2> &b) const
		throw()
	{
		return ((_l != b._l)||(memcmp(_b,b._b,_l)));
	}
	template<unsigned int C2>
	inline bool operator<(const Buffer<C2> &b) const
		throw()
	{
		return (memcmp(_b,b._b,std::min(_l,b._l)) < 0);
	}
	template<unsigned int C2>
	inline bool operator>(const Buffer<C2> &b) const
		throw()
	{
		return (b < *this);
	}
	template<unsigned int C2>
	inline bool operator<=(const Buffer<C2> &b) const
		throw()
	{
		return !(b < *this);
	}
	template<unsigned int C2>
	inline bool operator>=(const Buffer<C2> &b) const
		throw()
	{
		return !(*this < b);
	}

private:
	unsigned int _l;
	char ZT_VAR_MAY_ALIAS _b[C];
};

} // namespace ZeroTier

#endif