Merge pull request #8 from pprindeville/Base32Encoding

Correct alphabet; fix compilation issues on linux; add padding and make it on by default.
This commit is contained in:
Aleksandar Fabijanic 2012-11-13 15:24:32 -08:00
commit 0db2b8d98a
4 changed files with 47 additions and 37 deletions

View File

@ -59,7 +59,7 @@ class Foundation_API Base32EncoderBuf: public UnbufferedStreamBuf
/// not updated to match the buffer's state.
{
public:
Base32EncoderBuf(std::ostream& ostr);
Base32EncoderBuf(std::ostream& ostr, bool padding = true);
~Base32EncoderBuf();
int close();
@ -71,6 +71,7 @@ private:
unsigned char _group[5];
int _groupLength;
std::streambuf& _buf;
bool _doPadding;
static const unsigned char OUT_ENCODING[32];
@ -88,7 +89,7 @@ class Foundation_API Base32EncoderIOS: public virtual std::ios
/// order of the stream buffer and base classes.
{
public:
Base32EncoderIOS(std::ostream& ostr);
Base32EncoderIOS(std::ostream& ostr, bool padding = true);
~Base32EncoderIOS();
int close();
Base32EncoderBuf* rdbuf();
@ -116,7 +117,7 @@ class Foundation_API Base32Encoder: public Base32EncoderIOS, public std::ostream
/// not updated to match the buffer's state.
{
public:
Base32Encoder(std::ostream& ostr);
Base32Encoder(std::ostream& ostr, bool padding = true);
~Base32Encoder();
private:

View File

@ -38,6 +38,7 @@
#include "Poco/Base32Encoder.h"
#include "Poco/Exception.h"
#include "Poco/Mutex.h"
#include <cstring>
namespace Poco {
@ -89,7 +90,7 @@ int Base32DecoderBuf::readFromDevice()
else
{
unsigned char buffer[8];
memset(buffer, '=', sizeof(buffer));
std::memset(buffer, '=', sizeof(buffer));
int c;
// per RFC-4648, Section 6, permissible block lengths are:

View File

@ -45,13 +45,14 @@ const unsigned char Base32EncoderBuf::OUT_ENCODING[32] =
'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', '2', '3', '4', '5', '6', '8',
'Y', 'Z', '2', '3', '4', '5', '6', '7',
};
Base32EncoderBuf::Base32EncoderBuf(std::ostream& ostr):
Base32EncoderBuf::Base32EncoderBuf(std::ostream& ostr, bool padding):
_groupLength(0),
_buf(*ostr.rdbuf())
_buf(*ostr.rdbuf()),
_doPadding(padding)
{
}
@ -112,14 +113,14 @@ int Base32EncoderBuf::close()
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
idx = ((_group[0] & 0x07) << 2);
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
#if 0
if (_buf.sputc('=') == eof) return eof;
if (_buf.sputc('=') == eof) return eof;
if (_buf.sputc('=') == eof) return eof;
if (_buf.sputc('=') == eof) return eof;
if (_buf.sputc('=') == eof) return eof;
if (_buf.sputc('=') == eof) return eof;
#endif
if (_doPadding) {
if (_buf.sputc('=') == eof) return eof;
if (_buf.sputc('=') == eof) return eof;
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)
{
@ -133,12 +134,12 @@ int Base32EncoderBuf::close()
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
idx = ((_group[1] & 0x01) << 4);
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
#if 0
if (_buf.sputc('=') == eof) return eof;
if (_buf.sputc('=') == eof) return eof;
if (_buf.sputc('=') == eof) return eof;
if (_buf.sputc('=') == eof) return eof;
#endif
if (_doPadding) {
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 == 3)
{
@ -154,11 +155,11 @@ int Base32EncoderBuf::close()
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
idx = ((_group[2] & 0x0F) << 1);
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
#if 0
if (_buf.sputc('=') == eof) return eof;
if (_buf.sputc('=') == eof) return eof;
if (_buf.sputc('=') == eof) return eof;
#endif
if (_doPadding) {
if (_buf.sputc('=') == eof) return eof;
if (_buf.sputc('=') == eof) return eof;
if (_buf.sputc('=') == eof) return eof;
}
}
else if (_groupLength == 4)
{
@ -178,16 +179,15 @@ int Base32EncoderBuf::close()
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
idx = ((_group[3] & 0x03) << 3);
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
#if 0
if (_buf.sputc('=') == eof) return eof;
#endif
if (_doPadding && _buf.sputc('=') == eof) return eof;
}
_groupLength = 0;
return _buf.pubsync();
}
Base32EncoderIOS::Base32EncoderIOS(std::ostream& ostr): _buf(ostr)
Base32EncoderIOS::Base32EncoderIOS(std::ostream& ostr, bool padding):
_buf(ostr, padding)
{
poco_ios_init(&_buf);
}
@ -210,7 +210,8 @@ Base32EncoderBuf* Base32EncoderIOS::rdbuf()
}
Base32Encoder::Base32Encoder(std::ostream& ostr): Base32EncoderIOS(ostr), std::ostream(&_buf)
Base32Encoder::Base32Encoder(std::ostream& ostr, bool padding):
Base32EncoderIOS(ostr, padding), std::ostream(&_buf)
{
}

View File

@ -61,21 +61,28 @@ void Base32Test::testEncoder()
Base32Encoder encoder(str);
encoder << std::string("\00\01\02\03\04\05", 6);
encoder.close();
assert (str.str() == "AAAQEAYEAU");
assert (str.str() == "AAAQEAYEAU======");
}
{
std::ostringstream str;
Base32Encoder encoder(str);
encoder << std::string("\00\01\02\03", 4);
encoder.close();
assert (str.str() == "AAAQEAY");
assert (str.str() == "AAAQEAY=");
}
{
std::ostringstream str;
Base32Encoder encoder(str, false);
encoder << "ABCDEF";
encoder.close();
assert (str.str() == "IFBEGRCFIY");
}
{
std::ostringstream str;
Base32Encoder encoder(str);
encoder << "ABCDEF";
encoder.close();
assert (str.str() == "IFBEGRCFIY");
assert (str.str() == "IFBEGRCFIY======");
}
{
std::ostringstream str;
@ -90,7 +97,7 @@ void Base32Test::testEncoder()
void Base32Test::testDecoder()
{
{
std::istringstream istr("AAAQEAYEAU");
std::istringstream istr("AAAQEAYEAU======");
Base32Decoder decoder(istr);
assert (decoder.good() && decoder.get() == 0);
assert (decoder.good() && decoder.get() == 1);
@ -111,7 +118,7 @@ void Base32Test::testDecoder()
assert (decoder.good() && decoder.get() == -1);
}
{
std::istringstream istr("AAAQEAY");
std::istringstream istr("AAAQEAY=");
Base32Decoder decoder(istr);
assert (decoder.good() && decoder.get() == 0);
assert (decoder.good() && decoder.get() == 1);
@ -120,7 +127,7 @@ void Base32Test::testDecoder()
assert (decoder.good() && decoder.get() == -1);
}
{
std::istringstream istr("IFBEGRCFIY");
std::istringstream istr("IFBEGRCFIY======");
Base32Decoder decoder(istr);
std::string s;
decoder >> s;