Skip to content

Commit

Permalink
Add pf_invalid arg to std::string DecodeBase{32,64}
Browse files Browse the repository at this point in the history
Add support for the optional "pf_invalid" out parameter (which allows the caller
to detect decoding failures) to the std::string versions of DecodeBase32 and
DecodeBase64. The char* versions already have this feature.

Also, rename all uses of pfInvalid to pf_invalid to match style guidelines.
  • Loading branch information
gwillen committed Feb 11, 2019
1 parent 1bc149d commit 162ffef
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
16 changes: 8 additions & 8 deletions src/util/strencodings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ std::string EncodeBase64(const std::string& str)
return EncodeBase64((const unsigned char*)str.c_str(), str.size());
}

std::vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid)
std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid)
{
static const int decode64_table[256] =
{
Expand Down Expand Up @@ -183,14 +183,14 @@ std::vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid)
++p;
}
valid = valid && (p - e) % 4 == 0 && p - q < 4;
if (pfInvalid) *pfInvalid = !valid;
if (pf_invalid) *pf_invalid = !valid;

return ret;
}

std::string DecodeBase64(const std::string& str)
std::string DecodeBase64(const std::string& str, bool* pf_invalid)
{
std::vector<unsigned char> vchRet = DecodeBase64(str.c_str());
std::vector<unsigned char> vchRet = DecodeBase64(str.c_str(), pf_invalid);
return std::string((const char*)vchRet.data(), vchRet.size());
}

Expand All @@ -210,7 +210,7 @@ std::string EncodeBase32(const std::string& str)
return EncodeBase32((const unsigned char*)str.c_str(), str.size());
}

std::vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid)
std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid)
{
static const int decode32_table[256] =
{
Expand Down Expand Up @@ -252,14 +252,14 @@ std::vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid)
++p;
}
valid = valid && (p - e) % 8 == 0 && p - q < 8;
if (pfInvalid) *pfInvalid = !valid;
if (pf_invalid) *pf_invalid = !valid;

return ret;
}

std::string DecodeBase32(const std::string& str)
std::string DecodeBase32(const std::string& str, bool* pf_invalid)
{
std::vector<unsigned char> vchRet = DecodeBase32(str.c_str());
std::vector<unsigned char> vchRet = DecodeBase32(str.c_str(), pf_invalid);
return std::string((const char*)vchRet.data(), vchRet.size());
}

Expand Down
8 changes: 4 additions & 4 deletions src/util/strencodings.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ bool IsHex(const std::string& str);
* Return true if the string is a hex number, optionally prefixed with "0x"
*/
bool IsHexNumber(const std::string& str);
std::vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid = nullptr);
std::string DecodeBase64(const std::string& str);
std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid = nullptr);
std::string DecodeBase64(const std::string& str, bool* pf_invalid = nullptr);
std::string EncodeBase64(const unsigned char* pch, size_t len);
std::string EncodeBase64(const std::string& str);
std::vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid = nullptr);
std::string DecodeBase32(const std::string& str);
std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid = nullptr);
std::string DecodeBase32(const std::string& str, bool* pf_invalid = nullptr);
std::string EncodeBase32(const unsigned char* pch, size_t len);
std::string EncodeBase32(const std::string& str);

Expand Down

0 comments on commit 162ffef

Please sign in to comment.