home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 1998 June
/
Vpr9806a.iso
/
OLS
/
Windows
/
TAR32031
/
tar32031.exe
/
SRC
/
SRC
/
COMPRESS.C
< prev
next >
Wrap
C/C++ Source or Header
|
1998-01-15
|
12KB
|
261 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
*/
/*@H************************ < COMPRESS utility> ****************************
* $@(#) compress.c,v 4.3d 90/01/18 03:00:00 don Release ^ *
* *
* compress : compress.c *
* Main and Operating System Independent support functions *
* *
* port by : Donald J. Gloistein *
* *
* Source, Documentation, Object Code: *
* released to Public Domain. This code is ported from compress v4.0 *
* release joe. *
*--------------------------- Module Description --------------------------*
* The compress program is compatible with the compression/decompression *
* used on the Unix systems compress programs. This is version 4 and *
* supports up to 16 bits compression. The porting retained the Unix *
* meanings of all options, added a couple for MsDos and modified the *
* file name conventions to make more sense. *
* *
*--------------------------- Implementation Notes --------------------------*
* *
* compiled with : compress.h compress.fns *
* linked with : compapi.obj compusi.obj *
* problems: *
* See notes in compress.h for defines needed. *
* It should work now with Xenix *
* *
* Check the signal() handler functions in your compiler *
* documentation. This code assumes ANSI SYS V compatible *
* header and return values. Change as appropriate for your *
* compiler and operating system. *
* *
* This source compiles properly with Microsoft C compiler *
* version 5.1. *
* *
* CAUTION: because the program is in modules, make sure you recompile *
* all modules if you change the header or a define in the *
* compress.c file *
* *
* Algorithm from "A Technique for High Performance Data Compression", *
* Terry A. Welch, IEEE Computer Vol 17, No 6 (June 1984), pp 8-19. *
* *
* Assumptions: *
* When filenames are given, replaces with the compressed version *
* (.Z suffix) only if the file decreases in size. *
* Algorithm: *
* Modified Lempel-Ziv method (LZW). Basically finds common *
* substrings and replaces them with a variable size code. This is *
* deterministic, and can be done on the fly. Thus, the decompression *
* procedure needs no input table, but tracks the way the table was built. *
* *
* *
*--------------------------- Author(s) -------------------------*
* Initials ---- Name --------------------------------- *
* DjG Donald J. Gloistein *
* Plus many others, see rev.hst file for full list *
* Dal Dale A. Schumacher (Sozobon C port) *
* LvR Lyle V. Rains, many thanks for improved implementation *
*************************************************************************@H*/
/*@R************************< Revision History >*****************************
* *
* version -- date -- init ---Notes---------------------- *
* 4.01 08-29-88 DjG first cut for 16 bit MsDos version *
* 09-04-88 DjG fixed unlink on zcat if interupted. *
* added msdos filename logic and functions *
* 4.10 10-27-88 DjG revised API with coding changes by LvR. *
* 4.10a 10-30-88 DjG cleaned up code and fixed bug in freeing ptr. *
* 4.10b 11-01-88 DjG cleaned up the logic for inpath/outpath *
* Changed the logic to finding the file name *
* Fixed the allocation bug in the api *
* Added some more portability macros *
* 4.10c 11-04-88 DjG Changed maxcode from global to static in api. *
* Supplied some library functions for those who *
* don't have them, changed dos usi to use the *
* strrpbrk(). Checked casts in api again. Compiles*
* without warnings at pick level 3. *
* 4.10d 11-25-88 DjG revised some memory allocation, put more in the *
* header file. Corrected some typos. *
* Changed prog_name() to force lower case *
* Corrected bug, no longer unlinks existing file *
* if not enough memory to compress or decompress *
* 12-06-88 DjG VERY minor changes for casts and header defines *
* 12-08-88 DjG Adjusted path separator check in main function *
* Amiga uses split seg because of compiler *
* 12-09-88 DjG Debugging done, all defaults now Unix compress *
* defaults, including unlinking input file and *
* acting as a filter. Must use -h option to get *
* help screen. *
* 4.10e 12-11-88 DjG Fixed more casts, prototypes and header file. *
* 4.10f 12-12-88 DjG Fixed unlinking open files on error. This fails *
* on shared or os/2 platforms. *
* 12-15-88 DjG Fixed SIGTYPE for function passed to signal *
* Fixed problems with Xenix 2.2.1 *
* 4.2 12-19-88 DjG Replaced adaptive reset as an option. *
* 4.3 12-26-88 DjG Fixed long file name bug, fixed bug with *
* compressdir. -B option added, same as -b option *
* 05-06-89 Dal Ported to Sozobon/Alcyon C for Atari ST. Also, *
* created get_one() for console prompting. *
* 05-08-89 Dal Ported to Minix-ST *
* 4.3a 05-29-89 DjG Combined source code changes and now compiles *
* on Minix. *
* 4.3b 08-20-89 DjG Changed the version() to simplify it. Changed *
* the order of testing for linked files *
* 10-02-89 DjG Changed the double negative #ifndef NDEBUG *
* to a more logical coding. *
* 4.3c 12-25-89 DjG Fixed pointer bug in error message code *
* 01-06-90 LvR Fixed signed expansion on 68000 cpu's *
* 4.3d 01-18-90 LvR Fixed problem with token[] overrunning on some *
* files with large amounts of repeating characters*
*************************************************************************@R*/
#include <stdio.h>
#define MAIN /* header has defining instances of globals */
#include "compress.h" /* contains the rest of the include file declarations */
#include "misc.h"
#include "gzip.h"
#define ARGVAL() (*++(*argv) || (--argc && *++argv))
char suffix[] = SUFFIX ; /* only used in this file */
int open_infile(char *in_name);
extern char divi_flag;
extern int Afd;
FILE *Afp;
#if 0
int main(int argc,char **argv)
{
int chk_divi(char *name);
int ret;
char rets[10];
/* set up array for files to be converted */
if (argc < 3)
exit(1);
if (open_infile(argv[1]) == NO)
exit(2);
if (open_outfile(argv[2]) == NO)
exit(2);
divi_flag = chk_divi(argv[1]);
if ((ret = de_comp_init()) != OK)
return ret;
while(de_comp() != EOF)
;
/* fprintf(stderr,"SET next file[%s] and Hit Ret key\n",next_name(ifname));
gets(rets);*/
de_comp_end();
check_error();
return exit_stat;
}
#endif
int de_comp_init(void)
{
void de_comp_init2(void);
void close_arch(char c);
/* assume zcat */
keep = TRUE;
zcat_flg = do_decomp = 1;
maxbits = get_byte(); /* set -b from file */
block_compress = maxbits & BLOCK_MASK;
maxbits &= BIT_MASK;
if(maxbits > MAXBITS) {
fprintf(stderr," compressed with %d bits, can only handle %d bits\n",
maxbits, MAXBITS);
return GZIP_ERROR;
}
de_comp_init2();
return OK;
}
/*
* All previous #ifdef'ed code should return() a value.
* If no other option is available, the following is the original code.
* It not only reads from stderr (not a defined operation)
* but it does so via an explicit read() call on file descriptor 2!
* So much for portability. -Dal
*/
/* Dal included code to use the MSC getche() but it crashes because of
the freopening of stdin. Have to use the cludge for MSC. This function is
included so that others who have problems with the keyboard read can
change this function for their system. DjG
*/
ALLOCTYPE FAR *emalloc(x,y)
unsigned int x;
int y;
{
ALLOCTYPE FAR *p;
p = (ALLOCTYPE FAR *)ALLOCATE(x,y);
return(p);
}
void efree(ptr)
ALLOCTYPE FAR *ptr;
{
FREEIT(ptr);
}
/*
* read by get_byf
*/
int v_read(unsigned char *buf,int len)
{
int i,c;
for(i=0;i<len;i++,buf++){
if ((c = get_byte()) == EOF){
break;
}
*buf = c;
}
return i;
}
static int entry_flag=0;
int uncompress(void)
{
/* extern unsigned char *window;*/
if (entry_flag == 0){
de_comp_init();
entry_flag = 1;
}else if(entry_flag == -1)
return EOF;
if (de_comp() == OK)
return OK;
entry_flag = -1;
flush_window();
return OK;
}
/*
* call by archio.c
*/
void init_comp(void)
{
entry_flag=0;
}