home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / aufstols.zoo / aufstools.2 < prev    next >
Text File  |  1991-02-26  |  45KB  |  1,251 lines

  1. #! /bin/sh
  2. # This is a shell archive.  Remove anything before this line, then unpack
  3. # it by saving it into a file and typing "sh file".  To overwrite existing
  4. # files, type "sh file -c".  You can also feed this as standard input via
  5. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  6. # will see the following message at the end:
  7. #        "End of archive 2 (of 4)."
  8. # Contents:  capit/capit.c mcvert/mactypes.h mcvert/mcvert.1
  9. #   mcvert/unpack.c stuffit/updcrc.c unstuffit/updcrc.c
  10. # Wrapped by np@asun5 on Mon Dec  3 13:15:56 1990
  11. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  12. if test -f 'capit/capit.c' -a "${1}" != "-c" ; then 
  13.   echo shar: Will not clobber existing file \"'capit/capit.c'\"
  14. else
  15. echo shar: Extracting \"'capit/capit.c'\" \(8984 characters\)
  16. sed "s/^X//" >'capit/capit.c' <<'END_OF_FILE'
  17. X/* unmacbin - reverse of macbin - change a MacBinary file back in to
  18. X   the .info .data .rsrc style that xbin, macput and macget understand.
  19. X   Stole some from macbin. */
  20. X
  21. X/* Written by John M. Sellens, jmsellens@watdragon.uwaterloo.ca,
  22. X   Math Faculty Computing Facility,
  23. X   University of Waterloo
  24. X   Waterloo, Ontario, Canada
  25. X   N2L 3G1    */
  26. X
  27. X/* capit - convert a macbin file into a CAP file on a CAP Unix Mac disc.
  28. X   Basically:
  29. X      file.data => file
  30. X      file.rsrc => .resource/file
  31. X      file.info => mangle it then .finderinfo/file
  32. X   Nigel Perry, Dept of Computing, Imperial College, London SW7 2BZ, England. July 90.
  33. X   np@doc.ic.ac.uk
  34. X */
  35. X
  36. X#include <stdio.h>
  37. X#include <sys/types.h>
  38. X#include <sys/dir.h>
  39. X#include <strings.h>
  40. X
  41. X#define BSIZE    (128)    /* size of blocks in MacBinary file */
  42. Xtypedef long    int4;
  43. X
  44. X
  45. X/* following from mcvert program */
  46. X
  47. X/* Useful, though not particularly Mac related, values */
  48. Xtypedef unsigned char byte;     /* one byte, obviously */
  49. Xtypedef unsigned short word;    /* must be 2 bytes */
  50. Xtypedef unsigned long ulong;    /* 4 bytes */
  51. X
  52. X#define NAMELEN 63              /* maximum legal Mac file name length */
  53. X
  54. X/* Format of a bin file:
  55. XA bin file is composed of 128 byte blocks.  The first block is the
  56. Xinfo_header (see below).  Then comes the data fork, null padded to fill the
  57. Xlast block.  Then comes the resource fork, padded to fill the last block.  A
  58. Xproposal to follow with the text of the Get Info box has not been implemented,
  59. Xto the best of my knowledge.  Version, zero1 and zero2 are what the receiving
  60. Xprogram looks at to determine if a MacBinary transfer is being initiated.
  61. X*/ 
  62. Xtypedef struct {     /* info file header (128 bytes). Unfortunately, these
  63. X                        longs don't align to word boundaries */
  64. X            byte version;           /* there is only a version 0 at this time */
  65. X            byte nlen;              /* Length of filename. */
  66. X            byte name[NAMELEN];     /* Filename (only 1st nlen are significant)*/
  67. X            byte type[4];           /* File type. */
  68. X            byte auth[4];           /* File creator. */
  69. X            byte flags;             /* file flags: LkIvBnSyBzByChIt */
  70. X            byte zero1;             /* Locked, Invisible,Bundle, System */
  71. X                                    /* Bozo, Busy, Changed, Init */
  72. X            byte icon_vert[2];      /* Vertical icon position within window */
  73. X            byte icon_horiz[2];     /* Horizontal icon postion in window */
  74. X            byte window_id[2];      /* Window or folder ID. */
  75. X            byte protect;           /* = 1 for protected file, 0 otherwise */
  76. X            byte zero2;
  77. X            byte dflen[4];          /* Data Fork length (bytes) -   most sig.  */
  78. X            byte rflen[4];          /* Resource Fork length         byte first */
  79. X            byte cdate[4];          /* File's creation date. */
  80. X            byte mdate[4];          /* File's "last modified" date. */
  81. X            byte ilen[2];           /* GetInfo message length */
  82. X        byte flags2;            /* Finder flags, bits 0-7 */
  83. X        byte unused[14];       
  84. X        byte packlen[4];        /* length of total files when unpacked */
  85. X        byte headlen[2];        /* length of secondary header */
  86. X        byte uploadvers;        /* Version of MacBinary II that the uploading program is written for */
  87. X        byte readvers;          /* Minimum MacBinary II version needed to read this file */
  88. X            byte crc[2];            /* CRC of the previous 124 bytes */
  89. X        byte padding[2];        /* two trailing unused bytes */
  90. X            } info_header;
  91. X
  92. X/* end of mcvert stuff */
  93. X/* from CAP aufs documentation */
  94. X
  95. X#define FINFOLEN 32
  96. X#define MAXCLEN 199
  97. Xtypedef struct
  98. X{  /* byte fi_fndr[FINFOLEN]; */    /* finder info */
  99. X   /* what I think the above is... */
  100. X   ulong fndr_type, fndr_creator;
  101. X   word fndr_flags;
  102. X   ulong fndr_loc;
  103. X   word fndr_fldr;
  104. X   word fndr_icon;
  105. X   byte fndr_unused[8];
  106. X   word fndr_comment;
  107. X   ulong fndr_putaway;
  108. X   /* end of fi_fndr */
  109. X
  110. X   word fi_attr;            /* attributes */
  111. X#define FI_MAGIC1 255
  112. X   byte fi_magic1;        /* was: length of comment */
  113. X#define FI_VERSION 0x10        /* version major 1, minor 0 */
  114. X                /* if more than 8 versions then */
  115. X                /* something wrong anyway */
  116. X   byte fi_version;        /* version number */
  117. X#define FI_MAGIC 0xda
  118. X   byte fi_magic;        /* magic word check */
  119. X   byte fi_bitmap;        /* bitmap of included info */
  120. X#define FI_BM_SHORTFILENAME 0x1    /* is this included? */
  121. X#define FI_BM_MACINTOSHFILENAME 0x2 /* is this included? */
  122. X   byte fi_shortfilename[12+1];    /* possible short file name */
  123. X   byte fi_macfilename[32+1];    /* possible macintosh file name */
  124. X   byte fi_comln;        /* comment length */
  125. X   byte fi_comnt[MAXCLEN+1];    /* comment string */
  126. X} FileInfo;
  127. X
  128. X/* end aufs */
  129. X
  130. Xstatic info_header info;
  131. Xstatic FileInfo fndr_info;
  132. X
  133. Xstatic union trans {
  134. X    int4    num;
  135. X    char    ch[4];
  136. X} trans;
  137. X
  138. X
  139. Xmain(argc,argv)
  140. Xint argc;
  141. Xchar **argv;
  142. X{
  143. X    FILE *fp, *ofp;
  144. X    char bname[MAXNAMLEN];
  145. X    char iname[MAXNAMLEN];
  146. X    char dname[MAXNAMLEN];
  147. X    char rname[MAXNAMLEN];
  148. X    char buf[BSIZE];
  149. X    char * charp;
  150. X    int verbose = 0;
  151. X    int len;
  152. X    int arg;
  153. X    int err = 0;
  154. X    long dflen, rflen;
  155. X    char *ext, *disc;
  156. X    extern char *getenv();
  157. X
  158. X    if((ext = getenv("MAC_EXT")) == NULL) ext = ".bin";
  159. X    if((disc = getenv("MAC_DISC")) == NULL) disc = ".";
  160. X
  161. X    arg = 1;
  162. X    if (argc > 1 && strcmp(argv[1], "-v") == 0 ) {
  163. X    verbose = 1;
  164. X    ++arg;
  165. X    }
  166. X    if ( arg >= argc ) {
  167. X    fprintf(stderr,"%s: Usage: %s [-v] filename(s)\n",argv[0],argv[0]);
  168. X    exit(1);
  169. X    }
  170. X    for ( ; arg < argc; arg++ ) {
  171. X    if ( (charp = rindex (argv[arg], '.')) != NULL
  172. X        && strcmp(charp, ext) == 0 ) {
  173. X        *charp = '\0';
  174. X        strcpy(bname, argv[arg]);
  175. X        *charp = '.';
  176. X    } else
  177. X        strcpy(bname, argv[arg]);
  178. X
  179. X    sprintf(iname, "%s/.finderinfo/%s", disc, bname);
  180. X    sprintf(dname, "%s/%s", disc, bname);
  181. X    sprintf(rname, "%s/.resource/%s", disc, bname);
  182. X
  183. X    if (verbose)
  184. X        printf( "Converting '%s'\n", argv[arg] );
  185. X    if ( (fp = fopen( argv[arg], "r" )) == NULL ) {
  186. X        fprintf( stderr, "%s: couldn't open '%s' for reading",
  187. X        argv[0], argv[arg] );
  188. X        perror( "" );
  189. X        exit(++err);
  190. X    }
  191. X    if ( fread(&info, sizeof(info), 1, fp) <= 0 ) {
  192. X        fprintf( stderr, "%s: couldn't read .info header from '%s'",
  193. X        argv[0], argv[arg] );
  194. X        perror( "" );
  195. X        exit(++err);
  196. X    }
  197. X    if ( info.zero1 || info.zero2 || info.version ) {
  198. X        fprintf( stderr, "%s: '%s' is not in MacBinary format - skipped\n",
  199. X        argv[0], argv[arg] );
  200. X        ++err;
  201. X        continue;
  202. X    }
  203. X
  204. X    /* make the .finderinfo file */
  205. X    bzero(&fndr_info, sizeof(FileInfo));
  206. X    bcopy(info.type, &fndr_info.fndr_type, 4);
  207. X    bcopy(info.auth, &fndr_info.fndr_creator, 4);
  208. X        if(info.protect == '\1' ) fndr_info.fndr_flags = 0x40; /* maybe? */
  209. X    fndr_info.fi_magic1 = FI_MAGIC1;
  210. X    fndr_info.fi_version = FI_VERSION;
  211. X    fndr_info.fi_magic = FI_MAGIC;
  212. X    fndr_info.fi_bitmap = FI_BM_MACINTOSHFILENAME;
  213. X    bcopy(info.name, fndr_info.fi_macfilename, info.nlen);
  214. X
  215. X    /* write the .finderinfo file */
  216. X    if ( (ofp = fopen( iname, "w" )) == NULL ) {
  217. X        fprintf( stderr, "%s: couldn't open '%s' for writing",
  218. X        argv[0], iname );
  219. X        perror( "" );
  220. X        exit(++err);
  221. X    }
  222. X    fwrite( &fndr_info, sizeof(FileInfo), 1, ofp );
  223. X    fclose( ofp );
  224. X
  225. X    /* It appears that the .data and .rsrc parts of the MacBinary file
  226. X       are padded to the nearest 128 (BSIZE) byte boundary, but they
  227. X       should be trimmed to their proper size when we split them. */
  228. X
  229. X    trans.ch[0] = info.dflen[0]; trans.ch[1] = info.dflen[1];
  230. X    trans.ch[2] = info.dflen[2]; trans.ch[3] = info.dflen[3];
  231. X    dflen = ntohl( trans.num );
  232. X    trans.ch[0] = info.rflen[0]; trans.ch[1] = info.rflen[1];
  233. X    trans.ch[2] = info.rflen[2]; trans.ch[3] = info.rflen[3];
  234. X    rflen = ntohl( trans.num );
  235. X
  236. X    /* write the data fork */
  237. X    if ( (ofp = fopen( dname, "w" )) == NULL ) {
  238. X        fprintf( stderr, "%s: couldn't open '%s' for writing",
  239. X        argv[0], dname );
  240. X        perror( "" );
  241. X        exit(++err);
  242. X    }
  243. X    for ( len=0; len<dflen;  ) {
  244. X        if ( fread( buf, sizeof(char), BSIZE, fp ) != BSIZE ) {
  245. X        fprintf( stderr, "%s: couldn't read %d bytes from '%s'",
  246. X            argv[0], BSIZE, argv[arg] );
  247. X        fprintf(stderr,"got %d of %d'n",len,dflen);
  248. X        perror( "" );
  249. X        exit(++err);
  250. X        }
  251. X        len += BSIZE;
  252. X        if ( len > dflen )
  253. X        fwrite( buf, sizeof(char), BSIZE-len+dflen, ofp );
  254. X        else
  255. X        fwrite( buf, sizeof(char), BSIZE, ofp );
  256. X    }
  257. X    fclose( ofp );
  258. X        
  259. X    /* write the .resource file */
  260. X    if ( (ofp = fopen( rname, "w" )) == NULL ) {
  261. X        fprintf( stderr, "%s: couldn't open '%s' for writing",
  262. X        argv[0], rname );
  263. X        perror( "" );
  264. X        exit(++err);
  265. X    }
  266. X    for ( len=0; len<rflen;  ) {
  267. X        if ( fread( buf, sizeof(char), BSIZE, fp ) != BSIZE ) {
  268. X        fprintf( stderr, "%s: couldn't read %d bytes from '%s'",
  269. X            argv[0], BSIZE, argv[arg] );
  270. X        fprintf(stderr,"got %d of %d'n",len,rflen);
  271. X        perror( "" );
  272. X        exit(++err);
  273. X        }
  274. X        len += BSIZE;
  275. X        if ( len > rflen )
  276. X        fwrite( buf, sizeof(char), BSIZE-len+rflen, ofp );
  277. X        else
  278. X        fwrite( buf, sizeof(char), BSIZE, ofp );
  279. X    }
  280. X    fclose( ofp );
  281. X    fclose( fp );
  282. X    }
  283. X    exit( err );
  284. X}
  285. END_OF_FILE
  286. if test 8984 -ne `wc -c <'capit/capit.c'`; then
  287.     echo shar: \"'capit/capit.c'\" unpacked with wrong size!
  288. fi
  289. # end of 'capit/capit.c'
  290. fi
  291. if test -f 'mcvert/mactypes.h' -a "${1}" != "-c" ; then 
  292.   echo shar: Will not clobber existing file \"'mcvert/mactypes.h'\"
  293. else
  294. echo shar: Extracting \"'mcvert/mactypes.h'\" \(7478 characters\)
  295. sed "s/^X//" >'mcvert/mactypes.h' <<'END_OF_FILE'
  296. X#include <stdio.h>
  297. X#include <sys/types.h>
  298. X#include <sys/dir.h>
  299. X#include <sys/stat.h>
  300. X#include <sys/timeb.h>
  301. X
  302. X/* Useful, though not particularly Mac related, values */
  303. Xtypedef unsigned char byte;     /* one byte, obviously */
  304. Xtypedef unsigned short word;    /* must be 2 bytes */
  305. Xtypedef unsigned long ulong;    /* 4 bytes */
  306. X#define TRUE  1
  307. X#define FALSE 0
  308. X#define CR 0x0d
  309. X#define LF 0x0a
  310. X
  311. X/* Compatibility issues */
  312. X#ifdef BSD
  313. X#define mac2word (word) ntohs
  314. X#define mac2long (ulong) ntohl
  315. X#define word2mac (word) htons
  316. X#define long2mac (ulong) htonl
  317. X#else
  318. X#define mac2word
  319. X#define mac2long
  320. X#define word2mac
  321. X#define long2mac
  322. X#endif
  323. X
  324. X#ifdef MAXNAMLEN/* 4.2 BSD, stdio.h */
  325. X#define SYSNAMELEN MAXNAMLEN
  326. X#else
  327. X#define SYSNAMELEN DIRSIZ
  328. X#endif
  329. X
  330. X#define NAMELEN 63              /* maximum legal Mac file name length */
  331. X#define BINNAMELEN 68           /* NAMELEN + len(".bin\0") */
  332. X
  333. X/* Format of a bin file:
  334. XA bin file is composed of 128 byte blocks.  The first block is the
  335. Xinfo_header (see below).  Then comes the data fork, null padded to fill the
  336. Xlast block.  Then comes the resource fork, padded to fill the last block.  A
  337. Xproposal to follow with the text of the Get Info box has not been implemented,
  338. Xto the best of my knowledge.  Version, zero1 and zero2 are what the receiving
  339. Xprogram looks at to determine if a MacBinary transfer is being initiated.
  340. X*/ 
  341. Xtypedef struct {     /* info file header (128 bytes). Unfortunately, these
  342. X                        longs don't align to word boundaries */
  343. X            byte version;           /* there is only a version 0 at this time */
  344. X            byte nlen;              /* Length of filename. */
  345. X            byte name[NAMELEN];     /* Filename (only 1st nlen are significant)*/
  346. X            byte type[4];           /* File type. */
  347. X            byte auth[4];           /* File creator. */
  348. X            byte flags;             /* file flags: LkIvBnSyBzByChIt */
  349. X            byte zero1;             /* Locked, Invisible,Bundle, System */
  350. X                                    /* Bozo, Busy, Changed, Init */
  351. X            byte icon_vert[2];      /* Vertical icon position within window */
  352. X            byte icon_horiz[2];     /* Horizontal icon postion in window */
  353. X            byte window_id[2];      /* Window or folder ID. */
  354. X            byte protect;           /* = 1 for protected file, 0 otherwise */
  355. X            byte zero2;
  356. X            byte dlen[4];           /* Data Fork length (bytes) -   most sig.  */
  357. X            byte rlen[4];           /* Resource Fork length         byte first */
  358. X            byte ctim[4];           /* File's creation date. */
  359. X            byte mtim[4];           /* File's "last modified" date. */
  360. X            byte ilen[2];           /* GetInfo message length */
  361. X        byte flags2;            /* Finder flags, bits 0-7 */
  362. X        byte unused[14];       
  363. X        byte packlen[4];        /* length of total files when unpacked */
  364. X        byte headlen[2];        /* length of secondary header */
  365. X        byte uploadvers;        /* Version of MacBinary II that the uploading program is written for */
  366. X        byte readvers;          /* Minimum MacBinary II version needed to read this file */
  367. X            byte crc[2];            /* CRC of the previous 124 bytes */
  368. X        byte padding[2];        /* two trailing unused bytes */
  369. X            } info_header;
  370. X
  371. X/* The *.info file of a MacTerminal file transfer either has exactly this
  372. Xstructure or has the protect bit in bit 6 (near the sign bit) of byte zero1.
  373. XThe code I have for macbin suggests the difference, but I'm not so sure */
  374. X
  375. X/* Format of a hqx file:
  376. XIt begins with a line that begins "(This file
  377. Xand the rest is 64 character lines (except possibly the last, and not
  378. Xincluding newlines) where the first begins and the last ends with a colon.
  379. XThe characters between colons should be only from the set in tr86, below,
  380. Xeach of which corresponds to 6 bits of data.  Once that is translated to
  381. X8 bit bytes, you have the real data, except that the byte 0x90 may 
  382. Xindicate, if the following character is nonzero, that the previous
  383. Xbyte is to be repeated 1 to 255 times.  The byte 0x90 is represented by
  384. X0x9000.  The information in the file is the hqx_buf (see below),
  385. Xa CRC word, the data fork, a CRC word, the resource fork, and a CRC word.
  386. XThere is considerable confusion about the flags.  An official looking document
  387. Xunclearly states that the init bit is always clear, as is the following byte.
  388. XThe experience of others suggests, however, that this is not the case.
  389. X*/
  390. X
  391. X#define HQXLINELEN 64
  392. Xtypedef struct {
  393. X            byte version;           /* there is only a version 0 at this time */
  394. X            byte type[4];           /* File type. */
  395. X            byte auth[4];           /* File creator. */
  396. X            byte flags;             /* file flags: LkIvBnSyBzByChIt */
  397. X            byte protect;           /* ?Pr??????, don't know what ? bits mean */
  398. X            byte dlen[4];           /* Data Fork length (bytes) -   most sig.  */
  399. X            byte rlen[4];           /* Resource Fork length         byte first */
  400. X            byte bugblank;             /* to fix obscure sun 3/60 problem
  401. X                                          that always makes sizeof(hqx_header
  402. X                                          even */
  403. X            } hqx_header;
  404. Xtypedef struct {     /* hqx file header buffer (includes file name) */
  405. X            byte nlen;              /* Length of filename. */
  406. X            byte name[NAMELEN];     /* Filename: only nlen actually appear */
  407. X            hqx_header all_the_rest;/* and all the rest follows immediately */
  408. X            } hqx_buf;
  409. X
  410. X/* Format of a Packit file:
  411. XRepeat the following sequence for each file in the Packit file:
  412. X    4 byte identifier ("PMag" = not compressed, "Pma4" = compressed)
  413. X    320 byte compression data (if compressed file)
  414. X        = preorder transversal of Huffman tree
  415. X        255 0 bits corresponding to nonleaf nodes
  416. X        256 1 bits corresponding to leaf nodes
  417. X        256 bytes associating leaf nodes with bytes
  418. X        1   completely wasted bit
  419. X    92 byte header (see pit_header below) *
  420. X    2 bytes CRC word for header *
  421. X    data fork (length from header) *
  422. X    resource fork (length from header) *
  423. X    2 bytes CRC word for forks *
  424. X
  425. XLast file is followed by the 4 byte Ascii string, "Pend", and then the EOF.
  426. XThe CRC calculations differ from those in the binhex format.
  427. X
  428. X* these are in compressed form if compression is on for the file
  429. X
  430. X*/
  431. X
  432. Xtypedef struct {     /* Packit file header (92 bytes) */
  433. X            byte nlen;              /* Length of filename. */
  434. X            byte name[NAMELEN];     /* Filename (only 1st nlen are significant)*/
  435. X            byte type[4];           /* File type. */
  436. X            byte auth[4];           /* File creator. */
  437. X            byte flags;             /* file flags: LkIvBnSyBzByChIt */
  438. X            byte zero1;
  439. X            byte protect;           /* = 1 for protected file, 0 otherwise */
  440. X            byte zero2;
  441. X            byte dlen[4];           /* Data Fork length (bytes) -   most sig.  */
  442. X            byte rlen[4];           /* Resource Fork length         byte first */
  443. X            byte ctim[4];           /* File's creation date. */
  444. X            byte mtim[4];           /* File's "last modified" date. */
  445. X            } pit_header;
  446. X
  447. X/* types for constructing the Huffman tree */
  448. Xtypedef struct branch_st {
  449. X            byte flag;
  450. X            struct branch_st *one, *zero;
  451. X            } branch;
  452. X
  453. Xtypedef struct leaf_st {
  454. X            byte flag;
  455. X            byte data;
  456. X            } leaf;
  457. END_OF_FILE
  458. if test 7478 -ne `wc -c <'mcvert/mactypes.h'`; then
  459.     echo shar: \"'mcvert/mactypes.h'\" unpacked with wrong size!
  460. fi
  461. # end of 'mcvert/mactypes.h'
  462. fi
  463. if test -f 'mcvert/mcvert.1' -a "${1}" != "-c" ; then 
  464.   echo shar: Will not clobber existing file \"'mcvert/mcvert.1'\"
  465. else
  466. echo shar: Extracting \"'mcvert/mcvert.1'\" \(5991 characters\)
  467. sed "s/^X//" >'mcvert/mcvert.1' <<'END_OF_FILE'
  468. X.TH MCVERT LOCAL "May 5, 1987"
  469. X.UC 4.2
  470. X.SH NAME
  471. Xmcvert \- BinHex 4.0 to MacBinary file conversion utility
  472. X.SH SYNOPSIS
  473. X.B mcvert
  474. X[-options] name... [[-options] name...]...
  475. X.br
  476. X.SH DESCRIPTION
  477. XThe
  478. X.I mcvert
  479. Xprogram translates MacIntosh files from one format to another.
  480. XThe primary formats in which MacIntosh files are represented on non-Macs are:
  481. X.TP
  482. X.B MacBinary:
  483. XAn eight bit wide representation of the data and resource forks of a Mac
  484. Xfile and of relevant Finder information, MacBinary files are recognized
  485. Xas "special" by several MacIntosh terminal emulators.  These emulators,
  486. Xusing Kermit or Xmodem or any other file transfer protocol, can separate
  487. Xthe incoming file into forks and appropriately modify the Desktop to display
  488. Xicons, types, creation dates, and the like.
  489. X.TP
  490. X.B BinHex 4.0:
  491. XA seven bit wide representation of a Mac file with CRC error checking,
  492. XBinHex 4.0 files are designed for communication of Mac files over long
  493. Xdistance, possibly noisy, seven bit wide paths.
  494. X.TP
  495. X.B PackIt:
  496. XPackIt files are actually representations of collections of Mac files, possibly
  497. XHuffman compressed.  Packing many small related files together before
  498. Xa MacBinary transfer or a translation to BinHex 4.0 is common practice.
  499. X.TP
  500. X.B Text:
  501. XA MacIntosh ends each line of a plain text file with a carriage return
  502. Xcharacter (^M), rather than the newline character (^J) that some systems
  503. Xseem to prefer.  Moreover, a MacBinary file has prepended Finder information
  504. Xthat non-MacIntoshes don't need.
  505. X.TP
  506. X.B Data, Rsrc:
  507. XA Data or Rsrc file is the exact copy of the data or resource fork of a
  508. XMacIntosh file.
  509. X.PP
  510. XIt is the purpose of this program to convert to the MacBinary format
  511. Xfiles in other of the above formats, and vice versa.
  512. X.PP
  513. X.SH PARAMETERS
  514. XExactly one of the following operations may be specified for an input name:
  515. X.TP
  516. X.B x
  517. XBinHex 4.0 - files in the MacBinary format are translated to BinHex
  518. Xfiles, or vice versa.  The name argument may be the name of a file to be
  519. Xconverted or a basename to which an appropriate suffix must be appended
  520. Xto get a filename.  If the conversion is from Binhex 4.0 to MacBinary,
  521. Xseveral files may comprise the BinHex representation of the Mac file.
  522. XRather than manually concatenate the files and manually delete mail
  523. Xheaders and other extraneous garbage, one may specify the names of the
  524. Xfiles in order and
  525. X.I mcvert
  526. Xwill do the concatenating and deleting.  Conversely, in converting
  527. Xa MacBinary file to BinHex 4.0 format for mailing over long distances,
  528. Xone may be restricted to mail messages of no greater that some fixed
  529. Xlength.  In this case,
  530. X.I mcvert
  531. Xcan automatically divide the BinHex file into pieces and label each
  532. Xpiece appropriately.
  533. XOption 'x' is selected by default.
  534. X.TP
  535. X.B r
  536. XResource - files in the MacBinary format with empty data forks
  537. Xand nonempty resource forks are made from ordinary data files, or vice versa.
  538. X.TP
  539. X.B d
  540. XData - files in the MacBinary format with nonempty data forks
  541. Xand empty resource forks are made from ordinary data files, or vice versa.
  542. X.TP
  543. X.B u
  544. XText - files in the MacBinary format with nonempty data forks
  545. Xand empty resource forks are made from ordinary data files, or vice versa.
  546. XUnix newline
  547. Xcharacters are interchanged with MacIntosh carriage return
  548. Xcharacters, and a newly created MacBinary file has creator field given by
  549. Xthe MAC_EDITOR environment variable.
  550. X.PP
  551. X.SH OPTIONS
  552. X.TP
  553. X.B p | q
  554. XIf a BinHex to MacBinary conversion is taking place and option 'p' is selected,
  555. Xany file of type "PIT "
  556. Xwill be unpacked into its constituent parts.  This option does not recursively
  557. Xunpack "PIT " files packed in "PIT " files.
  558. XIf a MacBinary to BinHex conversion is taking place, this option is currently
  559. Xignored.  By default, option 'q' is selected.
  560. X.TP
  561. X.B U | D
  562. XWhen option 'U' is selected, the conversion that takes place is the one suitable
  563. Xfor Uploading files.  That is, the conversion is from MacBinary to something
  564. Xelse when 'U' is selected.  Conversely, option 'D', as in Download,
  565. Xconverts from something to MacBinary.  Option 'D' is the default.
  566. X.TP
  567. X.B s | v
  568. XNormally,
  569. X.I mcvert
  570. Xprints to stderr information about the files it is creating.  Selecting
  571. Xoption 's', as in silent, disables this reporting.  Option 'v', for verbose,
  572. Xis the default.
  573. X.SH "ENVIRONMENT VARIABLES"
  574. XThere are four environment variables one may use to customize 
  575. Xthe behavior of
  576. X.I mcvert
  577. Xslightly.
  578. X.TP
  579. X.B MAC_EDITOR
  580. XThe creator of MacBinary text files produced with options -uD.  
  581. XThe default is MACA, the creator type of MacWrite.
  582. X.TP
  583. X.B MAC_DLOAD_DIR
  584. XThe MacBinary files created when option -D is selected are placed in this
  585. Xdirectory.  The default is ".", the current working directory.
  586. X.TP
  587. X.B MAC_EXT
  588. XThe MacBinary files created when option -D is selected are named according
  589. Xto the filename field stored in the file header, with the name extended by
  590. Xthis suffix.  The default is ".bin".
  591. X.TP
  592. X.B MAC_LINE_LIMIT
  593. XThe BinHex files created when option -U is selected may be no longer than
  594. Xthis many lines long.  Files that would otherwise exceed this line limit
  595. Xare broken up into several files with numbers embedded into their file 
  596. Xnames to show their order.  Each such file has "Start of part x" and "End
  597. Xof part x" messages included where appropriate.
  598. X.SH BUGS
  599. XIt should be possible to discard bad input now and successfully translate
  600. Xgood input later, but bad input mostly just causes immediate termination.
  601. X.PP
  602. XA more diligent person would support BinHex 3.0 and BinHex 2.0 and BinHex
  603. X5000.0 B. C., but I've never seen anyone use them in three years.  A
  604. Xmore diligent person would also do something for users of macget and
  605. Xmacput, but hopefully someone will make those programs support the
  606. XMacBinary file protocol.
  607. X.SH SEE ALSO
  608. Xxbin(1), macget(1), macput(1), xmodem(1), kermit(1)
  609. X.SH AUTHOR
  610. XDoug Moore, Cornell University Computer Science.  Based upon
  611. X.I xbin
  612. Xby Dave Johnson, Brown University, as modified by Guido van Rossum, and upon
  613. X.I unpit
  614. Xby Allan G. Weber, as well as upon correspondence with several helpful
  615. Xreaders of USENET.
  616. X
  617. END_OF_FILE
  618. if test 5991 -ne `wc -c <'mcvert/mcvert.1'`; then
  619.     echo shar: \"'mcvert/mcvert.1'\" unpacked with wrong size!
  620. fi
  621. # end of 'mcvert/mcvert.1'
  622. fi
  623. if test -f 'mcvert/unpack.c' -a "${1}" != "-c" ; then 
  624.   echo shar: Will not clobber existing file \"'mcvert/unpack.c'\"
  625. else
  626. echo shar: Extracting \"'mcvert/unpack.c'\" \(6606 characters\)
  627. sed "s/^X//" >'mcvert/unpack.c' <<'END_OF_FILE'
  628. X#include "mactypes.h"
  629. X
  630. Xextern word magic[];
  631. Xextern FILE *verbose;
  632. Xextern char *dir, *ext;
  633. X
  634. Xulong pit_datalen, pit_rsrclen;
  635. Xword hqx_crc, write_pit_fork(); 
  636. Xchar pitfname[BINNAMELEN];              /* name of file being unpacked */
  637. XFILE *pitfile;                          /* output file */
  638. X
  639. Xbranch branchlist[255], *branchptr, *read_tree();
  640. Xleaf leaflist[256], *leafptr;
  641. Xword Huff_nibble, Huff_bit_count;
  642. Xbyte (*read_char)(), get_crc_byte(), getHuffbyte();
  643. X
  644. Xword un_pit()
  645. X{   char PitId[4];
  646. X    int i;
  647. X    word pit_crc;
  648. X
  649. X    hqx_crc = 0;
  650. X    /* Read and unpack until the PackIt End message is read */
  651. X    for (;;) {
  652. X        read_char = get_crc_byte;
  653. X        for (i = 0; i < 4; i++) PitId[i] = (char) get_crc_byte();
  654. X        if (!strncmp(PitId, "PEnd", 4)) break;
  655. X
  656. X        if (strncmp(PitId, "PMag", 4) && strncmp(PitId, "PMa4", 4))
  657. X            error("Unrecognized Packit format message %s", PitId);
  658. X
  659. X        if (PitId[3] == '4') {          /* if this file is compressed */
  660. X            branchptr = branchlist;     /* read the Huffman decoding  */
  661. X            leafptr = leaflist;         /* tree that is on the input  */
  662. X            Huff_bit_count = 0;         /* and use Huffman decoding   */
  663. X            read_tree();                /* subsequently               */
  664. X            read_char = getHuffbyte;
  665. X            }
  666. X
  667. X        read_pit_hdr();     /* also calculates datalen, rsrclen,
  668. X                               pitfile, pitfname */
  669. X        pit_crc = write_pit_fork(pit_datalen, 0);
  670. X        pit_crc = write_pit_fork(pit_rsrclen, pit_crc);
  671. X        check_pit_crc(pit_crc, "  File data/rsrc CRC mismatch in %s", pitfname);
  672. X        fclose(pitfile);
  673. X        }
  674. X    hqx_crc = (hqx_crc << 8) ^ magic[hqx_crc >> 8];
  675. X    hqx_crc = (hqx_crc << 8) ^ magic[hqx_crc >> 8];
  676. X    return hqx_crc;
  677. X    }
  678. X
  679. Xcheck_pit_crc(calc_crc, msg, name)
  680. Xword calc_crc;
  681. Xchar msg[], name[];
  682. X{   word read_crc;
  683. X    read_crc = (*read_char)() << 8;
  684. X    read_crc |= (*read_char)();
  685. X    if (read_crc != calc_crc) error(msg, name);
  686. X    }
  687. X
  688. X/* This routine reads the header of a packed file and appropriately twiddles it,
  689. X    determines if it has CRC problems, creates the .bin file, and puts the info
  690. X    into the .bin file.
  691. X    Output is pit_datalen, pit_rsrclen, pitfname, pitfile */
  692. Xread_pit_hdr()
  693. X{   register int n;
  694. X    register byte *pit_byte;
  695. X    register ulong pit_crc;
  696. X    pit_header pit;
  697. X    info_header info;
  698. X    short crc;
  699. X
  700. X    extern short calc_mb_crc();
  701. X    /* read the pit header and compute the CRC */
  702. X    pit_crc = 0;
  703. X    pit_byte = (byte *) &pit;
  704. X    for (n = 0; n < sizeof(pit_header); n++) {
  705. X        *pit_byte = (*read_char)();
  706. X        pit_crc = ((pit_crc & 0xff) << 8)
  707. X                    ^ magic[*pit_byte++ ^ (pit_crc >> 8)];
  708. X        }
  709. X
  710. X    /* stuff the pit header data into the info header */
  711. X    bzero(&info, sizeof(info_header));
  712. X    info.nlen = pit.nlen;
  713. X    strncpy(info.name, pit.name, pit.nlen);     /* name */
  714. X    bcopy(pit.type, info.type, 9);              /* type, author, flag */
  715. X    bcopy(pit.dlen, info.dlen, 16);             /* (d,r)len, (c,m)tim */
  716. X    info.flags  &= 0x7e;                        /* reset lock bit, init bit */
  717. X    if (pit.protect & 0x40) info.protect = 1;   /* copy protect bit */
  718. X    info.uploadvers = '\201';
  719. X    info.readvers = '\201';
  720. X
  721. X    /* calculate MacBinary CRC */
  722. X    crc = calc_mb_crc(&info, 124, 0);
  723. X    info.crc[0] = (char) (crc >> 8);
  724. X    info.crc[1] = (char) crc;
  725. X
  726. X    /* Create the .bin file and write the info to it */
  727. X    pit.name[pit.nlen] = '\0';
  728. X    unixify(pit.name);
  729. X    sprintf(pitfname, "%s/%s%s", dir, pit.name, ext);
  730. X    fprintf(verbose,
  731. X        " %-14s%-30s type = \"%4.4s\", author = \"%4.4s\"\n",
  732. X        (read_char == get_crc_byte) ? "Unpacking" : "Decompressing",
  733. X        pit.name, pit.type, pit.auth);
  734. X    if ((pitfile = fopen(pitfname, "w")) == NULL)
  735. X        error("  Cannot open %s", pitfname);
  736. X    check_pit_crc(pit_crc, "  File header CRC mismatch in %s", pitfname);
  737. X    fwrite(&info, sizeof(info_header), 1, pitfile);
  738. X
  739. X    /* Get a couple of items we'll need later */
  740. X    bcopy(pit.dlen, &pit_datalen, 4);
  741. X    pit_datalen = mac2long(pit_datalen);
  742. X    bcopy(pit.rlen, &pit_rsrclen, 4);
  743. X    pit_rsrclen = mac2long(pit_rsrclen);
  744. X    }
  745. X
  746. X/* This routine copies bytes from the decoded input stream to the output
  747. X    and calculates the CRC.  It also pads to a multiple of 128 bytes on the
  748. X    output, which is part of the .bin format */
  749. Xword write_pit_fork(nbytes, calc_crc)
  750. Xregister ulong nbytes;
  751. Xregister ulong calc_crc;
  752. X{   register ulong b;
  753. X    int extra_bytes;
  754. X
  755. X    extra_bytes = 127 - (nbytes+127)%128; /* pad fork to mult of 128 bytes */
  756. X    while (nbytes--) {
  757. X        b = (*read_char)();
  758. X        calc_crc = ((calc_crc & 0xff) << 8) ^ magic[b ^ (calc_crc >> 8)];
  759. X        putc(b, pitfile);
  760. X        }
  761. X    while (extra_bytes--) putc(0, pitfile);
  762. X    return (word) calc_crc;
  763. X    }
  764. X
  765. X/* This routine recursively reads the compression decoding data.
  766. X   It appears to be Huffman compression.  Every leaf is represented
  767. X   by a 1 bit, then the byte it represents.  A branch is represented
  768. X   by a 0 bit, then its zero and one sons */
  769. Xbranch *read_tree()
  770. X{   register branch *branchp;
  771. X    register leaf *leafp;
  772. X    register ulong b;
  773. X    if (!Huff_bit_count--) {
  774. X        Huff_nibble = get_crc_byte();
  775. X        Huff_bit_count = 7;
  776. X        }
  777. X    if ((Huff_nibble<<=1) & 0x0100) {
  778. X        leafp = leafptr++;
  779. X        leafp->flag = 1;
  780. X        b = get_crc_byte();
  781. X        leafp->data = Huff_nibble | (b >> Huff_bit_count);
  782. X        Huff_nibble = b << (8 - Huff_bit_count);
  783. X        return (branch *) leafp;
  784. X        }
  785. X    else {
  786. X        branchp = branchptr++;
  787. X        branchp->flag = 0;
  788. X        branchp->zero = read_tree();
  789. X        branchp->one  = read_tree();
  790. X        return branchp;
  791. X        }
  792. X    }
  793. X
  794. X/* This routine returns the next 8 bits.  It finds the byte in the
  795. X   Huffman decoding tree based on the bits from the input stream. */
  796. Xbyte getHuffbyte()
  797. X{   register branch *branchp;
  798. X    branchp = branchlist;
  799. X    while (!branchp->flag) {
  800. X        if (!Huff_bit_count--) {
  801. X            Huff_nibble = get_crc_byte();
  802. X            Huff_bit_count = 7;
  803. X            }
  804. X        branchp = ((Huff_nibble<<=1) & 0x0100) ? branchp->one : branchp->zero;
  805. X        }
  806. X    return ((leaf *) branchp)->data;
  807. X    }
  808. X
  809. X/* This routine returns the next byte on the .hqx input stream, hiding
  810. X    most file system details at a lower level.  .hqx CRC is maintained
  811. X    here */
  812. Xbyte get_crc_byte()
  813. X{   register ulong c;
  814. X    extern byte *buf_ptr, *buf_end;
  815. X    if (buf_ptr == buf_end) fill_hqxbuf();
  816. X    c = *buf_ptr++;
  817. X    hqx_crc = ((hqx_crc << 8) | c) ^ magic[hqx_crc >> 8];
  818. X    return (byte) c;
  819. X    }
  820. END_OF_FILE
  821. if test 6606 -ne `wc -c <'mcvert/unpack.c'`; then
  822.     echo shar: \"'mcvert/unpack.c'\" unpacked with wrong size!
  823. fi
  824. # end of 'mcvert/unpack.c'
  825. fi
  826. if test -f 'stuffit/updcrc.c' -a "${1}" != "-c" ; then 
  827.   echo shar: Will not clobber existing file \"'stuffit/updcrc.c'\"
  828. else
  829. echo shar: Extracting \"'stuffit/updcrc.c'\" \(5848 characters\)
  830. sed "s/^X//" >'stuffit/updcrc.c' <<'END_OF_FILE'
  831. X/* updcrc(3), crc(1) - calculate crc polynomials
  832. X *
  833. X * Calculate, intelligently, the CRC of a dataset incrementally given a 
  834. X * buffer full at a time.
  835. X * 
  836. X * Usage:
  837. X *     newcrc = updcrc( oldcrc, bufadr, buflen )
  838. X *         unsigned int oldcrc, buflen;
  839. X *         char *bufadr;
  840. X *
  841. X * Compiling with -DTEST creates a program to print the CRC of stdin to stdout.
  842. X * Compile with -DMAKETAB to print values for crctab to stdout.  If you change
  843. X *    the CRC polynomial parameters, be sure to do this and change
  844. X *    crctab's initial value.
  845. X *
  846. X * Notes:
  847. X *  Regards the data stream as an integer whose MSB is the MSB of the first
  848. X *  byte recieved.  This number is 'divided' (using xor instead of subtraction)
  849. X *  by the crc-polynomial P.
  850. X *  XMODEM does things a little differently, essentially treating the LSB of
  851. X * the first data byte as the MSB of the integer. Define SWAPPED to make
  852. X * things behave in this manner.
  853. X *
  854. X * Author:    Mark G. Mendel, 7/86
  855. X *        UUCP: ihnp4!umn-cs!hyper!mark, GEnie: mgm
  856. X */
  857. X
  858. X/* The CRC polynomial.
  859. X * These 4 values define the crc-polynomial.
  860. X * If you change them, you must change crctab[]'s initial value to what is
  861. X * printed by initcrctab() [see 'compile with -DMAKETAB' above].
  862. X */
  863. X    /* Value used by:                CITT    XMODEM    ARC      */
  864. X#define    P     0xA001     /* the poly:    0x1021    0x1021    A001    */
  865. X#define INIT_CRC 0L     /* init value:    -1    0    0    */
  866. X#define SWAPPED         /* bit order:    undef    defined    defined */
  867. X#define W    16     /* bits in CRC:16    16    16    */
  868. X
  869. X    /* data type that holds a W-bit unsigned integer */
  870. X#if W <= 16
  871. X#  define WTYPE    unsigned short
  872. X#else
  873. X#  define WTYPE   unsigned long
  874. X#endif
  875. X
  876. X    /* the number of bits per char: don't change it. */
  877. X#define B    8
  878. X
  879. Xstatic WTYPE crctab[1<<B] = /* as calculated by initcrctab() */ {
  880. X0x0,  0xc0c1,  0xc181,  0x140,  0xc301,  0x3c0,  0x280,  0xc241,
  881. X0xc601,  0x6c0,  0x780,  0xc741,  0x500,  0xc5c1,  0xc481,  0x440,
  882. X0xcc01,  0xcc0,  0xd80,  0xcd41,  0xf00,  0xcfc1,  0xce81,  0xe40,
  883. X0xa00,  0xcac1,  0xcb81,  0xb40,  0xc901,  0x9c0,  0x880,  0xc841,
  884. X0xd801,  0x18c0,  0x1980,  0xd941,  0x1b00,  0xdbc1,  0xda81,  0x1a40,
  885. X0x1e00,  0xdec1,  0xdf81,  0x1f40,  0xdd01,  0x1dc0,  0x1c80,  0xdc41,
  886. X0x1400,  0xd4c1,  0xd581,  0x1540,  0xd701,  0x17c0,  0x1680,  0xd641,
  887. X0xd201,  0x12c0,  0x1380,  0xd341,  0x1100,  0xd1c1,  0xd081,  0x1040,
  888. X0xf001,  0x30c0,  0x3180,  0xf141,  0x3300,  0xf3c1,  0xf281,  0x3240,
  889. X0x3600,  0xf6c1,  0xf781,  0x3740,  0xf501,  0x35c0,  0x3480,  0xf441,
  890. X0x3c00,  0xfcc1,  0xfd81,  0x3d40,  0xff01,  0x3fc0,  0x3e80,  0xfe41,
  891. X0xfa01,  0x3ac0,  0x3b80,  0xfb41,  0x3900,  0xf9c1,  0xf881,  0x3840,
  892. X0x2800,  0xe8c1,  0xe981,  0x2940,  0xeb01,  0x2bc0,  0x2a80,  0xea41,
  893. X0xee01,  0x2ec0,  0x2f80,  0xef41,  0x2d00,  0xedc1,  0xec81,  0x2c40,
  894. X0xe401,  0x24c0,  0x2580,  0xe541,  0x2700,  0xe7c1,  0xe681,  0x2640,
  895. X0x2200,  0xe2c1,  0xe381,  0x2340,  0xe101,  0x21c0,  0x2080,  0xe041,
  896. X0xa001,  0x60c0,  0x6180,  0xa141,  0x6300,  0xa3c1,  0xa281,  0x6240,
  897. X0x6600,  0xa6c1,  0xa781,  0x6740,  0xa501,  0x65c0,  0x6480,  0xa441,
  898. X0x6c00,  0xacc1,  0xad81,  0x6d40,  0xaf01,  0x6fc0,  0x6e80,  0xae41,
  899. X0xaa01,  0x6ac0,  0x6b80,  0xab41,  0x6900,  0xa9c1,  0xa881,  0x6840,
  900. X0x7800,  0xb8c1,  0xb981,  0x7940,  0xbb01,  0x7bc0,  0x7a80,  0xba41,
  901. X0xbe01,  0x7ec0,  0x7f80,  0xbf41,  0x7d00,  0xbdc1,  0xbc81,  0x7c40,
  902. X0xb401,  0x74c0,  0x7580,  0xb541,  0x7700,  0xb7c1,  0xb681,  0x7640,
  903. X0x7200,  0xb2c1,  0xb381,  0x7340,  0xb101,  0x71c0,  0x7080,  0xb041,
  904. X0x5000,  0x90c1,  0x9181,  0x5140,  0x9301,  0x53c0,  0x5280,  0x9241,
  905. X0x9601,  0x56c0,  0x5780,  0x9741,  0x5500,  0x95c1,  0x9481,  0x5440,
  906. X0x9c01,  0x5cc0,  0x5d80,  0x9d41,  0x5f00,  0x9fc1,  0x9e81,  0x5e40,
  907. X0x5a00,  0x9ac1,  0x9b81,  0x5b40,  0x9901,  0x59c0,  0x5880,  0x9841,
  908. X0x8801,  0x48c0,  0x4980,  0x8941,  0x4b00,  0x8bc1,  0x8a81,  0x4a40,
  909. X0x4e00,  0x8ec1,  0x8f81,  0x4f40,  0x8d01,  0x4dc0,  0x4c80,  0x8c41,
  910. X0x4400,  0x84c1,  0x8581,  0x4540,  0x8701,  0x47c0,  0x4680,  0x8641,
  911. X0x8201,  0x42c0,  0x4380,  0x8341,  0x4100,  0x81c1,  0x8081,  0x4040,
  912. X} ;
  913. X
  914. XWTYPE
  915. Xupdcrc( icrc, icp, icnt )
  916. X    WTYPE icrc;
  917. X    unsigned char *icp;
  918. X    int icnt;
  919. X{
  920. X    register WTYPE crc = icrc;
  921. X    register unsigned char *cp = icp;
  922. X    register int cnt = icnt;
  923. X
  924. X    while( cnt-- ) {
  925. X#ifndef SWAPPED
  926. X    crc = (crc<<B) ^ crctab[(crc>>(W-B)) ^ *cp++];
  927. X#else
  928. X    crc = (crc>>B) ^ crctab[(crc & ((1<<B)-1)) ^ *cp++]; 
  929. X#endif SWAPPED
  930. X    }
  931. X
  932. X    return( crc );
  933. X}
  934. X
  935. X#ifdef MAKETAB
  936. X
  937. X#include <stdio.h>
  938. Xmain()
  939. X{
  940. X    initcrctab();
  941. X}
  942. X
  943. Xinitcrctab()
  944. X{
  945. X    register  int b, i;
  946. X    WTYPE v;
  947. X
  948. X    
  949. X    for( b = 0; b <= (1<<B)-1; ++b ) {
  950. X#ifndef SWAPPED
  951. X    for( v = b<<(W-B), i = B; --i >= 0; )
  952. X        v = v & ((WTYPE)1<<(W-1)) ? (v<<1)^P : v<<1;
  953. X#else
  954. X    for( v = b, i = B; --i >= 0; )
  955. X        v = v & 1 ? (v>>1)^P : v>>1;
  956. X#endif        
  957. X    crctab[b] = v;
  958. X
  959. X    printf( "0x%lx,", v & ((1L<<W)-1L));
  960. X    if( (b&7) == 7 )
  961. X        printf("\n" );
  962. X    else
  963. X        printf("  ");
  964. X    }
  965. X}
  966. X#endif
  967. X
  968. X#ifdef TEST
  969. X
  970. X#include <stdio.h>
  971. X#include <fcntl.h>
  972. X
  973. X#define MAXBUF    4096
  974. X
  975. X
  976. X
  977. Xmain( ac, av )
  978. X    int ac; char **av;
  979. X{
  980. X    int fd;
  981. X    int nr;
  982. X    int i;
  983. X    char buf[MAXBUF];
  984. X    WTYPE crc, crc2;
  985. X
  986. X    fd = 0;
  987. X    if( ac > 1 )
  988. X    if( (fd = open( av[1], O_RDONLY )) < 0 ) {
  989. X        perror( av[1] );
  990. X        exit( -1 );
  991. X    }
  992. X    crc = crc2 = INIT_CRC;
  993. X
  994. X    while( (nr = read( fd, buf, MAXBUF )) > 0 ) {
  995. X    crc = updcrc( crc, buf, nr );
  996. X    }
  997. X
  998. X    if( nr != 0 )
  999. X    perror( "reading" );
  1000. X    else {
  1001. X    printf( "%lx\n", crc );
  1002. X    }
  1003. X
  1004. X#ifdef MAGICCHECK
  1005. X    /* tack one's complement of crc onto data stream, and
  1006. X       continue crc calculation.  Should get a constant (magic number)
  1007. X       dependent only on P, not the data.
  1008. X     */
  1009. X    crc2 = crc ^ -1L;
  1010. X    for( nr = W-B; nr >= 0; nr -= B ) {
  1011. X    buf[0] = (crc2 >> nr);
  1012. X    crc = updcrc(crc, buf, 1);
  1013. X    }
  1014. X
  1015. X    /* crc should now equal magic */
  1016. X    buf[0] = buf[1] = buf[2] = buf[3] = 0;
  1017. X    printf( "magic test: %lx =?= %lx\n", crc, updcrc(-1, buf, W/B));
  1018. X#endif MAGICCHECK
  1019. X}
  1020. X
  1021. X#endif
  1022. END_OF_FILE
  1023. if test 5848 -ne `wc -c <'stuffit/updcrc.c'`; then
  1024.     echo shar: \"'stuffit/updcrc.c'\" unpacked with wrong size!
  1025. fi
  1026. # end of 'stuffit/updcrc.c'
  1027. fi
  1028. if test -f 'unstuffit/updcrc.c' -a "${1}" != "-c" ; then 
  1029.   echo shar: Will not clobber existing file \"'unstuffit/updcrc.c'\"
  1030. else
  1031. echo shar: Extracting \"'unstuffit/updcrc.c'\" \(5848 characters\)
  1032. sed "s/^X//" >'unstuffit/updcrc.c' <<'END_OF_FILE'
  1033. X/* updcrc(3), crc(1) - calculate crc polynomials
  1034. X *
  1035. X * Calculate, intelligently, the CRC of a dataset incrementally given a 
  1036. X * buffer full at a time.
  1037. X * 
  1038. X * Usage:
  1039. X *     newcrc = updcrc( oldcrc, bufadr, buflen )
  1040. X *         unsigned int oldcrc, buflen;
  1041. X *         char *bufadr;
  1042. X *
  1043. X * Compiling with -DTEST creates a program to print the CRC of stdin to stdout.
  1044. X * Compile with -DMAKETAB to print values for crctab to stdout.  If you change
  1045. X *    the CRC polynomial parameters, be sure to do this and change
  1046. X *    crctab's initial value.
  1047. X *
  1048. X * Notes:
  1049. X *  Regards the data stream as an integer whose MSB is the MSB of the first
  1050. X *  byte recieved.  This number is 'divided' (using xor instead of subtraction)
  1051. X *  by the crc-polynomial P.
  1052. X *  XMODEM does things a little differently, essentially treating the LSB of
  1053. X * the first data byte as the MSB of the integer. Define SWAPPED to make
  1054. X * things behave in this manner.
  1055. X *
  1056. X * Author:    Mark G. Mendel, 7/86
  1057. X *        UUCP: ihnp4!umn-cs!hyper!mark, GEnie: mgm
  1058. X */
  1059. X
  1060. X/* The CRC polynomial.
  1061. X * These 4 values define the crc-polynomial.
  1062. X * If you change them, you must change crctab[]'s initial value to what is
  1063. X * printed by initcrctab() [see 'compile with -DMAKETAB' above].
  1064. X */
  1065. X    /* Value used by:                CITT    XMODEM    ARC      */
  1066. X#define    P     0xA001     /* the poly:    0x1021    0x1021    A001    */
  1067. X#define INIT_CRC 0L     /* init value:    -1    0    0    */
  1068. X#define SWAPPED         /* bit order:    undef    defined    defined */
  1069. X#define W    16     /* bits in CRC:16    16    16    */
  1070. X
  1071. X    /* data type that holds a W-bit unsigned integer */
  1072. X#if W <= 16
  1073. X#  define WTYPE    unsigned short
  1074. X#else
  1075. X#  define WTYPE   unsigned long
  1076. X#endif
  1077. X
  1078. X    /* the number of bits per char: don't change it. */
  1079. X#define B    8
  1080. X
  1081. Xstatic WTYPE crctab[1<<B] = /* as calculated by initcrctab() */ {
  1082. X0x0,  0xc0c1,  0xc181,  0x140,  0xc301,  0x3c0,  0x280,  0xc241,
  1083. X0xc601,  0x6c0,  0x780,  0xc741,  0x500,  0xc5c1,  0xc481,  0x440,
  1084. X0xcc01,  0xcc0,  0xd80,  0xcd41,  0xf00,  0xcfc1,  0xce81,  0xe40,
  1085. X0xa00,  0xcac1,  0xcb81,  0xb40,  0xc901,  0x9c0,  0x880,  0xc841,
  1086. X0xd801,  0x18c0,  0x1980,  0xd941,  0x1b00,  0xdbc1,  0xda81,  0x1a40,
  1087. X0x1e00,  0xdec1,  0xdf81,  0x1f40,  0xdd01,  0x1dc0,  0x1c80,  0xdc41,
  1088. X0x1400,  0xd4c1,  0xd581,  0x1540,  0xd701,  0x17c0,  0x1680,  0xd641,
  1089. X0xd201,  0x12c0,  0x1380,  0xd341,  0x1100,  0xd1c1,  0xd081,  0x1040,
  1090. X0xf001,  0x30c0,  0x3180,  0xf141,  0x3300,  0xf3c1,  0xf281,  0x3240,
  1091. X0x3600,  0xf6c1,  0xf781,  0x3740,  0xf501,  0x35c0,  0x3480,  0xf441,
  1092. X0x3c00,  0xfcc1,  0xfd81,  0x3d40,  0xff01,  0x3fc0,  0x3e80,  0xfe41,
  1093. X0xfa01,  0x3ac0,  0x3b80,  0xfb41,  0x3900,  0xf9c1,  0xf881,  0x3840,
  1094. X0x2800,  0xe8c1,  0xe981,  0x2940,  0xeb01,  0x2bc0,  0x2a80,  0xea41,
  1095. X0xee01,  0x2ec0,  0x2f80,  0xef41,  0x2d00,  0xedc1,  0xec81,  0x2c40,
  1096. X0xe401,  0x24c0,  0x2580,  0xe541,  0x2700,  0xe7c1,  0xe681,  0x2640,
  1097. X0x2200,  0xe2c1,  0xe381,  0x2340,  0xe101,  0x21c0,  0x2080,  0xe041,
  1098. X0xa001,  0x60c0,  0x6180,  0xa141,  0x6300,  0xa3c1,  0xa281,  0x6240,
  1099. X0x6600,  0xa6c1,  0xa781,  0x6740,  0xa501,  0x65c0,  0x6480,  0xa441,
  1100. X0x6c00,  0xacc1,  0xad81,  0x6d40,  0xaf01,  0x6fc0,  0x6e80,  0xae41,
  1101. X0xaa01,  0x6ac0,  0x6b80,  0xab41,  0x6900,  0xa9c1,  0xa881,  0x6840,
  1102. X0x7800,  0xb8c1,  0xb981,  0x7940,  0xbb01,  0x7bc0,  0x7a80,  0xba41,
  1103. X0xbe01,  0x7ec0,  0x7f80,  0xbf41,  0x7d00,  0xbdc1,  0xbc81,  0x7c40,
  1104. X0xb401,  0x74c0,  0x7580,  0xb541,  0x7700,  0xb7c1,  0xb681,  0x7640,
  1105. X0x7200,  0xb2c1,  0xb381,  0x7340,  0xb101,  0x71c0,  0x7080,  0xb041,
  1106. X0x5000,  0x90c1,  0x9181,  0x5140,  0x9301,  0x53c0,  0x5280,  0x9241,
  1107. X0x9601,  0x56c0,  0x5780,  0x9741,  0x5500,  0x95c1,  0x9481,  0x5440,
  1108. X0x9c01,  0x5cc0,  0x5d80,  0x9d41,  0x5f00,  0x9fc1,  0x9e81,  0x5e40,
  1109. X0x5a00,  0x9ac1,  0x9b81,  0x5b40,  0x9901,  0x59c0,  0x5880,  0x9841,
  1110. X0x8801,  0x48c0,  0x4980,  0x8941,  0x4b00,  0x8bc1,  0x8a81,  0x4a40,
  1111. X0x4e00,  0x8ec1,  0x8f81,  0x4f40,  0x8d01,  0x4dc0,  0x4c80,  0x8c41,
  1112. X0x4400,  0x84c1,  0x8581,  0x4540,  0x8701,  0x47c0,  0x4680,  0x8641,
  1113. X0x8201,  0x42c0,  0x4380,  0x8341,  0x4100,  0x81c1,  0x8081,  0x4040,
  1114. X} ;
  1115. X
  1116. XWTYPE
  1117. Xupdcrc( icrc, icp, icnt )
  1118. X    WTYPE icrc;
  1119. X    unsigned char *icp;
  1120. X    int icnt;
  1121. X{
  1122. X    register WTYPE crc = icrc;
  1123. X    register unsigned char *cp = icp;
  1124. X    register int cnt = icnt;
  1125. X
  1126. X    while( cnt-- ) {
  1127. X#ifndef SWAPPED
  1128. X    crc = (crc<<B) ^ crctab[(crc>>(W-B)) ^ *cp++];
  1129. X#else
  1130. X    crc = (crc>>B) ^ crctab[(crc & ((1<<B)-1)) ^ *cp++]; 
  1131. X#endif SWAPPED
  1132. X    }
  1133. X
  1134. X    return( crc );
  1135. X}
  1136. X
  1137. X#ifdef MAKETAB
  1138. X
  1139. X#include <stdio.h>
  1140. Xmain()
  1141. X{
  1142. X    initcrctab();
  1143. X}
  1144. X
  1145. Xinitcrctab()
  1146. X{
  1147. X    register  int b, i;
  1148. X    WTYPE v;
  1149. X
  1150. X    
  1151. X    for( b = 0; b <= (1<<B)-1; ++b ) {
  1152. X#ifndef SWAPPED
  1153. X    for( v = b<<(W-B), i = B; --i >= 0; )
  1154. X        v = v & ((WTYPE)1<<(W-1)) ? (v<<1)^P : v<<1;
  1155. X#else
  1156. X    for( v = b, i = B; --i >= 0; )
  1157. X        v = v & 1 ? (v>>1)^P : v>>1;
  1158. X#endif        
  1159. X    crctab[b] = v;
  1160. X
  1161. X    printf( "0x%lx,", v & ((1L<<W)-1L));
  1162. X    if( (b&7) == 7 )
  1163. X        printf("\n" );
  1164. X    else
  1165. X        printf("  ");
  1166. X    }
  1167. X}
  1168. X#endif
  1169. X
  1170. X#ifdef TEST
  1171. X
  1172. X#include <stdio.h>
  1173. X#include <fcntl.h>
  1174. X
  1175. X#define MAXBUF    4096
  1176. X
  1177. X
  1178. X
  1179. Xmain( ac, av )
  1180. X    int ac; char **av;
  1181. X{
  1182. X    int fd;
  1183. X    int nr;
  1184. X    int i;
  1185. X    char buf[MAXBUF];
  1186. X    WTYPE crc, crc2;
  1187. X
  1188. X    fd = 0;
  1189. X    if( ac > 1 )
  1190. X    if( (fd = open( av[1], O_RDONLY )) < 0 ) {
  1191. X        perror( av[1] );
  1192. X        exit( -1 );
  1193. X    }
  1194. X    crc = crc2 = INIT_CRC;
  1195. X
  1196. X    while( (nr = read( fd, buf, MAXBUF )) > 0 ) {
  1197. X    crc = updcrc( crc, buf, nr );
  1198. X    }
  1199. X
  1200. X    if( nr != 0 )
  1201. X    perror( "reading" );
  1202. X    else {
  1203. X    printf( "%lx\n", crc );
  1204. X    }
  1205. X
  1206. X#ifdef MAGICCHECK
  1207. X    /* tack one's complement of crc onto data stream, and
  1208. X       continue crc calculation.  Should get a constant (magic number)
  1209. X       dependent only on P, not the data.
  1210. X     */
  1211. X    crc2 = crc ^ -1L;
  1212. X    for( nr = W-B; nr >= 0; nr -= B ) {
  1213. X    buf[0] = (crc2 >> nr);
  1214. X    crc = updcrc(crc, buf, 1);
  1215. X    }
  1216. X
  1217. X    /* crc should now equal magic */
  1218. X    buf[0] = buf[1] = buf[2] = buf[3] = 0;
  1219. X    printf( "magic test: %lx =?= %lx\n", crc, updcrc(-1, buf, W/B));
  1220. X#endif MAGICCHECK
  1221. X}
  1222. X
  1223. X#endif
  1224. END_OF_FILE
  1225. if test 5848 -ne `wc -c <'unstuffit/updcrc.c'`; then
  1226.     echo shar: \"'unstuffit/updcrc.c'\" unpacked with wrong size!
  1227. fi
  1228. # end of 'unstuffit/updcrc.c'
  1229. fi
  1230. echo shar: End of archive 2 \(of 4\).
  1231. cp /dev/null ark2isdone
  1232. MISSING=""
  1233. for I in 1 2 3 4 ; do
  1234.     if test ! -f ark${I}isdone ; then
  1235.     MISSING="${MISSING} ${I}"
  1236.     fi
  1237. done
  1238. if test "${MISSING}" = "" ; then
  1239.     echo You have unpacked all 4 archives.
  1240.     rm -f ark[1-9]isdone
  1241. else
  1242.     echo You still need to unpack the following archives:
  1243.     echo "        " ${MISSING}
  1244. fi
  1245. ##  End of shell archive.
  1246. exit 0
  1247. --- end of part 2 ---
  1248.  
  1249.  
  1250.