LZSS.C
上传用户:szqxhk
上传日期:2010-02-15
资源大小:54k
文件大小:8k
源码类别:

压缩解压

开发平台:

Visual C++

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