decompress_uncompress.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* vi: set sw=4 ts=4: */
  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 "libbb.h"
  26. #include "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(int fd_in, int fd_out)
  65. {
  66. IF_DESKTOP(long long total_written = 0;)
  67. IF_DESKTOP(long long) int retval = -1;
  68. unsigned char *stackp;
  69. long code;
  70. int finchar;
  71. long oldcode;
  72. long incode;
  73. int inbits;
  74. int posbits;
  75. int outpos;
  76. int insize;
  77. int bitmask;
  78. long free_ent;
  79. long maxcode;
  80. long maxmaxcode;
  81. int n_bits;
  82. int rsize = 0;
  83. unsigned char *inbuf; /* were eating insane amounts of stack - */
  84. unsigned char *outbuf; /* bad for some embedded targets */
  85. unsigned char *htab;
  86. unsigned short *codetab;
  87. /* Hmm, these were statics - why?! */
  88. /* user settable max # bits/code */
  89. int maxbits; /* = BITS; */
  90. /* block compress mode -C compatible with 2.0 */
  91. int block_mode; /* = BLOCK_MODE; */
  92. inbuf = xzalloc(IBUFSIZ + 64);
  93. outbuf = xzalloc(OBUFSIZ + 2048);
  94. htab = xzalloc(HSIZE); /* wsn't zeroed out before, maybe can xmalloc? */
  95. codetab = xzalloc(HSIZE * sizeof(codetab[0]));
  96. insize = 0;
  97. /* xread isn't good here, we have to return - caller may want
  98. * to do some cleanup (e.g. delete incomplete unpacked file etc) */
  99. if (full_read(fd_in, inbuf, 1) != 1) {
  100. bb_error_msg("short read");
  101. goto err;
  102. }
  103. maxbits = inbuf[0] & BIT_MASK;
  104. block_mode = inbuf[0] & BLOCK_MODE;
  105. maxmaxcode = MAXCODE(maxbits);
  106. if (maxbits > BITS) {
  107. bb_error_msg("compressed with %d bits, can only handle "
  108. BITS_STR" bits", maxbits);
  109. goto err;
  110. }
  111. n_bits = INIT_BITS;
  112. maxcode = MAXCODE(INIT_BITS) - 1;
  113. bitmask = (1 << INIT_BITS) - 1;
  114. oldcode = -1;
  115. finchar = 0;
  116. outpos = 0;
  117. posbits = 0 << 3;
  118. free_ent = ((block_mode) ? FIRST : 256);
  119. /* As above, initialize the first 256 entries in the table. */
  120. /*clear_tab_prefixof(); - done by xzalloc */
  121. for (code = 255; code >= 0; --code) {
  122. tab_suffixof(code) = (unsigned char) code;
  123. }
  124. do {
  125. resetbuf:
  126. {
  127. int i;
  128. int e;
  129. int o;
  130. o = posbits >> 3;
  131. e = insize - o;
  132. for (i = 0; i < e; ++i)
  133. inbuf[i] = inbuf[i + o];
  134. insize = e;
  135. posbits = 0;
  136. }
  137. if (insize < (int) (IBUFSIZ + 64) - IBUFSIZ) {
  138. rsize = safe_read(fd_in, inbuf + insize, IBUFSIZ);
  139. if (rsize < 0)
  140. bb_error_msg_and_die(bb_msg_read_error);
  141. insize += rsize;
  142. }
  143. inbits = ((rsize > 0) ? (insize - insize % n_bits) << 3 :
  144. (insize << 3) - (n_bits - 1));
  145. while (inbits > posbits) {
  146. if (free_ent > maxcode) {
  147. posbits =
  148. ((posbits - 1) +
  149. ((n_bits << 3) -
  150. (posbits - 1 + (n_bits << 3)) % (n_bits << 3)));
  151. ++n_bits;
  152. if (n_bits == maxbits) {
  153. maxcode = maxmaxcode;
  154. } else {
  155. maxcode = MAXCODE(n_bits) - 1;
  156. }
  157. bitmask = (1 << n_bits) - 1;
  158. goto resetbuf;
  159. }
  160. {
  161. unsigned char *p = &inbuf[posbits >> 3];
  162. code = ((((long) (p[0])) | ((long) (p[1]) << 8) |
  163. ((long) (p[2]) << 16)) >> (posbits & 0x7)) & bitmask;
  164. }
  165. posbits += n_bits;
  166. if (oldcode == -1) {
  167. if (code >= 256)
  168. bb_error_msg_and_die("corrupted data"); /* %ld", code); */
  169. oldcode = code;
  170. finchar = (int) oldcode;
  171. outbuf[outpos++] = (unsigned char) finchar;
  172. continue;
  173. }
  174. if (code == CLEAR && block_mode) {
  175. clear_tab_prefixof();
  176. free_ent = FIRST - 1;
  177. posbits =
  178. ((posbits - 1) +
  179. ((n_bits << 3) -
  180. (posbits - 1 + (n_bits << 3)) % (n_bits << 3)));
  181. n_bits = INIT_BITS;
  182. maxcode = MAXCODE(INIT_BITS) - 1;
  183. bitmask = (1 << INIT_BITS) - 1;
  184. goto resetbuf;
  185. }
  186. incode = code;
  187. stackp = de_stack;
  188. /* Special case for KwKwK string. */
  189. if (code >= free_ent) {
  190. if (code > free_ent) {
  191. unsigned char *p;
  192. posbits -= n_bits;
  193. p = &inbuf[posbits >> 3];
  194. bb_error_msg
  195. ("insize:%d posbits:%d inbuf:%02X %02X %02X %02X %02X (%d)",
  196. insize, posbits, p[-1], p[0], p[1], p[2], p[3],
  197. (posbits & 07));
  198. bb_error_msg("corrupted data");
  199. goto err;
  200. }
  201. *--stackp = (unsigned char) finchar;
  202. code = oldcode;
  203. }
  204. /* Generate output characters in reverse order */
  205. while ((long) code >= (long) 256) {
  206. if (stackp <= &htabof(0))
  207. bb_error_msg_and_die("corrupted data");
  208. *--stackp = tab_suffixof(code);
  209. code = tab_prefixof(code);
  210. }
  211. finchar = tab_suffixof(code);
  212. *--stackp = (unsigned char) finchar;
  213. /* And put them out in forward order */
  214. {
  215. int i;
  216. i = de_stack - stackp;
  217. if (outpos + i >= OBUFSIZ) {
  218. do {
  219. if (i > OBUFSIZ - outpos) {
  220. i = OBUFSIZ - outpos;
  221. }
  222. if (i > 0) {
  223. memcpy(outbuf + outpos, stackp, i);
  224. outpos += i;
  225. }
  226. if (outpos >= OBUFSIZ) {
  227. xwrite(fd_out, outbuf, outpos);
  228. IF_DESKTOP(total_written += outpos;)
  229. outpos = 0;
  230. }
  231. stackp += i;
  232. i = de_stack - stackp;
  233. } while (i > 0);
  234. } else {
  235. memcpy(outbuf + outpos, stackp, i);
  236. outpos += i;
  237. }
  238. }
  239. /* Generate the new entry. */
  240. code = free_ent;
  241. if (code < maxmaxcode) {
  242. tab_prefixof(code) = (unsigned short) oldcode;
  243. tab_suffixof(code) = (unsigned char) finchar;
  244. free_ent = code + 1;
  245. }
  246. /* Remember previous code. */
  247. oldcode = incode;
  248. }
  249. } while (rsize > 0);
  250. if (outpos > 0) {
  251. xwrite(fd_out, outbuf, outpos);
  252. IF_DESKTOP(total_written += outpos;)
  253. }
  254. retval = IF_DESKTOP(total_written) + 0;
  255. err:
  256. free(inbuf);
  257. free(outbuf);
  258. free(htab);
  259. free(codetab);
  260. return retval;
  261. }