home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 2001 January
/
VPR0101A.BIN
/
OLS
/
TAR32053
/
tar32053.exe
/
SRC
/
UNZIP.C
< prev
next >
Wrap
C/C++ Source or Header
|
1999-05-23
|
5KB
|
154 lines
#ifndef __DEFCONF_H
#include "defconf.h"
#endif
/*
This file was hacked for kmtar for WIN32
at 1996-05-06.
by tantan SGL00213@niftyserve.or.jp
*/
/* unzip.c -- decompress files in gzip or pkzip format.
* Copyright (C) 1992-1993 Jean-loup Gailly
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License, see the file COPYING.
*
* The code in this file is derived from the file funzip.c written
* and put in the public domain by Mark Adler.
*/
/*
This version can extract files in gzip or pkzip format.
For the latter, only the first entry is extracted, and it has to be
either deflated or stored.
*/
#ifdef RCSID
static char rcsid[] = "$Id: unzip.c,v 0.13 1993/06/10 13:29:00 jloup Exp $";
#endif
#include "tailor.h"
#include "gzip.h"
#include "misc.h"
#include "main.h" /* for Exitcode */
#include "tar32.h" /* for ERROR constance */
/* PKZIP header definitions */
#define LOCSIG 0x04034b50L /* four-byte lead-in (lsb first) */
#define LOCFLG 6 /* offset of bit flag */
#define CRPFLG 1 /* bit for encrypted entry */
#define EXTFLG 8 /* bit for extended local header */
#define LOCHOW 8 /* offset of compression method */
#define LOCTIM 10 /* file mod time (for decryption) */
#define LOCCRC 14 /* offset of crc */
#define LOCSIZ 18 /* offset of compressed size */
#define LOCLEN 22 /* offset of uncompressed length */
#define LOCFIL 26 /* offset of file name field length */
#define LOCEXT 28 /* offset of extra field length */
#define LOCHDR 30 /* size of local header, including sig */
#define EXTHDR 16 /* size of extended local header, inc sig */
/* Globals */
int decrypt; /* flag to turn on decryption */
char *key; /* not used--needed to link crypt.c */
int pkzip = 0; /* set for a pkzip file */
int ext_header = 0; /* set if extended local header */
/* ===========================================================================
* Unzip in to out. This routine works on both gzip and pkzip files.
*
* IN assertions: the buffer inbuf contains already the beginning of
* the compressed data, from offsets inptr to insize-1 included.
* The magic header has already been checked. The output buffer is cleared.
*/
static int entry_flag=0; /* count entry number */
int unzip(void)
{
ulg orig_crc = 0; /* original crc */
ulg orig_len = 0; /* original uncompressed length */
int n,res,e;
uch buf[EXTHDR]; /* extended local header */
uch outbuf[4];
extern void inflate_init(void);
extern int _inflate(int *e);
extern void inflate_end(void);
#if 0
if (pkzip && !ext_header) { /* crc and length at the end otherwise */
orig_crc = LG(inbuf + LOCCRC);
orig_len = LG(inbuf + LOCLEN);
}
#endif
if (method == -1 && entry_flag == -1)
return EOF;
/* Decompress */
if (method != DEFLATED)
fatal("unzip","internal error, invalid method");
if (entry_flag == 0){
updcrc(NULL, 0); /* initialize crc */
inflate_init();
entry_flag = 1;
}
if (entry_flag == -1)
inflate_end();
else{
res = _inflate(&e);
if (res == 3)
fatal("unzip","out of memory");
else if (res != 0)
fatal("unzip","invalid compressed data--format violated");
if (!e)
return OK;
entry_flag = -1;
return OK;
}
/* Get the crc and original length */
if (!pkzip) {
/* crc32 (see algorithm.doc)
* uncompressed input size modulo 2^32
*/
int ret;
for (n = 0; n < 8; n++) {
ret = get_byte();
buf[n] = (uch)ret;
/* buf[n] = (uch)get_byte();*/ /* may cause an error if EOF */
}
if(ret == EOF){
Exitcode = ERROR_CANNOT_READ;
return EOF;
}
orig_crc = LG(buf);
orig_len = LG(buf+4);
}
/* Validate decompression */
if (orig_crc != updcrc(outbuf, 0)) {
fatal("unzip","invalid compressed data--crc error");
}
if (orig_len != (ulg)bytes_out) {
fatal("unzip","invalid compressed data--length error");
}
ext_header = pkzip = 0; /* for next file */
return OK;
}
void init_unzip(void)
{
clear_bufs();
entry_flag=0; /* count entry number */
}