kmime_codec_base64.h
Go to the documentation of this file.
00001 /* -*- c++ -*- 00002 kmime_codec_base64.h 00003 00004 KMime, the KDE Internet mail/usenet news message library. 00005 Copyright (c) 2001-2002 Marc Mutz <mutz@kde.org> 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Library General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Library General Public License for more details. 00016 00017 You should have received a copy of the GNU Library General Public License 00018 along with this library; see the file COPYING.LIB. If not, write to 00019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00020 Boston, MA 02110-1301, USA. 00021 */ 00052 #ifndef __KMIME_CODEC_BASE64__ 00053 #define __KMIME_CODEC_BASE64__ 00054 00055 #include "kmime_codecs.h" 00056 00057 namespace KMime { 00058 00064 class KMIME_EXPORT Base64Codec : public Codec 00065 { 00066 protected: 00067 friend class Codec; 00071 Base64Codec() : Codec() {} 00072 00073 public: 00077 virtual ~Base64Codec() {} 00078 00083 const char *name() const 00084 { return "base64"; } 00085 00090 int maxEncodedSizeFor( int insize, bool withCRLF=false ) const 00091 { 00092 // first, the total number of 4-char packets will be: 00093 int totalNumPackets = ( insize + 2 ) / 3; 00094 // now, after every 76/4'th packet there needs to be a linebreak: 00095 int numLineBreaks = totalNumPackets / (76/4); 00096 // and at the very end, too: 00097 ++numLineBreaks; 00098 // putting it all together, we have: 00099 return 4 * totalNumPackets + ( withCRLF ? 2 : 1 ) * numLineBreaks; 00100 } 00101 00106 int maxDecodedSizeFor( int insize, bool withCRLF=false ) const 00107 { 00108 // assuming all characters are part of the base64 stream (which 00109 // does almost never hold due to required linebreaking; but 00110 // additional non-base64 chars don't affect the output size), each 00111 // 4-tupel of them becomes a 3-tupel in the decoded octet 00112 // stream. So: 00113 int result = ( ( insize + 3 ) / 4 ) * 3; 00114 // but all of them may be \n, so 00115 if ( withCRLF ) { 00116 result *= 2; // :-o 00117 } 00118 00119 return result; 00120 } 00121 00126 Encoder *makeEncoder( bool withCRLF=false ) const; 00127 00132 Decoder *makeDecoder( bool withCRLF=false ) const; 00133 }; 00134 00140 class KMIME_EXPORT Rfc2047BEncodingCodec : public Base64Codec 00141 { 00142 protected: 00143 friend class Codec; 00147 Rfc2047BEncodingCodec() : Base64Codec() {} 00148 00149 public: 00153 virtual ~Rfc2047BEncodingCodec() {} 00154 00159 const char *name() const 00160 { return "b"; } 00161 00166 int maxEncodedSizeFor( int insize, bool withCRLF=false ) const 00167 { 00168 Q_UNUSED( withCRLF ); 00169 // Each (begun) 3-octet triple becomes a 4 char quartet, so: 00170 return ( ( insize + 2 ) / 3 ) * 4; 00171 } 00172 00177 int maxDecodedSizeFor( int insize, bool withCRLF=false ) const 00178 { 00179 Q_UNUSED( withCRLF ); 00180 // Each 4-char quartet becomes a 3-octet triple, the last one 00181 // possibly even less. So: 00182 return ( ( insize + 3 ) / 4 ) * 3; 00183 } 00184 00189 Encoder *makeEncoder( bool withCRLF=false ) const; 00190 }; 00191 00192 } // namespace KMime 00193 00194 #endif // __KMIME_CODEC_BASE64__