home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 2001 January
/
VPR0101A.BIN
/
OLS
/
TAR32053
/
tar32053.exe
/
SRC
/
GZIP.C
< prev
next >
Wrap
C/C++ Source or Header
|
1999-05-23
|
13KB
|
432 lines
#ifndef __DEFCONF_H
#include "defconf.h"
#endif
/*
* Gzip.c for kmtar Power Up Kit v3.x
* This file made from orignal gzip.c
*
at first 1993-08-08
last 1995-03-04
tantan SGL00213@niftyserve.or.jp
*/
#include <stdio.h>
#include <io.h>
#ifdef __TURBOC__
#include <string.h>
#endif
#include "gzip.h"
#include "lzw.h"
#include "tailor.h"
#include "archio.h"
#include "misc.h"
#include "compress.h"
#ifdef WIN32
#include <conio.h>
#endif
/* local variables */
int force = 0; /* */
int no_name = 0; /* don't save or restore the original file name */
int recursive = 0; /* recurse through directories (-r) */
int list = 0; /* list the file contents (-l) */
int verbose = 0; /* be verbose (-v) */
int quiet = 0; /* be very quiet (-q) */
int do_lzw = 0; /* generate output compatible with old compress (-Z) */
int test = 0; /* test .gz file integrity */
int foreground; /* set if program run in foreground */
char *progname; /* program name */
int maxbits = BITS; /* max bits per code for LZW */
int method = DEFLATED;/* compression method */
int level = 6; /* compression level */
int exit_code = OK; /* program exit code */
int save_orig_name=0; /* set if original name must be saved */
int last_member; /* set for .zip and .Z files */
int part_nb; /* number of parts in .gz file */
long gzip_time_stamp; /* original time stamp (modification time) */
char gzip_filename[MAX_PATH_LEN];
int gzip_ostype;
long ifile_size; /* input file size, -1 for devices (debug only) */
char *env; /* contents of GZIP env variable */
char **args = NULL; /* argv pointer if GZIP env variable defined */
char z_suffix[MAX_SUFFIX+1]; /* default suffix (can be set with --suffix) */
int z_len; /* strlen(z_suffix) */
long bytes_in; /* number of input bytes */
long bytes_out; /* number of output bytes */
long total_in = 0; /* input bytes for all files */
long total_out = 0; /* output bytes for all files */
/*char ifname[MAX_PATH_LEN]; input file name */
/*char ofname[MAX_PATH_LEN]; output file name */
int remove_ofname = 0; /* remove output file on error */
/*struct stat istat; status for input file */
int ifd; /* input file descriptor */
int ofd; /* output file descriptor */
unsigned insize; /* valid bytes in inbuf */
unsigned inptr; /* index of next byte to be processed in inbuf */
unsigned outcnt; /* bytes in output buffer */
DECLARE(uch, inbuf, INBUFSIZ +INBUF_EXTRA);
DECLARE(uch, window, 2L*WSIZE);
/*unsigned char *window = NULL;*/ /**/
/*uch *inbuf=NULL; */ /* input buffer */
long header_bytes=0; /**/
int uncompress();
static char eof_flag=0;
long sfx_header_length;
int (*work) OF((int infile, int outfile)); /* function to call */
int get_new_file(char *fname)
{
fprintf(stderr,"\n\aExchange FD for new file %s\n",fname);
fprintf(stderr," <<<<<<<< Hit anykey >>>>>>>\n");
getch();
if ((ifd = open_dev(fname,'r')) == -1){
printf("file %s dose not exist\n",fname);
return EOF;
}
return OK;
}
#define unget_byte() (inptr > 0 ? inptr--:0)
/* get_byteの内部変数*/
static int get_byte_divid = -1,get_byte_filenumber;
static char *get_byte_inner_fname;
int get_byte(void)
{
/*#define INSIZE 30000*/
#define INSIZE INBUFSIZ
extern char *Archives[];
extern int Afd;
//static int divid = -1,filenumber;
//static char *inner_fname;
int readlen;
/*
if (eof_flag)
fatal("get_byte","file read error");
*/
if (inbuf == NULL){
ALLOC(uch,inbuf,INSIZE);
if (inbuf == NULL)
fatal("get_byte","Out of memory");
}
if (insize == 0 || insize == inptr){
if (ifd == -1) return EOF;
if ((readlen = read(ifd,inbuf,INSIZE)) == 0){
if (readlen < 0)
fatal("get_byte","read error\n");
if (get_byte_divid == -1){
if (get_byte_divid = chk_DIVI()){
get_byte_filenumber=1;
get_byte_inner_fname = strdup(Archives[0]);
}
}
close(ifd);
ifd = -1;
if (get_byte_divid){
int ret,size;
sprintf(get_byte_inner_fname+strlen(get_byte_inner_fname)-3,
"%03d",get_byte_filenumber++);
if ((ifd = open_dev(get_byte_inner_fname,'r')) == -1)
ret = get_new_file(get_byte_inner_fname);
if (ret == EOF)
return EOF;
size = read(ifd,inbuf,INSIZE);
/* if (size != INSIZE){
fprintf(stderr,"internal error %s too small\n",
inner_fname);
exit(1);
}
*/
insize = size;
Afd = ifd;
}else{
/* eof_flag = 1;*/
return EOF;
}
}else if (readlen == -1){
/* eof_flag = 1;*/
return EOF;
}else
insize = readlen;
inptr=0;
}
return inbuf[inptr++];
}
/*
* init read buffer
*/
void init_read_buf(void)
{
extern unsigned long innerbuf_len;
extern int vm_page,read_page;
extern void init_unzip(void);
eof_flag=0;
innerbuf_len=0L;
#ifdef DYN_ALLOC
if (inbuf == NULL)
ALLOC(uch, inbuf, INBUFSIZ +INBUF_EXTRA);
if (window == NULL)
ALLOC(uch, window, 2L*WSIZE);
#endif
init_comp();
init_unzip();
}
/* ========================================================================
* Check the magic number of the input file and update ofname if an
* original name was given and to_stdout is not set.
* Return the compression method, -1 for error, -2 for warning.
* Set inptr to the offset of the next byte to be processed.
* This function may be called repeatedly for an input file consisting
* of several contiguous gzip'ed members.
* IN assertions: there is at least one remaining compressed member.
* If the member is a zip file, it must be the only one.
*/
int get_method(int in)
/* int in; input file descriptor */
{
uch flags;
char magic[2]; /* magic header */
sfx_header_length = 0;
ifd = in; /* local copy of in */
/* If --force and --stdout, zcat == cat, so do not complain about
* premature end of file: use try_byte instead of get_byte.
*/
magic[0] = (char)get_byte();
magic[1] = (char)get_byte();
/* for kmtarsef.exe 's SFX(Self Extracting) by tsuneo...*/
if(magic[0]=='M' && magic[1]=='Z'){
insize=0;
lseek(ifd,64000,SEEK_SET);
magic[0]=(char)get_byte();
magic[1]=(char)get_byte();
sfx_header_length = 64000;
}
/* gzip_time_stamp = istat.st_mtime; may be modified later for some methods */
method = -1; /* unknown yet */
part_nb++; /* number of parts in gzip file */
header_bytes = 0;
/* last_member = RECORD_IO; */
/* assume multiple members in gzip file except for record oriented I/O */
if (memcmp(magic, GZIP_MAGIC, 2) == 0
|| memcmp(magic, OLD_GZIP_MAGIC, 2) == 0) {
method = (int)get_byte();
if (method != DEFLATED) {
fprintf(stderr,
"unknown compress method %d -- get newer version of gzip\n",
method);
exit_code = GZIP_ERROR;
return -1;
}
work = unzip; /* do unzip function */
flags = (uch)get_byte();
if ((flags & ENCRYPTED) != 0) {
fprintf(stderr,
"Archive is encrypted -- get newer version of gzip\n");
exit_code = GZIP_ERROR;
return -1;
}
if ((flags & CONTINUATION) != 0) {
fprintf(stderr,
"Archive is a a multi-part gzip file -- get newer version of gzip\n");
exit_code = GZIP_ERROR;
if (force <= 1) return -1;
}
if ((flags & RESERVED) != 0) {
fprintf(stderr,
"Archive has flags 0x%x -- get newer version of gzip\n",
flags);
exit_code = GZIP_ERROR;
if (force <= 1) return -1;
}
gzip_time_stamp = (ulg)get_byte();
gzip_time_stamp |= ((ulg)get_byte()) << 8;
gzip_time_stamp |= ((ulg)get_byte()) << 16;
gzip_time_stamp |= ((ulg)get_byte()) << 24;
(void)get_byte(); /* Ignore extra flags for the moment */
gzip_ostype = get_byte(); /* Ignore OS type for the moment */
if ((flags & CONTINUATION) != 0) {
unsigned part = (unsigned)get_byte();
part |= ((unsigned)get_byte())<<8;
if (verbose) {
fprintf(stderr," part number %u\n", part);
}
}
if ((flags & EXTRA_FIELD) != 0) {
unsigned len = (unsigned)get_byte();
len |= ((unsigned)get_byte())<<8;
if (verbose) {
fprintf(stderr,"extra field of %u bytes ignored\n",len);
}
while (len--) (void)get_byte();
}
/* Get original file name if it was truncated */
if ((flags & ORIG_NAME) != 0) {
/* Discard the ORIG_NAME */
#if 0
char c; /* dummy used for NeXTstep 3.0 cc optimizer bug */
while ((c=get_byte()) != 0) c++;
#else
char *ptr = gzip_filename;
while((*ptr=get_byte()) != 0){ptr++;}
#endif
} /* ORIG_NAME */
/* Discard file comment if any */
if ((flags & COMMENT) != 0) {
while (get_char() != 0) /* null */ ;
}
if (part_nb == 1) {
header_bytes = inptr + 2*sizeof(long); /* include crc and size */
}
}else if (memcmp(magic, LZW_MAGIC, 2) == 0) {
work = uncompress;
method = COMPRESSED;
last_member = 1;
}else if(memcmp(magic,"BZ",2) == 0){
if(get_byte() == 'h'
&& get_byte() /* level ('1' - '9')*/
&& get_byte() == '\x31'
&& get_byte() == '\x41'
&& get_byte() == '\x59'
&& get_byte() == '\x26'
&& get_byte() == '\x53'
&& get_byte() == '\x59'
){
method = BZIP2ED;
}else{
method = NO_COMP;
}
}else {
method = NO_COMP;
}
#if 0
else if (memcmp(magic, PKZIP_MAGIC, 2) == 0 && inptr == 2
&& memcmp((char*)inbuf, PKZIP_MAGIC, 4) == 0) {
/* To simplify the code, we support a zip file when alone only.
* We are thus guaranteed that the entire local header fits in inbuf.
*/
inptr = 0;
work = unzip;
if (check_zipfile(in) != OK) return -1;
/* check_zipfile may get ofname from the local header */
last_member = 1;
} else if (memcmp(magic, PACK_MAGIC, 2) == 0) {
work = unpack;
method = PACKED;
}
else if (memcmp(magic, LZW_MAGIC, 2) == 0) {
work = unlzw;
method = COMPRESSED;
last_member = 1;
} else if (memcmp(magic, LZH_MAGIC, 2) == 0) {
work = unlzh;
method = LZHED;
last_member = 1;
}
#endif
/*
if (method != DEFLATED){
exit_code = GZIP_ERROR;
return -1;
}
*/
if (method >= 0) return method;
}
#ifdef DLL
void gzip_static_init(void)
{
force = 0; /* */
no_name = 0; /* don't save or restore the original file name */
recursive = 0; /* recurse through directories (-r) */
list = 0; /* list the file contents (-l) */
verbose = 0; /* be verbose (-v) */
quiet = 0; /* be very quiet (-q) */
do_lzw = 0; /* generate output compatible with old compress (-Z) */
test = 0; /* test .gz file integrity */
foreground=0; /* set if program run in foreground */
progname=NULL; /* program name */
maxbits = BITS; /* max bits per code for LZW */
method = DEFLATED;/* compression method */
level = 6; /* compression level */
exit_code = OK; /* program exit code */
save_orig_name=0; /* set if original name must be saved */
last_member=0; /* set for .zip and .Z files */
part_nb=0; /* number of parts in .gz file */
gzip_time_stamp=0; /* original time stamp (modification time) */
memset(gzip_filename,0,sizeof(gzip_filename));
gzip_ostype = -1;
ifile_size=0; /* input file size, -1 for devices (debug only) */
env=NULL; /* contents of GZIP env variable */
args = NULL; /* argv pointer if GZIP env variable defined */
memset(z_suffix,0,sizeof(char)*(MAX_SUFFIX+1)); /* default suffix (can be set with --suffix) */
z_len=0; /* strlen(z_suffix) */
bytes_in=0; /* number of input bytes */
bytes_out=0; /* number of output bytes */
total_in = 0; /* input bytes for all files */
total_out = 0; /* output bytes for all files */
/*char ifname[MAX_PATH_LEN]; input file name */
/*char ofname[MAX_PATH_LEN]; output file name */
remove_ofname = 0; /* remove output file on error */
/*struct stat istat; status for input file */
ifd=0; /* input file descriptor */
ofd=0; /* output file descriptor */
insize=0; /* valid bytes in inbuf */
inptr=0; /* index of next byte to be processed in inbuf */
outcnt=0; /* bytes in output buffer */
//DECLARE(uch, inbuf, INBUFSIZ +INBUF_EXTRA);
//DECLARE(uch, window, 2L*WSIZE);
#ifdef DYN_ALLOC
inbuf=NULL;
window=NULL;
#else
memset(inbuf,0,INBUFSIZ+INBUF_EXTRA);
memset(window,0,2L*WSIZE);
#endif
/*unsigned char *window = NULL;*/ /**/
/*uch *inbuf=NULL; */ /* input buffer */
header_bytes=0; /**/
//int uncompress();
eof_flag=0;
// int (*work) OF((int infile, int outfile)); /* function to call */
work=NULL;
get_byte_divid = -1,get_byte_filenumber=0;
get_byte_inner_fname=NULL;
}
#endif /* DLL */