Adding new files for AppleDouble support ... NOT PART OF THE BUILD

This commit is contained in:
rhp%netscape.com 2000-01-27 00:02:23 +00:00
parent eebd0c9ee3
commit 04d3844e67
4 changed files with 3854 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,725 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
/**
* Apple Double encode/decode stream
* ----------------------------------
*
* 11sep95 mym created.
*/
#include "nscore.h"
#include "msgCore.h"
#include "nsMsgAppleDouble.h"
#include "nsMsgAppleDoubleCodes.h"
#include "m_binhex.h"
#include "m_cvstrm.h"
#include "ad_codes.h"
extern int MK_MSG_SAVE_ATTACH_AS;
#ifdef XP_MAC
#pragma warn_unusedarg off
extern int MK_UNABLE_TO_OPEN_TMP_FILE;
extern int MK_MIME_ERROR_WRITING_FILE;
/* ---------------------------------------------------------------------------------
**
** The codes for Apple-double encoding stream. --- it's only useful on Mac OS
**
** ---------------------------------------------------------------------------------
*/
#define WORKING_BUFF_SIZE 8192
typedef struct _AppledoubleEncodeObject
{
appledouble_encode_object ap_encode_obj;
char* buff; /* the working buff. */
PRInt32 s_buff; /* the working buff size. */
XP_File fp; /* file to hold the encoding */
char *fname; /* and the file name. */
} AppleDoubleEncodeObject;
/*
Let's go "l" characters forward of the encoding for this write.
Note:
"s" is just a dummy paramter.
*/
PRIVATE int
net_AppleDouble_Encode_Write (
void *stream, const char* s, PRInt32 l)
{
int status = 0;
AppleDoubleEncodeObject * obj = (AppleDoubleEncodeObject*)stream;
PRInt32 count, size;
while (l > 0)
{
size = obj->s_buff * 11 / 16;
size = PR_MIN(l, size);
status = ap_encode_next(&(obj->ap_encode_obj),
obj->buff,
size,
&count);
if (status == noErr || status == errDone)
{
/*
* we get the encode data, so call the next stream to write it to the disk.
*/
if (XP_FileWrite(obj->buff, count, obj->fp) != count)
return errFileWrite;
}
if (status != noErr ) /* abort when error / done? */
break;
l -= size;
}
return status;
}
/*
** is the stream ready for writing?
*/
PRIVATE unsigned int net_AppleDouble_Encode_Ready (void *stream)
{
return(PR_MAX_WRITE_READY); /* always ready for writing */
}
PRIVATE void net_AppleDouble_Encode_Complete (void *stream)
{
AppleDoubleEncodeObject * obj = (AppleDoubleEncodeObject*)stream;
ap_encode_end(&(obj->ap_encode_obj), false); /* this is a normal ending */
if (obj->fp)
{
XP_FileClose(obj->fp); /* done with the target file */
FREEIF(obj->fname); /* and the file name too */
}
FREEIF(obj->buff); /* free the working buff. */
PR_FREE(obj);
}
PRIVATE void net_AppleDouble_Encode_Abort (void *stream, int status)
{
AppleDoubleEncodeObject * obj = (AppleDoubleEncodeObject*)stream;
ap_encode_end(&(obj->ap_encode_obj), true); /* it is an aborting exist... */
if (obj->fp)
{
XP_FileClose(obj->fp);
XP_FileRemove (obj->fname, xpURL); /* remove the partial file. */
FREEIF(obj->fname);
}
FREEIF(obj->buff); /* free the working buff. */
PR_FREE(obj);
}
/*
** fe_MakeAppleDoubleEncodeStream
** ------------------------------
**
** Will create a apple double encode stream:
**
** -> take the filename as the input source (it needs to be a mac file.)
** -> take a file name for the temp file we are generating.
*/
PUBLIC NET_StreamClass *
fe_MakeAppleDoubleEncodeStream (int format_out,
void *data_obj,
URL_Struct *URL_s,
MWContext *window_id,
char* src_filename,
char* dst_filename,
char* separator)
{
AppleDoubleEncodeObject* obj;
NET_StreamClass* stream;
char* working_buff = NULL;
int bSize = WORKING_BUFF_SIZE;
TRACEMSG(("Setting up apple encode stream. Have URL: %s\n", URL_s->address));
stream = XP_NEW(NET_StreamClass);
if(stream == NULL)
return(NULL);
obj = XP_NEW(AppleDoubleEncodeObject);
if (obj == NULL)
{
PR_FREE (stream);
return(NULL);
}
while (!working_buff && (bSize >= 512))
{
working_buff = (char *)PR_CALLOC(bSize);
if (!working_buff)
bSize /= 2;
}
if (working_buff == NULL)
{
PR_FREE (obj);
PR_FREE (stream);
return (NULL);
}
stream->name = "Apple Double Encode";
stream->complete = (MKStreamCompleteFunc) net_AppleDouble_Encode_Complete;
stream->abort = (MKStreamAbortFunc) net_AppleDouble_Encode_Abort;
stream->put_block = (MKStreamWriteFunc) net_AppleDouble_Encode_Write;
stream->is_write_ready = (MKStreamWriteReadyFunc) net_AppleDouble_Encode_Ready;
stream->data_object = obj;
stream->window_id = window_id;
obj->fp = XP_FileOpen(dst_filename, xpFileToPost, XP_FILE_WRITE_BIN);
if (obj->fp == NULL)
{
PR_FREE (working_buff);
PR_FREE (obj);
PR_FREE (stream);
return (NULL);
}
obj->fname = XP_STRDUP(dst_filename);
obj->buff = working_buff;
obj->s_buff = bSize;
/*
** setup all the need information on the apple double encoder.
*/
ap_encode_init(&(obj->ap_encode_obj),
src_filename, /* pass the file name of the source. */
separator);
TRACEMSG(("Returning stream from NET_AppleDoubleEncoder\n"));
return stream;
}
#endif
/*
** ---------------------------------------------------------------------------------
**
** The codes for the Apple sigle/double decoding.
**
** ---------------------------------------------------------------------------------
*/
typedef struct AppleDoubleDecodeObject
{
appledouble_decode_object ap_decode_obj;
char* in_buff; /* the temporary buff to accumulate */
/* the input, make sure the call to */
/* the dedcoder engine big enough buff */
PRInt32 bytes_in_buff; /* the count for the temporary buff. */
NET_StreamClass* binhex_stream; /* a binhex encode stream to convert */
/* the decoded mac file to binhex. */
} AppleDoubleDecodeObject;
PRIVATE int
net_AppleDouble_Decode_Write (
void *stream, const char* s, PRInt32 l)
{
int status = NOERR;
AppleDoubleDecodeObject * obj = (AppleDoubleDecodeObject*) stream;
PRInt32 size;
/*
** To force an effecient decoding, we should
** make sure that the buff pass to the decode next is great than 1024 bytes.
*/
if (obj->bytes_in_buff + l > 1024)
{
size = 1024 - obj->bytes_in_buff;
XP_MEMCPY(obj->in_buff+obj->bytes_in_buff,
s,
size);
s += size;
l -= size;
status = ap_decode_next(&(obj->ap_decode_obj),
obj->in_buff,
1024);
obj->bytes_in_buff = 0;
}
if (l > 1024)
{
/* we are sure that obj->bytes_in_buff == 0 at this point. */
status = ap_decode_next(&(obj->ap_decode_obj),
(char *)s,
l);
}
else
{
/* and we are sure we will not get overflow with the buff. */
XP_MEMCPY(obj->in_buff+obj->bytes_in_buff,
s,
l);
obj->bytes_in_buff += l;
}
return status;
}
PRIVATE unsigned int
net_AppleDouble_Decode_Ready (NET_StreamClass *stream)
{
return(PR_MAX_WRITE_READY); /* always ready for writing */
}
PRIVATE void
net_AppleDouble_Decode_Complete (void *stream)
{
AppleDoubleDecodeObject *obj = (AppleDoubleDecodeObject *)stream;
if (obj->bytes_in_buff)
{
ap_decode_next(&(obj->ap_decode_obj), /* do the last calls. */
(char *)obj->in_buff,
obj->bytes_in_buff);
obj->bytes_in_buff = 0;
}
ap_decode_end(&(obj->ap_decode_obj), FALSE); /* it is a normal clean up cases.*/
if (obj->binhex_stream)
PR_FREE(obj->binhex_stream);
if (obj->in_buff)
PR_FREE(obj->in_buff);
PR_FREE(obj);
}
PRIVATE void
net_AppleDouble_Decode_Abort (
void *stream, int status)
{
AppleDoubleDecodeObject *obj = (AppleDoubleDecodeObject *)stream;
ap_decode_end(&(obj->ap_decode_obj), TRUE); /* it is an abort. */
if (obj->binhex_stream)
PR_FREE(obj->binhex_stream);
if (obj->in_buff)
PR_FREE(obj->in_buff);
PR_FREE(obj);
}
/*
** fe_MakeAppleDoubleDecodeStream_1
** ---------------------------------
**
** Create the apple double decode stream.
**
** In the Mac OS, it will create a stream to decode to an apple file;
**
** In other OS, the stream will decode apple double object,
** then encode it in binhex format, and save to the file.
*/
#ifndef XP_MAC
static void
simple_copy(MWContext* context, char* saveName, void* closure)
{
/* just copy the filename to the closure, so the caller can get it. */
XP_STRCPY(closure, saveName);
}
#endif
PUBLIC NET_StreamClass *
fe_MakeAppleDoubleDecodeStream_1 (int format_out,
void *data_obj,
URL_Struct *URL_s,
MWContext *window_id)
{
#ifdef XP_MAC
return fe_MakeAppleDoubleDecodeStream(format_out,
data_obj,
URL_s,
window_id,
false,
NULL);
#else
#if 0 /* just a test in the mac OS */
NET_StreamClass *p;
char* url;
StandardFileReply reply;
StandardPutFile("\pSave binhex encoded file as:", "\pUntitled", &reply);
if (!reply.sfGood)
{
return NULL;
}
url = my_PathnameFromFSSpec(&(reply.sfFile));
p = fe_MakeAppleDoubleDecodeStream(format_out,
data_obj,
URL_s,
window_id,
true,
url+7);
PR_FREE(url);
return (p);
#else /* for the none mac-os to get a file name */
NET_StreamClass *p;
char* filename;
filename = PR_CALLOC(1024);
if (filename == NULL)
return NULL;
/***** RICHIE - CONVERT THIS
if (FE_PromptForFileName(window_id,
XP_GetString(MK_MSG_SAVE_ATTACH_AS),
0,
FALSE,
FALSE,
simple_copy,
filename) == -1)
{
return NULL;
}
***/
p = fe_MakeAppleDoubleDecodeStream(format_out,
data_obj,
URL_s,
window_id,
TRUE,
filename);
PR_FREE(filename);
return (p);
#endif
#endif
}
PUBLIC NET_StreamClass *
fe_MakeAppleDoubleDecodeStream (int format_out,
void *data_obj,
URL_Struct *URL_s,
MWContext *window_id,
PRBool write_as_binhex,
char *dst_filename)
{
AppleDoubleDecodeObject* obj;
NET_StreamClass* stream;
TRACEMSG(("Setting up apple double decode stream. Have URL: %s\n", URL_s->address));
stream = XP_NEW(NET_StreamClass);
if(stream == NULL)
return(NULL);
obj = XP_NEW(AppleDoubleDecodeObject);
if (obj == NULL)
{
PR_FREE(stream);
return(NULL);
}
stream->name = "AppleDouble Decode";
stream->complete = (MKStreamCompleteFunc) net_AppleDouble_Decode_Complete;
stream->abort = (MKStreamAbortFunc) net_AppleDouble_Decode_Abort;
stream->put_block = (MKStreamWriteFunc) net_AppleDouble_Decode_Write;
stream->is_write_ready = (MKStreamWriteReadyFunc) net_AppleDouble_Decode_Ready;
stream->data_object = obj;
stream->window_id = window_id;
/*
** setup all the need information on the apple double encoder.
*/
obj->in_buff = (char *)PR_CALLOC(1024);
if (obj->in_buff == NULL)
{
PR_FREE(obj);
PR_FREE(stream);
return (NULL);
}
obj->bytes_in_buff = 0;
if (write_as_binhex)
{
obj->binhex_stream =
fe_MakeBinHexEncodeStream(format_out,
data_obj,
URL_s,
window_id,
dst_filename);
if (obj->binhex_stream == NULL)
{
PR_FREE(obj);
PR_FREE(stream);
PR_FREE(obj->in_buff);
return NULL;
}
ap_decode_init(&(obj->ap_decode_obj),
FALSE,
TRUE,
obj->binhex_stream);
}
else
{
obj->binhex_stream = NULL;
ap_decode_init(&(obj->ap_decode_obj),
FALSE,
FALSE,
window_id);
/*
* jt 8/8/97 -- I think this should be set to true. But'
* let's not touch it for now.
*
* obj->ap_decode_obj.is_binary = TRUE;
*/
}
if (dst_filename)
{
XP_STRNCPY_SAFE(obj->ap_decode_obj.fname, dst_filename,
sizeof(obj->ap_decode_obj.fname));
}
#ifdef XP_MAC
obj->ap_decode_obj.mSpec = (FSSpec*)( URL_s->fe_data );
#endif
TRACEMSG(("Returning stream from NET_AppleDoubleDecode\n"));
return stream;
}
/*
** fe_MakeAppleSingleDecodeStream_1
** --------------------------------
**
** Create the apple single decode stream.
**
** In the Mac OS, it will create a stream to decode object to an apple file;
**
** In other OS, the stream will decode apple single object,
** then encode context in binhex format, and save to the file.
*/
PUBLIC NET_StreamClass *
fe_MakeAppleSingleDecodeStream_1 (int format_out,
void *data_obj,
URL_Struct *URL_s,
MWContext *window_id)
{
#ifdef XP_MAC
return fe_MakeAppleSingleDecodeStream(format_out,
data_obj,
URL_s,
window_id,
FALSE,
NULL);
#else
#if 0 /* just a test in the mac OS */
NET_StreamClass *p;
char* url;
StandardFileReply reply;
StandardPutFile("\pSave binhex encoded file as:", "\pUntitled", &reply);
if (!reply.sfGood)
{
return NULL;
}
url = my_PathnameFromFSSpec(&(reply.sfFile));
p = fe_MakeAppleSingleDecodeStream(format_out,
data_obj,
URL_s,
window_id,
true,
url+7);
PR_FREE(url);
return (p);
#else /* for the none mac-os to get a file name */
NET_StreamClass *p;
char* filename;
char* defaultPath = 0;
defaultPath = URL_s->content_name;
#ifdef XP_WIN16
if (XP_FileNameContainsBadChars(defaultPath))
defaultPath = 0;
#endif
filename = PR_CALLOC(1024);
if (filename == NULL)
return NULL;
/***** RICHIE - CONVERT THIS
if (FE_PromptForFileName(window_id,
XP_GetString(MK_MSG_SAVE_ATTACH_AS),
defaultPath,
FALSE,
FALSE,
simple_copy,
filename) == -1)
{
return NULL;
}
*********/
p = fe_MakeAppleSingleDecodeStream(format_out,
data_obj,
URL_s,
window_id,
FALSE,
filename);
PR_FREE(filename);
return (p);
#endif
#endif
}
/*
** Create the Apple Doube Decode stream.
**
*/
PUBLIC NET_StreamClass *
fe_MakeAppleSingleDecodeStream (int format_out,
void *data_obj,
URL_Struct *URL_s,
MWContext *window_id,
PRBool write_as_binhex,
char *dst_filename)
{
AppleDoubleDecodeObject* obj;
NET_StreamClass* stream;
int encoding = kEncodeNone; /* default is that we don't know the encoding */
TRACEMSG(("Setting up apple single decode stream. Have URL: %s\n", URL_s->address));
stream = XP_NEW(NET_StreamClass);
if(stream == NULL)
return(NULL);
obj = XP_NEW(AppleDoubleDecodeObject);
if (obj == NULL)
{
PR_FREE(stream);
return(NULL);
}
stream->name = "AppleSingle Decode";
stream->complete = (MKStreamCompleteFunc) net_AppleDouble_Decode_Complete;
stream->abort = (MKStreamAbortFunc) net_AppleDouble_Decode_Abort;
stream->put_block = (MKStreamWriteFunc) net_AppleDouble_Decode_Write;
stream->is_write_ready = (MKStreamWriteReadyFunc) net_AppleDouble_Decode_Ready;
stream->data_object = obj;
stream->window_id = window_id;
/*
** setup all the need information on the apple double encoder.
*/
obj->in_buff = (char *)PR_CALLOC(1024);
if (obj->in_buff == NULL)
{
PR_FREE(obj);
PR_FREE(stream);
return (NULL);
}
obj->bytes_in_buff = 0;
if (write_as_binhex)
{
obj->binhex_stream =
fe_MakeBinHexEncodeStream(format_out,
data_obj,
URL_s,
window_id,
dst_filename);
if (obj->binhex_stream == NULL)
{
PR_FREE(obj);
PR_FREE(stream);
PR_FREE(obj->in_buff);
return NULL;
}
ap_decode_init(&(obj->ap_decode_obj),
TRUE,
TRUE,
obj->binhex_stream);
}
else
{
obj->binhex_stream = NULL;
ap_decode_init(&(obj->ap_decode_obj),
TRUE,
FALSE,
window_id);
#ifndef XP_MAC
obj->ap_decode_obj.is_binary = TRUE;
#endif
}
if (dst_filename)
{
XP_STRNCPY_SAFE(obj->ap_decode_obj.fname, dst_filename,
sizeof(obj->ap_decode_obj.fname));
}
/* If we are of a broken content-type, impose its encoding. */
if (URL_s->content_type
&& !XP_STRNCASECMP(URL_s->content_type, "x-uuencode-apple-single", 23))
obj->ap_decode_obj.encoding = kEncodeUU;
else
obj->ap_decode_obj.encoding = kEncodeNone;
TRACEMSG(("Returning stream from NET_AppleSingleDecode\n"));
return stream;
}

