home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / LZ_C.ARJ / LZHUF.C < prev    next >
C/C++ Source or Header  |  1989-04-10  |  15KB  |  626 lines

  1. /**************************************************************
  2.     lzhuf.c
  3.     written by Haruyasu Yoshizaki 11/20/1988
  4.     some minor changes 4/6/1989
  5.     comments translated by Haruhiko Okumura 4/7/1989
  6. **************************************************************/
  7.  
  8. /*
  9. LZHUF.C (c)1989 by Haruyasu Yoshizaki, Haruhiko Okumura, and Kenji Rikitake.
  10. All rights reserved. Permission granted for non-commercial use.
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17.  
  18. FILE  *infile, *outfile;
  19. unsigned long int  textsize = 0, codesize = 0, printcount = 0;
  20.  
  21. char wterr[] = "Can't write.";
  22.  
  23. void Error(char *message)
  24. {
  25.     printf("\n%s\n", message);
  26.     exit(EXIT_FAILURE);
  27. }
  28.  
  29. /********** LZSS compression **********/
  30.  
  31. #define N        4096    /* buffer size */
  32. #define F        60    /* lookahead buffer size */
  33. #define THRESHOLD    2
  34. #define NIL        N    /* leaf of tree */
  35.  
  36. unsigned char
  37.         text_buf[N + F - 1];
  38. int        match_position, match_length,
  39.         lson[N + 1], rson[N + 257], dad[N + 1];
  40.  
  41. void InitTree(void)  /* initialize trees */
  42. {
  43.     int  i;
  44.  
  45.     for (i = N + 1; i <= N + 256; i++)
  46.         rson[i] = NIL;            /* root */
  47.     for (i = 0; i < N; i++)
  48.         dad[i] = NIL;            /* node */
  49. }
  50.  
  51. void InsertNode(int r)  /* insert to tree */
  52. {
  53.     int  i, p, cmp;
  54.     unsigned char  *key;
  55.     unsigned c;
  56.  
  57.     cmp = 1;
  58.     key = &text_buf[r];
  59.     p = N + 1 + key[0];
  60.     rson[r] = lson[r] = NIL;
  61.     match_length = 0;
  62.     for ( ; ; ) {
  63.         if (cmp >= 0) {
  64.             if (rson[p] != NIL)
  65.                 p = rson[p];
  66.             else {
  67.                 rson[p] = r;
  68.                 dad[r] = p;
  69.                 return;
  70.             }
  71.         } else {
  72.             if (lson[p] != NIL)
  73.                 p = lson[p];
  74.             else {
  75.                 lson[p] = r;
  76.                 dad[r] = p;
  77.                 return;
  78.             }
  79.         }
  80.         for (i = 1; i < F; i++)
  81.             if ((cmp = key[i] - text_buf[p + i]) != 0)
  82.                 break;
  83.         if (i > THRESHOLD) {
  84.             if (i > match_length) {
  85.                 match_position = ((r - p) & (N - 1)) - 1;
  86.                 if ((match_length = i) >= F)
  87.                     break;
  88.             }
  89.             if (i == match_length) {
  90.                 if ((c = ((r - p) & (N - 1)) - 1) < match_position) {
  91.                     match_position = c;
  92.                 }
  93.             }
  94.         }
  95.     }
  96.     dad[r] = dad[p];
  97.     lson[r] = lson[p];
  98.     rson[r] = rson[p];
  99.     dad[lson[p]] = r;
  100.     dad[rson[p]] = r;
  101.     if (rson[dad[p]] == p)
  102.         rson[dad[p]] = r;
  103.     else
  104.         lson[dad[p]] = r;
  105.     dad[p] = NIL;  /* remove p */
  106. }
  107.  
  108. void DeleteNode(int p)  /* remove from tree */
  109. {
  110.     int  q;
  111.  
  112.     if (dad[p] == NIL)
  113.         return;            /* not registered */
  114.     if (rson[p] == NIL)
  115.         q = lson[p];
  116.     else
  117.     if (lson[p] == NIL)
  118.         q = rson[p];
  119.     else {
  120.         q = lson[p];
  121.         if (rson[q] != NIL) {
  122.             do {
  123.                 q = rson[q];
  124.             } while (rson[q] != NIL);
  125.             rson[dad[q]] = lson[q];
  126.             dad[lson[q]] = dad[q];
  127.             lson[q] = lson[p];
  128.             dad[lson[p]] = q;
  129.         }
  130.         rson[q] = rson[p];
  131.         dad[rson[p]] = q;
  132.     }
  133.     dad[q] = dad[p];
  134.     if (rson[dad[p]] == p)
  135.         rson[dad[p]] = q;
  136.     else
  137.         lson[dad[p]] = q;
  138.     dad[p] = NIL;
  139. }
  140.  
  141. /* Huffman coding */
  142.  
  143. #define N_CHAR      (256 - THRESHOLD + F)
  144.                 /* kinds of characters (character code = 0..N_CHAR-1) */
  145. #define T         (N_CHAR * 2 - 1)    /* size of table */
  146. #define R         (T - 1)            /* position of root */
  147. #define MAX_FREQ    0x8000        /* updates tree when the */
  148.                     /* root frequency comes to this value. */
  149. typedef unsigned char uchar;
  150.  
  151.  
  152. /* table for encoding and decoding the upper 6 bits of position */
  153.  
  154. /* for encoding */
  155. uchar p_len[64] = {
  156.     0x03, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05,
  157.     0x05, 0x05, 0x05, 0x05, 0x06, 0x06, 0x06, 0x06,
  158.     0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  159.     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  160.     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  161.     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  162.     0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  163.     0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08
  164. };
  165.  
  166. uchar p_code[64] = {
  167.     0x00, 0x20, 0x30, 0x40, 0x50, 0x58, 0x60, 0x68,
  168.     0x70, 0x78, 0x80, 0x88, 0x90, 0x94, 0x98, 0x9C,
  169.     0xA0, 0xA4, 0xA8, 0xAC, 0xB0, 0xB4, 0xB8, 0xBC,
  170.     0xC0, 0xC2, 0xC4, 0xC6, 0xC8, 0xCA, 0xCC, 0xCE,
  171.     0xD0, 0xD2, 0xD4, 0xD6, 0xD8, 0xDA, 0xDC, 0xDE,
  172.     0xE0, 0xE2, 0xE4, 0xE6, 0xE8, 0xEA, 0xEC, 0xEE,
  173.     0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
  174.     0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF
  175. };
  176.  
  177. /* for decoding */
  178. uchar d_code[256] = {
  179.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  180.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  181.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  182.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  183.     0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  184.     0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  185.     0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  186.     0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  187.     0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  188.     0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  189.     0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  190.     0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  191.     0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  192.     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  193.     0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  194.     0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
  195.     0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A,
  196.     0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B,
  197.     0x0C, 0x0C, 0x0C, 0x0C, 0x0D, 0x0D, 0x0D, 0x0D,
  198.     0x0E, 0x0E, 0x0E, 0x0E, 0x0F, 0x0F, 0x0F, 0x0F,
  199.     0x10, 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x11,
  200.     0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13,
  201.     0x14, 0x14, 0x14, 0x14, 0x15, 0x15, 0x15, 0x15,
  202.     0x16, 0x16, 0x16, 0x16, 0x17, 0x17, 0x17, 0x17,
  203.     0x18, 0x18, 0x19, 0x19, 0x1A, 0x1A, 0x1B, 0x1B,
  204.     0x1C, 0x1C, 0x1D, 0x1D, 0x1E, 0x1E, 0x1F, 0x1F,
  205.     0x20, 0x20, 0x21, 0x21, 0x22, 0x22, 0x23, 0x23,
  206.     0x24, 0x24, 0x25, 0x25, 0x26, 0x26, 0x27, 0x27,
  207.     0x28, 0x28, 0x29, 0x29, 0x2A, 0x2A, 0x2B, 0x2B,
  208.     0x2C, 0x2C, 0x2D, 0x2D, 0x2E, 0x2E, 0x2F, 0x2F,
  209.     0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  210.     0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
  211. };
  212.  
  213. uchar d_len[256] = {
  214.     0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  215.     0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  216.     0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  217.     0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  218.     0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  219.     0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  220.     0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  221.     0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  222.     0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  223.     0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  224.     0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  225.     0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  226.     0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  227.     0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  228.     0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  229.     0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  230.     0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  231.     0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  232.     0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  233.     0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  234.     0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  235.     0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  236.     0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  237.     0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  238.     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  239.     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  240.     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  241.     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  242.     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  243.     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  244.     0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  245.     0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  246. };
  247.  
  248. unsigned freq[T + 1];    /* frequency table */
  249.  
  250. int prnt[T + N_CHAR];    /* pointers to parent nodes, except for the */
  251.             /* elements [T..T + N_CHAR - 1] which are used to get */
  252.             /* the positions of leaves corresponding to the codes. */
  253.  
  254. int son[T];        /* pointers to child nodes (son[], son[] + 1) */
  255.  
  256. unsigned getbuf = 0;
  257. uchar getlen = 0;
  258.  
  259. int GetBit(void)    /* get one bit */
  260. {
  261.     int i;
  262.  
  263.     while (getlen <= 8) {
  264.         if ((i = getc(infile)) < 0) i = 0;
  265.         getbuf |= i << (8 - getlen);
  266.         getlen += 8;
  267.     }
  268.     i = getbuf;
  269.     getbuf <<= 1;
  270.     getlen--;
  271.     return (i < 0);
  272. }
  273.  
  274. int GetByte(void)    /* get one byte */
  275. {
  276.     unsigned i;
  277.  
  278.     while (getlen <= 8) {
  279.         if ((i = getc(infile)) < 0) i = 0;
  280.         getbuf |= i << (8 - getlen);
  281.         getlen += 8;
  282.     }
  283.     i = getbuf;
  284.     getbuf <<= 8;
  285.     getlen -= 8;
  286.     return i >> 8;
  287. }
  288.  
  289. unsigned putbuf = 0;
  290. uchar putlen = 0;
  291.  
  292. void Putcode(int l, unsigned c)        /* output c bits of code */
  293. {
  294.     putbuf |= c >> putlen;
  295.     if ((putlen += l) >= 8) {
  296.         if (putc(putbuf >> 8, outfile) == EOF) {
  297.             Error(wterr);
  298.         }
  299.         if ((putlen -= 8) >= 8) {
  300.             if (putc(putbuf, outfile) == EOF) {
  301.                 Error(wterr);
  302.             }
  303.             codesize += 2;
  304.             putlen -= 8;
  305.             putbuf = c << (l - putlen);
  306.         } else {
  307.             putbuf <<= 8;
  308.             codesize++;
  309.         }
  310.     }
  311. }
  312.  
  313.  
  314. /* initialization of tree */
  315.  
  316. void StartHuff(void)
  317. {
  318.     int i, j;
  319.  
  320.     for (i = 0; i < N_CHAR; i++) {
  321.         freq[i] = 1;
  322.         son[i] = i + T;
  323.         prnt[i + T] = i;
  324.     }
  325.     i = 0; j = N_CHAR;
  326.     while (j <= R) {
  327.         freq[j] = freq[i] + freq[i + 1];
  328.         son[j] = i;
  329.         prnt[i] = prnt[i + 1] = j;
  330.         i += 2; j++;
  331.     }
  332.     freq[T] = 0xffff;
  333.     prnt[R] = 0;
  334. }
  335.  
  336.  
  337. /* reconstruction of tree */
  338.  
  339. void reconst(void)
  340. {
  341.     int i, j, k;
  342.     unsigned f, l;
  343.  
  344.     /* collect leaf nodes in the first half of the table */
  345.     /* and replace the freq by (freq + 1) / 2. */
  346.     j = 0;
  347.     for (i = 0; i < T; i++) {
  348.         if (son[i] >= T) {
  349.             freq[j] = (freq[i] + 1) / 2;
  350.             son[j] = son[i];
  351.             j++;
  352.         }
  353.     }
  354.     /* begin constructing tree by connecting sons */
  355.     for (i = 0, j = N_CHAR; j < T; i += 2, j++) {
  356.         k = i + 1;
  357.         f = freq[j] = freq[i] + freq[k];
  358.         for (k = j - 1; f < freq[k]; k--);
  359.         k++;
  360.         l = (j - k) * 2;
  361.         memmove(&freq[k + 1], &freq[k], l);
  362.         freq[k] = f;
  363.         memmove(&son[k + 1], &son[k], l);
  364.         son[k] = i;
  365.     }
  366.     /* connect prnt */
  367.     for (i = 0; i < T; i++) {
  368.         if ((k = son[i]) >= T) {
  369.             prnt[k] = i;
  370.         } else {
  371.             prnt[k] = prnt[k + 1] = i;
  372.         }
  373.     }
  374. }
  375.  
  376.  
  377. /* increment frequency of given code by one, and update tree */
  378.  
  379. void update(int c)
  380. {
  381.     int i, j, k, l;
  382.  
  383.     if (freq[R] == MAX_FREQ) {
  384.         reconst();
  385.     }
  386.     c = prnt[c + T];
  387.     do {
  388.         k = ++freq[c];
  389.  
  390.         /* if the order is disturbed, exchange nodes */
  391.         if (k > freq[l = c + 1]) {
  392.             while (k > freq[++l]);
  393.             l--;
  394.             freq[c] = freq[l];
  395.             freq[l] = k;
  396.  
  397.             i = son[c];
  398.             prnt[i] = l;
  399.             if (i < T) prnt[i + 1] = l;
  400.  
  401.             j = son[l];
  402.             son[l] = i;
  403.  
  404.             prnt[j] = c;
  405.             if (j < T) prnt[j + 1] = c;
  406.             son[c] = j;
  407.  
  408.             c = l;
  409.         }
  410.     } while ((c = prnt[c]) != 0);    /* repeat up to root */
  411. }
  412.  
  413. unsigned code, len;
  414.  
  415. void EncodeChar(unsigned c)
  416. {
  417.     unsigned i;
  418.     int j, k;
  419.  
  420.     i = 0;
  421.     j = 0;
  422.     k = prnt[c + T];
  423.  
  424.     /* travel from leaf to root */
  425.     do {
  426.         i >>= 1;
  427.  
  428.         /* if node's address is odd-numbered, choose bigger brother node */
  429.         if (k & 1) i += 0x8000;
  430.  
  431.         j++;
  432.     } while ((k = prnt[k]) != R);
  433.     Putcode(j, i);
  434.     code = i;
  435.     len = j;
  436.     update(c);
  437. }
  438.  
  439. void EncodePosition(unsigned c)
  440. {
  441.     unsigned i;
  442.  
  443.     /* output upper 6 bits by table lookup */
  444.     i = c >> 6;
  445.     Putcode(p_len[i], (unsigned)p_code[i] << 8);
  446.  
  447.     /* output lower 6 bits verbatim */
  448.     Putcode(6, (c & 0x3f) << 10);
  449. }
  450.  
  451. void EncodeEnd(void)
  452. {
  453.     if (putlen) {
  454.         if (putc(putbuf >> 8, outfile) == EOF) {
  455.             Error(wterr);
  456.         }
  457.         codesize++;
  458.     }
  459. }
  460.  
  461. int DecodeChar(void)
  462. {
  463.     unsigned c;
  464.  
  465.     c = son[R];
  466.  
  467.     /* travel from root to leaf, */
  468.     /* choosing the smaller child node (son[]) if the read bit is 0, */
  469.     /* the bigger (son[]+1} if 1 */
  470.     while (c < T) {
  471.         c += GetBit();
  472.         c = son[c];
  473.     }
  474.     c -= T;
  475.     update(c);
  476.     return c;
  477. }
  478.  
  479. int DecodePosition(void)
  480. {
  481.     unsigned i, j, c;
  482.  
  483.     /* recover upper 6 bits from table */
  484.     i = GetByte();
  485.     c = (unsigned)d_code[i] << 6;
  486.     j = d_len[i];
  487.  
  488.     /* read lower 6 bits verbatim */
  489.     j -= 2;
  490.     while (j--) {
  491.         i = (i << 1) + GetBit();
  492.     }
  493.     return c | (i & 0x3f);
  494. }
  495.  
  496. /* compression */
  497.  
  498. void Encode(void)  /* compression */
  499. {
  500.     int  i, c, len, r, s, last_match_length;
  501.  
  502.     fseek(infile, 0L, 2);
  503.     textsize = ftell(infile);
  504.     if (fwrite(&textsize, sizeof textsize, 1, outfile) < 1)
  505.         Error(wterr);    /* output size of text */
  506.     if (textsize == 0)
  507.         return;
  508.     rewind(infile);
  509.     textsize = 0;            /* rewind and re-read */
  510.     StartHuff();
  511.     InitTree();
  512.     s = 0;
  513.     r = N - F;
  514.     for (i = s; i < r; i++)
  515.         text_buf[i] = ' ';
  516.     for (len = 0; len < F && (c = getc(infile)) != EOF; len++)
  517.         text_buf[r + len] = c;
  518.     textsize = len;
  519.     for (i = 1; i <= F; i++)
  520.         InsertNode(r - i);
  521.     InsertNode(r);
  522.     do {
  523.         if (match_length > len)
  524.             match_length = len;
  525.         if (match_length <= THRESHOLD) {
  526.             match_length = 1;
  527.             EncodeChar(text_buf[r]);
  528.         } else {
  529.             EncodeChar(255 - THRESHOLD + match_length);
  530.             EncodePosition(match_position);
  531.         }
  532.         last_match_length = match_length;
  533.         for (i = 0; i < last_match_length &&
  534.                 (c = getc(infile)) != EOF; i++) {
  535.             DeleteNode(s);
  536.             text_buf[s] = c;
  537.             if (s < F - 1)
  538.                 text_buf[s + N] = c;
  539.             s = (s + 1) & (N - 1);
  540.             r = (r + 1) & (N - 1);
  541.             InsertNode(r);
  542.         }
  543.         if ((textsize += i) > printcount) {
  544.             printf("%12ld\r", textsize);
  545.             printcount += 1024;
  546.         }
  547.         while (i++ < last_match_length) {
  548.             DeleteNode(s);
  549.             s = (s + 1) & (N - 1);
  550.             r = (r + 1) & (N - 1);
  551.             if (--len) InsertNode(r);
  552.         }
  553.     } while (len > 0);
  554.     EncodeEnd();
  555.     printf("In : %ld bytes\n", textsize);
  556.     printf("Out: %ld bytes\n", codesize);
  557.     printf("Out/In: %.3f\n", (double)codesize / textsize);
  558. }
  559.  
  560. void Decode(void)  /* recover */
  561. {
  562.     int  i, j, k, r, c;
  563.     unsigned long int  count;
  564.  
  565.     if (fread(&textsize, sizeof textsize, 1, infile) < 1)
  566.         Error("Can't read");  /* read size of text */
  567.     if (textsize == 0)
  568.         return;
  569.     StartHuff();
  570.     for (i = 0; i < N - F; i++)
  571.         text_buf[i] = ' ';
  572.     r = N - F;
  573.     for (count = 0; count < textsize; ) {
  574.         c = DecodeChar();
  575.         if (c < 256) {
  576.             if (putc(c, outfile) == EOF) {
  577.                 Error(wterr);
  578.             }
  579.             text_buf[r++] = c;
  580.             r &= (N - 1);
  581.             count++;
  582.         } else {
  583.             i = (r - DecodePosition() - 1) & (N - 1);
  584.             j = c - 255 + THRESHOLD;
  585.             for (k = 0; k < j; k++) {
  586.                 c = text_buf[(i + k) & (N - 1)];
  587.                 if (putc(c, outfile) == EOF) {
  588.                     Error(wterr);
  589.                 }
  590.                 text_buf[r++] = c;
  591.                 r &= (N - 1);
  592.                 count++;
  593.             }
  594.         }
  595.         if (count > printcount) {
  596.             printf("%12ld\r", count);
  597.             printcount += 1024;
  598.         }
  599.     }
  600.     printf("%12ld\n", count);
  601. }
  602.  
  603. int main(int argc, char *argv[])
  604. {
  605.     char  *s;
  606.  
  607.     if (argc != 4) {
  608.         printf("'lzhuf e file1 file2' encodes file1 into file2.\n"
  609.                "'lzhuf d file2 file1' decodes file2 into file1.\n");
  610.         return EXIT_FAILURE;
  611.     }
  612.     if ((s = argv[1], s[1] || strpbrk(s, "DEde") == NULL)
  613.      || (s = argv[2], (infile  = fopen(s, "rb")) == NULL)
  614.      || (s = argv[3], (outfile = fopen(s, "wb")) == NULL)) {
  615.         printf("??? %s\n", s);
  616.         return EXIT_FAILURE;
  617.     }
  618.     if (toupper(*argv[1]) == 'E')
  619.         Encode();
  620.     else
  621.         Decode();
  622.     fclose(infile);
  623.     fclose(outfile);
  624.     return EXIT_SUCCESS;
  625. }
  626.