diff --git a/kmime/kmime_codecs.cpp b/kmime/kmime_codecs.cpp index 72f4cbb89..9953180ba 100644 --- a/kmime/kmime_codecs.cpp +++ b/kmime/kmime_codecs.cpp @@ -1,241 +1,221 @@ /* -*- c++ -*- - kmime_codecs.cpp This file is part of KMime, the KDE internet mail/usenet news message library. + Copyright (c) 2001-2002 Marc Mutz KMime is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. KMime is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA In addition, as a special exception, the copyright holders give permission to link the code of this library with any edition of the Qt library by Trolltech AS, Norway (or with modified versions of Qt that use the same license as Qt), and distribute linked combinations including the two. You must obey the GNU General Public License in all respects for all of the code used other than Qt. If you modify this file, you may extend this exception to your version of the file, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ #include "kmime_codecs.h" #include "kmime_util.h" #include "kmime_codec_base64.h" #include "kmime_codec_qp.h" #include "kmime_codec_uuencode.h" #include "kmime_codec_identity.h" -#include +#include "kautodeletehash.h" -#include +#include +#include #include #include #include using namespace KMime; +using namespace KPIM; namespace KMime { // global list of KMime::Codec's -Q3AsciiDict* Codec::all = 0; -static KStaticDeleter > sdAll; +KAutoDeleteHash * Codec::all = 0; +static KStaticDeleter< KAutoDeleteHash > sdAll; #if defined(QT_THREAD_SUPPORT) QMutex* Codec::dictLock = 0; static KStaticDeleter sdDictLock; #endif void Codec::fillDictionary() { - - all->setAutoDelete(true); - //all->insert( "7bit", new SevenBitCodec() ); //all->insert( "8bit", new EightBitCodec() ); all->insert( "base64", new Base64Codec() ); all->insert( "quoted-printable", new QuotedPrintableCodec() ); all->insert( "b", new Rfc2047BEncodingCodec() ); all->insert( "q", new Rfc2047QEncodingCodec() ); all->insert( "x-kmime-rfc2231", new Rfc2231EncodingCodec() ); all->insert( "x-uuencode", new UUCodec() ); //all->insert( "binary", new BinaryCodec() ); } Codec * Codec::codecForName( const char * name ) { + const QByteArray ba( name ); + return codecForName( ba ); +} + +Codec * Codec::codecForName( const QByteArray & name ) { #if defined(QT_THREAD_SUPPORT) if ( !dictLock ) sdDictLock.setObject( dictLock, new QMutex ); dictLock->lock(); // protect "all" #endif if ( !all ) { - sdAll.setObject( all, new Q3AsciiDict( 11, false /* case-insensitive */) ); + sdAll.setObject( all, new KAutoDeleteHash() ); fillDictionary(); } + QByteArray lowerName = name; + KPIM::kAsciiToLower( lowerName.data() ); Codec * codec = (*all)[ name ]; #if defined(QT_THREAD_SUPPORT) dictLock->unlock(); #endif if ( !codec ) kdDebug() << "Unknown codec \"" << name << "\" requested!" << endl; return codec; } -Codec * Codec::codecForName( const Q3CString & name ) { - return codecForName( name.data() ); -} - bool Codec::encode( const char* & scursor, const char * const send, - char* & dcursor, const char * const dend, - bool withCRLF ) const + char* & dcursor, const char * const dend, + bool withCRLF ) const { // get an encoder: Encoder * enc = makeEncoder( withCRLF ); assert( enc ); // encode and check for output buffer overflow: while ( !enc->encode( scursor, send, dcursor, dend ) ) if ( dcursor == dend ) { delete enc; return false; // not enough space in output buffer } // finish and check for output buffer overflow: while ( !enc->finish( dcursor, dend ) ) if ( dcursor == dend ) { delete enc; return false; // not enough space in output buffer } // cleanup and return: delete enc; return true; // successfully encoded. } QByteArray Codec::encode( const QByteArray & src, bool withCRLF ) const { // allocate buffer for the worst case: QByteArray result( maxEncodedSizeFor( src.size(), withCRLF ) ); // set up iterators: QByteArray::ConstIterator iit = src.begin(); QByteArray::ConstIterator iend = src.end(); QByteArray::Iterator oit = result.begin(); QByteArray::ConstIterator oend = result.end(); // encode if ( !encode( iit, iend, oit, oend, withCRLF ) ) kdFatal() << name() << " codec lies about it's mEncodedSizeFor()" - << endl; - - // shrink result to actual size: - result.truncate( oit - result.begin() ); - - return result; -} - -Q3CString Codec::encodeToQCString( const QByteArray & src, bool withCRLF ) const -{ - // allocate buffer for the worst case (remember to add one for the trailing NUL) - Q3CString result( maxEncodedSizeFor( src.size(), withCRLF ) + 1 ); - - // set up iterators: - QByteArray::ConstIterator iit = src.begin(); - QByteArray::ConstIterator iend = src.end(); - QByteArray::Iterator oit = result.begin(); - QByteArray::ConstIterator oend = result.end() - 1; - - // encode - if ( !encode( iit, iend, oit, oend, withCRLF ) ) - kdFatal() << name() << " codec lies about it's mEncodedSizeFor()" - << endl; + << endl; // shrink result to actual size: result.truncate( oit - result.begin() ); return result; } QByteArray Codec::decode( const QByteArray & src, bool withCRLF ) const { // allocate buffer for the worst case: QByteArray result( maxDecodedSizeFor( src.size(), withCRLF ) ); // set up iterators: QByteArray::ConstIterator iit = src.begin(); QByteArray::ConstIterator iend = src.end(); QByteArray::Iterator oit = result.begin(); QByteArray::ConstIterator oend = result.end(); // decode if ( !decode( iit, iend, oit, oend, withCRLF ) ) kdFatal() << name() << " codec lies about it's maxDecodedSizeFor()" - << endl; + << endl; // shrink result to actual size: result.truncate( oit - result.begin() ); return result; } bool Codec::decode( const char* & scursor, const char * const send, - char* & dcursor, const char * const dend, - bool withCRLF ) const + char* & dcursor, const char * const dend, + bool withCRLF ) const { // get a decoder: Decoder * dec = makeDecoder( withCRLF ); assert( dec ); // decode and check for output buffer overflow: while ( !dec->decode( scursor, send, dcursor, dend ) ) if ( dcursor == dend ) { delete dec; return false; // not enough space in output buffer } // finish and check for output buffer overflow: while ( !dec->finish( dcursor, dend ) ) if ( dcursor == dend ) { delete dec; return false; // not enough space in output buffer } // cleanup and return: delete dec; return true; // successfully encoded. } // write as much as possible off the output buffer. Return true if // flushing was complete, false if some chars could not be flushed. bool Encoder::flushOutputBuffer( char* & dcursor, const char * const dend ) { int i; // copy output buffer to output stream: for ( i = 0 ; dcursor != dend && i < mOutputBufferCursor ; ++i ) *dcursor++ = mOutputBuffer[i]; // calculate the number of missing chars: int numCharsLeft = mOutputBufferCursor - i; // push the remaining chars to the begin of the buffer: if ( numCharsLeft ) qmemmove( mOutputBuffer, mOutputBuffer + i, numCharsLeft ); // adjust cursor: mOutputBufferCursor = numCharsLeft; return !numCharsLeft; } } // namespace KMime diff --git a/kmime/kmime_codecs.h b/kmime/kmime_codecs.h index 54a093c75..e40d89d48 100644 --- a/kmime/kmime_codecs.h +++ b/kmime/kmime_codecs.h @@ -1,367 +1,359 @@ /* -*- c++ -*- - kmime_codecs.h This file is part of KMime, the KDE internet mail/usenet news message library. + Copyright (c) 2001-2002 Marc Mutz KMime is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. KMime is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA In addition, as a special exception, the copyright holders give permission to link the code of this library with any edition of the Qt library by Trolltech AS, Norway (or with modified versions of Qt that use the same license as Qt), and distribute linked combinations including the two. You must obey the GNU General Public License in all respects for all of the code used other than Qt. If you modify this file, you may extend this exception to your version of the file, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ #ifndef __KMIME_CODECS__ #define __KMIME_CODECS__ -#include #if defined(QT_THREAD_SUPPORT) # include #endif -#include // QByteArray +//#include +#include #include // for kdFatal() #include +namespace KPIM { + template class KAutoDeleteHash; +} +using namespace KPIM; + namespace KMime { // forward declarations: class Encoder; class Decoder; /** Abstract base class of codecs like base64 and quoted-printable. It's a singleton. @short Codecs for common mail transfer encodings. @author Marc Mutz */ class KDE_EXPORT Codec { protected: - static Q3AsciiDict* all; + static KAutoDeleteHash * all; #if defined(QT_THREAD_SUPPORT) static QMutex* dictLock; #endif Codec() {} private: static void fillDictionary(); public: static Codec * codecForName( const char * name ); - static Codec * codecForName( const Q3CString & name ); + static Codec * codecForName( const QByteArray & name ); virtual int maxEncodedSizeFor( int insize, bool withCRLF=false ) const = 0; virtual int maxDecodedSizeFor( int insize, bool withCRLF=false ) const = 0; virtual Encoder * makeEncoder( bool withCRLF=false ) const = 0; virtual Decoder * makeDecoder( bool withCRLF=false ) const = 0; /** * Convenience wrapper that can be used for small chunks of data * when you can provide a large enough buffer. The default * implementation creates an Encoder and uses it. * * Encodes a chunk of bytes starting at @p scursor and extending to * @p send into the buffer described by @p dcursor and @p dend. * * This function doesn't support chaining of blocks. The returned * block cannot be added to, but you don't need to finalize it, too. * * Example usage (@p in contains the input data): *
    * KMime::Codec * codec = KMime::Codec::codecForName( "base64" );
    * kdFatal( !codec ) << "no base64 codec found!?" << endl;
    * QByteArray out( in.size()*1.4 ); // crude maximal size of b64 encoding
    * QByteArray::Iterator iit = in.begin();
    * QByteArray::Iterator oit = out.begin();
    * if ( !codec->encode( iit, in.end(), oit, out.end() ) ) {
    *   kdDebug() << "output buffer too small" << endl;
    *   return;
    * }
    * kdDebug() << "Size of encoded data: " << oit - out.begin() << endl;
    * 
