diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp index 01d98f5..fa9edfc 100644 --- a/src/utils/utils.cpp +++ b/src/utils/utils.cpp @@ -36,33 +36,27 @@ import secureAllocator; /// \param encodedData Base64 encoded string. /// \return a vector of the decoded binary data. std::vector base64Decode(const std::string &encodedData) { - // Allocate a buffer for the decoded data (the size of the decoded data is always less than the size of the encoded data) - std::vector decodedData(encodedData.size()); - - // Custom deleter for BIO objects - auto bioDeleter = [](BIO *bio) -> void { BIO_free_all(bio); }; - // Create a BIO object to decode the data - const std::unique_ptr b64(BIO_new(BIO_f_base64()), bioDeleter); + std::unique_ptr bio( + BIO_new_mem_buf(encodedData.data(), static_cast(encodedData.size())), &BIO_free_all); + if (bio == nullptr) + throw std::bad_alloc(); // Memory allocation failed - // Don't use newlines to flush buffer - BIO_set_flags(b64.get(), BIO_FLAGS_BASE64_NO_NL); + // Create a base64 BIO + BIO *b64 = BIO_new(BIO_f_base64()); + if (b64 == nullptr) + throw std::bad_alloc(); // Memory allocation failed - // Create a memory BIO to store the encoded data - std::unique_ptr bio( - BIO_new_mem_buf(encodedData.data(), static_cast(encodedData.size())), bioDeleter); + // Don't use newlines to flush buffer + BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); - // Check if memory allocation failed - if (b64 == nullptr || bio == nullptr) - throw std::bad_alloc(); + // Push the base64 BIO to the memory BIO + bio.reset(BIO_push(b64, bio.release())); // Transfer ownership to bio - // Push the memory BIO to the base64 BIO - bio.reset(BIO_push(b64.get(), bio.get())); + std::vector decodedData(encodedData.size()); // Decode the data const int len = BIO_read(bio.get(), decodedData.data(), static_cast(decodedData.size())); - - // Check if the decoding failed if (len < 0) throw std::runtime_error("BIO_read() failed."); diff --git a/src/utils/utils.cppm b/src/utils/utils.cppm index f7bcff6..116c4a8 100644 --- a/src/utils/utils.cppm +++ b/src/utils/utils.cppm @@ -78,33 +78,34 @@ export { /// \param input a vector of the binary data to be encoded. /// \return Base64-encoded string. std::string base64Encode(const uCharVector auto &input) { - // Custom deleter for BIO objects - auto bioDeleter = [](BIO *bio) { BIO_free_all(bio); }; - // Create a BIO object to encode the data - std::unique_ptr b64(BIO_new(BIO_f_base64()), bioDeleter); - - // Don't use newlines to flush buffer - BIO_set_flags(b64.get(), BIO_FLAGS_BASE64_NO_NL); + const std::unique_ptr b64(BIO_new(BIO_f_base64()), &BIO_free_all); + if (b64 == nullptr) + throw std::bad_alloc(); // Memory allocation failed // Create a memory BIO to store the encoded data - std::unique_ptr bio(BIO_new(BIO_s_mem()), bioDeleter); + BIO *bio = BIO_new(BIO_s_mem()); + if (bio == nullptr) + throw std::bad_alloc(); // Memory allocation failed - // Check if memory allocation failed - if (b64 == nullptr || bio == nullptr) - throw std::bad_alloc(); + // Don't use newlines to flush buffer + BIO_set_flags(b64.get(), BIO_FLAGS_BASE64_NO_NL); // Push the memory BIO to the base64 BIO - bio.reset(BIO_push(b64.get(), bio.release())); + bio = BIO_push(b64.get(), bio); // Transfer ownership to b64 - if (BIO_write(bio.get(), input.data(), static_cast(input.size())) < 0) + // Write the data to the BIO + if (BIO_write(bio, input.data(), static_cast(input.size())) < 0) throw std::runtime_error("BIO_write() failed."); - BIO_flush(bio.get()); + // Flush the BIO + BIO_flush(bio); + // Get the pointer to the BIO's data BUF_MEM *bufferPtr; BIO_get_mem_ptr(b64.get(), &bufferPtr); + // Create a string from the data std::string encodedData(bufferPtr->data, bufferPtr->length); return encodedData;