I have data in stl containers (vector). Each node in the vector is a structure which also contains stl strings.
struct record
{
string name;
string location;
int salary;
}
vector< record > employees;
I want to serialize employees but I also want to encrypt it before serializing.
my encryption function looks like this:
Encode(const char * inBfr, const int in_size, char ** outBfr, int& out_size )
By searching it looks like the stl standard doesn’t require the memory of my structure to be contiguous so I can’t just grab the memory of employees variable. Is there any other smart way that I can use this encoding function with my stl based structures/container? It is good for me that Encode function works in plain char * buffers so I know exactly what goes in and out but stl structures are not and I am tring to find a nice way so I can use stl with this function.
I am also opening to using any other stl containers if that helps.
Although the element in the
std::vector<T>are guaranteed to be laid out contiguously, this doesn’t really help: the record you have may include padding and, more importantly, will store thestd::string‘s content external to thestd::stringobject (in case the small string optimization is used, the value may be embedded inside thestd::stringbut it will also contain a couple of bytes which are not part of thestd::strings value). Thus, you best option is to format your record and encrypt the formatted string.The formatting is straight forward but personally I would encapsulate the encoding function into a simple
std::streambufso that the encryption can be done by a filtering stream buffer. Given the signature you gave, this could look something like this:Now, creating an output operator for your records just printing to an
std::ostreamcan be used to create an encoded