/src/freetype/src/gzip/infcodes.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 250 lines · 222 code · 17 blank · 11 comment · 26 complexity · 6e31a6e69f1ca532d4b985d5a8424306 MD5 · raw file

  1. /* infcodes.c -- process literals and length/distance pairs
  2. * Copyright (C) 1995-2002 Mark Adler
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. */
  5. #include "zutil.h"
  6. #include "inftrees.h"
  7. #include "infblock.h"
  8. #include "infcodes.h"
  9. #include "infutil.h"
  10. /* simplify the use of the inflate_huft type with some defines */
  11. #define exop word.what.Exop
  12. #define bits word.what.Bits
  13. typedef enum { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
  14. START, /* x: set up for LEN */
  15. LEN, /* i: get length/literal/eob next */
  16. LENEXT, /* i: getting length extra (have base) */
  17. DIST, /* i: get distance next */
  18. DISTEXT, /* i: getting distance extra */
  19. COPY, /* o: copying bytes in window, waiting for space */
  20. LIT, /* o: got literal, waiting for output space */
  21. WASH, /* o: got eob, possibly still output waiting */
  22. END, /* x: got eob and all data flushed */
  23. BADCODE} /* x: got error */
  24. inflate_codes_mode;
  25. /* inflate codes private state */
  26. struct inflate_codes_state {
  27. /* mode */
  28. inflate_codes_mode mode; /* current inflate_codes mode */
  29. /* mode dependent information */
  30. uInt len;
  31. union {
  32. struct {
  33. inflate_huft *tree; /* pointer into tree */
  34. uInt need; /* bits needed */
  35. } code; /* if LEN or DIST, where in tree */
  36. uInt lit; /* if LIT, literal */
  37. struct {
  38. uInt get; /* bits to get for extra */
  39. uInt dist; /* distance back to copy from */
  40. } copy; /* if EXT or COPY, where and how much */
  41. } sub; /* submode */
  42. /* mode independent information */
  43. Byte lbits; /* ltree bits decoded per branch */
  44. Byte dbits; /* dtree bits decoder per branch */
  45. inflate_huft *ltree; /* literal/length/eob tree */
  46. inflate_huft *dtree; /* distance tree */
  47. };
  48. local inflate_codes_statef *inflate_codes_new( /* bl, bd, tl, td, z) */
  49. uInt bl, uInt bd,
  50. inflate_huft *tl,
  51. inflate_huft *td, /* need separate declaration for Borland C++ */
  52. z_streamp z )
  53. {
  54. inflate_codes_statef *c;
  55. if ((c = (inflate_codes_statef *)
  56. ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
  57. {
  58. c->mode = START;
  59. c->lbits = (Byte)bl;
  60. c->dbits = (Byte)bd;
  61. c->ltree = tl;
  62. c->dtree = td;
  63. Tracev((stderr, "inflate: codes new\n"));
  64. }
  65. return c;
  66. }
  67. local int inflate_codes( /* s, z, r) */
  68. inflate_blocks_statef *s,
  69. z_streamp z,
  70. int r )
  71. {
  72. uInt j; /* temporary storage */
  73. inflate_huft *t; /* temporary pointer */
  74. uInt e; /* extra bits or operation */
  75. uLong b; /* bit buffer */
  76. uInt k; /* bits in bit buffer */
  77. Bytef *p; /* input data pointer */
  78. uInt n; /* bytes available there */
  79. Bytef *q; /* output window write pointer */
  80. uInt m; /* bytes to end of window or read pointer */
  81. Bytef *f; /* pointer to copy strings from */
  82. inflate_codes_statef *c = s->sub.decode.codes; /* codes state */
  83. /* copy input/output information to locals (UPDATE macro restores) */
  84. LOAD
  85. /* process input and output based on current state */
  86. while (1) switch (c->mode)
  87. { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
  88. case START: /* x: set up for LEN */
  89. #ifndef SLOW
  90. if (m >= 258 && n >= 10)
  91. {
  92. UPDATE
  93. r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
  94. LOAD
  95. if (r != Z_OK)
  96. {
  97. c->mode = r == Z_STREAM_END ? WASH : BADCODE;
  98. break;
  99. }
  100. }
  101. #endif /* !SLOW */
  102. c->sub.code.need = c->lbits;
  103. c->sub.code.tree = c->ltree;
  104. c->mode = LEN;
  105. case LEN: /* i: get length/literal/eob next */
  106. j = c->sub.code.need;
  107. NEEDBITS(j)
  108. t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
  109. DUMPBITS(t->bits)
  110. e = (uInt)(t->exop);
  111. if (e == 0) /* literal */
  112. {
  113. c->sub.lit = t->base;
  114. Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
  115. "inflate: literal '%c'\n" :
  116. "inflate: literal 0x%02x\n", t->base));
  117. c->mode = LIT;
  118. break;
  119. }
  120. if (e & 16) /* length */
  121. {
  122. c->sub.copy.get = e & 15;
  123. c->len = t->base;
  124. c->mode = LENEXT;
  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. if (e & 32) /* end of block */
  134. {
  135. Tracevv((stderr, "inflate: end of block\n"));
  136. c->mode = WASH;
  137. break;
  138. }
  139. c->mode = BADCODE; /* invalid code */
  140. z->msg = (char*)"invalid literal/length code";
  141. r = Z_DATA_ERROR;
  142. LEAVE
  143. case LENEXT: /* i: getting length extra (have base) */
  144. j = c->sub.copy.get;
  145. NEEDBITS(j)
  146. c->len += (uInt)b & inflate_mask[j];
  147. DUMPBITS(j)
  148. c->sub.code.need = c->dbits;
  149. c->sub.code.tree = c->dtree;
  150. Tracevv((stderr, "inflate: length %u\n", c->len));
  151. c->mode = DIST;
  152. case DIST: /* i: get distance next */
  153. j = c->sub.code.need;
  154. NEEDBITS(j)
  155. t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
  156. DUMPBITS(t->bits)
  157. e = (uInt)(t->exop);
  158. if (e & 16) /* distance */
  159. {
  160. c->sub.copy.get = e & 15;
  161. c->sub.copy.dist = t->base;
  162. c->mode = DISTEXT;
  163. break;
  164. }
  165. if ((e & 64) == 0) /* next table */
  166. {
  167. c->sub.code.need = e;
  168. c->sub.code.tree = t + t->base;
  169. break;
  170. }
  171. c->mode = BADCODE; /* invalid code */
  172. z->msg = (char*)"invalid distance code";
  173. r = Z_DATA_ERROR;
  174. LEAVE
  175. case DISTEXT: /* i: getting distance extra */
  176. j = c->sub.copy.get;
  177. NEEDBITS(j)
  178. c->sub.copy.dist += (uInt)b & inflate_mask[j];
  179. DUMPBITS(j)
  180. Tracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist));
  181. c->mode = COPY;
  182. case COPY: /* o: copying bytes in window, waiting for space */
  183. f = q - c->sub.copy.dist;
  184. while (f < s->window) /* modulo window size-"while" instead */
  185. f += s->end - s->window; /* of "if" handles invalid distances */
  186. while (c->len)
  187. {
  188. NEEDOUT
  189. OUTBYTE(*f++)
  190. if (f == s->end)
  191. f = s->window;
  192. c->len--;
  193. }
  194. c->mode = START;
  195. break;
  196. case LIT: /* o: got literal, waiting for output space */
  197. NEEDOUT
  198. OUTBYTE(c->sub.lit)
  199. c->mode = START;
  200. break;
  201. case WASH: /* o: got eob, possibly more output */
  202. if (k > 7) /* return unused byte, if any */
  203. {
  204. Assert(k < 16, "inflate_codes grabbed too many bytes")
  205. k -= 8;
  206. n++;
  207. p--; /* can always return one */
  208. }
  209. FLUSH
  210. if (s->read != s->write)
  211. LEAVE
  212. c->mode = END;
  213. case END:
  214. r = Z_STREAM_END;
  215. LEAVE
  216. case BADCODE: /* x: got error */
  217. r = Z_DATA_ERROR;
  218. LEAVE
  219. default:
  220. r = Z_STREAM_ERROR;
  221. LEAVE
  222. }
  223. #ifdef NEED_DUMMY_RETURN
  224. return Z_STREAM_ERROR; /* Some dumb compilers complain without this */
  225. #endif
  226. }
  227. local void inflate_codes_free( /* c, z) */
  228. inflate_codes_statef *c,
  229. z_streamp z )
  230. {
  231. ZFREE(z, c);
  232. Tracev((stderr, "inflate: codes free\n"));
  233. }