home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / stuff 1.0.2 / src / stuff.c < prev    next >
Encoding:
Text File  |  1996-01-05  |  8.7 KB  |  298 lines  |  [TEXT/CWIE]

  1. // stuff
  2. // version 1.0.2
  3. // updated for CW7 on 951215
  4.  
  5. #include "stuff.h"
  6.  
  7. long prog_div;
  8. long uncomp_tot,comp_tot;
  9.  
  10. #ifndef min
  11. #define min(a,b) ((a)<(b)?(a):(b))
  12. #endif
  13.  
  14. struct sitHdr sh;
  15. struct fileHdr fh;
  16.  
  17. short ofd;
  18. ushort crc;
  19. int rmfiles;
  20. long total;
  21. char *Creator, *Type;
  22.  
  23. static unsigned short crctab[1<<8] = {
  24. 0x0, 0xc0c1, 0xc181, 0x140, 0xc301, 0x3c0, 0x280, 0xc241,
  25. 0xc601, 0x6c0, 0x780, 0xc741, 0x500, 0xc5c1, 0xc481, 0x440,
  26. 0xcc01, 0xcc0, 0xd80, 0xcd41, 0xf00, 0xcfc1, 0xce81, 0xe40,
  27. 0xa00, 0xcac1, 0xcb81, 0xb40, 0xc901, 0x9c0, 0x880, 0xc841,
  28. 0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40,
  29. 0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
  30. 0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641,
  31. 0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,
  32. 0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240,
  33. 0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
  34. 0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41,
  35. 0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
  36. 0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41,
  37. 0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,
  38. 0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,
  39. 0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,
  40. 0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240,
  41. 0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
  42. 0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41,
  43. 0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
  44. 0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41,
  45. 0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,
  46. 0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640,
  47. 0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
  48. 0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241,
  49. 0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,
  50. 0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40,
  51. 0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,
  52. 0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40,
  53. 0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
  54. 0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,
  55. 0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040,
  56. } ;
  57.  
  58. unsigned short updcrc(unsigned short icrc, unsigned char *icp, int icnt);
  59. void BeginProcessing(void);
  60. void EndProcessing(int numDocs);
  61. void ProcessDoc (FSSpec *curDocFSS, char mode);
  62. void scanfolder(FSSpec *);
  63.  
  64. unsigned short updcrc(unsigned short icrc, unsigned char *icp, int icnt)
  65.     {
  66.     register unsigned short crc = icrc;
  67.     register unsigned char *cp = icp;
  68.     register int cnt = icnt;
  69.     while( cnt-- ) 
  70.         {
  71.         crc = (crc>>8) ^ crctab[(crc & ((1<<8)-1)) ^ *cp++];
  72.         }
  73.     return( crc );
  74.     }
  75.  
  76. static cp2(
  77. unsigned short x,
  78. char dest[])
  79. {
  80.     dest[0] = x>>8;
  81.     dest[1] = x;
  82. }
  83.  
  84. static cp4(
  85. unsigned long x,
  86. char dest[])
  87. {
  88.     dest[0] = x>>24;
  89.     dest[1] = x>>16;
  90.     dest[2] = x>>8;
  91.     dest[3] = x;
  92. }
  93.  
  94. static FSSpec new;
  95. void    UpdateFileName(FSSpec *name);
  96.  
  97. extern long bytes_out;    /* Total number of byte to output */
  98.  
  99. void BeginProcessing(void)
  100.     {
  101.     uncomp_tot = 0;
  102.     comp_tot = 0;
  103.     total = 0;
  104.     }
  105.     
  106. void EndProcessing(int numDocs)
  107.     {
  108.     long inOutCount = 22;
  109.     ChkOsErr(SetFPos(ofd,1,0));
  110.     total += inOutCount;
  111.     UpdateFileName(&new);
  112.     /* header header */
  113.     memcpy(sh.sig1,"SIT!",4);
  114.     cp2(numDocs,(char *)sh.numFiles);
  115.     cp4(total,(char *)sh.arcLen);
  116.     memcpy(sh.sig2,"rLau",4);
  117.     sh.version = 1;
  118.     ChkOsErr(FSWrite(ofd,&inOutCount,&sh));
  119.     FSClose(ofd);
  120.     ofd = 0;
  121.     }
  122.  
  123. void ProcessDoc (FSSpec *curDocFSS, char mode)
  124.     {
  125.     long inOutCount;
  126.     OSErr err,iErr;
  127.     int siz;
  128.     int i,n;
  129.     short fd;
  130.     long fpos1, fpos2;
  131.     if (!total)
  132.         {
  133.         new = *curDocFSS;
  134.         if (new.name[0] < 28)
  135.             {
  136.             BlockMove(".sit", &new.name[new.name[0]+1], 4);
  137.             new.name[0] += 4;
  138.             }
  139.         else
  140.             BlockMove("\parchive.sit", &new.name, 12);
  141.         iErr = FSpCreate(&new,'SIT!','SIT!',0);
  142.         if (iErr != dupFNErr) ChkOsErr(iErr);
  143.         ChkOsErr(FSpOpenDF(&new,0,&ofd));    
  144.         ChkOsErr(SetEOF(ofd,22));
  145.         ChkOsErr(SetFPos(ofd,1,22));
  146.         }
  147.     else
  148.         {
  149.         if (!memcmp(&new, curDocFSS, sizeof(FSSpec))) return;
  150.         }
  151.     UpdateFileName(curDocFSS);
  152.     ChkOsErr(GetFPos(ofd,&fpos1));
  153.     /* write empty header, will seek back and fill in later */
  154.     inOutCount = 112;
  155.     memset(&fh,0,inOutCount);
  156.     memcpy((void *)fh.fName, &(curDocFSS->name[0]), 1+curDocFSS->name[0]);
  157.     if (mode)    /* operate on directory entry */
  158.         {
  159.         CInfoPBRec curDocPB;
  160.         curDocPB.hFileInfo.ioVRefNum = curDocFSS->vRefNum;
  161.         curDocPB.hFileInfo.ioDirID = curDocFSS->parID;
  162.         curDocPB.hFileInfo.ioNamePtr = curDocFSS->name;
  163.         curDocPB.hFileInfo.ioFDirIndex = 0;    
  164.         err = PBGetCatInfoSync (&curDocPB);
  165.         memcpy((void *)fh.cDate, (void *)&curDocPB.hFileInfo.ioFlCrDat, 4);
  166.         memcpy((void *)fh.mDate, (void *)&curDocPB.hFileInfo.ioFlMdDat, 4);
  167.         fh.compRMethod = fh.compDMethod = mode;
  168.         cp2(updcrc(0,(unsigned char *)&fh,110), (char *)fh.hdrCRC);
  169.         ChkOsErr(FSWrite(ofd,&inOutCount,&fh));
  170.         total += inOutCount;
  171.         return;
  172.         }
  173.     else
  174.         {
  175.         HParamBlockRec pb;
  176.         ChkOsErr(FSWrite(ofd,&inOutCount,&fh));
  177.         memset(&pb, 0, sizeof(HParamBlockRec));
  178.         pb.fileParam.ioVRefNum = curDocFSS->vRefNum;
  179.         pb.fileParam.ioDirID = curDocFSS->parID;
  180.         pb.fileParam.ioNamePtr = curDocFSS->name;
  181.         err = PBHGetFInfoSync (&pb);
  182.         memcpy((void *)fh.fType, (void *)&pb.fileParam.ioFlFndrInfo.fdType, 4);
  183.         memcpy((void *)fh.fCreator, (void *)&pb.fileParam.ioFlFndrInfo.fdCreator, 4);
  184.         memcpy((void *)fh.FndrFlags, (void *)&pb.fileParam.ioFlFndrInfo.fdFlags, 2);
  185.         memcpy((void *)fh.cDate, (void *)&pb.fileParam.ioFlCrDat, 4);
  186.         memcpy((void *)fh.mDate, (void *)&pb.fileParam.ioFlMdDat, 4);
  187.         cp4(pb.fileParam.ioFlRLgLen,(char *)fh.rLen);
  188.         cp4(pb.fileParam.ioFlLgLen,(char *)fh.dLen);
  189.         /* look for resource fork */
  190.         if (pb.fileParam.ioFlRLgLen) 
  191.             {    /* resource fork exists */
  192.             long fpos3;
  193.             OSErr err = FSpOpenRF(curDocFSS, fsRdPerm, &fd);
  194.             crc = 0;
  195.             prog_div = 0x4000000L/pb.fileParam.ioFlRLgLen;
  196.             ChkOsErr(GetFPos(ofd,&fpos3));
  197.             compress(fd, ofd);
  198.             if (bytes_out > pb.fileParam.ioFlRLgLen)
  199.                 {
  200.                 ChkOsErr(SetFPos(fd,1,0));
  201.                 ChkOsErr(SetEOF(ofd,fpos3));
  202.                 copy(fd, ofd);
  203.                 bytes_out = pb.fileParam.ioFlRLgLen;
  204.                 }
  205.             else fh.compRMethod = lpzComp;
  206.             FSClose(fd);
  207.             fd = 0;
  208.             uncomp_tot += pb.fileParam.ioFlRLgLen;
  209.             comp_tot += bytes_out;
  210.             cp4(bytes_out,(char *)fh.cRLen);
  211.             cp2(crc,(char *)fh.rsrcCRC);
  212.             }
  213.         /* look for data fork */
  214.         if (pb.fileParam.ioFlLgLen) 
  215.             {    /* data fork exists */
  216.             long fpos3;
  217.             OSErr err = FSpOpenDF(curDocFSS, fsRdPerm, &fd);
  218.             crc = 0;
  219.             prog_div = 0x4000000L/pb.fileParam.ioFlLgLen;
  220.             ChkOsErr(GetFPos(ofd,&fpos3));
  221.             compress(fd, ofd);
  222.             if (bytes_out > pb.fileParam.ioFlLgLen)
  223.                 {
  224.                 ChkOsErr(SetFPos(fd,1,0));
  225.                 ChkOsErr(SetEOF(ofd,fpos3));
  226.                 copy(fd, ofd);
  227.                 bytes_out = pb.fileParam.ioFlLgLen;
  228.                 }
  229.             else fh.compDMethod = lpzComp;
  230.             FSClose(fd);
  231.             fd = 0;
  232.             uncomp_tot += pb.fileParam.ioFlLgLen;
  233.             comp_tot += bytes_out;
  234.             cp4(bytes_out,(char *)fh.cDLen);
  235.             cp2(crc,(char *)fh.dataCRC);
  236.             }
  237.         cp2(updcrc(0,(unsigned char *)&fh,110), (char *)fh.hdrCRC);
  238.         ChkOsErr(GetFPos(ofd,&fpos2));
  239.         ChkOsErr(SetFPos(ofd,1,fpos1));
  240.         inOutCount = 112;
  241.         ChkOsErr(FSWrite(ofd,&inOutCount,&fh));
  242.         ChkOsErr(SetFPos(ofd,1,fpos2));
  243.         total += (fpos2 - fpos1);
  244.         }
  245.     }
  246.  
  247. void scanfolder(FSSpec *curDocFSS)
  248.     {
  249.     CInfoPBRec curDocPB;
  250.     long fpos1,fpos2,inOutCount;
  251.     long old_uncomp_tot = uncomp_tot;
  252.     long old_comp_tot = comp_tot;
  253.     int i;
  254.     FSSpec    new;
  255.     OSErr err;
  256.     OSErr iErr;
  257.     ProcessDoc(curDocFSS, 0x20);
  258.     ChkOsErr(GetFPos(ofd,&fpos1));
  259.     curDocPB.hFileInfo.ioVRefNum = curDocFSS->vRefNum;
  260.     curDocPB.hFileInfo.ioDirID = curDocFSS->parID;
  261.     curDocPB.hFileInfo.ioNamePtr = curDocFSS->name;
  262.     curDocPB.hFileInfo.ioFDirIndex = 0;    
  263.     err = PBGetCatInfoSync (&curDocPB);
  264.     new.parID = curDocPB.hFileInfo.ioDirID;
  265.     new.vRefNum = curDocFSS->vRefNum;
  266.     for (i = 1; err != fnfErr; i++) 
  267.         {
  268.         curDocPB.hFileInfo.ioVRefNum = new.vRefNum;
  269.         curDocPB.hFileInfo.ioDirID = new.parID;
  270.         curDocPB.hFileInfo.ioNamePtr = new.name;
  271.         curDocPB.hFileInfo.ioFDirIndex = i;    
  272.         err = PBGetCatInfoSync (&curDocPB);
  273.         if (err == noErr)
  274.             {
  275.             Boolean isFolder,wasAlias;
  276.             err = ResolveAliasFile (&new, true, &isFolder, &wasAlias);
  277.             if (err == noErr) 
  278.                 {
  279.                 if (isFolder) scanfolder(&new);
  280.                 else ProcessDoc (&new, 0x0);
  281.                 }
  282.             }
  283.         }
  284.     ProcessDoc(curDocFSS, 0x21);
  285.     ChkOsErr(GetFPos(ofd,&fpos2));
  286.     inOutCount = 112;
  287.     fpos1 -= inOutCount;
  288.     ChkOsErr(SetFPos(ofd,1,fpos1));
  289.     ChkOsErr(FSRead(ofd,&inOutCount,&fh));
  290.     inOutCount = 112;
  291.     cp4(fpos2-fpos1-inOutCount,(char *)fh.cDLen);
  292.     cp4(uncomp_tot-old_uncomp_tot,(char *)fh.dLen);
  293.     cp2(updcrc(0,(unsigned char *)&fh,110), (char *)fh.hdrCRC);
  294.     ChkOsErr(SetFPos(ofd,1,fpos1));
  295.     ChkOsErr(FSWrite(ofd,&inOutCount,&fh));
  296.     ChkOsErr(SetFPos(ofd,1,fpos2));
  297.     }
  298.