⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unarj.c

📁 解压arj文件的程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/* UNARJ.C, UNARJ, R JUNG, 07/29/96
 * Main Extractor routine
 * Copyright (c) 1991-97 by ARJ Software, Inc.  All rights reserved.
 *
 *   This code may be freely used in programs that are NOT ARJ archivers
 *   (both compress and extract ARJ archives).
 *
 *   If you wish to distribute a modified version of this program, you
 *   MUST indicate that it is a modified version both in the program and
 *   source code.
 *
 *   We are holding the copyright on the source code, so please do not
 *   delete our name from the program files or from the documentation.
 *
 *   We wish to give credit to Haruhiko Okumura for providing the
 *   basic ideas for ARJ and UNARJ in his program AR.  Please note
 *   that UNARJ is significantly different from AR from an archive
 *   structural point of view.
 *
 * Modification history:
 * Date      Programmer  Description of modification.
 * 04/05/91  R. Jung     Rewrote code.
 * 04/23/91  M. Adler    Portabilized.
 * 04/29/91  R. Jung     Added l command.  Removed 16 bit dependency in
 *                       fillbuf().
 * 05/19/91  R. Jung     Fixed extended header skipping code.
 * 05/25/91  R. Jung     Improved find_header().
 * 06/03/91  R. Jung     Changed arguments in get_mode_str() and
 *                       set_ftime_mode().
 * 06/19/81  R. Jung     Added two more %c in printf() in list_arc().
 * 07/07/91  R. Jung     Added default_case_path() to extract().
 *                       Added strlower().
 * 07/20/91  R. Jung     Changed uint ratio() to static uint ratio().
 * 07/21/91  R. Jung     Added #ifdef VMS.
 * 08/28/91  R. Jung     Changed M_DIFFHOST message.
 * 08/31/91  R. Jung     Added changes to support MAC THINK_C compiler
 *                       per Eric Larson.
 * 10/07/91  R. Jung     Added missing ; to THINK_C additions.
 * 11/11/91  R. Jung     Added host_os test to fwrite_txt_crc().
 * 11/24/91  R. Jung     Added more error_count processing.
 * 12/03/91  R. Jung     Added backup file processing.
 * 02/17/93  R. Jung     Added archive modified date support.
 * 01/22/94  R. Jung     Changed copyright message.
 * 07/29/96  R. Jung     Added "/" to list of path separators.
 *
 */

#include "unarj.h"

#ifdef MODERN
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#else /* !MODERN */
extern void free();
extern void exit();
extern char *strcat();
extern char *strcpy();
extern char *strncpy();
extern char *strchr();
extern char *strrchr();
extern int strlen();
extern int strcmp();
#ifdef VMS
#include <ssdef.h>
#define EXIT_FAILURE SS$_ABORT
#define EXIT_SUCCESS SS$_NORMAL
#else
#define EXIT_FAILURE (1)
#define EXIT_SUCCESS (0)
#endif
#define toupper(c)   ((c)>='a'&&(c)<='z'?(c)-('a'-'A'):(c))
#define tolower(c)   ((c)>='A'&&(c)<='Z'?(c)+('a'-'A'):(c))
#endif /* ?MODERN */

#ifdef THINK_C
#include <console.h>
#endif

/* Global variables */

UCRC   crc;
FILE   *arcfile;
FILE   *outfile;
ushort bitbuf;
long   compsize;
long   origsize;
uchar  subbitbuf;
uchar  header[HEADERSIZE_MAX];
char   arc_name[FNAME_MAX];
int    command;
int    bitcount;
int    file_type;
int    no_output;
int    error_count;

/* Messages */

static char *M_USAGE  [] =
{
"Usage:  UNARJ archive[.arj]    (list archive)\n",
"        UNARJ e archive        (extract archive)\n",
"        UNARJ l archive        (list archive)\n",
"        UNARJ t archive        (test archive)\n",
"        UNARJ x archive        (extract with pathnames)\n",
"\n",
"This is an ARJ demonstration program and ** IS NOT OPTIMIZED ** for speed.\n",
"You may freely use, copy and distribute this program, provided that no fee\n",
"is charged for such use, copying or distribution, and it is distributed\n",
"ONLY in its original unmodified state.  UNARJ is provided as is without\n",
"warranty of any kind, express or implied, including but not limited to\n",
"the implied warranties of merchantability and fitness for a particular\n",
"purpose.  Refer to UNARJ.DOC for more warranty information.\n",
"\n",
"ARJ Software, Inc.      Internet address:  robjung@world.std.com\n",
"P.O. Box 249                    Web site:  www.arjsoft.com\n",
"Norwood MA 02062\n",
"USA\n",
NULL
};

char M_VERSION [] = "UNARJ (Demo version) 2.43 Copyright (c) 1991-97 ARJ Software, Inc.\n\n";

