From 65077d4d287bcd315e8e52bd7281d96d24be111c Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Mon, 12 Oct 2020 17:27:17 +0200 Subject: [PATCH] Templatize B64Decode() --- pdns/base64.cc | 10 ++++++---- pdns/base64.hh | 2 +- pdns/sodcrypto.cc | 19 +++++++++++-------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/pdns/base64.cc b/pdns/base64.cc index 6dbb904849ed..060938579730 100644 --- a/pdns/base64.cc +++ b/pdns/base64.cc @@ -28,7 +28,7 @@ #include #include -int B64Decode(const std::string& src, std::string& dst) +template 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 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(d.get()), olen ); + dst.resize(olen); return 0; } return -1; } +template int B64Decode(const std::string& strInput, std::string& strOutput); + std::string Base64Encode(const std::string& src) { if (!src.empty()) { diff --git a/pdns/base64.hh b/pdns/base64.hh index f7b39ec250e6..a84181db3212 100644 --- a/pdns/base64.hh +++ b/pdns/base64.hh @@ -22,5 +22,5 @@ #pragma once #include -int B64Decode(const std::string& src, std::string& dst); +template int B64Decode(const std::string& src, Container& dst); std::string Base64Encode (const std::string& src); diff --git a/pdns/sodcrypto.cc b/pdns/sodcrypto.cc index 967efc87f257..5c96e1a1625e 100644 --- a/pdns/sodcrypto.cc +++ b/pdns/sodcrypto.cc @@ -204,7 +204,7 @@ static inline char B64Encode1(unsigned char uc) } using namespace anonpdns; -int B64Decode(const std::string& strInput, std::string& strOutput) +template 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>(const std::string& strInput, std::vector& strOutput); +template int B64Decode(const std::string& strInput, std::string& strOutput); + /* www.kbcafe.com Copyright 2001-2002 Randy Charles Morin