32 #include "kmime_codecs.h"
33 #include "kmime_util.h"
35 #include "kmime_codec_base64.h"
36 #include "kmime_codec_qp.h"
37 #include "kmime_codec_uuencode.h"
38 #include "kmime_codec_identity.h"
42 #include <tqcstring.h>
43 #include <kstaticdeleter.h>
48 using namespace KMime;
53 TQAsciiDict<Codec>* Codec::all = 0;
54 static KStaticDeleter<TQAsciiDict<Codec> > sdAll;
55 #if defined(TQT_THREAD_SUPPORT)
56 TQMutex* Codec::dictLock = 0;
57 static KStaticDeleter<TQMutex> sdDictLock;
60 void Codec::fillDictionary() {
62 all->setAutoDelete(
true);
66 all->insert(
"base64",
new Base64Codec() );
67 all->insert(
"quoted-printable",
new QuotedPrintableCodec() );
68 all->insert(
"b",
new Rfc2047BEncodingCodec() );
69 all->insert(
"q",
new Rfc2047TQEncodingCodec() );
70 all->insert(
"x-kmime-rfc2231",
new Rfc2231EncodingCodec() );
71 all->insert(
"x-uuencode",
new UUCodec() );
76 Codec * Codec::codecForName(
const char * name ) {
77 #if defined(TQT_THREAD_SUPPORT)
79 sdDictLock.setObject( dictLock,
new TQMutex );
83 sdAll.setObject( all,
new TQAsciiDict<Codec>( 11,
false ) );
87 #if defined(TQT_THREAD_SUPPORT)
92 kdDebug() <<
"Unknown codec \"" <<
name <<
"\" requested!" << endl;
97 Codec * Codec::codecForName(
const TQCString & name ) {
98 return codecForName(
name.data() );
102 char* & dcursor,
const char *
const dend,
103 bool withCRLF )
const
106 Encoder * enc = makeEncoder( withCRLF );
110 while ( !enc->
encode( scursor, send, dcursor, dend ) )
111 if ( dcursor == dend ) {
117 while ( !enc->
finish( dcursor, dend ) )
118 if ( dcursor == dend ) {
131 TQByteArray result( maxEncodedSizeFor( src.size(), withCRLF ) );
134 TQByteArray::ConstIterator iit = src.begin();
135 TQByteArray::ConstIterator iend = src.end();
136 TQByteArray::Iterator oit = result.begin();
137 TQByteArray::ConstIterator oend = result.end();
140 if ( !
encode( iit, iend, oit, oend, withCRLF ) )
141 kdFatal() <<
name() <<
" codec lies about it's mEncodedSizeFor()"
145 result.truncate( oit - result.begin() );
153 TQCString result( maxEncodedSizeFor( src.size(), withCRLF ) + 1 );
156 TQByteArray::ConstIterator iit = src.begin();
157 TQByteArray::ConstIterator iend = src.end();
158 TQByteArray::Iterator oit = result.begin();
159 TQByteArray::ConstIterator oend = result.end() - 1;
162 if ( !
encode( iit, iend, oit, oend, withCRLF ) )
163 kdFatal() <<
name() <<
" codec lies about it's mEncodedSizeFor()"
167 result.truncate( oit - result.begin() );
175 TQByteArray result( maxDecodedSizeFor( src.size(), withCRLF ) );
178 TQByteArray::ConstIterator iit = src.begin();
179 TQByteArray::ConstIterator iend = src.end();
180 TQByteArray::Iterator oit = result.begin();
181 TQByteArray::ConstIterator oend = result.end();
184 if ( !
decode( iit, iend, oit, oend, withCRLF ) )
185 kdFatal() <<
name() <<
" codec lies about it's maxDecodedSizeFor()"
189 result.truncate( oit - result.begin() );
195 char* & dcursor,
const char *
const dend,
196 bool withCRLF )
const
199 Decoder * dec = makeDecoder( withCRLF );
203 while ( !dec->
decode( scursor, send, dcursor, dend ) )
204 if ( dcursor == dend ) {
210 while ( !dec->
finish( dcursor, dend ) )
211 if ( dcursor == dend ) {
226 for ( i = 0 ; dcursor != dend && i < mOutputBufferCursor ; ++i )
227 *dcursor++ = mOutputBuffer[i];
230 int numCharsLeft = mOutputBufferCursor - i;
233 tqmemmove( mOutputBuffer, mOutputBuffer + i, numCharsLeft );
235 mOutputBufferCursor = numCharsLeft;
237 return !numCharsLeft;
Abstract base class of codecs like base64 and quoted-printable.
virtual const char * name() const =0
virtual TQCString encodeToTQCString(const TQByteArray &src, bool withCRLF=false) const
Even more convenient, but also a bit slower and more memory intensive, since it allocates storage for...
virtual bool decode(const char *&scursor, const char *const send, 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 buf...
virtual bool encode(const char *&scursor, const char *const send, 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 buf...
Stateful decoder class, modelled after TQTextDecoder.
virtual bool decode(const char *&scursor, const char *const send, char *&dcursor, const char *const dend)=0
Decode a chunk of data, maintaining state information between calls.
virtual bool finish(char *&dcursor, const char *const dend)=0
Call this method to finalize the output stream.
Stateful encoder class, modelled after TQTextEncoder.
virtual bool finish(char *&dcursor, const char *const dend)=0
Call this method to finalize the output stream.
virtual bool encode(const char *&scursor, const char *const send, char *&dcursor, const char *const dend)=0
Encode a chunk of data, maintaining state information between calls.
bool flushOutputBuffer(char *&dcursor, const char *const dend)
Writes characters from the output buffer to the output stream.