From e92baf7258f83f0c94ecd6e345655495c49b96d8 Mon Sep 17 00:00:00 2001 From: Philip Prindeville Date: Tue, 13 Nov 2012 16:01:35 -0700 Subject: [PATCH] Correct alphabet; fix compilation issues on linux; add padding and make it on by default. --- Foundation/include/Poco/Base32Encoder.h | 7 ++-- Foundation/src/Base32Decoder.cpp | 3 +- Foundation/src/Base32Encoder.cpp | 55 +++++++++++++------------ Foundation/testsuite/src/Base32Test.cpp | 19 ++++++--- 4 files changed, 47 insertions(+), 37 deletions(-) diff --git a/Foundation/include/Poco/Base32Encoder.h b/Foundation/include/Poco/Base32Encoder.h index cf3dd916e..dcde55a37 100644 --- a/Foundation/include/Poco/Base32Encoder.h +++ b/Foundation/include/Poco/Base32Encoder.h @@ -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: diff --git a/Foundation/src/Base32Decoder.cpp b/Foundation/src/Base32Decoder.cpp index 71156ac62..4e49effe4 100644 --- a/Foundation/src/Base32Decoder.cpp +++ b/Foundation/src/Base32Decoder.cpp @@ -38,6 +38,7 @@ #include "Poco/Base32Encoder.h" #include "Poco/Exception.h" #include "Poco/Mutex.h" +#include 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: diff --git a/Foundation/src/Base32Encoder.cpp b/Foundation/src/Base32Encoder.cpp index 1a742d8bf..7e4664dbb 100644 --- a/Foundation/src/Base32Encoder.cpp +++ b/Foundation/src/Base32Encoder.cpp @@ -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) { } diff --git a/Foundation/testsuite/src/Base32Test.cpp b/Foundation/testsuite/src/Base32Test.cpp index f2d537d94..37a6fab7e 100644 --- a/Foundation/testsuite/src/Base32Test.cpp +++ b/Foundation/testsuite/src/Base32Test.cpp @@ -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;