]> git.ipfire.org Git - thirdparty/pdns.git/blobdiff - pdns/base64.cc
Merge pull request #14182 from rgacogne/ddist-dynblock-tag
[thirdparty/pdns.git] / pdns / base64.cc
index 6a0ca12f68b14091e95f93b397dfa59dcaabf864..06093857973037896311ef90275f809958d3147b 100644 (file)
@@ -1,3 +1,24 @@
+/*
+ * This file is part of PowerDNS or dnsdist.
+ * Copyright -- PowerDNS.COM B.V. and its contributors
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * In addition, for the avoidance of any doubt, permission is granted to
+ * link this program with OpenSSL and to (re)distribute the binaries
+ * produced as the result of such linking.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
@@ -7,7 +28,7 @@
 #include <openssl/bio.h>
 #include <openssl/evp.h>
 
-int B64Decode(const std::string& src, std::string& dst)
+template<typename Container> int B64Decode(const std::string& src, Container& dst)
 {
   if (src.empty() ) {
     dst.clear();
@@ -15,26 +36,28 @@ int B64Decode(const std::string& src, std::string& dst)
   }
   int dlen = ( src.length() * 6 + 7 ) / 8 ;
   ssize_t olen = 0;
-  boost::scoped_array<unsigned char> d( new unsigned char[dlen] );
+  dst.resize(dlen);
   BIO *bio, *b64;
   bio = BIO_new(BIO_s_mem());
   BIO_write(bio, src.c_str(), src.length());
   b64 = BIO_new(BIO_f_base64());
   bio = BIO_push(b64, bio);
   BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
-  olen = BIO_read(b64, d.get(), dlen);
+  olen = BIO_read(b64, &dst.at(0), dlen);
   if ((olen == 0 || olen == -1) && BIO_should_retry(bio)) {
     BIO_free_all(bio);
     throw std::runtime_error("BIO_read failed to read all data from memory buffer");
   }
   BIO_free_all(bio);
   if (olen > 0) {
-    dst = std::string( reinterpret_cast<const char*>(d.get()), olen );
+    dst.resize(olen);
     return 0;
   }
   return -1;
 }
 
+template int B64Decode<std::string>(const std::string& strInput, std::string& strOutput);
+
 std::string Base64Encode(const std::string& src)
 {
   if (!src.empty()) {
@@ -44,7 +67,8 @@ std::string Base64Encode(const std::string& src)
     bio = BIO_new(BIO_s_mem());
     bio = BIO_push(b64, bio);
     BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
-    if (BIO_write(bio, src.c_str(), src.length()) != src.length()) {
+    int bioWriteRet = BIO_write(bio, src.c_str(), src.length());
+    if (bioWriteRet < 0 || (size_t) bioWriteRet != src.length()) {
       BIO_free_all(bio);
       throw std::runtime_error("BIO_write failed to write all data to memory buffer");
     }