Fix some weird behavior in CString::Base64Decode and wrapped strings

With the current implementation of CString::Base64Decode the following
code would fail (meaning b would be false):

    CString t = "some very long string ...";
    bool b = (t == t.Base64Encode_n(true).Base64Decode_n());

The same code without wrapping the base64 output would give b = true
as expected.

The new implementation removes all new lines before decoding so
decoding a wrapped base64 text gives the expected result.

Furthermore replaced malloc and free with new and delete and removed
the check for p in CString::Base64Encode since new will throw if it
failed.

The changes don't affect any existing code.

git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@1410 726aef4b-f618-498e-8847-2d620e286838
This commit is contained in:
sebastinas
2009-03-04 18:20:08 +00:00
parent 812526278a
commit 20bbdee968

View File

@@ -735,13 +735,8 @@ bool CString::Base64Encode(CString& sRet, unsigned int uWrap) const {
return 0;
}
//p = output = (unsigned char *)malloc(toalloc);
p = output = new unsigned char [toalloc];
if (!p) {
return false;
}
while (i < len - mod) {
*p++ = b64table[input[i++] >> 2];
*p++ = b64table[((input[i - 1] << 4) | (input[i] >> 4)) & 0x3f];
@@ -781,11 +776,16 @@ bool CString::Base64Encode(CString& sRet, unsigned int uWrap) const {
}
unsigned long CString::Base64Decode(CString& sRet) const {
const char* in = c_str();
CString sTmp(*this);
// remove new lines
sTmp.Replace("\r", "");
sTmp.Replace("\n", "");
const char* in = sTmp.c_str();
char c, c1, *p;
unsigned long i;
unsigned long uLen = size();
char* out = (char*) malloc(size() +1);
unsigned long uLen = sTmp.size();
char* out = new char[uLen + 1];
for (i = 0, p = out; i < uLen; i++) {
c = (char)base64_table[(unsigned char)in[i++]];
@@ -812,7 +812,7 @@ unsigned long CString::Base64Decode(CString& sRet) const {
unsigned long uRet = p - out;
sRet.clear();
sRet.append(out, uRet);
free(out);
delete[] out;
return uRet;
}