* * @param scursor/send begin and end of input buffer * @param dcursor/dend begin and end of output buffer * @param withCRLF If true, make the lineends CRLF, else make them LF only. * * @return false if the encoded data didn't fit into the output * buffer. **/ virtual bool encode( const char* & scursor, const char * const send, - char* & dcursor, const char * const dend, - bool withCRLF=false ) const; + char* & dcursor, const char * const dend, + bool withCRLF=false ) const; /** * Convenience wrapper that can be used for small chunks of data * when you can provide a large enough buffer. The default * implementation creates a Decoder and uses it. * * Decodes a chunk of bytes starting at @p scursor and extending to * @p send into the buffer described by @p dcursor and @p dend. * * This function doesn't support chaining of blocks. The returned * block cannot be added to, but you don't need to finalize it, too. * * Example usage (@p in contains the input data): *
    * KMime::Codec * codec = KMime::Codec::codecForName( "base64" );
    * kdFatal( !codec ) << "no base64 codec found!?" << endl;
    * QByteArray out( in.size() ); // good guess for any encoding...
    * QByteArray::Iterator iit = in.begin();
    * QByteArray::Iterator oit = out.begin();
    * if ( !codec->decode( iit, in.end(), oit, out.end() ) ) {
    *   kdDebug() << "output buffer too small" << endl;
    *   return;
    * }
    * kdDebug() << "Size of decoded data: " << oit - out.begin() << endl;
    * 
