I use the code below where I use BIO_read from an in-memory-buffer to do base64 decoding. Sometimes BIO_read returns 0 for the size parameter. I could not yet figure out why that would be happening. Any Ideas?
static std::vector<unsigned char> base64_decode(void *input, int length)
{
std::vector<unsigned char> result(length, 0);
BIO *b64, *bmem;
b64 = BIO_new(BIO_f_base64());
bmem = BIO_new_mem_buf(input, length);
bmem = BIO_push(b64, bmem);
int size = BIO_read(bmem, &result[0], length);
if (size == 0)
fprintf(stderr, "Problem\n");
result.resize(size);
BIO_free_all(bmem);
return result;
}
I had similar issues with base64 decoding until I understood that base64 can be formatted with newlines or not.
You should try to normalize your input by removing newline characters and then setting the OpenSSL
BIO_FLAGS_BASE64_NO_NLflag on your b64 BIO (see the OpenSSL documentation on b64 BIO).