home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #7 / amigamamagazinepolishissue1998.iso / archiwizery / unarj / source / unarj.c < prev    next >
C/C++ Source or Header  |  1997-11-17  |  23KB  |  1,020 lines

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