加密和序列化stl字符串和其他容器 [英] encrypting and serializing stl string and other containers
问题描述
我有stl容器中的数据(向量)。向量中的每个节点都是一个也包含stl字符串的结构。
结构记录
{
string名称;
string location;
int salary;
}
向量<记录>雇员;
我想序列化员工,但我也想在序列化之前加密它。
我的加密函数如下所示:
Encode * inBfr,const int in_size,char ** outBfr,int& out_size)
stl标准不需要我的结构的内存是连续的,所以我不能只抓住 employees
变量的内存。有没有任何其他智能方式,我可以使用这个编码函数与我的stl基于结构/容器?对我来说,Encode函数在纯char *缓冲区中工作是有好处的,所以我知道什么进入和退出,但stl结构不是,我正在找到一个很好的方式,所以我可以使用stl这个函数。
我也打算使用任何其他stl容器。虽然 std :: vector< T>
中的元素保证连续布局,但这并不真正有帮助:您可以包含填充和更多重要的是,将 std :: string
对象外部的 std :: string
使用小字符串优化,该值可以嵌入在 std :: string
内,但它也将包含不是 std :: string
s value)。因此,你最好的选择是格式化记录并加密格式化的字符串。
格式化是直接的,但我个人地将编码函数封装到一个简单的 std :: streambuf
,以便可以通过过滤流缓冲区进行加密。鉴于您给出的签名,这可能看起来像这样:
class encryptbuf
:public std :: streambuf {
std :: streambuf * d_sbuf;
char d_buffer [1024];
public:
encryptbuf(std :: streambuf * sbuf)
:d_sbuf(sbuf){
this-> setp(this-> d_buffer,this-> d_buffer + sizeof(this-> d_buffer)-1);
}
int overflow(int c){
if(c!= std :: char_traits< char> :: eof()){
* this-> pptr )= std :: char_traits< char> :: to_char_type(c);
this-> pbump(1);
}
return this-> pubsync()? std :: char_traits< char> :: eof():std :: char_traits< char> :: not_eof(c);
}
int sync(){
char * out(0);
int size(0);
Encode(this-> pbase(),this-> pptr() - this-> pbase(),& out,size);
this-> d_sbuf-> sputn(out,size);
delete [] out; // dunno:看来输出缓冲区是分配的,但是如何?
this-> setp(this-> pbase(),this-> epptr());
return this-> d_sbuf-> pubsync();
}
};
int main(){
encryptbuf sbuf(std :: cout.rdbuf());
std :: ostream eout(& sbuf);
eout<< print something encoded to standard output\\\
< std :: flush;
}
现在,为您的记录创建一个输出操作符, $ c> std :: ostream 可用于创建编码的
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 the std::string
's content external to the std::string
object (in case the small string optimization is used, the value may be embedded inside the std::string
but it will also contain a couple of bytes which are not part of the std::string
s 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::streambuf
so that the encryption can be done by a filtering stream buffer. Given the signature you gave, this could look something like this:
class encryptbuf
: public std::streambuf {
std::streambuf* d_sbuf;
char d_buffer[1024];
public:
encryptbuf(std::streambuf* sbuf)
: d_sbuf(sbuf) {
this->setp(this->d_buffer, this->d_buffer + sizeof(this->d_buffer) - 1);
}
int overflow(int c) {
if (c != std::char_traits<char>::eof()) {
*this->pptr() = std::char_traits<char>::to_char_type(c);
this->pbump(1);
}
return this->pubsync()? std::char_traits<char>::eof(): std::char_traits<char>::not_eof(c);
}
int sync() {
char* out(0);
int size(0);
Encode(this->pbase(), this->pptr() - this->pbase(), &out, size);
this->d_sbuf->sputn(out, size);
delete[] out; // dunno: it seems the output buffer is allocated but how?
this->setp(this->pbase(), this->epptr());
return this->d_sbuf->pubsync();
}
};
int main() {
encryptbuf sbuf(std::cout.rdbuf());
std::ostream eout(&sbuf);
eout << "print something encoded to standard output\n" << std::flush;
}
Now, creating an output operator for your records just printing to an std::ostream
can be used to create an encoded
这篇关于加密和序列化stl字符串和其他容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!