home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample SMSAM / SampleSMSAM Source / VirtualFile / VirtualEnclosedBinaryMacFile.cp < prev    next >
Encoding:
Text File  |  1995-07-28  |  12.3 KB  |  396 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        VirtualEnclosedBinaryMacFile.cp
  3.  
  4.     Copyright:    © 1991-1994 by Apple Computer, Inc.
  5.                 All rights reserved.
  6.  
  7.     Part of the AOCE Sample SMSAM Package.  Consult the license
  8.     which came with this software for your specific legal rights.
  9.  
  10. */
  11.  
  12.  
  13.  
  14. #ifndef __BLJSTANDARDINCLUDES__
  15. #include "BLJStandardIncludes.h"
  16. #endif
  17.  
  18. #ifndef __VIRTUALFILE__
  19. #include "VirtualFile.h"
  20. #endif
  21.  
  22. #ifndef __ERRORS__
  23. #include <Errors.h>
  24. #endif
  25.  
  26. #ifndef __FILEINFILE__
  27. #include "FileInFile.h"
  28. #endif
  29.  
  30. #ifndef __VIRTUALENCLOSEDBINARYMACFILE__
  31. #include "VirtualEnclosedBinaryMacFile.h"
  32. #endif
  33.  
  34. #ifndef __UTILITIES__
  35. #include "Utilities.h"
  36. #endif
  37.  
  38. #pragma segment VirtualEnclosedBinaryMacFile
  39.  
  40. // constructor for an already-packed file created in a raw file, which came from the host
  41.  
  42. TVirtualEnclosedBinaryMacFile::TVirtualEnclosedBinaryMacFile(TVirtualFile* rawFile, unsigned long enclosedFilePacker):
  43.     TVirtualMacFile (),
  44.     fFile ( rawFile ),
  45.     fRFFile ( nil ),
  46.     fDFFile ( nil ),
  47.     fHeaderSizeInVirtualFile ( 0 ),
  48.     fEnclosedFilePacker ( enclosedFilePacker ),
  49.     fValidEnclosedBinaryFile ( false )
  50. {
  51.     long temp;
  52.     
  53.     fFile->RegisterReference();
  54.     Lock ();
  55.     
  56.     switch ( fEnclosedFilePacker )
  57.     {
  58.         case kCCSBinary:
  59.             fHeaderSizeInVirtualFile = sizeof ( fFileInfoCCSBinary );
  60.             temp = fHeaderSizeInVirtualFile;
  61.             fValidEnclosedBinaryFile = (fFile->ReadData ( fFileInfoCCSBinary, temp ) == noErr);
  62.             Unlock ();
  63.         
  64.             //    Now, validate that we've got a 'valid' CCS Binary file    
  65.             long ccsBinaryFileSize;
  66.             fValidEnclosedBinaryFile = fValidEnclosedBinaryFile && (fFile->GetEnd(ccsBinaryFileSize) == noErr);    
  67.             fValidEnclosedBinaryFile = fValidEnclosedBinaryFile && (ccsBinaryFileSize >= fHeaderSizeInVirtualFile );
  68.             
  69.             if (fValidEnclosedBinaryFile)
  70.             {
  71.                 BlockMove(&fFileInfoCCSBinary[kCCSDFLength], &fDataBytes, 4);
  72.                 BlockMove(&fFileInfoCCSBinary[kCCSRFLength], &fResBytes, 4);
  73.                 
  74.                 fValidEnclosedBinaryFile = (fDataBytes >= 0) && (fResBytes >= 0) 
  75.                                 && (fDataBytes + fResBytes + fHeaderSizeInVirtualFile <= ccsBinaryFileSize);
  76.                                                         
  77.             /*  now copy appropriate things for general usage */
  78.                 BlockMove(&fFileInfoCCSBinary[kCCSFnLen], fName, 64);        //move length byte and name
  79.                 BlockMove(&fFileInfoCCSBinary[kCCSFType], &fFinderInfo, 16);    //move Finder info (16 bytes)
  80.                 BlockMove(&fFileInfoCCSBinary[kCCSCDate], &fCreationDate, 4);
  81.                 BlockMove(&fFileInfoCCSBinary[kCCSModDate], &fModifiedDate, 4);
  82.                 fDataForkPadBytes = 0;
  83.                 fResourceForkPadBytes = 0;
  84.             }
  85.             break;
  86.  
  87.         case kMacBinaryII:
  88.             fHeaderSizeInVirtualFile = sizeof ( fFileInfoMacBinary );
  89.             temp = fHeaderSizeInVirtualFile;            
  90.             fValidEnclosedBinaryFile = (fFile->ReadData ( fFileInfoMacBinary, temp ) == noErr);
  91.             Unlock ();
  92.         
  93.             fValidEnclosedBinaryFile = fValidEnclosedBinaryFile 
  94.                                         && (fFileInfoMacBinary[0] == 0x00) && (fFileInfoMacBinary[74] == 0x00);
  95.                                         
  96.             BlockMove(&fFileInfoMacBinary[kCRC], &fCRC, 2);
  97.             
  98.             fValidEnclosedBinaryFile = fValidEnclosedBinaryFile && ( DoCRC(fFileInfoMacBinary, kBytesForCRC) == fCRC);
  99.             
  100.             if (fValidEnclosedBinaryFile)
  101.             {
  102.                 BlockMove(&fFileInfoMacBinary[kDFLength], &fDataBytes, 4);
  103.                 BlockMove(&fFileInfoMacBinary[kRFLength], &fResBytes, 4);
  104.                 
  105.             /*  now copy appropriate things for general usage */
  106.                 BlockMove(&fFileInfoMacBinary[kFileNameLen], fName, 64);        //move length byte and name
  107.                 BlockMove(&fFileInfoMacBinary[kFileType], &fFinderInfo, 16);    //move Finder info (16 bytes)
  108.                 BlockMove(&fFileInfoMacBinary[kFileCrDate], &fCreationDate, 4);
  109.                 BlockMove(&fFileInfoMacBinary[kFileMdDate], &fModifiedDate, 4);
  110.                 BlockMove(&fFileInfoMacBinary[kLenSecHdr], &fSecondaryHeaderLength, 2);
  111.                 fDataForkPadBytes = 128L - (fDataBytes % 128L) % 128L;
  112.                 fResourceForkPadBytes = 128L - (fResBytes % 128L) % 128L;
  113.                 fHeaderSizeInVirtualFile += fSecondaryHeaderLength;
  114.             }
  115.             break;
  116.         
  117.         default:
  118.             fValidEnclosedBinaryFile = false;
  119.     }    
  120. }
  121.  
  122. //--------------------------------------------------------------------------------
  123.  
  124.  
  125. TVirtualEnclosedBinaryMacFile::~TVirtualEnclosedBinaryMacFile()
  126. {
  127.     delete fRFFile;
  128.     delete fDFFile;
  129.  
  130.     fFile->UnregisterReference();
  131. }
  132.  
  133. //--------------------------------------------------------------------------------
  134.  
  135. Boolean TVirtualEnclosedBinaryMacFile::IsValidEnclosedBinaryFile() const
  136. {
  137.     return fValidEnclosedBinaryFile;
  138. }
  139.  
  140. //--------------------------------------------------------------------------------
  141.  
  142.  
  143. OSErr TVirtualEnclosedBinaryMacFile::Open ()
  144. {
  145.     if (fDFFile == nil)
  146.     {
  147.         fDFFile = new TFileInFile(fFile, fHeaderSizeInVirtualFile, fDataBytes);
  148.     }
  149.  
  150.     if (fRFFile == nil)
  151.     {
  152.         fRFFile = new TFileInFile(fFile, fHeaderSizeInVirtualFile + fDataBytes + fDataForkPadBytes, fResBytes);
  153.     }
  154.  
  155.     return noErr;
  156. }
  157.  
  158. //--------------------------------------------------------------------------------
  159.  
  160. OSErr TVirtualEnclosedBinaryMacFile::Close ()
  161. {
  162.     return noErr;
  163. }
  164.  
  165. //--------------------------------------------------------------------------------
  166.  
  167. OSErr TVirtualEnclosedBinaryMacFile::ReadData (void* buffer,long& count,TVirtualMacFile::ForkType whichFork)
  168. {
  169.     TVirtualFile* file = (whichFork == kData) ? fDFFile : fRFFile;
  170.     return file ? file->ReadData ( buffer, count ) : -1;
  171. }
  172.  
  173. //--------------------------------------------------------------------------------
  174.  
  175. OSErr TVirtualEnclosedBinaryMacFile::WriteData ( const void* buffer, long& count, TVirtualMacFile::ForkType whichFork)
  176. {
  177.     TVirtualFile* file = (whichFork == kData) ? fDFFile : fRFFile;
  178.     return file ? file->WriteData ( buffer, count ) : -1;
  179. }
  180.  
  181. //--------------------------------------------------------------------------------
  182.  
  183. OSErr TVirtualEnclosedBinaryMacFile::SetEnd (long /* logEof */, ForkType /* whichFork */)
  184. {
  185.     return  permErr;
  186. }
  187.  
  188. //--------------------------------------------------------------------------------
  189.  
  190. OSErr TVirtualEnclosedBinaryMacFile::GetEnd (long& logEof, TVirtualMacFile::ForkType whichFork) const
  191. {
  192.     logEof = (whichFork == kData) ?  fDataBytes : fResBytes;
  193.     return noErr;
  194. }
  195.  
  196. //--------------------------------------------------------------------------------
  197.  
  198. OSErr TVirtualEnclosedBinaryMacFile::SetPosition (short posMode, long posOff, TVirtualMacFile::ForkType whichFork)
  199. {
  200.     TVirtualFile* file = (whichFork == kData) ? fDFFile : fRFFile;
  201.     return file ? file->SetPosition (posMode,posOff) : -1;
  202. }
  203.  
  204. //--------------------------------------------------------------------------------
  205.  
  206. OSErr TVirtualEnclosedBinaryMacFile::GetPosition (long& filePos,TVirtualMacFile::ForkType whichFork)    const
  207. {
  208.     TVirtualFile* file = (whichFork == kData) ? fDFFile : fRFFile;
  209.     return file ? file->GetPosition (filePos) : -1;
  210. }
  211.  
  212. //--------------------------------------------------------------------------------
  213.  
  214. OSErr TVirtualEnclosedBinaryMacFile::SetFinderInfo (const FInfo& finderInfo)
  215. {
  216.     fFinderInfo = finderInfo;
  217.     return noErr;
  218. }
  219.  
  220. //--------------------------------------------------------------------------------
  221.  
  222. OSErr TVirtualEnclosedBinaryMacFile::GetFinderInfo (FInfo& finderInfo)  const
  223. {
  224.     finderInfo = fFinderInfo;
  225.     return noErr;
  226. }
  227.  
  228. //--------------------------------------------------------------------------------
  229.  
  230. OSErr TVirtualEnclosedBinaryMacFile::GetDate (unsigned long& dateTime,TVirtualMacFile::WhichDateType whichDate) const
  231. {
  232.     dateTime = (whichDate == kCreationDate) ? fCreationDate : fModifiedDate;
  233.     return noErr;
  234. }
  235.  
  236. //--------------------------------------------------------------------------------
  237.  
  238. OSErr TVirtualEnclosedBinaryMacFile::SetDate (unsigned long dateTime,TVirtualMacFile::WhichDateType whichDate)
  239. {
  240.     if  (whichDate == kCreationDate)
  241.         fCreationDate = dateTime;
  242.     else
  243.         fModifiedDate = dateTime;
  244.     return noErr;
  245. }
  246.  
  247. //--------------------------------------------------------------------------------
  248.  
  249. void TVirtualEnclosedBinaryMacFile::SetUserRef(long ref)
  250. {
  251.     fFile->SetUserRef(ref);
  252. }
  253.  
  254. //--------------------------------------------------------------------------------
  255.  
  256. long TVirtualEnclosedBinaryMacFile::GetUserRef() const
  257. {
  258.     return fFile->GetUserRef();
  259. }
  260.  
  261. //--------------------------------------------------------------------------------
  262.  
  263. OSErr TVirtualEnclosedBinaryMacFile::SetFileName(const Str31 name)
  264. {
  265.     PLstrcpy(fName,name);
  266.     return noErr;
  267. }
  268.  
  269. //--------------------------------------------------------------------------------
  270.  
  271. OSErr TVirtualEnclosedBinaryMacFile::GetFileName(Str31 name) const
  272. {
  273.     PLstrcpy(name,fName);
  274.     return noErr;
  275. }
  276.  
  277. //--------------------------------------------------------------------------------
  278.  
  279. OSErr TVirtualEnclosedBinaryMacFile::SetSpec ( const FSSpec& spec )
  280. {    unused(spec);
  281.     return -1;
  282. }
  283.  
  284. //--------------------------------------------------------------------------------
  285.  
  286. OSErr TVirtualEnclosedBinaryMacFile::GetSpec ( FSSpec& spec ) const
  287. {    unused(spec);
  288.     return -1;
  289. }
  290.  
  291. //--------------------------------------------------------------------------------
  292.  
  293. /********************************************************************************
  294.     Function DoCRC
  295.  *******************************************************************************/
  296.  
  297. unsigned short    DoCRC(void *ptr, short n)
  298.  
  299. /*
  300.     calculates CRC for MacBinaryII headers
  301.                 
  302. */
  303.     
  304. {    
  305.     unsigned short        i, crc = 0;
  306.     char                    *ptc = (char *)ptr;
  307.     
  308.     while ( n-- ){
  309.         
  310.         crc ^= (((unsigned short)*ptc++) << 8);    
  311.         
  312.         for ( i = 0; i< 8; ++i ) { 
  313.             if ( crc & 0x8000 )
  314.                 crc = (crc << 1) ^ 0x1021;
  315.             else
  316.                 crc = crc << 1;
  317.         }
  318.     }
  319.     return ( crc );
  320.     
  321. }
  322.  
  323.  
  324. /***********************************|****************************************/
  325.  
  326. void SetUpEnclosedBinaryHeader(TVirtualMacFile* attachment, attachmentHeader& attachmentInfo, 
  327.                                 unsigned long enclosedFilePacker, const CStr255& fileName )
  328. {
  329.     short    crc;
  330.     Byte    macBinaryVersion = kMacBinaryVersion;
  331.  
  332.     for ( int i = 0; i < 128; i++ )
  333.         attachmentInfo.header[i] = 0x00;                //clear the header
  334.     for ( i = 0; i < 64; i++ )
  335.         attachmentInfo.fName[i] = 0x00;                    //clear the filename
  336.         
  337.     attachment->Open ();
  338.     attachment->GetEnd ( attachmentInfo.dataBytes, TVirtualMacFile::kData );
  339.     attachmentInfo.dataForkPadBytes = (( attachmentInfo.dataBytes + 127L ) & 0xFFFFFF80 ) - attachmentInfo.dataBytes;
  340.     attachment->GetEnd ( attachmentInfo.resBytes, TVirtualMacFile::kResource );
  341.     attachmentInfo.resourceForkPadBytes = (( attachmentInfo.resBytes + 127L ) & 0xFFFFFF80 ) - attachmentInfo.resBytes;
  342.     attachment->GetFinderInfo ( attachmentInfo.fndrInfo );
  343.     
  344.     attachment->Close ();
  345.  
  346.     BlockMove ( (StringPtr) fileName, & attachmentInfo.fName, fileName.Length() + 1 );
  347.  
  348.     switch (enclosedFilePacker)
  349.     {
  350.         case kCCSBinary:
  351.             strcpy(attachmentInfo.enclosedFilePackerName, "CCSBinary");
  352.             attachmentInfo.headerLength = kEndCCSHead;
  353.             BlockMove ( (StringPtr) fileName, &attachmentInfo.header[kCCSFnLen], fileName.Length() + 1 );
  354.             BlockMove( &attachmentInfo.fndrInfo, &attachmentInfo.header[kCCSFType], 16);    //move Finder info (16 bytes)
  355.             BlockMove( &attachmentInfo.dataBytes, &attachmentInfo.header[kCCSDFLength], 4);    //length of data fork
  356.             BlockMove( &attachmentInfo.resBytes, &attachmentInfo.header[kCCSRFLength], 4);    //length of resource fork
  357.  
  358.             break;
  359.     
  360. ///******************************   old code   **********************************/
  361. //
  362. //static void SetUpMacInfoHeader(TVirtualMacFile* attachment, macInfoHeader& headerInfo, const CStr255& fileName )
  363. //{
  364. //    attachment->GetEnd ( headerInfo.DataBytes,TVirtualMacFile::kData );
  365. //    attachment->GetEnd ( headerInfo.ResBytes,TVirtualMacFile::kResource );
  366. //    attachment->GetFinderInfo ( headerInfo.fndrInfo );
  367. //
  368. //    BlockMove ( (StringPtr) fileName, & headerInfo.fName, fileName.Length() + 1 );
  369. //}
  370. //
  371. /***********************************|****************************************/
  372.     
  373.     
  374.     
  375.         case kMacBinaryII:
  376.             strcpy(attachmentInfo.enclosedFilePackerName, "MacBinaryII");
  377.             attachmentInfo.headerLength = kEndMB2Head;
  378.             BlockMove ( (StringPtr) fileName, & attachmentInfo.header[kFileNameLen], fileName.Length() + 1 );
  379.             BlockMove( &attachmentInfo.fndrInfo, &attachmentInfo.header[kFileType], 16);    //move Finder info (16 bytes)
  380.             BlockMove( &attachmentInfo.dataBytes, &attachmentInfo.header[kDFLength], 4);    //length of data fork
  381.             BlockMove( &attachmentInfo.resBytes, &attachmentInfo.header[kRFLength], 4);        //length of resource fork
  382.             BlockMove( &macBinaryVersion, &attachmentInfo.header[kMB2Version], 1);
  383.             BlockMove( &macBinaryVersion, &attachmentInfo.header[kMBMinVersion], 1);
  384.         
  385.             crc = DoCRC(attachmentInfo.header, kBytesForCRC);
  386.             BlockMove( &crc, &attachmentInfo.header[kCRC], 2);
  387.             
  388.             break;
  389.     }    
  390.         
  391.     attachmentInfo.fileSize = attachmentInfo.dataBytes + attachmentInfo.resBytes + attachmentInfo.headerLength;
  392.  
  393. }
  394.  
  395. /***********************************|****************************************/
  396.