base64: add possibility for the decoding strings to not have padding characters
Add Base64::RequirePaddingOnDecodedStrings(bool) method. while here: - add Decode(...) methods taking wide characters as the input string
This commit is contained in:
parent
a3438f4ae0
commit
1cd2b4cafa
|
@ -5,7 +5,7 @@
|
|||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2012, Tomasz Sowa
|
||||
* Copyright (c) 2012-2022, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -50,6 +50,7 @@ Base64::Base64()
|
|||
padding = '=';
|
||||
tabbase64_len = 64;
|
||||
not_existing = 256; // first 8 bits are zero
|
||||
require_padding_on_decoded_strings = true;
|
||||
}
|
||||
|
||||
|
||||
|
@ -64,6 +65,14 @@ void Base64::SetPadding(char c)
|
|||
}
|
||||
|
||||
|
||||
void Base64::RequirePaddingOnDecodedStrings(bool require)
|
||||
{
|
||||
this->require_padding_on_decoded_strings = require;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void Base64::Convert3to4Pieces(const char * s, size_t len, unsigned int * tab4)
|
||||
{
|
||||
const unsigned char * tab3 = reinterpret_cast<const unsigned char*>(s);
|
||||
|
@ -155,20 +164,6 @@ bool Base64::CharFromBase64(unsigned int from, unsigned int & to)
|
|||
|
||||
|
||||
|
||||
bool Base64::Make4pieces(const char * s, unsigned int * tab4)
|
||||
{
|
||||
for(size_t i=0 ; i<4 ; ++i)
|
||||
{
|
||||
if( s[i] == padding )
|
||||
tab4[i] = not_existing;
|
||||
else
|
||||
if( !CharFromBase64(s[i], tab4[i]) )
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -185,15 +180,6 @@ void Base64::Save4PiecesToString(std::string & s, const unsigned int * tab4)
|
|||
|
||||
|
||||
|
||||
void Base64::Save3PiecesToString(std::string & s, const unsigned int * tab3)
|
||||
{
|
||||
for(int i=0 ; i<3 ; ++i)
|
||||
{
|
||||
if( tab3[i] != not_existing )
|
||||
s += tab3[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -229,31 +215,9 @@ void Base64::Encode(const std::string & in, std::string & out)
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
bool Base64::Decode(const char * in, size_t len, std::string & out)
|
||||
{
|
||||
unsigned int tab3[3];
|
||||
unsigned int tab4[4];
|
||||
|
||||
out.clear();
|
||||
size_t new_size = len - len/3 + 3;
|
||||
out.reserve(new_size);
|
||||
|
||||
if( len % 4 != 0 )
|
||||
return false;
|
||||
|
||||
for(size_t i=0 ; i<len ; i+=4)
|
||||
{
|
||||
if( !Make4pieces(in+i, tab4) )
|
||||
return false;
|
||||
|
||||
ConvertFrom4to3Pieces(tab4, tab3);
|
||||
Save3PiecesToString(out, tab3);
|
||||
}
|
||||
|
||||
return true;
|
||||
return DecodeRaw(in, len, out);
|
||||
}
|
||||
|
||||
|
||||
|
@ -270,6 +234,24 @@ bool Base64::Decode(const std::string & in, std::string & out)
|
|||
}
|
||||
|
||||
|
||||
bool Base64::Decode(const wchar_t * in, size_t len, std::string & out)
|
||||
{
|
||||
return DecodeRaw(in, len, out);
|
||||
}
|
||||
|
||||
|
||||
bool Base64::Decode(const wchar_t * in, std::string & out)
|
||||
{
|
||||
size_t len = wcslen(in);
|
||||
return Decode(in, len, out);
|
||||
}
|
||||
|
||||
|
||||
bool Base64::Decode(const std::wstring & in, std::string & out)
|
||||
{
|
||||
return Decode(in.c_str(), in.size(), out);
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace Tito
|
||||
|
|
87
src/base64.h
87
src/base64.h
|
@ -5,7 +5,7 @@
|
|||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2012, Tomasz Sowa
|
||||
* Copyright (c) 2012-2022, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -48,9 +48,18 @@ public:
|
|||
|
||||
Base64();
|
||||
|
||||
// default '=';
|
||||
/*
|
||||
* padding character
|
||||
* default '=';
|
||||
*/
|
||||
void SetPadding(char c);
|
||||
|
||||
/*
|
||||
* if false then allow the strings passed to Decode(...) methods to now have the trailing padding characters ('=')
|
||||
* default: true
|
||||
*/
|
||||
void RequirePaddingOnDecodedStrings(bool require);
|
||||
|
||||
void Encode(const char * in, size_t len, std::string & out);
|
||||
void Encode(const char * in, std::string & out);
|
||||
void Encode(const std::string & in, std::string & out);
|
||||
|
@ -59,6 +68,9 @@ public:
|
|||
bool Decode(const char * in, std::string & out);
|
||||
bool Decode(const std::string & in, std::string & out);
|
||||
|
||||
bool Decode(const wchar_t * in, size_t len, std::string & out);
|
||||
bool Decode(const wchar_t * in, std::string & out);
|
||||
bool Decode(const std::wstring & in, std::string & out);
|
||||
|
||||
|
||||
private:
|
||||
|
@ -67,17 +79,84 @@ private:
|
|||
size_t tabbase64_len;
|
||||
char padding;
|
||||
unsigned int not_existing;
|
||||
bool require_padding_on_decoded_strings;
|
||||
|
||||
void Convert3to4Pieces(const char * s, size_t len, unsigned int * tab4);
|
||||
void ConvertFrom4to3Pieces(const unsigned int * tab4, unsigned int * tab3);
|
||||
bool CharFromBase64(unsigned int from, unsigned int & to);
|
||||
bool Make4pieces(const char * s, unsigned int * tab4);
|
||||
void Save4PiecesToString(std::string & s, const unsigned int * tab4);
|
||||
void Save3PiecesToString(std::string & s, const unsigned int * tab3);
|
||||
|
||||
template<typename CharType>
|
||||
bool Make4pieces(const CharType * s, size_t len, unsigned int * tab4);
|
||||
|
||||
template<typename StringType>
|
||||
void Save3PiecesToString(const unsigned int * tab3, StringType & s);
|
||||
|
||||
template<typename CharType, typename StringType>
|
||||
bool DecodeRaw(const CharType * in, size_t len, StringType & out);
|
||||
|
||||
};
|
||||
|
||||
|
||||
template<typename CharType>
|
||||
bool Base64::Make4pieces(const CharType * s, size_t len, unsigned int * tab4)
|
||||
{
|
||||
for(size_t i=0 ; i<4 ; ++i)
|
||||
{
|
||||
if( i >= len || s[i] == padding )
|
||||
tab4[i] = not_existing;
|
||||
else
|
||||
if( !CharFromBase64(static_cast<unsigned int>(s[i]), tab4[i]) )
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
template<typename StringType>
|
||||
void Base64::Save3PiecesToString(const unsigned int * tab3, StringType & s)
|
||||
{
|
||||
for(int i=0 ; i<3 ; ++i)
|
||||
{
|
||||
if( tab3[i] != not_existing )
|
||||
s += tab3[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename CharType, typename StringType>
|
||||
bool Base64::DecodeRaw(const CharType * in, size_t len, StringType & out)
|
||||
{
|
||||
unsigned int tab3[3];
|
||||
unsigned int tab4[4];
|
||||
|
||||
out.clear();
|
||||
size_t new_size = len - len/3 + 3;
|
||||
out.reserve(new_size);
|
||||
|
||||
if( require_padding_on_decoded_strings && len % 4 != 0 )
|
||||
return false;
|
||||
|
||||
while( len > 0 )
|
||||
{
|
||||
if( !Make4pieces(in, len, tab4) )
|
||||
return false;
|
||||
|
||||
ConvertFrom4to3Pieces(tab4, tab3);
|
||||
Save3PiecesToString(tab3, out);
|
||||
|
||||
in += 4;
|
||||
len = (len >= 4) ? len - 4 : 0;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace Tito
|
||||
|
||||
|
|
Loading…
Reference in New Issue