3
0

decompress_uncompress.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 "unarchive.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. //error check??
  140. insize += rsize;
  141. }
  142. inbits = ((rsize > 0) ? (insize - insize % n_bits) << 3 :
  143. (insize << 3) - (n_bits - 1));
  144. while (inbits > posbits) {
  145. if (free_ent > maxcode) {
  146. posbits =
  147. ((posbits - 1) +
  148. ((n_bits << 3) -
  149. (posbits - 1 + (n_bits << 3)) % (n_bits << 3)));
  150. ++n_bits;
  151. if (n_bits == maxbits) {
  152. maxcode = maxmaxcode;
  153. } else {
  154. maxcode = MAXCODE(n_bits) - 1;
  155. }
  156. bitmask = (1 << n_bits) - 1;
  157. goto resetbuf;
  158. }
  159. {
  160. unsigned char *p = &inbuf[posbits >> 3];
  161. code = ((((long) (p[0])) | ((long) (p[1]) << 8) |
  162. ((long) (p[2]) << 16)) >> (posbits & 0x7)) & bitmask;
  163. }
  164. posbits += n_bits;
  165. if (oldcode == -1) {
  166. oldcode = code;
  167. finchar = (int) oldcode;
  168. outbuf[outpos++] = (unsigned char) finchar;
  169. continue;
  170. }
  171. if (code == CLEAR && block_mode) {
  172. clear_tab_prefixof();
  173. free_ent = FIRST - 1;
  174. posbits =
  175. ((posbits - 1) +
  176. ((n_bits << 3) -
  177. (posbits - 1 + (n_bits << 3)) % (n_bits << 3)));
  178. n_bits = INIT_BITS;
  179. maxcode = MAXCODE(INIT_BITS) - 1;
  180. bitmask = (1 << INIT_BITS) - 1;
  181. goto resetbuf;
  182. }
  183. incode = code;
  184. stackp = de_stack;
  185. /* Special case for KwKwK string. */
  186. if (code >= free_ent) {
  187. if (code > free_ent) {
  188. unsigned char *p;
  189. posbits -= n_bits;
  190. p = &inbuf[posbits >> 3];
  191. bb_error_msg
  192. ("insize:%d posbits:%d inbuf:%02X %02X %02X %02X %02X (%d)",
  193. insize, posbits, p[-1], p[0], p[1], p[2], p[3],
  194. (posbits & 07));
  195. bb_error_msg("uncompress: corrupt input");
  196. goto err;
  197. }
  198. *--stackp = (unsigned char) finchar;
  199. code = oldcode;
  200. }
  201. /* Generate output characters in reverse order */
  202. while ((long) code >= (long) 256) {
  203. *--stackp = tab_suffixof(code);
  204. code = tab_prefixof(code);
  205. }
  206. finchar = tab_suffixof(code);
  207. *--stackp = (unsigned char) finchar;
  208. /* And put them out in forward order */
  209. {
  210. int i;
  211. i = de_stack - stackp;
  212. if (outpos + i >= OBUFSIZ) {
  213. do {
  214. if (i > OBUFSIZ - outpos) {
  215. i = OBUFSIZ - outpos;
  216. }
  217. if (i > 0) {
  218. memcpy(outbuf + outpos, stackp, i);
  219. outpos += i;
  220. }
  221. if (outpos >= OBUFSIZ) {
  222. full_write(fd_out, outbuf, outpos);
  223. //error check??
  224. IF_DESKTOP(total_written += outpos;)
  225. outpos = 0;
  226. }
  227. stackp += i;
  228. i = de_stack - stackp;
  229. } while (i > 0);
  230. } else {
  231. memcpy(outbuf + outpos, stackp, i);
  232. outpos += i;
  233. }
  234. }
  235. /* Generate the new entry. */
  236. code = free_ent;
  237. if (code < maxmaxcode) {
  238. tab_prefixof(code) = (unsigned short) oldcode;
  239. tab_suffixof(code) = (unsigned char) finchar;
  240. free_ent = code + 1;
  241. }
  242. /* Remember previous code. */
  243. oldcode = incode;
  244. }
  245. } while (rsize > 0);
  246. if (outpos > 0) {
  247. full_write(fd_out, outbuf, outpos);
  248. //error check??
  249. IF_DESKTOP(total_written += outpos;)
  250. }
  251. retval = IF_DESKTOP(total_written) + 0;
  252. err:
  253. free(inbuf);
  254. free(outbuf);
  255. free(htab);
  256. free(codetab);
  257. return retval;
  258. }