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

  1. #define EXIT_SUCCESS 0
  2. #define EXIT_FAILURE 1
  3. /**************************************************************
  4.     LZSS.C -- A Data Compression Program
  5.     (tab = 4 spaces)
  6. ***************************************************************
  7.     4/6/1989 Haruhiko Okumura
  8.     Use, distribute, and modify this program freely.
  9.     Please send me your improved versions.
  10.         PC-VAN        SCIENCE
  11.         NIFTY-Serve    PAF01022
  12.         CompuServe    74050,1022
  13. **************************************************************/
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <ctype.h>
  18.  
  19. #define N         4096    /* size of ring buffer */
  20. #define F           18    /* upper limit for match_length */
  21. #define THRESHOLD    2   /* encode string into position and length
  22.                            if match_length is greater than this */
  23. #define NIL            N    /* index for root of binary search trees */
  24.  
  25. unsigned long int
  26.         textsize = 0,    /* text size counter */
  27.         codesize = 0,    /* code size counter */
  28.         printcount = 0;    /* counter for reporting progress every 1K bytes */
  29. unsigned char
  30.         text_buf[N + F - 1];    /* ring buffer of size N,
  31.             with extra F-1 bytes to facilitate string comparison */
  32. int        match_position, match_length,  /* of longest match.  These are
  33.             set by the InsertNode() procedure. */
  34.         lson[N + 1], rson[N + 257], dad[N + 1];  /* left & right children &
  35.             parents -- These constitute binary search trees. */
  36. FILE    *infile, *outfile;  /* input & output files */
  37.  
  38. void InitTree(void)  /* initialize trees */
  39. {
  40.     int  i;
  41.  
  42.     /* For i = 0 to N - 1, rson[i] and lson[i] will be the right and
  43.        left children of node i.  These nodes need not be initialized.
  44.        Also, dad[i] is the parent of node i.  These are initialized to
  45.        NIL (= N), which stands for 'not used.'
  46.        For i = 0 to 255, rson[N + i + 1] is the root of the tree
  47.        for strings that begin with character i.  These are initialized
  48.        to NIL.  Note there are 256 trees. */
  49.  
  50.     for (i = N + 1; i <= N + 256; i++) rson[i] = NIL;
  51.     for (i = 0; i < N; i++) dad[i] = NIL;
  52. }
  53.  
  54. void InsertNode(int r)
  55.     /* Inserts string of length F, text_buf[r..r+F-1], into one of the
  56.        trees (text_buf[r]'th tree) and returns the longest-match position
  57.        and length via the global variables match_position and match_length.
  58.        If match_length = F, then removes the old node in favor of the new
  59.        one, because the old one will be deleted sooner.
  60.        Note r plays double role, as tree node and position in buffer. */
  61. {
  62.     int  i, p, cmp;
  63.     unsigned char  *key;
  64.  
  65.     cmp = 1;  key = &text_buf[r];  p = N + 1 + key[0];
  66.     rson[r] = lson[r] = NIL;  match_length = 0;
  67.     for ( ; ; ) {
  68.         if (cmp >= 0) {
  69.             if (rson[p] != NIL) p = rson[p];
  70.             else {  rson[p] = r;  dad[r] = p;  return;  }
  71.         } else {
  72.             if (lson[p] != NIL) p = lson[p];
  73.             else {  lson[p] = r;  dad[r] = p;  return;  }
  74.         }
  75.         for (i = 1; i < F; i++)
  76.             if ((cmp = key[i] - text_buf[p + i]) != 0)  break;
  77.         if (i > match_length) {
  78.             match_position = p;
  79.             if ((match_length = i) >= F)  break;
  80.         }
  81.     }
  82.     dad[r] = dad[p];  lson[r] = lson[p];  rson[r] = rson[p];
  83.     dad[lson[p]] = r;  dad[rson[p]] = r;
  84.     if (rson[dad[p]] == p) rson[dad[p]] = r;
  85.     else                   lson[dad[p]] = r;
  86.     dad[p] = NIL;  /* remove p */
  87. }
  88.  
  89. void DeleteNode(int p)  /* deletes node p from tree */
  90. {
  91.     int  q;
  92.     
  93.     if (dad[p] == NIL) return;  /* not in tree */
  94.     if (rson[p] == NIL) q = lson[p];
  95.     else if (lson[p] == NIL) q = rson[p];
  96.     else {
  97.         q = lson[p];
  98.         if (rson[q] != NIL) {
  99.             do {  q = rson[q];  } while (rson[q] != NIL);
  100.             rson[dad[q]] = lson[q];  dad[lson[q]] = dad[q];
  101.             lson[q] = lson[p];  dad[lson[p]] = q;
  102.         }
  103.         rson[q] = rson[p];  dad[rson[p]] = q;
  104.     }
  105.     dad[q] = dad[p];
  106.     if (rson[dad[p]] == p) rson[dad[p]] = q;  else lson[dad[p]] = q;
  107.     dad[p] = NIL;
  108. }
  109.  
  110. void Encode(void)
  111. {
  112.     int  i, c, len, r, s, last_match_length, code_buf_ptr;
  113.     unsigned char  code_buf[17], mask;
  114.     
  115.     InitTree();  /* initialize trees */
  116.     code_buf[0] = 0;  /* code_buf[1..16] saves eight units of code, and
  117.         code_buf[0] works as eight flags, "1" representing that the unit
  118.         is an unencoded letter (1 byte), "0" a position-and-length pair
  119.         (2 bytes).  Thus, eight units require at most 16 bytes of code. */
  120.     code_buf_ptr = mask = 1;
  121.     s = 0;  r = N - F;
  122.     for (i = s; i < r; i++) text_buf[i] = ' ';  /* Clear the buffer with
  123.         any character that will appear often. */
  124.     for (len = 0; len < F && (c = getc(infile)) != EOF; len++)
  125.         text_buf[r + len] = c;  /* Read F bytes into the last F bytes of
  126.             the buffer */
  127.     if ((textsize = len) == 0) return;  /* text of size zero */
  128.     for (i = 1; i <= F; i++) InsertNode(r - i);  /* Insert the F strings,
  129.         each of which begins with one or more 'space' characters.  Note
  130.         the order in which these strings are inserted.  This way,
  131.         degenerate trees will be less likely to occur. */
  132.     InsertNode(r);  /* Finally, insert the whole string just read.  The
  133.         global variables match_length and match_position are set. */
  134.     do {
  135.         if (match_length > len) match_length = len;  /* match_length
  136.             may be spuriously long near the end of text. */
  137.         if (match_length <= THRESHOLD) {
  138.             match_length = 1;  /* Not long enough match.  Send one byte. */
  139.             code_buf[0] |= mask;  /* 'send one byte' flag */
  140.             code_buf[code_buf_ptr++] = text_buf[r];  /* Send uncoded. */
  141.         } else {
  142.             code_buf[code_buf_ptr++] = (unsigned char) match_position;
  143.             code_buf[code_buf_ptr++] = (unsigned char)
  144.                 (((match_position >> 4) & 0xf0)
  145.               | (match_length - (THRESHOLD + 1)));  /* Send position and
  146.                     length pair. Note match_length > THRESHOLD. */
  147.         }
  148.         if ((mask <<= 1) == 0) {  /* Shift mask left one bit. */
  149.             for (i = 0; i < code_buf_ptr; i++)  /* Send at most 8 units of */
  150.                 putc(code_buf[i], outfile);     /* code together */
  151.             codesize += code_buf_ptr;
  152.             code_buf[0] = 0;  code_buf_ptr = mask = 1;
  153.         }
  154.         last_match_length = match_length;
  155.         for (i = 0; i < last_match_length &&
  156.                 (c = getc(infile)) != EOF; i++) {
  157.             DeleteNode(s);        /* Delete old strings and */
  158.             text_buf[s] = c;    /* read new bytes */
  159.             if (s < F - 1) text_buf[s + N] = c;  /* If the position is
  160.                 near the end of buffer, extend the buffer to make
  161.                 string comparison easier. */
  162.             s = (s + 1) & (N - 1);  r = (r + 1) & (N - 1);
  163.                 /* Since this is a ring buffer, increment the position
  164.                    modulo N. */
  165.             InsertNode(r);    /* Register the string in text_buf[r..r+F-1] */
  166.         }
  167.         if ((textsize += i) > printcount) {
  168.             printf("%12ld\r", textsize);  printcount += 1024;
  169.                 /* Reports progress each time the textsize exceeds
  170.                    multiples of 1024. */
  171.         }
  172.         while (i++ < last_match_length) {    /* After the end of text, */
  173.             DeleteNode(s);                    /* no need to read, but */
  174.             s = (s + 1) & (N - 1);  r = (r + 1) & (N - 1);
  175.             if (--len) InsertNode(r);        /* buffer may not be empty. */
  176.         }
  177.     } while (len > 0);    /* until length of string to be processed is zero */
  178.     if (code_buf_ptr > 1) {        /* Send remaining code. */
  179.         for (i = 0; i < code_buf_ptr; i++) putc(code_buf[i], outfile);
  180.         codesize += code_buf_ptr;
  181.     }
  182.     printf("In : %ld bytes\n", textsize);    /* Encoding is done. */
  183.     printf("Out: %ld bytes\n", codesize);
  184.     printf("Out/In: %.3f\n", (double)codesize / textsize);
  185. }
  186.  
  187. void Decode(void)    /* Just the reverse of Encode(). */
  188. {
  189.     int  i, j, k, r, c;
  190.     unsigned int  flags;
  191.     
  192.     for (i = 0; i < N - F; i++) text_buf[i] = ' ';
  193.     r = N - F;  flags = 0;
  194.     for ( ; ; ) {
  195.         if (((flags >>= 1) & 256) == 0) {
  196.             if ((c = getc(infile)) == EOF) break;
  197.             flags = c | 0xff00;        /* uses higher byte cleverly */
  198.         }                            /* to count eight */
  199.         if (flags & 1) {
  200.             if ((c = getc(infile)) == EOF) break;
  201.             putc(c, outfile);  text_buf[r++] = c;  r &= (N - 1);
  202.         } else {
  203.             if ((i = getc(infile)) == EOF) break;
  204.             if ((j = getc(infile)) == EOF) break;
  205.             i |= ((j & 0xf0) << 4);  j = (j & 0x0f) + THRESHOLD;
  206.             for (k = 0; k <= j; k++) {
  207.                 c = text_buf[(i + k) & (N - 1)];
  208.                 putc(c, outfile);  text_buf[r++] = c;  r &= (N - 1);
  209.             }
  210.         }
  211.     }
  212. }
  213.  
  214. int main(int argc, char *argv[])
  215. {
  216.     char  *s;
  217.     
  218.     if (argc != 4) {
  219.         printf("'lzss e file1 file2' encodes file1 into file2.\n"
  220.                "'lzss d file2 file1' decodes file2 into file1.\n");
  221.         return EXIT_FAILURE;
  222.     }
  223.     if ((s = argv[1], s[1] || strpbrk(s, "DEde") == NULL)
  224.      || (s = argv[2], (infile  = fopen(s, "rb")) == NULL)
  225.      || (s = argv[3], (outfile = fopen(s, "wb")) == NULL)) {
  226.         printf("??? %s\n", s);  return EXIT_FAILURE;
  227.     }
  228.     if (toupper(*argv[1]) == 'E') Encode();  else Decode();
  229.     fclose(infile);  fclose(outfile);
  230.     return EXIT_SUCCESS;
  231. }
  232.