home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-28 | 12.3 KB | 396 lines | [TEXT/MPS ] |
- /*
- File: VirtualEnclosedBinaryMacFile.cp
-
- Copyright: © 1991-1994 by Apple Computer, Inc.
- All rights reserved.
-
- Part of the AOCE Sample SMSAM Package. Consult the license
- which came with this software for your specific legal rights.
-
- */
-
-
-
- #ifndef __BLJSTANDARDINCLUDES__
- #include "BLJStandardIncludes.h"
- #endif
-
- #ifndef __VIRTUALFILE__
- #include "VirtualFile.h"
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __FILEINFILE__
- #include "FileInFile.h"
- #endif
-
- #ifndef __VIRTUALENCLOSEDBINARYMACFILE__
- #include "VirtualEnclosedBinaryMacFile.h"
- #endif
-
- #ifndef __UTILITIES__
- #include "Utilities.h"
- #endif
-
- #pragma segment VirtualEnclosedBinaryMacFile
-
- // constructor for an already-packed file created in a raw file, which came from the host
-
- TVirtualEnclosedBinaryMacFile::TVirtualEnclosedBinaryMacFile(TVirtualFile* rawFile, unsigned long enclosedFilePacker):
- TVirtualMacFile (),
- fFile ( rawFile ),
- fRFFile ( nil ),
- fDFFile ( nil ),
- fHeaderSizeInVirtualFile ( 0 ),
- fEnclosedFilePacker ( enclosedFilePacker ),
- fValidEnclosedBinaryFile ( false )
- {
- long temp;
-
- fFile->RegisterReference();
- Lock ();
-
- switch ( fEnclosedFilePacker )
- {
- case kCCSBinary:
- fHeaderSizeInVirtualFile = sizeof ( fFileInfoCCSBinary );
- temp = fHeaderSizeInVirtualFile;
- fValidEnclosedBinaryFile = (fFile->ReadData ( fFileInfoCCSBinary, temp ) == noErr);
- Unlock ();
-
- // Now, validate that we've got a 'valid' CCS Binary file
- long ccsBinaryFileSize;
- fValidEnclosedBinaryFile = fValidEnclosedBinaryFile && (fFile->GetEnd(ccsBinaryFileSize) == noErr);
- fValidEnclosedBinaryFile = fValidEnclosedBinaryFile && (ccsBinaryFileSize >= fHeaderSizeInVirtualFile );
-
- if (fValidEnclosedBinaryFile)
- {
- BlockMove(&fFileInfoCCSBinary[kCCSDFLength], &fDataBytes, 4);
- BlockMove(&fFileInfoCCSBinary[kCCSRFLength], &fResBytes, 4);
-
- fValidEnclosedBinaryFile = (fDataBytes >= 0) && (fResBytes >= 0)
- && (fDataBytes + fResBytes + fHeaderSizeInVirtualFile <= ccsBinaryFileSize);
-
- /* now copy appropriate things for general usage */
- BlockMove(&fFileInfoCCSBinary[kCCSFnLen], fName, 64); //move length byte and name
- BlockMove(&fFileInfoCCSBinary[kCCSFType], &fFinderInfo, 16); //move Finder info (16 bytes)
- BlockMove(&fFileInfoCCSBinary[kCCSCDate], &fCreationDate, 4);
- BlockMove(&fFileInfoCCSBinary[kCCSModDate], &fModifiedDate, 4);
- fDataForkPadBytes = 0;
- fResourceForkPadBytes = 0;
- }
- break;
-
- case kMacBinaryII:
- fHeaderSizeInVirtualFile = sizeof ( fFileInfoMacBinary );
- temp = fHeaderSizeInVirtualFile;
- fValidEnclosedBinaryFile = (fFile->ReadData ( fFileInfoMacBinary, temp ) == noErr);
- Unlock ();
-
- fValidEnclosedBinaryFile = fValidEnclosedBinaryFile
- && (fFileInfoMacBinary[0] == 0x00) && (fFileInfoMacBinary[74] == 0x00);
-
- BlockMove(&fFileInfoMacBinary[kCRC], &fCRC, 2);
-
- fValidEnclosedBinaryFile = fValidEnclosedBinaryFile && ( DoCRC(fFileInfoMacBinary, kBytesForCRC) == fCRC);
-
- if (fValidEnclosedBinaryFile)
- {
- BlockMove(&fFileInfoMacBinary[kDFLength], &fDataBytes, 4);
- BlockMove(&fFileInfoMacBinary[kRFLength], &fResBytes, 4);
-
- /* now copy appropriate things for general usage */
- BlockMove(&fFileInfoMacBinary[kFileNameLen], fName, 64); //move length byte and name
- BlockMove(&fFileInfoMacBinary[kFileType], &fFinderInfo, 16); //move Finder info (16 bytes)
- BlockMove(&fFileInfoMacBinary[kFileCrDate], &fCreationDate, 4);
- BlockMove(&fFileInfoMacBinary[kFileMdDate], &fModifiedDate, 4);
- BlockMove(&fFileInfoMacBinary[kLenSecHdr], &fSecondaryHeaderLength, 2);
- fDataForkPadBytes = 128L - (fDataBytes % 128L) % 128L;
- fResourceForkPadBytes = 128L - (fResBytes % 128L) % 128L;
- fHeaderSizeInVirtualFile += fSecondaryHeaderLength;
- }
- break;
-
- default:
- fValidEnclosedBinaryFile = false;
- }
- }
-
- //--------------------------------------------------------------------------------
-
-
- TVirtualEnclosedBinaryMacFile::~TVirtualEnclosedBinaryMacFile()
- {
- delete fRFFile;
- delete fDFFile;
-
- fFile->UnregisterReference();
- }
-
- //--------------------------------------------------------------------------------
-
- Boolean TVirtualEnclosedBinaryMacFile::IsValidEnclosedBinaryFile() const
- {
- return fValidEnclosedBinaryFile;
- }
-
- //--------------------------------------------------------------------------------
-
-
- OSErr TVirtualEnclosedBinaryMacFile::Open ()
- {
- if (fDFFile == nil)
- {
- fDFFile = new TFileInFile(fFile, fHeaderSizeInVirtualFile, fDataBytes);
- }
-
- if (fRFFile == nil)
- {
- fRFFile = new TFileInFile(fFile, fHeaderSizeInVirtualFile + fDataBytes + fDataForkPadBytes, fResBytes);
- }
-
- return noErr;
- }
-
- //--------------------------------------------------------------------------------
-
- OSErr TVirtualEnclosedBinaryMacFile::Close ()
- {
- return noErr;
- }
-
- //--------------------------------------------------------------------------------
-
- OSErr TVirtualEnclosedBinaryMacFile::ReadData (void* buffer,long& count,TVirtualMacFile::ForkType whichFork)
- {
- TVirtualFile* file = (whichFork == kData) ? fDFFile : fRFFile;
- return file ? file->ReadData ( buffer, count ) : -1;
- }
-
- //--------------------------------------------------------------------------------
-
- OSErr TVirtualEnclosedBinaryMacFile::WriteData ( const void* buffer, long& count, TVirtualMacFile::ForkType whichFork)
- {
- TVirtualFile* file = (whichFork == kData) ? fDFFile : fRFFile;
- return file ? file->WriteData ( buffer, count ) : -1;
- }
-
- //--------------------------------------------------------------------------------
-
- OSErr TVirtualEnclosedBinaryMacFile::SetEnd (long /* logEof */, ForkType /* whichFork */)
- {
- return permErr;
- }
-
- //--------------------------------------------------------------------------------
-
- OSErr TVirtualEnclosedBinaryMacFile::GetEnd (long& logEof, TVirtualMacFile::ForkType whichFork) const
- {
- logEof = (whichFork == kData) ? fDataBytes : fResBytes;
- return noErr;
- }
-
- //--------------------------------------------------------------------------------
-
- OSErr TVirtualEnclosedBinaryMacFile::SetPosition (short posMode, long posOff, TVirtualMacFile::ForkType whichFork)
- {
- TVirtualFile* file = (whichFork == kData) ? fDFFile : fRFFile;
- return file ? file->SetPosition (posMode,posOff) : -1;
- }
-
- //--------------------------------------------------------------------------------
-
- OSErr TVirtualEnclosedBinaryMacFile::GetPosition (long& filePos,TVirtualMacFile::ForkType whichFork) const
- {
- TVirtualFile* file = (whichFork == kData) ? fDFFile : fRFFile;
- return file ? file->GetPosition (filePos) : -1;
- }
-
- //--------------------------------------------------------------------------------
-
- OSErr TVirtualEnclosedBinaryMacFile::SetFinderInfo (const FInfo& finderInfo)
- {
- fFinderInfo = finderInfo;
- return noErr;
- }
-
- //--------------------------------------------------------------------------------
-
- OSErr TVirtualEnclosedBinaryMacFile::GetFinderInfo (FInfo& finderInfo) const
- {
- finderInfo = fFinderInfo;
- return noErr;
- }
-
- //--------------------------------------------------------------------------------
-
- OSErr TVirtualEnclosedBinaryMacFile::GetDate (unsigned long& dateTime,TVirtualMacFile::WhichDateType whichDate) const
- {
- dateTime = (whichDate == kCreationDate) ? fCreationDate : fModifiedDate;
- return noErr;
- }
-
- //--------------------------------------------------------------------------------
-
- OSErr TVirtualEnclosedBinaryMacFile::SetDate (unsigned long dateTime,TVirtualMacFile::WhichDateType whichDate)
- {
- if (whichDate == kCreationDate)
- fCreationDate = dateTime;
- else
- fModifiedDate = dateTime;
- return noErr;
- }
-
- //--------------------------------------------------------------------------------
-
- void TVirtualEnclosedBinaryMacFile::SetUserRef(long ref)
- {
- fFile->SetUserRef(ref);
- }
-
- //--------------------------------------------------------------------------------
-
- long TVirtualEnclosedBinaryMacFile::GetUserRef() const
- {
- return fFile->GetUserRef();
- }
-
- //--------------------------------------------------------------------------------
-
- OSErr TVirtualEnclosedBinaryMacFile::SetFileName(const Str31 name)
- {
- PLstrcpy(fName,name);
- return noErr;
- }
-
- //--------------------------------------------------------------------------------
-
- OSErr TVirtualEnclosedBinaryMacFile::GetFileName(Str31 name) const
- {
- PLstrcpy(name,fName);
- return noErr;
- }
-
- //--------------------------------------------------------------------------------
-
- OSErr TVirtualEnclosedBinaryMacFile::SetSpec ( const FSSpec& spec )
- { unused(spec);
- return -1;
- }
-
- //--------------------------------------------------------------------------------
-
- OSErr TVirtualEnclosedBinaryMacFile::GetSpec ( FSSpec& spec ) const
- { unused(spec);
- return -1;
- }
-
- //--------------------------------------------------------------------------------
-
- /********************************************************************************
- Function DoCRC
- *******************************************************************************/
-
- unsigned short DoCRC(void *ptr, short n)
-
- /*
- calculates CRC for MacBinaryII headers
-
- */
-
- {
- unsigned short i, crc = 0;
- char *ptc = (char *)ptr;
-
- while ( n-- ){
-
- crc ^= (((unsigned short)*ptc++) << 8);
-
- for ( i = 0; i< 8; ++i ) {
- if ( crc & 0x8000 )
- crc = (crc << 1) ^ 0x1021;
- else
- crc = crc << 1;
- }
- }
- return ( crc );
-
- }
-
-
- /***********************************|****************************************/
-
- void SetUpEnclosedBinaryHeader(TVirtualMacFile* attachment, attachmentHeader& attachmentInfo,
- unsigned long enclosedFilePacker, const CStr255& fileName )
- {
- short crc;
- Byte macBinaryVersion = kMacBinaryVersion;
-
- for ( int i = 0; i < 128; i++ )
- attachmentInfo.header[i] = 0x00; //clear the header
- for ( i = 0; i < 64; i++ )
- attachmentInfo.fName[i] = 0x00; //clear the filename
-
- attachment->Open ();
- attachment->GetEnd ( attachmentInfo.dataBytes, TVirtualMacFile::kData );
- attachmentInfo.dataForkPadBytes = (( attachmentInfo.dataBytes + 127L ) & 0xFFFFFF80 ) - attachmentInfo.dataBytes;
- attachment->GetEnd ( attachmentInfo.resBytes, TVirtualMacFile::kResource );
- attachmentInfo.resourceForkPadBytes = (( attachmentInfo.resBytes + 127L ) & 0xFFFFFF80 ) - attachmentInfo.resBytes;
- attachment->GetFinderInfo ( attachmentInfo.fndrInfo );
-
- attachment->Close ();
-
- BlockMove ( (StringPtr) fileName, & attachmentInfo.fName, fileName.Length() + 1 );
-
- switch (enclosedFilePacker)
- {
- case kCCSBinary:
- strcpy(attachmentInfo.enclosedFilePackerName, "CCSBinary");
- attachmentInfo.headerLength = kEndCCSHead;
- BlockMove ( (StringPtr) fileName, &attachmentInfo.header[kCCSFnLen], fileName.Length() + 1 );
- BlockMove( &attachmentInfo.fndrInfo, &attachmentInfo.header[kCCSFType], 16); //move Finder info (16 bytes)
- BlockMove( &attachmentInfo.dataBytes, &attachmentInfo.header[kCCSDFLength], 4); //length of data fork
- BlockMove( &attachmentInfo.resBytes, &attachmentInfo.header[kCCSRFLength], 4); //length of resource fork
-
- break;
-
- ///****************************** old code **********************************/
- //
- //static void SetUpMacInfoHeader(TVirtualMacFile* attachment, macInfoHeader& headerInfo, const CStr255& fileName )
- //{
- // attachment->GetEnd ( headerInfo.DataBytes,TVirtualMacFile::kData );
- // attachment->GetEnd ( headerInfo.ResBytes,TVirtualMacFile::kResource );
- // attachment->GetFinderInfo ( headerInfo.fndrInfo );
- //
- // BlockMove ( (StringPtr) fileName, & headerInfo.fName, fileName.Length() + 1 );
- //}
- //
- /***********************************|****************************************/
-
-
-
- case kMacBinaryII:
- strcpy(attachmentInfo.enclosedFilePackerName, "MacBinaryII");
- attachmentInfo.headerLength = kEndMB2Head;
- BlockMove ( (StringPtr) fileName, & attachmentInfo.header[kFileNameLen], fileName.Length() + 1 );
- BlockMove( &attachmentInfo.fndrInfo, &attachmentInfo.header[kFileType], 16); //move Finder info (16 bytes)
- BlockMove( &attachmentInfo.dataBytes, &attachmentInfo.header[kDFLength], 4); //length of data fork
- BlockMove( &attachmentInfo.resBytes, &attachmentInfo.header[kRFLength], 4); //length of resource fork
- BlockMove( &macBinaryVersion, &attachmentInfo.header[kMB2Version], 1);
- BlockMove( &macBinaryVersion, &attachmentInfo.header[kMBMinVersion], 1);
-
- crc = DoCRC(attachmentInfo.header, kBytesForCRC);
- BlockMove( &crc, &attachmentInfo.header[kCRC], 2);
-
- break;
- }
-
- attachmentInfo.fileSize = attachmentInfo.dataBytes + attachmentInfo.resBytes + attachmentInfo.headerLength;
-
- }
-
- /***********************************|****************************************/
-