Sans Pareil Technologies, Inc.

Key To Your Business

Base64Encoder



An encoder that uses the base64 scheme to encode data written to output streams. See the unit test suite for sample usage.

UnbufferedStreamBuf.h

A base interface for unbuffered I/O streams.

    1: /*
    2: * UnufferedStreamBuf.h
    3: *
    4: * $Id:  *poco/1.4/Foundation/include/Poco/UnbufferedStreamBuf.h#1 $
    5: *
    6: * Library: Foundation
    7: * Package: Streams
    8: * Module:  StreamBuf
    9: *
   10: * Definition of template BasicUnbufferedStreamBuf and class UnbufferedStreamBuf.
   11: *
   12: * Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
   13: * and Contributors.
   14: *
   15: * Permission is hereby granted, free of charge, to any person or organization
   16: * obtaining a copy of the software and accompanying documentation covered by
   17: * this license (the "Software") to use, reproduce, display, distribute,
   18: * execute, and transmit the Software, and to prepare derivative works of the
   19: * Software, and to permit third-parties to whom the Software is furnished to
   20: * do so, all subject to the following:
   21: *
   22: * The copyright notices in the Software and this entire statement, including
   23: * the above license grant, this restriction and the following disclaimer,
   24: * must be included in all copies of the Software, in whole or in part, and
   25: * all derivative works of the Software, unless such copies or derivative
   26: * works are solely in the form of machine-executable object code generated by
   27: * a source language processor.
   28: *
   29: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   30: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   31: * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
   32: * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
   33: * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
   34: * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   35: * DEALINGS IN THE SOFTWARE.
   36: */
   37:
   38:
   39: #ifndef SPT_UNBUFFEREDSTREAMBUF
   40: #define SPT_UNBUFFEREDSTREAMBUF
   41:
   42:
   43: #if defined( ARDUINO )
   44: #include "../StandardCplusplus/streambuf"
   45: #include "../StandardCplusplus/iosfwd"
   46: #include "../StandardCplusplus/ios"
   47: #else
   48: #include <streambuf>
   49: #include <iosfwd>
   50: #include <ios>
   51: #endif
   52:
   53:
   54: namespace spt
   55: {
   56:   /// This is an implementation of an unbuffered streambuf
   57:   /// that greatly simplifies the implementation of
   58:   /// custom streambufs of various kinds.
   59:   /// Derived classes only have to override the methods
   60:   /// readFromDevice() or writeToDevice().
   61:   template <typename ch, typename tr>
   62:   class BasicUnbufferedStreamBuf: public std::basic_streambuf<ch, tr>
   63:   {
   64:   protected:
   65:     typedef std::basic_streambuf<ch, tr> Base;
   66:     typedef std::basic_ios<ch, tr> IOS;
   67:     typedef ch char_type;
   68:     typedef tr char_traits;
   69:     typedef typename Base::int_type int_type;
   70:     typedef typename Base::pos_type pos_type;
   71:     typedef typename Base::off_type off_type;
   72:     typedef typename IOS::openmode openmode;
   73:
   74:   public:
   75:     BasicUnbufferedStreamBuf() :
   76:       pb( char_traits::eof() ), ispb( false )
   77:     {
   78:       this->setg( 0, 0, 0 );
   79:       this->setp( 0, 0 );
   80:     }
   81:
   82:     ~BasicUnbufferedStreamBuf() {}
   83:
   84:     virtual int_type overflow( int_type c )
   85:     {
   86:       if ( c != char_traits::eof() ) return writeToDevice( char_traits::to_char_type( c ) );
   87:       else return c;
   88:     }
   89:
   90:     virtual int_type underflow()
   91:     {
   92:       if ( ispb ) return pb;
   93:       else
   94:       {
   95:         int_type c = readFromDevice();
   96:         if ( c != char_traits::eof() )
   97:         {
   98:           ispb = true;
   99:           pb   = c;
  100:         }
  101:
  102:         return c;
  103:       }
  104:     }
  105:
  106:     virtual int_type uflow()
  107:     {
  108:       if (ispb)
  109:       {
  110:         ispb = false;
  111:         return pb;
  112:       }
  113:       else
  114:       {
  115:         int_type c = readFromDevice();
  116:         if ( c != char_traits::eof() ) pb = c;
  117:         return c;
  118:       }
  119:     }
  120:
  121:     virtual int_type pbackfail( int_type c )
  122:     {
  123:       if ( ispb ) return char_traits::eof();
  124:       else
  125:       {
  126:         ispb = true;
  127:         pb   = c;
  128:         return c;
  129:       }
  130:     }
  131:
  132:     /// Some platforms (for example, Compaq C++) have buggy implementations of
  133:     /// xsgetn that handle null buffers incorrectly.
  134:     /// Anyway, it does not hurt to provide an optimized implementation
  135:     /// of xsgetn for this streambuf implementation.
  136:     virtual std::streamsize xsgetn( char_type* p, std::streamsize count )
  137:     {
  138:       std::streamsize copied = 0;
  139:
  140:       while ( count > 0 )
  141:       {
  142:         int_type c = uflow();
  143:         if ( c == char_traits::eof() ) break;
  144:         *p++ = char_traits::to_char_type(c);
  145:         ++copied;
  146:         --count;
  147:       }
  148:
  149:       return copied;
  150:     }
  151:
  152:   protected:
  153:     static int_type charToInt( char_type c )
  154:     {
  155:       return char_traits::to_int_type( c );
  156:     }
  157:
  158:   private:
  159:     virtual int_type readFromDevice()
  160:     {
  161:       return char_traits::eof();
  162:     }
  163:
  164:     virtual int_type writeToDevice( char_type )
  165:     {
  166:       return char_traits::eof();
  167:     }
  168:
  169:     int_type pb;
  170:     bool ispb;
  171:
  172:     BasicUnbufferedStreamBuf( const BasicUnbufferedStreamBuf& );
  173:     BasicUnbufferedStreamBuf& operator = ( const BasicUnbufferedStreamBuf& );
  174:   };
  175:
  176:
  177:   // We provide an instantiation for char
  178:   typedef BasicUnbufferedStreamBuf<char, std::char_traits<char> > UnbufferedStreamBuf;
  179:
  180:
  181: } // namespace spt
  182:
  183:
  184: #endif // SPT_UNBUFFEREDSTREAMBUF


