decompress_uncompress.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #include "libbb.h"
  2. /* uncompress for busybox -- (c) 2002 Robert Griebl
  3. *
  4. * based on the original compress42.c source
  5. * (see disclaimer below)
  6. */
  7. /* (N)compress42.c - File compression ala IEEE Computer, Mar 1992.
  8. *
  9. * Authors:
  10. * Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas)
  11. * Jim McKie (decvax!mcvax!jim)
  12. * Steve Davies (decvax!vax135!petsd!peora!srd)
  13. * Ken Turkowski (decvax!decwrl!turtlevax!ken)
  14. * James A. Woods (decvax!ihnp4!ames!jaw)
  15. * Joe Orost (decvax!vax135!petsd!joe)
  16. * Dave Mack (csu@alembic.acs.com)
  17. * Peter Jannesen, Network Communication Systems
  18. * (peter@ncs.nl)
  19. *
  20. * marc@suse.de : a small security fix for a buffer overflow
  21. *
  22. * [... History snipped ...]
  23. *
  24. */
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. /* Default input buffer size */
  29. #define IBUFSIZ 2048
  30. /* Default output buffer size */
  31. #define OBUFSIZ 2048
  32. /* Defines for third byte of header */
  33. #define MAGIC_1 (char_type)'\037' /* First byte of compressed file */
  34. #define MAGIC_2 (char_type)'\235' /* Second byte of compressed file */
  35. #define BIT_MASK 0x1f /* Mask for 'number of compresssion bits' */
  36. /* Masks 0x20 and 0x40 are free. */
  37. /* I think 0x20 should mean that there is */
  38. /* a fourth header byte (for expansion). */
  39. #define BLOCK_MODE 0x80 /* Block compresssion if table is full and */
  40. /* compression rate is dropping flush tables */
  41. /* the next two codes should not be changed lightly, as they must not */
  42. /* lie within the contiguous general code space. */
  43. #define FIRST 257 /* first free entry */
  44. #define CLEAR 256 /* table clear output code */
  45. #define INIT_BITS 9 /* initial number of bits/code */
  46. /* machine variants which require cc -Dmachine: pdp11, z8000, DOS */
  47. #define FAST
  48. #define HBITS 17 /* 50% occupancy */
  49. #define HSIZE (1<<HBITS)
  50. #define HMASK (HSIZE-1)
  51. #define HPRIME 9941
  52. #define BITS 16
  53. #undef MAXSEG_64K
  54. #define MAXCODE(n) (1L << (n))
  55. /* Block compress mode -C compatible with 2.0 */
  56. static int block_mode = BLOCK_MODE;
  57. /* user settable max # bits/code */
  58. static int maxbits = BITS;
  59. /* Input buffer */
  60. static unsigned char inbuf[IBUFSIZ + 64];
  61. /* Output buffer */
  62. static unsigned char outbuf[OBUFSIZ + 2048];
  63. static long int htab[HSIZE];
  64. static unsigned short codetab[HSIZE];
  65. #define htabof(i) htab[i]
  66. #define codetabof(i) codetab[i]
  67. #define tab_prefixof(i) codetabof(i)
  68. #define tab_suffixof(i) ((unsigned char *)(htab))[i]
  69. #define de_stack ((unsigned char *)&(htab[HSIZE-1]))
  70. #define clear_htab() memset(htab, -1, sizeof(htab))
  71. #define clear_tab_prefixof() memset(codetab, 0, 256);
  72. /*
  73. * Decompress stdin to stdout. This routine adapts to the codes in the
  74. * file building the "string" table on-the-fly; requiring no table to
  75. * be stored in the compressed file. The tables used herein are shared
  76. * with those of the compress() routine. See the definitions above.
  77. */
  78. int uncompress(int fd_in, int fd_out)
  79. {
  80. unsigned char *stackp;
  81. long int code;
  82. int finchar;
  83. long int oldcode;
  84. long int incode;
  85. int inbits;
  86. int posbits;
  87. int outpos;
  88. int insize;
  89. int bitmask;
  90. long int free_ent;
  91. long int maxcode;
  92. long int maxmaxcode;
  93. int n_bits;
  94. int rsize = 0;
  95. insize = 0;
  96. inbuf[0] = bb_xread_char(fd_in);
  97. maxbits = inbuf[0] & BIT_MASK;
  98. block_mode = inbuf[0] & BLOCK_MODE;
  99. maxmaxcode = MAXCODE(maxbits);
  100. if (maxbits > BITS) {
  101. bb_error_msg("compressed with %d bits, can only handle %d bits", maxbits,
  102. BITS);
  103. return -1;
  104. }
  105. maxcode = MAXCODE(n_bits = INIT_BITS) - 1;
  106. bitmask = (1 << n_bits) - 1;
  107. oldcode = -1;
  108. finchar = 0;
  109. outpos = 0;
  110. posbits = 0 << 3;
  111. free_ent = ((block_mode) ? FIRST : 256);
  112. /* As above, initialize the first 256 entries in the table. */
  113. clear_tab_prefixof();
  114. for (code = 255; code >= 0; --code) {
  115. tab_suffixof(code) = (unsigned char) code;
  116. }
  117. do {
  118. resetbuf:;
  119. {
  120. int i;
  121. int e;
  122. int o;
  123. e = insize - (o = (posbits >> 3));
  124. for (i = 0; i < e; ++i)
  125. inbuf[i] = inbuf[i + o];
  126. insize = e;
  127. posbits = 0;
  128. }
  129. if (insize < (int) sizeof(inbuf) - IBUFSIZ) {
  130. rsize = safe_read(fd_in, inbuf + insize, IBUFSIZ);
  131. insize += rsize;
  132. }
  133. inbits = ((rsize > 0) ? (insize - insize % n_bits) << 3 :
  134. (insize << 3) - (n_bits - 1));
  135. while (inbits > posbits) {
  136. if (free_ent > maxcode) {
  137. posbits =
  138. ((posbits - 1) +
  139. ((n_bits << 3) -
  140. (posbits - 1 + (n_bits << 3)) % (n_bits << 3)));
  141. ++n_bits;
  142. if (n_bits == maxbits) {
  143. maxcode = maxmaxcode;
  144. } else {
  145. maxcode = MAXCODE(n_bits) - 1;
  146. }
  147. bitmask = (1 << n_bits) - 1;
  148. goto resetbuf;
  149. }
  150. {
  151. unsigned char *p = &inbuf[posbits >> 3];
  152. code =
  153. ((((long) (p[0])) | ((long) (p[1]) << 8) |
  154. ((long) (p[2]) << 16)) >> (posbits & 0x7)) & bitmask;
  155. }
  156. posbits += n_bits;
  157. if (oldcode == -1) {
  158. outbuf[outpos++] = (unsigned char) (finchar =
  159. (int) (oldcode = code));
  160. continue;
  161. }
  162. if (code == CLEAR && block_mode) {
  163. clear_tab_prefixof();
  164. free_ent = FIRST - 1;
  165. posbits =
  166. ((posbits - 1) +
  167. ((n_bits << 3) -
  168. (posbits - 1 + (n_bits << 3)) % (n_bits << 3)));
  169. maxcode = MAXCODE(n_bits = INIT_BITS) - 1;
  170. bitmask = (1 << n_bits) - 1;
  171. goto resetbuf;
  172. }
  173. incode = code;
  174. stackp = de_stack;
  175. /* Special case for KwKwK string. */
  176. if (code >= free_ent) {
  177. if (code > free_ent) {
  178. unsigned char *p;
  179. posbits -= n_bits;
  180. p = &inbuf[posbits >> 3];
  181. bb_error_msg
  182. ("insize:%d posbits:%d inbuf:%02X %02X %02X %02X %02X (%d)",
  183. insize, posbits, p[-1], p[0], p[1], p[2], p[3],
  184. (posbits & 07));
  185. bb_error_msg("uncompress: corrupt input");
  186. return -1;
  187. }
  188. *--stackp = (unsigned char) finchar;
  189. code = oldcode;
  190. }
  191. /* Generate output characters in reverse order */
  192. while ((long int) code >= (long int) 256) {
  193. *--stackp = tab_suffixof(code);
  194. code = tab_prefixof(code);
  195. }
  196. *--stackp = (unsigned char) (finchar = tab_suffixof(code));
  197. /* And put them out in forward order */
  198. {
  199. int i;
  200. if (outpos + (i = (de_stack - stackp)) >= OBUFSIZ) {
  201. do {
  202. if (i > OBUFSIZ - outpos) {
  203. i = OBUFSIZ - outpos;
  204. }
  205. if (i > 0) {
  206. memcpy(outbuf + outpos, stackp, i);
  207. outpos += i;
  208. }
  209. if (outpos >= OBUFSIZ) {
  210. write(fd_out, outbuf, outpos);
  211. outpos = 0;
  212. }
  213. stackp += i;
  214. } while ((i = (de_stack - stackp)) > 0);
  215. } else {
  216. memcpy(outbuf + outpos, stackp, i);
  217. outpos += i;
  218. }
  219. }
  220. /* Generate the new entry. */
  221. if ((code = free_ent) < maxmaxcode) {
  222. tab_prefixof(code) = (unsigned short) oldcode;
  223. tab_suffixof(code) = (unsigned char) finchar;
  224. free_ent = code + 1;
  225. }
  226. /* Remember previous code. */
  227. oldcode = incode;
  228. }
  229. } while (rsize > 0);
  230. if (outpos > 0) {
  231. write(fd_out, outbuf, outpos);
  232. }
  233. return 0;
  234. }