]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Templatize B64Decode()
authorRemi Gacogne <remi.gacogne@powerdns.com>
Mon, 12 Oct 2020 15:27:17 +0000 (17:27 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Mon, 11 Jan 2021 09:22:00 +0000 (10:22 +0100)
pdns/base64.cc
pdns/base64.hh
pdns/sodcrypto.cc

index 6dbb904849edda51c0237fc8c58e8794be5381d6..06093857973037896311ef90275f809958d3147b 100644 (file)
@@ -28,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();
@@ -36,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()) {
index f7b39ec250e6056d413566f4f9c1dfc2459ae769..a84181db32127f29626e83f075589e4521eaf650 100644 (file)
@@ -22,5 +22,5 @@
 #pragma once
 #include <string>
 
-int B64Decode(const std::string& src, std::string& dst);
+template<typename Container> int B64Decode(const std::string& src, Container& dst);
 std::string Base64Encode (const std::string& src);
index 967efc87f2576dfbc27920611e43defd5fb2caec..5c96e1a1625e643b3212592f67c0f6a71f4720f9 100644 (file)
@@ -204,7 +204,7 @@ static inline char B64Encode1(unsigned char uc)
 }
 using namespace anonpdns;
 
-int B64Decode(const std::string& strInput, std::string& strOutput)
+template<typename Container> int B64Decode(const std::string& strInput, Container& strOutput)
 {
   // Set up a decoding buffer
   long cBuf = 0;
@@ -276,21 +276,24 @@ int B64Decode(const std::string& strInput, std::string& strOutput)
     // may have been padding, so those padded bytes
     // are actually ignored.
 #if BYTE_ORDER == BIG_ENDIAN
-    strOutput += pBuf[sizeof(long)-3];
-    strOutput += pBuf[sizeof(long)-2];
-    strOutput += pBuf[sizeof(long)-1];
+    strOutput.push_back(pBuf[sizeof(long)-3]);
+    strOutput.push_back(pBuf[sizeof(long)-2]);
+    strOutput.push_back(pBuf[sizeof(long)-1]);
 #else
-    strOutput += pBuf[2];
-    strOutput += pBuf[1];
-    strOutput += pBuf[0];
+    strOutput.push_back(pBuf[2]);
+    strOutput.push_back(pBuf[1]);
+    strOutput.push_back(pBuf[0]);
 #endif
   } // while
   if(pad)
-    strOutput.resize(strOutput.length()-pad);
+    strOutput.resize(strOutput.size()-pad);
 
   return 1;
 }
 
+template int B64Decode<std::vector<uint8_t>>(const std::string& strInput, std::vector<uint8_t>& strOutput);
+template int B64Decode<std::string>(const std::string& strInput, std::string& strOutput);
+
 /*
 www.kbcafe.com
 Copyright 2001-2002 Randy Charles Morin