Base64Encoder.h

Interfaces for the stream buffer, IO stream and encoder classes.

    1: /*
    2: * Base64Encoder.h
    3: *
    4: * Definition of class Base64Encoder.
    5: *
    6: * Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
    7: * and Contributors.
    8: *
    9: * Permission is hereby granted, free of charge, to any person or organization
   10: * obtaining a copy of the software and accompanying documentation covered by
   11: * this license (the "Software") to use, reproduce, display, distribute,
   12: * execute, and transmit the Software, and to prepare derivative works of the
   13: * Software, and to permit third-parties to whom the Software is furnished to
   14: * do so, all subject to the following:
   15: *
   16: * The copyright notices in the Software and this entire statement, including
   17: * the above license grant, this restriction and the following disclaimer,
   18: * must be included in all copies of the Software, in whole or in part, and
   19: * all derivative works of the Software, unless such copies or derivative
   20: * works are solely in the form of machine-executable object code generated by
   21: * a source language processor.
   22: *
   23: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   24: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   25: * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
   26: * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
   27: * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
   28: * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   29: * DEALINGS IN THE SOFTWARE.
   30: */
   31:
   32:
   33: #ifndef SPT_BASE64ENCODER
   34: #define SPT_BASE64ENCODER
   35:
   36:
   37: #include "SPT.h"
   38: #include "UnbufferedStreamBuf.h"
   39: #include "UnbufferedStreamBuf.h"
   40:
   41: #if defined( ARDUINO )
   42: #include "../StandardCplusplus/ostream"
   43: #else
   44: #include <ostream>
   45: #endif
   46:
   47:
   48: namespace spt
   49: {
   50:   /// This streambuf base64-encodes all data written
   51:   /// to it and forwards it to a connected
   52:   /// ostream.
   53:   ///
   54:   /// Note: The characters are directly written
   55:   /// to the ostream's streambuf, thus bypassing
   56:   /// the ostream. The ostream's state is therefore
   57:   /// not updated to match the buffer's state.
   58:   class Base64EncoderBuf: public UnbufferedStreamBuf
   59:   {
   60:   public:
   61:     Base64EncoderBuf(std::ostream& ostr);
   62:     ~Base64EncoderBuf();
   63:
   64:     /// Closes the stream buffer.
   65:     int close();
   66:
   67:     /// Specify the line length.
   68:     ///
   69:     /// After the given number of characters have been written,
   70:     /// a newline character will be written.
   71:     ///
   72:     /// Specify 0 for an unlimited line length.
   73:     void setLineLength( int32_t lineLength );
   74:
   75:     /// Returns the currently set line length.
   76:     int32_t getLineLength() const;
   77:
   78:   private:
   79:     int_type writeToDevice( char c );
   80:
   81:     Byte group[3];
   82:     int32_t groupLength;
   83:     int32_t pos;
   84:     int32_t lineLength;
   85:     std::streambuf& buf;
   86:
   87:     static const Byte OUT_ENCODING[64];
   88:     friend class Base64DecoderBuf;
   89:
   90:     Base64EncoderBuf( const Base64EncoderBuf& );
   91:     Base64EncoderBuf& operator = ( const Base64EncoderBuf& );
   92:   };
   93:
   94:
   95:   /// The base class for Base64Encoder.
   96:   ///
   97:   /// This class is needed to ensure the correct initialization
   98:   /// order of the stream buffer and base classes.
   99:   class Base64EncoderIOS: public virtual std::ios
  100:   {
  101:   public:
  102:     Base64EncoderIOS( std::ostream& ostr );
  103:     ~Base64EncoderIOS();
  104:
  105:     int close();
  106:     Base64EncoderBuf* rdbuf();
  107:
  108:   protected:
  109:     Base64EncoderBuf buf;
  110:
  111:   private:
  112:     Base64EncoderIOS( const Base64EncoderIOS& );
  113:     Base64EncoderIOS& operator = ( const Base64EncoderIOS& );
  114:   };
  115:
  116:
  117:   /// This ostream base64-encodes all data
  118:   /// written to it and forwards it to
  119:   /// a connected ostream.
  120:   /// Always call close() when done
  121:   /// writing data, to ensure proper
  122:   /// completion of the encoding operation.
  123:   ///
  124:   /// Note: The characters are directly written
  125:   /// to the ostream's streambuf, thus bypassing
  126:   /// the ostream. The ostream's state is therefore
  127:   /// not updated to match the buffer's state.
  128:   class Base64Encoder: public Base64EncoderIOS, public std::ostream
  129:   {
  130:   public:
  131:     Base64Encoder( std::ostream& ostr );
  132:     ~Base64Encoder();
  133:
  134:   private:
  135:     Base64Encoder( const Base64Encoder& );
  136:     Base64Encoder& operator = ( const Base64Encoder& );
  137:   };
  138:
  139:
  140: } // namespace spt
  141:
  142:
  143: #endif // SPT_BASE64ENCODER


