I have this buffer:
unsigned char *buffer; int buffer_length;
This is how I now convert it to a base64 encoded buffer:
BIO *mem = BIO_new(BIO_s_mem()); BIO *b64 = BIO_new(BIO_f_base64()); mem = BIO_push(b64, mem); int write_length = BIO_write(mem, buffer, buffer_length); if (write_length != buffer_length) //* return -1; int flush_result = BIO_flush(mem); if (flush_result != 1) return -1; unsigned char *result; //** int result_length = BIO_get_mem_data(mem, &result); //use the base64-encoded result to do whatever I need to do BIO_free_all(mem); return 0;
While this is working. However, is this good and reliable code? I have special questions about code snippets marked with asterisks above:
- (
//* ) Is it right to assume that BIO_write() always writes the entire base64 encoded string at once or do I need to create a loop here? - (
//** ) Is it correct to have an unsigned char* type, or should I use char * instead?
source share