I'm using openssl BIO objects to convert a binary string into a base64 string. The code is as follows:
void ToBase64(std::string & s_in) {
BIO * b_s = BIO_new( BIO_s_mem() );
BIO * b64_f = BIO_new( BIO_f_base64() );
b_s = BIO_push( b64_f , b_s);
std::cout << "IN::" << s_in.length();
BIO_write(b_s, s_in.c_str(), s_in.length());
char * pp;
int sz = BIO_get_mem_data(b_s, &pp);
std::cout << "OUT::" << sz << endl;
s_in.assign(pp,sz);
//std::cout << sz << " " << std::string(pp,sz) << std::endl;
BIO_free (b64_f); // TODO ret error potential
BIO_free (b_s); //
}
The in length is either 64 or 72. However the output is always 65, which is incorrect it should be much larger than that. The documentation isn't the best in the world, AFAIK the bio_s_mem object is supposed to grow dynamically. What am I doing wrong ?
I am probably better off finding a self contained C++ class that doesn't offer streaming support, and supports base64 conversions. Streaming support is not suited to my application. However I just wanted to stick to openSSL since I am allready depending on some of the crypto routines. Anyhow I'll make such a decision after profiling.