mirror of
https://github.com/pocoproject/poco.git
synced 2025-04-24 09:08:43 +02:00
Base64Encoder/Decoder: added support for 'base64url' encoding
Conflicts: Foundation/src/Base64Decoder.cpp Foundation/src/Base64Encoder.cpp
This commit is contained in:
parent
6273f52385
commit
a9f0279382
@ -37,20 +37,24 @@ class Foundation_API Base64DecoderBuf: public UnbufferedStreamBuf
|
|||||||
/// its streambuf.
|
/// its streambuf.
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Base64DecoderBuf(std::istream& istr);
|
Base64DecoderBuf(std::istream& istr, int options = 0);
|
||||||
~Base64DecoderBuf();
|
~Base64DecoderBuf();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int readFromDevice();
|
int readFromDevice();
|
||||||
int readOne();
|
int readOne();
|
||||||
|
|
||||||
|
int _options;
|
||||||
unsigned char _group[3];
|
unsigned char _group[3];
|
||||||
int _groupLength;
|
int _groupLength;
|
||||||
int _groupIndex;
|
int _groupIndex;
|
||||||
std::streambuf& _buf;
|
std::streambuf& _buf;
|
||||||
|
const unsigned char* _pInEncoding;
|
||||||
|
|
||||||
static unsigned char IN_ENCODING[256];
|
static unsigned char IN_ENCODING[256];
|
||||||
static bool IN_ENCODING_INIT;
|
static bool IN_ENCODING_INIT;
|
||||||
|
static unsigned char IN_ENCODING_URL[256];
|
||||||
|
static bool IN_ENCODING_URL_INIT;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Base64DecoderBuf(const Base64DecoderBuf&);
|
Base64DecoderBuf(const Base64DecoderBuf&);
|
||||||
@ -65,7 +69,7 @@ class Foundation_API Base64DecoderIOS: public virtual std::ios
|
|||||||
/// order of the stream buffer and base classes.
|
/// order of the stream buffer and base classes.
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Base64DecoderIOS(std::istream& istr);
|
Base64DecoderIOS(std::istream& istr, int options = 0);
|
||||||
~Base64DecoderIOS();
|
~Base64DecoderIOS();
|
||||||
Base64DecoderBuf* rdbuf();
|
Base64DecoderBuf* rdbuf();
|
||||||
|
|
||||||
@ -89,7 +93,7 @@ class Foundation_API Base64Decoder: public Base64DecoderIOS, public std::istream
|
|||||||
/// its streambuf.
|
/// its streambuf.
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Base64Decoder(std::istream& istr);
|
Base64Decoder(std::istream& istr, int options = 0);
|
||||||
~Base64Decoder();
|
~Base64Decoder();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -26,6 +26,19 @@
|
|||||||
namespace Poco {
|
namespace Poco {
|
||||||
|
|
||||||
|
|
||||||
|
enum Base64EncodingOptions
|
||||||
|
{
|
||||||
|
BASE64_URL_ENCODING = 0x01,
|
||||||
|
/// Use the URL and filename-safe alphabet,
|
||||||
|
/// replacing '+' with '-' and '/' with '_'.
|
||||||
|
///
|
||||||
|
/// Will also set line length to unlimited.
|
||||||
|
|
||||||
|
BASE64_NO_PADDING = 0x02
|
||||||
|
/// Do not append padding characters ('=') at end.
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
class Foundation_API Base64EncoderBuf: public UnbufferedStreamBuf
|
class Foundation_API Base64EncoderBuf: public UnbufferedStreamBuf
|
||||||
/// This streambuf base64-encodes all data written
|
/// This streambuf base64-encodes all data written
|
||||||
/// to it and forwards it to a connected
|
/// to it and forwards it to a connected
|
||||||
@ -37,7 +50,7 @@ class Foundation_API Base64EncoderBuf: public UnbufferedStreamBuf
|
|||||||
/// not updated to match the buffer's state.
|
/// not updated to match the buffer's state.
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Base64EncoderBuf(std::ostream& ostr);
|
Base64EncoderBuf(std::ostream& ostr, int options = 0);
|
||||||
~Base64EncoderBuf();
|
~Base64EncoderBuf();
|
||||||
|
|
||||||
int close();
|
int close();
|
||||||
@ -57,13 +70,16 @@ public:
|
|||||||
private:
|
private:
|
||||||
int writeToDevice(char c);
|
int writeToDevice(char c);
|
||||||
|
|
||||||
|
int _options;
|
||||||
unsigned char _group[3];
|
unsigned char _group[3];
|
||||||
int _groupLength;
|
int _groupLength;
|
||||||
int _pos;
|
int _pos;
|
||||||
int _lineLength;
|
int _lineLength;
|
||||||
std::streambuf& _buf;
|
std::streambuf& _buf;
|
||||||
|
const unsigned char* _pOutEncoding;
|
||||||
|
|
||||||
static const unsigned char OUT_ENCODING[64];
|
static const unsigned char OUT_ENCODING[64];
|
||||||
|
static const unsigned char OUT_ENCODING_URL[64];
|
||||||
|
|
||||||
friend class Base64DecoderBuf;
|
friend class Base64DecoderBuf;
|
||||||
|
|
||||||
@ -79,7 +95,7 @@ class Foundation_API Base64EncoderIOS: public virtual std::ios
|
|||||||
/// order of the stream buffer and base classes.
|
/// order of the stream buffer and base classes.
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Base64EncoderIOS(std::ostream& ostr);
|
Base64EncoderIOS(std::ostream& ostr, int options = 0);
|
||||||
~Base64EncoderIOS();
|
~Base64EncoderIOS();
|
||||||
int close();
|
int close();
|
||||||
Base64EncoderBuf* rdbuf();
|
Base64EncoderBuf* rdbuf();
|
||||||
@ -107,7 +123,7 @@ class Foundation_API Base64Encoder: public Base64EncoderIOS, public std::ostream
|
|||||||
/// not updated to match the buffer's state.
|
/// not updated to match the buffer's state.
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Base64Encoder(std::ostream& ostr);
|
Base64Encoder(std::ostream& ostr, int options = 0);
|
||||||
~Base64Encoder();
|
~Base64Encoder();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -23,6 +23,8 @@ namespace Poco {
|
|||||||
|
|
||||||
unsigned char Base64DecoderBuf::IN_ENCODING[256];
|
unsigned char Base64DecoderBuf::IN_ENCODING[256];
|
||||||
bool Base64DecoderBuf::IN_ENCODING_INIT = false;
|
bool Base64DecoderBuf::IN_ENCODING_INIT = false;
|
||||||
|
unsigned char Base64DecoderBuf::IN_ENCODING_URL[256];
|
||||||
|
bool Base64DecoderBuf::IN_ENCODING_URL_INIT = false;
|
||||||
|
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
@ -31,12 +33,32 @@ namespace
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Base64DecoderBuf::Base64DecoderBuf(std::istream& istr):
|
Base64DecoderBuf::Base64DecoderBuf(std::istream& istr, int options):
|
||||||
|
_options(options),
|
||||||
_groupLength(0),
|
_groupLength(0),
|
||||||
_groupIndex(0),
|
_groupIndex(0),
|
||||||
_buf(*istr.rdbuf())
|
_buf(*istr.rdbuf()),
|
||||||
|
_pInEncoding((options & BASE64_URL_ENCODING) ? IN_ENCODING_URL : IN_ENCODING)
|
||||||
{
|
{
|
||||||
FastMutex::ScopedLock lock(mutex);
|
FastMutex::ScopedLock lock(mutex);
|
||||||
|
if (options & BASE64_URL_ENCODING)
|
||||||
|
{
|
||||||
|
if (!IN_ENCODING_URL_INIT)
|
||||||
|
{
|
||||||
|
for (unsigned i = 0; i < sizeof(IN_ENCODING_URL); i++)
|
||||||
|
{
|
||||||
|
IN_ENCODING_URL[i] = 0xFF;
|
||||||
|
}
|
||||||
|
for (unsigned i = 0; i < sizeof(Base64EncoderBuf::OUT_ENCODING_URL); i++)
|
||||||
|
{
|
||||||
|
IN_ENCODING_URL[Base64EncoderBuf::OUT_ENCODING_URL[i]] = i;
|
||||||
|
}
|
||||||
|
IN_ENCODING_URL[static_cast<unsigned char>('=')] = '\0';
|
||||||
|
IN_ENCODING_URL_INIT = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
if (!IN_ENCODING_INIT)
|
if (!IN_ENCODING_INIT)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < sizeof(IN_ENCODING); i++)
|
for (unsigned i = 0; i < sizeof(IN_ENCODING); i++)
|
||||||
@ -51,6 +73,7 @@ Base64DecoderBuf::Base64DecoderBuf(std::istream& istr):
|
|||||||
IN_ENCODING_INIT = true;
|
IN_ENCODING_INIT = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Base64DecoderBuf::~Base64DecoderBuf()
|
Base64DecoderBuf::~Base64DecoderBuf()
|
||||||
@ -70,20 +93,36 @@ int Base64DecoderBuf::readFromDevice()
|
|||||||
int c;
|
int c;
|
||||||
if ((c = readOne()) == -1) return -1;
|
if ((c = readOne()) == -1) return -1;
|
||||||
buffer[0] = (unsigned char) c;
|
buffer[0] = (unsigned char) c;
|
||||||
if (IN_ENCODING[buffer[0]] == 0xFF) throw DataFormatException();
|
if (_pInEncoding[buffer[0]] == 0xFF) throw DataFormatException();
|
||||||
if ((c = readOne()) == -1) throw DataFormatException();
|
if ((c = readOne()) == -1) return -1;
|
||||||
buffer[1] = (unsigned char) c;
|
buffer[1] = (unsigned char) c;
|
||||||
if (IN_ENCODING[buffer[1]] == 0xFF) throw DataFormatException();
|
if (_pInEncoding[buffer[1]] == 0xFF) throw DataFormatException();
|
||||||
|
if (_options & BASE64_NO_PADDING)
|
||||||
|
{
|
||||||
|
if ((c = readOne()) != -1)
|
||||||
|
buffer[2] = c;
|
||||||
|
else
|
||||||
|
buffer[2] = '=';
|
||||||
|
if (_pInEncoding[buffer[2]] == 0xFF) throw DataFormatException();
|
||||||
|
if ((c = readOne()) != -1)
|
||||||
|
buffer[3] = c;
|
||||||
|
else
|
||||||
|
buffer[3] = '=';
|
||||||
|
if (_pInEncoding[buffer[3]] == 0xFF) throw DataFormatException();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
if ((c = readOne()) == -1) throw DataFormatException();
|
if ((c = readOne()) == -1) throw DataFormatException();
|
||||||
buffer[2] = c;
|
buffer[2] = c;
|
||||||
if (IN_ENCODING[buffer[2]] == 0xFF) throw DataFormatException();
|
if (_pInEncoding[buffer[2]] == 0xFF) throw DataFormatException();
|
||||||
if ((c = readOne()) == -1) throw DataFormatException();
|
if ((c = readOne()) == -1) throw DataFormatException();
|
||||||
buffer[3] = c;
|
buffer[3] = c;
|
||||||
if (IN_ENCODING[buffer[3]] == 0xFF) throw DataFormatException();
|
if (_pInEncoding[buffer[3]] == 0xFF) throw DataFormatException();
|
||||||
|
}
|
||||||
|
|
||||||
_group[0] = (IN_ENCODING[buffer[0]] << 2) | (IN_ENCODING[buffer[1]] >> 4);
|
_group[0] = (_pInEncoding[buffer[0]] << 2) | (_pInEncoding[buffer[1]] >> 4);
|
||||||
_group[1] = ((IN_ENCODING[buffer[1]] & 0x0F) << 4) | (IN_ENCODING[buffer[2]] >> 2);
|
_group[1] = ((_pInEncoding[buffer[1]] & 0x0F) << 4) | (_pInEncoding[buffer[2]] >> 2);
|
||||||
_group[2] = (IN_ENCODING[buffer[2]] << 6) | IN_ENCODING[buffer[3]];
|
_group[2] = (_pInEncoding[buffer[2]] << 6) | _pInEncoding[buffer[3]];
|
||||||
|
|
||||||
if (buffer[2] == '=')
|
if (buffer[2] == '=')
|
||||||
_groupLength = 1;
|
_groupLength = 1;
|
||||||
@ -100,13 +139,16 @@ int Base64DecoderBuf::readFromDevice()
|
|||||||
int Base64DecoderBuf::readOne()
|
int Base64DecoderBuf::readOne()
|
||||||
{
|
{
|
||||||
int ch = _buf.sbumpc();
|
int ch = _buf.sbumpc();
|
||||||
|
if (!(_options & BASE64_URL_ENCODING))
|
||||||
|
{
|
||||||
while (ch == ' ' || ch == '\r' || ch == '\t' || ch == '\n')
|
while (ch == ' ' || ch == '\r' || ch == '\t' || ch == '\n')
|
||||||
ch = _buf.sbumpc();
|
ch = _buf.sbumpc();
|
||||||
|
}
|
||||||
return ch;
|
return ch;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Base64DecoderIOS::Base64DecoderIOS(std::istream& istr): _buf(istr)
|
Base64DecoderIOS::Base64DecoderIOS(std::istream& istr, int options): _buf(istr, options)
|
||||||
{
|
{
|
||||||
poco_ios_init(&_buf);
|
poco_ios_init(&_buf);
|
||||||
}
|
}
|
||||||
@ -123,7 +165,7 @@ Base64DecoderBuf* Base64DecoderIOS::rdbuf()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Base64Decoder::Base64Decoder(std::istream& istr): Base64DecoderIOS(istr), std::istream(&_buf)
|
Base64Decoder::Base64Decoder(std::istream& istr, int options): Base64DecoderIOS(istr, options), std::istream(&_buf)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,11 +31,26 @@ const unsigned char Base64EncoderBuf::OUT_ENCODING[64] =
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
Base64EncoderBuf::Base64EncoderBuf(std::ostream& ostr):
|
const unsigned char Base64EncoderBuf::OUT_ENCODING_URL[64] =
|
||||||
|
{
|
||||||
|
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
|
||||||
|
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
||||||
|
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
|
||||||
|
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
|
||||||
|
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
|
||||||
|
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
|
||||||
|
'w', 'x', 'y', 'z', '0', '1', '2', '3',
|
||||||
|
'4', '5', '6', '7', '8', '9', '-', '_'
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Base64EncoderBuf::Base64EncoderBuf(std::ostream& ostr, int options):
|
||||||
|
_options(options),
|
||||||
_groupLength(0),
|
_groupLength(0),
|
||||||
_pos(0),
|
_pos(0),
|
||||||
_lineLength(72),
|
_lineLength((options & BASE64_URL_ENCODING) ? 0 : 72),
|
||||||
_buf(*ostr.rdbuf())
|
_buf(*ostr.rdbuf()),
|
||||||
|
_pOutEncoding((options & BASE64_URL_ENCODING) ? OUT_ENCODING_URL : OUT_ENCODING)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,13 +88,13 @@ int Base64EncoderBuf::writeToDevice(char c)
|
|||||||
{
|
{
|
||||||
unsigned char idx;
|
unsigned char idx;
|
||||||
idx = _group[0] >> 2;
|
idx = _group[0] >> 2;
|
||||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
if (_buf.sputc(_pOutEncoding[idx]) == eof) return eof;
|
||||||
idx = ((_group[0] & 0x03) << 4) | (_group[1] >> 4);
|
idx = ((_group[0] & 0x03) << 4) | (_group[1] >> 4);
|
||||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
if (_buf.sputc(_pOutEncoding[idx]) == eof) return eof;
|
||||||
idx = ((_group[1] & 0x0F) << 2) | (_group[2] >> 6);
|
idx = ((_group[1] & 0x0F) << 2) | (_group[2] >> 6);
|
||||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
if (_buf.sputc(_pOutEncoding[idx]) == eof) return eof;
|
||||||
idx = _group[2] & 0x3F;
|
idx = _group[2] & 0x3F;
|
||||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
if (_buf.sputc(_pOutEncoding[idx]) == eof) return eof;
|
||||||
_pos += 4;
|
_pos += 4;
|
||||||
if (_lineLength > 0 && _pos >= _lineLength)
|
if (_lineLength > 0 && _pos >= _lineLength)
|
||||||
{
|
{
|
||||||
@ -103,30 +118,36 @@ int Base64EncoderBuf::close()
|
|||||||
_group[1] = 0;
|
_group[1] = 0;
|
||||||
unsigned char idx;
|
unsigned char idx;
|
||||||
idx = _group[0] >> 2;
|
idx = _group[0] >> 2;
|
||||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
if (_buf.sputc(_pOutEncoding[idx]) == eof) return eof;
|
||||||
idx = ((_group[0] & 0x03) << 4) | (_group[1] >> 4);
|
idx = ((_group[0] & 0x03) << 4) | (_group[1] >> 4);
|
||||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
if (_buf.sputc(_pOutEncoding[idx]) == eof) return eof;
|
||||||
|
if (!(_options & BASE64_NO_PADDING))
|
||||||
|
{
|
||||||
if (_buf.sputc('=') == eof) return eof;
|
if (_buf.sputc('=') == eof) return eof;
|
||||||
if (_buf.sputc('=') == eof) return eof;
|
if (_buf.sputc('=') == eof) return eof;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else if (_groupLength == 2)
|
else if (_groupLength == 2)
|
||||||
{
|
{
|
||||||
_group[2] = 0;
|
_group[2] = 0;
|
||||||
unsigned char idx;
|
unsigned char idx;
|
||||||
idx = _group[0] >> 2;
|
idx = _group[0] >> 2;
|
||||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
if (_buf.sputc(_pOutEncoding[idx]) == eof) return eof;
|
||||||
idx = ((_group[0] & 0x03) << 4) | (_group[1] >> 4);
|
idx = ((_group[0] & 0x03) << 4) | (_group[1] >> 4);
|
||||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
if (_buf.sputc(_pOutEncoding[idx]) == eof) return eof;
|
||||||
idx = ((_group[1] & 0x0F) << 2) | (_group[2] >> 6);
|
idx = ((_group[1] & 0x0F) << 2) | (_group[2] >> 6);
|
||||||
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
|
if (_buf.sputc(_pOutEncoding[idx]) == eof) return eof;
|
||||||
|
if (!(_options & BASE64_NO_PADDING))
|
||||||
|
{
|
||||||
if (_buf.sputc('=') == eof) return eof;
|
if (_buf.sputc('=') == eof) return eof;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
_groupLength = 0;
|
_groupLength = 0;
|
||||||
return _buf.pubsync();
|
return _buf.pubsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Base64EncoderIOS::Base64EncoderIOS(std::ostream& ostr): _buf(ostr)
|
Base64EncoderIOS::Base64EncoderIOS(std::ostream& ostr, int options): _buf(ostr, options)
|
||||||
{
|
{
|
||||||
poco_ios_init(&_buf);
|
poco_ios_init(&_buf);
|
||||||
}
|
}
|
||||||
@ -149,7 +170,7 @@ Base64EncoderBuf* Base64EncoderIOS::rdbuf()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Base64Encoder::Base64Encoder(std::ostream& ostr): Base64EncoderIOS(ostr), std::ostream(&_buf)
|
Base64Encoder::Base64Encoder(std::ostream& ostr, int options): Base64EncoderIOS(ostr, options), std::ostream(&_buf)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,6 +55,79 @@ void Base64Test::testEncoder()
|
|||||||
encoder.close();
|
encoder.close();
|
||||||
assert (str.str() == "QUJDREVG");
|
assert (str.str() == "QUJDREVG");
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
std::ostringstream str;
|
||||||
|
Base64Encoder encoder(str);
|
||||||
|
encoder << "!@#$%^&*()_~<>";
|
||||||
|
encoder.close();
|
||||||
|
assert (str.str() == "IUAjJCVeJiooKV9+PD4=");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Base64Test::testEncoderURL()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
std::ostringstream str;
|
||||||
|
Base64Encoder encoder(str, Poco::BASE64_URL_ENCODING);
|
||||||
|
encoder << std::string("\00\01\02\03\04\05", 6);
|
||||||
|
encoder.close();
|
||||||
|
assert (str.str() == "AAECAwQF");
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::ostringstream str;
|
||||||
|
Base64Encoder encoder(str, Poco::BASE64_URL_ENCODING);
|
||||||
|
encoder << std::string("\00\01\02\03", 4);
|
||||||
|
encoder.close();
|
||||||
|
assert (str.str() == "AAECAw==");
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::ostringstream str;
|
||||||
|
Base64Encoder encoder(str, Poco::BASE64_URL_ENCODING);
|
||||||
|
encoder << "ABCDEF";
|
||||||
|
encoder.close();
|
||||||
|
assert (str.str() == "QUJDREVG");
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::ostringstream str;
|
||||||
|
Base64Encoder encoder(str, Poco::BASE64_URL_ENCODING);
|
||||||
|
encoder << "!@#$%^&*()_~<>";
|
||||||
|
encoder.close();
|
||||||
|
assert (str.str() == "IUAjJCVeJiooKV9-PD4=");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Base64Test::testEncoderNoPadding()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
std::ostringstream str;
|
||||||
|
Base64Encoder encoder(str, Poco::BASE64_URL_ENCODING | Poco::BASE64_NO_PADDING);
|
||||||
|
encoder << std::string("\00\01\02\03\04\05", 6);
|
||||||
|
encoder.close();
|
||||||
|
assert (str.str() == "AAECAwQF");
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::ostringstream str;
|
||||||
|
Base64Encoder encoder(str, Poco::BASE64_URL_ENCODING | Poco::BASE64_NO_PADDING);
|
||||||
|
encoder << std::string("\00\01\02\03", 4);
|
||||||
|
encoder.close();
|
||||||
|
assert (str.str() == "AAECAw");
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::ostringstream str;
|
||||||
|
Base64Encoder encoder(str, Poco::BASE64_URL_ENCODING | Poco::BASE64_NO_PADDING);
|
||||||
|
encoder << "ABCDEF";
|
||||||
|
encoder.close();
|
||||||
|
assert (str.str() == "QUJDREVG");
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::ostringstream str;
|
||||||
|
Base64Encoder encoder(str, Poco::BASE64_URL_ENCODING | Poco::BASE64_NO_PADDING);
|
||||||
|
encoder << "!@#$%^&*()_~<>";
|
||||||
|
encoder.close();
|
||||||
|
assert (str.str() == "IUAjJCVeJiooKV9-PD4");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -125,6 +198,115 @@ void Base64Test::testDecoder()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Base64Test::testDecoderURL()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
std::istringstream istr("AAECAwQF");
|
||||||
|
Base64Decoder decoder(istr, Poco::BASE64_URL_ENCODING);
|
||||||
|
assert (decoder.good() && decoder.get() == 0);
|
||||||
|
assert (decoder.good() && decoder.get() == 1);
|
||||||
|
assert (decoder.good() && decoder.get() == 2);
|
||||||
|
assert (decoder.good() && decoder.get() == 3);
|
||||||
|
assert (decoder.good() && decoder.get() == 4);
|
||||||
|
assert (decoder.good() && decoder.get() == 5);
|
||||||
|
assert (decoder.good() && decoder.get() == -1);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::istringstream istr("AAECAwQ=");
|
||||||
|
Base64Decoder decoder(istr, Poco::BASE64_URL_ENCODING);
|
||||||
|
assert (decoder.good() && decoder.get() == 0);
|
||||||
|
assert (decoder.good() && decoder.get() == 1);
|
||||||
|
assert (decoder.good() && decoder.get() == 2);
|
||||||
|
assert (decoder.good() && decoder.get() == 3);
|
||||||
|
assert (decoder.good() && decoder.get() == 4);
|
||||||
|
assert (decoder.good() && decoder.get() == -1);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::istringstream istr("AAECAw==", Poco::BASE64_URL_ENCODING);
|
||||||
|
Base64Decoder decoder(istr);
|
||||||
|
assert (decoder.good() && decoder.get() == 0);
|
||||||
|
assert (decoder.good() && decoder.get() == 1);
|
||||||
|
assert (decoder.good() && decoder.get() == 2);
|
||||||
|
assert (decoder.good() && decoder.get() == 3);
|
||||||
|
assert (decoder.good() && decoder.get() == -1);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::istringstream istr("QUJDREVG", Poco::BASE64_URL_ENCODING);
|
||||||
|
Base64Decoder decoder(istr);
|
||||||
|
std::string s;
|
||||||
|
decoder >> s;
|
||||||
|
assert (s == "ABCDEF");
|
||||||
|
assert (decoder.eof());
|
||||||
|
assert (!decoder.fail());
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::istringstream istr("QUJ\r\nDRE\r\nVG");
|
||||||
|
Base64Decoder decoder(istr, Poco::BASE64_URL_ENCODING);
|
||||||
|
std::string s;
|
||||||
|
decoder >> s;
|
||||||
|
assert (decoder.bad());
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::istringstream istr("QUJD#REVG");
|
||||||
|
Base64Decoder decoder(istr, Poco::BASE64_URL_ENCODING);
|
||||||
|
std::string s;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
decoder >> s;
|
||||||
|
assert (decoder.bad());
|
||||||
|
}
|
||||||
|
catch (DataFormatException&)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
assert (!decoder.eof());
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::istringstream istr("IUAjJCVeJiooKV9-PD4=");
|
||||||
|
Base64Decoder decoder(istr, Poco::BASE64_URL_ENCODING);
|
||||||
|
std::string s;
|
||||||
|
decoder >> s;
|
||||||
|
assert (s == "!@#$%^&*()_~<>");
|
||||||
|
assert (decoder.eof());
|
||||||
|
assert (!decoder.fail());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Base64Test::testDecoderNoPadding()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
std::istringstream istr("AAECAwQF");
|
||||||
|
Base64Decoder decoder(istr, Poco::BASE64_NO_PADDING);
|
||||||
|
assert (decoder.good() && decoder.get() == 0);
|
||||||
|
assert (decoder.good() && decoder.get() == 1);
|
||||||
|
assert (decoder.good() && decoder.get() == 2);
|
||||||
|
assert (decoder.good() && decoder.get() == 3);
|
||||||
|
assert (decoder.good() && decoder.get() == 4);
|
||||||
|
assert (decoder.good() && decoder.get() == 5);
|
||||||
|
assert (decoder.good() && decoder.get() == -1);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::istringstream istr("AAECAwQ");
|
||||||
|
Base64Decoder decoder(istr, Poco::BASE64_NO_PADDING);
|
||||||
|
assert (decoder.good() && decoder.get() == 0);
|
||||||
|
assert (decoder.good() && decoder.get() == 1);
|
||||||
|
assert (decoder.good() && decoder.get() == 2);
|
||||||
|
assert (decoder.good() && decoder.get() == 3);
|
||||||
|
assert (decoder.good() && decoder.get() == 4);
|
||||||
|
assert (decoder.good() && decoder.get() == -1);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::istringstream istr("AAECAw");
|
||||||
|
Base64Decoder decoder(istr, Poco::BASE64_NO_PADDING);
|
||||||
|
assert (decoder.good() && decoder.get() == 0);
|
||||||
|
assert (decoder.good() && decoder.get() == 1);
|
||||||
|
assert (decoder.good() && decoder.get() == 2);
|
||||||
|
assert (decoder.good() && decoder.get() == 3);
|
||||||
|
assert (decoder.good() && decoder.get() == -1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void Base64Test::testEncodeDecode()
|
void Base64Test::testEncodeDecode()
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
@ -170,7 +352,11 @@ CppUnit::Test* Base64Test::suite()
|
|||||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("Base64Test");
|
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("Base64Test");
|
||||||
|
|
||||||
CppUnit_addTest(pSuite, Base64Test, testEncoder);
|
CppUnit_addTest(pSuite, Base64Test, testEncoder);
|
||||||
|
CppUnit_addTest(pSuite, Base64Test, testEncoderURL);
|
||||||
|
CppUnit_addTest(pSuite, Base64Test, testEncoderNoPadding);
|
||||||
CppUnit_addTest(pSuite, Base64Test, testDecoder);
|
CppUnit_addTest(pSuite, Base64Test, testDecoder);
|
||||||
|
CppUnit_addTest(pSuite, Base64Test, testDecoderURL);
|
||||||
|
CppUnit_addTest(pSuite, Base64Test, testDecoderNoPadding);
|
||||||
CppUnit_addTest(pSuite, Base64Test, testEncodeDecode);
|
CppUnit_addTest(pSuite, Base64Test, testEncodeDecode);
|
||||||
|
|
||||||
return pSuite;
|
return pSuite;
|
||||||
|
@ -25,7 +25,11 @@ public:
|
|||||||
~Base64Test();
|
~Base64Test();
|
||||||
|
|
||||||
void testEncoder();
|
void testEncoder();
|
||||||
|
void testEncoderURL();
|
||||||
|
void testEncoderNoPadding();
|
||||||
void testDecoder();
|
void testDecoder();
|
||||||
|
void testDecoderURL();
|
||||||
|
void testDecoderNoPadding();
|
||||||
void testEncodeDecode();
|
void testEncodeDecode();
|
||||||
|
|
||||||
void setUp();
|
void setUp();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user