* * @param scursor/send begin and end of input buffer * @param dcursor/dend begin and end of output buffer * @param withCRLF If true, make the lineends CRLF, else make them LF only. * * @return false if the decoded data didn't fit into the output * buffer. **/ virtual bool decode( const char* & scursor, const char * const send, - char* & dcursor, const char * const dend, - bool withCRLF=false ) const; + char* & dcursor, const char * const dend, + bool withCRLF=false ) const; /** * Even more convenient, but also a bit slower and more memory * intensive, since it allocates storage for the worst case and then * shrinks the result QByteArray to the actual size again. * * For use with small @p src. **/ virtual QByteArray encode( const QByteArray & src, bool withCRLF=false ) const; - /** - * Even more convenient, but also a bit slower and more memory - * intensive, since it allocates storage for the worst case and then - * shrinks the result QCString to the actual size again. - * - * For use with small @p src. - * - * This method only works for codecs whose output is in the 8bit - * domain (ie. not in the binary domain). Codecs that do not fall - * into this category will return a null QCString. - **/ - virtual Q3CString encodeToQCString( const QByteArray & src, bool withCRLF=false ) const; - /** * Even more convenient, but also a bit slower and more memory * intensive, since it allocates storage for the worst case and then * shrinks the result QByteArray to the actual size again. * * For use with small @p src. **/ virtual QByteArray decode( const QByteArray & src, bool withCRLF=false ) const; /** * @return the name of the encoding. Guaranteed to be lowercase. */ virtual const char * name() const = 0; virtual ~Codec() {} }; /** * Stateful decoder class, modelled after QTextDecoder. * * @section Overview * * KMime decoders are designed to be able to process encoded data in * chunks of arbitrary size and to work with output buffers of also * arbitrary size. They maintain any state necessary to go on where * the previous call left off. * * The class consists of only two methods of interest: see decode, * which decodes an input block and finalize, which flushes any * remaining data to the output stream. * * Typically, you will create a decoder instance, call decode as * often as necessary, then call finalize (most often a single * call suffices, but it might be that during that call the output * buffer is filled, so you should be prepared to call finalize * as often as necessary, ie. until it returns @p true). * * @section Return Values * * Both methods return @p true to indicate that they've finished their * job. For decode, a return value of @p true means that the * current input block has been finished (@p false most often means * that the output buffer is full, but that isn't required * behavior. The decode call is free to return at arbitrary * times during processing). * * For finalize, a return value of @p true means that all data * implicitly or explicitly stored in the decoder instance has been * flushed to the output buffer. A @p false return value should be * interpreted as "check if the output buffer is full and call me * again", just as with decode. * * @section Usage Pattern * * Since the decoder maintains state, you can only use it once. After * a sequence of input blocks has been processed, you finalize * the output and then delete the decoder instance. If you want to * process another input block sequence, you create a new instance. * * Typical usage (@p in contains the (base64-encoded) input data), * taking into account all the conventions detailed above: * *
  * KMime::Codec * codec = KMime::Codec::codecForName( "base64" );
  * kdFatal( !codec ) << "No codec found for base64!" << endl;
  * KMime::Decoder * dec = codec->makeDecoder();
  * assert( dec ); // should not happen
  * QByteArray out( 256 ); // small buffer is enough ;-)
  * QByteArray::Iterator iit = in.begin();
  * QByteArray::Iterator oit = out.begin();
  * // decode the chunk
  * while ( !dec->decode( iit, in.end(), oit, out.end() ) )
  *   if ( oit == out.end() ) { // output buffer full, process contents
  *     do_something_with( out );
  *     oit = out.begin();
  *   }
  * // repeat while loop for each input block
  * // ...
  * // finish (flush remaining data from decoder):
  * while ( !dec->finish( oit, out.end() ) )
  *   if ( oit == out.end() ) { // output buffer full, process contents
  *     do_something_with( out );
  *     oit = out.begin();
  *   }
  * // now process last chunk:
  * out.resize( oit - out.begin() );
  * do_something_with( out );
  * // _delete_ the decoder, but not the codec:
  * delete dec;
  * 
* * @short Stateful CTE decoder class * @author Marc Mutz **/ class Decoder { protected: friend class Codec; /** * Protected constructor. Use KMime::Codec::makeDecoder to * create an instance. The bool parameter determines whether lines * end with CRLF (true) or LF (false, default). **/ Decoder( bool withCRLF=false ) : mWithCRLF( withCRLF ) {} public: virtual ~Decoder() {} /** Decode a chunk of data, maintaining state information between * calls. See class decumentation for calling conventions. **/ virtual bool decode( const char* & scursor, const char * const send, - char* & dcursor, const char * const dend ) = 0; + char* & dcursor, const char * const dend ) = 0; /** Call this method to finalize the output stream. Writes all * remaining data and resets the decoder. See KMime::Codec for * calling conventions. **/ virtual bool finish( char* & dcursor, const char * const dend ) = 0; protected: const bool mWithCRLF; }; /** Stateful encoder class, modelled after QTextEncoder. @short Stateful encoder class @author Marc Mutz */ class Encoder { protected: friend class Codec; /** Protected constructor. Use KMime::Codec::makeEncoder if you want one. The bool parameter determines whether lines end with CRLF (true) or LF (false, default). */ Encoder( bool withCRLF=false ) : mOutputBufferCursor( 0 ), mWithCRLF( withCRLF ) {} public: virtual ~Encoder() {} /** Encode a chunk of data, maintaining state information between calls. See KMime::Codec for calling conventions. */ virtual bool encode( const char* & scursor, const char * const send, - char* & dcursor, const char * const dend ) = 0; + char* & dcursor, const char * const dend ) = 0; /** Call this method to finalize the output stream. Writes all remaining data and resets the encoder. See KMime::Codec for calling conventions. */ virtual bool finish( char* & dcursor, const char * const dend ) = 0; protected: /** Space in the output buffer */ enum { maxBufferedChars = 8 }; /** Writes @p ch to the output stream or the output buffer, depending on whether or not the output stream has space left. @return true if written to the output stream, false if buffered. */ bool write( char ch, char* & dcursor, const char * const dend ) { if ( dcursor != dend ) { // if there's space in the output stream, write there: *dcursor++ = ch; return true; } else { // else buffer the output: kdFatal( mOutputBufferCursor >= maxBufferedChars ) - << "KMime::Encoder: internal buffer overflow!" << endl; + << "KMime::Encoder: internal buffer overflow!" << endl; mOutputBuffer[ mOutputBufferCursor++ ] = ch; return false; } } /** Writes characters from the output buffer to the output stream. Implementations of encode and finish should call this at the very beginning and for each iteration of the while loop. @return true if all chars could be written, false otherwise */ bool flushOutputBuffer( char* & dcursor, const char * const dend ); /** Convenience function. Outputs LF or CRLF, based on the state of mWithCRLF */ bool writeCRLF( char* & dcursor, const char * const dend ) { if ( mWithCRLF ) write( '\r', dcursor, dend ); return write( '\n', dcursor, dend ); } private: /** An output buffer to simplyfy some codecs. Use with write and flushOutputBuffer */ char mOutputBuffer[ maxBufferedChars ]; protected: uchar mOutputBufferCursor; const bool mWithCRLF; }; } // namespace KMime #endif // __KMIME_CODECS__