dodo  0.0.1
A C++ library to create containerized Linux services
bytes.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the dodo library (https://github.com/jmspit/dodo).
3  * Copyright (c) 2019 Jan-Marten Spit.
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, version 3.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /**
19  * @file bytes.hpp
20  * Defines the dodo::common::Bytes class.
21  */
22 
23 #ifndef common_bytes_hpp
24 #define common_bytes_hpp
25 
26 #include <string>
27 #include <cassert>
28 
29 namespace dodo::common {
30 
31  /**
32  * Octet aka 'byte'.
33  */
34  typedef uint8_t Octet;
35 
36  /**
37  * An array of Octets with size elements. Provides base64 conversion, random data generation, appending of data from
38  * various other sources. Memory management is implicit, resizing allocates at least a chunk.
39  *
40  * Note that Bytes objects are not thread safe, and that in general the pointer returned by getArray() may
41  * be invalidated by subjequent calls to append(), which could possibly realloc memory, invalidating the previously
42  * returned pointer.
43  */
44  class Bytes {
45 
46  public:
47 
48  /**
49  * Construct an empty Bytes.
50  */
51  Bytes() : array_(nullptr), size_(0), allocated_size(0) {}
52 
53  /**
54  * Construct an Bytes by taking ownership of existing data (which will be freed when this object is destructed).
55  * @param data The data.
56  * @param size The size of the data.
57  */
58  Bytes( Octet* data, size_t size ) {
59  array_ = data;
60  size = size;
61  }
62 
63  /**
64  * Construct and fill from a std::string, not including the NULL terminator (so size will be string.length() ).
65  * @param s The source string.
66  */
67  Bytes( const std::string &s ) : array_(nullptr), size_(0), allocated_size(0) {
68  *this = s;
69  }
70 
71  /**
72  * Destruct and clean.
73  */
74  virtual ~Bytes() { this->free(); }
75 
76  /**
77  * Assign from std::string, not including a NULL terminator (so size will be string.length() ).
78  * @param s The base64 string to assign from.
79  * @return This Bytes.
80  */
81  Bytes& operator=( const std::string &s );
82 
83  /**
84  * Decodes the base64 string into this Bytes.
85  * @param src The base64-encoded source string.
86  * @return A reference to this Bytes
87  */
88  Bytes& decodeBase64( const std::string& src );
89 
90  /**
91  * Encodes the this Bytes into a base64-encoded string.
92  * @return The base64 encoded data of the Bytes.
93  */
94  std::string encodeBase64() const;
95 
96  /**
97  * Convert to a std::string. If the Octects contain an intermediate zero,
98  * an common::Exception is thrown, unless the zero is the last Octet. So use this
99  * only when you are sure the Bytes data is also a string.
100  * @return the Byte data as a std::string.
101  */
102  std::string asString() const;
103 
104  /**
105  * Reserve memory in the Bytes.
106  * @param size The size in bytes to allocate .
107  */
108  void reserve( size_t size );
109 
110  /**
111  * Free and clear data.
112  */
113  void free();
114 
115  /**
116  * The way in which two Bytes instances match.
117  */
118  enum class MatchType {
119  Mismatch, /**< The local array does not match the other array. */
120  Contained, /**< The local array is contained in the other array, but the other array has more data. */
121  Contains, /**< The local array contains the other array, but the local array has more data. */
122  Full /**< The local and other array are equal in content and size. */
123  };
124 
125  /**
126  * Match this Bytes from index to the other array (entirely).
127  * @param other The other Bytes.
128  * @param index The start index into the local array to match from.
129  * @param octets The number of Octets, from the start of the arrays, that match.
130  * @return The MatchType.
131  */
132  MatchType match( const Bytes& other, size_t index, size_t &octets );
133 
134  /**
135  * Append another Bytes.
136  * @param src The Bytes to append.
137  */
138  void append( const Bytes& src );
139 
140  /**
141  * Append the first n Octets from Bytes.
142  * @param src The Bytes to append from.
143  * @param n The number of octets to append.
144  */
145  void append( const Bytes& src, size_t n );
146 
147  /**
148  * Append from arbitrary memory areas.
149  * @param src The memory to append from.
150  * @param n The number of octets to append.
151  */
152  void append( const Octet* src, size_t n );
153 
154  /**
155  * Append a single Octet
156  * @param src The Octet to append.
157  */
158  void append( Octet src );
159 
160  /**
161  * Generate a random set of Octets.
162  * @param octets The number of random Octets.
163  */
164  void random( size_t octets );
165 
166  /**
167  * hex dump the first n octets of the data.
168  * @param n The number of octets to dump.
169  * @return a string with hex numbers.
170  */
171  std::string hexDump( size_t n ) const;
172 
173  /**
174  * Always allocate chunks of this size. Any Bytes that has been called with reserve( n > 1 ) will
175  * at least allocate 1 chunk of alloc_block bytes. Avoids frequent calls to realloc when using many append
176  * calls with small amount of data such as append( Octet ).
177  */
178  const size_t alloc_block = 32;
179 
180  /**
181  * Return the array. Note that this pointer may be invalidated by
182  * subsequent calls to append (which implictly calls reserve, which may ralloc the memory block). Avoid using
183  * this method.
184  * @return a pointer to the array of Octets.
185  */
186  Octet* getArray() const { return array_; }
187 
188  /**
189  * Return the array size.
190  * @return the array size.
191  */
192  size_t getSize() const { return size_; }
193 
194  /**
195  * Return the Octet at index. Only in debug builds the index is asserted to be within range.
196  * @param index The index of the Octet
197  * @return The Octet.
198  */
199  Octet getOctet( size_t index ) const { assert( index < size_ ); return array_[index]; }
200 
201  private:
202  /**
203  * The Octet array.
204  */
206 
207  /**
208  * The array size in Octets (using an int as that is what OpenSSL uses)
209  */
210  size_t size_;
211 
212  /**
213  * The allocated size, always >= size.
214  */
216 
217  };
218 
219 }
220 
221 #endif
dodo::common::Bytes::size_
size_t size_
The array size in Octets (using an int as that is what OpenSSL uses)
Definition: bytes.hpp:210
dodo::common::Bytes::encodeBase64
std::string encodeBase64() const
Encodes the this Bytes into a base64-encoded string.
Definition: bytes.cpp:137
dodo::common::Bytes::getArray
Octet * getArray() const
Return the array.
Definition: bytes.hpp:186
dodo::common::Bytes::alloc_block
const size_t alloc_block
Always allocate chunks of this size.
Definition: bytes.hpp:178
dodo::common::Bytes::decodeBase64
Bytes & decodeBase64(const std::string &src)
Decodes the base64 string into this Bytes.
Definition: bytes.cpp:115
dodo::common::Bytes::getOctet
Octet getOctet(size_t index) const
Return the Octet at index.
Definition: bytes.hpp:199
dodo::common::Bytes::MatchType
MatchType
The way in which two Bytes instances match.
Definition: bytes.hpp:118
dodo::common::Bytes::free
void free()
Free and clear data.
Definition: bytes.cpp:56
dodo::common::Bytes::reserve
void reserve(size_t size)
Reserve memory in the Bytes.
Definition: bytes.cpp:33
dodo::common::Bytes::Bytes
Bytes(Octet *data, size_t size)
Construct an Bytes by taking ownership of existing data (which will be freed when this object is dest...
Definition: bytes.hpp:58
dodo::common::Bytes
An array of Octets with size elements.
Definition: bytes.hpp:44
dodo::common::Bytes::Bytes
Bytes(const std::string &s)
Construct and fill from a std::string, not including the NULL terminator (so size will be string....
Definition: bytes.hpp:67
dodo::common::Bytes::append
void append(const Bytes &src)
Append another Bytes.
Definition: bytes.cpp:65
dodo::common::Bytes::random
void random(size_t octets)
Generate a random set of Octets.
Definition: bytes.cpp:107
dodo::common::Bytes::MatchType::Mismatch
@ Mismatch
The local array does not match the other array.
dodo::common::Bytes::array_
Octet * array_
The Octet array.
Definition: bytes.hpp:205
dodo::common::Bytes::MatchType::Contains
@ Contains
The local array contains the other array, but the local array has more data.
dodo::common
Common and utility interfaces.
Definition: application.hpp:29
dodo::common::Bytes::asString
std::string asString() const
Convert to a std::string.
Definition: bytes.cpp:98
dodo::common::Octet
uint8_t Octet
Octet aka 'byte'.
Definition: bytes.hpp:34
dodo::common::Bytes::match
MatchType match(const Bytes &other, size_t index, size_t &octets)
Match this Bytes from index to the other array (entirely).
Definition: bytes.cpp:163
dodo::common::Bytes::MatchType::Full
@ Full
The local and other array are equal in content and size.
dodo::common::Bytes::hexDump
std::string hexDump(size_t n) const
hex dump the first n octets of the data.
Definition: bytes.cpp:184
dodo::common::Bytes::MatchType::Contained
@ Contained
The local array is contained in the other array, but the other array has more data.
dodo::common::Bytes::operator=
Bytes & operator=(const std::string &s)
Assign from std::string, not including a NULL terminator (so size will be string.length() ).
Definition: bytes.cpp:89
dodo::common::Bytes::getSize
size_t getSize() const
Return the array size.
Definition: bytes.hpp:192
dodo::common::Bytes::Bytes
Bytes()
Construct an empty Bytes.
Definition: bytes.hpp:51
dodo::common::Bytes::allocated_size
size_t allocated_size
The allocated size, always >= size.
Definition: bytes.hpp:215
dodo::common::Bytes::~Bytes
virtual ~Bytes()
Destruct and clean.
Definition: bytes.hpp:74