📄 compress.c
字号:
if( fwrite( buf, 1, n_bits, stdout ) != n_bits)
writeerr();
bytes_out += n_bits;
}
offset = 0;
if ( clear_flg ) {
maxcode = MAXCODE (n_bits = INIT_BITS);
clear_flg = 0;
}
else {
n_bits++;
if ( n_bits == maxbits )
maxcode = maxmaxcode;
else
maxcode = MAXCODE(n_bits);
}
#ifdef DEBUG
if ( debug ) {
fprintf( stderr, "\nChange to %d bits\n", n_bits );
col = 0;
}
#endif /* DEBUG */
}
} else {
/*
* At EOF, write the rest of the buffer.
*/
if ( offset > 0 )
fwrite( buf, 1, (offset + 7) / 8, stdout );
bytes_out += (offset + 7) / 8;
offset = 0;
fflush( stdout );
#ifdef DEBUG
if ( verbose )
fprintf( stderr, "\n" );
#endif /* DEBUG */
if( ferror( stdout ) )
writeerr();
}
}
/*
* Decompress stdin to stdout. This routine adapts to the codes in the
* file building the "string" table on-the-fly; requiring no table to
* be stored in the compressed file. The tables used herein are shared
* with those of the compress() routine. See the definitions above.
*/
decompress() {
register char_type *stackp;
register int finchar;
register code_int code, oldcode, incode;
/*
* As above, initialize the first 256 entries in the table.
*/
maxcode = MAXCODE(n_bits = INIT_BITS);
for ( code = 255; code >= 0; code-- ) {
tab_prefixof(code) = 0;
tab_suffixof(code) = (char_type)code;
}
free_ent = ((block_compress) ? FIRST : 256 );
finchar = oldcode = getcode();
if(oldcode == -1) /* EOF already? */
return; /* Get out of here */
putchar( (char)finchar ); /* first code must be 8 bits = char */
if(ferror(stdout)) /* Crash if can't write */
writeerr();
stackp = de_stack;
while ( (code = getcode()) > -1 ) {
if ( (code == CLEAR) && block_compress ) {
for ( code = 255; code >= 0; code-- )
tab_prefixof(code) = 0;
clear_flg = 1;
free_ent = FIRST - 1;
if ( (code = getcode ()) == -1 ) /* O, untimely death! */
break;
}
incode = code;
/*
* Special case for KwKwK string.
*/
if ( code >= free_ent ) {
*stackp++ = finchar;
code = oldcode;
}
/*
* Generate output characters in reverse order
*/
#ifdef SIGNED_COMPARE_SLOW
while ( ((unsigned long)code) >= ((unsigned long)256) ) {
#else
while ( code >= 256 ) {
#endif
*stackp++ = tab_suffixof(code);
code = tab_prefixof(code);
}
*stackp++ = finchar = tab_suffixof(code);
/*
* And put them out in forward order
*/
do
putchar ( *--stackp );
while ( stackp > de_stack );
/*
* Generate the new entry.
*/
if ( (code=free_ent) < maxmaxcode ) {
tab_prefixof(code) = (unsigned short)oldcode;
tab_suffixof(code) = finchar;
free_ent = code+1;
}
/*
* Remember previous code.
*/
oldcode = incode;
}
fflush( stdout );
if(ferror(stdout))
writeerr();
}
/*****************************************************************
* TAG( getcode )
*
* Read one code from the standard input. If EOF, return -1.
* Inputs:
* stdin
* Outputs:
* code or -1 is returned.
*/
code_int
getcode() {
/*
* On the VAX, it is important to have the register declarations
* in exactly the order given, or the asm will break.
*/
register code_int code;
static int offset = 0, size = 0;
static char_type buf[BITS];
register int r_off, bits;
register char_type *bp = buf;
if ( clear_flg > 0 || offset >= size || free_ent > maxcode ) {
/*
* If the next entry will be too big for the current code
* size, then we must increase the size. This implies reading
* a new buffer full, too.
*/
if ( free_ent > maxcode ) {
n_bits++;
if ( n_bits == maxbits )
maxcode = maxmaxcode; /* won't get any bigger now */
else
maxcode = MAXCODE(n_bits);
}
if ( clear_flg > 0) {
maxcode = MAXCODE (n_bits = INIT_BITS);
clear_flg = 0;
}
size = fread( buf, 1, n_bits, stdin );
if ( size <= 0 )
return -1; /* end of file */
offset = 0;
/* Round size down to integral number of codes */
size = (size << 3) - (n_bits - 1);
}
r_off = offset;
bits = n_bits;
#ifdef vax
asm( "extzv r10,r9,(r8),r11" );
#else /* not a vax */
/*
* Get to the first byte.
*/
bp += (r_off >> 3);
r_off &= 7;
/* Get first part (low order bits) */
#ifdef NO_UCHAR
code = ((*bp++ >> r_off) & rmask[8 - r_off]) & 0xff;
#else
code = (*bp++ >> r_off);
#endif /* NO_UCHAR */
bits -= (8 - r_off);
r_off = 8 - r_off; /* now, offset into code word */
/* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
if ( bits >= 8 ) {
#ifdef NO_UCHAR
code |= (*bp++ & 0xff) << r_off;
#else
code |= *bp++ << r_off;
#endif /* NO_UCHAR */
r_off += 8;
bits -= 8;
}
/* high order bits. */
code |= (*bp & rmask[bits]) << r_off;
#endif /* vax */
offset += n_bits;
return code;
}
char *
rindex(s, c) /* For those who don't have it in libc.a */
register char *s, c;
{
char *p;
for (p = NULL; *s; s++)
if (*s == c)
p = s;
return(p);
}
#ifdef DEBUG
printcodes()
{
/*
* Just print out codes from input file. For debugging.
*/
code_int code;
int col = 0, bits;
bits = n_bits = INIT_BITS;
maxcode = MAXCODE(n_bits);
free_ent = ((block_compress) ? FIRST : 256 );
while ( ( code = getcode() ) >= 0 ) {
if ( (code == CLEAR) && block_compress ) {
free_ent = FIRST - 1;
clear_flg = 1;
}
else if ( free_ent < maxmaxcode )
free_ent++;
if ( bits != n_bits ) {
fprintf(stderr, "\nChange to %d bits\n", n_bits );
bits = n_bits;
col = 0;
}
fprintf(stderr, "%5d%c", code, (col+=6) >= 74 ? (col = 0, '\n') : ' ' );
}
putc( '\n', stderr );
exit( 0 );
}
code_int sorttab[1<<BITS]; /* sorted pointers into htab */
dump_tab() /* dump string table */
{
register int i, first;
register ent;
#define STACK_SIZE 15000
int stack_top = STACK_SIZE;
register c;
if(do_decomp == 0) { /* compressing */
register int flag = 1;
for(i=0; i<hsize; i++) { /* build sort pointers */
if((long)htabof(i) >= 0) {
sorttab[codetabof(i)] = i;
}
}
first = block_compress ? FIRST : 256;
for(i = first; i < free_ent; i++) {
fprintf(stderr, "%5d: \"", i);
de_stack[--stack_top] = '\n';
de_stack[--stack_top] = '"';
stack_top = in_stack((htabof(sorttab[i])>>maxbits)&0xff,
stack_top);
for(ent=htabof(sorttab[i]) & ((1<<maxbits)-1);
ent > 256;
ent=htabof(sorttab[ent]) & ((1<<maxbits)-1)) {
stack_top = in_stack(htabof(sorttab[ent]) >> maxbits,
stack_top);
}
stack_top = in_stack(ent, stack_top);
fwrite( &de_stack[stack_top], 1, STACK_SIZE-stack_top, stderr);
stack_top = STACK_SIZE;
}
} else if(!debug) { /* decompressing */
for ( i = 0; i < free_ent; i++ ) {
ent = i;
c = tab_suffixof(ent);
if ( isascii(c) && isprint(c) )
fprintf( stderr, "%5d: %5d/'%c' \"",
ent, tab_prefixof(ent), c );
else
fprintf( stderr, "%5d: %5d/\\%03o \"",
ent, tab_prefixof(ent), c );
de_stack[--stack_top] = '\n';
de_stack[--stack_top] = '"';
for ( ; ent != NULL;
ent = (ent >= FIRST ? tab_prefixof(ent) : NULL) ) {
stack_top = in_stack(tab_suffixof(ent), stack_top);
}
fwrite( &de_stack[stack_top], 1, STACK_SIZE - stack_top, stderr );
stack_top = STACK_SIZE;
}
}
}
int
in_stack(c, stack_top)
register c, stack_top;
{
if ( (isascii(c) && isprint(c) && c != '\\') || c == ' ' ) {
de_stack[--stack_top] = c;
} else {
switch( c ) {
case '\n': de_stack[--stack_top] = 'n'; break;
case '\t': de_stack[--stack_top] = 't'; break;
case '\b': de_stack[--stack_top] = 'b'; break;
case '\f': de_stack[--stack_top] = 'f'; break;
case '\r': de_stack[--stack_top] = 'r'; break;
case '\\': de_stack[--stack_top] = '\\'; break;
default:
de_stack[--stack_top] = '0' + c % 8;
de_stack[--stack_top] = '0' + (c / 8) % 8;
de_stack[--stack_top] = '0' + c / 64;
break;
}
de_stack[--stack_top] = '\\';
}
return stack_top;
}
#endif /* DEBUG */
writeerr()
{
perror ( ofname );
if (valid)
unlink ( ofname );
exit ( 1 );
}
copystat(ifname, ofname)
char *ifname, *ofname;
{
int mode;
time_t timep[2];
fclose(stdin);
fclose(stdout);
if (exit_stat == 2 && (!force)) { /* No compression: remove file.Z */
if(!quiet)
fprintf(stderr, "No compression -- %s unchanged",
ifname);
} else { /* ***** Successful Compression ***** */
exit_stat = 0;
mode = insbuf.st_mode & 07777;
if (chmod(ofname, mode)) /* Copy modes */
perror(ofname);
#ifndef OS2
chown(ofname, insbuf.st_uid, insbuf.st_gid); /* Copy ownership */
#endif
timep[0] = insbuf.st_atime;
timep[1] = insbuf.st_mtime;
utime(ofname, timep); /* Update last accessed and modified times */
valid = 0; /* prevent latent ofname removal */
if (unlink(ifname)) /* Remove input file */
perror(ifname);
if(!quiet)
fprintf(stderr, " -- replaced with %s",ofname);
return; /* Successful return */
}
/* Unsuccessful return -- one of the tests failed */
if (unlink(ofname))
perror(ofname);
}
/*
* This routine returns 1 if we are running in the foreground and stderr
* is a tty.
*/
foreground()
{
if(bgnd_flag) { /* background? */
return(0);
} else { /* foreground */
if(isatty(2)) { /* and stderr is a tty */
return(1);
} else {
return(0);
}
}
}
SIGTYP onintr ( )
{
if (valid)
unlink ( ofname );
exit ( 1 );
}
SIGTYP oops ( ) /* wild pointer -- assume bad input */
{
if ( do_decomp == 1 )
fprintf ( stderr, "uncompress: corrupt input\n" );
if (valid)
unlink ( ofname );
exit ( 1 );
}
cl_block () /* table clear for block compress */
{
register long int rat;
checkpoint = in_count + CHECK_GAP;
#ifdef DEBUG
if ( debug ) {
fprintf ( stderr, "count: %ld, ratio: ", in_count );
prratio ( stderr, in_count, bytes_out );
fprintf ( stderr, "\n");
}
#endif /* DEBUG */
if(in_count > 0x007fffff) { /* shift will overflow */
rat = bytes_out >> 8;
if(rat == 0) { /* Don't divide by zero */
rat = 0x7fffffff;
} else {
rat = in_count / rat;
}
} else {
rat = (in_count << 8) / bytes_out; /* 8 fractional bits */
}
if ( rat > ratio ) {
ratio = rat;
} else {
ratio = 0;
#ifdef DEBUG
if(verbose)
dump_tab(); /* dump string table */
#endif
cl_hash ( (count_int) hsize );
free_ent = FIRST;
clear_flg = 1;
output ( (code_int) CLEAR );
#ifdef DEBUG
if(debug)
fprintf ( stderr, "clear\n" );
#endif /* DEBUG */
}
}
cl_hash(hsize) /* reset code table */
register count_int hsize;
{
#ifndef XENIX_16 /* Normal machine */
register count_int *htab_p = htab+hsize;
#else
register j;
register long k = hsize;
register count_int *htab_p;
#endif
register long i;
register long m1 = -1;
#ifdef XENIX_16
for(j=0; j<=8 && k>=0; j++,k-=8192) {
i = 8192;
if(k < 8192) {
i = k;
}
htab_p = &(htab[j][i]);
i -= 16;
if(i > 0) {
#else
i = hsize - 16;
#endif
do { /* might use Sys V memset(3) here */
*(htab_p-16) = m1;
*(htab_p-15) = m1;
*(htab_p-14) = m1;
*(htab_p-13) = m1;
*(htab_p-12) = m1;
*(htab_p-11) = m1;
*(htab_p-10) = m1;
*(htab_p-9) = m1;
*(htab_p-8) = m1;
*(htab_p-7) = m1;
*(htab_p-6) = m1;
*(htab_p-5) = m1;
*(htab_p-4) = m1;
*(htab_p-3) = m1;
*(htab_p-2) = m1;
*(htab_p-1) = m1;
htab_p -= 16;
} while ((i -= 16) >= 0);
#ifdef XENIX_16
}
}
#endif
for ( i += 16; i > 0; i-- )
*--htab_p = m1;
}
prratio(stream, num, den)
FILE *stream;
long int num, den;
{
register int q; /* Doesn't need to be long */
if(num > 214748L) { /* 2147483647/10000 */
q = num / (den / 10000L);
} else {
q = 10000L * num / den; /* Long calculations, though */
}
if (q < 0) {
putc('-', stream);
q = -q;
}
fprintf(stream, "%d.%02d%%", q / 100, q % 100);
}
version()
{
fprintf(stderr, "%s, patchlevel %d\n", version_id, PATCHLEVEL);
fprintf(stderr, "Options: ");
#ifdef vax
fprintf(stderr, "vax, ");
#endif
#ifdef SHORTNAMES
fprintf(stderr,"SHORTNAMES, ");
#endif
#ifdef VOIDSIG
fprintf(stderr,"VOIDSIG, ");
#endif
#ifdef DIRENT
fprintf(stderr,"DIRENT, ");
#endif
#ifdef NO_UCHAR
fprintf(stderr, "NO_UCHAR, ");
#endif
#ifdef SIGNED_COMPARE_SLOW
fprintf(stderr, "SIGNED_COMPARE_SLOW, ");
#endif
#ifdef XENIX_16
fprintf(stderr, "XENIX_16, ");
#endif
#ifdef COMPATIBLE
fprintf(stderr, "COMPATIBLE, ");
#endif
#ifdef DEBUG
fprintf(stderr, "DEBUG, ");
#endif
#ifdef BSD4
fprintf(stderr, "BSD4, ");
#endif
fprintf(stderr, "BITS = %d\n", BITS);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -