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