char M_ARCDATE [] = "Archive created: %s";
char M_ARCDATEM[] = ", modified: %s";
char M_BADCOMND[] = "Bad UNARJ command: %s";
char M_BADCOMNT[] = "Invalid comment header";
char M_BADHEADR[] = "Bad header";
char M_BADTABLE[] = "Bad Huffman code";
char M_CANTOPEN[] = "Can't open %s";
char M_CANTREAD[] = "Can't read file or unexpected end of file";
char M_CANTWRIT[] = "Can't write file. Disk full?";
char M_CRCERROR[] = "CRC error!\n";
char M_CRCOK   [] = "CRC OK\n";
char M_DIFFHOST[] = "  Binary file!";
char M_ENCRYPT [] = "File is password encrypted, ";
char M_ERRORCNT[] = "%sFound %5d error(s)!";
char M_EXTRACT [] = "Extracting %-25s";
char M_FEXISTS [] = "%-25s exists, ";
char M_HEADRCRC[] = "Header CRC error!";
char M_NBRFILES[] = "%5d file(s)\n";
char M_NOMEMORY[] = "Out of memory";
char M_NOTARJ  [] = "%s is not an ARJ archive";
char M_PROCARC [] = "Processing archive: %s\n";
char M_SKIPPED [] = "Skipped %s\n";
char M_SUFFIX  [] = ARJ_SUFFIX;
char M_TESTING [] = "Testing    %-25s";
char M_UNKNMETH[] = "Unsupported method: %d, ";
char M_UNKNTYPE[] = "Unsupported file type: %d, ";
char M_UNKNVERS[] = "Unsupported version: %d, ";

#define get_crc()       get_longword()
#define fget_crc(f)     fget_longword(f)

#define setup_get(PTR)  (get_ptr = (PTR))
#define get_byte()      ((uchar)(*get_ptr++ & 0xff))

#define BUFFERSIZE      4096

#define ASCII_MASK      0x7F

#define CRCPOLY         0xEDB88320L

#define UPDATE_CRC(r,c) r=crctable[((uchar)(r)^(uchar)(c))&0xff]^(r>>CHAR_BIT)

/* Local functions */

#ifdef MODERN
static void  make_crctable(void);
static void  crc_buf(char *str, int len);
static void  strparity(uchar *p);
static FILE  *fopen_msg(char *name, char *mode);
static int   fget_byte(FILE *f);
static uint  fget_word(FILE *f);
static ulong fget_longword(FILE *f);
static void  fread_crc(uchar *p, int n, FILE *f);
static void  decode_path(char *name);
static void  get_date_str(char *str, ulong tstamp);
static int   parse_path(char *pathname, char *path, char *entry);
static void  strncopy(char *to, char *from, int len);
static uint  get_word(void);
static ulong get_longword(void);
static long  find_header(FILE *fd);
static int   read_header(int first, FILE *fd, char *name);
static void  skip(void);
static void  unstore(void);
static int   check_flags(void);
static int   extract(void);
static int   test(void);
static uint  ratio(long a, long b);
static void  list_start(void);
static void  list_arc(int count);
static void  execute_cmd(void);
static void  help(void);
#endif /* MODERN */

/* Local variables */

static char   filename[FNAME_MAX];
static char   comment[COMMENT_MAX];
static char   *hdr_filename;
static char   *hdr_comment;

static ushort headersize;
static uchar  first_hdr_size;
static uchar  arj_nbr;
static uchar  arj_x_nbr;
static uchar  host_os;
static uchar  arj_flags;
static short  method;
static uint   file_mode;
static ulong  time_stamp;
static short  entry_pos;
static ushort host_data;
static uchar  *get_ptr;
static UCRC   file_crc;
static UCRC   header_crc;

static long   first_hdr_pos;
static long   torigsize;
static long   tcompsize;

static int    clock_inx;

static char   *writemode[2]  = { "wb",  "w" };

static UCRC   crctable[UCHAR_MAX + 1];

/* Functions */

static void
make_crctable()
{
    uint i, j;
    UCRC r;

    for (i = 0; i <= UCHAR_MAX; i++)
    {
        r = i;
        for (j = CHAR_BIT; j > 0; j--)
        {
            if (r & 1)
                r = (r >> 1) ^ CRCPOLY;
            else
                r >>= 1;
        }
        crctable[i] = r;
    }
}

static void
crc_buf(str, len)
char *str;
int  len;
{
    while (len--)
        UPDATE_CRC(crc, *str++);
}

void
disp_clock()
{
    static char clock_str[4] = { '|', '/', '-', '\\' };

    printf("(%c)\b\b\b", clock_str[clock_inx]);
    clock_inx = (clock_inx + 1) & 0x03;
}

