libkmime

kmime_codecs.h
1 /*
2  kmime_codecs.h
3 
4  This file is part of KMime, the KDE internet mail/usenet news message library.
5  Copyright (c) 2001-2002 Marc Mutz <mutz@kde.org>
6 
7  KMime is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License, version 2, as
9  published by the Free Software Foundation.
10 
11  KMime is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 
20  In addition, as a special exception, the copyright holders give
21  permission to link the code of this library with any edition of
22  the TQt library by Trolltech AS, Norway (or with modified versions
23  of TQt that use the same license as TQt), and distribute linked
24  combinations including the two. You must obey the GNU General
25  Public License in all respects for all of the code used other than
26  TQt. If you modify this file, you may extend this exception to
27  your version of the file, but you are not obligated to do so. If
28  you do not wish to do so, delete this exception statement from
29  your version.
30 */
31 
32 #ifndef __KMIME_CODECS__
33 #define __KMIME_CODECS__
34 
35 #include <tqasciidict.h>
36 #if defined(TQT_THREAD_SUPPORT)
37 # include <tqmutex.h>
38 #endif
39 
40 #include <tqcstring.h> // TQByteArray
41 
42 #include <kdebug.h> // for kdFatal()
43 #include <tdemacros.h>
44 
45 namespace KMime {
46 
47 // forward declarations:
48 class Encoder;
49 class Decoder;
50 
57 class TDE_EXPORT Codec {
58 protected:
59 
60  static TQAsciiDict<Codec>* all;
61 #if defined(TQT_THREAD_SUPPORT)
62  static TQMutex* dictLock;
63 #endif
64 
65  Codec() {}
66 private:
67  static void fillDictionary();
68 
69 public:
70  static Codec * codecForName( const char * name );
71  static Codec * codecForName( const TQCString & name );
72 
73  virtual int maxEncodedSizeFor( int insize, bool withCRLF=false ) const = 0;
74  virtual int maxDecodedSizeFor( int insize, bool withCRLF=false ) const = 0;
75 
76  virtual Encoder * makeEncoder( bool withCRLF=false ) const = 0;
77  virtual Decoder * makeDecoder( bool withCRLF=false ) const = 0;
78 
111  virtual bool encode( const char* & scursor, const char * const send,
112  char* & dcursor, const char * const dend,
113  bool withCRLF=false ) const;
114 
147  virtual bool decode( const char* & scursor, const char * const send,
148  char* & dcursor, const char * const dend,
149  bool withCRLF=false ) const;
150 
158  virtual TQByteArray encode( const TQByteArray & src, bool withCRLF=false ) const;
159 
171  virtual TQCString encodeToTQCString( const TQByteArray & src, bool withCRLF=false ) const;
172 
180  virtual TQByteArray decode( const TQByteArray & src, bool withCRLF=false ) const;
181 
185  virtual const char * name() const = 0;
186 
187  virtual ~Codec() {}
188 
189 };
190 
268 class Decoder {
269 protected:
270  friend class Codec;
276  Decoder( bool withCRLF=false )
277  : mWithCRLF( withCRLF ) {}
278 public:
279  virtual ~Decoder() {}
280 
284  virtual bool decode( const char* & scursor, const char * const send,
285  char* & dcursor, const char * const dend ) = 0;
290  virtual bool finish( char* & dcursor, const char * const dend ) = 0;
291 
292 protected:
293  const bool mWithCRLF;
294 };
295 
300 class Encoder {
301 protected:
302  friend class Codec;
306  Encoder( bool withCRLF=false )
307  : mOutputBufferCursor( 0 ), mWithCRLF( withCRLF ) {}
308 public:
309  virtual ~Encoder() {}
310 
313  virtual bool encode( const char* & scursor, const char * const send,
314  char* & dcursor, const char * const dend ) = 0;
315 
319  virtual bool finish( char* & dcursor, const char * const dend ) = 0;
320 
321 protected:
323  enum { maxBufferedChars = 8 };
324 
328  bool write( char ch, char* & dcursor, const char * const dend ) {
329  if ( dcursor != dend ) {
330  // if there's space in the output stream, write there:
331  *dcursor++ = ch;
332  return true;
333  } else {
334  // else buffer the output:
335  kdFatal( mOutputBufferCursor >= maxBufferedChars )
336  << "KMime::Encoder: internal buffer overflow!" << endl;
337  mOutputBuffer[ mOutputBufferCursor++ ] = ch;
338  return false;
339  }
340  }
341 
346  bool flushOutputBuffer( char* & dcursor, const char * const dend );
347 
350  bool writeCRLF( char* & dcursor, const char * const dend ) {
351  if ( mWithCRLF )
352  write( '\r', dcursor, dend );
353  return write( '\n', dcursor, dend );
354  }
355 
356 private:
359  char mOutputBuffer[ maxBufferedChars ];
360 protected:
361  uchar mOutputBufferCursor;
362  const bool mWithCRLF;
363 };
364 
365 } // namespace KMime
366 
367 #endif // __KMIME_CODECS__
Abstract base class of codecs like base64 and quoted-printable.
Definition: kmime_codecs.h:57
virtual const char * name() const =0
Stateful decoder class, modelled after TQTextDecoder.
Definition: kmime_codecs.h:268
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.
Decoder(bool withCRLF=false)
Protected constructor.
Definition: kmime_codecs.h:276
Stateful encoder class, modelled after TQTextEncoder.
Definition: kmime_codecs.h:300
virtual bool finish(char *&dcursor, const char *const dend)=0
Call this method to finalize the output stream.
Encoder(bool withCRLF=false)
Protected constructor.
Definition: kmime_codecs.h:306
bool write(char ch, char *&dcursor, const char *const dend)
Writes ch to the output stream or the output buffer, depending on whether or not the output stream ha...
Definition: kmime_codecs.h:328
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 writeCRLF(char *&dcursor, const char *const dend)
Convenience function.
Definition: kmime_codecs.h:350
bool flushOutputBuffer(char *&dcursor, const char *const dend)
Writes characters from the output buffer to the output stream.