View File

@ -0,0 +1,616 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
/*
*
* apple-double.c
* --------------
*
* The codes to do apple double encoding/decoding.
*
* 02aug95 mym created.
* 27sep95 mym Add the XP_Mac to ensure the cross-platform.
*
*/
#include "nsID.h"
#include "nscore.h"
#include "msgCore.h"
#include "nsMsgAppleDouble.h"
#include "nsMsgAppleCodes.h"
#ifdef XP_MAC
#pragma warn_unusedarg off
#include "m_cvstrm.h"
#pragma cplusplus on
#include "InternetConfig.h"
#include "ufilemgr.h"
#include "BufferStream.h"
#include "Umimemap.h"
#include "uprefd.h"
#include "ulaunch.h"
void DecodingDone( appledouble_decode_object* p_ap_decode_obj );
OSErr my_FSSpecFromPathname(char* src_filename, FSSpec* fspec)
{
/* don't resolve aliases... */
return CFileMgr::FSSpecFromLocalUnixPath(src_filename, fspec, false);
}
char* my_PathnameFromFSSpec(FSSpec* fspec)
{
return CFileMgr::GetURLFromFileSpec(*fspec);
}
/* returns true if the resource fork should be sent */
PRBool isMacFile(char* filename)
{
Boolean returnValue = FALSE;
FSSpec fspec;
my_FSSpecFromPathname(filename, &fspec);
returnValue = CFileMgr::FileHasResourceFork(fspec);
/* always use IC even if the pref isn't checked since we have no
other way to determine if the resource is significant */
if ( returnValue )
{
CMimeMapper * mapper = CPrefs::sMimeTypes.FindMimeType(fspec);
if ( mapper )
{
returnValue = mapper->GetFileFlags()& ICmap_resource_fork_mask;
return returnValue;
}
// Get the Internet Config file mapping for this type
// and see if the existing resources are significant.
// First, get the type/creator of the file.
FInfo fileInfo;
OSErr err = ::FSpGetFInfo(&fspec, &fileInfo);
if (err == noErr)
{
if ( fileInfo.fdType == 'APPL' )
return TRUE;
ICMapEntry ent;
err = CInternetConfigInterface::GetInternetConfigFileMapping(fileInfo.fdType,
fileInfo.fdCreator,
fspec.name, &ent);
if (err == noErr)
{
// resource fork is significant if the resource fork mask bit is set
returnValue = (ent.flags & ICmap_resource_fork_mask) != 0;
}
}
}
return (PRBool) returnValue;
}
void DecodingDone( appledouble_decode_object* p_ap_decode_obj )
{
FSSpec fspec;
fspec.vRefNum = p_ap_decode_obj->vRefNum;
fspec.parID = p_ap_decode_obj->dirId;
fspec.name[0] = nsCRT::strlen(p_ap_decode_obj->fname);
XP_STRCPY((char*)fspec.name+1, p_ap_decode_obj->fname);
CMimeMapper * mapper = CPrefs::sMimeTypes.FindMimeType(fspec);
if( mapper && (mapper->GetLoadAction() == CMimeMapper::Launch ) )
{
LFileBufferStream file( fspec );
LaunchFile( &file );
}
}
#pragma cplusplus reset
/*
* ap_encode_init
* --------------
*
* Setup the encode envirment
*/
int ap_encode_init(
appledouble_encode_object *p_ap_encode_obj,
char* fname,
char* separator)
{
FSSpec fspec;
if (my_FSSpecFromPathname(fname, &fspec) != noErr )
return -1;
XP_MEMSET(p_ap_encode_obj, 0, sizeof(appledouble_encode_object));
/*
** Fill out the source file inforamtion.
*/
XP_MEMCPY(p_ap_encode_obj->fname, fspec.name+1, *fspec.name);
p_ap_encode_obj->fname[*fspec.name] = '\0';
p_ap_encode_obj->vRefNum = fspec.vRefNum;
p_ap_encode_obj->dirId = fspec.parID;
p_ap_encode_obj->boundary = XP_STRDUP(separator);
return noErr;
}
/*
** ap_encode_next
** --------------
**
** return :
** noErr : everything is ok
** errDone : when encoding is done.
** errors : otherwise.
*/
int ap_encode_next(
appledouble_encode_object* p_ap_encode_obj,
char *to_buff,
PRInt32 buff_size,
PRInt32* real_size)
{
int status;
/*
** install the out buff now.
*/
p_ap_encode_obj->outbuff = to_buff;
p_ap_encode_obj->s_outbuff = buff_size;
p_ap_encode_obj->pos_outbuff = 0;
/*
** first copy the outstandind data in the overflow buff to the out buffer.
*/
if (p_ap_encode_obj->s_overflow)
{
status = write_stream(p_ap_encode_obj,
p_ap_encode_obj->b_overflow,
p_ap_encode_obj->s_overflow);
if (status != noErr)
return status;
p_ap_encode_obj->s_overflow = 0;
}
/*
** go the next processing stage based on the current state.
*/
switch (p_ap_encode_obj->state)
{
case kInit:
/*
** We are in the starting position, fill out the header.
*/
status = fill_apple_mime_header(p_ap_encode_obj);
if (status != noErr)
break; /* some error happens */
p_ap_encode_obj->state = kDoingHeaderPortion;
status = ap_encode_header(p_ap_encode_obj, true);
/* it is the first time to calling */
if (status == errDone)
{
p_ap_encode_obj->state = kDoneHeaderPortion;
}
else
{
break; /* we need more work on header portion. */
}
/*
** we are done with the header, so let's go to the data port.
*/
p_ap_encode_obj->state = kDoingDataPortion;
status = ap_encode_data(p_ap_encode_obj, true);
/* it is first time call do data portion */
if (status == errDone)
{
p_ap_encode_obj->state = kDoneDataPortion;
status = noErr;
}
break;
case kDoingHeaderPortion:
status = ap_encode_header(p_ap_encode_obj, false);
/* continue with the header portion. */
if (status == errDone)
{
p_ap_encode_obj->state = kDoneHeaderPortion;
}
else
{
break; /* we need more work on header portion. */
}
/*
** start the data portion.
*/
p_ap_encode_obj->state = kDoingDataPortion;
status = ap_encode_data(p_ap_encode_obj, true);
/* it is the first time calling */
if (status == errDone)
{
p_ap_encode_obj->state = kDoneDataPortion;
status = noErr;
}
break;
case kDoingDataPortion:
status = ap_encode_data(p_ap_encode_obj, false);
/* it is not the first time */
if (status == errDone)
{
p_ap_encode_obj->state = kDoneDataPortion;
status = noErr;
}
break;
case kDoneDataPortion:
#if 0
status = write_stream(p_ap_encode_obj,
"\n-----\n\n",
8);
if (status == noErr)
#endif
status = errDone; /* we are really done. */
break;
}
*real_size = p_ap_encode_obj->pos_outbuff;
return status;
}
/*
** ap_encode_end
** -------------
**
** clear the apple encoding.
*/
int ap_encode_end(
appledouble_encode_object *p_ap_encode_obj,
PRBool is_aborting)
{
/*
** clear up the apple doubler.
*/
if (p_ap_encode_obj == NULL)
return noErr;
if (p_ap_encode_obj->fileId) /* close the file if it is open. */
FSClose(p_ap_encode_obj->fileId);
PR_FREEIF(p_ap_encode_obj->boundary); /* the boundary string. */
return noErr;
}
#endif /* the ifdef of XP_MAC */
/*
** The initial of the apple double decoder.
**
** Set up the next output stream based on the input.
*/
int ap_decode_init(
appledouble_decode_object* p_ap_decode_obj,
PRBool is_apple_single,
PRBool write_as_binhex,
void *closure)
{
XP_MEMSET(p_ap_decode_obj, 0, sizeof(appledouble_decode_object));
/* presume first buff starts a line */
p_ap_decode_obj->uu_starts_line = TRUE;
if (write_as_binhex)
{
p_ap_decode_obj->write_as_binhex = TRUE;
p_ap_decode_obj->binhex_stream = (NET_StreamClass*)closure;
p_ap_decode_obj->data_size = 0;
}
else
{
p_ap_decode_obj->write_as_binhex = FALSE;
p_ap_decode_obj->binhex_stream = NULL;
p_ap_decode_obj->context = (MWContext*)closure;
}
p_ap_decode_obj->is_apple_single = is_apple_single;
if (is_apple_single)
{
p_ap_decode_obj->encoding = kEncodeNone;
}
return NOERR;
}
static int ap_decode_state_machine(appledouble_decode_object* p_ap_decode_obj);
/*
* process the buffer
*/
int ap_decode_next(
appledouble_decode_object* p_ap_decode_obj,
char *in_buff,
PRInt32 buff_size)
{
/*
** install the buff to the decoder.
*/
p_ap_decode_obj->inbuff = in_buff;
p_ap_decode_obj->s_inbuff = buff_size;
p_ap_decode_obj->pos_inbuff = 0;
/*
** run off the decode state machine
*/
return ap_decode_state_machine(p_ap_decode_obj);
}
PRIVATE int ap_decode_state_machine(
appledouble_decode_object* p_ap_decode_obj)
{
int status = NOERR;
PRInt32 size;
switch (p_ap_decode_obj->state)
{
case kInit:
/*
** Make sure that there are stuff in the buff
** before we can parse the file head .
*/
if (p_ap_decode_obj->s_inbuff <=1 )
return NOERR;
if (p_ap_decode_obj->is_apple_single)
{
p_ap_decode_obj->state = kBeginHeaderPortion;
}
else
{
status = ap_seek_part_start(p_ap_decode_obj);
if (status != errDone)
return status;
p_ap_decode_obj->state = kBeginParseHeader;
}
status = ap_decode_state_machine(p_ap_decode_obj);
break;
case kBeginSeekBoundary:
p_ap_decode_obj->state = kSeekingBoundary;
status = ap_seek_to_boundary(p_ap_decode_obj, TRUE);
if (status == errDone)
{
p_ap_decode_obj->state = kBeginParseHeader;
status = ap_decode_state_machine(p_ap_decode_obj);
}
break;
case kSeekingBoundary:
status = ap_seek_to_boundary(p_ap_decode_obj, FALSE);
if (status == errDone)
{
p_ap_decode_obj->state = kBeginParseHeader;
status = ap_decode_state_machine(p_ap_decode_obj);
}
break;
case kBeginParseHeader:
p_ap_decode_obj->state = kParsingHeader;
status = ap_parse_header(p_ap_decode_obj, TRUE);
if (status == errDone)
{
if (p_ap_decode_obj->which_part == kDataPortion)
p_ap_decode_obj->state = kBeginDataPortion;
else if (p_ap_decode_obj->which_part == kHeaderPortion)
p_ap_decode_obj->state = kBeginHeaderPortion;
else
p_ap_decode_obj->state = kFinishing;
status = ap_decode_state_machine(p_ap_decode_obj);
}
break;
case kParsingHeader:
status = ap_parse_header(p_ap_decode_obj, FALSE);
if (status == errDone)
{
if (p_ap_decode_obj->which_part == kDataPortion)
p_ap_decode_obj->state = kBeginDataPortion;
else if (p_ap_decode_obj->which_part == kHeaderPortion)
p_ap_decode_obj->state = kBeginHeaderPortion;
else
p_ap_decode_obj->state = kFinishing;
status = ap_decode_state_machine(p_ap_decode_obj);
}
break;
case kBeginHeaderPortion:
p_ap_decode_obj->state = kProcessingHeaderPortion;
status = ap_decode_process_header(p_ap_decode_obj, TRUE);
if (status == errDone)
{
if (p_ap_decode_obj->is_apple_single)
p_ap_decode_obj->state = kBeginDataPortion;
else
p_ap_decode_obj->state = kBeginSeekBoundary;
status = ap_decode_state_machine(p_ap_decode_obj);
}
break;
case kProcessingHeaderPortion:
status = ap_decode_process_header(p_ap_decode_obj, FALSE);
if (status == errDone)
{
if (p_ap_decode_obj->is_apple_single)
p_ap_decode_obj->state = kBeginDataPortion;
else
p_ap_decode_obj->state = kBeginSeekBoundary;
status = ap_decode_state_machine(p_ap_decode_obj);
}
break;
case kBeginDataPortion:
p_ap_decode_obj->state = kProcessingDataPortion;
status = ap_decode_process_data(p_ap_decode_obj, TRUE);
if (status == errDone)
{
if (p_ap_decode_obj->is_apple_single)
p_ap_decode_obj->state = kFinishing;
else
p_ap_decode_obj->state = kBeginSeekBoundary;
status = ap_decode_state_machine(p_ap_decode_obj);
}
break;
case kProcessingDataPortion:
status = ap_decode_process_data(p_ap_decode_obj, FALSE);
if (status == errDone)
{
if (p_ap_decode_obj->is_apple_single)
p_ap_decode_obj->state = kFinishing;
else
p_ap_decode_obj->state = kBeginSeekBoundary;
status = ap_decode_state_machine(p_ap_decode_obj);
}
break;
case kFinishing:
if (p_ap_decode_obj->write_as_binhex)
{
if (p_ap_decode_obj->tmpfd)
{
/*
** It is time to append the data fork to bin hex encoder.
**
** The reason behind this dirt work is resource fork is the last
** piece in the binhex, while it is the first piece in apple double.
*/
XP_FileSeek(p_ap_decode_obj->tmpfd, 0L, SEEK_SET);
while (p_ap_decode_obj->data_size > 0)
{
char buff[1024];
size = PR_MIN(1024, p_ap_decode_obj->data_size);
XP_FileRead(buff, size, p_ap_decode_obj->tmpfd);
status = (*p_ap_decode_obj->binhex_stream->put_block)
(p_ap_decode_obj->binhex_stream->data_object,
buff,
size);
p_ap_decode_obj->data_size -= size;
}
}
if (p_ap_decode_obj->data_size <= 0)
{
/* CALL put_block with size == 0 to close a part. */
status = (*p_ap_decode_obj->binhex_stream->put_block)
(p_ap_decode_obj->binhex_stream->data_object,
NULL,
0);
if (status != NOERR)
break;
/* and now we are really done. */
status = errDone;
}
else
status = NOERR;
}
break;
}
return (status == errEOB) ? NOERR : status;
}
int ap_decode_end(
appledouble_decode_object* p_ap_decode_obj,
PRBool is_aborting)
{
/*
** clear up the apple doubler object.
*/
if (p_ap_decode_obj == NULL)
return NOERR;
PR_FREEIF(p_ap_decode_obj->boundary0);
#ifdef XP_MAC
if (p_ap_decode_obj->fileId)
FSClose(p_ap_decode_obj->fileId);
if( p_ap_decode_obj->vRefNum )
FlushVol(nil, p_ap_decode_obj->vRefNum );
#endif
if (p_ap_decode_obj->write_as_binhex)
{
/*
** make sure close the binhex stream too.
*/
if (is_aborting)
{
(*p_ap_decode_obj->binhex_stream->abort)
(p_ap_decode_obj->binhex_stream->data_object, 0);
}
else
{
(*p_ap_decode_obj->binhex_stream->complete)
(p_ap_decode_obj->binhex_stream->data_object);
}
if (p_ap_decode_obj->tmpfd)
XP_FileClose(p_ap_decode_obj->tmpfd);
if (p_ap_decode_obj->tmpfname)
{
XP_FileRemove(p_ap_decode_obj->tmpfname, xpTemporary);
/* remove tmp file if we used it */
PR_FREEIF(p_ap_decode_obj->tmpfname); /* and release the file name too. */
}
}
else if (p_ap_decode_obj->fd)
{
XP_FileClose(p_ap_decode_obj->fd);
}
#ifdef XP_MAC
if( !is_aborting )
DecodingDone( p_ap_decode_obj);
#endif
return NOERR;
}

