home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / GNUNARJ.ZIP / UNARJ.C < prev    next >
Text File  |  1992-06-09  |  26KB  |  1,109 lines

  1. /* UNARJ.C, UNARJ, R JUNG, 12/03/91
  2.  * Main Extractor routine
  3.  * Copyright (c) 1991 by Robert K Jung.  All rights reserved.
  4.  *
  5.  *   This code may be freely used in programs that are NOT ARJ archivers
  6.  *   (both compress and extract ARJ archives).
  7.  *
  8.  *   If you wish to distribute a modified version of this program, you
  9.  *   MUST indicate that it is a modified version both in the program and
  10.  *   source code.
  11.  *
  12.  *   If you modify this program, I would appreciate a copy of the new
  13.  *   source code.  I am holding the copyright on the source code, so
  14.  *   please do not delete my name from the program files or from the
  15.  *   documentation.
  16.  *
  17.  *   I wish to give credit to Haruhiko Okumura for providing the
  18.  *   basic ideas for ARJ and UNARJ in his program AR.  Please note
  19.  *   that UNARJ is significantly different from AR from an archive
  20.  *   structural point of view.
  21.  *
  22.  * Modification history:
  23.  * Date      Programmer  Description of modification.
  24.  * 04/05/91  R. Jung     Rewrote code.
  25.  * 04/23/91  M. Adler    Portabilized.
  26.  * 04/29/91  R. Jung     Added l command.  Removed 16 bit dependency in
  27.  *                       fillbuf().
  28.  * 05/19/91  R. Jung     Fixed extended header skipping code.
  29.  * 05/25/91  R. Jung     Improved find_header().
  30.  * 06/03/91  R. Jung     Changed arguments in get_mode_str() and
  31.  *                       set_ftime_mode().
  32.  * 06/19/81  R. Jung     Added two more %c in printf() in list_arc().
  33.  * 07/07/91  R. Jung     Added default_case_path() to extract().
  34.  *                       Added strlower().
  35.  * 07/20/91  R. Jung     Changed uint ratio() to static uint ratio().
  36.  * 07/21/91  R. Jung     Added #ifdef VMS.
  37.  * 08/28/91  R. Jung     Changed M_DIFFHOST message.
  38.  * 08/31/91  R. Jung     Added changes to support MAC THINK_C compiler
  39.  *                       per Eric Larson.
  40.  * 10/07/91  R. Jung     Added missing ; to THINK_C additions.
  41.  * 11/11/91  R. Jung     Added host_os test to fwrite_txt_crc().
  42.  * 11/24/91  R. Jung     Added more error_count processing.
  43.  * 12/03/91  R. Jung     Added backup file processing.
  44.  *
  45.  * 12/28/91  S. Dudley   Added support for filename lists
  46.  *
  47.  * 06/06/92 Jack Grey    Altered generic set_ftime_mode in ENVIRON.C to use
  48.  *                       FILESTATUS4 structure and removed DosSetFileMode
  49.  *                       call; added setvbuf calls to file_open routine.
  50.  *             Altered main for .ARJ file extension completion
  51.  *             and handling of filename lists
  52.  */
  53.  
  54. #include "unarj.h"
  55.  
  56. #ifdef MODERN
  57. #include <stdlib.h>
  58. #include <string.h>
  59. #include <ctype.h>
  60. /* 06/06/92 Jack Grey substituted these definitions for toupper and 
  61.     to lower for those appearing in GNU ctypes.h 
  62. */
  63. #define toupper(c)   ((c)>='a'&&(c)<='z'?(c)-('a'-'A'):(c))
  64. #define tolower(c)   ((c)>='A'&&(c)<='Z'?(c)+('a'-'A'):(c))
  65. #define max(a,b)     ((a)>(b)?(a):(b))
  66. #define min(a,b)     ((a)<(b)?(a):(b))
  67.  
  68. #else /* !MODERN */
  69. extern void free();
  70. extern void exit();
  71. extern char *strcat();
  72. extern char *strcpy();
  73. extern char *strncpy();
  74. extern char *strchr();
  75. extern char *strrchr();
  76. extern int strlen();
  77. extern int strcmp();
  78. #ifdef VMS
  79. #include <ssdef.h>
  80. #define EXIT_FAILURE SS$_ABORT
  81. #define EXIT_SUCCESS SS$_NORMAL
  82. #else
  83. #define EXIT_FAILURE (1)
  84. #define EXIT_SUCCESS (0)
  85. #endif
  86. #define toupper(c)   ((c)>='a'&&(c)<='z'?(c)-('a'-'A'):(c))
  87. #define tolower(c)   ((c)>='A'&&(c)<='Z'?(c)+('a'-'A'):(c))
  88. #endif /* ?MODERN */
  89.  
  90. #ifdef THINK_C
  91. #include <console.h>
  92. #endif
  93.  
  94. /* Global variables */
  95.  
  96. UCRC   crc;
  97. FILE   *arcfile;
  98. FILE   *outfile;
  99. ushort bitbuf;
  100. long   compsize;
  101. long   origsize;
  102. uchar  subbitbuf;
  103. uchar  header[HEADERSIZE_MAX];
  104. char   arc_name[FNAME_MAX];
  105. int    command;
  106. int    bitcount;
  107. int    file_type;
  108. int    no_output;
  109. int    error_count;
  110.  
  111. /* Messages */
  112.  
  113. static char *M_USAGE  [] =
  114. {
  115. "Usage:  UNARJ archive[.arj]                 (list archive)\n",
  116. "        UNARJ e archive     [filespec(s)]   (extract archive)\n",
  117. "        UNARJ l archive     [filespec(s)]   (list archive)\n",
  118. "        UNARJ t archive     [filespec(s)]   (test archive)\n",
  119. "        UNARJ x archive     [filespec(s)]   (extract with pathnames)\n",
  120. "\n",
  121. "This is an ARJ demonstration program and ** IS NOT OPTIMIZED ** for speed.\n",
  122. "You may freely use, copy and distribute this program, provided that no fee\n",
  123. "is charged for such use, copying or distribution, and it is distributed\n",
  124. "ONLY in its original unmodified state.  UNARJ is provided as is without\n",
  125. "warranty of any kind, express or implied, including but not limited to\n",
  126. "the implied warranties of merchantability and fitness for a particular\n",
  127. "purpose.  Refer to UNARJ.DOC for more warranty information.  If you find\n",
  128. "UNARJ of value, a gift of $10 or any amount would greatly appreciated.\n",
  129. "\n",
  130. "Robert K Jung                   Internet address :  robjung@world.std.com\n",
  131. "2606 Village Road West          CompuServe userid:  72077,445\n",
  132. "Norwood, Massachusetts 02062\n",
  133. "USA\n",
  134. NULL
  135. };
  136.  
  137. char M_VERSION [] = "UNARJ (Demo version) 2.30 Copyright (c) 1991 Robert K Jung\n\n";
  138.  
  139. char M_ARCDATE [] = "Archive date      : %s\n";
  140. char M_BADCOMND[] = "Bad UNARJ command: %s";
  141. char M_BADCOMNT[] = "Invalid comment header";
  142. char M_BADHEADR[] = "Bad header";
  143. char M_BADTABLE[] = "Bad Huffman code";
  144. char M_CANTOPEN[] = "Can't open %s";
  145. char M_CANTREAD[] = "Can't read file or unexpected end of file";
  146. char M_CANTWRIT[] = "Can't write file. Disk full?";
  147. char M_CRCERROR[] = "CRC error!\n";
  148. char M_CRCOK   [] = "CRC OK\n";
  149. char M_DIFFHOST[] = "  Binary file!";
  150. char M_ENCRYPT [] = "File is password encrypted, ";
  151. char M_ERRORCNT[] = "%sFound %5d error(s)!";
  152. char M_EXTRACT [] = "Extracting %-25s";
  153. char M_FEXISTS [] = "%-25s exists, ";
  154. char M_HEADRCRC[] = "Header CRC error!";
  155. char M_NBRFILES[] = "%5d file(s)\n";
  156. char M_NOMEMORY[] = "Out of memory";
  157. char M_NOTARJ  [] = "%s is not an ARJ archive";
  158. char M_PROCARC [] = "Processing archive: %s\n";
  159. char M_SKIPPED [] = "Skipped %s\n";
  160. char M_SUFFIX  [] = ARJ_SUFFIX;
  161. char M_TESTING [] = "Testing    %-25s";
  162. char M_UNKNMETH[] = "Unsupported method: %d, ";
  163. char M_UNKNTYPE[] = "Unsupported file type: %d, ";
  164. char M_UNKNVERS[] = "Unsupported version: %d, ";
  165.  
  166. #define get_crc()       get_longword()
  167. #define fget_crc(f)     fget_longword(f)
  168.  
  169. #define setup_get(PTR)  (get_ptr = (PTR))
  170. #define get_byte()      ((uchar)(*get_ptr++ & 0xff))
  171.  
  172. #define BUFFERSIZE      4096
  173.  
  174. #define ASCII_MASK      0x7F
  175.  
  176. #define CRCPOLY         0xEDB88320L
  177.  
  178. #define UPDATE_CRC(r,c) r=crctable[((uchar)(r)^(uchar)(c))&0xff]^(r>>CHAR_BIT)
  179.  
  180. /* Local functions */
  181.  
  182. #ifdef MODERN
  183. static void  make_crctable(void);
  184. static void  crc_buf(char *str, int len);
  185. static void  strparity(uchar *p);
  186. static FILE  *fopen_msg(char *name, char *mode);
  187. static int   fget_byte(FILE *f);
  188. static uint  fget_word(FILE *f);
  189. static ulong fget_longword(FILE *f);
  190. static void  fread_crc(uchar *p, int n, FILE *f);
  191. static void  decode_path(char *name);
  192. static void  get_date_str(char *str, ulong tstamp);
  193. static int   parse_path(char *pathname, char *path, char *entry);
  194. static void  strncopy(char *to, char *from, int len);
  195. static uint  get_word(void);
  196. static ulong get_longword(void);
  197. static long  find_header(FILE *fd);
  198. static int   read_header(int first, FILE *fd, char *name);
  199. static void  skip(void);
  200. static void  unstore(void);
  201. static int   check_flags(void);
  202. static int   extract(void);
  203. static int   test(void);
  204. static uint  ratio(long a, long b);
  205. static void  list_start(void);
  206. static void  list_arc(int count);
  207. static void  help(void);
  208.  
  209. static void  execute_cmd(char **list); /*SJD Sat  12-28-1991  14:26:53 */
  210. static int MatchWC(char *pat, char *fn); /*SJD Sat  12-28-1991  14:35:57 */
  211.  
  212. #endif /* MODERN */
  213.  
  214. /* Local variables */
  215.  
  216. static char   filename[FNAME_MAX];
  217. static char   comment[COMMENT_MAX];
  218. static char   *hdr_filename;
  219. static char   *hdr_comment;
  220.  
  221. static ushort headersize;
  222. static uchar  first_hdr_size;
  223. static uchar  arj_nbr;
  224. static uchar  arj_x_nbr;
  225. static uchar  host_os;
  226. static uchar  arj_flags;
  227. static short  method;
  228. static uint   file_mode;
  229. static ulong  time_stamp;
  230. static short  entry_pos;
  231. static ushort host_data;
  232. static uchar  *get_ptr;
  233. static UCRC   file_crc;
  234. static UCRC   header_crc;
  235.  
  236. static long   first_hdr_pos;
  237. static long   torigsize;
  238. static long   tcompsize;
  239.  
  240. static int    clock_inx;
  241.  
  242. static char   *writemode[2]  = { "wb",  "w" };
  243.  
  244. static UCRC   crctable[UCHAR_MAX + 1];
  245.  
  246. /* Functions */
  247.  
  248. static void
  249. make_crctable()
  250. {
  251.     uint i, j;
  252.     UCRC r;
  253.  
  254.     for (i = 0; i <= UCHAR_MAX; i++)
  255.     {
  256.         r = i;
  257.         for (j = CHAR_BIT; j > 0; j--)
  258.         {
  259.             if (r & 1)
  260.                 r = (r >> 1) ^ CRCPOLY;
  261.             else
  262.                 r >>= 1;
  263.         }
  264.         crctable[i] = r;
  265.     }
  266. }
  267.  
  268. static void
  269. crc_buf(str, len)
  270. char *str;
  271. int  len;
  272. {
  273.     while (len--)
  274.         UPDATE_CRC(crc, *str++);
  275. }
  276.  
  277. void
  278. disp_clock()
  279. {
  280.     static char clock_str[4] = { '|', '/', '-', '\\' };
  281.  
  282.     printf("(%c)\b\b\b", clock_str[clock_inx]);
  283.     clock_inx = (clock_inx + 1) & 0x03;
  284. }
  285.  
  286. void
  287. error(fmt, arg)
  288. char *fmt;
  289. char *arg;
  290. {
  291.     putc('\n', stdout);
  292.     printf(fmt, arg, error_count);
  293.     putc('\n', stdout);
  294.     exit(EXIT_FAILURE);
  295. }
  296.  
  297. static void
  298. strparity(p)
  299. uchar *p;
  300. {
  301.     while (*p)
  302.     {
  303.         FIX_PARITY(*p);
  304.         p++;
  305.     }
  306. }
  307.  
  308. static FILE *
  309. fopen_msg(name, mode)
  310. char *name;
  311. char *mode;
  312. {
  313.     FILE *fd;
  314.  
  315.     fd = file_open(name, mode);
  316.     if (fd == NULL)
  317.         error(M_CANTOPEN, name);
  318.     return fd;
  319. }
  320.  
  321. static int
  322. fget_byte(f)
  323. FILE *f;
  324. {
  325.     int c;
  326.  
  327.     if ((c = getc(f)) == EOF)
  328.         error(M_CANTREAD, "");
  329.     return c & 0xFF;
  330. }
  331.  
  332. static uint
  333. fget_word(f)
  334. FILE *f;
  335. {
  336.     uint b0, b1;
  337.  
  338.     b0 = fget_byte(f);
  339.     b1 = fget_byte(f);
  340.     return (b1 << 8) + b0;
  341. }
  342.  
  343. static ulong
  344. fget_longword(f)
  345. FILE *f;
  346. {
  347.     ulong b0, b1, b2, b3;
  348.  
  349.     b0 = fget_byte(f);
  350.     b1 = fget_byte(f);
  351.     b2 = fget_byte(f);
  352.     b3 = fget_byte(f);
  353.     return (b3 << 24) + (b2 << 16) + (b1 << 8) + b0;
  354. }
  355.  
  356. static void
  357. fread_crc(p, n, f)
  358. uchar *p;
  359. int   n;
  360. FILE  *f;
  361. {
  362.     n = file_read((char *)p, 1, n, f);
  363.     origsize += n;
  364.     crc_buf((char *)p, n);
  365. }
  366.  
  367. void
  368. fwrite_txt_crc(p, n)
  369. uchar *p;
  370. int   n;
  371. {
  372.     uchar c;
  373.  
  374.     crc_buf((char *)p, n);
  375.     if (no_output)
  376.         return;
  377.  
  378.     if (file_type == TEXT_TYPE)
  379.     {
  380.         while (n--)
  381.         {
  382.             c = *p++;
  383.             if (host_os != OS)
  384.             {
  385.                 FIX_PARITY(c);
  386.             }
  387.             if (putc((int) c, outfile) == EOF)
  388.                 error(M_CANTWRIT, "");
  389.         }
  390.     }
  391.     else
  392.     {
  393.         if (file_write((char *)p, 1, n, outfile) != n)
  394.             error(M_CANTWRIT, "");
  395.     }
  396. }
  397.  
  398. void
  399. init_getbits()
  400. {
  401.     bitbuf = 0;
  402.     subbitbuf = 0;
  403.     bitcount = 0;
  404.     fillbuf(2 * CHAR_BIT);
  405. }
  406.  
  407. void
  408. fillbuf(n)                /* Shift bitbuf n bits left, read n bits */
  409. int n;
  410. {
  411.     bitbuf = (bitbuf << n) & 0xFFFF;  /* lose the first n bits */
  412.     while (n > bitcount)
  413.     {
  414.         bitbuf |= subbitbuf << (n -= bitcount);
  415.         if (compsize != 0)
  416.         {
  417.             compsize--;
  418.             subbitbuf = (uchar) getc(arcfile);
  419.         }
  420.         else
  421.             subbitbuf = 0;
  422.         bitcount = CHAR_BIT;
  423.     }
  424.     bitbuf |= subbitbuf >> (bitcount -= n);
  425. }
  426.  
  427. ushort
  428. getbits(n)
  429. int n;
  430. {
  431.     ushort x;
  432.  
  433.     x = bitbuf >> (2 * CHAR_BIT - n);
  434.     fillbuf(n);
  435.     return x;
  436. }
  437.  
  438. static void
  439. decode_path(name)
  440. char *name;
  441. {
  442.     for ( ; *name; name++)
  443.     {
  444.         if (*name == ARJ_PATH_CHAR)
  445.             *name = PATH_CHAR;
  446.     }
  447. }
  448.  
  449. static void
  450. get_date_str(str, tstamp)
  451. char  *str;
  452. ulong tstamp;
  453. {
  454.     sprintf(str, "%04u-%02u-%02u %02u:%02u:%02u",
  455.            ts_year(tstamp), ts_month(tstamp), ts_day(tstamp),
  456.            ts_hour(tstamp), ts_min(tstamp), ts_sec(tstamp));
  457. }
  458.  
  459. static int
  460. parse_path(pathname, path, entry)
  461. char *pathname;
  462. char *path;
  463. char *entry;
  464. {
  465.     char *cptr, *ptr, *fptr;
  466.     short pos;
  467.  
  468.     fptr = NULL;
  469.     for (cptr = PATH_SEPARATORS; *cptr; cptr++)
  470.     {
  471.         if ((ptr = strrchr(pathname, *cptr)) != NULL &&
  472.                 (fptr == NULL || ptr > fptr))
  473.             fptr = ptr;
  474.     }
  475.     if (fptr == NULL)
  476.         pos = 0;
  477.     else
  478.         pos = fptr + 1 - pathname;
  479.     if (path != NULL)
  480.     {
  481.        strncpy(path, pathname, pos);
  482.        path[pos] = NULL_CHAR;
  483.     }
  484.     if (entry != NULL)
  485.        strcpy(entry, &pathname[pos]);
  486.     return pos;
  487. }
  488.  
  489. static void
  490. strncopy(to, from, len)
  491. char *to;
  492. char *from;
  493. int  len;
  494. {
  495.     int i;
  496.  
  497.     for (i = 1; i < len && *from; i++)
  498.         *to++ = *from++;
  499.     *to = NULL_CHAR;
  500. }
  501.  
  502. void
  503. strlower(s)
  504. char *s;
  505. {
  506.     while (*s)
  507.     {
  508.         *s = (char) tolower(*s);
  509.         s++;
  510.     }
  511. }
  512.  
  513. void
  514. strupper(s)
  515. char *s;
  516. {
  517.     while (*s)
  518.     {
  519.         *s = (char) toupper(*s);
  520.         s++;
  521.     }
  522. }
  523.  
  524. voidp *
  525. malloc_msg(size)
  526. int size;
  527. {
  528.     char *p;
  529.  
  530.     if ((p = (char *)xmalloc(size)) == NULL)
  531.         error(M_NOMEMORY, "");
  532.     return (voidp *)p;
  533. }
  534.  
  535. static uint
  536. get_word()
  537. {
  538.     uint b0, b1;
  539.  
  540.     b0 = get_byte();
  541.     b1 = get_byte();
  542.     return (b1 << 8) + b0;
  543. }
  544.  
  545. static ulong
  546. get_longword()
  547. {
  548.     ulong b0, b1, b2, b3;
  549.  
  550.     b0 = get_byte();
  551.     b1 = get_byte();
  552.     b2 = get_byte();
  553.     b3 = get_byte();
  554.     return (b3 << 24) + (b2 << 16) + (b1 << 8) + b0;
  555. }
  556.  
  557. static long
  558. find_header(fd)
  559. FILE *fd;
  560. {
  561.     long arcpos, lastpos;
  562.     int c;
  563.  
  564.     arcpos = file_tell(fd);
  565.     file_seek(fd, 0L, SEEK_END);
  566.     lastpos = file_tell(fd) - 2;
  567.     if (lastpos > MAXSFX)
  568.         lastpos = MAXSFX;
  569.     for ( ; arcpos < lastpos; arcpos++)
  570.     {
  571.         file_seek(fd, arcpos, SEEK_SET);
  572.         c = fget_byte(fd);
  573.         while (arcpos < lastpos)
  574.         {
  575.             if (c != HEADER_ID_LO)  /* low order first */
  576.                 c = fget_byte(fd);
  577.             else if ((c = fget_byte(fd)) == HEADER_ID_HI)
  578.                 break;
  579.             arcpos++;
  580.         }
  581.         if (arcpos >= lastpos)
  582.             break;
  583.         if ((headersize = fget_word(fd)) <= HEADERSIZE_MAX)
  584.         {
  585.             crc = CRC_MASK;
  586.             fread_crc(header, (int) headersize, fd);
  587.             if ((crc ^ CRC_MASK) == fget_crc(fd))
  588.             {
  589.                 file_seek(fd, arcpos, SEEK_SET);
  590.                 return arcpos;
  591.             }
  592.         }
  593.     }
  594.     return -1;          /* could not find a valid header */
  595. }
  596.  
  597. static int
  598. read_header(first, fd, name)
  599. int  first;
  600. FILE *fd;
  601. char *name;
  602. {
  603.     ushort extheadersize, header_id;
  604.  
  605.     header_id = fget_word(fd);
  606.     if (header_id != HEADER_ID)
  607.     {
  608.         if (first)
  609.             error(M_NOTARJ, name);
  610.         else
  611.             error(M_BADHEADR, "");
  612.     }
  613.  
  614.     headersize = fget_word(fd);
  615.     if (headersize == 0)
  616.         return 0;               /* end of archive */
  617.     if (headersize > HEADERSIZE_MAX)
  618.         error(M_BADHEADR, "");
  619.  
  620.     crc = CRC_MASK;
  621.     fread_crc(header, (int) headersize, fd);
  622.     header_crc = fget_crc(fd);
  623.     if ((crc ^ CRC_MASK) != header_crc)
  624.         error(M_HEADRCRC, "");
  625.  
  626.     setup_get(header);
  627.     first_hdr_size = get_byte();
  628.     arj_nbr = get_byte();
  629.     arj_x_nbr = get_byte();
  630.     host_os = get_byte();
  631.     arj_flags = get_byte();
  632.     method = get_byte();
  633.     file_type = get_byte();
  634.     (void)get_byte();
  635.     time_stamp = get_longword();
  636.     compsize = get_longword();
  637.     origsize = get_longword();
  638.     file_crc = get_crc();
  639.     entry_pos = get_word();
  640.     file_mode = get_word();
  641.     host_data = get_word();
  642.  
  643.     hdr_filename = (char *)&header[first_hdr_size];
  644.     strncopy(filename, hdr_filename, sizeof(filename));
  645.     if (host_os != OS)
  646.         strparity((uchar *)filename);
  647.     if ((arj_flags & PATHSYM_FLAG) != 0)
  648.         decode_path(filename);
  649.  
  650.     hdr_comment = (char *)&header[first_hdr_size + strlen(hdr_filename) + 1];
  651.     strncopy(comment, hdr_comment, sizeof(comment));
  652.     if (host_os != OS)
  653.         strparity((uchar *)comment);
  654.  
  655.     /* if extheadersize == 0 then no CRC */
  656.     /* otherwise read extheader data and read 4 bytes for CRC */
  657.  
  658.     while ((extheadersize = fget_word(fd)) != 0)
  659.         file_seek(fd, (long) (extheadersize + 4), SEEK_CUR);
  660.  
  661.     return 1;                   /* success */
  662. }
  663.  
  664. static void
  665. skip()
  666. {
  667.     file_seek(arcfile, compsize, SEEK_CUR);
  668. }
  669.  
  670. static void
  671. unstore()
  672. {
  673.     int n;
  674.     long pos;
  675.     char *buffer;
  676.  
  677.     buffer = (char *)malloc_msg(BUFFERSIZE);
  678.     pos = file_tell(arcfile);
  679.     disp_clock();
  680.     n = (int)(BUFFERSIZE - (pos % BUFFERSIZE));
  681.     n = compsize > (long)n ? n : (int)compsize;
  682.     while (compsize > 0)
  683.     {
  684.         if (file_read(buffer, 1, n, arcfile) != n)
  685.             error(M_CANTREAD, "");
  686.         disp_clock();
  687.         compsize -= n;
  688.         fwrite_txt_crc((uchar *)buffer, n);
  689.         n = compsize > BUFFERSIZE ? BUFFERSIZE : (int)compsize;
  690.     }
  691.     free(buffer);
  692. }
  693.  
  694. static int
  695. check_flags()
  696. {
  697.     if (arj_x_nbr > ARJ_X_VERSION)
  698.     {
  699.         printf(M_UNKNVERS, arj_x_nbr);
  700.         printf(M_SKIPPED, filename);
  701.         skip();
  702.         return -1;
  703.     }
  704.     if ((arj_flags & GARBLE_FLAG) != 0)
  705.     {
  706.         printf(M_ENCRYPT);
  707.         printf(M_SKIPPED, filename);
  708.         skip();
  709.         return -1;
  710.     }
  711.     if (method < 0 || method > MAXMETHOD || (method == 4 && arj_nbr == 1))
  712.     {
  713.         printf(M_UNKNMETH, method);
  714.         printf(M_SKIPPED, filename);
  715.         skip();
  716.         return -1;
  717.     }
  718.     if (file_type != BINARY_TYPE && file_type != TEXT_TYPE)
  719.     {
  720.         printf(M_UNKNTYPE, file_type);
  721.         printf(M_SKIPPED, filename);
  722.         skip();
  723.         return -1;
  724.     }
  725.     return 0;
  726. }
  727.  
  728. static int
  729. extract()
  730. {
  731.     char name[FNAME_MAX];
  732.  
  733.     if (check_flags())
  734.     {
  735.         error_count++;
  736.         return 0;
  737.     }
  738.  
  739.     no_output = 0;
  740.     if (command == 'E')
  741.         strcpy(name, &filename[entry_pos]);
  742.     else
  743.     {
  744.         strcpy(name, DEFAULT_DIR);
  745.         strcat(name, filename);
  746.     }
  747.  
  748.     if (host_os != OS)
  749.         default_case_path(name);
  750.  
  751.     if (file_exists(name))
  752.     {
  753.         printf(M_FEXISTS, name);
  754.         printf(M_SKIPPED, name);
  755.         skip();
  756.         error_count++;
  757.         return 0;
  758.     }
  759.     outfile = file_open(name, writemode[file_type & 1]);
  760.     if (outfile == NULL)
  761.     {
  762.         printf(M_CANTOPEN, name);
  763.         putchar('\n');
  764.         skip();
  765.         error_count++;
  766.         return 0;
  767.     }
  768.     printf(M_EXTRACT, name);
  769.     if (host_os != OS && file_type == BINARY_TYPE)
  770.         printf(M_DIFFHOST);
  771.     printf("  ");
  772.  
  773.     crc = CRC_MASK;
  774.  
  775.     if (method == 0)
  776.         unstore();
  777.     else if (method == 1 || method == 2 || method == 3)
  778.         decode();
  779.     else if (method == 4)
  780.         decode_f();
  781.     fclose(outfile);
  782.  
  783.     set_ftime_mode(name, time_stamp, file_mode, (uint) host_os);
  784.  
  785.     if ((crc ^ CRC_MASK) == file_crc)
  786.         printf(M_CRCOK);
  787.     else
  788.     {
  789.         printf(M_CRCERROR);
  790.         error_count++;
  791.     }
  792.     return 1;
  793. }
  794.  
  795. static int
  796. test()
  797. {
  798.     if (check_flags())
  799.         return 0;
  800.  
  801.     no_output = 1;
  802.     printf(M_TESTING, filename);
  803.     printf("  ");
  804.  
  805.     crc = CRC_MASK;
  806.  
  807.     if (method == 0)
  808.         unstore();
  809.     else if (method == 1 || method == 2 || method == 3)
  810.         decode();
  811.     else if (method == 4)
  812.         decode_f();
  813.  
  814.     if ((crc ^ CRC_MASK) == file_crc)
  815.         printf(M_CRCOK);
  816.     else
  817.     {
  818.         printf(M_CRCERROR);
  819.         error_count++;
  820.     }
  821.     return 1;
  822. }
  823.  
  824. static uint
  825. ratio(a, b)
  826. long a, b;
  827. {
  828.    int i;
  829.  
  830.    for (i = 0; i < 3; i++)
  831.        if (a <= LONG_MAX / 10)
  832.            a *= 10;
  833.        else
  834.            b /= 10;
  835.    if ((long) (a + (b >> 1)) < a)
  836.    {
  837.        a >>= 1;
  838.        b >>= 1;
  839.    }
  840.    if (b == 0)
  841.        return 0;
  842.    return (uint) ((a + (b >> 1)) / b);
  843. }
  844.  
  845. static void
  846. list_start()
  847. {
  848.     printf("Filename       Original Compressed Ratio DateTime modified CRC-32   AttrBTPMGVX\n");
  849.     printf("------------ ---------- ---------- ----- ----------------- -------- -----------\n");
  850. }
  851.  
  852. static void
  853. list_arc(count)
  854. int count;
  855. {
  856.     uint r;
  857.     int garble_mode, path_mode, volume_mode, extfil_mode, ftype, bckf_mode;
  858.     char date_str[20], fmode_str[10];
  859.     static char mode[5] = { 'B', 'T', '?', 'D', 'V' };
  860.     static char pthf[2] = { ' ', '+' };
  861.     static char pwdf[2] = { ' ', 'G' };  /* plain, encrypted */
  862.     static char volf[2] = { ' ', 'V' };
  863.     static char extf[2] = { ' ', 'X' };
  864.     static char bckf[2] = { ' ', '*' };
  865.  
  866.     if (count == 0)
  867.         list_start();
  868.  
  869.     garble_mode = ((arj_flags & GARBLE_FLAG) != 0);
  870.     volume_mode = ((arj_flags & VOLUME_FLAG) != 0);
  871.     extfil_mode = ((arj_flags & EXTFILE_FLAG) != 0);
  872.     bckf_mode   = ((arj_flags & BACKUP_FLAG) != 0);
  873.     path_mode   = (entry_pos > 0);
  874.     r = ratio(compsize, origsize);
  875.     torigsize += origsize;
  876.     tcompsize += compsize;
  877.     ftype = file_type;
  878.     if (ftype != BINARY_TYPE && ftype != TEXT_TYPE && ftype != DIR_TYPE &&
  879.             ftype != LABEL_TYPE)
  880.         ftype = 3;
  881.     get_date_str(date_str, time_stamp);
  882.     strcpy(fmode_str, "    ");
  883.     if (host_os == OS)
  884.         get_mode_str(fmode_str, (uint) file_mode);
  885.     if (strlen(&filename[entry_pos]) > 12)
  886.         printf("%-12s\n             ", &filename[entry_pos]);
  887.     else
  888.         printf("%-12s ", &filename[entry_pos]);
  889.     printf("%10ld %10ld %u.%03u %s %08lX %4s%c%c%c%u%c%c%c\n",
  890.         origsize, compsize, r / 1000, r % 1000, &date_str[2], file_crc,
  891.         fmode_str, bckf[bckf_mode], mode[ftype], pthf[path_mode], method,
  892.         pwdf[garble_mode], volf[volume_mode], extf[extfil_mode]);
  893. }
  894.  
  895. static int /*SJD Sat  12-28-1991  14:30:14 */
  896. MatchWC(pat, fn) /*SJD Sat  12-28-1991  14:30:14 */
  897.   char *pat, *fn; /*SJD Sat  12-28-1991  14:30:14 */
  898. {
  899.   while (*pat && *fn)
  900.   {
  901.     if (*pat=='*' || *fn=='*')
  902.     {
  903. /*    if (*fn != '.')
  904.         fn++; */
  905.  
  906.       while (*fn && *fn != '.')
  907.         fn++;
  908.  
  909.       if (*pat != '.')
  910.         pat++;
  911.  
  912.        while (*pat && *pat != '.')
  913.         pat++;
  914.     }
  915.     else if (*pat=='?' || *fn=='?')
  916.     {
  917.       if (*pat != '.')
  918.         pat++;
  919.  
  920.       if (*fn != '.')
  921.         fn++;
  922.     }
  923.     else
  924.     {
  925.  
  926.       int ch1=toupper(*pat);
  927.       int ch2=toupper(*fn);
  928.       pat++;
  929.       fn++;
  930.  
  931.       if (ch1 != ch2)
  932.         return 0;
  933. /*
  934.       if (toupper(*pat) != toupper(*fn))
  935.         return 0;
  936.       pat++;
  937.       fn++;
  938. */
  939.     }
  940.   }
  941.  
  942.   return (*pat=='\0' && *fn=='\0');
  943. }
  944.  
  945. static void
  946. execute_cmd(list) /*SJD Sat  12-28-1991  14:27:04 */
  947.   char **list;
  948. {
  949.     int file_count;
  950.     char date_str[22];
  951.     char **lp; /*SJD Sat  12-28-1991  14:31:29 */
  952.     int doit; /*SJD Sat  12-28-1991  14:31:54 */
  953.     uint r;
  954.  
  955.     first_hdr_pos = 0;
  956.     time_stamp = 0;
  957.     first_hdr_size = FIRST_HDR_SIZE;
  958.  
  959.     arcfile = fopen_msg(arc_name, "rb");
  960.  
  961.     printf(M_PROCARC, arc_name);
  962.  
  963.     first_hdr_pos = find_header(arcfile);
  964.     if (first_hdr_pos < 0)
  965.         error(M_NOTARJ, arc_name);
  966.     file_seek(arcfile, first_hdr_pos, SEEK_SET);
  967.     if (!read_header(1, arcfile, arc_name))
  968.         error(M_BADCOMNT, "");
  969.     get_date_str(date_str, time_stamp);
  970.     printf(M_ARCDATE, date_str);
  971.  
  972.     file_count = 0;
  973.     while (read_header(0, arcfile, arc_name))
  974.     {
  975.      /*SJD Sat  12-28-1991  14:36:03  begin modifications */
  976.  
  977.       doit=0;
  978.  
  979.       if (*list==NULL)
  980.         doit=1;
  981.       else for (lp=list; *lp; lp++)
  982.         if (MatchWC(*lp, filename+entry_pos))
  983.         {
  984.           doit=1;
  985.           break;
  986.         }
  987.  
  988.       if (!doit)
  989.         skip();
  990.       else switch (command)/*SJD Sat  12-28-1991  14:36:16  end modifications*/
  991.         {
  992.         case 'E':
  993.         case 'X':
  994.             if (extract())
  995.                 file_count++;
  996.             break;
  997.         case 'L':
  998.             list_arc(file_count++);
  999.             skip();
  1000.             break;
  1001.         case 'T':
  1002.             if (test())
  1003.                 file_count++;
  1004.             break;
  1005.         }
  1006.     }
  1007.  
  1008.     if (command == 'L')
  1009.     {
  1010.         printf("------------ ---------- ---------- ----- -----------------\n");
  1011.         r = ratio(tcompsize, torigsize);
  1012.         printf(" %5d files %10ld %10ld %u.%03u %s\n",
  1013.             file_count, torigsize, tcompsize, r / 1000, r % 1000, &date_str[2]);
  1014.     }
  1015.     else
  1016.         printf(M_NBRFILES, file_count);
  1017.  
  1018.     fclose(arcfile);
  1019. }
  1020.  
  1021. static void
  1022. help()
  1023. {
  1024.     int i;
  1025.  
  1026.     for (i = 0; M_USAGE[i] != NULL; i++)
  1027.         printf(M_USAGE[i]);
  1028. }
  1029. /***************************************************************************\
  1030. main program
  1031. \***************************************************************************/
  1032.  
  1033. int
  1034. main(argc, argv)
  1035. int  argc;
  1036. char *argv[];
  1037. {
  1038.     int i, j, lastc;
  1039.     char *arc_p;
  1040.     char **list=NULL;
  1041.     char *strpnt;
  1042.  
  1043.     printf(M_VERSION);
  1044.  
  1045.     switch( argc )
  1046.     {
  1047.     case 1:
  1048.         help();
  1049.         return EXIT_SUCCESS;
  1050.     break;
  1051.     case 2:
  1052.         command = 'L';
  1053.         arc_p = argv[1];
  1054.         list=argv+2;
  1055.     break;
  1056.     default:
  1057.         if (strlen(argv[1]) > 1)
  1058.     {
  1059.             error(M_BADCOMND, argv[1]);
  1060.             return EXIT_FAILURE;
  1061.     }
  1062.         command = toupper(*argv[1]);
  1063.         if (strchr("ELTX", command) == NULL)
  1064.     {
  1065.             error(M_BADCOMND, argv[1]);
  1066.             return EXIT_FAILURE;
  1067.     }
  1068.  
  1069.         arc_p = argv[2];
  1070.         list=argv+3;
  1071.     break;
  1072.     }
  1073.  
  1074.     strncopy(arc_name, arc_p, FNAME_MAX);
  1075.     case_path(arc_name);
  1076.     i = max(0,strlen(arc_name) - 1);
  1077.     while ((arc_name[i] == ' ') && (i>=0))
  1078.     {
  1079.     arc_name[i]= '\0';
  1080.     i--;
  1081.     }
  1082.     i = strlen(arc_name);
  1083.     j = parse_path(arc_name, (char *)NULL, (char *)NULL);
  1084.     lastc = max(0,i - 1);
  1085.     if (arc_name[lastc] == ARJ_DOT)
  1086.         arc_name[lastc] = NULL_CHAR;
  1087.     else if ((strstr( arc_name, ARJ_SUFFIX) == NULL) &&
  1088.          (strstr( arc_name, ".ARJ") == NULL))
  1089.      {
  1090.             strcat(arc_name, ARJ_SUFFIX);
  1091.      }
  1092.  
  1093.     make_crctable();
  1094.  
  1095.     error_count = 0;
  1096.     clock_inx = 0;
  1097.     arcfile = NULL;
  1098.     outfile = NULL;
  1099.  
  1100.     execute_cmd(list);
  1101.  
  1102.     if (error_count > 0)
  1103.         error(M_ERRORCNT, "");
  1104.  
  1105.     return EXIT_SUCCESS;
  1106. }
  1107.  
  1108. /* end UNARJ.C */
  1109.