filename) == -1)
filename) == -1 */ )
PRIVATE void binhex_process(	binhex_decode_object *p_bh_decode_obj){	int32  	status;	uint16 	tmpcrc, cval;	unsigned char  ctmp, c = p_bh_decode_obj->rlebuf;		/* do CRC */	ctmp = p_bh_decode_obj->inCRC ? c : 0;	cval = p_bh_decode_obj->CRC   & 0xf000;	tmpcrc = ((uint16) (p_bh_decode_obj->CRC << 4) | 						 (ctmp >> 4))					 ^ (cval | (cval >> 7) | 					 	(cval >> 12));	cval = tmpcrc & 0xf000;	p_bh_decode_obj->CRC = ((uint16) (tmpcrc << 4) | 							(ctmp & 0x0f))						 ^ (cval | (cval >> 7) | 						 	(cval >> 12));	/* handle state */	switch (p_bh_decode_obj->state) 	{		case BINHEX_STATE_START:			p_bh_decode_obj->state 		= BINHEX_STATE_FNAME;			p_bh_decode_obj->count 		= 1;#ifndef XP_MAC			p_bh_decode_obj->name = XP_ALLOC(64);#endif			*(p_bh_decode_obj->name) = (c & 63);			break;					case BINHEX_STATE_FNAME:			p_bh_decode_obj->name[p_bh_decode_obj->count] = c;						if (p_bh_decode_obj->count++ > *(p_bh_decode_obj->name)) 			{#if 0							char* p;				/* convert it to the c-string too. 		*/				c = *(p_bh_decode_obj->name);				p = p_bh_decode_obj->name;							while (c--)				{					*p = *(p+1); p++;				}									*p = '\0';#endif								p_bh_decode_obj->state = BINHEX_STATE_HEADER;				p_bh_decode_obj->count = 0;			}			break;					case BINHEX_STATE_HEADER:			((char *)&p_bh_decode_obj->head)[p_bh_decode_obj->count] = c;			if (++p_bh_decode_obj->count == 18) 			{#ifndef XP_MAC				if (sizeof(binhex_header) != 18)	/* fix the alignment problem in some OS */				{					char *p = (char *)&p_bh_decode_obj->head;					p += 19;					for (c = 0; c < 8; c++)					{						*p = *(p-2);	p--;					} 				}#endif				p_bh_decode_obj->state = BINHEX_STATE_HCRC;				p_bh_decode_obj->inCRC = 1;				p_bh_decode_obj->count = 0;			}			break;					case BINHEX_STATE_DFORK:		case BINHEX_STATE_RFORK:			p_bh_decode_obj->outbuff[p_bh_decode_obj->pos_outbuff++] = c;			if (-- p_bh_decode_obj->count == 0) 			{#ifdef XP_MAC					long howMuch = p_bh_decode_obj->pos_outbuff;				status = FSWrite(p_bh_decode_obj->fileId, 								&howMuch,								p_bh_decode_obj->outbuff);												FSClose(p_bh_decode_obj->fileId);#else												/* only output data fork in the non-mac system.			*/				if (p_bh_decode_obj->state == BINHEX_STATE_DFORK)				{					status = p_bh_decode_obj->fileId->write(p_bh_decode_obj->outbuff,                                                   p_bh_decode_obj->pos_outbuff)									    == p_bh_decode_obj->pos_outbuff ? NOERR : errFileWrite;													p_bh_decode_obj->fileId->close();				}				else				{					status = NOERR;				/* do nothing for resource fork.	*/				}#endif				p_bh_decode_obj->pos_outbuff = 0;								if (status != NOERR)					p_bh_decode_obj->state = status;				else				{					p_bh_decode_obj->state ++;					p_bh_decode_obj->fileId = 0;				}				p_bh_decode_obj->inCRC = 1;			}			else if (p_bh_decode_obj->pos_outbuff >= MAX_BUFF_SIZE)			{				#ifdef XP_MAC					long howMuch = p_bh_decode_obj->pos_outbuff;				status = FSWrite(p_bh_decode_obj->fileId, 								&howMuch,								p_bh_decode_obj->outbuff);#else				if (p_bh_decode_obj->state == BINHEX_STATE_DFORK)				{					status = p_bh_decode_obj->fileId->write(p_bh_decode_obj->outbuff,								                                  p_bh_decode_obj->pos_outbuff) 									    == p_bh_decode_obj->pos_outbuff ? NOERR : errFileWrite; 				}				else				{					status = NOERR;			/* don't care about the resource fork. */				}#endif											if (status != NOERR)					p_bh_decode_obj->state = status;									p_bh_decode_obj->pos_outbuff = 0;			}			break;					case BINHEX_STATE_HCRC:		case BINHEX_STATE_DCRC:		case BINHEX_STATE_RCRC:			if (!p_bh_decode_obj->count++) 			{				p_bh_decode_obj->fileCRC = (unsigned short) c << 8;			} 			else 			{				if ((p_bh_decode_obj->fileCRC | c) != p_bh_decode_obj->CRC) 				{					if (p_bh_decode_obj->state > BINHEX_STATE_HCRC) 					{#ifdef XP_MAC						HDelete(p_bh_decode_obj->vRefNum, 								p_bh_decode_obj->parID,								(unsigned char*)p_bh_decode_obj->name);#else						p_bh_decode_obj->name->Delete(PR_FALSE);#endif					}					p_bh_decode_obj->state = errDecoding;					break;				}								/*				** passed the CRC check!!!				*/				p_bh_decode_obj->CRC = 0;				if (++ p_bh_decode_obj->state == BINHEX_STATE_FINISH) 				{#ifdef XP_MAC					FInfo 	finfo;					/* set back the file information.before we declare done ! */					finfo.fdType 	= p_bh_decode_obj->head.type;					finfo.fdCreator = p_bh_decode_obj->head.creator;					finfo.fdFlags 	= p_bh_decode_obj->head.flags & 0xf800;										HSetFInfo(p_bh_decode_obj->vRefNum, 								p_bh_decode_obj->parID, 								(unsigned char *)p_bh_decode_obj->name, 								&finfo);#endif					/* 	now We are done with everything.	*/							p_bh_decode_obj->state++;					break;				}								if (p_bh_decode_obj->state == BINHEX_STATE_DFORK) 				{#ifdef XP_MAC					StandardFileReply	reply;					if( !p_bh_decode_obj->mSpec )					{							StandardPutFile("\pSave decoded file as:", 									(unsigned char *)p_bh_decode_obj->name, 									&reply);															if (!reply.sfGood) 						{							p_bh_decode_obj->state = errUsrCancel;							break;						}					}					else					{						reply.sfFile.vRefNum = p_bh_decode_obj->mSpec->vRefNum;						reply.sfFile.parID = p_bh_decode_obj->mSpec->parID;						nsCRT::memcpy(&reply.sfFile.name, p_bh_decode_obj->mSpec->name , 63 );							}															nsCRT::memcpy(p_bh_decode_obj->name, 							reply.sfFile.name, 							*(reply.sfFile.name)+1);	/* save the new file name.	*/											p_bh_decode_obj->vRefNum = reply.sfFile.vRefNum;					p_bh_decode_obj->parID   = reply.sfFile.parID;											HDelete(reply.sfFile.vRefNum,									reply.sfFile.parID, 									reply.sfFile.name);										status = HCreate(p_bh_decode_obj->vRefNum, 									 p_bh_decode_obj->parID, 									 reply.sfFile.name,									 p_bh_decode_obj->head.creator, 									 p_bh_decode_obj->head.type);#else	/* non-mac OS case */					char* filename;										filename = XP_ALLOC(1024);					if (filename == NULL ||						FE_PromptForFileName(p_bh_decode_obj->context, 										XP_GetString(MK_MSG_SAVE_DECODED_AS),										0,										FALSE,										FALSE,										simple_copy,										filename) == -1)					{						FREEIF(filename);						p_bh_decode_obj->state = errUsrCancel;						break;					}					if (p_bh_decode_obj->name)            delete p_bh_decode_obj->fileId;					p_bh_decode_obj->name = new nsFileSpec(filename);          if (!p_bh_decode_obj->name)            status = errFileOpen;          else          {					  p_bh_decode_obj->fileId = new nsIOFileStream(*(p_bh_decode_obj->name), (PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE));					  if (p_bh_decode_obj->fileId == NULL)						  status = errFileOpen;					  else						  status = NOERR;          }					XP_FREE(filename);					#endif					if (status != NOERR)						p_bh_decode_obj->state = status;											p_bh_decode_obj->count 						= ntohl(p_bh_decode_obj->head.dlen);				}				else				{					p_bh_decode_obj->count 						= ntohl(p_bh_decode_obj->head.rlen);	/* it should in host byte order */				}								if (p_bh_decode_obj->count) 				{					p_bh_decode_obj->inCRC = 0;#ifdef XP_MAC					if (p_bh_decode_obj->state == BINHEX_STATE_DFORK)						status = HOpen(p_bh_decode_obj->vRefNum,									   p_bh_decode_obj->parID,									   (unsigned char*)p_bh_decode_obj->name,									   fsWrPerm,									   &(p_bh_decode_obj->fileId));					else						status = HOpenRF(p_bh_decode_obj->vRefNum,									   p_bh_decode_obj->parID,									   (unsigned char*)p_bh_decode_obj->name,									   fsWrPerm,									   &(p_bh_decode_obj->fileId));					if (status != NOERR) 					{						p_bh_decode_obj->state = errFileOpen;						HDelete(p_bh_decode_obj->vRefNum,										p_bh_decode_obj->parID, 										(unsigned char*)p_bh_decode_obj->name);						break;					}#else					/* for None Mac OS -- nothing is required, file already open.	*/					#endif											} 				else 				{					/* nothing inside, so skip to the next state. */					p_bh_decode_obj->state ++;				}			}			break;	}		return;}