View File

@ -0,0 +1,735 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
/*
*
* apple_double_encode.c
* ---------------------
*
* The routines doing the Apple Double Encoding.
*
* 2aug95 mym Created.
*
*/
#include "nscore.h"
#include "msgCore.h"
#include "nsMsgAppleDouble.h"
#include "nsMsgAppleCodes.h"
#ifdef XP_MAC
extern int MK_UNABLE_TO_OPEN_TMP_FILE;
extern int MK_MIME_ERROR_WRITING_FILE;
#include <Errors.h>
/*
** Local Functions prototypes.
*/
PRIVATE int output64chunk( appledouble_encode_object* p_ap_encode_obj,
int c1, int c2, int c3, int pads);
PRIVATE int to64(appledouble_encode_object* p_ap_encode_obj,
char *p,
int in_size);
PRIVATE int finish64(appledouble_encode_object* p_ap_encode_obj);
#define BUFF_LEFT(p) ((p)->s_outbuff - (p)->pos_outbuff)
/*
** write_stream.
*/
int write_stream(
appledouble_encode_object *p_ap_encode_obj,
char *out_string,
int len)
{
if (p_ap_encode_obj->pos_outbuff + len < p_ap_encode_obj->s_outbuff)
{
XP_MEMCPY(p_ap_encode_obj->outbuff + p_ap_encode_obj->pos_outbuff,
out_string,
len);
p_ap_encode_obj->pos_outbuff += len;
return noErr;
}
else
{
/*
** If the buff doesn't have enough space, use the overflow buffer then.
*/
int s_len = p_ap_encode_obj->s_outbuff - p_ap_encode_obj->pos_outbuff;
XP_MEMCPY(p_ap_encode_obj->outbuff + p_ap_encode_obj->pos_outbuff,
out_string,
s_len);
XP_MEMCPY(p_ap_encode_obj->b_overflow + p_ap_encode_obj->s_overflow,
out_string + s_len,
p_ap_encode_obj->s_overflow += (len - s_len));
p_ap_encode_obj->pos_outbuff += s_len;
return errEOB;
}
}
int fill_apple_mime_header(
appledouble_encode_object *p_ap_encode_obj)
{
int status;
char tmpstr[266];
#if 0
// strcpy(tmpstr, "Content-Type: multipart/mixed; boundary=\"-\"\n\n---\n");
// status = write_stream(p_ap_encode_env,
// tmpstr,
// strlen(tmpstr));
// if (status != noErr)
// return status;
sprintf(tmpstr,
"Content-Type: multipart/appledouble; boundary=\"=\"; name=\"");
status = write_stream(p_ap_encode_obj,
tmpstr,
strlen(tmpstr));
if (status != noErr)
return status;
status = write_stream(p_ap_encode_obj,
p_ap_encode_obj->fname,
nsCRT::strlen(p_ap_encode_obj->fname));
if (status != noErr)
return status;
XP_SPRINTF(tmpstr,
"\"\nContent-Disposition: inline; filename=\"%s\"\n\n\n--=\n",
p_ap_encode_obj->fname);
#endif
XP_SPRINTF(tmpstr, "--%s"CRLF, p_ap_encode_obj->boundary);
status = write_stream(p_ap_encode_obj,
tmpstr,
nsCRT::strlen(tmpstr));
return status;
}
int ap_encode_file_infor(
appledouble_encode_object *p_ap_encode_obj)
{
CInfoPBRec cipbr;
HFileInfo *fpb = (HFileInfo *)&cipbr;
ap_header head;
ap_entry entries[NUM_ENTRIES];
ap_dates dates;
short i;
long comlen, procID;
DateTimeRec cur_time;
unsigned long cur_secs;
IOParam vinfo;
GetVolParmsInfoBuffer vp;
DTPBRec dtp;
char comment[256];
Str63 fname;
int status;
strcpy((char *)fname+1,p_ap_encode_obj->fname);
fname[0] = nsCRT::strlen(p_ap_encode_obj->fname);
fpb->ioNamePtr = fname;
fpb->ioDirID = p_ap_encode_obj->dirId;
fpb->ioVRefNum = p_ap_encode_obj->vRefNum;
fpb->ioFDirIndex = 0;
if (PBGetCatInfoSync(&cipbr) != noErr)
{
return errFileOpen;
}
/* get a file comment, if possible */
procID = 0;
GetWDInfo(p_ap_encode_obj->vRefNum, &fpb->ioVRefNum, &fpb->ioDirID, &procID);
memset((void *) &vinfo, '\0', sizeof (vinfo));
vinfo.ioCompletion = nil;
vinfo.ioVRefNum = fpb->ioVRefNum;
vinfo.ioBuffer = (Ptr) &vp;
vinfo.ioReqCount = sizeof (vp);
comlen = 0;
if (PBHGetVolParmsSync((HParmBlkPtr) &vinfo) == noErr &&
((vp.vMAttrib >> bHasDesktopMgr) & 1))
{
memset((void *) &dtp, '\0', sizeof (dtp));
dtp.ioVRefNum = fpb->ioVRefNum;
if (PBDTGetPath(&dtp) == noErr)
{
dtp.ioCompletion = nil;
dtp.ioDTBuffer = (Ptr) comment;
dtp.ioNamePtr = fpb->ioNamePtr;
dtp.ioDirID = fpb->ioFlParID;
if (PBDTGetCommentSync(&dtp) == noErr)
comlen = dtp.ioDTActCount;
}
}
/* write header */
// head.magic = dfork ? APPLESINGLE_MAGIC : APPLEDOUBLE_MAGIC;
head.magic = APPLEDOUBLE_MAGIC; /* always do apple double */
head.version = VERSION;
memset(head.fill, '\0', sizeof (head.fill));
head.entries = NUM_ENTRIES - 1;
status = to64(p_ap_encode_obj,
(char *) &head,
sizeof (head));
if (status != noErr)
return status;
/* write entry descriptors */
entries[0].offset = sizeof (head) + sizeof (ap_entry) * head.entries;
entries[0].id = ENT_NAME;
entries[0].length = *fpb->ioNamePtr;
entries[1].id = ENT_FINFO;
entries[1].length = sizeof (FInfo) + sizeof (FXInfo);
entries[2].id = ENT_DATES;
entries[2].length = sizeof (ap_dates);
entries[3].id = ENT_COMMENT;
entries[3].length = comlen;
entries[4].id = ENT_RFORK;
entries[4].length = fpb->ioFlRLgLen;
entries[5].id = ENT_DFORK;
entries[5].length = fpb->ioFlLgLen;
/* correct the link in the entries. */
for (i = 1; i < NUM_ENTRIES; ++i)
{
entries[i].offset = entries[i-1].offset + entries[i-1].length;
}
status = to64(p_ap_encode_obj,
(char *) entries,
sizeof (ap_entry) * head.entries);
if (status != noErr)
return status;
/* write name */
status = to64(p_ap_encode_obj,
(char *) fpb->ioNamePtr + 1,
*fpb->ioNamePtr);
if (status != noErr)
return status;
/* write finder info */
status = to64(p_ap_encode_obj,
(char *) &fpb->ioFlFndrInfo,
sizeof (FInfo));
if (status != noErr)
return status;
status = to64(p_ap_encode_obj,
(char *) &fpb->ioFlXFndrInfo,
sizeof (FXInfo));
if (status != noErr)
return status;
/* write dates */
GetTime(&cur_time);
DateToSeconds(&cur_time, &cur_secs);
dates.create = fpb->ioFlCrDat + CONVERT_TIME;
dates.modify = fpb->ioFlMdDat + CONVERT_TIME;
dates.backup = fpb->ioFlBkDat + CONVERT_TIME;
dates.access = cur_secs + CONVERT_TIME;
status = to64(p_ap_encode_obj,
(char *) &dates,
sizeof (ap_dates));
if (status != noErr)
return status;
/* write comment */
if (comlen)
{
status = to64(p_ap_encode_obj,
comment,
comlen * sizeof(char));
}
/*
** Get some help information on deciding the file type.
*/
if (fpb->ioFlFndrInfo.fdType == 'TEXT' || fpb->ioFlFndrInfo.fdType == 'text')
{
p_ap_encode_obj->text_file_type = true;
}
return status;
}
/*
** ap_encode_header
**
** encode the file header and the resource fork.
**
*/
int ap_encode_header(
appledouble_encode_object* p_ap_encode_obj,
PRBool firstime)
{
Str255 name;
char rd_buff[256];
short fileId;
OSErr retval = noErr;
int status;
long inCount;
if (firstime)
{
XP_STRCPY(rd_buff,
"Content-Type: application/applefile\nContent-Transfer-Encoding: base64\n\n");
status = write_stream(p_ap_encode_obj,
rd_buff,
strlen(rd_buff));
if (status != noErr)
return status;
status = ap_encode_file_infor(p_ap_encode_obj);
if (status != noErr)
return status;
/*
** preparing to encode the resource fork.
*/
name[0] = strlen(p_ap_encode_obj->fname);
strcpy((char *)name+1, p_ap_encode_obj->fname);
if (HOpenRF(p_ap_encode_obj->vRefNum, p_ap_encode_obj->dirId,
name, fsRdPerm,
&p_ap_encode_obj->fileId) != noErr)
{
return errFileOpen;
}
}
fileId = p_ap_encode_obj->fileId;
while (retval == noErr)
{
if (BUFF_LEFT(p_ap_encode_obj) < 400)
break;
inCount = 256;
retval = FSRead(fileId, &inCount, rd_buff);
if (inCount)
{
status = to64(p_ap_encode_obj,
rd_buff,
inCount);
if (status != noErr)
return status;
}
}
if (retval == eofErr)
{
FSClose(fileId);
status = finish64(p_ap_encode_obj);
if (status != noErr)
return status;
/*
** write out the boundary
*/
XP_SPRINTF(rd_buff,
CRLF"--%s"CRLF,
p_ap_encode_obj->boundary);
status = write_stream(p_ap_encode_obj,
rd_buff,
nsCRT::strlen(rd_buff));
if (status == noErr)
status = errDone;
}
return status;
}
static void replace(char *p, int len, char frm, char to)
{
for (; len > 0; len--, p++)
if (*p == frm) *p = to;
}
/* Description of the various file formats and their magic numbers */
struct magic
{
char *name; /* Name of the file format */
char *num; /* The magic number */
int len; /* Length (0 means strlen(magicnum)) */
};
/* The magic numbers of the file formats we know about */
static struct magic magic[] =
{
{ "image/gif", "GIF", 0 },
{ "image/jpeg", "\377\330\377", 0 },
{ "video/mpeg", "\0\0\001\263", 4 },
{ "application/postscript", "%!", 0 },
};
static int num_magic = (sizeof(magic)/sizeof(magic[0]));
static char *text_type = TEXT_PLAIN; /* the text file type. */
static char *default_type = APPLICATION_OCTET_STREAM;
/*
* Determins the format of the file "inputf". The name
* of the file format (or NULL on error) is returned.
*/
PRIVATE char *magic_look(char *inbuff, int numread)
{
int i, j;
for (i=0; i<num_magic; i++)
{
if (magic[i].len == 0)
magic[i].len = nsCRT::strlen(magic[i].num);
}
for (i=0; i<num_magic; i++)
{
if (numread >= magic[i].len)
{
for (j=0; j<magic[i].len; j++)
{
if (inbuff[j] != magic[i].num[j]) break;
}
if (j == magic[i].len)
return magic[i].name;
}
}
return default_type;
}
/*
** ap_encode_data
**
** ---------------
**
** encode on the data fork.
**
*/
int ap_encode_data(
appledouble_encode_object* p_ap_encode_obj,
PRBool firstime)
{
Str255 name;
char rd_buff[256];
short fileId;
OSErr retval = noErr;
long in_count;
int status;
if (firstime)
{
char* magic_type;
/*
** preparing to encode the data fork.
*/
name[0] = nsCRT::strlen(p_ap_encode_obj->fname);
XP_STRCPY((char*)name+1, p_ap_encode_obj->fname);
if (HOpen( p_ap_encode_obj->vRefNum,
p_ap_encode_obj->dirId,
name,
fsRdPerm,
&fileId) != noErr)
{
return errFileOpen;
}
p_ap_encode_obj->fileId = fileId;
if (!p_ap_encode_obj->text_file_type)
{
OSErr err;
FSSpec file_spec;
char* path;
Bool do_magic = true;
/* First attempt to get the file's mime type via FE_FileType.
If that fails, we'll do a "magic_look"
*/
err = FSMakeFSSpec(p_ap_encode_obj->vRefNum, p_ap_encode_obj->dirId, name, &file_spec);
if (err == noErr)
{
path = my_PathnameFromFSSpec(&file_spec);
if (path != NULL)
{
char* ignore;
FE_FileType(path, &do_magic, &magic_type, &ignore);
/*
if we ended up with the default type, dispose of it
so we can do a magic_look
*/
if (do_magic && magic_type)
PR_FREEIF(magic_type);
}
}
if (do_magic)
{
/*
** do a smart check for the file type.
*/
in_count = 256;
retval = FSRead(fileId, &in_count, rd_buff);
magic_type = magic_look(rd_buff, in_count);
/* don't forget to rewind the index to start point. */
SetFPos(fileId, fsFromStart, 0L);
}
}
else
{
magic_type = text_type; /* we already know it is a text type. */
}
/*
** the data portion header information.
*/
XP_SPRINTF(rd_buff,
"Content-Type: %s; name=\"%s\"" CRLF "Content-Transfer-Encoding: base64" CRLF "Content-Disposition: inline; filename=\"%s\""CRLF CRLF,
magic_type,
p_ap_encode_obj->fname,
p_ap_encode_obj->fname);
status = write_stream(p_ap_encode_obj,
rd_buff,
nsCRT::strlen(rd_buff));
if (status != noErr)
return status;
}
while (retval == noErr)
{
if (BUFF_LEFT(p_ap_encode_obj) < 400)
break;
in_count = 256;
retval = FSRead(p_ap_encode_obj->fileId,
&in_count,
rd_buff);
if (in_count)
{
/* replace(rd_buff, in_count, '\r', '\n'); */
/* ** may be need to do character set conversion here for localization. ** */
status = to64(p_ap_encode_obj,
rd_buff,
in_count);
if (status != noErr)
return status;
}
}
if (retval == eofErr)
{
FSClose(p_ap_encode_obj->fileId);
status = finish64(p_ap_encode_obj);
if (status != noErr)
return status;
/* write out the boundary */
XP_SPRINTF(rd_buff,
CRLF"--%s--"CRLF CRLF,
p_ap_encode_obj->boundary);
status = write_stream(p_ap_encode_obj,
rd_buff,
nsCRT::strlen(rd_buff));
if (status == noErr)
status = errDone;
}
return status;
}
static char basis_64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/*
** convert the stream in the inbuff to 64 format and put it in the out buff.
** To make the life easier, the caller will responcable of the cheking of the outbuff's bundary.
*/
PRIVATE int
to64(appledouble_encode_object* p_ap_encode_obj,
char *p,
int in_size)
{
int status;
int c1, c2, c3, ct;
unsigned char *inbuff = (unsigned char*)p;
ct = p_ap_encode_obj->ct; /* the char count left last time. */
/*
** resume the left state of the last conversion.
*/
switch (p_ap_encode_obj->state64)
{
case 0:
p_ap_encode_obj->c1 = c1 = *inbuff ++;
if (--in_size <= 0)
{
p_ap_encode_obj->state64 = 1;
return noErr;
}
p_ap_encode_obj->c2 = c2 = *inbuff ++;
if (--in_size <= 0)
{
p_ap_encode_obj->state64 = 2;
return noErr;
}
c3 = *inbuff ++; --in_size;
break;
case 1:
c1 = p_ap_encode_obj->c1;
p_ap_encode_obj->c2 = c2 = *inbuff ++;
if (--in_size <= 0)
{
p_ap_encode_obj->state64 = 2;
return noErr;
}
c3 = *inbuff ++; --in_size;
break;
case 2:
c1 = p_ap_encode_obj->c1;
c2 = p_ap_encode_obj->c2;
c3 = *inbuff ++; --in_size;
break;
}
while (in_size >= 0)
{
status = output64chunk(p_ap_encode_obj,
c1,
c2,
c3,
0);
if (status != noErr)
return status;
ct += 4;
if (ct > 71)
{
status = write_stream(p_ap_encode_obj,
CRLF,
2);
if (status != noErr)
return status;
ct = 0;
}
if (in_size <= 0)
{
p_ap_encode_obj->state64 = 0;
break;
}
c1 = (int)*inbuff++;
if (--in_size <= 0)
{
p_ap_encode_obj->c1 = c1;
p_ap_encode_obj->state64 = 1;
break;
}
c2 = *inbuff++;
if (--in_size <= 0)
{
p_ap_encode_obj->c1 = c1;
p_ap_encode_obj->c2 = c2;
p_ap_encode_obj->state64 = 2;
break;
}
c3 = *inbuff++;
in_size--;
}
p_ap_encode_obj->ct = ct;
return status;
}
/*
** clear the left base64 encodes.
*/
PRIVATE int
finish64(appledouble_encode_object* p_ap_encode_obj)
{
int status;
switch (p_ap_encode_obj->state64)
{
case 0:
break;
case 1:
status = output64chunk(p_ap_encode_obj,
p_ap_encode_obj->c1,
0,
0,
2);
break;
case 2:
status = output64chunk(p_ap_encode_obj,
p_ap_encode_obj->c1,
p_ap_encode_obj->c2,
0,
1);
break;
}
status = write_stream(p_ap_encode_obj, CRLF, 2);
p_ap_encode_obj->state64 = 0;
p_ap_encode_obj->ct = 0;
return status;
}
PRIVATE int output64chunk(
appledouble_encode_object* p_ap_encode_obj,
int c1, int c2, int c3, int pads)
{
char tmpstr[32];
char *p = tmpstr;
*p++ = basis_64[c1>>2];
*p++ = basis_64[((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)];
if (pads == 2)
{
*p++ = '=';
*p++ = '=';
}
else if (pads)
{
*p++ = basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)];
*p++ = '=';
}
else
{
*p++ = basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)];
*p++ = basis_64[c3 & 0x3F];
}
return write_stream(p_ap_encode_obj,
tmpstr,
p-tmpstr);
}
#endif /* if define XP_MAC */