Correct alphabet; fix compilation issues on linux; add padding and make it on by default.

This commit is contained in:
Philip Prindeville 2012-11-13 16:01:35 -07:00
parent 9b89394b5b
commit e92baf7258
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. /// not updated to match the buffer's state.
{ {
public: public:
Base32EncoderBuf(std::ostream& ostr); Base32EncoderBuf(std::ostream& ostr, bool padding = true);
~Base32EncoderBuf(); ~Base32EncoderBuf();
int close(); int close();
@ -71,6 +71,7 @@ private:
unsigned char _group[5]; unsigned char _group[5];
int _groupLength; int _groupLength;
std::streambuf& _buf; std::streambuf& _buf;
bool _doPadding;
static const unsigned char OUT_ENCODING[32]; 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. /// order of the stream buffer and base classes.
{ {
public: public:
Base32EncoderIOS(std::ostream& ostr); Base32EncoderIOS(std::ostream& ostr, bool padding = true);
~Base32EncoderIOS(); ~Base32EncoderIOS();
int close(); int close();
Base32EncoderBuf* rdbuf(); Base32EncoderBuf* rdbuf();
@ -116,7 +117,7 @@ class Foundation_API Base32Encoder: public Base32EncoderIOS, public std::ostream
/// not updated to match the buffer's state. /// not updated to match the buffer's state.
{ {
public: public:
Base32Encoder(std::ostream& ostr); Base32Encoder(std::ostream& ostr, bool padding = true);
~Base32Encoder(); ~Base32Encoder();
private: private:

View File

@ -38,6 +38,7 @@
#include "Poco/Base32Encoder.h" #include "Poco/Base32Encoder.h"
#include "Poco/Exception.h" #include "Poco/Exception.h"
#include "Poco/Mutex.h" #include "Poco/Mutex.h"
#include <cstring>
namespace Poco { namespace Poco {
@ -89,7 +90,7 @@ int Base32DecoderBuf::readFromDevice()
else else
{ {
unsigned char buffer[8]; unsigned char buffer[8];
memset(buffer, '=', sizeof(buffer)); std::memset(buffer, '=', sizeof(buffer));
int c; int c;
// per RFC-4648, Section 6, permissible block lengths are: // 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', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', '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), _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; if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
idx = ((_group[0] & 0x07) << 2); idx = ((_group[0] & 0x07) << 2);
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof; if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
#if 0 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;
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 }
} }
else if (_groupLength == 2) else if (_groupLength == 2)
{ {
@ -133,12 +134,12 @@ int Base32EncoderBuf::close()
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof; if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
idx = ((_group[1] & 0x01) << 4); idx = ((_group[1] & 0x01) << 4);
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof; if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
#if 0 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;
if (_buf.sputc('=') == eof) return eof; if (_buf.sputc('=') == eof) return eof;
#endif }
} }
else if (_groupLength == 3) else if (_groupLength == 3)
{ {
@ -154,11 +155,11 @@ int Base32EncoderBuf::close()
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof; if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
idx = ((_group[2] & 0x0F) << 1); idx = ((_group[2] & 0x0F) << 1);
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof; if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
#if 0 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;
#endif }
} }
else if (_groupLength == 4) else if (_groupLength == 4)
{ {
@ -178,16 +179,15 @@ int Base32EncoderBuf::close()
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof; if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
idx = ((_group[3] & 0x03) << 3); idx = ((_group[3] & 0x03) << 3);
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof; if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
#if 0 if (_doPadding && _buf.sputc('=') == eof) return eof;
if (_buf.sputc('=') == eof) return eof;
#endif
} }
_groupLength = 0; _groupLength = 0;
return _buf.pubsync(); return _buf.pubsync();
} }
Base32EncoderIOS::Base32EncoderIOS(std::ostream& ostr): _buf(ostr) Base32EncoderIOS::Base32EncoderIOS(std::ostream& ostr, bool padding):
_buf(ostr, padding)
{ {
poco_ios_init(&_buf); 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); Base32Encoder encoder(str);
encoder << std::string("\00\01\02\03\04\05", 6); encoder << std::string("\00\01\02\03\04\05", 6);
encoder.close(); encoder.close();
assert (str.str() == "AAAQEAYEAU"); assert (str.str() == "AAAQEAYEAU======");
} }
{ {
std::ostringstream str; std::ostringstream str;
Base32Encoder encoder(str); Base32Encoder encoder(str);
encoder << std::string("\00\01\02\03", 4); encoder << std::string("\00\01\02\03", 4);
encoder.close(); 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; std::ostringstream str;
Base32Encoder encoder(str); Base32Encoder encoder(str);
encoder << "ABCDEF"; encoder << "ABCDEF";
encoder.close(); encoder.close();
assert (str.str() == "IFBEGRCFIY"); assert (str.str() == "IFBEGRCFIY======");
} }
{ {
std::ostringstream str; std::ostringstream str;
@ -90,7 +97,7 @@ void Base32Test::testEncoder()
void Base32Test::testDecoder() void Base32Test::testDecoder()
{ {
{ {
std::istringstream istr("AAAQEAYEAU"); std::istringstream istr("AAAQEAYEAU======");
Base32Decoder decoder(istr); Base32Decoder decoder(istr);
assert (decoder.good() && decoder.get() == 0); assert (decoder.good() && decoder.get() == 0);
assert (decoder.good() && decoder.get() == 1); assert (decoder.good() && decoder.get() == 1);
@ -111,7 +118,7 @@ void Base32Test::testDecoder()
assert (decoder.good() && decoder.get() == -1); assert (decoder.good() && decoder.get() == -1);
} }
{ {
std::istringstream istr("AAAQEAY"); std::istringstream istr("AAAQEAY=");
Base32Decoder decoder(istr); Base32Decoder decoder(istr);
assert (decoder.good() && decoder.get() == 0); assert (decoder.good() && decoder.get() == 0);
assert (decoder.good() && decoder.get() == 1); assert (decoder.good() && decoder.get() == 1);
@ -120,7 +127,7 @@ void Base32Test::testDecoder()
assert (decoder.good() && decoder.get() == -1); assert (decoder.good() && decoder.get() == -1);
} }
{ {
std::istringstream istr("IFBEGRCFIY"); std::istringstream istr("IFBEGRCFIY======");
Base32Decoder decoder(istr); Base32Decoder decoder(istr);
std::string s; std::string s;
decoder >> s; decoder >> s;