Skip to content

Commit

Permalink
Templatize B64Decode()
Browse files Browse the repository at this point in the history
  • Loading branch information
rgacogne committed Jan 11, 2021
1 parent e13f437 commit 65077d4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
10 changes: 6 additions & 4 deletions pdns/base64.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,36 @@
#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();
return 0;
}
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()) {
Expand Down
2 changes: 1 addition & 1 deletion pdns/base64.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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);
19 changes: 11 additions & 8 deletions pdns/sodcrypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 65077d4

Please sign in to comment.