libkmime

kmime_codec_base64.h
1 /*
2  kmime_codec_base64.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_CODEC_BASE64__
33 #define __KMIME_CODEC_BASE64__
34 
35 #include "kmime_codecs.h"
36 
37 namespace KMime {
38 
39 class Base64Codec : public Codec {
40 protected:
41  friend class Codec;
42  Base64Codec() : Codec() {}
43 
44 public:
45  virtual ~Base64Codec() {}
46 
47  const char * name() const {
48  return "base64";
49  }
50 
51  int maxEncodedSizeFor( int insize, bool withCRLF=false ) const {
52  // first, the total number of 4-char packets will be:
53  int totalNumPackets = ( insize + 2 ) / 3;
54  // now, after every 76/4'th packet there needs to be a linebreak:
55  int numLineBreaks = totalNumPackets / (76/4);
56  // and at the very end, too:
57  ++numLineBreaks;
58  // putting it all together, we have:
59  return 4 * totalNumPackets + ( withCRLF ? 2 : 1 ) * numLineBreaks;
60  }
61 
62  int maxDecodedSizeFor( int insize, bool withCRLF=false ) const {
63  // assuming all characters are part of the base64 stream (which
64  // does almost never hold due to required linebreaking; but
65  // additional non-base64 chars don't affect the output size), each
66  // 4-tupel of them becomes a 3-tupel in the decoded octet
67  // stream. So:
68  int result = ( ( insize + 3 ) / 4 ) * 3;
69  // but all of them may be \n, so
70  if ( withCRLF )
71  result *= 2; // :-o
72 
73  return result;
74  }
75 
76  Encoder * makeEncoder( bool withCRLF=false ) const;
77  Decoder * makeDecoder( bool withCRLF=false ) const;
78 };
79 
80 
81 
82 class Rfc2047BEncodingCodec : public Base64Codec {
83 protected:
84  friend class Codec;
85  Rfc2047BEncodingCodec()
86  : Base64Codec() {}
87 
88 public:
89  virtual ~Rfc2047BEncodingCodec() {}
90 
91  const char * name() const { return "b"; }
92 
93  int maxEncodedSizeFor( int insize, bool withCRLF=false ) const {
94  (void)withCRLF; // keep compiler happy
95  // Each (begun) 3-octet triple becomes a 4 char quartet, so:
96  return ( ( insize + 2 ) / 3 ) * 4;
97  }
98 
99  int maxDecodedSizeFor( int insize, bool withCRLF=false ) const {
100  (void)withCRLF; // keep compiler happy
101  // Each 4-char quartet becomes a 3-octet triple, the last one
102  // possibly even less. So:
103  return ( ( insize + 3 ) / 4 ) * 3;
104  }
105 
106  Encoder * makeEncoder( bool withCRLF=false ) const;
107 };
108 
109 
110 } // namespace KMime
111 
112 #endif // __KMIME_CODEC_BASE64__