dodo
0.0.1
A C++ library to create containerized Linux services
|
An array of Octets with size elements. More...
#include <bytes.hpp>
Public Types | |
enum | MatchType { MatchType::Mismatch, MatchType::Contained, MatchType::Contains, MatchType::Full } |
The way in which two Bytes instances match. More... | |
Public Member Functions | |
Bytes () | |
Construct an empty Bytes. More... | |
Bytes (const std::string &s) | |
Construct and fill from a std::string, not including the NULL terminator (so size will be string.length() ). More... | |
Bytes (Octet *data, size_t size) | |
Construct an Bytes by taking ownership of existing data (which will be freed when this object is destructed). More... | |
virtual | ~Bytes () |
Destruct and clean. More... | |
void | append (const Bytes &src) |
Append another Bytes. More... | |
void | append (const Bytes &src, size_t n) |
Append the first n Octets from Bytes. More... | |
void | append (const Octet *src, size_t n) |
Append from arbitrary memory areas. More... | |
void | append (Octet src) |
Append a single Octet. More... | |
std::string | asString () const |
Convert to a std::string. More... | |
Bytes & | decodeBase64 (const std::string &src) |
Decodes the base64 string into this Bytes. More... | |
std::string | encodeBase64 () const |
Encodes the this Bytes into a base64-encoded string. More... | |
void | free () |
Free and clear data. More... | |
Octet * | getArray () const |
Return the array. More... | |
Octet | getOctet (size_t index) const |
Return the Octet at index. More... | |
size_t | getSize () const |
Return the array size. More... | |
std::string | hexDump (size_t n) const |
hex dump the first n octets of the data. More... | |
MatchType | match (const Bytes &other, size_t index, size_t &octets) |
Match this Bytes from index to the other array (entirely). More... | |
Bytes & | operator= (const std::string &s) |
Assign from std::string, not including a NULL terminator (so size will be string.length() ). More... | |
void | random (size_t octets) |
Generate a random set of Octets. More... | |
void | reserve (size_t size) |
Reserve memory in the Bytes. More... | |
Data Fields | |
const size_t | alloc_block = 32 |
Always allocate chunks of this size. More... | |
Private Attributes | |
size_t | allocated_size |
The allocated size, always >= size. More... | |
Octet * | array_ |
The Octet array. More... | |
size_t | size_ |
The array size in Octets (using an int as that is what OpenSSL uses) More... | |
An array of Octets with size elements.
Provides base64 conversion, random data generation, appending of data from various other sources. Memory management is implicit, resizing allocates at least a chunk.
Note that Bytes objects are not thread safe, and that in general the pointer returned by getArray() may be invalidated by subjequent calls to append(), which could possibly realloc memory, invalidating the previously returned pointer.
|
strong |
The way in which two Bytes instances match.
|
inline |
|
inline |
|
inline |
|
inlinevirtual |
void dodo::common::Bytes::append | ( | const Bytes & | src | ) |
Append another Bytes.
src | The Bytes to append. |
Definition at line 65 of file bytes.cpp.
References array_, reserve(), and size_.
Referenced by dodo::common::DataCrypt::decrypt(), dodo::network::protocol::stomp::Connect::generate(), dodo::persist::sqlite::Query::getBytes(), dodo::network::protocol::http::HTTPRequest::parseBody(), dodo::network::protocol::http::HTTPResponse::parseBody(), and dodo::network::protocol::http::HTTPMessage::parseChunkedBody().
void dodo::common::Bytes::append | ( | const Bytes & | src, |
size_t | n | ||
) |
void dodo::common::Bytes::append | ( | const Octet * | src, |
size_t | n | ||
) |
void dodo::common::Bytes::append | ( | Octet | src | ) |
std::string dodo::common::Bytes::asString | ( | ) | const |
Convert to a std::string.
If the Octects contain an intermediate zero, an common::Exception is thrown, unless the zero is the last Octet. So use this only when you are sure the Bytes data is also a string.
Definition at line 98 of file bytes.cpp.
References array_, size_, and throw_Exception.
Referenced by dodo::network::protocol::http::HTTPRequest::asString(), dodo::network::protocol::http::HTTPResponse::asString(), and dodo::network::TLSContext::TLSContext().
Bytes & dodo::common::Bytes::decodeBase64 | ( | const std::string & | src | ) |
Decodes the base64 string into this Bytes.
src | The base64-encoded source string. |
Definition at line 115 of file bytes.cpp.
References array_, reserve(), and throw_Exception.
Referenced by dodo::common::DataCrypt::decrypt().
std::string dodo::common::Bytes::encodeBase64 | ( | ) | const |
Encodes the this Bytes into a base64-encoded string.
Definition at line 137 of file bytes.cpp.
References array_, size_, and throw_Exception.
Referenced by dodo::common::DataCrypt::encrypt().
void dodo::common::Bytes::free | ( | ) |
Free and clear data.
Definition at line 56 of file bytes.cpp.
References allocated_size, array_, and size_.
Referenced by dodo::network::protocol::stomp::Connect::generate(), dodo::persist::sqlite::Query::getBytes(), and ~Bytes().
|
inline |
Return the array.
Note that this pointer may be invalidated by subsequent calls to append (which implictly calls reserve, which may ralloc the memory block). Avoid using this method.
Definition at line 186 of file bytes.hpp.
References array_.
Referenced by dodo::persist::sqlite::DML::bind(), dodo::common::DataCrypt::decrypt(), dodo::common::DataCrypt::encrypt(), dodo::network::TCPConnectionData::readBuffer(), dodo::network::protocol::http::HTTPRequest::send(), and dodo::network::protocol::http::HTTPResponse::send().
|
inline |
Return the Octet at index.
Only in debug builds the index is asserted to be within range.
index | The index of the Octet |
Definition at line 199 of file bytes.hpp.
Referenced by dodo::network::protocol::stomp::Frame::readCommand().
|
inline |
Return the array size.
Definition at line 192 of file bytes.hpp.
References size_.
Referenced by dodo::network::protocol::http::HTTPRequest::asString(), dodo::network::protocol::http::HTTPResponse::asString(), dodo::persist::sqlite::DML::bind(), dodo::common::DataCrypt::decrypt(), dodo::common::DataCrypt::encrypt(), dodo::network::TCPConnectionData::readBuffer(), dodo::network::protocol::stomp::Frame::readCommand(), dodo::network::protocol::http::HTTPRequest::send(), dodo::network::protocol::http::HTTPResponse::send(), and dodo::network::protocol::http::HTTPMessage::setBody().
std::string dodo::common::Bytes::hexDump | ( | size_t | n | ) | const |
hex dump the first n octets of the data.
n | The number of octets to dump. |
Definition at line 184 of file bytes.cpp.
Referenced by dodo::network::TCPConnectionData::readBuffer().
Bytes::MatchType dodo::common::Bytes::match | ( | const Bytes & | other, |
size_t | index, | ||
size_t & | octets | ||
) |
Match this Bytes from index to the other array (entirely).
other | The other Bytes. |
index | The start index into the local array to match from. |
octets | The number of Octets, from the start of the arrays, that match. |
Definition at line 163 of file bytes.cpp.
References array_, Contained, Contains, Full, Mismatch, and size_.
Bytes & dodo::common::Bytes::operator= | ( | const std::string & | s | ) |
void dodo::common::Bytes::random | ( | size_t | octets | ) |
void dodo::common::Bytes::reserve | ( | size_t | size | ) |
Reserve memory in the Bytes.
size | The size in bytes to allocate . |
Definition at line 33 of file bytes.cpp.
References alloc_block, allocated_size, array_, size_, and throw_SystemException.
Referenced by append(), decodeBase64(), dodo::common::DataCrypt::decrypt(), dodo::common::DataCrypt::encrypt(), operator=(), random(), and dodo::network::TCPConnectionData::readBuffer().
const size_t dodo::common::Bytes::alloc_block = 32 |
Always allocate chunks of this size.
Any Bytes that has been called with reserve( n > 1 ) will at least allocate 1 chunk of alloc_block bytes. Avoids frequent calls to realloc when using many append calls with small amount of data such as append( Octet ).
Definition at line 178 of file bytes.hpp.
Referenced by reserve().
|
private |
|
private |
The Octet array.
Definition at line 205 of file bytes.hpp.
Referenced by append(), asString(), Bytes(), decodeBase64(), encodeBase64(), free(), getArray(), getOctet(), hexDump(), match(), operator=(), random(), and reserve().
|
private |
The array size in Octets (using an int as that is what OpenSSL uses)
Definition at line 210 of file bytes.hpp.
Referenced by append(), asString(), encodeBase64(), free(), getOctet(), getSize(), hexDump(), match(), random(), and reserve().