home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / macutils.lzh / MACUTILS / UNSIT / unsit.c < prev    next >
Text File  |  1996-02-02  |  33KB  |  1,167 lines

  1. /* Modified Aug & Dec 90, Nigel Perry, Dept of Computing, Imperial College,
  2.    London SW7 2BZ, np@doc.ic.ac.uk
  3.  
  4.    If called by AUFSNAME (default unstuffit) will input/output to
  5.    CAPS aufs disc.
  6.  
  7.    Will now untiff multiple files.
  8.  
  9.  */
  10. /*
  11.            unsit - Macintosh StuffIt file extractor
  12.  
  13.              Version 1.5f, for StuffIt 1.5
  14.  
  15.                 July 23, 1990
  16.  
  17. This program will unpack a Macintosh StuffIt file into separate files.
  18. The data fork of a StuffIt file contains both the data and resource
  19. forks of the packed files.  The program will unpack each Mac file into
  20. either separate .data, .rsrc., and .info files that can be downloaded
  21. to a Mac using macput and MacTerminal over a tty line, or into a
  22. single MacBinary format file.  The MacBinary format is generally more
  23. convenient for those with network connections and FTP capability.  The
  24. program is much like the "unpit" program for breaking apart Packit
  25. archive files.
  26.  
  27.             ***** IMPORTANT *****
  28. To extract StuffIt files that have been compressed with the Lempel-Ziv
  29. compression method, unsit pipes the data through the "compress"
  30. program with the appropriate switches, rather than incorporate the
  31. uncompression routines within "unsit".  Therefore, it is necessary to
  32. have the "compress" program on the system and in the search path to
  33. make "unsit" work.  "Compress" is available from the comp.sources.unix
  34. archives.
  35.  
  36. The program syntax is much like unpit and macput/macget, with some added
  37. options:
  38.  
  39.     unsit [-rdulM] [-vqfm] stuffit-file.data
  40.  
  41. Only one of the flags r, d, u, l, or M should be specified.  The
  42. default mode is to create the three macput/MacTerminal compatible
  43. file.  The -M flag will cause the output to be in MacBinary format (a
  44. single file).  This can be swapped (default = MacBinary, -M = macput)
  45. by changing the definitions of DEFAULT_MODE and OTHER_MODE below.  The
  46. -r and -d flags will cause only the resource and data forks to be
  47. written.  The -u flag will cause only the data fork to be written and
  48. to have carriage return characters changed to Unix newline characters.
  49. The -l flag will make the program only list the files in the StuffIt
  50. file.
  51.  
  52. The -v flag causes the program to list the names, sizes, type, and
  53. creators of the files it is writing.  The -q flag causes it to list
  54. the name, type and size of each file and wait for a 'y' or 'n' for
  55. either writing that file or skipping it, respectively.  The -m flag is
  56. used when the input file in in the MacBinary format instead of just
  57. the data fork.  It causes the program to skip the 128 byte MacBinary
  58. header before looking for the StuffIt header.  It is not necessary to
  59. specify the -m flag since the program now checks for MacBinary format
  60. input files and handles them correctly
  61.  
  62. Version 1.5 of the unsit supports extracting files and folders as
  63. implemented by StuffIt 1.5's "Hierarchy Maintained Folder" feature.
  64. Each folder is extracted as a subdirectory on the Unix system with the
  65. files in the folder placed in the corresponding subdirectory.  The -f
  66. option can be used to "flatten" out the hierarchy and unsit will store
  67. all the files in the current directory.  If the query option (-q) is
  68. used and a "n" response is given to a folder name, none of the files
  69. or folders in that folder will be extraced.
  70.  
  71. Some of the program is borrowed from the macput.c/macget.c programs.
  72. Many, many thanks to Raymond Lau, the author of StuffIt, for including
  73. information on the format of the StuffIt archives in the
  74. documentation.  Several changes and enhancements supplied by David
  75. Shanks (cde@atelabs.UUCP) have been incorporated into the program for
  76. doing things like supporting System V and recognizing MacBinary files.
  77. Christopher Bingham <kb@umnstat.stat.umn.edu> supplied some Macbinary
  78. patches.  Code was also borrowed from the macbin program by Jim Budler
  79. for convert macput format files to MacBinary.  I'm always glad to
  80. receive advice, suggestions, or comments about the program so feel free
  81. to send whatever you think would be helpful
  82.  
  83.  
  84.     Author: Allan G. Weber
  85.         weber@sipi.usc.edu
  86.         ...!usc!sipi!weber
  87.     Date:   July 23, 1990
  88.  
  89. */
  90.  
  91. #include <stdio.h>
  92. #ifndef OSK
  93. #include <sys/types.h>
  94. #include <sys/stat.h>
  95. #else
  96. #include "/dd/blarsdefs/sys/stat.h"
  97. #endif
  98.  
  99. typedef long OSType;
  100.  
  101. #include "stuffit.h"
  102.  
  103. /*
  104.  * Define the following if your Unix can only handle 14 character file names
  105.  * (e.g. Version 7 and System V).
  106.  */
  107. /* #define SHORTNAMES */
  108.  
  109. /*
  110.  * The following defines the name of the compress program that is used for the
  111.  * uncompression of Lempel-Ziv compressed files.  If the path is set up to
  112.  * include the right directory, this should work.
  113.  */
  114. #define COMPRESS   "compress"
  115.  
  116. #define IOBUFSIZ   4096
  117.  
  118. #define MACBINHDRSIZE  128L
  119.  
  120. #define INIT_CRC 0L
  121. extern unsigned short updcrc();
  122.  
  123. #define INFOBYTES 128
  124.  
  125. #define BYTEMASK 0xff
  126.  
  127. #define S_SIGNATURE    0
  128. #define S_NUMFILES     4
  129. #define S_ARCLENGTH    6
  130. #define S_SIGNATURE2  10
  131. #define    S_VERSION     14
  132. #define SITHDRSIZE    22
  133.  
  134. #define F_COMPRMETHOD    0
  135. #define F_COMPDMETHOD    1
  136. #define F_FNAME          2
  137. #define F_FTYPE         66
  138. #define F_CREATOR       70
  139. #define F_FNDRFLAGS     74
  140. #define F_CREATIONDATE  76
  141. #define F_MODDATE       80
  142. #define F_RSRCLENGTH    84
  143. #define F_DATALENGTH    88
  144. #define F_COMPRLENGTH   92
  145. #define F_COMPDLENGTH   96
  146. #define F_RSRCCRC      100
  147. #define F_DATACRC      102
  148. #define F_HDRCRC       110
  149. #define FILEHDRSIZE    112
  150.  
  151. #define F_NAMELEN 63
  152. #ifdef SHORTNAMES        /* short file names */
  153. # define I_NAMELEN 15        /* 14 char file names + '\0' terminator */
  154. #else
  155. # define I_NAMELEN 69        /* 63 + strlen(".info") + 1 */
  156. #endif
  157.  
  158. /* The following are copied out of macput.c/macget.c */
  159. #define I_NAMEOFF 1
  160. /* 65 <-> 80 is the FInfo structure */
  161. #define I_TYPEOFF 65
  162. #define I_AUTHOFF 69
  163. #define I_FLAGOFF 73
  164. #define I_LOCKOFF 81
  165. #define I_DLENOFF 83
  166. #define I_RLENOFF 87
  167. #define I_CTIMOFF 91
  168. #define I_MTIMOFF 95
  169.  
  170. #define INITED_BUG
  171. #define INITED_OFF    I_FLAGOFF    /* offset to byte with Inited flag */
  172. #define INITED_MASK    (~1)        /* mask to '&' with byte to reset it */
  173.  
  174. #define TEXT 0
  175. #define DATA 1
  176. #define RSRC 2
  177. #define MACPUT 3
  178. #define DUMP 4
  179. #define MACBINARY 5
  180.  
  181. /* Swap the following definitions if you want the output to default to
  182.    MacBinary, and the -M switch to create macput file (.data, .rsrc, .info) */
  183. #define DEFAULT_MODE MACPUT
  184. #define OTHER_MODE   MACBINARY
  185.  
  186. /* #define ADDBIN */    /* add .bin to macbinary file names */
  187.  
  188. #define NODECODE 0
  189. #define DECODE   1
  190.  
  191. #define H_ERROR -1
  192. #define H_EOF    0
  193. #define H_WRITE  1
  194. #define H_SKIP   2
  195.  
  196. /* if called by this name program works on aufs files */
  197. #define AUFSNAME "unstuffit"
  198.  
  199. struct node {
  200.     int flag, byte;
  201.     struct node *one, *zero;
  202. } nodelist[512], *nodeptr, *read_tree();    /* 512 should be big enough */
  203.  
  204. struct sitHdr sithdr;
  205.  
  206. char f_info[I_NAMELEN];
  207. char f_data[I_NAMELEN];
  208. char f_rsrc[I_NAMELEN];
  209.  
  210. char info[INFOBYTES];
  211. char mname[F_NAMELEN+1];
  212. char uname[F_NAMELEN+1];
  213. char dname[F_NAMELEN+1];    /* directory name for aufs */
  214. char iobuf[IOBUFSIZ];
  215. char zbuf[128];            /* buffer of zeros to pad MacBinary forks */
  216.  
  217. int mode, txtmode, listonly, verbose, query, flatten;
  218. int bit, chkcrc, numfiles, depth;
  219. int aufs;            /* aufs flag */
  220. int debug = 0;
  221. FILE *infp;
  222.  
  223. long get4();
  224. short get2();
  225. unsigned short write_file();
  226.  
  227. /********************************************************************************/
  228. /* added for aufs, nicked from various places... */
  229.  
  230. /* following from mcvert program */
  231.  
  232. /* Useful, though not particularly Mac related, values */
  233. typedef unsigned char byte;     /* one byte, obviously */
  234. typedef unsigned short word;    /* must be 2 bytes */
  235. typedef unsigned long ulong;    /* 4 bytes */
  236.  
  237. #define NAMELEN 63              /* maximum legal Mac file name length */
  238.  
  239. /* Format of a bin file:
  240. A bin file is composed of 128 byte blocks.  The first block is the
  241. info_header (see below).  Then comes the data fork, null padded to fill the
  242. last block.  Then comes the resource fork, padded to fill the last block.  A
  243. proposal to follow with the text of the Get Info box has not been implemented,
  244. to the best of my knowledge.  Version, zero1 and zero2 are what the receiving
  245. program looks at to determine if a MacBinary transfer is being initiated.
  246. */ 
  247. typedef struct {     /* info file header (128 bytes). Unfortunately, these
  248.                         longs don't align to word boundaries */
  249.             byte version;           /* there is only a version 0 at this time */
  250.             byte nlen;              /* Length of filename. */
  251.             byte name[NAMELEN];     /* Filename (only 1st nlen are significant)*/
  252.             byte type[4];           /* File type. */
  253.             byte auth[4];           /* File creator. */
  254.             byte flags;             /* file flags: LkIvBnSyBzByChIt */
  255.             byte zero1;             /* Locked, Invisible,Bundle, System */
  256.                                     /* Bozo, Busy, Changed, Init */
  257.             byte icon_vert[2];      /* Vertical icon position within window */
  258.             byte icon_horiz[2];     /* Horizontal icon postion in window */
  259.             byte window_id[2];      /* Window or folder ID. */
  260.             byte protect;           /* = 1 for protected file, 0 otherwise */
  261.             byte zero2;
  262.             byte dflen[4];          /* Data Fork length (bytes) -   most sig.  */
  263.             byte rflen[4];          /* Resource Fork length         byte first */
  264.             byte cdate[4];          /* File's creation date. */
  265.             byte mdate[4];          /* File's "last modified" date. */
  266.             byte ilen[2];           /* GetInfo message length */
  267.         byte flags2;            /* Finder flags, bits 0-7 */
  268.         byte unused[14];       
  269.         byte packlen[4];        /* length of total files when unpacked */
  270.         byte headlen[2];        /* length of secondary header */
  271.         byte uploadvers;        /* Version of MacBinary II that the uploading program is written for */
  272.         byte readvers;          /* Minimum MacBinary II version needed to read this file */
  273.             byte _crc[2];            /* CRC of the previous 124 bytes */
  274.         byte padding[2];        /* two trailing unused bytes */
  275.             } info_header;
  276.  
  277. /* end of mcvert stuff */
  278. /* from CAP aufs documentation */
  279.  
  280. #define FINFOLEN 32
  281. #define MAXCLEN 199
  282. typedef struct
  283. {  /* byte fi_fndr[FINFOLEN]; */    /* finder info */
  284.    /* what I think the above is... */
  285.    ulong fndr_type, fndr_creator;
  286.    word fndr_flags;
  287.    ulong fndr_loc;
  288.    word fndr_fldr;
  289.    word fndr_icon;
  290.    byte fndr_unused[8];
  291.    word fndr_comment;
  292.    ulong fndr_putaway;
  293.    /* end of fi_fndr */
  294.  
  295.    word fi_attr;            /* attributes */
  296. #define FI_MAGIC1 255
  297.    byte fi_magic1;        /* was: length of comment */
  298. #define FI_VERSION 0x10        /* version major 1, minor 0 */
  299.                 /* if more than 8 versions then */
  300.                 /* something wrong anyway */
  301.    byte fi_version;        /* version number */
  302. #define FI_MAGIC 0xda
  303.    byte fi_magic;        /* magic word check */
  304.    byte fi_bitmap;        /* bitmap of included info */
  305. #define FI_BM_SHORTFILENAME 0x1    /* is this included? */
  306. #define FI_BM_MACINTOSHFILENAME 0x2 /* is this included? */
  307.    byte fi_shortfilename[12+1];    /* possible short file name */
  308.    byte fi_macfilename[32+1];    /* possible macintosh file name */
  309.    byte fi_comln;        /* comment length */
  310.    byte fi_comnt[MAXCLEN+1];    /* comment string */
  311. } FileInfo;
  312.  
  313. FileInfo fndr_info;
  314.  
  315. /* end aufs */
  316. /********************************************************************************/
  317.  
  318. main(argc, argv)
  319. int argc;
  320. char *argv[];
  321. {
  322.     int status;
  323.     int c;
  324.     extern int optind;
  325.     extern char *optarg;
  326.     int errflg;
  327.     int macbin;
  328.  
  329.     mode = DEFAULT_MODE;
  330.     errflg = 0;
  331.     macbin = 0;
  332.     flatten = 0;
  333.     numfiles = 0;
  334.     depth = 0;
  335.     aufs = strcmp(argv[0], AUFSNAME) == 0;
  336.  
  337.     while ((c = getopt(argc, argv, "DMdflmqruvx")) != EOF)
  338.     switch (c) {
  339.       case 'r':        /* extract resource fork only */
  340.         mode = RSRC;
  341.         break;
  342.       case 'd':        /* extract data fork only */
  343.         mode = DATA;
  344.         break;
  345.       case 'u':        /* extract data fork as Unix text file */
  346.         mode = TEXT;
  347.         break;
  348.       case 'l':        /* list contents of archive */
  349.         listonly++;
  350.         break;
  351.       case 'q':        /* query user on each extraction */
  352.         query++;
  353.         break;
  354.       case 'v':        /* verbose mode */
  355.         verbose++;
  356.         break;
  357.       case 'x':        /* don't decode data, just dump to files*/
  358.         mode = DUMP;
  359.         break;
  360.       case 'm':        /* input file is in Macbinary format */
  361.         macbin = 1;
  362.         break;
  363.       case 'M':        /* output file in OTHER_MODE */
  364.         mode = OTHER_MODE;
  365.         break;
  366.       case 'f':        /* don't create flat directory tree */
  367.         flatten = 1;
  368.         break;
  369.       case 'D':        /* debugging mode */
  370.         debug = 1;
  371.         break;
  372.       case '?':
  373.         errflg++;
  374.         break;
  375.     }
  376.     if (errflg) {
  377.     usage();
  378.     exit(1);
  379.     }
  380.  
  381.     /* -a incompatible with -M and -u */
  382.     if(aufs && (mode == MACBINARY || mode == TEXT))
  383.     {   fprintf(stderr, "Aufs mode cannot be combined with MacBinary (-M) or text (-t) mode\n");
  384.     exit(1);
  385.     }
  386.  
  387.     if (optind == argc) {
  388.     usage();
  389.     exit(1);
  390.     }
  391.     else
  392.     while(optind < argc)
  393.         {   if ((infp = fopen(argv[optind], "r")) == NULL)
  394.         {   fprintf(stderr,"Can't open input file \"%s\"\n",argv[optind]);
  395.             exit(1);
  396.         }
  397.  
  398.         if (macbin) {
  399.         if (fseek(infp, MACBINHDRSIZE, 0) == -1) {
  400.             fprintf(stderr, "Can't skip over MacBinary header\n");
  401.             exit(1);
  402.         }
  403.         }
  404.  
  405.         if (readsithdr(&sithdr) == 0) {
  406.         fprintf(stderr, "Can't read file header\n");
  407.         exit(1);
  408.         }
  409.         if (debug) {
  410.         printf("archive header (%d bytes):\n", SITHDRSIZE);
  411.         printf("numFiles=%d, arcLength=%ld, version=%d\n",
  412.                sithdr.numFiles, sithdr.arcLength, sithdr.version & 0xff);
  413.         }
  414.         status = extract("", 0);
  415.         if(status < 0) exit(1);
  416.  
  417.         optind++;
  418.     }
  419.  
  420.     exit(0);
  421. }
  422.  
  423. usage()
  424. {
  425. #ifndef OSK
  426.     fprintf(stderr, "Usage: unsit/unstuffit [-rdulM] [-vqfm] filename\n");
  427. #else
  428.     fprintf(stderr, "Unsit V1.15f Update by Nigel Perry\n");
  429.     fprintf(stderr, "             Orginal by Allen G Weber\n");
  430.     fprintf(stderr, "             OSK Port by Dean Leiber\n\n");
  431.     fprintf(stderr, "Usage: Unsit [-rdulM] [-vqfm] filename\n\n");
  432.     fprintf(stderr, " Options:\n");
  433.     fprintf(stderr, "             -r = Extract Resource Fork only \n");
  434.     fprintf(stderr, "             -d = Extract Data Fork only\n");
  435.     fprintf(stderr, "             -u = Mac -> Unix Text\n");
  436.     fprintf(stderr, "             -l = List Archive Contents\n");
  437.     fprintf(stderr, "             -M = Macbinary\n");
  438.     fprintf(stderr, " Modifiers:\n");
  439.     fprintf(stderr, "             -v = Verbose\n");
  440.     fprintf(stderr, "             -q = Query (Prompt for Y/N)\n");
  441.     fprintf(stderr, "             -m = Input Macbinary [Default]\n");
  442.     fprintf(stderr, "             -f = Flatten Archive\n");
  443. #endif
  444. }
  445.  
  446. /*
  447.   extract(parent, skip) - Extract all files from the current folder.
  448.   char *parent;           name of parent folder
  449.   int  skip;              1 to skip all files and folders in this one
  450.                           0 to extract them
  451.  
  452.   returns 1 if came an endFolder record
  453.           0 if EOF
  454.      -1 if error (bad fileHdr, bad file, etc.)
  455. */
  456.  
  457. extract(parent, skip)
  458. char *parent;
  459. int skip;
  460. {
  461.     struct fileHdr filehdr;
  462.     struct stat sbuf;
  463.     int status, rstat, sstat, skipit;
  464.     char name[256];
  465.  
  466.     while (1) {
  467.     rstat = readfilehdr(&filehdr, skip);
  468.     if (rstat == H_ERROR || rstat == H_EOF) {
  469.         status = rstat;
  470.         break;
  471.     }
  472.     if (debug) {
  473.         printf("file header (%d bytes):\n", FILEHDRSIZE);
  474.         printf("compRMethod=%d, compDMethod=%d\n",
  475.            filehdr.compRMethod, filehdr.compDMethod);
  476.         printf("rsrcLength=%ld, dataLength=%ld\n",
  477.            filehdr.rsrcLength, filehdr.dataLength);
  478.         printf("compRLength=%ld, compDLength=%ld\n",
  479.            filehdr.compRLength, filehdr.compDLength);
  480.         printf("rsrcCRC=%d=0x%04x, dataCRC=%d=0x%04x\n",
  481.            filehdr.rsrcCRC, filehdr.rsrcCRC,
  482.            filehdr.dataCRC, filehdr.dataCRC);
  483.     }
  484.  
  485.     skipit = (rstat == H_SKIP) ? 1 : 0;
  486.  
  487.     if (filehdr.compRMethod == endFolder && 
  488.         filehdr.compDMethod == endFolder) {
  489.         status = 1;        /* finished with this folder */
  490.         break;
  491.     }
  492.     else if (filehdr.compRMethod == startFolder && 
  493.          filehdr.compDMethod == startFolder) {
  494.         if (!listonly && rstat == H_WRITE && !flatten) {
  495.         sstat = stat(uname, &sbuf);
  496.         if (sstat == -1) {    /* directory doesn't exist */
  497.             if (mkdir(uname, 0777) == -1) {
  498.             fprintf(stderr,
  499.                 "Can't create subdirectory %s\n", uname);
  500.             return(-1);
  501.             }
  502.         }
  503.         else {        /* something exists with this name */
  504.             if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
  505.             fprintf(stderr, "Directory name %s already in use\n",
  506.                 uname);
  507.             return(-1);
  508.             }
  509.         }
  510.         if(aufs) /* create .finderinfo & .resource subdirectories */
  511.         {   strcpy(dname, uname);
  512.             strcat(dname, "/.finderinfo");
  513.             sstat = stat(dname, &sbuf);
  514.             if (sstat == -1) {    /* directory doesn't exist */
  515.             if (mkdir(dname, 0777) == -1) {
  516.                 fprintf(stderr,
  517.                     "Can't create subdirectory %s\n", dname);
  518.                 return(-1);
  519.             }
  520.             }
  521.             else {        /* something exists with this name */
  522.             if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
  523.                 fprintf(stderr, "Directory name %s already in use\n",
  524.                     dname);
  525.                 return(-1);
  526.             }
  527.             }
  528.             strcpy(dname, uname);
  529.             strcat(dname, "/.resource");
  530.             sstat = stat(dname, &sbuf);
  531.             if (sstat == -1) {    /* directory doesn't exist */
  532.             if (mkdir(dname, 0777) == -1) {
  533.                 fprintf(stderr,
  534.                     "Can't create subdirectory %s\n", dname);
  535.                 return(-1);
  536.             }
  537.             }
  538.             else {        /* something exists with this name */
  539.             if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
  540.                 fprintf(stderr, "Directory name %s already in use\n",
  541.                     dname);
  542.                 return(-1);
  543.             }
  544.             }
  545.         }
  546.         if (chdir(uname) == -1) {
  547.             fprintf(stderr, "Can't chdir to %s\n", uname);
  548.             return(-1);
  549.         }
  550.         sprintf(name,"%s:%s", parent, uname);
  551.         }
  552.         depth++;
  553.         status = extract(name, skipit);
  554.         depth--;
  555.         if (status != 1)
  556.         break;        /* problem with folder */
  557.         if (depth == 0)    /* count how many top-level files done */
  558.         numfiles++;
  559.         if (!flatten)
  560.         chdir("..");
  561.     }
  562.     else {
  563.         if ((status = extractfile(&filehdr, skipit)) != 1)
  564.         break;
  565.         if (depth == 0)    /* count how many top-level files done */
  566.         numfiles++;
  567.     }
  568.     if (numfiles == sithdr.numFiles)
  569.         break;
  570.     }
  571.     return(status);
  572. }
  573.  
  574. extractfile(fh, skip)
  575. struct fileHdr *fh;
  576. int skip;
  577. {
  578.     unsigned short _crc;
  579.     FILE *fp, *fp1;
  580.     int n;
  581.  
  582.     f_data[0] = f_rsrc[0] = f_info[0] = '\0'; /* assume no output files */
  583.     /* figure out what file names to use and what to do */
  584.     if (!listonly && !skip) {
  585.     switch (mode) {
  586.       case MACPUT:        /* do both rsrc and data forks */
  587.         if(aufs)
  588.         {   sprintf(f_data, "%.*s", I_NAMELEN - 1, uname);
  589.         sprintf(f_rsrc, ".resource/%.*s", I_NAMELEN - 1, uname);
  590.         sprintf(f_info, ".finderinfo/%.*s", I_NAMELEN - 1, uname);
  591.         }
  592.         else
  593.         {   sprintf(f_data, "%.*s.data", I_NAMELEN - 6, uname);
  594.         sprintf(f_rsrc, "%.*s.rsrc", I_NAMELEN - 6, uname);
  595.         sprintf(f_info, "%.*s.info", I_NAMELEN - 6, uname);
  596.         }
  597.         break;
  598.       case RSRC:        /* rsrc fork only */
  599.         if(aufs)
  600.         {   sprintf(f_rsrc, ".resource/%.*s", I_NAMELEN - 1, uname);
  601.         sprintf(f_info, ".finderinfo/%.*s", I_NAMELEN - 1, uname);
  602.         }
  603.         else
  604.         sprintf(f_rsrc, "%.*s.rsrc", I_NAMELEN - 6, uname);
  605.         break;
  606.       case DATA:        /* data fork only */
  607.         if(aufs)
  608.         {   sprintf(f_data, "%.*s", I_NAMELEN - 1, uname);
  609.         sprintf(f_info, ".finderinfo/%.*s", I_NAMELEN - 1, uname);
  610.         }
  611.         else
  612.             sprintf(f_data, "%.*s.data", I_NAMELEN - 6, uname);
  613.         break;
  614.       case TEXT:
  615.         sprintf(f_data, "%.*s.text", I_NAMELEN - 6, uname);
  616.         break;
  617.       case DUMP:        /* for debugging, dump data as is */
  618.         sprintf(f_data, "%.*s.ddump", I_NAMELEN - 7, uname);
  619.         sprintf(f_rsrc, "%.*s.rdump", I_NAMELEN - 7, uname);
  620.         fh->compRMethod = fh->compDMethod = noComp;
  621.         break;
  622.       case MACBINARY:    /* output file in MacBinary format */
  623.         sprintf(f_data, "%.*s.data", I_NAMELEN - 6, uname);
  624.         sprintf(f_rsrc, "%.*s.rsrc", I_NAMELEN - 6, uname);
  625. #ifndef ADDBIN
  626.         sprintf(f_info, "%.*s", I_NAMELEN - 1, uname);
  627. #else
  628.         sprintf(f_info, "%.*s.bin", I_NAMELEN - 5, uname);
  629. #endif /*ADDBIN*/
  630.         break;
  631.     }
  632.     }
  633.  
  634.     fp = NULL;            /* so we can tell if a file is open */
  635.     if (f_info[0] != '\0' && check_access(f_info) != -1) {
  636.     fp = fopen(f_info, "w");
  637.     if (fp == NULL) {
  638.         perror(f_info);
  639.         exit(1);
  640.     }
  641.     if(aufs) /* convert info structure */
  642.     {   register info_header *pinfo;
  643.     
  644.         pinfo = (info_header *)info;
  645.  
  646.         /* make the .finderinfo file */
  647.         bzero(&fndr_info, sizeof(FileInfo));
  648.         bcopy(pinfo->type, &fndr_info.fndr_type, 4);
  649.         bcopy(pinfo->auth, &fndr_info.fndr_creator, 4);
  650.         bcopy(&pinfo->flags, &fndr_info.fndr_flags, 2);
  651.         fndr_info.fi_magic1 = FI_MAGIC1;
  652.         fndr_info.fi_version = FI_VERSION;
  653.         fndr_info.fi_magic = FI_MAGIC;
  654.         fndr_info.fi_bitmap = FI_BM_MACINTOSHFILENAME;
  655.         bcopy(pinfo->name, fndr_info.fi_macfilename, pinfo->nlen);
  656.  
  657.         fwrite(&fndr_info, sizeof(FileInfo), 1, fp);
  658.     }
  659.     else
  660.     {   if (mode == MACBINARY) { /* convert to MacBinary header */
  661.         /* taken from the macbin program */
  662.         if (info[74] & 0x40) info[81] = '\1'; /* protected */
  663.         info[74] = '\0'; /* clear zero2 */
  664.         info[82] = '\0'; /* force zero3 clear */
  665.         }
  666.         fwrite(info, 1, INFOBYTES, fp);
  667.     }
  668.     }
  669.  
  670.     if (f_rsrc[0] != '\0') {
  671.     txtmode = 0;
  672.     _crc = write_file(f_rsrc, fh->compRLength,
  673.              fh->rsrcLength, fh->compRMethod);
  674.     if (chkcrc && fh->rsrcCRC != _crc) {
  675.         fprintf(stderr,
  676.             "CRC error on resource fork: need 0x%04x, got 0x%04x\n",
  677.             fh->rsrcCRC, _crc);
  678.         return(-1);
  679.     }
  680.     }
  681.     else {
  682.     fseek(infp, (long) fh->compRLength, 1);
  683.     }
  684.     if (f_data[0] != '\0') {
  685.     txtmode = (mode == TEXT);
  686.     _crc = write_file(f_data, fh->compDLength,
  687.              fh->dataLength, fh->compDMethod);
  688.     if (chkcrc && fh->dataCRC != _crc) {
  689.         fprintf(stderr,
  690.             "CRC error on data fork: need 0x%04x, got 0x%04x\n",
  691.             fh->dataCRC, _crc);
  692.         return(-1);
  693.     }
  694.     }
  695.     else {
  696.     fseek(infp, (long) fh->compDLength, 1);
  697.     }
  698.     if (fp != NULL) {
  699.     /* if Macbinary output, copy the data and resource forks to the
  700.        end of the info file, and pad each to multiples of 128 bytes. */
  701.     if (mode == MACBINARY) {
  702.         fp1 = fopen(f_data, "r"); /* re-open the file we just wrote */
  703.         if (fp1 == NULL) {
  704.         perror(f_data);
  705.         exit(1);
  706.         }
  707.         while ((n = fread(iobuf, 1, IOBUFSIZ, fp1)) > 0)
  708.         fwrite(iobuf, 1, n, fp); /* append it to the info file */
  709.         /* pad out to multiple of 128 if in MacBinary format */
  710.         n = fh->dataLength % 128;
  711.         if (n > 0)
  712.         outc(zbuf, 128 - n, fp);
  713.         fclose(fp1);
  714.         unlink(f_data);
  715.         fp1 = fopen(f_rsrc, "r"); /* re-open the file we just wrote */
  716.         if (fp1 == NULL) {
  717.         perror(f_rsrc);
  718.         exit(1);
  719.         }
  720.         while ((n = fread(iobuf, 1, IOBUFSIZ, fp1)) > 0)
  721.         fwrite(iobuf, 1, n, fp); /* append it to the info file */
  722.         /* pad out to multiple of 128 if in MacBinary format */
  723.         n = fh->rsrcLength % 128;
  724.         if (n > 0)
  725.         outc(zbuf, 128 - n, fp);
  726.         fclose(fp1);
  727.         unlink(f_rsrc);
  728.     }
  729.     fclose(fp);
  730.     }
  731.     return(1);
  732. }
  733.  
  734. readsithdr(s)
  735. struct sitHdr *s;
  736. {
  737.     char temp[FILEHDRSIZE];
  738.     int count = 0;
  739.  
  740.     for (;;) {
  741.     if (fread(temp, 1, SITHDRSIZE, infp) != SITHDRSIZE) {
  742.         fprintf(stderr, "Can't read file header\n");
  743.         return(0);
  744.     }
  745.         
  746.     if (strncmp(temp + S_SIGNATURE,  "SIT!", 4) == 0 &&
  747.         strncmp(temp + S_SIGNATURE2, "rLau", 4) == 0) {
  748.         s->numFiles = get2(temp + S_NUMFILES);
  749.         s->arcLength = get4(temp + S_ARCLENGTH);
  750.         return(1);
  751.     }
  752.     
  753.     if (++count == 2) {
  754.         fprintf(stderr, "Not a StuffIt file\n");
  755.         return(0);
  756.     }
  757.     
  758.     if (fread(&temp[SITHDRSIZE], 1, FILEHDRSIZE - SITHDRSIZE, infp) !=
  759.         FILEHDRSIZE - SITHDRSIZE) {
  760.         fprintf(stderr, "Can't read file header\n");
  761.         return(0);
  762.     }
  763.     
  764.     if (strncmp(temp + I_TYPEOFF, "SIT!", 4) == 0 &&
  765.         strncmp(temp + I_AUTHOFF, "SIT!", 4) == 0) {    /* MacBinary format */
  766.         fseek(infp, (long)(INFOBYTES-FILEHDRSIZE), 1);    /* Skip over header */
  767.     }
  768.     }
  769. }
  770.  
  771. /*
  772.   readfilehdr - reads the file header for each file and the folder start
  773.   and end records.
  774.  
  775.   returns: H_ERROR = error
  776.        H_EOF   = EOF
  777.        H_WRITE = write file/folder
  778.        H_SKIP  = skip file/folder
  779. */
  780.  
  781. readfilehdr(f, skip)
  782. struct fileHdr *f;
  783. int skip;
  784. {
  785.     unsigned short _crc;
  786.     int i, n, write_it, isfolder;
  787.     char hdr[FILEHDRSIZE];
  788.     char ch, *mp, *up;
  789.     char *tp, temp[10];
  790.  
  791.     for (i = 0; i < INFOBYTES; i++)
  792.     info[i] = '\0';
  793.  
  794.     /* read in the next file header, which could be folder start/end record */
  795.     n = fread(hdr, 1, FILEHDRSIZE, infp);
  796.     if (n == 0)            /* return 0 on EOF */
  797.     return(H_EOF);
  798.     else if (n != FILEHDRSIZE) {
  799.     fprintf(stderr, "Can't read file header\n");
  800.     return(H_ERROR);
  801.     }
  802.  
  803.     /* check the CRC for the file header */
  804.     _crc = INIT_CRC;
  805.     _crc = updcrc(_crc, hdr, FILEHDRSIZE - 2);
  806.     f->hdrCRC = get2(hdr + F_HDRCRC);
  807.     if (f->hdrCRC != _crc) {
  808.     fprintf(stderr, "Header CRC mismatch: got 0x%04x, need 0x%04x\n",
  809.         f->hdrCRC, _crc);
  810.     return(H_ERROR);
  811.     }
  812.  
  813.     /* grab the name of the file or folder */
  814.     n = hdr[F_FNAME] & BYTEMASK;
  815.     if (n > F_NAMELEN)
  816.     n = F_NAMELEN;
  817.     info[I_NAMEOFF] = n;
  818.     copy(info + I_NAMEOFF + 1, hdr + F_FNAME + 1, n);
  819.     strncpy(mname, hdr + F_FNAME + 1, n);
  820.     mname[n] = '\0';
  821.     /* copy to a string with no illegal Unix characters in the file name */
  822.     mp = mname;
  823.     up = uname;
  824.     while ((ch = *mp++) != '\0') {
  825.     if (ch <= ' ' || ch > '~' || index("/!()[]*<>?\\\"$\';&`", ch) != NULL)
  826.         ch = '_';
  827.     *up++ = ch;
  828.     }
  829.     *up = '\0';
  830.  
  831.     /* get lots of other stuff from the header */
  832.     f->compRMethod = hdr[F_COMPRMETHOD];
  833.     f->compDMethod = hdr[F_COMPDMETHOD];
  834.     f->rsrcLength = get4(hdr + F_RSRCLENGTH);
  835.     f->dataLength = get4(hdr + F_DATALENGTH);
  836.     f->compRLength = get4(hdr + F_COMPRLENGTH);
  837.     f->compDLength = get4(hdr + F_COMPDLENGTH);
  838.     f->rsrcCRC = get2(hdr + F_RSRCCRC);
  839.     f->dataCRC = get2(hdr + F_DATACRC);
  840.  
  841.     /* if it's an end folder record, don't need to do any more */
  842.     if (f->compRMethod == endFolder && f->compDMethod == endFolder)
  843.     return(H_WRITE);
  844.  
  845.     /* prepare an info file in case its needed */
  846.  
  847.     copy(info + I_TYPEOFF, hdr + F_FTYPE, 4);
  848.     copy(info + I_AUTHOFF, hdr + F_CREATOR, 4);
  849.     copy(info + I_FLAGOFF, hdr + F_FNDRFLAGS, 2);
  850. #ifdef INITED_BUG
  851.     info[INITED_OFF] &= INITED_MASK; /* reset init bit */
  852. #endif
  853.     copy(info + I_DLENOFF, hdr + F_DATALENGTH, 4);
  854.     copy(info + I_RLENOFF, hdr + F_RSRCLENGTH, 4);
  855.     copy(info + I_CTIMOFF, hdr + F_CREATIONDATE, 4);
  856.     copy(info + I_MTIMOFF, hdr + F_MODDATE, 4);
  857.  
  858.     isfolder = f->compRMethod == startFolder && f->compDMethod == startFolder;
  859.     
  860.     /* list the file name if verbose or listonly mode, also if query mode */
  861.     if (skip)            /* skip = 1 if skipping all in this folder */
  862.     write_it = 0;
  863.     else {
  864.     write_it = 1;
  865.     if (listonly || verbose || query) {
  866.         for (i = 0; i < depth; i++)
  867.         putchar(' ');
  868.         if (isfolder)
  869.         printf("Folder: \"%s\"", uname);
  870.         else
  871.         printf("name=\"%s\", type=%4.4s, author=%4.4s, data=%ld, rsrc=%ld",
  872.                uname, hdr + F_FTYPE, hdr + F_CREATOR,
  873.                f->dataLength, f->rsrcLength);
  874.         if (query) {    /* if querying, check with the boss */
  875.         printf(" ? ");
  876.         fgets(temp, sizeof(temp) - 1, stdin);
  877.         tp = temp;
  878.         write_it = 0;
  879.         while (*tp != '\0') {
  880.             if (*tp == 'y' || *tp == 'Y') {
  881.             write_it = 1;
  882.             break;
  883.             }
  884.             else
  885.             tp++;
  886.         }
  887.         }
  888.         else        /* otherwise, terminate the line */
  889.         putchar('\n');
  890.     }
  891.     }
  892.     return(write_it ? H_WRITE : H_SKIP);
  893. }
  894.  
  895. check_access(fname)    /* return 0 if OK to write on file fname, -1 otherwise */
  896. char *fname;
  897. {
  898.     char temp[10], *tp;
  899.  
  900.     if (access(fname, 0) == -1) {
  901.     return(0);
  902.     }
  903.     else {
  904.     printf("%s exists.  Overwrite? ", fname);
  905.     fgets(temp, sizeof(temp) - 1, stdin);
  906.     tp = temp;
  907.     while (*tp != '\0') {
  908.         if (*tp == 'y' || *tp == 'Y') {
  909.         return(0);
  910.         }
  911.         else
  912.         tp++;
  913.     }
  914.     }
  915.     return(-1);
  916. }
  917.  
  918. unsigned short write_file(fname, ibytes, obytes, type)
  919. char *fname;
  920. unsigned long ibytes, obytes;
  921. unsigned char type;
  922. {
  923.     unsigned short _crc;
  924.     int i, n, ch, lastch;
  925.     FILE *outf;
  926.     char temp[256];
  927.  
  928.     _crc = INIT_CRC;
  929.     chkcrc = 1;            /* usually can check the CRC */
  930.  
  931.     if (check_access(fname) == -1) {
  932.     fseek(infp, ibytes, 1);
  933.     chkcrc = 0;        /* inhibit crc check if file not written */
  934.         return(-1);
  935.     }
  936.     
  937.     switch (type) {
  938.       case noComp:        /* no compression */
  939.     outf = fopen(fname, "w");
  940.     if (outf == NULL) {
  941.         perror(fname);
  942.         exit(1);
  943.     }
  944.     while (ibytes > 0) {
  945.         n = (ibytes > IOBUFSIZ) ? IOBUFSIZ : ibytes;
  946.         n = fread(iobuf, 1, n, infp);
  947.         if (n == 0)
  948.         break;
  949.         _crc = updcrc(_crc, iobuf, n);
  950.         outc(iobuf, n, outf);
  951.         ibytes -= n;
  952.     }
  953.     fclose(outf);
  954.     break;
  955.       case rleComp:        /* run length encoding */
  956.     outf = fopen(fname, "w");
  957.     if (outf == NULL) {
  958.         perror(fname);
  959.         exit(1);
  960.     }
  961.     while (ibytes > 0) {
  962.         ch = getc(infp) & 0xff;
  963.         ibytes--;
  964.         if (ch == 0x90) {    /* see if its the repeat marker */
  965.         n = getc(infp) & 0xff; /* get the repeat count */
  966.         ibytes--;
  967.         if (n == 0) { /* 0x90 was really an 0x90 */
  968.             iobuf[0] = 0x90;
  969.             _crc = updcrc(_crc, iobuf, 1);
  970.             outc(iobuf, 1, outf);
  971.         }
  972.         else {
  973.             n--;
  974.             for (i = 0; i < n; i++)
  975.             iobuf[i] = lastch;
  976.             _crc = updcrc(_crc, iobuf, n);
  977.             outc(iobuf, n, outf);
  978.         }
  979.         }
  980.         else {
  981.         iobuf[0] = ch;
  982.         _crc = updcrc(_crc, iobuf, 1);
  983.         lastch = ch;
  984.         outc(iobuf, 1, outf);
  985.         }
  986.     }
  987.     fclose(outf);
  988.     break;
  989.       case lzwComp:        /* LZW compression */
  990.     sprintf(temp, "%s%s", COMPRESS, " -d -c -n -b 14 ");
  991.     if (txtmode) {
  992.         strcat(temp, "| tr \'\\015\' \'\\012\' ");
  993.         chkcrc = 0;        /* can't check CRC in this case */
  994.     }
  995.     strcat(temp, "> '");
  996.     strcat(temp, fname);
  997.     strcat(temp, "'");
  998.     outf = popen(temp, "w");
  999.     if (outf == NULL) {
  1000.         perror(fname);
  1001.         exit(1);
  1002.     }
  1003.     while (ibytes > 0) {
  1004.         n = (ibytes > IOBUFSIZ) ? IOBUFSIZ : ibytes;
  1005.         n = fread(iobuf, 1, n, infp);
  1006.         if (n == 0)
  1007.         break;
  1008.         fwrite(iobuf, 1, n, outf);
  1009.         ibytes -= n;
  1010.     }
  1011.     pclose(outf);
  1012.     if (chkcrc) {
  1013.         outf = fopen(fname, "r"); /* read the file to get CRC value */
  1014.         if (outf == NULL) {
  1015.         perror(fname);
  1016.         exit(1);
  1017.         }
  1018.         while (1) {
  1019.         n = fread(iobuf, 1, IOBUFSIZ, outf);
  1020.         if (n == 0)
  1021.             break;
  1022.         _crc = updcrc(_crc, iobuf, n);
  1023.         }
  1024.         fclose(outf);
  1025.     }
  1026.     break;
  1027.       case hufComp:        /* Huffman compression */
  1028.     outf = fopen(fname, "w");
  1029.     if (outf == NULL) {
  1030.         perror(fname);
  1031.         exit(1);
  1032.     }
  1033.     nodeptr = nodelist;
  1034.     bit = 0;        /* put us on a byte boundary */
  1035.     read_tree();
  1036.     while (obytes > 0) {
  1037.         n = (obytes > IOBUFSIZ) ? IOBUFSIZ : obytes;
  1038.         for (i = 0; i < n; i++)
  1039.         iobuf[i] = gethuffbyte(DECODE);
  1040.         _crc = updcrc(_crc, iobuf, n);
  1041.         outc(iobuf, n, outf);
  1042.         obytes -= n;
  1043.     }
  1044.     fclose(outf);
  1045.     break;
  1046.       default:
  1047.     fprintf(stderr, "Unknown compression method\n");
  1048.     chkcrc = 0;        /* inhibit crc check if file not written */
  1049.     return(-1);
  1050.     }
  1051.  
  1052.     return(_crc & 0xffff);
  1053. }
  1054.  
  1055. outc(p, n, fp)
  1056. char *p;
  1057. int n;
  1058. FILE *fp;
  1059. {
  1060.     register char *p1;
  1061.     register int i;
  1062.     if (txtmode) {
  1063.     for (i = 0, p1 = p; i < n; i++, p1++)
  1064.         if ((*p1 & BYTEMASK) == '\r')
  1065.         *p1 = '\n';
  1066.     }
  1067.     fwrite(p, 1, n, fp);
  1068. }
  1069.  
  1070. long get4(bp)
  1071. char *bp;
  1072. {
  1073.     register int i;
  1074.     long value = 0;
  1075.  
  1076.     for (i = 0; i < 4; i++) {
  1077.     value <<= 8;
  1078.     value |= (*bp & BYTEMASK);
  1079.     bp++;
  1080.     }
  1081.     return(value);
  1082. }
  1083.  
  1084. short get2(bp)
  1085. char *bp;
  1086. {
  1087.     register int i;
  1088.     int value = 0;
  1089.  
  1090.     for (i = 0; i < 2; i++) {
  1091.     value <<= 8;
  1092.     value |= (*bp & BYTEMASK);
  1093.     bp++;
  1094.     }
  1095.     return(value);
  1096. }
  1097.  
  1098. copy(p1, p2, n)
  1099. char *p1, *p2;
  1100. int n;
  1101. {
  1102.     while (n-- > 0)
  1103.         *p1++ = *p2++;
  1104. }
  1105.  
  1106. /* This routine recursively reads the Huffman encoding table and builds
  1107.    and decoding tree. */
  1108.  
  1109. struct node *read_tree()
  1110. {
  1111.     struct node *np;
  1112.     np = nodeptr++;
  1113.     if (getbit() == 1) {
  1114.         np->flag = 1;
  1115.         np->byte = gethuffbyte(NODECODE);
  1116.     }
  1117.     else {
  1118.         np->flag = 0;
  1119.         np->zero = read_tree();
  1120.         np->one  = read_tree();
  1121.     }
  1122.     return(np);
  1123. }
  1124.  
  1125. /* This routine returns the next bit in the input stream (MSB first) */
  1126.  
  1127. getbit()
  1128. {
  1129.     static char b;
  1130.     if (bit == 0) {
  1131.         b = getc(infp) & 0xff;
  1132.         bit = 8;
  1133.     }
  1134.     bit--;
  1135.     return((b >> bit) & 1);
  1136. }
  1137.  
  1138. /* This routine returns the next 8 bits.  If decoding is on, it finds the
  1139. byte in the decoding tree based on the bits from the input stream.  If
  1140. decoding is not on, it either gets it directly from the input stream or
  1141. puts it together from 8 calls to getbit(), depending on whether or not we
  1142. are currently on a byte boundary
  1143. */
  1144. gethuffbyte(decode)
  1145. int decode;
  1146. {
  1147.     register struct node *np;
  1148.     register int i, b;
  1149.     if (decode == DECODE) {
  1150.         np = nodelist;
  1151.         while (np->flag == 0)
  1152.             np = (getbit()) ? np->one : np->zero;
  1153.         b = np->byte;
  1154.     }
  1155.     else {
  1156.         if (bit == 0)    /* on byte boundary? */
  1157.             b = getc(infp) & 0xff;
  1158.         else {        /* no, put a byte together */
  1159.             b = 0;
  1160.             for (i = 8; i > 0; i--) {
  1161.                 b = (b << 1) + getbit();
  1162.             }
  1163.         }
  1164.     }
  1165.     return(b);
  1166. }
  1167.