char error[OPENSSL_ERROR_MAX];
BIO* b64 = NULL;
BIO* bio = NULL;
- char* p = NULL;
+ const char* p = NULL;
int r;
// Initialize the base64 encoder
goto ERROR;
}
- // Disable line breaks and a trailing newline
- BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
-
// Initialize a memory buffer
bio = BIO_new(BIO_s_mem());
if (!bio) {
}
// Connect both things
- b64 = BIO_push(b64, bio);
+ bio = BIO_push(b64, bio);
+
+ // Disable line breaks and a trailing newline
+ BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
// Write the input
- r = BIO_write(b64, buffer, length);
+ r = BIO_write(bio, buffer, length);
if (r < 1) {
ERR_error_string_n(ERR_get_error(), error, sizeof(error));
}
// Flush
- BIO_flush(b64);
+ BIO_flush(bio);
// Fetch a pointer to the output and determine its length
- const size_t l = BIO_get_mem_data(b64, &p);
+ const size_t l = BIO_get_mem_data(bio, &p);
// Copy the output to the heap
*output = strndup(p, l);
r = 0;
ERROR:
- if (p)
- free(p);
if (bio)
- BIO_free(bio);
- if (b64)
- BIO_free(b64);
+ BIO_free_all(bio);
return r;
}