-
Notifications
You must be signed in to change notification settings - Fork 403
/
buffer.hpp
1882 lines (1629 loc) · 60.3 KB
/
buffer.hpp
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012- OpenVPN Inc.
//
// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
//
// These templates define the fundamental data buffer classes used by the
// OpenVPN core. Normally OpenVPN uses buffers of unsigned chars, but the
// templatization of the classes would allow buffers of other types to
// be defined.
//
// Fundamentally a buffer is an object with 4 fields:
//
// 1. a pointer to underlying data array
// 2. the capacity of the underlying data array
// 3. an offset into the data array
// 4. the size of the referenced data within the array
//
// The BufferType template is the lowest-level buffer class template. It refers
// to a buffer but without any notion of ownership of the underlying data.
//
// The BufferAllocatedType template is a higher-level template that inherits
// from BufferType but which asserts ownership over the resources of the buffer --
// for example, it will free the underlying buffer in its destructor.
//
// Since most of the time, we want our buffers to be made out of unsigned chars,
// some typedefs at the end of the file define common instantations for the
// BufferType and BufferAllocatedType templates.
//
// Buffer : a simple buffer of unsigned char without ownership semantics
// ConstBuffer : like buffer but where the data pointed to by the buffer is read-only
// BufferAllocated : an allocated Buffer with ownership semantics
// BufferPtr : a smart, reference-counted pointer to a BufferAllocatedRc
#pragma once
#include <string>
#include <cstdlib> // defines std::abort()
#include <cstring>
#include <algorithm>
#include <type_traits> // for std::is_nothrow_move_constructible_v, std::remove_const, std::enable_if_t, and std::is_const_v
#ifndef OPENVPN_NO_IO
#include <openvpn/io/io.hpp>
#endif
#include <openvpn/common/size.hpp>
#include <openvpn/common/exception.hpp>
#include <openvpn/common/rc.hpp>
#include <openvpn/buffer/bufclamp.hpp>
#include <openvpn/common/make_rc.hpp>
#ifdef OPENVPN_BUFFER_ABORT
#define OPENVPN_BUFFER_THROW(exc) \
{ \
std::abort(); \
}
#else
#define OPENVPN_BUFFER_THROW(exc) \
{ \
throw BufferException(BufferException::exc); \
}
#endif
namespace openvpn {
// ===============================================================================================
// special-purpose exception class for Buffer classes
// ===============================================================================================
/**
@brief report various types of exceptions or errors that may occur when working with buffers
@details defines a C++ class called BufferException which is derived from the standard
std::exception class. The purpose of this class is to provide a way to handle and report
various types of exceptions or errors that may occur when working with buffers (data
containers) in the OpenVPN application.
The BufferException class does not take any direct input. Instead, it is designed to be
instantiated and thrown (raised) when certain exceptional conditions arise during buffer
operations. The class defines an enumeration Status that lists various types of buffer-related
exceptions, such as buffer_full, buffer_underflow, buffer_overflow, buffer_offset, and others.
When a BufferException is thrown, it can be caught and handled by the calling code. The output
produced by this class is an error message that describes the specific type of exception that
occurred. This error message can be obtained by calling the what() member function, which
returns a C-style string (const char*) containing the error description.
The class achieves its purpose through the following logic and algorithm:
The BufferException class has two constructors: one that takes only a Status value, and another
that takes both a Status value and an additional error message string. The what() member
function is overridden from the base std::exception class. It returns the error message
associated with the exception. If an additional error message string was provided during
construction, the what() function returns a combination of the status string (e.g.,
"buffer_full") and the additional message. If no additional message was provided, the what()
function returns only the status string. The status_string() private static member function
is used to convert the Status enumeration value to a corresponding string representation
(e.g., "buffer_full" for buffer_full).
The important logic flow in this code is the conversion of the Status enumeration value to
a human-readable string representation, which is then used to construct the error message
returned by the what() function. This error message can be used by the calling code to
understand the nature of the exception and take appropriate action.
It's important to note that this code does not handle any data transformations or perform
any complex algorithms. Its sole purpose is to provide a mechanism for reporting and
handling buffer-related exceptions in a consistent and descriptive manner.
*/
class BufferException : public std::exception
{
public:
enum Status
{
buffer_full,
buffer_headroom,
buffer_underflow,
buffer_overflow,
buffer_offset,
buffer_index,
buffer_const_index,
buffer_push_front_headroom,
buffer_no_reset_impl,
buffer_pop_back,
buffer_set_size,
buffer_range,
};
explicit BufferException(Status status)
: status_(status)
{
}
BufferException(Status status, const std::string &msg)
: status_(status),
msg_(std::string(status_string(status)) + " : " + msg)
{
}
const char *what() const noexcept override
{
if (!msg_.empty())
return msg_.c_str();
else
return status_string(status_);
}
Status status() const
{
return status_;
}
virtual ~BufferException() noexcept = default;
private:
static const char *status_string(const Status status)
{
switch (status)
{
case buffer_full:
return "buffer_full";
case buffer_headroom:
return "buffer_headroom";
case buffer_underflow:
return "buffer_underflow";
case buffer_overflow:
return "buffer_overflow";
case buffer_offset:
return "buffer_offset";
case buffer_index:
return "buffer_index";
case buffer_const_index:
return "buffer_const_index";
case buffer_push_front_headroom:
return "buffer_push_front_headroom";
case buffer_no_reset_impl:
return "buffer_no_reset_impl";
case buffer_pop_back:
return "buffer_pop_back";
case buffer_set_size:
return "buffer_set_size";
case buffer_range:
return "buffer_range";
default:
return "buffer_???";
}
}
Status status_;
std::string msg_;
};
// ===============================================================================================
// ===============================================================================================
template <typename T>
class BufferAllocatedType;
template <typename T>
class BufferType;
// ===============================================================================================
// class ConstBufferType
// ===============================================================================================
/**
@brief Immutable buffer with double ended access and adjustable free space at both ends.
@details This template implements a buffer with double ended access and adjustable free space at
both ends. It's generalized for type \p T but is only really fully functional with
various types that can represent strings, as it does have string interoperation helpers.
It is particularly useful for use as a building block in implementing protocols such
as wire protocols for a network.
Data layout:
@verbatim
data_ ->|--------------|------------------ Buffered Data --------------------|--------------|
^-- offset_ ---^ ^ ^
^ ^----- size_ -----------------------------------------^ ^
^ ^
^-- capacity_ ----------------------------------------------------------------------^
@endverbatim
*/
template <typename T>
class ConstBufferType
{
public:
typedef T value_type;
typedef T *type;
typedef const T *const_type;
typedef typename std::remove_const_t<T> NCT;
/**
* @brief Default constructor for ConstBufferType.
*/
ConstBufferType();
/**
* @brief Constructs a ConstBufferType from a void pointer and size.
* @param data Pointer to the data.
* @param size Size of the data in bytes.
* @param filled Indicates whether the buffer is filled or not.
* @note The term filled indicates that the memory pointed to contains desired data and
* that the size of the buffer should be set equal to its capacity, as opposed to
* this being a buffer of available memory with capacity 'size' and size zero. See
* layout diagram for more insight.
*/
ConstBufferType(void *data, const size_t size, const bool filled);
/**
* @brief Constructs a ConstBufferType from a const void pointer and size.
* This constructor is disabled when T is already const.
* @param data Pointer to the const data.
* @param size Size of the data in bytes.
* @param filled Indicates whether the buffer is filled or not.
*/
template <typename U = T,
typename std::enable_if_t<!std::is_const_v<U>, int> = 0>
ConstBufferType(const void *data, const size_t size, const bool filled);
/**
* @brief Constructs a ConstBufferType from a pointer to T and size.
* @param data Pointer to the data.
* @param size Size of the data in bytes.
* @param filled Indicates whether the buffer is filled or not.
*/
ConstBufferType(T *data, const size_t size, const bool filled);
/**
* @brief Constructs a ConstBufferType from a const pointer to T and size.
* This constructor is disabled when T is already const.
* @param data Pointer to the const data.
* @param size Size of the data in bytes.
* @param filled Indicates whether the buffer is filled or not.
*/
template <typename U = T,
typename std::enable_if_t<!std::is_const_v<U>, int> = 0>
ConstBufferType(const U *data, const size_t size, const bool filled);
/**
* @brief Needed because this class has virtual member functions and is
* intended as a base class.
*/
virtual ~ConstBufferType() = default;
/**
* @brief Const indexing operator for ConstBufferType.
* @param index Index of the element to access.
* @return Const reference to the element at the specified index.
*/
const auto &operator[](const size_t index) const;
/**
* @brief Non-const indexing operator for ConstBufferType.
* @param index Index of the element to access.
* @return Non-const reference to the element at the specified index.
*/
auto &operator[](const size_t index);
/**
* @brief Initializes the headroom (offset) of the buffer.
* @param headroom The desired headroom value.
*/
void init_headroom(const size_t headroom);
/**
* @brief Resets the offset of the buffer.
* @param offset The new offset value.
*/
void reset_offset(const size_t offset);
/**
* @brief Resets the size of the buffer to zero.
*/
void reset_size();
/**
* @brief Resets the content of the buffer.
*/
void reset_content();
/**
* @brief Returns a const pointer to the null-terminated string representation of the buffer.
* @return Const pointer to the null-terminated string.
*/
const T *c_str() const;
/**
* @brief Returns the length of the buffer.
* @return The length of the buffer in elements.
*/
size_t length() const;
/**
* @brief Returns a const pointer to the start of the buffer.
* @return Const pointer to the start of the buffer.
*/
const T *c_data() const;
/**
* @brief Returns a const pointer to the end of the buffer.
* @return Const pointer to the end of the buffer.
*/
const T *c_data_end() const;
/**
* @brief Returns a const pointer to the start of the raw data in the buffer.
* @return Const pointer to the start of the raw data.
*/
const T *c_data_raw() const;
/**
* @brief Returns the capacity (raw size) of the allocated buffer in T objects.
* @return The capacity of the buffer in T objects.
*/
size_t capacity() const;
/**
* @brief Returns the current offset (headroom) into the buffer.
* @return The offset into the buffer.
*/
size_t offset() const;
/**
* @brief Returns true if the buffer is not empty.
* @return True if the buffer is not empty, false otherwise.
*/
bool defined() const;
/**
* @brief Returns true if the data memory is defined (allocated).
* @return True if the data memory is defined, false otherwise.
*/
bool allocated() const;
/**
* @brief Returns true if the buffer is empty.
* @return True if the buffer is empty, false otherwise.
*/
bool empty() const;
/**
* @brief Returns the size of the buffer in T objects.
* @return The size of the buffer in T objects.
*/
size_t size() const;
/**
* @brief Removes and returns the last element from the buffer.
* @return The last element of the buffer.
*/
T pop_back();
/**
* @brief Removes and returns the first element from the buffer.
* @return The first element of the buffer.
*/
T pop_front();
/**
* @brief Returns the first element of the buffer.
* @return The first element of the buffer.
*/
T front() const;
/**
* @brief Returns the last element of the buffer.
* @return The last element of the buffer.
*/
T back() const;
/**
* @brief Advances the buffer by the specified delta.
* @param delta The amount to advance the buffer.
*/
void advance(const size_t delta);
/**
* @brief Returns true if the buffer contains a null character.
* @return True if the buffer contains a null character, false otherwise.
*/
bool contains_null() const;
/**
* @brief Returns true if the buffer is zeroed (all elements are zero).
* @return True if the buffer is zeroed, false otherwise.
*/
bool is_zeroed() const;
#ifndef OPENVPN_NO_IO
/**
* @brief Return an openvpn_io::const_buffer object used by asio write methods.
* @return A const_buffer object representing the underlying buffer data.
*/
openvpn_io::const_buffer const_buffer() const;
/**
* @brief Return a clamped version of const_buffer().
* @details This function returns a const_buffer object that represents the underlying
* buffer data, but with a size clamped to a certain limit. This can be useful
* when you want to ensure that the buffer size does not exceed a specific
* value.
* @return A const_buffer object representing the underlying buffer data, with
* the size clamped to a certain limit.
*/
openvpn_io::const_buffer const_buffer_clamp() const;
/**
* @brief Return a const_buffer object with a specified size limit.
* @details This function returns a const_buffer object that represents the underlying
* buffer data, but with a size limited to the specified `limit` value. This
* can be useful when you want to ensure that the buffer size does not exceed
* a specific value.
* @param limit The maximum size of the returned const_buffer object.
* @return A const_buffer object representing the underlying buffer data, with
* the size limited to the specified `limit` value.
*/
openvpn_io::const_buffer const_buffer_limit(const size_t limit) const;
#endif
/**
* @brief Read data from the buffer into the specified memory location.
* @param data Pointer to the memory location where the data will be read.
* @param size Number of bytes to read from the buffer.
*/
void read(NCT *data, const size_t size);
/**
* @brief Read data from the buffer into the specified memory location.
* @param data Pointer to the memory location where the data will be read.
* @param size Number of bytes to read from the buffer.
*/
void read(void *data, const size_t size);
/**
* @brief Allocate memory and read data from the buffer into the allocated memory.
* @param size Number of bytes to read from the buffer.
* @return Pointer to the allocated memory containing the read data.
*/
auto *read_alloc(const size_t size);
/**
* @brief Allocate memory and read data from the buffer into the allocated memory.
* @param size Number of bytes to read from the buffer.
* @return Buffer containing the read data.
*/
auto read_alloc_buf(const size_t size);
/**
* @brief Return the maximum allowable size value in T objects given the current offset (without considering resize).
* @return The maximum allowable size value in T objects.
*/
size_t max_size() const;
/**
* @brief After an external method, operating on the array as a mutable unsigned char buffer, has written data to the array, use this method to set the array length in terms of T objects.
* @param size The new size of the array in terms of T objects.
*/
void set_size(const size_t size);
/**
* @brief Increment the size of the array (usually used in a similar context to set_size such as after mutable_buffer_append).
* @param delta The amount to increment the size by.
*/
void inc_size(const size_t delta);
/**
* @brief Get a range of the buffer as a ConstBufferType object.
* @param offset The starting offset of the range.
* @param len The length of the range.
* @return A ConstBufferType object representing the specified range of the buffer.
*/
ConstBufferType range(size_t offset, size_t len) const;
/**
* @brief Get a const pointer to the element at the specified index in the array.
* @param index The index of the element to retrieve.
* @return A const pointer to the element at the specified index.
*/
const T *c_index(const size_t index) const;
/**
* @brief Equality operator to compare this buffer with another ConstBufferType object.
* @param other The ConstBufferType object to compare with.
* @return true if the buffers are equal, false otherwise.
*/
bool operator==(const ConstBufferType &other) const;
/**
* @brief Inequality operator to compare this buffer with another ConstBufferType object.
* @param other The ConstBufferType object to compare with.
* @return true if the buffers are not equal, false otherwise.
*/
bool operator!=(const ConstBufferType &other) const;
protected: // mutable implementations are only available to derived classes
/**
* @brief Reserve additional memory for the buffer.
* @param n The amount of additional memory to reserve.
*/
void reserve(const size_t n);
/**
* @brief Get a mutable pointer to the start of the array.
* @return A mutable pointer to the start of the array.
*/
T *data();
/**
* @brief Get a mutable pointer to the end of the array.
* @return A mutable pointer to the end of the array.
*/
T *data_end();
/**
* @brief Get a mutable pointer to the start of the raw data.
* @return A mutable pointer to the start of the raw data.
*/
T *data_raw();
/**
* @brief Return the number of additional T objects that can be added before capacity is reached (without considering resize).
* @param tailroom (Optional) The amount of additional space to reserve at the end of the buffer.
* @return The number of additional T objects that can be added.
*/
size_t remaining(const size_t tailroom = 0) const;
/**
* @brief Return the maximum allowable size value in T objects, taking into account the specified tailroom.
* @param tailroom The amount of additional space to reserve at the end of the buffer.
* @return The maximum allowable size value in T objects, considering the tailroom.
*/
size_t max_size_tailroom(const size_t tailroom) const;
/**
* @brief Append a T object to the end of the array, resizing the array if necessary.
* @param value The T object to append.
*/
void push_back(const T &value);
/**
* @brief Append a T object to the array, with possible resize.
* @param value The T object to be appended to the array.
*/
void push_front(const T &value);
/**
* @brief Place a T object after the last object in the array, with possible resize to contain it.
* However, it doesn't actually change the size of the array to reflect the added object.
* Useful for maintaining null-terminated strings.
* @param value The T object to be placed after the last object in the array.
* @throws Will throw an exception if there is no room for the trailing value and the resize fails.
*/
void set_trailer(const T &value);
/**
* @brief Null-terminate the array.
* @throws Will throw an exception if there is no room, termination is required, and the resize fails.
*/
void null_terminate();
/**
* @brief Get a mutable index into the array.
* @param index The index of the element to be accessed.
* @return A pointer to the element at the specified index.
*/
T *index(const size_t index);
#ifndef OPENVPN_NO_IO
/**
* @brief Return an openvpn_io::mutable_buffer object used by asio read methods, starting from data().
* @param tailroom The amount of tailroom to reserve in the buffer (default: 0).
* @return An openvpn_io::mutable_buffer object.
*/
openvpn_io::mutable_buffer mutable_buffer(const size_t tailroom = 0);
/**
* @brief Return an openvpn_io::mutable_buffer object used by asio read methods, starting from data_end().
* @param tailroom The amount of tailroom to reserve in the buffer (default: 0).
* @return An openvpn_io::mutable_buffer object.
*/
openvpn_io::mutable_buffer mutable_buffer_append(const size_t tailroom = 0);
/**
* @brief Clamped version of mutable_buffer().
* @param tailroom The amount of tailroom to reserve in the buffer (default: 0).
* @return An openvpn_io::mutable_buffer object.
*/
openvpn_io::mutable_buffer mutable_buffer_clamp(const size_t tailroom = 0);
/**
* @brief Clamped version of mutable_buffer_append().
* @param tailroom The amount of tailroom to reserve in the buffer (default: 0).
* @return An openvpn_io::mutable_buffer object.
*/
openvpn_io::mutable_buffer mutable_buffer_append_clamp(const size_t tailroom = 0);
#endif
/**
* @brief Realign the buffer with the specified headroom.
* @param headroom The amount of headroom to reserve in the buffer.
* @note This is useful for aligning structures within the buffer or for adjusting the
* headroom in the buffer. It does one by adjusting the other.
*/
void realign(size_t headroom);
/**
* @brief Write data to the buffer.
* @param data A pointer to the data to be written.
* @param size The number of T objects to be written.
*/
void write(const T *data, const size_t size);
/**
* @brief Write data to the buffer.
* @param data A pointer to the data to be written.
* @param size The number of bytes to be written.
*/
void write(const void *data, const size_t size);
/**
* @brief Prepend data to the buffer.
* @param data A pointer to the data to be prepended.
* @param size The number of T objects to be prepended.
*/
void prepend(const T *data, const size_t size);
/**
* @brief Prepend data to the buffer.
* @param data A pointer to the data to be prepended.
* @param size The number of bytes to be prepended.
*/
void prepend(const void *data, const size_t size);
/**
* @brief Allocate space for writing data to the buffer.
* @param size The number of T objects to allocate space for.
* @return A pointer to the allocated space in the buffer.
*/
T *write_alloc(const size_t size);
/**
* @brief Allocate space for prepending data to the buffer.
* @param size The number of T objects to allocate space for.
* @return A pointer to the allocated space in the buffer.
*/
T *prepend_alloc(const size_t size);
/**
* @brief Reset the buffer with the specified minimum capacity and flags.
* @param min_capacity The minimum capacity of the buffer.
* @param flags Flags to control the behavior of the reset operation.
*/
void reset(const size_t min_capacity, const unsigned int flags);
/**
* @brief Reset the buffer with the specified headroom, minimum capacity, and flags.
* @param headroom The amount of headroom to reserve in the buffer.
* @param min_capacity The minimum capacity of the buffer.
* @param flags Flags to control the behavior of the reset operation.
*/
void reset(const size_t headroom, const size_t min_capacity, const unsigned int flags);
/**
* @brief Append data from another buffer to this buffer.
* @tparam B The type of the other buffer.
* @param other The other buffer to be appended.
*/
template <typename B>
void append(const B &other);
/**
* @brief Swap the contents of this buffer with another buffer.
* @tparam T_ The type of the other buffer.
* @param other The other buffer to swap with.
*/
template <typename T_>
void swap(ConstBufferType<T_> &other);
/**
* @brief Throw an exception when the buffer is full.
* @param newcap The new capacity required for the buffer.
* @param allocated A flag indicating whether memory was allocated.
*/
void buffer_full_error(const size_t newcap, const bool allocated) const;
/**
* @brief Called when the reset method needs to expand the buffer size.
* @param min_capacity The minimum capacity required for the buffer.
* @param flags Flags to control the behavior of the reset operation.
*/
virtual void reset_impl(const size_t min_capacity, const unsigned int flags);
/**
* @brief Derived classes can implement buffer growing semantics by overloading this method.
* In the default implementation, buffers are non-growable, so an exception is thrown.
* @param new_capacity The new capacity required for the buffer.
* @throws std::exception if the buffer cannot be resized.
*/
virtual void resize(const size_t new_capacity);
/**
* @brief Construct a ConstBufferType object.
* @param data A pointer to the data buffer.
* @param offset The offset from data where the T array starts.
* @param size The number of T objects in the array.
* @param capacity The maximum number of T objects that can be stored in the buffer.
*/
ConstBufferType(T *data, const size_t offset, const size_t size, const size_t capacity);
/**
* @brief Construct a ConstBufferType object from a const U* pointer.
* This constructor is disabled when T is already const.
* @tparam U The type of the data pointer.
* @param data A pointer to the data buffer.
* @param offset The offset from data where the T array starts.
* @param size The number of T objects in the array.
* @param capacity The maximum number of T objects that can be stored in the buffer.
*/
template <typename U = T,
typename std::enable_if_t<!std::is_const_v<U>, int> = 0>
ConstBufferType(const U *data, const size_t offset, const size_t size, const size_t capacity);
private:
// Even though *data_ is declared as non-const, within ConstBufferType
// we MUST always treat it as const. But derived classes may treat it
// as non-const as long as they passed in non-const data to begin with.
T *data_; // pointer to data
size_t offset_; // offset from data_ of beginning of T array (to allow for headroom)
size_t size_; // number of T objects in array starting at data_ + offset_
size_t capacity_; // maximum number of array objects of type T for which memory is allocated, starting at data_
};
// ===============================================================================================
// class BufferType
// ===============================================================================================
template <typename T>
class BufferType : public ConstBufferType<T>
{
private:
template <typename>
friend class ConstBufferType;
public:
using ConstBufferType<T>::empty;
using ConstBufferType<T>::capacity;
using ConstBufferType<T>::offset;
using ConstBufferType<T>::back;
using ConstBufferType<T>::init_headroom;
using ConstBufferType<T>::operator[];
using ConstBufferType<T>::reserve;
using ConstBufferType<T>::data;
using ConstBufferType<T>::data_end;
using ConstBufferType<T>::data_raw;
using ConstBufferType<T>::remaining;
using ConstBufferType<T>::max_size_tailroom;
using ConstBufferType<T>::push_back;
using ConstBufferType<T>::push_front;
using ConstBufferType<T>::set_trailer;
using ConstBufferType<T>::null_terminate;
using ConstBufferType<T>::index;
#ifndef OPENVPN_NO_IO
using ConstBufferType<T>::mutable_buffer;
using ConstBufferType<T>::mutable_buffer_append;
using ConstBufferType<T>::mutable_buffer_clamp;
using ConstBufferType<T>::mutable_buffer_append_clamp;
#endif
using ConstBufferType<T>::realign;
using ConstBufferType<T>::write;
using ConstBufferType<T>::prepend;
using ConstBufferType<T>::write_alloc;
using ConstBufferType<T>::prepend_alloc;
using ConstBufferType<T>::reset;
using ConstBufferType<T>::append;
using ConstBufferType<T>::reset_impl;
using ConstBufferType<T>::resize;
using ConstBufferType<T>::buffer_full_error;
/**
* @brief Default constructor for BufferType.
*/
BufferType() = default;
/**
* @brief Constructor for BufferType that takes a void pointer, size, and a flag indicating if the buffer is filled.
* @param data A void pointer to the buffer data.
* @param size The size of the buffer.
* @param filled A boolean flag indicating if the buffer is filled.
*/
BufferType(void *data, const size_t size, const bool filled)
: ConstBufferType<T>(data, size, filled) {};
/**
* @brief Constructor for BufferType that takes a T pointer, size, and a flag indicating if the buffer is filled.
* @param data A pointer to the buffer data of type T.
* @param size The size of the buffer.
* @param filled A boolean flag indicating if the buffer is filled.
*/
BufferType(T *data, const size_t size, const bool filled)
: ConstBufferType<T>(data, size, filled) {};
/**
* @brief Protected constructor for BufferType that takes a T pointer, offset, size, and capacity.
* @param data A pointer to the buffer data of type T.
* @param offset The offset of the buffer.
* @param size The size of the buffer.
* @param capacity The capacity of the buffer.
*/
protected:
BufferType(T *data, const size_t offset, const size_t size, const size_t capacity)
: ConstBufferType<T>(data, offset, size, capacity) {};
};
// ===============================================================================================
// class BufferAllocatedType
// ===============================================================================================
// Allocation and security for the buffer
struct BufAllocFlags
{
enum
{
CONSTRUCT_ZERO = (1 << 0), ///< if enabled, constructors/init will zero allocated space
DESTRUCT_ZERO = (1 << 1), ///< if enabled, destructor will zero data before deletion
GROW = (1 << 2), ///< if enabled, buffer will grow (otherwise buffer_full exception will be thrown)
ARRAY = (1 << 3), ///< if enabled, use as array
};
};
template <typename T>
class BufferAllocatedType : public BufferType<T>
{
private:
// Friend to all specializations of this template allows access to other.data_
template <typename>
friend class BufferAllocatedType;
public:
using BufferType<T>::init_headroom;
using BufferType<T>::buffer_full_error;
using BufferType<T>::size;
using BufferType<T>::capacity;
using BufferType<T>::offset;
using BufferType<T>::data_raw;
using BufferType<T>::c_data_raw;
using BufferType<T>::data;
using BufferType<T>::c_data;
using BufferType<T>::swap;
/**
* @brief Default constructor.
*/
BufferAllocatedType();
/**
* @brief Constructs a BufferAllocatedType with the specified capacity and flags.
* @param capacity The initial capacity of the buffer.
* @param flags The flags to set for the buffer.
*/
BufferAllocatedType(const size_t capacity, const unsigned int flags);
/**
* @brief Constructs a BufferAllocatedType with the specified data, size, and flags.
* @param data A pointer to the data to be copied into the buffer.
* @param size The size of the data to be copied.
* @param flags The flags to set for the buffer.
*/
BufferAllocatedType(const T *data, const size_t size, const unsigned int flags);
/**
* @brief Copy constructor.
* @param other The BufferAllocatedType object to copy from.
*/
BufferAllocatedType(const BufferAllocatedType &other);
/**
* @brief Constructs a BufferAllocatedType from a BufferType object with the specified flags.
* @tparam T_ The template parameter type of the BufferType object.
* @param other The BufferType object to copy from.
* @param flags The flags to set for the new BufferAllocatedType object.
*/
template <typename T_>
BufferAllocatedType(const BufferType<T_> &other, const unsigned int flags);
/**
* @brief Assignment operator.
* @param other The BufferAllocatedType object to copy from.
*/
void operator=(const BufferAllocatedType &other);
/**
* @brief Initializes the buffer with the specified capacity and flags.
* @param capacity The initial capacity of the buffer.
* @param flags The flags to set for the buffer.
*/
void init(const size_t capacity, const unsigned int flags);
/**
* @brief Initializes the buffer with the specified data, size, and flags.
* @param data A pointer to the data to be copied into the buffer.
* @param size The size of the data to be copied.
* @param flags The flags to set for the buffer.
*/
void init(const T *data, const size_t size, const unsigned int flags);
/**
* @brief Reallocates the buffer to the specified new capacity.
* @param newcap The new capacity for the buffer.
*/
void realloc(const size_t newcap);
/**
@brief Realign the buffer with the specified headroom.
@param headroom The amount of headroom to reserve in the buffer.
@return BufferAllocatedType& A reference to the realigned buffer.
@note May reallocate or throw an exception if the reallocation fails.
@throws if the buffer is full and the reallocation fails.
*/
BufferAllocatedType &realign(const size_t headroom);
/**
* @brief Resets the buffer with the specified minimum capacity and flags.
* @param min_capacity The minimum capacity for the buffer.
* @param flags The flags to set for the buffer.
*/
void reset(const size_t min_capacity, const unsigned int flags);
/**
* @brief Resets the buffer with the specified headroom, minimum capacity, and flags.
* @param headroom The additional capacity to allocate beyond the minimum capacity.
* @param min_capacity The minimum capacity for the buffer.
* @param flags The flags to set for the buffer.
*/
void reset(const size_t headroom, const size_t min_capacity, const unsigned int flags);
/**
* @brief Moves the contents of another BufferAllocatedType object into this object.
* @tparam T_ The template parameter type of the other BufferAllocatedType object.
* @tparam R_ The template parameter type of the other BufferAllocatedType object.
* @param other The other BufferAllocatedType object to move from.
*/
template <typename T_>
void move(BufferAllocatedType<T_> &other);
/**
* @brief Swaps the contents of this BufferAllocatedType object with another BufferAllocatedType object.
* @tparam T_ The template parameter type of the other BufferAllocatedType object.
* @tparam R_ The template parameter type of the other BufferAllocatedType object.
* @param other The other BufferAllocatedType object to swap with.
*/
template <typename T_>
void swap(BufferAllocatedType<T_> &other);
/**
* @brief Move constructor.
* @tparam T_ The template parameter type of the other BufferAllocatedType object.