Back to index

lightning-sunbird  0.9+nobinonly
nsMsgAppleDoubleEncode.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org Code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 /*
00040 *
00041 *   apple-double.c
00042 *      --------------
00043 *
00044 *        The codes to do apple double encoding/decoding.
00045 *             
00046 *             02aug95              mym           created.
00047 *             27sep95              mym           Add the XP_Mac to ensure the cross-platform.
00048 *             
00049 */
00050 #include "nsID.h"
00051 #include "nsCRT.h"
00052 #include "nscore.h"
00053 #include "msgCore.h"
00054 #include "nsMsgAppleDouble.h"
00055 #include "nsMsgAppleCodes.h"
00056 #include "nsFileSpec.h"
00057 #include "nsMsgCompUtils.h"
00058 #include "nsCExternalHandlerService.h"
00059 #include "nsIMIMEService.h"
00060 #include "nsMimeTypes.h"
00061 #include "prmem.h"
00062 
00063 #if defined(XP_MAC) || defined(XP_MACOSX)
00064 
00065 #ifdef XP_MAC
00066 #pragma warn_unusedarg off
00067 #pragma cplusplus on
00068 #else
00069 #include "MoreFilesX.h"
00070 #endif
00071 
00072 void   
00073 MacGetFileType(nsFileSpec   *fs, 
00074                PRBool       *useDefault, 
00075                char         **fileType, 
00076                char         **encoding)
00077 {
00078        if ((fs == NULL) || (fileType == NULL) || (encoding == NULL))
00079               return;
00080 
00081   if (!fs->Exists())
00082     return;
00083 
00084        *useDefault = TRUE;
00085        *fileType = NULL;
00086        *encoding = NULL;
00087 
00088        FInfo         fndrInfo;
00089 #if defined(XP_MAC)
00090   OSErr err = FSpGetFInfo( fs->GetFSSpecPtr(), &fndrInfo );
00091 #else
00092   FSSpec fsSpec;
00093   FSPathMakeFSSpec((UInt8 *)fs->GetNativePathCString(), &fsSpec, NULL);
00094   OSErr err = FSpGetFInfo (&fsSpec, &fndrInfo);
00095 #endif
00096 
00097   if ( (err != noErr) || (fndrInfo.fdType == 'TEXT') )
00098     *fileType = nsCRT::strdup(APPLICATION_OCTET_STREAM);
00099   else
00100   {
00101     // At this point, we should call the mime service and
00102     // see what we can find out?
00103     nsresult      rv;
00104     nsIURI        *tURI = nsnull;
00105     nsFileURL     tFileURL(*fs);
00106 
00107     if (NS_SUCCEEDED(nsMsgNewURL(&tURI, tFileURL.GetURLString())) && tURI)
00108     {
00109       nsCOMPtr<nsIMIMEService> mimeFinder (do_GetService(NS_MIMESERVICE_CONTRACTID, &rv));
00110       if (NS_SUCCEEDED(rv) && mimeFinder) 
00111       {
00112         nsCAutoString mimeType;
00113         rv = mimeFinder->GetTypeFromURI(tURI, mimeType);
00114         if (NS_SUCCEEDED(rv)) 
00115         {
00116           *fileType = ToNewCString(mimeType);
00117           return;
00118         }        
00119       }
00120     }
00121 
00122     // If we hit here, return something...default to this...
00123     *fileType = nsCRT::strdup(APPLICATION_OCTET_STREAM);
00124   }
00125 }
00126 
00127 #pragma cplusplus reset
00128 
00129 /*
00130 *      ap_encode_init
00131 *      --------------
00132 *      
00133 *      Setup the encode envirment
00134 */
00135 
00136 int ap_encode_init( appledouble_encode_object *p_ap_encode_obj,
00137                     const char                *fname,
00138                     char                      *separator)
00139 {
00140        FSSpec fspec;
00141        
00142        nsFileSpec  mySpec(fname);
00143        if (!mySpec.Exists())
00144               return -1;
00145 
00146 #if defined(XP_MAC)
00147        fspec = mySpec.GetFSSpec();
00148 #else
00149        FSPathMakeFSSpec((const UInt8 *)fname, &fspec, NULL);
00150 #endif
00151        memset(p_ap_encode_obj, 0, sizeof(appledouble_encode_object));
00152        
00153        /*
00154        **     Fill out the source file inforamtion.
00155        */     
00156        memcpy(p_ap_encode_obj->fname, fspec.name+1, *fspec.name);
00157        p_ap_encode_obj->fname[*fspec.name] = '\0';
00158        p_ap_encode_obj->vRefNum = fspec.vRefNum;
00159        p_ap_encode_obj->dirId   = fspec.parID;
00160        
00161        p_ap_encode_obj->boundary = nsCRT::strdup(separator);
00162        return noErr;
00163 }
00164 
00165 /*
00166 **     ap_encode_next
00167 **     --------------
00168 **            
00169 **            return :
00170 **                   noErr  :      everything is ok
00171 **                   errDone       :      when encoding is done.
00172 **                   errors :      otherwise.
00173 */
00174 int ap_encode_next(
00175        appledouble_encode_object* p_ap_encode_obj, 
00176        char   *to_buff, 
00177        PRInt32       buff_size, 
00178        PRInt32*      real_size)
00179 {
00180        int status;
00181        
00182        /*
00183        **     install the out buff now.
00184        */
00185        p_ap_encode_obj->outbuff     = to_buff;
00186        p_ap_encode_obj->s_outbuff   = buff_size;
00187        p_ap_encode_obj->pos_outbuff = 0;
00188        
00189        /*
00190        **     first copy the outstandind data in the overflow buff to the out buffer. 
00191        */
00192        if (p_ap_encode_obj->s_overflow)
00193        {
00194               status = write_stream(p_ap_encode_obj, 
00195                                                         p_ap_encode_obj->b_overflow,
00196                                                         p_ap_encode_obj->s_overflow);
00197               if (status != noErr)
00198                      return status;
00199                             
00200               p_ap_encode_obj->s_overflow = 0;
00201        }
00202 
00203        /*
00204        ** go the next processing stage based on the current state. 
00205        */
00206        switch (p_ap_encode_obj->state)
00207        {
00208               case kInit:
00209                      /*
00210                      ** We are in the  starting position, fill out the header.
00211                      */
00212                      status = fill_apple_mime_header(p_ap_encode_obj); 
00213                      if (status != noErr)
00214                             break;                             /* some error happens */
00215                             
00216                      p_ap_encode_obj->state = kDoingHeaderPortion;
00217                      status = ap_encode_header(p_ap_encode_obj, true); 
00218                                                                       /* it is the first time to calling               */                                               
00219                      if (status == errDone)
00220                      {
00221                             p_ap_encode_obj->state = kDoneHeaderPortion;
00222                      }
00223                      else
00224                      {
00225                             break;                             /* we need more work on header portion.   */
00226                      }                    
00227                             
00228                      /*
00229                      ** we are done with the header, so let's go to the data port.
00230                      */
00231                      p_ap_encode_obj->state = kDoingDataPortion;
00232                      status = ap_encode_data(p_ap_encode_obj, true);                
00233                                                                       /* it is first time call do data portion */
00234                                                  
00235                      if (status == errDone)
00236                      {
00237                             p_ap_encode_obj->state  = kDoneDataPortion;
00238                             status = noErr;
00239                      }
00240                      break;
00241 
00242               case kDoingHeaderPortion:
00243               
00244                      status = ap_encode_header(p_ap_encode_obj, false);                    
00245                                                                       /* continue with the header portion.      */
00246                      if (status == errDone)
00247                      {
00248                             p_ap_encode_obj->state = kDoneHeaderPortion;
00249                      }
00250                      else
00251                      {
00252                             break;                             /* we need more work on header portion.   */                          
00253                      }
00254                      
00255                      /*
00256                      ** start the data portion.
00257                      */
00258                      p_ap_encode_obj->state = kDoingDataPortion;
00259                      status = ap_encode_data(p_ap_encode_obj, true);                              
00260                                                                       /* it is the first time calling           */
00261                      if (status == errDone)
00262                      {
00263                             p_ap_encode_obj->state  = kDoneDataPortion;
00264                             status = noErr;
00265                      }
00266                      break;
00267 
00268               case kDoingDataPortion:
00269               
00270                      status = ap_encode_data(p_ap_encode_obj, false);                             
00271                                                                       /* it is not the first time                      */
00272                                                                                            
00273                      if (status == errDone)
00274                      {
00275                             p_ap_encode_obj->state = kDoneDataPortion;
00276                             status = noErr;
00277                      }
00278                      break;
00279 
00280               case kDoneDataPortion:
00281                             status = errDone;           /* we are really done.                                  */
00282 
00283                      break;
00284        }
00285        
00286        *real_size = p_ap_encode_obj->pos_outbuff;
00287        return status;
00288 }
00289 
00290 /*
00291 **     ap_encode_end
00292 **     -------------
00293 **
00294 **     clear the apple encoding.
00295 */
00296 
00297 int ap_encode_end(
00298        appledouble_encode_object *p_ap_encode_obj, 
00299        PRBool is_aborting)
00300 {
00301        /*
00302        ** clear up the apple doubler.
00303        */
00304        if (p_ap_encode_obj == NULL)
00305               return noErr;
00306 
00307        if (p_ap_encode_obj->fileId)                     /* close the file if it is open.   */
00308               FSClose(p_ap_encode_obj->fileId);
00309 
00310        PR_FREEIF(p_ap_encode_obj->boundary);            /* the boundary string.                          */
00311        
00312        return noErr;
00313 }
00314 
00315 #endif /* the ifdef of XP_MAC */
00316