dodo
0.0.1
A C++ library to create containerized Linux services
|
Go to the documentation of this file.
25 #include <openssl/evp.h>
26 #include <openssl/rand.h>
35 if ( sz ==
size_ )
return;
42 if ( !sz )
array_ =
nullptr;
48 array_ =
static_cast< Octet*
>( std::malloc( chunkedsz ) );
81 memcpy(
array_ + osize, src, n );
91 for (
size_t i = 0; i < s.length(); i++ ) {
100 for (
size_t i = 0; i <
size_; i++ ) {
102 else if ( i !=
size_ -1 )
throw_Exception(
"Bytes contain an intermediate zero - string convesrion failed" );
108 if ( octets !=
size_ ) {
116 Octet* temp = (
Octet*)std::malloc( src.length() );
119 EVP_ENCODE_CTX* ctx = EVP_ENCODE_CTX_new();
121 EVP_DecodeInit( ctx );
123 int rc = EVP_DecodeUpdate( ctx, temp, &len, (
const unsigned char*)src.c_str(), (
int)src.length() );
126 rc = EVP_DecodeFinal( ctx, temp + len, &len );
129 EVP_ENCODE_CTX_free( ctx );
132 memcpy(
array_, temp, actsize );
138 size_t base64sz =
size_;
139 base64sz = (base64sz / 48 + 1)*65+1;
140 unsigned char* target = (
unsigned char*)std::malloc( base64sz );
142 EVP_ENCODE_CTX* ctx = EVP_ENCODE_CTX_new();
144 EVP_EncodeInit( ctx );
146 int rc = EVP_EncodeUpdate( ctx, target, &len,
array_, (
int)
size_ );
149 EVP_EncodeFinal( ctx, target + actsize, &len );
152 EVP_ENCODE_CTX_free( ctx );
153 std::stringstream ss;
154 for (
int i = 0; i < actsize; i++ ) {
155 char c =
static_cast<unsigned char>( target[i] );
156 if ( c && c !=
'\n' ) ss << c;
164 size_t local_size =
size_ - index;
165 size_t match_size = std::min( local_size, other.
size_ );
168 if ( local_size < other.
size_ ) {
171 }
else if ( local_size > other.
size_ ) {
172 octets = other.
size_;
185 std::stringstream ss;
186 for (
size_t i = 0; i <
size_ && i < n; i++ ) {
187 if ( i != 0 ) ss <<
":";
188 ss << std::setw(2) << std::hex << std::setfill(
'0') << (
unsigned int)
array_[i];
size_t size_
The array size in Octets (using an int as that is what OpenSSL uses)
std::string encodeBase64() const
Encodes the this Bytes into a base64-encoded string.
const size_t alloc_block
Always allocate chunks of this size.
Bytes & decodeBase64(const std::string &src)
Decodes the base64 string into this Bytes.
MatchType
The way in which two Bytes instances match.
void free()
Free and clear data.
void reserve(size_t size)
Reserve memory in the Bytes.
An array of Octets with size elements.
void append(const Bytes &src)
Append another Bytes.
void random(size_t octets)
Generate a random set of Octets.
@ Mismatch
The local array does not match the other array.
#define throw_SystemException(what, errno)
Throws an Exception with errno, passes FILE and LINE to constructor.
#define throw_Exception(what)
Throws an Exception, passes FILE and LINE to constructor.
Octet * array_
The Octet array.
@ Contains
The local array contains the other array, but the local array has more data.
Common and utility interfaces.
std::string asString() const
Convert to a std::string.
uint8_t Octet
Octet aka 'byte'.
MatchType match(const Bytes &other, size_t index, size_t &octets)
Match this Bytes from index to the other array (entirely).
@ Full
The local and other array are equal in content and size.
std::string hexDump(size_t n) const
hex dump the first n octets of the data.
@ Contained
The local array is contained in the other array, but the other array has more data.
Bytes & operator=(const std::string &s)
Assign from std::string, not including a NULL terminator (so size will be string.length() ).
size_t allocated_size
The allocated size, always >= size.