Back to index

lightning-sunbird  0.9+nobinonly
nsMimeConverter.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #include <stdio.h>
00038 #include "mimecom.h"
00039 #include "modmimee.h"
00040 #include "nscore.h"
00041 #include "nsMimeConverter.h"
00042 #include "comi18n.h"
00043 #include "nsMsgI18N.h"
00044 #include "prmem.h"
00045 #include "plstr.h"
00046 #include "nsReadableUtils.h"
00047 
00048 NS_IMPL_THREADSAFE_ADDREF(nsMimeConverter)
00049 NS_IMPL_THREADSAFE_RELEASE(nsMimeConverter)
00050 
00051 NS_INTERFACE_MAP_BEGIN(nsMimeConverter)
00052    NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIMimeConverter)
00053    NS_INTERFACE_MAP_ENTRY(nsIMimeConverter)
00054 NS_INTERFACE_MAP_END
00055 
00056 /*
00057  * nsMimeConverter definitions....
00058  */
00059 
00060 /* 
00061  * Inherited methods for nsMimeConverter
00062  */
00063 nsMimeConverter::nsMimeConverter()
00064 {
00065 }
00066 
00067 nsMimeConverter::~nsMimeConverter()
00068 {
00069 }
00070 
00071 nsresult
00072 nsMimeConverter::DecodeMimeHeader(const char *header, 
00073                                   char **decodedString,
00074                                   const char *default_charset, 
00075                                   PRBool override_charset,
00076                                   PRBool eatContinuations)
00077 {
00078   *decodedString = MIME_DecodeMimeHeader(header, default_charset, 
00079                                           override_charset,
00080                                           eatContinuations);
00081   return NS_OK;
00082 }
00083 
00084 // Decode routine (also converts output to unicode)
00085 nsresult 
00086 nsMimeConverter::DecodeMimeHeader(const char *header, 
00087                                   PRUnichar **decodedString,
00088                                   const char *default_charset,
00089                                   PRBool override_charset,
00090                                   PRBool eatContinuations)
00091 {
00092   char *decodedCstr = nsnull;
00093   nsresult res = NS_OK;
00094 
00095   // apply MIME decode.
00096   decodedCstr = MIME_DecodeMimeHeader(header, default_charset,
00097                                       override_charset, eatContinuations);
00098   if (nsnull == decodedCstr) {
00099     *decodedString = ToNewUnicode(NS_ConvertUTF8toUCS2(header));
00100   } else {
00101     *decodedString = ToNewUnicode(NS_ConvertUTF8toUCS2(decodedCstr));
00102     PR_FREEIF(decodedCstr);
00103   }
00104   if (!(*decodedString))
00105     res = NS_ERROR_OUT_OF_MEMORY;
00106   return res;
00107 }
00108 
00109 // Decode routine (also converts output to unicode)
00110 nsresult 
00111 nsMimeConverter::DecodeMimeHeader(const char *header, 
00112                                   nsAString& decodedString,
00113                                   const char *default_charset,
00114                                   PRBool override_charset,
00115                                   PRBool eatContinuations)
00116 {
00117   char *decodedCstr = nsnull;
00118 
00119   // apply MIME decode.
00120   decodedCstr = MIME_DecodeMimeHeader(header, default_charset,
00121                                       override_charset, eatContinuations);
00122   if (nsnull == decodedCstr) {
00123     CopyUTF8toUTF16(header, decodedString);
00124   } else {
00125     CopyUTF8toUTF16(decodedCstr, decodedString);
00126     PR_FREEIF(decodedCstr);
00127   }
00128 
00129   return NS_OK;
00130 }
00131 
00132 nsresult
00133 nsMimeConverter::EncodeMimePartIIStr(const char    *header, 
00134                                            PRBool        structured, 
00135                                            const char    *mailCharset, 
00136                                            PRInt32       fieldnamelen,
00137                                            PRInt32 encodedWordSize, 
00138                                            char          **encodedString)
00139 {
00140 
00141   // Encoder needs utf-8 string.
00142   nsAutoString tempUnicodeString;
00143   nsresult rv = ConvertToUnicode(mailCharset, header, tempUnicodeString);
00144   NS_ENSURE_SUCCESS(rv, rv);
00145   rv = EncodeMimePartIIStr_UTF8(NS_ConvertUCS2toUTF8(tempUnicodeString).get(), structured, mailCharset, fieldnamelen, encodedWordSize, encodedString);
00146   return rv;
00147 }
00148 
00149 nsresult
00150 nsMimeConverter::EncodeMimePartIIStr_UTF8(const char    *header, 
00151                                           PRBool        structured, 
00152                                           const char    *mailCharset, 
00153                                           PRInt32       fieldnamelen,
00154                                           PRInt32       encodedWordSize, 
00155                                           char          **encodedString)
00156 {
00157   char *retString = MIME_EncodeMimePartIIStr(header, structured, mailCharset, fieldnamelen, encodedWordSize);
00158   if (retString == NULL)
00159     return NS_ERROR_FAILURE;
00160   else
00161   {
00162     *encodedString = retString;
00163     return NS_OK;
00164   }
00165 }
00166 
00167 
00168 nsresult 
00169 nsMimeConverter::B64EncoderInit(nsresult (*output_fn) (const char *buf, PRInt32 size, void *closure), void *closure, 
00170                    MimeEncoderData **returnEncoderData) 
00171 {
00172 MimeEncoderData   *ptr;
00173 
00174   ptr = MimeB64EncoderInit(output_fn, closure);
00175   if (ptr)
00176   {
00177     *returnEncoderData = ptr;
00178     return NS_OK;
00179   }
00180   else
00181     return NS_ERROR_OUT_OF_MEMORY;
00182 }
00183 
00184 nsresult
00185 nsMimeConverter::QPEncoderInit (nsresult (*output_fn) (const char *buf, 
00186                       PRInt32 size, void *closure), void *closure, MimeEncoderData ** returnEncoderData) 
00187 {
00188 MimeEncoderData   *ptr;
00189 
00190   ptr = MimeQPEncoderInit(output_fn, closure);
00191   if (ptr)
00192   {
00193     *returnEncoderData = ptr;
00194     return NS_OK;
00195   }
00196   else
00197     return NS_ERROR_OUT_OF_MEMORY;
00198 }
00199 
00200 nsresult
00201 nsMimeConverter::UUEncoderInit (char *filename, nsresult (*output_fn) 
00202                      (const char *buf, PRInt32 size, void *closure), void *closure, 
00203                      MimeEncoderData ** returnEncoderData)
00204 {
00205 MimeEncoderData   *ptr;
00206 
00207   ptr = MimeUUEncoderInit(filename, output_fn, closure);
00208   if (ptr)
00209   {
00210     *returnEncoderData = ptr;
00211     return NS_OK;
00212   }
00213   else
00214     return NS_ERROR_OUT_OF_MEMORY;
00215 }
00216 
00217 nsresult
00218 nsMimeConverter::EncoderDestroy(MimeEncoderData *data, PRBool abort_p) 
00219 {
00220   MimeEncoderDestroy(data, abort_p);
00221   return NS_OK;
00222 }
00223 
00224 nsresult
00225 nsMimeConverter::EncoderWrite (MimeEncoderData *data, const char *buffer, PRInt32 size, PRInt32 *written) 
00226 {
00227   PRInt32   writeCount;
00228   writeCount = MimeEncoderWrite(data, buffer, size);
00229   *written = writeCount;
00230   return NS_OK;
00231 }