decompress_uncompress.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /* vi: set sw=4 ts=4: */
  2. #include "libbb.h"
  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. */
  26. /* Default input buffer size */
  27. #define IBUFSIZ 2048
  28. /* Default output buffer size */
  29. #define OBUFSIZ 2048
  30. /* Defines for third byte of header */
  31. #define BIT_MASK 0x1f /* Mask for 'number of compresssion bits' */
  32. /* Masks 0x20 and 0x40 are free. */
  33. /* I think 0x20 should mean that there is */
  34. /* a fourth header byte (for expansion). */
  35. #define BLOCK_MODE 0x80 /* Block compression if table is full and */
  36. /* compression rate is dropping flush tables */
  37. /* the next two codes should not be changed lightly, as they must not */
  38. /* lie within the contiguous general code space. */
  39. #define FIRST 257 /* first free entry */
  40. #define CLEAR 256 /* table clear output code */
  41. #define INIT_BITS 9 /* initial number of bits/code */
  42. /* machine variants which require cc -Dmachine: pdp11, z8000, DOS */
  43. #define HBITS 17 /* 50% occupancy */
  44. #define HSIZE (1<<HBITS)
  45. #define HMASK (HSIZE-1) /* unused */
  46. #define HPRIME 9941 /* unused */
  47. #define BITS 16
  48. #define BITS_STR "16"
  49. #undef MAXSEG_64K /* unused */
  50. #define MAXCODE(n) (1L << (n))
  51. #define htabof(i) htab[i]
  52. #define codetabof(i) codetab[i]
  53. #define tab_prefixof(i) codetabof(i)
  54. #define tab_suffixof(i) ((unsigned char *)(htab))[i]
  55. #define de_stack ((unsigned char *)&(htab[HSIZE-1]))
  56. #define clear_tab_prefixof() memset(codetab, 0, 256)
  57. /*
  58. * Decompress stdin to stdout. This routine adapts to the codes in the
  59. * file building the "string" table on-the-fly; requiring no table to
  60. * be stored in the compressed file.
  61. */
  62. USE_DESKTOP(long long) int
  63. uncompress(int fd_in, int fd_out)
  64. {
  65. USE_DESKTOP(long long total_written = 0;)
  66. USE_DESKTOP(long long) int retval = -1;
  67. unsigned char *stackp;
  68. long code;
  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. inbuf = xzalloc(IBUFSIZ + 64);
  92. outbuf = xzalloc(OBUFSIZ + 2048);
  93. htab = xzalloc(HSIZE); /* wsn't zeroed out before, maybe can xmalloc? */
  94. codetab = xzalloc(HSIZE * sizeof(codetab[0]));
  95. insize = 0;
  96. /* xread isn't good here, we have to return - caller may want
  97. * to do some cleanup (e.g. delete incomplete unpacked file etc) */
  98. if (full_read(fd_in, inbuf, 1) != 1) {
  99. bb_error_msg("short read");
  100. goto err;
  101. }
  102. maxbits = inbuf[0] & BIT_MASK;
  103. block_mode = inbuf[0] & BLOCK_MODE;
  104. maxmaxcode = MAXCODE(maxbits);
  105. if (maxbits > BITS) {
  106. bb_error_msg("compressed with %d bits, can only handle "
  107. BITS_STR" bits", maxbits);
  108. goto err;
  109. }
  110. n_bits = INIT_BITS;
  111. maxcode = MAXCODE(INIT_BITS) - 1;
  112. bitmask = (1 << INIT_BITS) - 1;
  113. oldcode = -1;
  114. finchar = 0;
  115. outpos = 0;
  116. posbits = 0 << 3;
  117. free_ent = ((block_mode) ? FIRST : 256);
  118. /* As above, initialize the first 256 entries in the table. */
  119. /*clear_tab_prefixof(); - done by xzalloc */
  120. for (code = 255; code >= 0; --code) {
  121. tab_suffixof(code) = (unsigned char) code;
  122. }
  123. do {
  124. resetbuf:
  125. {
  126. int i;
  127. int e;
  128. int o;
  129. o = posbits >> 3;
  130. e = insize - o;
  131. for (i = 0; i < e; ++i)
  132. inbuf[i] = inbuf[i + o];
  133. insize = e;
  134. posbits = 0;
  135. }
  136. if (insize < (int) (IBUFSIZ + 64) - IBUFSIZ) {
  137. rsize = safe_read(fd_in, inbuf + insize, IBUFSIZ);
  138. //error check??
  139. insize += rsize;
  140. }
  141. inbits = ((rsize > 0) ? (insize - insize % n_bits) << 3 :
  142. (insize << 3) - (n_bits - 1));
  143. while (inbits > posbits) {
  144. if (free_ent > maxcode) {
  145. posbits =
  146. ((posbits - 1) +
  147. ((n_bits << 3) -
  148. (posbits - 1 + (n_bits << 3)) % (n_bits << 3)));
  149. ++n_bits;
  150. if (n_bits == maxbits) {
  151. maxcode = maxmaxcode;
  152. } else {
  153. maxcode = MAXCODE(n_bits) - 1;
  154. }
  155. bitmask = (1 << n_bits) - 1;
  156. goto resetbuf;
  157. }
  158. {
  159. unsigned char *p = &inbuf[posbits >> 3];
  160. code = ((((long) (p[0])) | ((long) (p[1]) << 8) |
  161. ((long) (p[2]) << 16)) >> (posbits & 0x7)) & bitmask;
  162. }
  163. posbits += n_bits;
  164. if (oldcode == -1) {
  165. oldcode = code;
  166. finchar = (int) oldcode;
  167. outbuf[outpos++] = (unsigned char) finchar;
  168. continue;
  169. }
  170. if (code == CLEAR && block_mode) {
  171. clear_tab_prefixof();
  172. free_ent = FIRST - 1;
  173. posbits =
  174. ((posbits - 1) +
  175. ((n_bits << 3) -
  176. (posbits - 1 + (n_bits << 3)) % (n_bits << 3)));
  177. n_bits = INIT_BITS;
  178. maxcode = MAXCODE(INIT_BITS) - 1;
  179. bitmask = (1 << INIT_BITS) - 1;
  180. goto resetbuf;
  181. }
  182. incode = code;
  183. stackp = de_stack;
  184. /* Special case for KwKwK string. */
  185. if (code >= free_ent) {
  186. if (code > free_ent) {
  187. unsigned char *p;
  188. posbits -= n_bits;
  189. p = &inbuf[posbits >> 3];
  190. bb_error_msg
  191. ("insize:%d posbits:%d inbuf:%02X %02X %02X %02X %02X (%d)",
  192. insize, posbits, p[-1], p[0], p[1], p[2], p[3],
  193. (posbits & 07));
  194. bb_error_msg("uncompress: corrupt input");
  195. goto err;
  196. }
  197. *--stackp = (unsigned char) finchar;
  198. code = oldcode;
  199. }
  200. /* Generate output characters in reverse order */
  201. while ((long) code >= (long) 256) {
  202. *--stackp = tab_suffixof(code);
  203. code = tab_prefixof(code);
  204. }
  205. finchar = tab_suffixof(code);
  206. *--stackp = (unsigned char) finchar;
  207. /* And put them out in forward order */
  208. {
  209. int i;
  210. i = de_stack - stackp;
  211. if (outpos + i >= OBUFSIZ) {
  212. do {
  213. if (i > OBUFSIZ - outpos) {
  214. i = OBUFSIZ - outpos;
  215. }
  216. if (i > 0) {
  217. memcpy(outbuf + outpos, stackp, i);
  218. outpos += i;
  219. }
  220. if (outpos >= OBUFSIZ) {
  221. full_write(fd_out, outbuf, outpos);
  222. //error check??
  223. USE_DESKTOP(total_written += outpos;)
  224. outpos = 0;
  225. }
  226. stackp += i;
  227. i = de_stack - stackp;
  228. } while (i > 0);
  229. } else {
  230. memcpy(outbuf + outpos, stackp, i);
  231. outpos += i;
  232. }
  233. }
  234. /* Generate the new entry. */
  235. code = free_ent;
  236. if (code < maxmaxcode) {
  237. tab_prefixof(code) = (unsigned short) oldcode;
  238. tab_suffixof(code) = (unsigned char) finchar;
  239. free_ent = code + 1;
  240. }
  241. /* Remember previous code. */
  242. oldcode = incode;
  243. }
  244. } while (rsize > 0);
  245. if (outpos > 0) {
  246. full_write(fd_out, outbuf, outpos);
  247. //error check??
  248. USE_DESKTOP(total_written += outpos;)
  249. }
  250. retval = USE_DESKTOP(total_written) + 0;
  251. err:
  252. free(inbuf);
  253. free(outbuf);
  254. free(htab);
  255. free(codetab);
  256. return retval;
  257. }