lzss.C 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: lzss.cc /main/5 1996/07/18 16:00:08 drk $ */
  24. #include "compression/lzss.h"
  25. /*
  26. Adapted from LDS (lossless datacompression sources) Version 1.1 by
  27. Nico E. de Vries.
  28. qfc. 12-8-93.
  29. */
  30. /**************************************************************
  31. LZSS.C -- A Data Compression Program
  32. (tab = 4 spaces)
  33. ***************************************************************
  34. 4/6/1989 Haruhiko Okumura
  35. Use, distribute, and modify this program freely.
  36. Please send me your improved versions.
  37. PC-VAN SCIENCE
  38. NIFTY-Serve PAF01022
  39. CompuServe 74050,1022
  40. **************************************************************/
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <ctype.h>
  45. #define N 4096 /* size of ring buffer */
  46. #define F 18 /* upper limit for match_length */
  47. #define THRESHOLD 2 /* encode string into position and length
  48. if match_length is greater than this */
  49. #define NIL N /* index for root of binary search trees */
  50. unsigned long int
  51. textsize = 0, /* text size counter */
  52. codesize = 0, /* code size counter */
  53. printcount = 0; /* counter for reporting progress every 1K bytes */
  54. unsigned char
  55. text_buf[N + F - 1]; /* ring buffer of size N,
  56. with extra F-1 bytes to facilitate string comparison */
  57. int match_position, match_length, /* of longest match. These are
  58. set by the InsertNode() procedure. */
  59. lson[N + 1], rson[N + 257], dad[N + 1]; /* left & right children &
  60. parents -- These constitute binary search trees. */
  61. //FILE *infile, *outfile; /* input & output files */
  62. void InitTree(void) /* initialize trees */
  63. {
  64. int i;
  65. /* For i = 0 to N - 1, rson[i] and lson[i] will be the right and
  66. left children of node i. These nodes need not be initialized.
  67. Also, dad[i] is the parent of node i. These are initialized to
  68. NIL (= N), which stands for 'not used.'
  69. For i = 0 to 255, rson[N + i + 1] is the root of the tree
  70. for strings that begin with character i. These are initialized
  71. to NIL. Note there are 256 trees. */
  72. for (i = N + 1; i <= N + 256; i++) rson[i] = NIL;
  73. for (i = 0; i < N; i++) dad[i] = NIL;
  74. }
  75. void InsertNode(int r)
  76. /* Inserts string of length F, text_buf[r..r+F-1], into one of the
  77. trees (text_buf[r]'th tree) and returns the longest-match position
  78. and length via the global variables match_position and match_length.
  79. If match_length = F, then removes the old node in favor of the new
  80. one, because the old one will be deleted sooner.
  81. Note r plays double role, as tree node and position in buffer. */
  82. {
  83. int i, p, cmp;
  84. unsigned char *key;
  85. cmp = 1; key = &text_buf[r]; p = N + 1 + key[0];
  86. rson[r] = lson[r] = NIL; match_length = 0;
  87. for ( ; ; ) {
  88. if (cmp >= 0) {
  89. if (rson[p] != NIL) p = rson[p];
  90. else { rson[p] = r; dad[r] = p; return; }
  91. } else {
  92. if (lson[p] != NIL) p = lson[p];
  93. else { lson[p] = r; dad[r] = p; return; }
  94. }
  95. for (i = 1; i < F; i++)
  96. if ((cmp = key[i] - text_buf[p + i]) != 0) break;
  97. if (i > match_length) {
  98. match_position = p;
  99. if ((match_length = i) >= F) break;
  100. }
  101. }
  102. dad[r] = dad[p]; lson[r] = lson[p]; rson[r] = rson[p];
  103. dad[lson[p]] = r; dad[rson[p]] = r;
  104. if (rson[dad[p]] == p) rson[dad[p]] = r;
  105. else lson[dad[p]] = r;
  106. dad[p] = NIL; /* remove p */
  107. }
  108. void DeleteNode(int p) /* deletes node p from tree */
  109. {
  110. int q;
  111. if (dad[p] == NIL) return; /* not in tree */
  112. if (rson[p] == NIL) q = lson[p];
  113. else if (lson[p] == NIL) q = rson[p];
  114. else {
  115. q = lson[p];
  116. if (rson[q] != NIL) {
  117. do { q = rson[q]; } while (rson[q] != NIL);
  118. rson[dad[q]] = lson[q]; dad[lson[q]] = dad[q];
  119. lson[q] = lson[p]; dad[lson[p]] = q;
  120. }
  121. rson[q] = rson[p]; dad[rson[p]] = q;
  122. }
  123. dad[q] = dad[p];
  124. if (rson[dad[p]] == p) rson[dad[p]] = q; else lson[dad[p]] = q;
  125. dad[p] = NIL;
  126. }
  127. void lzss::compress(const buffer& uncompressed, buffer& compressed)
  128. {
  129. if ( compressed.buf_sz() < uncompressed.buf_sz() )
  130. compressed.expand_chunk(uncompressed.buf_sz());
  131. ////////////////////////////////////////////
  132. ////////////////////////////////////////////
  133. int i, c, len, r, s, last_match_length, code_buf_ptr;
  134. unsigned char code_buf[17], mask;
  135. InitTree(); /* initialize trees */
  136. code_buf[0] = 0; /* code_buf[1..16] saves eight units of code, and
  137. code_buf[0] works as eight flags, "1" representing that the unit
  138. is an unencoded letter (1 byte), "0" a position-and-length pair
  139. (2 bytes). Thus, eight units require at most 16 bytes of code. */
  140. code_buf_ptr = mask = 1;
  141. s = 0; r = N - F;
  142. for (i = s; i < r; i++) text_buf[i] = ' '; /* Clear the buffer with
  143. any character that will appear often. */
  144. char* unc_str = uncompressed.get_base();
  145. int unc_str_len = uncompressed.content_sz();
  146. int unc_str_ptr = 0;
  147. for (len = 0; len < F; len++) {
  148. if ( unc_str_ptr == unc_str_len )
  149. break;
  150. c = unc_str[unc_str_ptr++];
  151. //cerr << char(c);
  152. text_buf[r + len] = c; /* Read F bytes into the last F bytes of
  153. the buffer */
  154. }
  155. if ((textsize = len) == 0) return; /* text of size zero */
  156. for (i = 1; i <= F; i++) InsertNode(r - i); /* Insert the F strings,
  157. each of which begins with one or more 'space' characters. Note
  158. the order in which these strings are inserted. This way,
  159. degenerate trees will be less likely to occur. */
  160. InsertNode(r); /* Finally, insert the whole string just read. The
  161. global variables match_length and match_position are set. */
  162. do {
  163. if (match_length > len) match_length = len; /* match_length
  164. may be spuriously long near the end of text. */
  165. if (match_length <= THRESHOLD) {
  166. match_length = 1; /* Not long enough match. Send one byte. */
  167. code_buf[0] |= mask; /* 'send one byte' flag */
  168. code_buf[code_buf_ptr++] = text_buf[r]; /* Send uncoded. */
  169. } else {
  170. code_buf[code_buf_ptr++] = (unsigned char) match_position;
  171. code_buf[code_buf_ptr++] = (unsigned char)
  172. (((match_position >> 4) & 0xf0)
  173. | (match_length - (THRESHOLD + 1))); /* Send position and
  174. length pair. Note match_length > THRESHOLD. */
  175. }
  176. if ((mask <<= 1) == 0) { /* Shift mask left one bit. */
  177. // for (i = 0; i < code_buf_ptr; i++) { /* Send at most 8 units of */
  178. // putc(code_buf[i], outfile); /* code together */
  179. // }
  180. compressed.put((char*)code_buf, code_buf_ptr, true);
  181. codesize += code_buf_ptr;
  182. code_buf[0] = 0; code_buf_ptr = mask = 1;
  183. }
  184. last_match_length = match_length;
  185. for (i = 0; i < last_match_length; i++) {
  186. if ( unc_str_ptr == unc_str_len )
  187. break;
  188. c = unc_str[unc_str_ptr++];
  189. DeleteNode(s); /* Delete old strings and */
  190. text_buf[s] = c; /* read new bytes */
  191. if (s < F - 1) text_buf[s + N] = c; /* If the position is
  192. near the end of buffer, extend the buffer to make
  193. string comparison easier. */
  194. //s = (s + 1) & (N - 1); r = (r + 1) & (N - 1);
  195. s++; s &= (N - 1); r++; r &= (N - 1);
  196. /* Since this is a ring buffer, increment the position
  197. modulo N. */
  198. InsertNode(r); /* Register the string in text_buf[r..r+F-1] */
  199. }
  200. // if ((textsize += i) > printcount) {
  201. // printf("%12ld\r", textsize); printcount += 1024;
  202. // /* Reports progress each time the textsize exceeds
  203. // multiples of 1024. */
  204. // }
  205. while (i++ < last_match_length) {/* After the end of text, */
  206. DeleteNode(s); /* no need to read, but */
  207. //s = (s + 1) & (N - 1); r = (r + 1) & (N - 1);
  208. s++; s &= (N - 1); r++; r &= (N - 1);
  209. if (--len) InsertNode(r); /* buffer may not be empty. */
  210. }
  211. } while (len > 0); /* until length of string to be processed is zero */
  212. if (code_buf_ptr > 1) { /* Send remaining code. */
  213. // for (i = 0; i < code_buf_ptr; i++) {
  214. // //putc(code_buf[i], outfile);
  215. // compressed.put(code_buf[i], true);
  216. // }
  217. compressed.put((char*)code_buf, code_buf_ptr, true);
  218. codesize += code_buf_ptr;
  219. }
  220. //printf("In : %ld bytes\n", textsize); /* Encoding is done. */
  221. //printf("Out: %ld bytes\n", codesize);
  222. //printf("Out/In: %.3f\n", (double)codesize / textsize);
  223. }
  224. void lzss::decompress(buffer& compressed, buffer& uncompressed)
  225. {
  226. int i, j, k, r, c;
  227. unsigned int flags;
  228. for (i = 0; i < N - F; i++) text_buf[i] = ' ';
  229. r = N - F; flags = 0;
  230. for (;;) {
  231. if (((flags >>= 1) & 256) == 0) {
  232. // if ((c = getc(infile)) == EOF) break;
  233. if ( compressed.content_sz() == 0 )
  234. break;
  235. compressed.getusc(c);
  236. flags = c | 0xff00; /* uses higher byte cleverly */
  237. } /* to count eight */
  238. if (flags & 1) {
  239. //if ((c = getc(infile)) == EOF) break;
  240. //putc(c, outfile); text_buf[r++] = c; r &= (N - 1);
  241. if ( compressed.content_sz() == 0 )
  242. break;
  243. compressed.getusc(c);
  244. //debug(cerr, char(c));
  245. uncompressed.put(char(c), true); text_buf[r++] = c; r &= (N - 1);
  246. } else {
  247. //if ((i = getc(infile)) == EOF) break;
  248. //if ((j = getc(infile)) == EOF) break;
  249. if ( compressed.content_sz() == 0 )
  250. break;
  251. else {
  252. compressed.getusc(i);
  253. }
  254. if ( compressed.content_sz() == 0 )
  255. break;
  256. else
  257. compressed.getusc(j);
  258. i |= ((j & 0xf0) << 4); j = (j & 0x0f) + THRESHOLD;
  259. for (k = 0; k <= j; k++) {
  260. c = text_buf[(i + k) & (N - 1)];
  261. //putc(c, outfile);
  262. //debug(cerr, char(c));
  263. uncompressed.put(char(c), true);
  264. text_buf[r++] = c; r &= (N - 1);
  265. }
  266. }
  267. }
  268. }
  269. io_status lzss::build_dict(lex_func_t, getchar_func_t)
  270. {
  271. return done;
  272. }
  273. MMDB_BODIES(lzss)