void
error(fmt, arg)
char *fmt;
char *arg;
{
    putc('\n', stdout);
    printf(fmt, arg, error_count);
    putc('\n', stdout);
    exit(EXIT_FAILURE);
}

static void
strparity(p)
uchar *p;
{
    while (*p)
    {
        FIX_PARITY(*p);
        p++;
    }
}

static FILE *
fopen_msg(name, mode)
char *name;
char *mode;
{
    FILE *fd;

    fd = file_open(name, mode);
    if (fd == NULL)
        error(M_CANTOPEN, name);
    return fd;
}

static int
fget_byte(f)
FILE *f;
{
    int c;

    if ((c = getc(f)) == EOF)
        error(M_CANTREAD, "");
    return c & 0xFF;
}

static uint
fget_word(f)
FILE *f;
{
    uint b0, b1;

    b0 = fget_byte(f);
    b1 = fget_byte(f);
    return (b1 << 8) + b0;
}

static ulong
fget_longword(f)
FILE *f;
{
    ulong b0, b1, b2, b3;

    b0 = fget_byte(f);
    b1 = fget_byte(f);
    b2 = fget_byte(f);
    b3 = fget_byte(f);
    return (b3 << 24) + (b2 << 16) + (b1 << 8) + b0;
}

static void
fread_crc(p, n, f)
uchar *p;
int   n;
FILE  *f;
{
    n = file_read((char *)p, 1, n, f);
    origsize += n;
    crc_buf((char *)p, n);
}

void
fwrite_txt_crc(p, n)
uchar *p;
int   n;
{
    uchar c;

    crc_buf((char *)p, n);
    if (no_output)
        return;

    if (file_type == TEXT_TYPE)
    {
        while (n--)
        {
            c = *p++;
            if (host_os != OS)
            {
                FIX_PARITY(c);
            }
            if (putc((int) c, outfile) == EOF)
                error(M_CANTWRIT, "");
        }
    }
    else
    {
        if (file_write((char *)p, 1, n, outfile) != n)
            error(M_CANTWRIT, "");
    }
}

void
init_getbits()
{
    bitbuf = 0;
    subbitbuf = 0;
    bitcount = 0;
    fillbuf(2 * CHAR_BIT);
}

void
fillbuf(n)                /* Shift bitbuf n bits left, read n bits */
int n;
{
    bitbuf = (bitbuf << n) & 0xFFFF;  /* lose the first n bits */
    while (n > bitcount)
    {
        bitbuf |= subbitbuf << (n -= bitcount);
        if (compsize != 0)
        {
            compsize--;
            subbitbuf = (uchar) getc(arcfile);
        }
        else
            subbitbuf = 0;
        bitcount = CHAR_BIT;
    }
    bitbuf |= subbitbuf >> (bitcount -= n);
}

ushort
getbits(n)
int n;
{
    ushort x;

    x = bitbuf >> (2 * CHAR_BIT - n);
    fillbuf(n);
    return x;
}

static void
decode_path(name)
char *name;
{
    for ( ; *name; name++)
    {
        if (*name == ARJ_PATH_CHAR)
            *name = PATH_CHAR;
    }
}

static void
get_date_str(str, tstamp)
char  *str;
ulong tstamp;
{
    sprintf(str, "%04u-%02u-%02u %02u:%02u:%02u",
           ts_year(tstamp), ts_month(tstamp), ts_day(tstamp),
           ts_hour(tstamp), ts_min(tstamp), ts_sec(tstamp));
}

static int
parse_path(pathname, path, entry)
char *pathname;
char *path;
char *entry;
{
    char *cptr, *ptr, *fptr;
    short pos;

    fptr = NULL;
    for (cptr = PATH_SEPARATORS; *cptr; cptr++)
    {
        if ((ptr = strrchr(pathname, *cptr)) != NULL &&
                (fptr == NULL || ptr > fptr))
            fptr = ptr;
    }
    if (fptr == NULL)
        pos = 0;
    else
        pos = fptr + 1 - pathname;
    if (path != NULL)
    {
       strncpy(path, pathname, pos);
       path[pos] = NULL_CHAR;
    }
    if (entry != NULL)
       strcpy(entry, &pathname[pos]);
    return pos;
}

static void
strncopy(to, from, len)
char *to;
char *from;
int  len;
{
    int i;

    for (i = 1; i < len && *from; i++)
        *to++ = *from++;
    *to = NULL_CHAR;
}

void
strlower(s)
char *s;
{
    while (*s)
    {
        *s = (char) tolower(*s);
        s++;
    }
}

void
strupper(s)
char *s;
{
    while (*s)
    {
        *s = (char) toupper(*s);
        s++;
    }
}

voidp *
malloc_msg(size)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -