decompress_uncompress.c 7.9 KB

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