infcodes.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* infcodes.c -- process literals and length/distance pairs
  2. * Copyright (C) 1995-1998 Mark Adler
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. */
  5. #include <linux/zutil.h>
  6. #include "inftrees.h"
  7. #include "infblock.h"
  8. #include "infcodes.h"
  9. #include "infutil.h"
  10. #include "inffast.h"
  11. /* simplify the use of the inflate_huft type with some defines */
  12. #define exop word.what.Exop
  13. #define bits word.what.Bits
  14. inflate_codes_statef *zlib_inflate_codes_new(
  15. uInt bl,
  16. uInt bd,
  17. inflate_huft *tl,
  18. inflate_huft *td, /* need separate declaration for Borland C++ */
  19. z_streamp z
  20. )
  21. {
  22. inflate_codes_statef *c;
  23. c = &WS(z)->working_state;
  24. {
  25. c->mode = START;
  26. c->lbits = (Byte)bl;
  27. c->dbits = (Byte)bd;
  28. c->ltree = tl;
  29. c->dtree = td;
  30. }
  31. return c;
  32. }
  33. int zlib_inflate_codes(
  34. inflate_blocks_statef *s,
  35. z_streamp z,
  36. int r
  37. )
  38. {
  39. uInt j; /* temporary storage */
  40. inflate_huft *t; /* temporary pointer */
  41. uInt e; /* extra bits or operation */
  42. uLong b; /* bit buffer */
  43. uInt k; /* bits in bit buffer */
  44. Byte *p; /* input data pointer */
  45. uInt n; /* bytes available there */
  46. Byte *q; /* output window write pointer */
  47. uInt m; /* bytes to end of window or read pointer */
  48. Byte *f; /* pointer to copy strings from */
  49. inflate_codes_statef *c = s->sub.decode.codes; /* codes state */
  50. /* copy input/output information to locals (UPDATE macro restores) */
  51. LOAD
  52. /* process input and output based on current state */
  53. while (1) switch (c->mode)
  54. { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
  55. case START: /* x: set up for LEN */
  56. #ifndef SLOW
  57. if (m >= 258 && n >= 10)
  58. {
  59. UPDATE
  60. r = zlib_inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
  61. LOAD
  62. if (r != Z_OK)
  63. {
  64. c->mode = r == Z_STREAM_END ? WASH : BADCODE;
  65. break;
  66. }
  67. }
  68. #endif /* !SLOW */
  69. c->sub.code.need = c->lbits;
  70. c->sub.code.tree = c->ltree;
  71. c->mode = LEN;
  72. case LEN: /* i: get length/literal/eob next */
  73. j = c->sub.code.need;
  74. NEEDBITS(j)
  75. t = c->sub.code.tree + ((uInt)b & zlib_inflate_mask[j]);
  76. DUMPBITS(t->bits)
  77. e = (uInt)(t->exop);
  78. if (e == 0) /* literal */
  79. {
  80. c->sub.lit = t->base;
  81. c->mode = LIT;
  82. break;
  83. }
  84. if (e & 16) /* length */
  85. {
  86. c->sub.copy.get = e & 15;
  87. c->len = t->base;
  88. c->mode = LENEXT;
  89. break;
  90. }
  91. if ((e & 64) == 0) /* next table */
  92. {
  93. c->sub.code.need = e;
  94. c->sub.code.tree = t + t->base;
  95. break;
  96. }
  97. if (e & 32) /* end of block */
  98. {
  99. c->mode = WASH;
  100. break;
  101. }
  102. c->mode = BADCODE; /* invalid code */
  103. z->msg = (char*)"invalid literal/length code";
  104. r = Z_DATA_ERROR;
  105. LEAVE
  106. case LENEXT: /* i: getting length extra (have base) */
  107. j = c->sub.copy.get;
  108. NEEDBITS(j)
  109. c->len += (uInt)b & zlib_inflate_mask[j];
  110. DUMPBITS(j)
  111. c->sub.code.need = c->dbits;
  112. c->sub.code.tree = c->dtree;
  113. c->mode = DIST;
  114. case DIST: /* i: get distance next */
  115. j = c->sub.code.need;
  116. NEEDBITS(j)
  117. t = c->sub.code.tree + ((uInt)b & zlib_inflate_mask[j]);
  118. DUMPBITS(t->bits)
  119. e = (uInt)(t->exop);
  120. if (e & 16) /* distance */
  121. {
  122. c->sub.copy.get = e & 15;
  123. c->sub.copy.dist = t->base;
  124. c->mode = DISTEXT;
  125. break;
  126. }
  127. if ((e & 64) == 0) /* next table */
  128. {
  129. c->sub.code.need = e;
  130. c->sub.code.tree = t + t->base;
  131. break;
  132. }
  133. c->mode = BADCODE; /* invalid code */
  134. z->msg = (char*)"invalid distance code";
  135. r = Z_DATA_ERROR;
  136. LEAVE
  137. case DISTEXT: /* i: getting distance extra */
  138. j = c->sub.copy.get;
  139. NEEDBITS(j)
  140. c->sub.copy.dist += (uInt)b & zlib_inflate_mask[j];
  141. DUMPBITS(j)
  142. c->mode = COPY;
  143. case COPY: /* o: copying bytes in window, waiting for space */
  144. f = q - c->sub.copy.dist;
  145. while (f < s->window) /* modulo window size-"while" instead */
  146. f += s->end - s->window; /* of "if" handles invalid distances */
  147. while (c->len)
  148. {
  149. NEEDOUT
  150. OUTBYTE(*f++)
  151. if (f == s->end)
  152. f = s->window;
  153. c->len--;
  154. }
  155. c->mode = START;
  156. break;
  157. case LIT: /* o: got literal, waiting for output space */
  158. NEEDOUT
  159. OUTBYTE(c->sub.lit)
  160. c->mode = START;
  161. break;
  162. case WASH: /* o: got eob, possibly more output */
  163. if (k > 7) /* return unused byte, if any */
  164. {
  165. k -= 8;
  166. n++;
  167. p--; /* can always return one */
  168. }
  169. FLUSH
  170. if (s->read != s->write)
  171. LEAVE
  172. c->mode = END;
  173. case END:
  174. r = Z_STREAM_END;
  175. LEAVE
  176. case BADCODE: /* x: got error */
  177. r = Z_DATA_ERROR;
  178. LEAVE
  179. default:
  180. r = Z_STREAM_ERROR;
  181. LEAVE
  182. }
  183. #ifdef NEED_DUMMY_RETURN
  184. return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
  185. #endif
  186. }
  187. void zlib_inflate_codes_free(
  188. inflate_codes_statef *c,
  189. z_streamp z
  190. )
  191. {
  192. }