libkmime
kmime_codec_base64.h
00001 /* -*- c++ -*- 00002 kmime_codec_base64.h 00003 00004 This file is part of KMime, the KDE internet mail/usenet news message library. 00005 Copyright (c) 2001-2002 Marc Mutz <mutz@kde.org> 00006 00007 KMime is free software; you can redistribute it and/or modify it 00008 under the terms of the GNU General Public License, version 2, as 00009 published by the Free Software Foundation. 00010 00011 KMime is distributed in the hope that it will be useful, but 00012 WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this library; if not, write to the Free Software 00018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 00020 In addition, as a special exception, the copyright holders give 00021 permission to link the code of this library with any edition of 00022 the Qt library by Trolltech AS, Norway (or with modified versions 00023 of Qt that use the same license as Qt), and distribute linked 00024 combinations including the two. You must obey the GNU General 00025 Public License in all respects for all of the code used other than 00026 Qt. If you modify this file, you may extend this exception to 00027 your version of the file, but you are not obligated to do so. If 00028 you do not wish to do so, delete this exception statement from 00029 your version. 00030 */ 00031 00032 #ifndef __KMIME_CODEC_BASE64__ 00033 #define __KMIME_CODEC_BASE64__ 00034 00035 #include "kmime_codecs.h" 00036 00037 namespace KMime { 00038 00039 class Base64Codec : public Codec { 00040 protected: 00041 friend class Codec; 00042 Base64Codec() : Codec() {} 00043 00044 public: 00045 virtual ~Base64Codec() {} 00046 00047 const char * name() const { 00048 return "base64"; 00049 } 00050 00051 int maxEncodedSizeFor( int insize, bool withCRLF=false ) const { 00052 // first, the total number of 4-char packets will be: 00053 int totalNumPackets = ( insize + 2 ) / 3; 00054 // now, after every 76/4'th packet there needs to be a linebreak: 00055 int numLineBreaks = totalNumPackets / (76/4); 00056 // and at the very end, too: 00057 ++numLineBreaks; 00058 // putting it all together, we have: 00059 return 4 * totalNumPackets + ( withCRLF ? 2 : 1 ) * numLineBreaks; 00060 } 00061 00062 int maxDecodedSizeFor( int insize, bool withCRLF=false ) const { 00063 // assuming all characters are part of the base64 stream (which 00064 // does almost never hold due to required linebreaking; but 00065 // additional non-base64 chars don't affect the output size), each 00066 // 4-tupel of them becomes a 3-tupel in the decoded octet 00067 // stream. So: 00068 int result = ( ( insize + 3 ) / 4 ) * 3; 00069 // but all of them may be \n, so 00070 if ( withCRLF ) 00071 result *= 2; // :-o 00072 00073 return result; 00074 } 00075 00076 Encoder * makeEncoder( bool withCRLF=false ) const; 00077 Decoder * makeDecoder( bool withCRLF=false ) const; 00078 }; 00079 00080 00081 00082 class Rfc2047BEncodingCodec : public Base64Codec { 00083 protected: 00084 friend class Codec; 00085 Rfc2047BEncodingCodec() 00086 : Base64Codec() {} 00087 00088 public: 00089 virtual ~Rfc2047BEncodingCodec() {} 00090 00091 const char * name() const { return "b"; } 00092 00093 int maxEncodedSizeFor( int insize, bool withCRLF=false ) const { 00094 (void)withCRLF; // keep compiler happy 00095 // Each (begun) 3-octet triple becomes a 4 char quartet, so: 00096 return ( ( insize + 2 ) / 3 ) * 4; 00097 } 00098 00099 int maxDecodedSizeFor( int insize, bool withCRLF=false ) const { 00100 (void)withCRLF; // keep compiler happy 00101 // Each 4-char quartet becomes a 3-octet triple, the last one 00102 // possibly even less. So: 00103 return ( ( insize + 3 ) / 4 ) * 3; 00104 } 00105 00106 Encoder * makeEncoder( bool withCRLF=false ) const; 00107 }; 00108 00109 00110 } // namespace KMime 00111 00112 #endif // __KMIME_CODEC_BASE64__