home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume34 / freeze / part03 < prev    next >
Encoding:
Text File  |  1993-01-18  |  12.7 KB  |  556 lines

  1. Newsgroups: comp.sources.misc
  2. From: leo@ipmce.su (Leonid A. Broukhis)
  3. Subject: v34i127:  freeze - Freeze/melt compression program vers. 2.4, Part03/03
  4. Message-ID: <1993Jan19.043628.29675@sparky.imd.sterling.com>
  5. X-Md4-Signature: 69c593040bb2be9fc06cad59c79ae8de
  6. Date: Tue, 19 Jan 1993 04:36:28 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: leo@ipmce.su (Leonid A. Broukhis)
  10. Posting-number: Volume 34, Issue 127
  11. Archive-name: freeze/part03
  12. Environment: ISC, Xenix, SunOS, MS-DOS
  13. Supersedes: freeze: Volume 25, Issue 12-13
  14.  
  15. #! /bin/sh
  16. # This is a shell archive.  Remove anything before this line, then feed it
  17. # into a shell via "sh file" or similar.  To overwrite existing files,
  18. # type "sh file -c".
  19. # Contents:  bitio.c bitio.h compat.h debug.c decode.c huf.h patchlev.h
  20. #   showhuf.c
  21. # Wrapped by kent@sparky on Mon Jan 18 22:27:49 1993
  22. PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:/usr/lbin ; export PATH
  23. echo If this archive is complete, you will see the following message:
  24. echo '          "shar: End of archive 3 (of 3)."'
  25. if test -f 'bitio.c' -a "${1}" != "-c" ; then 
  26.   echo shar: Will not clobber existing file \"'bitio.c'\"
  27. else
  28.   echo shar: Extracting \"'bitio.c'\" \(1429 characters\)
  29.   sed "s/^X//" >'bitio.c' <<'END_OF_FILE'
  30. X#include "freeze.h"
  31. X#include "bitio.h"
  32. X
  33. Xul_t     getbuf = 0;     /* assume sizeof (ul_t) >= 4 */
  34. Xus_t     putbuf;
  35. Xuc_t     bitlen = 0, __, crpt_flag = 0;
  36. X
  37. X/* get N bits (N <= 16), returning in Bit(N-1)...Bit 0 */
  38. X
  39. Xshort GetNBits (n)
  40. X    register us_t n;
  41. X{
  42. X    register ul_t dx = getbuf;
  43. X    register uc_t c;
  44. X
  45. X    static us_t mask[17] = {
  46. X        0x0000,
  47. X        0x0001, 0x0003, 0x0007, 0x000f,
  48. X        0x001f, 0x003f, 0x007f, 0x00ff,
  49. X        0x01ff, 0x03ff, 0x07ff, 0x0fff,
  50. X        0x1fff, 0x3fff, 0x7fff, 0xffff };
  51. X
  52. X    while (bitlen < n)
  53. X        {
  54. X            c = getchar ();
  55. X            dx |= (ul_t) c << (BYSH - bitlen);
  56. X            bitlen += 8;
  57. X        }
  58. X    crpt_flag = feof(stdin);
  59. X    getbuf = dx << n;
  60. X    bitlen -= n;
  61. X    return (dx >> (bits(getbuf) - n)) & mask[n];
  62. X}
  63. X
  64. X/* output `l' high bits of `c' */
  65. X
  66. Xvoid Putcode (l, c)
  67. X    register int l;
  68. X    us_t c;
  69. X{
  70. X    register us_t len = bitlen;
  71. X    register us_t b = (us_t)putbuf;
  72. X    b |= c >> len;
  73. X    if ((len += l) >= 8) {
  74. X        putchar ((int)(b >> 8));
  75. X        if ((len -= 8) >= 8) {
  76. X            putchar ((int)b);
  77. X            bytes_out += 2;
  78. X            len -= 8;
  79. X            b = c << (l - len);
  80. X        } else {
  81. X            b <<= 8;
  82. X            bytes_out++;
  83. X        }
  84. X    }
  85. X    if (ferror(stdout))
  86. X        writeerr();
  87. X    putbuf = b;
  88. X    bitlen = len;
  89. X}
  90. X
  91. X
  92. X/* Flushes the bit I/O buffers and check the state of stdout */
  93. X
  94. Xvoid EncodeEnd ()
  95. X{
  96. X    if (bitlen) {
  97. X        putchar((int)(putbuf >> 8));
  98. X        bytes_out++;
  99. X        if (ferror(stdout))
  100. X            writeerr();
  101. X    }
  102. X}
  103. X
  104. X/* File too short or invalid header, print a message */
  105. X
  106. Xvoid crpt_message ( )
  107. X{
  108. X    fprintf ( stderr, "melt: corrupt input\n" );
  109. X}
  110. X
  111. END_OF_FILE
  112.   if test 1429 -ne `wc -c <'bitio.c'`; then
  113.     echo shar: \"'bitio.c'\" unpacked with wrong size!
  114.   fi
  115.   # end of 'bitio.c'
  116. fi
  117. if test -f 'bitio.h' -a "${1}" != "-c" ; then 
  118.   echo shar: Will not clobber existing file \"'bitio.h'\"
  119. else
  120.   echo shar: Extracting \"'bitio.h'\" \(915 characters\)
  121.   sed "s/^X//" >'bitio.h' <<'END_OF_FILE'
  122. X/* Some definitions for faster bit-level I/O */
  123. X
  124. Xextern ul_t getbuf;    /* Bit I/O buffers */
  125. Xextern us_t putbuf;
  126. Xextern uc_t bitlen;            /* Number of bits actually in `???buf' */
  127. X
  128. Xextern uc_t crpt_flag;   /* 1 == EOF was read when melting */
  129. Xextern uc_t __;          /* temporary variable for GetBit/Byte */
  130. X
  131. Xextern void EncodeEnd(), Putcode(), crpt_message();
  132. X
  133. X#define bits(x) ((int)sizeof(x)*8)
  134. X#define BYSH  (bits(getbuf)-8)
  135. X#define BISH  (bits(getbuf)-1)
  136. X
  137. X#define GetByte()       (bitlen >= 8 ? (__ = getbuf >> BYSH, bitlen -= 8,\
  138. X            getbuf <<= 8, __) : (getbuf |= (ul_t) \
  139. X            (getchar() & 0xFF) << (BYSH - bitlen), __ = getbuf\
  140. X            >> BYSH, getbuf <<= 8, __))
  141. X
  142. X#define GetBit()        (bitlen ? (__ = getbuf >> BISH, bitlen--, \
  143. X            getbuf <<= 1, __) : (getbuf = __ = getchar(), \
  144. X            getbuf <<= BYSH + 1, bitlen = 7, __ >> 7))
  145. X
  146. X#define InitIO()        { crpt_flag = bitlen = 0; putbuf = 0; getbuf = 0; }
  147. END_OF_FILE
  148.   if test 915 -ne `wc -c <'bitio.h'`; then
  149.     echo shar: \"'bitio.h'\" unpacked with wrong size!
  150.   fi
  151.   # end of 'bitio.h'
  152. fi
  153. if test -f 'compat.h' -a "${1}" != "-c" ; then 
  154.   echo shar: Will not clobber existing file \"'compat.h'\"
  155. else
  156.   echo shar: Extracting \"'compat.h'\" \(183 characters\)
  157.   sed "s/^X//" >'compat.h' <<'END_OF_FILE'
  158. Xextern uc_t    Table1[];
  159. X
  160. X#define F1              60
  161. X#define N1              4096
  162. X#define N_CHAR1         (256 - THRESHOLD + F1 + 1)
  163. X
  164. Xextern short DecodePOld();
  165. X
  166. Xextern void melt1();
  167. END_OF_FILE
  168.   if test 183 -ne `wc -c <'compat.h'`; then
  169.     echo shar: \"'compat.h'\" unpacked with wrong size!
  170.   fi
  171.   # end of 'compat.h'
  172. fi
  173. if test -f 'debug.c' -a "${1}" != "-c" ; then 
  174.   echo shar: Will not clobber existing file \"'debug.c'\"
  175. else
  176.   echo shar: Extracting \"'debug.c'\" \(1547 characters\)
  177.   sed "s/^X//" >'debug.c' <<'END_OF_FILE'
  178. X#if defined(DEBUG)
  179. X#include "freeze.h"
  180. X#include "huf.h"
  181. X#include "bitio.h"
  182. X
  183. X          /*---------------------------*/
  184. X          /*      DEBUG      MODULE    */
  185. X          /*---------------------------*/
  186. X
  187. Xprintcodes(mode)
  188. X{
  189. X    /*
  190. X     * Just print out codes from input file.  For debugging.
  191. X     */
  192. X    register short k, c, col = 0;
  193. X
  194. X#ifdef COMPAT
  195. X    if (!mode) {
  196. X        StartHuff(N_CHAR1);
  197. X        init(Table1);
  198. X    } else
  199. X#endif
  200. X    {
  201. X        if (read_header() == EOF) {
  202. X            fprintf(stderr, "Bad header\n");
  203. X            return;
  204. X        }
  205. X        StartHuff(N_CHAR2);
  206. X        init(Table2);
  207. X    }
  208. X
  209. X    InitIO();
  210. X
  211. X    for (;;) {
  212. X
  213. X        if((c = DecodeChar()) == ENDOF)
  214. X            break;
  215. X        if (c < 256) {
  216. X        fprintf(stderr, "%5d%c", c,
  217. X            (col+=8) >= 74 ? (col = 0, '\n') : '\t' );
  218. X        } else {
  219. X        c = c - 256 + THRESHOLD;
  220. X
  221. X        k = DecodePosition();
  222. X
  223. X        fprintf(stderr, "%2d-%d%c", c, k,
  224. X            (col+=8) >= 74 ? (col = 0, '\n') : '\t' );
  225. X        }
  226. X    }
  227. X    putc( '\n', stderr );
  228. X    exit( 0 );
  229. X}
  230. X
  231. X/* for pretty char printing */
  232. X
  233. Xchar *
  234. Xpr_char(c)
  235. X    register uc_t c;
  236. X{
  237. X    static char buf[5];
  238. X    register i = 4;
  239. X    buf[4] = '\0';
  240. X    if ( (isascii((int)c) && isprint((int)c) && c != '\\') || c == ' ' ) {
  241. X        buf[--i] = c;
  242. X    } else {
  243. X        switch( c ) {
  244. X        case '\n': buf[--i] = 'n'; break;
  245. X        case '\t': buf[--i] = 't'; break;
  246. X        case '\b': buf[--i] = 'b'; break;
  247. X        case '\f': buf[--i] = 'f'; break;
  248. X        case '\r': buf[--i] = 'r'; break;
  249. X        case '\\': buf[--i] = '\\'; break;
  250. X        default:
  251. X        buf[--i] = '0' + c % 8;
  252. X        buf[--i] = '0' + (c / 8) % 8;
  253. X        buf[--i] = '0' + c / 64;
  254. X        break;
  255. X        }
  256. X        buf[--i] = '\\';
  257. X    }
  258. X    return &buf[i];
  259. X}
  260. X#endif
  261. END_OF_FILE
  262.   if test 1547 -ne `wc -c <'debug.c'`; then
  263.     echo shar: \"'debug.c'\" unpacked with wrong size!
  264.   fi
  265.   # end of 'debug.c'
  266. fi
  267. if test -f 'decode.c' -a "${1}" != "-c" ; then 
  268.   echo shar: Will not clobber existing file \"'decode.c'\"
  269. else
  270.   echo shar: Extracting \"'decode.c'\" \(1998 characters\)
  271.   sed "s/^X//" >'decode.c' <<'END_OF_FILE'
  272. X#include "freeze.h"
  273. X#include "huf.h"
  274. X#include "bitio.h"
  275. X
  276. X/*
  277. X * Melt stdin to stdout.
  278. X */
  279. X
  280. Xvoid melt2 ()
  281. X{
  282. X    register short    i, j, k, r, c;
  283. X
  284. X/* Huffman-dependent part */
  285. X    if(read_header() == EOF)
  286. X        return;
  287. X    StartHuff(N_CHAR2);
  288. X    init(Table2);
  289. X/* end of Huffman-dependent part */
  290. X
  291. X    InitIO();
  292. X    for (i = 0; i < MAXDIST; i++)
  293. X        text_buf[i] = ' ';
  294. X    r = MAXDIST;
  295. X    for (in_count = 0;; ) {
  296. X        c = DecodeChar();
  297. X
  298. X        if (c == ENDOF)
  299. X            break;
  300. X        if (c < 256) {
  301. X#ifdef DEBUG
  302. X            if (debug)
  303. X                fprintf(stderr, "'%s'\n", pr_char((uc_t)c));
  304. X            else
  305. X#endif /* DEBUG */
  306. X                putchar (c);
  307. X            text_buf[r++] = c;
  308. X            r &= WINMASK;
  309. X            in_count++;
  310. X        } else {
  311. X            i = (r - DecodePosition() - 1) & WINMASK;
  312. X            j = c - 256 + THRESHOLD;
  313. X#ifdef DEBUG
  314. X            if (debug)
  315. X                fputc('"', stderr);
  316. X#endif
  317. X            for (k = 0; k < j; k++) {
  318. X                c = text_buf[(i + k) & WINMASK];
  319. X#ifdef DEBUG
  320. X                if (debug)
  321. X                    fprintf(stderr, "%s", pr_char((uc_t)c));
  322. X                else
  323. X#endif
  324. X                    putchar (c);
  325. X                text_buf[r++] = c;
  326. X                r &= WINMASK;
  327. X                in_count++;
  328. X            }
  329. X#ifdef DEBUG
  330. X            if (debug)
  331. X                fprintf(stderr, "\"\n");
  332. X#endif
  333. X        }
  334. X        INDICATOR
  335. X    }
  336. X}
  337. X
  338. X#ifdef COMPAT
  339. Xvoid melt1 ()
  340. X{
  341. X    register short    i, j, k, r, c;
  342. X
  343. X    StartHuff(N_CHAR1);
  344. X    init(Table1);
  345. X    InitIO();
  346. X    for (i = 0; i < N1 - F1; i++)
  347. X        text_buf[i] = ' ';
  348. X    r = N1 - F1;
  349. X    for (in_count = 0;; ) {
  350. X        c =  DecodeChar();
  351. X
  352. X        if (c == ENDOF)
  353. X            break;
  354. X
  355. X        if (c < 256) {
  356. X#ifdef DEBUG
  357. X            if (debug)
  358. X                fprintf(stderr, "'%s'\n", pr_char((uc_t)c));
  359. X            else
  360. X#endif /* DEBUG */
  361. X                putchar (c);
  362. X            text_buf[r++] = c;
  363. X            r &= (N1 - 1);
  364. X            in_count++;
  365. X        } else {
  366. X            i = (r - DecodePOld() - 1) & (N1 - 1);
  367. X            j = c - 256 + THRESHOLD;
  368. X#ifdef DEBUG
  369. X            if (debug)
  370. X                fputc('"', stderr);
  371. X#endif
  372. X            for (k = 0; k < j; k++) {
  373. X                c = text_buf[(i + k) & (N1 - 1)];
  374. X#ifdef DEBUG
  375. X                if (debug)
  376. X                    fprintf(stderr, "%s", pr_char((uc_t)c));
  377. X                else
  378. X#endif
  379. X                    putchar (c);
  380. X                text_buf[r++] = c;
  381. X                r &= (N1 - 1);
  382. X                in_count++;
  383. X            }
  384. X#ifdef DEBUG
  385. X            if (debug)
  386. X                fprintf(stderr, "\"\n");
  387. X#endif
  388. X        }
  389. X        INDICATOR
  390. X    }
  391. X}
  392. X#endif
  393. END_OF_FILE
  394.   if test 1998 -ne `wc -c <'decode.c'`; then
  395.     echo shar: \"'decode.c'\" unpacked with wrong size!
  396.   fi
  397.   # end of 'decode.c'
  398. fi
  399. if test -f 'huf.h' -a "${1}" != "-c" ; then 
  400.   echo shar: Will not clobber existing file \"'huf.h'\"
  401. else
  402.   echo shar: Extracting \"'huf.h'\" \(204 characters\)
  403.   sed "s/^X//" >'huf.h' <<'END_OF_FILE'
  404. X
  405. Xextern us_t freq[];
  406. Xextern short son[], prnt[];
  407. Xextern void StartHuff(), init(), write_header(), EncodeChar();
  408. Xextern void EncodePosition();
  409. X#define MAX_FREQ        (us_t)0x8000 /* Tree update timing */
  410. END_OF_FILE
  411.   if test 204 -ne `wc -c <'huf.h'`; then
  412.     echo shar: \"'huf.h'\" unpacked with wrong size!
  413.   fi
  414.   # end of 'huf.h'
  415. fi
  416. if test -f 'patchlev.h' -a "${1}" != "-c" ; then 
  417.   echo shar: Will not clobber existing file \"'patchlev.h'\"
  418. else
  419.   echo shar: Extracting \"'patchlev.h'\" \(49 characters\)
  420.   sed "s/^X//" >'patchlev.h' <<'END_OF_FILE'
  421. X#define PATCHLEVEL 0
  422. X#define PATCHDATE "1/15/93"
  423. END_OF_FILE
  424.   if test 49 -ne `wc -c <'patchlev.h'`; then
  425.     echo shar: \"'patchlev.h'\" unpacked with wrong size!
  426.   fi
  427.   # end of 'patchlev.h'
  428. fi
  429. if test -f 'showhuf.c' -a "${1}" != "-c" ; then 
  430.   echo shar: Will not clobber existing file \"'showhuf.c'\"
  431. else
  432.   echo shar: Extracting \"'showhuf.c'\" \(1964 characters\)
  433.   sed "s/^X//" >'showhuf.c' <<'END_OF_FILE'
  434. X#include "freeze.h"
  435. X#include "huf.h"
  436. X
  437. Xuc_t Table2[9];
  438. X
  439. X/* prints out Huffman information from a frozen file, just for fun
  440. X * and testing purposes.
  441. X */
  442. X
  443. Xint main(argc, argv)
  444. Xint argc; char ** argv;
  445. X{
  446. X    if (argc != 2) {
  447. X        fprintf(stderr, "Usage: showhuf frozen_file\n");
  448. X        return 1;
  449. X    }
  450. X
  451. X    if (freopen(argv[1], "r", stdin) == NULL) {
  452. X        fprintf(stderr, "showhuf: can't open file %s", argv[1]);
  453. X        perror(" ");
  454. X        return 1;
  455. X    }
  456. X    if (getchar() != MAGIC1)
  457. X        goto reject;
  458. X
  459. X    switch (getchar()) {
  460. X    case MAGIC2_1:
  461. X        printf("%s: no Huffman table (old-style)\n", argv[1]);
  462. X        return 0;
  463. X
  464. X    case MAGIC2_2:
  465. X        break;
  466. X
  467. X    default: reject:
  468. X        printf("%s is not in frozen format\n", argv[1]);
  469. X        return 0;
  470. X    }
  471. X
  472. X    /* Now the real work begins... */
  473. X
  474. X    printf("%s: ", argv[1]);
  475. X
  476. X    if (read_header() == EOF)
  477. X        return 1;
  478. X
  479. X    printf("%d,%d,%d,%d,%d,%d,%d,%d\n",
  480. X        Table2[1], Table2[2], Table2[3], Table2[4],
  481. X        Table2[5], Table2[6], Table2[7], Table2[8]);
  482. X
  483. X    /* ... and ends */
  484. X
  485. X    return 0;
  486. X}
  487. X
  488. X/* Reconstructs `Table' from the header of the frozen file and checks
  489. X    its correctness. Returns 0 if OK, EOF otherwise.
  490. X*/
  491. X
  492. Xint read_header() {
  493. X    short i, j;
  494. X    i = getchar() & 0xFF;
  495. X    i |= (getchar() & 0xFF) << 8;
  496. X    Table2[1] = i & 1; i >>= 1;
  497. X    Table2[2] = i & 3; i >>= 2;
  498. X    Table2[3] = i & 7; i >>= 3;
  499. X    Table2[4] = i & 0xF; i >>= 4;
  500. X    Table2[5] = i & 0x1F; i >>= 5;
  501. X
  502. X    if (i & 1 || (i = getchar()) & 0xC0) {
  503. X        fprintf(stderr, "Unknown header format.\n");
  504. X        return EOF;
  505. X    }
  506. X
  507. X    Table2[6] = i & 0x3F;
  508. X
  509. X    i = Table2[1] + Table2[2] + Table2[3] + Table2[4] +
  510. X    Table2[5] + Table2[6];
  511. X
  512. X    i = 62 - i;     /* free variable length codes for 7 & 8 bits */
  513. X
  514. X    j = 128 * Table2[1] + 64 * Table2[2] + 32 * Table2[3] +
  515. X    16 * Table2[4] + 8 * Table2[5] + 4 * Table2[6];
  516. X
  517. X    j = 256 - j;    /* free byte images for these codes */
  518. X
  519. X/*      Equation:
  520. X        Table[7] + Table[8] = i
  521. X    2 * Table[7] + Table[8] = j
  522. X*/
  523. X    j -= i;
  524. X    if (j < 0 || i < j) {
  525. X        printf("Warning - bad Huffman table");
  526. X        return 0;
  527. X    }
  528. X    Table2[7] = j;
  529. X    Table2[8] = i - j;
  530. X
  531. X    return 0;
  532. X}
  533. END_OF_FILE
  534.   if test 1964 -ne `wc -c <'showhuf.c'`; then
  535.     echo shar: \"'showhuf.c'\" unpacked with wrong size!
  536.   fi
  537.   # end of 'showhuf.c'
  538. fi
  539. echo shar: End of archive 3 \(of 3\).
  540. cp /dev/null ark3isdone
  541. MISSING=""
  542. for I in 1 2 3 ; do
  543.     if test ! -f ark${I}isdone ; then
  544.     MISSING="${MISSING} ${I}"
  545.     fi
  546. done
  547. if test "${MISSING}" = "" ; then
  548.     echo You have unpacked all 3 archives.
  549.     rm -f ark[1-9]isdone
  550. else
  551.     echo You still must unpack the following archives:
  552.     echo "        " ${MISSING}
  553. fi
  554. exit 0
  555. exit 0 # Just in case...
  556.