Advertisement
Guest User

Untitled

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