1 | /* -*- c++ -*- |
2 | kmime_codec_base64.h |
3 | |
4 | KMime, the KDE Internet mail/usenet news message library. |
5 | Copyright (c) 2001-2002 Marc Mutz <mutz@kde.org> |
6 | |
7 | This library is free software; you can redistribute it and/or |
8 | modify it under the terms of the GNU Library General Public |
9 | License as published by the Free Software Foundation; either |
10 | version 2 of the License, or (at your option) any later version. |
11 | |
12 | This library is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | Library General Public License for more details. |
16 | |
17 | You should have received a copy of the GNU Library General Public License |
18 | along with this library; see the file COPYING.LIB. If not, write to |
19 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | Boston, MA 02110-1301, USA. |
21 | */ |
22 | /** |
23 | @file |
24 | This file is part of the API for handling @ref MIME data and |
25 | defines the @ref Base64 and @ref RFC2047B @ref Codec classes. |
26 | |
27 | @brief |
28 | Defines the Base64Codec and Rfc2047BEncodingCodec classes. |
29 | |
30 | @authors Marc Mutz \<mutz@kde.org\> |
31 | |
32 | @glossary @anchor Base64 @anchor base64 @b base64: |
33 | a binary to text encoding scheme based on @ref RFC1421. |
34 | |
35 | @glossary @anchor RFC1421 @anchor rfc1421 @b RFC @b 1421: |
36 | RFC that defines the <a href="http://tools.ietf.org/html/rfc1421"> |
37 | Privacy Enhancement for Internet Electronic Mail: Part I: |
38 | Message Encryption and Authentication Procedures</a>. |
39 | |
40 | @glossary @anchor RFC2045 @anchor rfc2045 @b RFC @b 2045: |
41 | RFC that defines the <a href="http://tools.ietf.org/html/rfc2045"> |
42 | MIME Part One: Format of Internet Message Bodies</a>. |
43 | |
44 | @glossary @anchor RFC2047 @anchor rfc2047 @b RFC @b 2047: |
45 | RFC that defines the <a href="http://tools.ietf.org/html/rfc2047"> |
46 | MIME Part Three: Message Header Extensions for Non-ASCII Text</a>. |
47 | |
48 | @glossary @anchor RFC2047B @anchor rfc2047b @b RFC @b 2047B: |
49 | Section 4.1 of @ref RFC2047. |
50 | */ |
51 | |
52 | #ifndef __KMIME_CODEC_BASE64__ |
53 | #define __KMIME_CODEC_BASE64__ |
54 | |
55 | #include "kmime_codecs.h" |
56 | |
57 | namespace KMime { |
58 | |
59 | /** |
60 | @brief |
61 | A class representing the @ref codec for @ref Base64 as specified in |
62 | @ref RFC2045 |
63 | */ |
64 | class KMIME_EXPORT Base64Codec : public Codec |
65 | { |
66 | protected: |
67 | friend class Codec; |
68 | /** |
69 | Constructs a Base64 codec. |
70 | */ |
71 | Base64Codec() : Codec() {} |
72 | |
73 | public: |
74 | /** |
75 | Destroys the codec. |
76 | */ |
77 | virtual ~Base64Codec() {} |
78 | |
79 | /** |
80 | @copydoc |
81 | Codec::name() |
82 | */ |
83 | const char *name() const |
84 | { return "base64" ; } |
85 | |
86 | /** |
87 | @copydoc |
88 | Codec::maxEncodedSizeFor() |
89 | */ |
90 | int maxEncodedSizeFor( int insize, bool withCRLF=false ) const |
91 | { |
92 | // first, the total number of 4-char packets will be: |
93 | int totalNumPackets = ( insize + 2 ) / 3; |
94 | // now, after every 76/4'th packet there needs to be a linebreak: |
95 | int numLineBreaks = totalNumPackets / ( 76 / 4 ); |
96 | // and at the very end, too: |
97 | ++numLineBreaks; |
98 | // putting it all together, we have: |
99 | return 4 * totalNumPackets + ( withCRLF ? 2 : 1 ) * numLineBreaks; |
100 | } |
101 | |
102 | /** |
103 | @copydoc |
104 | Codec::maxDecodedSizeFor() |
105 | */ |
106 | int maxDecodedSizeFor( int insize, bool withCRLF=false ) const |
107 | { |
108 | // assuming all characters are part of the base64 stream (which |
109 | // does almost never hold due to required linebreaking; but |
110 | // additional non-base64 chars don't affect the output size), each |
111 | // 4-tupel of them becomes a 3-tupel in the decoded octet |
112 | // stream. So: |
113 | int result = ( ( insize + 3 ) / 4 ) * 3; |
114 | // but all of them may be \n, so |
115 | if ( withCRLF ) { |
116 | result *= 2; // :-o |
117 | } |
118 | |
119 | return result; |
120 | } |
121 | |
122 | /** |
123 | @copydoc |
124 | Codec::makeEncoder() |
125 | */ |
126 | Encoder *makeEncoder( bool withCRLF=false ) const; |
127 | |
128 | /** |
129 | @copydoc |
130 | Codec::makeDecoder() |
131 | */ |
132 | Decoder *makeDecoder( bool withCRLF=false ) const; |
133 | }; |
134 | |
135 | /** |
136 | @brief |
137 | A class representing the @ref codec for the B encoding as specified |
138 | in @ref RFC2047B. |
139 | */ |
140 | class KMIME_EXPORT Rfc2047BEncodingCodec : public Base64Codec |
141 | { |
142 | protected: |
143 | friend class Codec; |
144 | /** |
145 | Constructs a RFC2047B codec. |
146 | */ |
147 | Rfc2047BEncodingCodec() : Base64Codec() {} |
148 | |
149 | public: |
150 | /** |
151 | Destroys the codec. |
152 | */ |
153 | virtual ~Rfc2047BEncodingCodec() {} |
154 | |
155 | /** |
156 | @copydoc |
157 | Codec::name() |
158 | */ |
159 | const char *name() const |
160 | { return "b" ; } |
161 | |
162 | /** |
163 | @copydoc |
164 | Codec::maxEncodedSizeFor() |
165 | */ |
166 | int maxEncodedSizeFor( int insize, bool withCRLF=false ) const |
167 | { |
168 | Q_UNUSED( withCRLF ); |
169 | // Each (begun) 3-octet triple becomes a 4 char quartet, so: |
170 | return ( ( insize + 2 ) / 3 ) * 4; |
171 | } |
172 | |
173 | /** |
174 | @copydoc |
175 | Codec::maxDecodedSizeFor() |
176 | */ |
177 | int maxDecodedSizeFor( int insize, bool withCRLF=false ) const |
178 | { |
179 | Q_UNUSED( withCRLF ); |
180 | // Each 4-char quartet becomes a 3-octet triple, the last one |
181 | // possibly even less. So: |
182 | return ( ( insize + 3 ) / 4 ) * 3; |
183 | } |
184 | |
185 | /** |
186 | @copydoc |
187 | Codec::makeEncoder() |
188 | */ |
189 | Encoder *makeEncoder( bool withCRLF=false ) const; |
190 | }; |
191 | |
192 | } // namespace KMime |
193 | |
194 | #endif // __KMIME_CODEC_BASE64__ |
195 | |