Base64Encoder.cpp

Implementation of the base 64 stream encoder.

    1: /*
    2: * Base64Encoder.cpp
    3: *
    4: * $Id:  *poco/1.4/Foundation/src/Base64Encoder.cpp#2 $
    5: *
    6: * Library: Foundation
    7: * Package: Streams
    8: * Module:  Base64
    9: *
   10: * Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
   11: * and Contributors.
   12: *
   13: * Permission is hereby granted, free of charge, to any person or organization
   14: * obtaining a copy of the software and accompanying documentation covered by
   15: * this license (the "Software") to use, reproduce, display, distribute,
   16: * execute, and transmit the Software, and to prepare derivative works of the
   17: * Software, and to permit third-parties to whom the Software is furnished to
   18: * do so, all subject to the following:
   19: *
   20: * The copyright notices in the Software and this entire statement, including
   21: * the above license grant, this restriction and the following disclaimer,
   22: * must be included in all copies of the Software, in whole or in part, and
   23: * all derivative works of the Software, unless such copies or derivative
   24: * works are solely in the form of machine-executable object code generated by
   25: * a source language processor.
   26: *
   27: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   28: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   29: * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
   30: * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
   31: * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
   32: * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   33: * DEALINGS IN THE SOFTWARE.
   34: */
   35:
   36:
   37: #include "Base64Encoder.h"
   38:
   39:
   40: using spt::Byte;
   41: using spt::Base64Encoder;
   42: using spt::Base64EncoderBuf;
   43: using spt::Base64EncoderIOS;
   44:
   45:
   46: const Byte Base64EncoderBuf::OUT_ENCODING[64] =
   47: {
   48:         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
   49:         'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
   50:         'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
   51:         'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
   52:         'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
   53:         'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
   54:         'w', 'x', 'y', 'z', '0', '1', '2', '3',
   55:         '4', '5', '6', '7', '8', '9', '+', '/'
   56: };
   57:
   58:
   59: Base64EncoderBuf::Base64EncoderBuf(std::ostream& ostr) :
   60:   groupLength(0), pos(0),
   61:   lineLength(72), buf( *ostr.rdbuf() ) {}
   62:
   63:
   64: Base64EncoderBuf::~Base64EncoderBuf()
   65: {
   66: #if !defined( ARDUINO )
   67:         try
   68:         {
   69: #endif
   70:     close();
   71: #if !defined( ARDUINO )
   72:   }
   73:   catch (...) {}
   74: #endif
   75: }
   76:
   77:
   78: void Base64EncoderBuf::setLineLength( int32_t ll ) { lineLength = ll; }
   79:
   80:
   81: int32_t Base64EncoderBuf::getLineLength() const { return lineLength; }
   82:
   83:
   84: Base64EncoderBuf::int_type Base64EncoderBuf::writeToDevice( char c )
   85: {
   86:         static const int eof = std::char_traits<char>::eof();
   87:   group[groupLength++] = static_cast<Byte>( c );
   88:
   89:   if ( groupLength == 3 )
   90:         {
   91:     Byte idx;
   92:     idx = group[0] >> 2;
   93:     if ( buf.sputc(OUT_ENCODING[idx]) == eof ) return eof;
   94:
   95:     idx = ( ( group[0] & 0x03 ) << 4 ) | ( group[1] >> 4 );
   96:     if ( buf.sputc( OUT_ENCODING[idx] ) == eof ) return eof;
   97:
   98:     idx = ( ( group[1] & 0x0F ) << 2 ) | ( group[2] >> 6 );
   99:     if ( buf.sputc( OUT_ENCODING[idx] ) == eof ) return eof;
  100:
  101:     idx = group[2] & 0x3F;
  102:     if ( buf.sputc( OUT_ENCODING[idx] ) == eof ) return eof;
  103:
  104:     pos += 4;
  105:     if ( lineLength > 0 && pos >= lineLength )
  106:                 {
  107:       if ( buf.sputc( '\r' ) == eof ) return eof;
  108:       if ( buf.sputc( '\n' ) == eof) return eof;
  109:       pos = 0;
  110:                 }
  111:
  112:     groupLength = 0;
  113:         }
  114:
  115:   return charToInt( c );
  116: }
  117:
  118:
  119: int Base64EncoderBuf::close()
  120: {
  121:         static const int eof = std::char_traits<char>::eof();
  122:
  123:   if ( sync() == eof ) return eof;
  124:   if ( groupLength == 1 )
  125:         {
  126:     group[1] = 0;
  127:     Byte idx;
  128:     idx = group[0] >> 2;
  129:     if ( buf.sputc( OUT_ENCODING[idx] ) == eof ) return eof;
  130:
  131:     idx = ( ( group[0] & 0x03 ) << 4 ) | ( group[1] >> 4 );
  132:     if ( buf.sputc( OUT_ENCODING[idx] ) == eof ) return eof;
  133:
  134:     if ( buf.sputc( '=' ) == eof ) return eof;
  135:     if ( buf.sputc( '=' ) == eof ) return eof;
  136:         }
  137:   else if ( groupLength == 2 )
  138:         {
  139:     group[2] = 0;
  140:     Byte idx;
  141:     idx = group[0] >> 2;
  142:     if ( buf.sputc( OUT_ENCODING[idx] ) == eof ) return eof;
  143:
  144:     idx = ( (group[0] & 0x03) << 4 ) | ( group[1] >> 4 );
  145:     if ( buf.sputc( OUT_ENCODING[idx] ) == eof ) return eof;
  146:
  147:     idx = ( ( group[1] & 0x0F ) << 2 ) | ( group[2] >> 6 );
  148:     if ( buf.sputc( OUT_ENCODING[idx] ) == eof ) return eof;
  149:
  150:     if ( buf.sputc( '=' ) == eof ) return eof;
  151:         }
  152:
  153:   groupLength = 0;
  154:   return buf.pubsync();
  155: }
  156:
  157:
  158: Base64EncoderIOS::Base64EncoderIOS( std::ostream& ostr ): buf( ostr )
  159: {
  160:   init( &buf );
  161: }
  162:
  163:
  164: Base64EncoderIOS::~Base64EncoderIOS() {}
  165:
  166:
  167: int Base64EncoderIOS::close() { return buf.close(); }
  168:
  169:
  170: Base64EncoderBuf* Base64EncoderIOS::rdbuf() { return &buf; }
  171:
  172:
  173: Base64Encoder::Base64Encoder( std::ostream& ostr ) :
  174:     Base64EncoderIOS( ostr ), std::ostream( &buf ) {}
  175:
  176:
  177: Base64Encoder::~Base64Encoder() {}