home *** CD-ROM | disk | FTP | other *** search
/ Best Objectech Shareware Selections / UNTITLED.iso / boss / util / comp / 011 / unarj.arj / UNARJ.C < prev    next >
Text File  |  1991-05-30  |  21KB  |  953 lines

  1. /* UNARJ.C, UNARJ, R JUNG, 05/25/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 archivers.
  6.  *
  7.  *   If you wish to distribute a modified version of this program, you
  8.  *   must include the source code.
  9.  *
  10.  *   If you modify this program, I would appreciate a copy of the new
  11.  *   source code.  I am holding the copyright on the source code, so
  12.  *   please do not delete my name from the program files or from the
  13.  *   documentation.
  14.  *
  15.  *   I wish to give credit to Haruhiko Okumura for providing the
  16.  *   basic ideas for ARJ and UNARJ in his program AR.  Please note
  17.  *   that UNARJ is significantly different from AR from an archive
  18.  *   structural point of view.
  19.  *
  20.  * Modification history:
  21.  * Date      Programmer  Description of modification.
  22.  * 04/05/91  R. Jung     Rewrote code.
  23.  * 04/23/91  M. Adler     Portabilized.
  24.  * 04/29/91  R. Jung     Added l command.  Removed 16 bit dependency in
  25.  *             fillbuf().
  26.  * 05/19/91  R. Jung     Fixed extended header skipping code.
  27.  * 05/25/91  R. Jung     Improved find_header().
  28.  *
  29.  */
  30.  
  31. #include "unarj.h"
  32.  
  33. #ifdef MODERN
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <ctype.h>
  37. #else /* !MODERN */
  38. extern void free();
  39. extern void exit();
  40. extern char *strcat();
  41. extern char *strcpy();
  42. extern char *strncpy();
  43. extern char *strchr();
  44. extern char *strrchr();
  45. extern int strlen();
  46. extern int strcmp();
  47. #define EXIT_FAILURE (1)
  48. #define EXIT_SUCCESS (0)
  49. #define toupper(c) ((c)>='a'&&(c)<='z'?(c)-('a'-'A'):(c))
  50. #endif /* ?MODERN */
  51.  
  52. /* Global variables */
  53.  
  54. UCRC   crc;
  55. FILE   *arcfile;
  56. FILE   *outfile;
  57. ushort bitbuf;
  58. long   compsize;
  59. long   origsize;
  60. uchar  subbitbuf;
  61. uchar  header[HEADERSIZE_MAX];
  62. char   arc_name[FNAME_MAX];
  63. int    command;
  64. int    bitcount;
  65. int    file_type;
  66. int    no_output;
  67. int    error_count;
  68.  
  69. /* Messages */
  70.  
  71. static char *M_USAGE  [] =
  72. {
  73. "Usage:  UNARJ archive[.arj]    (list archive)\n",
  74. "        UNARJ e archive        (extract archive)\n",
  75. "        UNARJ l archive        (list archive)\n",
  76. "        UNARJ t archive        (test archive)\n",
  77. "        UNARJ x archive        (extract with pathnames)\n",
  78. "\n",
  79. "This is an ARJ demonstration program and ** IS NOT OPTIMIZED ** for speed.\n",
  80. "You may freely use, copy and distribute this program, provided that no fee\n",
  81. "is charged for such use, copying or distribution, and it is distributed\n",
  82. "ONLY in its original unmodified state.  UNARJ is provided as is without\n",
  83. "warranty of any kind, expressed or implied, including but not limited to\n",
  84. "the implied warranties of merchantability and fitness for a particular\n",
  85. "purpose.  Refer to UNARJ.DOC for more warranty information.  If you find\n",
  86. "UNARJ of value, a gift of $10 or any amount would greatly appreciated.\n",
  87. "\n",
  88. "Robert K Jung                   Internet address :  robjung@world.std.com\n",
  89. "2606 Village Road West          CompuServe userid:  72077,445\n",
  90. "Norwood, Massachusetts 02062\n",
  91. "USA\n",
  92. NULL
  93. };
  94.  
  95. char M_VERSION [] = "UNARJ (Demo version) 2.10 Copyright (c) 1991 Robert K Jung\n\n";
  96.  
  97. char M_ARCDATE [] = "Archive date      : %s\n";
  98. char M_BADCOMND[] = "Bad UNARJ command: %s";
  99. char M_BADCOMNT[] = "Invalid comment header";
  100. char M_BADHEADR[] = "Bad header";
  101. char M_BADTABLE[] = "Bad Huffman code";
  102. char M_CANTOPEN[] = "Can't open %s";
  103. char M_CANTREAD[] = "Can't read file or unexpected end of file";
  104. char M_CANTWRIT[] = "Can't write file. Disk full?";
  105. char M_CRCERROR[] = "CRC error!\n";
  106. char M_CRCOK   [] = "CRC OK\n";
  107. char M_DIFFHOST[] = ", Warning! Binary file from a different OS";
  108. char M_ENCRYPT [] = "File is password encrypted, ";
  109. char M_ERRORCNT[] = "%sFound %5d error(s)!";
  110. char M_EXTRACT [] = "Extracting %-25s";
  111. char M_FEXISTS [] = "%-25s exists, ";
  112. char M_HEADRCRC[] = "Header CRC error!";
  113. char M_NBRFILES[] = "%5d file(s)\n";
  114. char M_NOMEMORY[] = "Out of memory";
  115. char M_NOTARJ  [] = "%s is not an ARJ archive";
  116. char M_PROCARC [] = "Processing archive: %s\n";
  117. char M_SKIPPED [] = "Skipped %s\n";
  118. char M_SUFFIX  [] = ARJ_SUFFIX;
  119. char M_TESTING [] = "Testing    %-25s";
  120. char M_UNKNMETH[] = "Unsupported method: %d, ";
  121. char M_UNKNTYPE[] = "Unsupported file type: %d, ";
  122. char M_UNKNVERS[] = "Unsupported version: %d, ";
  123.  
  124. #define get_crc()    get_longword()
  125. #define fget_crc(f)    fget_longword(f)
  126.  
  127. #define setup_get(PTR)    (get_ptr = (PTR))
  128. #define get_byte()    ((uchar)(*get_ptr++ & 0xff))
  129.  
  130. #define BUFFERSIZE    4096
  131.  
  132. #define ASCII_MASK    0x7F
  133.  
  134. #define CRCPOLY     0xEDB88320L
  135.  
  136. #define UPDATE_CRC(r,c) r=crctable[((uchar)(r)^(uchar)(c))&0xff]^(r>>CHAR_BIT)
  137.  
  138. /* Local functions */
  139.  
  140. #ifdef MODERN
  141. static void  make_crctable(void);
  142. static void  crc_buf(char *str, int len);
  143. static void  strparity(uchar *p);
  144. static FILE  *fopen_msg(char *name, char *mode);
  145. static int   fget_byte(FILE *f);
  146. static uint  fget_word(FILE *f);
  147. static ulong fget_longword(FILE *f);
  148. static void  fread_crc(uchar *p, int n, FILE *f);
  149. static void  decode_path(char *name);
  150. static void  get_date_str(char *str, ulong tstamp);
  151. static int   parse_path(char *pathname, char *path, char *entry);
  152. static void  strncopy(char *to, char *from, int len);
  153. static uint  get_word(void);
  154. static ulong get_longword(void);
  155. static long  find_header(FILE *fd);
  156. static int   read_header(int first, FILE *fd, char *name);
  157. static void  skip(void);
  158. static void  unstore(void);
  159. static int   check_flags(void);
  160. static int   extract(void);
  161. static int   test(void);
  162. static uint  ratio(long a, long b);
  163. static void  list_start(void);
  164. static void  list_arc(int count);
  165. static void  execute_cmd(void);
  166. static void  help(void);
  167. #endif /* MODERN */
  168.  
  169. /* Local variables */
  170.  
  171. static char   filename[FNAME_MAX];
  172. static char   comment[COMMENT_MAX];
  173. static char   *hdr_filename;
  174. static char   *hdr_comment;
  175.  
  176. static ushort headersize;
  177. static uchar  first_hdr_size;
  178. static uchar  arj_nbr;
  179. static uchar  arj_x_nbr;
  180. static uchar  host_os;
  181. static uchar  arj_flags;
  182. static short  method;
  183. static uint   file_mode;
  184. static ulong  time_stamp;
  185. static short  entry_pos;
  186. static ushort host_data;
  187. static uchar  *get_ptr;
  188. static UCRC   file_crc;
  189. static UCRC   header_crc;
  190.  
  191. static long   first_hdr_pos;
  192. static long   torigsize;
  193. static long   tcompsize;
  194.  
  195. static int    clock_inx;
  196.  
  197. static char   *writemode[2]  = { "wb",    "w" };
  198.  
  199. static UCRC   crctable[UCHAR_MAX + 1];
  200.  
  201. /* Functions */
  202.  
  203. static void
  204. make_crctable()
  205. {
  206.     uint i, j;
  207.     UCRC r;
  208.  
  209.     for (i = 0; i <= UCHAR_MAX; i++)
  210.     {
  211.     r = i;
  212.     for (j = CHAR_BIT; j > 0; j--)
  213.     {
  214.         if (r & 1)
  215.         r = (r >> 1) ^ CRCPOLY;
  216.         else
  217.         r >>= 1;
  218.     }
  219.     crctable[i] = r;
  220.     }
  221. }
  222.  
  223. static void
  224. crc_buf(str, len)
  225. char *str;
  226. int  len;
  227. {
  228.     while (len--)
  229.     UPDATE_CRC(crc, *str++);
  230. }
  231.  
  232. void
  233. disp_clock()
  234. {
  235.     static char clock_str[4] = { '|', '/', '-', '\\' };
  236.  
  237.     printf("(%c)\b\b\b", clock_str[clock_inx]);
  238.     clock_inx = (clock_inx + 1) & 0x03;
  239. }
  240.  
  241. void
  242. error(fmt, arg)
  243. char *fmt;
  244. char *arg;
  245. {
  246.     putc('\n', stdout);
  247.     printf(fmt, arg, error_count);
  248.     putc('\n', stdout);
  249.     exit(EXIT_FAILURE);
  250. }
  251.  
  252. static void
  253. strparity(p)
  254. uchar *p;
  255. {
  256.     while (*p)
  257.     {
  258.     FIX_PARITY(*p);
  259.     p++;
  260.     }
  261. }
  262.  
  263. static FILE *
  264. fopen_msg(name, mode)
  265. char *name;
  266. char *mode;
  267. {
  268.     FILE *fd;
  269.  
  270.     fd = file_open(name, mode);
  271.     if (fd == NULL)
  272.     error(M_CANTOPEN, name);
  273.     return fd;
  274. }
  275.  
  276. static int
  277. fget_byte(f)
  278. FILE *f;
  279. {
  280.     int c;
  281.  
  282.     if ((c = getc(f)) == EOF)
  283.     error(M_CANTREAD, "");
  284.     return c & 0xFF;
  285. }
  286.  
  287. static uint
  288. fget_word(f)
  289. FILE *f;
  290. {
  291.     uint b0, b1;
  292.  
  293.     b0 = fget_byte(f);
  294.     b1 = fget_byte(f);
  295.     return (b1 << 8) + b0;
  296. }
  297.  
  298. static ulong
  299. fget_longword(f)
  300. FILE *f;
  301. {
  302.     ulong b0, b1, b2, b3;
  303.  
  304.     b0 = fget_byte(f);
  305.     b1 = fget_byte(f);
  306.     b2 = fget_byte(f);
  307.     b3 = fget_byte(f);
  308.     return (b3 << 24) + (b2 << 16) + (b1 << 8) + b0;
  309. }
  310.  
  311. static void
  312. fread_crc(p, n, f)
  313. uchar *p;
  314. int   n;
  315. FILE  *f;
  316. {
  317.     n = file_read((char *)p, 1, n, f);
  318.     origsize += n;
  319.     crc_buf((char *)p, n);
  320. }
  321.  
  322. void
  323. fwrite_txt_crc(p, n)
  324. uchar