home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / Samples / COMPRESS.ARJ / LZ / LZHUF.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-12  |  14.4 KB  |  622 lines

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