base64 encoder/decoder: Add parameter to control linefeed handling
[libi2ncommon] / src / stringfunc.cpp
index 3813a1c..3c8ce1a 100644 (file)
@@ -712,9 +712,10 @@ static std::string _convert_BIO_to_string(BIO *input)
     * Data size limit is 2GB on 32 bit (LONG_MAX)
     *
     * @param input String to encode
+    * @param one_line Encode all data as one line, no wrapping with line feeds
     * @return base64 encoded string
     */
-std::string base64_encode(const std::string &input)
+std::string base64_encode(const std::string &input, bool one_line)
 {
     // check for empty buffer
     if (input.empty())
@@ -727,7 +728,8 @@ std::string base64_encode(const std::string &input)
     // setup encoder. Note: BIO_free_all frees both BIOs.
     BIO_Ptr base64_encoder(BIO_new(BIO_f_base64()), BIO_free_all);
     BIO *encoder_bio = base64_encoder.get();
-    BIO_set_flags(encoder_bio, BIO_FLAGS_BASE64_NO_NL);
+    if (one_line)
+        BIO_set_flags(encoder_bio, BIO_FLAGS_BASE64_NO_NL);
 
     // chain output buffer and encoder together
     BIO *encoded_result = BIO_new(BIO_s_mem());
@@ -752,9 +754,10 @@ std::string base64_encode(const std::string &input)
     * @brief base64 decode a string using OpenSSL base64 functions
     *
     * @param input String to decode
+    * @param one_line Expect all base64 data in one line. Input with line feeds will fail.
     * @return base64 decoded string
     */
-std::string base64_decode(const std::string &input)
+std::string base64_decode(const std::string &input, bool one_line)
 {
     // check for empty buffer
     if (input.empty())
@@ -767,7 +770,8 @@ std::string base64_decode(const std::string &input)
     // setup encoder. Note: BIO_free_all frees both BIOs.
     BIO_Ptr base64_decoder(BIO_new(BIO_f_base64()), BIO_free_all);
     BIO *bio_base64 = base64_decoder.get();
-    BIO_set_flags(bio_base64, BIO_FLAGS_BASE64_NO_NL);
+    if (one_line)
+        BIO_set_flags(bio_base64, BIO_FLAGS_BASE64_NO_NL);
 
     // chain input buffer and decoder together
     BIO *bio_input = BIO_new_mem_buf((void*)input.c_str(), input.size());