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