home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sources.misc
- From: leo@ipmce.su (Leonid A. Broukhis)
- Subject: v34i127: freeze - Freeze/melt compression program vers. 2.4, Part03/03
- Message-ID: <1993Jan19.043628.29675@sparky.imd.sterling.com>
- X-Md4-Signature: 69c593040bb2be9fc06cad59c79ae8de
- Date: Tue, 19 Jan 1993 04:36:28 GMT
- Approved: kent@sparky.imd.sterling.com
-
- Submitted-by: leo@ipmce.su (Leonid A. Broukhis)
- Posting-number: Volume 34, Issue 127
- Archive-name: freeze/part03
- Environment: ISC, Xenix, SunOS, MS-DOS
- Supersedes: freeze: Volume 25, Issue 12-13
-
- #! /bin/sh
- # This is a shell archive. Remove anything before this line, then feed it
- # into a shell via "sh file" or similar. To overwrite existing files,
- # type "sh file -c".
- # Contents: bitio.c bitio.h compat.h debug.c decode.c huf.h patchlev.h
- # showhuf.c
- # Wrapped by kent@sparky on Mon Jan 18 22:27:49 1993
- PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:/usr/lbin ; export PATH
- echo If this archive is complete, you will see the following message:
- echo ' "shar: End of archive 3 (of 3)."'
- if test -f 'bitio.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'bitio.c'\"
- else
- echo shar: Extracting \"'bitio.c'\" \(1429 characters\)
- sed "s/^X//" >'bitio.c' <<'END_OF_FILE'
- X#include "freeze.h"
- X#include "bitio.h"
- X
- Xul_t getbuf = 0; /* assume sizeof (ul_t) >= 4 */
- Xus_t putbuf;
- Xuc_t bitlen = 0, __, crpt_flag = 0;
- X
- X/* get N bits (N <= 16), returning in Bit(N-1)...Bit 0 */
- X
- Xshort GetNBits (n)
- X register us_t n;
- X{
- X register ul_t dx = getbuf;
- X register uc_t c;
- X
- X static us_t mask[17] = {
- X 0x0000,
- X 0x0001, 0x0003, 0x0007, 0x000f,
- X 0x001f, 0x003f, 0x007f, 0x00ff,
- X 0x01ff, 0x03ff, 0x07ff, 0x0fff,
- X 0x1fff, 0x3fff, 0x7fff, 0xffff };
- X
- X while (bitlen < n)
- X {
- X c = getchar ();
- X dx |= (ul_t) c << (BYSH - bitlen);
- X bitlen += 8;
- X }
- X crpt_flag = feof(stdin);
- X getbuf = dx << n;
- X bitlen -= n;
- X return (dx >> (bits(getbuf) - n)) & mask[n];
- X}
- X
- X/* output `l' high bits of `c' */
- X
- Xvoid Putcode (l, c)
- X register int l;
- X us_t c;
- X{
- X register us_t len = bitlen;
- X register us_t b = (us_t)putbuf;
- X b |= c >> len;
- X if ((len += l) >= 8) {
- X putchar ((int)(b >> 8));
- X if ((len -= 8) >= 8) {
- X putchar ((int)b);
- X bytes_out += 2;
- X len -= 8;
- X b = c << (l - len);
- X } else {
- X b <<= 8;
- X bytes_out++;
- X }
- X }
- X if (ferror(stdout))
- X writeerr();
- X putbuf = b;
- X bitlen = len;
- X}
- X
- X
- X/* Flushes the bit I/O buffers and check the state of stdout */
- X
- Xvoid EncodeEnd ()
- X{
- X if (bitlen) {
- X putchar((int)(putbuf >> 8));
- X bytes_out++;
- X if (ferror(stdout))
- X writeerr();
- X }
- X}
- X
- X/* File too short or invalid header, print a message */
- X
- Xvoid crpt_message ( )
- X{
- X fprintf ( stderr, "melt: corrupt input\n" );
- X}
- X
- END_OF_FILE
- if test 1429 -ne `wc -c <'bitio.c'`; then
- echo shar: \"'bitio.c'\" unpacked with wrong size!
- fi
- # end of 'bitio.c'
- fi
- if test -f 'bitio.h' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'bitio.h'\"
- else
- echo shar: Extracting \"'bitio.h'\" \(915 characters\)
- sed "s/^X//" >'bitio.h' <<'END_OF_FILE'
- X/* Some definitions for faster bit-level I/O */
- X
- Xextern ul_t getbuf; /* Bit I/O buffers */
- Xextern us_t putbuf;
- Xextern uc_t bitlen; /* Number of bits actually in `???buf' */
- X
- Xextern uc_t crpt_flag; /* 1 == EOF was read when melting */
- Xextern uc_t __; /* temporary variable for GetBit/Byte */
- X
- Xextern void EncodeEnd(), Putcode(), crpt_message();
- X
- X#define bits(x) ((int)sizeof(x)*8)
- X#define BYSH (bits(getbuf)-8)
- X#define BISH (bits(getbuf)-1)
- X
- X#define GetByte() (bitlen >= 8 ? (__ = getbuf >> BYSH, bitlen -= 8,\
- X getbuf <<= 8, __) : (getbuf |= (ul_t) \
- X (getchar() & 0xFF) << (BYSH - bitlen), __ = getbuf\
- X >> BYSH, getbuf <<= 8, __))
- X
- X#define GetBit() (bitlen ? (__ = getbuf >> BISH, bitlen--, \
- X getbuf <<= 1, __) : (getbuf = __ = getchar(), \
- X getbuf <<= BYSH + 1, bitlen = 7, __ >> 7))
- X
- X#define InitIO() { crpt_flag = bitlen = 0; putbuf = 0; getbuf = 0; }
- END_OF_FILE
- if test 915 -ne `wc -c <'bitio.h'`; then
- echo shar: \"'bitio.h'\" unpacked with wrong size!
- fi
- # end of 'bitio.h'
- fi
- if test -f 'compat.h' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'compat.h'\"
- else
- echo shar: Extracting \"'compat.h'\" \(183 characters\)
- sed "s/^X//" >'compat.h' <<'END_OF_FILE'
- Xextern uc_t Table1[];
- X
- X#define F1 60
- X#define N1 4096
- X#define N_CHAR1 (256 - THRESHOLD + F1 + 1)
- X
- Xextern short DecodePOld();
- X
- Xextern void melt1();
- END_OF_FILE
- if test 183 -ne `wc -c <'compat.h'`; then
- echo shar: \"'compat.h'\" unpacked with wrong size!
- fi
- # end of 'compat.h'
- fi
- if test -f 'debug.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'debug.c'\"
- else
- echo shar: Extracting \"'debug.c'\" \(1547 characters\)
- sed "s/^X//" >'debug.c' <<'END_OF_FILE'
- X#if defined(DEBUG)
- X#include "freeze.h"
- X#include "huf.h"
- X#include "bitio.h"
- X
- X /*---------------------------*/
- X /* DEBUG MODULE */
- X /*---------------------------*/
- X
- Xprintcodes(mode)
- X{
- X /*
- X * Just print out codes from input file. For debugging.
- X */
- X register short k, c, col = 0;
- X
- X#ifdef COMPAT
- X if (!mode) {
- X StartHuff(N_CHAR1);
- X init(Table1);
- X } else
- X#endif
- X {
- X if (read_header() == EOF) {
- X fprintf(stderr, "Bad header\n");
- X return;
- X }
- X StartHuff(N_CHAR2);
- X init(Table2);
- X }
- X
- X InitIO();
- X
- X for (;;) {
- X
- X if((c = DecodeChar()) == ENDOF)
- X break;
- X if (c < 256) {
- X fprintf(stderr, "%5d%c", c,
- X (col+=8) >= 74 ? (col = 0, '\n') : '\t' );
- X } else {
- X c = c - 256 + THRESHOLD;
- X
- X k = DecodePosition();
- X
- X fprintf(stderr, "%2d-%d%c", c, k,
- X (col+=8) >= 74 ? (col = 0, '\n') : '\t' );
- X }
- X }
- X putc( '\n', stderr );
- X exit( 0 );
- X}
- X
- X/* for pretty char printing */
- X
- Xchar *
- Xpr_char(c)
- X register uc_t c;
- X{
- X static char buf[5];
- X register i = 4;
- X buf[4] = '\0';
- X if ( (isascii((int)c) && isprint((int)c) && c != '\\') || c == ' ' ) {
- X buf[--i] = c;
- X } else {
- X switch( c ) {
- X case '\n': buf[--i] = 'n'; break;
- X case '\t': buf[--i] = 't'; break;
- X case '\b': buf[--i] = 'b'; break;
- X case '\f': buf[--i] = 'f'; break;
- X case '\r': buf[--i] = 'r'; break;
- X case '\\': buf[--i] = '\\'; break;
- X default:
- X buf[--i] = '0' + c % 8;
- X buf[--i] = '0' + (c / 8) % 8;
- X buf[--i] = '0' + c / 64;
- X break;
- X }
- X buf[--i] = '\\';
- X }
- X return &buf[i];
- X}
- X#endif
- END_OF_FILE
- if test 1547 -ne `wc -c <'debug.c'`; then
- echo shar: \"'debug.c'\" unpacked with wrong size!
- fi
- # end of 'debug.c'
- fi
- if test -f 'decode.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'decode.c'\"
- else
- echo shar: Extracting \"'decode.c'\" \(1998 characters\)
- sed "s/^X//" >'decode.c' <<'END_OF_FILE'
- X#include "freeze.h"
- X#include "huf.h"
- X#include "bitio.h"
- X
- X/*
- X * Melt stdin to stdout.
- X */
- X
- Xvoid melt2 ()
- X{
- X register short i, j, k, r, c;
- X
- X/* Huffman-dependent part */
- X if(read_header() == EOF)
- X return;
- X StartHuff(N_CHAR2);
- X init(Table2);
- X/* end of Huffman-dependent part */
- X
- X InitIO();
- X for (i = 0; i < MAXDIST; i++)
- X text_buf[i] = ' ';
- X r = MAXDIST;
- X for (in_count = 0;; ) {
- X c = DecodeChar();
- X
- X if (c == ENDOF)
- X break;
- X if (c < 256) {
- X#ifdef DEBUG
- X if (debug)
- X fprintf(stderr, "'%s'\n", pr_char((uc_t)c));
- X else
- X#endif /* DEBUG */
- X putchar (c);
- X text_buf[r++] = c;
- X r &= WINMASK;
- X in_count++;
- X } else {
- X i = (r - DecodePosition() - 1) & WINMASK;
- X j = c - 256 + THRESHOLD;
- X#ifdef DEBUG
- X if (debug)
- X fputc('"', stderr);
- X#endif
- X for (k = 0; k < j; k++) {
- X c = text_buf[(i + k) & WINMASK];
- X#ifdef DEBUG
- X if (debug)
- X fprintf(stderr, "%s", pr_char((uc_t)c));
- X else
- X#endif
- X putchar (c);
- X text_buf[r++] = c;
- X r &= WINMASK;
- X in_count++;
- X }
- X#ifdef DEBUG
- X if (debug)
- X fprintf(stderr, "\"\n");
- X#endif
- X }
- X INDICATOR
- X }
- X}
- X
- X#ifdef COMPAT
- Xvoid melt1 ()
- X{
- X register short i, j, k, r, c;
- X
- X StartHuff(N_CHAR1);
- X init(Table1);
- X InitIO();
- X for (i = 0; i < N1 - F1; i++)
- X text_buf[i] = ' ';
- X r = N1 - F1;
- X for (in_count = 0;; ) {
- X c = DecodeChar();
- X
- X if (c == ENDOF)
- X break;
- X
- X if (c < 256) {
- X#ifdef DEBUG
- X if (debug)
- X fprintf(stderr, "'%s'\n", pr_char((uc_t)c));
- X else
- X#endif /* DEBUG */
- X putchar (c);
- X text_buf[r++] = c;
- X r &= (N1 - 1);
- X in_count++;
- X } else {
- X i = (r - DecodePOld() - 1) & (N1 - 1);
- X j = c - 256 + THRESHOLD;
- X#ifdef DEBUG
- X if (debug)
- X fputc('"', stderr);
- X#endif
- X for (k = 0; k < j; k++) {
- X c = text_buf[(i + k) & (N1 - 1)];
- X#ifdef DEBUG
- X if (debug)
- X fprintf(stderr, "%s", pr_char((uc_t)c));
- X else
- X#endif
- X putchar (c);
- X text_buf[r++] = c;
- X r &= (N1 - 1);
- X in_count++;
- X }
- X#ifdef DEBUG
- X if (debug)
- X fprintf(stderr, "\"\n");
- X#endif
- X }
- X INDICATOR
- X }
- X}
- X#endif
- END_OF_FILE
- if test 1998 -ne `wc -c <'decode.c'`; then
- echo shar: \"'decode.c'\" unpacked with wrong size!
- fi
- # end of 'decode.c'
- fi
- if test -f 'huf.h' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'huf.h'\"
- else
- echo shar: Extracting \"'huf.h'\" \(204 characters\)
- sed "s/^X//" >'huf.h' <<'END_OF_FILE'
- X
- Xextern us_t freq[];
- Xextern short son[], prnt[];
- Xextern void StartHuff(), init(), write_header(), EncodeChar();
- Xextern void EncodePosition();
- X#define MAX_FREQ (us_t)0x8000 /* Tree update timing */
- END_OF_FILE
- if test 204 -ne `wc -c <'huf.h'`; then
- echo shar: \"'huf.h'\" unpacked with wrong size!
- fi
- # end of 'huf.h'
- fi
- if test -f 'patchlev.h' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'patchlev.h'\"
- else
- echo shar: Extracting \"'patchlev.h'\" \(49 characters\)
- sed "s/^X//" >'patchlev.h' <<'END_OF_FILE'
- X#define PATCHLEVEL 0
- X#define PATCHDATE "1/15/93"
- END_OF_FILE
- if test 49 -ne `wc -c <'patchlev.h'`; then
- echo shar: \"'patchlev.h'\" unpacked with wrong size!
- fi
- # end of 'patchlev.h'
- fi
- if test -f 'showhuf.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'showhuf.c'\"
- else
- echo shar: Extracting \"'showhuf.c'\" \(1964 characters\)
- sed "s/^X//" >'showhuf.c' <<'END_OF_FILE'
- X#include "freeze.h"
- X#include "huf.h"
- X
- Xuc_t Table2[9];
- X
- X/* prints out Huffman information from a frozen file, just for fun
- X * and testing purposes.
- X */
- X
- Xint main(argc, argv)
- Xint argc; char ** argv;
- X{
- X if (argc != 2) {
- X fprintf(stderr, "Usage: showhuf frozen_file\n");
- X return 1;
- X }
- X
- X if (freopen(argv[1], "r", stdin) == NULL) {
- X fprintf(stderr, "showhuf: can't open file %s", argv[1]);
- X perror(" ");
- X return 1;
- X }
- X if (getchar() != MAGIC1)
- X goto reject;
- X
- X switch (getchar()) {
- X case MAGIC2_1:
- X printf("%s: no Huffman table (old-style)\n", argv[1]);
- X return 0;
- X
- X case MAGIC2_2:
- X break;
- X
- X default: reject:
- X printf("%s is not in frozen format\n", argv[1]);
- X return 0;
- X }
- X
- X /* Now the real work begins... */
- X
- X printf("%s: ", argv[1]);
- X
- X if (read_header() == EOF)
- X return 1;
- X
- X printf("%d,%d,%d,%d,%d,%d,%d,%d\n",
- X Table2[1], Table2[2], Table2[3], Table2[4],
- X Table2[5], Table2[6], Table2[7], Table2[8]);
- X
- X /* ... and ends */
- X
- X return 0;
- X}
- X
- X/* Reconstructs `Table' from the header of the frozen file and checks
- X its correctness. Returns 0 if OK, EOF otherwise.
- X*/
- X
- Xint read_header() {
- X short i, j;
- X i = getchar() & 0xFF;
- X i |= (getchar() & 0xFF) << 8;
- X Table2[1] = i & 1; i >>= 1;
- X Table2[2] = i & 3; i >>= 2;
- X Table2[3] = i & 7; i >>= 3;
- X Table2[4] = i & 0xF; i >>= 4;
- X Table2[5] = i & 0x1F; i >>= 5;
- X
- X if (i & 1 || (i = getchar()) & 0xC0) {
- X fprintf(stderr, "Unknown header format.\n");
- X return EOF;
- X }
- X
- X Table2[6] = i & 0x3F;
- X
- X i = Table2[1] + Table2[2] + Table2[3] + Table2[4] +
- X Table2[5] + Table2[6];
- X
- X i = 62 - i; /* free variable length codes for 7 & 8 bits */
- X
- X j = 128 * Table2[1] + 64 * Table2[2] + 32 * Table2[3] +
- X 16 * Table2[4] + 8 * Table2[5] + 4 * Table2[6];
- X
- X j = 256 - j; /* free byte images for these codes */
- X
- X/* Equation:
- X Table[7] + Table[8] = i
- X 2 * Table[7] + Table[8] = j
- X*/
- X j -= i;
- X if (j < 0 || i < j) {
- X printf("Warning - bad Huffman table");
- X return 0;
- X }
- X Table2[7] = j;
- X Table2[8] = i - j;
- X
- X return 0;
- X}
- END_OF_FILE
- if test 1964 -ne `wc -c <'showhuf.c'`; then
- echo shar: \"'showhuf.c'\" unpacked with wrong size!
- fi
- # end of 'showhuf.c'
- fi
- echo shar: End of archive 3 \(of 3\).
- cp /dev/null ark3isdone
- MISSING=""
- for I in 1 2 3 ; do
- if test ! -f ark${I}isdone ; then
- MISSING="${MISSING} ${I}"
- fi
- done
- if test "${MISSING}" = "" ; then
- echo You have unpacked all 3 archives.
- rm -f ark[1-9]isdone
- else
- echo You still must unpack the following archives:
- echo " " ${MISSING}
- fi
- exit 0
- exit 0 # Just in case...
-