00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "kmime_codecs.h"
00033 #include "kmime_util.h"
00034
00035 #include "kmime_codec_base64.h"
00036 #include "kmime_codec_qp.h"
00037 #include "kmime_codec_uuencode.h"
00038 #include "kmime_codec_identity.h"
00039
00040 #include <kdebug.h>
00041
00042 #include <qcstring.h>
00043 #include <kstaticdeleter.h>
00044
00045 #include <cassert>
00046 #include <cstring>
00047
00048 using namespace KMime;
00049
00050 namespace KMime {
00051
00052
00053 QAsciiDict<Codec>* Codec::all = 0;
00054 static KStaticDeleter<QAsciiDict<Codec> > sdAll;
00055 #if defined(QT_THREAD_SUPPORT)
00056 QMutex* Codec::dictLock = 0;
00057 static KStaticDeleter<QMutex> sdDictLock;
00058 #endif
00059
00060 void Codec::fillDictionary() {
00061
00062 all->setAutoDelete(true);
00063
00064
00065
00066 all->insert( "base64", new Base64Codec() );
00067 all->insert( "quoted-printable", new QuotedPrintableCodec() );
00068 all->insert( "b", new Rfc2047BEncodingCodec() );
00069 all->insert( "q", new Rfc2047QEncodingCodec() );
00070 all->insert( "x-kmime-rfc2231", new Rfc2231EncodingCodec() );
00071 all->insert( "x-uuencode", new UUCodec() );
00072
00073
00074 }
00075
00076 Codec * Codec::codecForName( const char * name ) {
00077 #if defined(QT_THREAD_SUPPORT)
00078 if ( !dictLock )
00079 sdDictLock.setObject( dictLock, new QMutex );
00080 dictLock->lock();
00081 #endif
00082 if ( !all ) {
00083 sdAll.setObject( all, new QAsciiDict<Codec>( 11, false ) );
00084 fillDictionary();
00085 }
00086 Codec * codec = (*all)[ name ];
00087 #if defined(QT_THREAD_SUPPORT)
00088 dictLock->unlock();
00089 #endif
00090
00091 if ( !codec )
00092 kdDebug() << "Unknown codec \"" << name << "\" requested!" << endl;
00093
00094 return codec;
00095 }
00096
00097 Codec * Codec::codecForName( const QCString & name ) {
00098 return codecForName( name.data() );
00099 }
00100
00101 bool Codec::encode( const char* & scursor, const char * const send,
00102 char* & dcursor, const char * const dend,
00103 bool withCRLF ) const
00104 {
00105
00106 Encoder * enc = makeEncoder( withCRLF );
00107 assert( enc );
00108
00109
00110 while ( !enc->encode( scursor, send, dcursor, dend ) )
00111 if ( dcursor == dend ) {
00112 delete enc;
00113 return false;
00114 }
00115
00116
00117 while ( !enc->finish( dcursor, dend ) )
00118 if ( dcursor == dend ) {
00119 delete enc;
00120 return false;
00121 }
00122
00123
00124 delete enc;
00125 return true;
00126 }
00127
00128 QByteArray Codec::encode( const QByteArray & src, bool withCRLF ) const
00129 {
00130
00131 QByteArray result( maxEncodedSizeFor( src.size(), withCRLF ) );
00132
00133
00134 QByteArray::ConstIterator iit = src.begin();
00135 QByteArray::ConstIterator iend = src.end();
00136 QByteArray::Iterator oit = result.begin();
00137 QByteArray::ConstIterator oend = result.end();
00138
00139
00140 if ( !encode( iit, iend, oit, oend, withCRLF ) )
00141 kdFatal() << name() << " codec lies about it's mEncodedSizeFor()"
00142 << endl;
00143
00144
00145 result.truncate( oit - result.begin() );
00146
00147 return result;
00148 }
00149
00150 QCString Codec::encodeToQCString( const QByteArray & src, bool withCRLF ) const
00151 {
00152
00153 QCString result( maxEncodedSizeFor( src.size(), withCRLF ) + 1 );
00154
00155
00156 QByteArray::ConstIterator iit = src.begin();
00157 QByteArray::ConstIterator iend = src.end();
00158 QByteArray::Iterator oit = result.begin();
00159 QByteArray::ConstIterator oend = result.end() - 1;
00160
00161
00162 if ( !encode( iit, iend, oit, oend, withCRLF ) )
00163 kdFatal() << name() << " codec lies about it's mEncodedSizeFor()"
00164 << endl;
00165
00166
00167 result.truncate( oit - result.begin() );
00168
00169 return result;
00170 }
00171
00172 QByteArray Codec::decode( const QByteArray & src, bool withCRLF ) const
00173 {
00174
00175 QByteArray result( maxDecodedSizeFor( src.size(), withCRLF ) );
00176
00177
00178 QByteArray::ConstIterator iit = src.begin();
00179 QByteArray::ConstIterator iend = src.end();
00180 QByteArray::Iterator oit = result.begin();
00181 QByteArray::ConstIterator oend = result.end();
00182
00183
00184 if ( !decode( iit, iend, oit, oend, withCRLF ) )
00185 kdFatal() << name() << " codec lies about it's maxDecodedSizeFor()"
00186 << endl;
00187
00188
00189 result.truncate( oit - result.begin() );
00190
00191 return result;
00192 }
00193
00194 bool Codec::decode( const char* & scursor, const char * const send,
00195 char* & dcursor, const char * const dend,
00196 bool withCRLF ) const
00197 {
00198
00199 Decoder * dec = makeDecoder( withCRLF );
00200 assert( dec );
00201
00202
00203 while ( !dec->decode( scursor, send, dcursor, dend ) )
00204 if ( dcursor == dend ) {
00205 delete dec;
00206 return false;
00207 }
00208
00209
00210 while ( !dec->finish( dcursor, dend ) )
00211 if ( dcursor == dend ) {
00212 delete dec;
00213 return false;
00214 }
00215
00216
00217 delete dec;
00218 return true;
00219 }
00220
00221
00222
00223 bool Encoder::flushOutputBuffer( char* & dcursor, const char * const dend ) {
00224 int i;
00225
00226 for ( i = 0 ; dcursor != dend && i < mOutputBufferCursor ; ++i )
00227 *dcursor++ = mOutputBuffer[i];
00228
00229
00230 int numCharsLeft = mOutputBufferCursor - i;
00231
00232 if ( numCharsLeft )
00233 qmemmove( mOutputBuffer, mOutputBuffer + i, numCharsLeft );
00234
00235 mOutputBufferCursor = numCharsLeft;
00236
00237 return !numCharsLeft;
00238 }
00239
00240
00241 }