dodo  0.0.1
A C++ library to create containerized Linux services
dodo::common::Bytes Class Reference

An array of Octets with size elements. More...

#include <bytes.hpp>

Collaboration diagram for dodo::common::Bytes:

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...
 
BytesdecodeBase64 (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...
 
OctetgetArray () 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...
 
Bytesoperator= (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...
 
Octetarray_
 The Octet array. More...
 
size_t size_
 The array size in Octets (using an int as that is what OpenSSL uses) More...
 

Detailed Description

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.

Definition at line 44 of file bytes.hpp.

Member Enumeration Documentation

◆ MatchType

The way in which two Bytes instances match.

Enumerator
Mismatch 

The local array does not match the other array.

Contained 

The local array is contained in the other array, but the other array has more data.

Contains 

The local array contains the other array, but the local array has more data.

Full 

The local and other array are equal in content and size.

Definition at line 118 of file bytes.hpp.

Constructor & Destructor Documentation

◆ Bytes() [1/3]

dodo::common::Bytes::Bytes ( )
inline

Construct an empty Bytes.

Definition at line 51 of file bytes.hpp.

◆ Bytes() [2/3]

dodo::common::Bytes::Bytes ( Octet data,
size_t  size 
)
inline

Construct an Bytes by taking ownership of existing data (which will be freed when this object is destructed).

Parameters
dataThe data.
sizeThe size of the data.

Definition at line 58 of file bytes.hpp.

References array_.

◆ Bytes() [3/3]

dodo::common::Bytes::Bytes ( const std::string &  s)
inline

Construct and fill from a std::string, not including the NULL terminator (so size will be string.length() ).

Parameters
sThe source string.

Definition at line 67 of file bytes.hpp.

◆ ~Bytes()

virtual dodo::common::Bytes::~Bytes ( )
inlinevirtual

Destruct and clean.

Definition at line 74 of file bytes.hpp.

References free().

Here is the call graph for this function:

Member Function Documentation

◆ append() [1/4]

void dodo::common::Bytes::append ( const Bytes src)

◆ append() [2/4]

void dodo::common::Bytes::append ( const Bytes src,
size_t  n 
)

Append the first n Octets from Bytes.

Parameters
srcThe Bytes to append from.
nThe number of octets to append.

Definition at line 71 of file bytes.cpp.

References array_, reserve(), size_, and throw_Exception.

Here is the call graph for this function:

◆ append() [3/4]

void dodo::common::Bytes::append ( const Octet src,
size_t  n 
)

Append from arbitrary memory areas.

Parameters
srcThe memory to append from.
nThe number of octets to append.

Definition at line 78 of file bytes.cpp.

References array_, reserve(), and size_.

Here is the call graph for this function:

◆ append() [4/4]

void dodo::common::Bytes::append ( Octet  src)

Append a single Octet.

Parameters
srcThe Octet to append.

Definition at line 84 of file bytes.cpp.

References array_, reserve(), and size_.

Here is the call graph for this function:

◆ asString()

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.

Returns
the Byte data as a std::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().

Here is the caller graph for this function:

◆ decodeBase64()

Bytes & dodo::common::Bytes::decodeBase64 ( const std::string &  src)

Decodes the base64 string into this Bytes.

Parameters
srcThe base64-encoded source string.
Returns
A reference to this Bytes

Definition at line 115 of file bytes.cpp.

References array_, reserve(), and throw_Exception.

Referenced by dodo::common::DataCrypt::decrypt().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ encodeBase64()

std::string dodo::common::Bytes::encodeBase64 ( ) const

Encodes the this Bytes into a base64-encoded string.

Returns
The base64 encoded data of the Bytes.

Definition at line 137 of file bytes.cpp.

References array_, size_, and throw_Exception.

Referenced by dodo::common::DataCrypt::encrypt().

Here is the caller graph for this function:

◆ free()

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().

Here is the caller graph for this function:

◆ getArray()

Octet* dodo::common::Bytes::getArray ( ) const
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.

Returns
a pointer to the array of Octets.

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().

Here is the caller graph for this function:

◆ getOctet()

Octet dodo::common::Bytes::getOctet ( size_t  index) const
inline

Return the Octet at index.

Only in debug builds the index is asserted to be within range.

Parameters
indexThe index of the Octet
Returns
The Octet.

Definition at line 199 of file bytes.hpp.

References array_, and size_.

Referenced by dodo::network::protocol::stomp::Frame::readCommand().

Here is the caller graph for this function:

◆ getSize()

◆ hexDump()

std::string dodo::common::Bytes::hexDump ( size_t  n) const

hex dump the first n octets of the data.

Parameters
nThe number of octets to dump.
Returns
a string with hex numbers.

Definition at line 184 of file bytes.cpp.

References array_, and size_.

Referenced by dodo::network::TCPConnectionData::readBuffer().

Here is the caller graph for this function:

◆ match()

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).

Parameters
otherThe other Bytes.
indexThe start index into the local array to match from.
octetsThe number of Octets, from the start of the arrays, that match.
Returns
The MatchType.

Definition at line 163 of file bytes.cpp.

References array_, Contained, Contains, Full, Mismatch, and size_.

◆ operator=()

Bytes & dodo::common::Bytes::operator= ( const std::string &  s)

Assign from std::string, not including a NULL terminator (so size will be string.length() ).

Parameters
sThe base64 string to assign from.
Returns
This Bytes.

Definition at line 89 of file bytes.cpp.

References array_, and reserve().

Here is the call graph for this function:

◆ random()

void dodo::common::Bytes::random ( size_t  octets)

Generate a random set of Octets.

Parameters
octetsThe number of random Octets.

Definition at line 107 of file bytes.cpp.

References array_, reserve(), and size_.

Referenced by dodo::common::DataCrypt::encrypt().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reserve()

void dodo::common::Bytes::reserve ( size_t  size)

Reserve memory in the Bytes.

Parameters
sizeThe 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().

Here is the caller graph for this function:

Field Documentation

◆ alloc_block

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().

◆ allocated_size

size_t dodo::common::Bytes::allocated_size
private

The allocated size, always >= size.

Definition at line 215 of file bytes.hpp.

Referenced by free(), and reserve().

◆ array_

Octet* dodo::common::Bytes::array_
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().

◆ size_

size_t dodo::common::Bytes::size_
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().


The documentation for this class was generated from the following files: