inffast.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /* inffast.c -- fast decoding
  2. * Copyright (C) 1995-2017 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 "inflate.h"
  8. #include "inffast.h"
  9. #ifdef ASMINF
  10. # pragma message("Assembler code may have bugs -- use at your own risk")
  11. #else
  12. /*
  13. Decode literal, length, and distance codes and write out the resulting
  14. literal and match bytes until either not enough input or output is
  15. available, an end-of-block is encountered, or a data error is encountered.
  16. When large enough input and output buffers are supplied to inflate(), for
  17. example, a 16K input buffer and a 64K output buffer, more than 95% of the
  18. inflate execution time is spent in this routine.
  19. Entry assumptions:
  20. state->mode == LEN
  21. strm->avail_in >= 6
  22. strm->avail_out >= 258
  23. start >= strm->avail_out
  24. state->bits < 8
  25. On return, state->mode is one of:
  26. LEN -- ran out of enough output space or enough available input
  27. TYPE -- reached end of block code, inflate() to interpret next block
  28. BAD -- error in block data
  29. Notes:
  30. - The maximum input bits used by a length/distance pair is 15 bits for the
  31. length code, 5 bits for the length extra, 15 bits for the distance code,
  32. and 13 bits for the distance extra. This totals 48 bits, or six bytes.
  33. Therefore if strm->avail_in >= 6, then there is enough input to avoid
  34. checking for available input while decoding.
  35. - The maximum bytes that a single length/distance pair can output is 258
  36. bytes, which is the maximum length that can be coded. inflate_fast()
  37. requires strm->avail_out >= 258 for each loop to avoid checking for
  38. output space.
  39. */
  40. void ZLIB_INTERNAL inflate_fast(z_streamp strm, unsigned start) {
  41. struct inflate_state FAR *state;
  42. z_const unsigned char FAR *in; /* local strm->next_in */
  43. z_const unsigned char FAR *last; /* have enough input while in < last */
  44. unsigned char FAR *out; /* local strm->next_out */
  45. unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
  46. unsigned char FAR *end; /* while out < end, enough space available */
  47. #ifdef INFLATE_STRICT
  48. unsigned dmax; /* maximum distance from zlib header */
  49. #endif
  50. unsigned wsize; /* window size or zero if not using window */
  51. unsigned whave; /* valid bytes in the window */
  52. unsigned wnext; /* window write index */
  53. unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
  54. unsigned long hold; /* local strm->hold */
  55. unsigned bits; /* local strm->bits */
  56. code const FAR *lcode; /* local strm->lencode */
  57. code const FAR *dcode; /* local strm->distcode */
  58. unsigned lmask; /* mask for first level of length codes */
  59. unsigned dmask; /* mask for first level of distance codes */
  60. code const *here; /* retrieved table entry */
  61. unsigned op; /* code bits, operation, extra bits, or */
  62. /* window position, window bytes to copy */
  63. unsigned len; /* match length, unused bytes */
  64. unsigned dist; /* match distance */
  65. unsigned char FAR *from; /* where to copy match from */
  66. /* copy state to local variables */
  67. state = (struct inflate_state FAR *)strm->state;
  68. in = strm->next_in;
  69. last = in + (strm->avail_in - 5);
  70. out = strm->next_out;
  71. beg = out - (start - strm->avail_out);
  72. end = out + (strm->avail_out - 257);
  73. #ifdef INFLATE_STRICT
  74. dmax = state->dmax;
  75. #endif
  76. wsize = state->wsize;
  77. whave = state->whave;
  78. wnext = state->wnext;
  79. window = state->window;
  80. hold = state->hold;
  81. bits = state->bits;
  82. lcode = state->lencode;
  83. dcode = state->distcode;
  84. lmask = (1U << state->lenbits) - 1;
  85. dmask = (1U << state->distbits) - 1;
  86. /* decode literals and length/distances until end-of-block or not enough
  87. input data or output space */
  88. do {
  89. if (bits < 15) {
  90. hold += (unsigned long)(*in++) << bits;
  91. bits += 8;
  92. hold += (unsigned long)(*in++) << bits;
  93. bits += 8;
  94. }
  95. here = lcode + (hold & lmask);
  96. dolen:
  97. op = (unsigned)(here->bits);
  98. hold >>= op;
  99. bits -= op;
  100. op = (unsigned)(here->op);
  101. if (op == 0) { /* literal */
  102. Tracevv((stderr, here->val >= 0x20 && here->val < 0x7f ?
  103. "inflate: literal '%c'\n" :
  104. "inflate: literal 0x%02x\n", here->val));
  105. *out++ = (unsigned char)(here->val);
  106. }
  107. else if (op & 16) { /* length base */
  108. len = (unsigned)(here->val);
  109. op &= 15; /* number of extra bits */
  110. if (op) {
  111. if (bits < op) {
  112. hold += (unsigned long)(*in++) << bits;
  113. bits += 8;
  114. }
  115. len += (unsigned)hold & ((1U << op) - 1);
  116. hold >>= op;
  117. bits -= op;
  118. }
  119. Tracevv((stderr, "inflate: length %u\n", len));
  120. if (bits < 15) {
  121. hold += (unsigned long)(*in++) << bits;
  122. bits += 8;
  123. hold += (unsigned long)(*in++) << bits;
  124. bits += 8;
  125. }
  126. here = dcode + (hold & dmask);
  127. dodist:
  128. op = (unsigned)(here->bits);
  129. hold >>= op;
  130. bits -= op;
  131. op = (unsigned)(here->op);
  132. if (op & 16) { /* distance base */
  133. dist = (unsigned)(here->val);
  134. op &= 15; /* number of extra bits */
  135. if (bits < op) {
  136. hold += (unsigned long)(*in++) << bits;
  137. bits += 8;
  138. if (bits < op) {
  139. hold += (unsigned long)(*in++) << bits;
  140. bits += 8;
  141. }
  142. }
  143. dist += (unsigned)hold & ((1U << op) - 1);
  144. #ifdef INFLATE_STRICT
  145. if (dist > dmax) {
  146. strm->msg = (char *)"invalid distance too far back";
  147. state->mode = BAD;
  148. break;
  149. }
  150. #endif
  151. hold >>= op;
  152. bits -= op;
  153. Tracevv((stderr, "inflate: distance %u\n", dist));
  154. op = (unsigned)(out - beg); /* max distance in output */
  155. if (dist > op) { /* see if copy from window */
  156. op = dist - op; /* distance back in window */
  157. if (op > whave) {
  158. if (state->sane) {
  159. strm->msg =
  160. (char *)"invalid distance too far back";
  161. state->mode = BAD;
  162. break;
  163. }
  164. #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  165. if (len <= op - whave) {
  166. do {
  167. *out++ = 0;
  168. } while (--len);
  169. continue;
  170. }
  171. len -= op - whave;
  172. do {
  173. *out++ = 0;
  174. } while (--op > whave);
  175. if (op == 0) {
  176. from = out - dist;
  177. do {
  178. *out++ = *from++;
  179. } while (--len);
  180. continue;
  181. }
  182. #endif
  183. }
  184. from = window;
  185. if (wnext == 0) { /* very common case */
  186. from += wsize - op;
  187. if (op < len) { /* some from window */
  188. len -= op;
  189. do {
  190. *out++ = *from++;
  191. } while (--op);
  192. from = out - dist; /* rest from output */
  193. }
  194. }
  195. else if (wnext < op) { /* wrap around window */
  196. from += wsize + wnext - op;
  197. op -= wnext;
  198. if (op < len) { /* some from end of window */
  199. len -= op;
  200. do {
  201. *out++ = *from++;
  202. } while (--op);
  203. from = window;
  204. if (wnext < len) { /* some from start of window */
  205. op = wnext;
  206. len -= op;
  207. do {
  208. *out++ = *from++;
  209. } while (--op);
  210. from = out - dist; /* rest from output */
  211. }
  212. }
  213. }
  214. else { /* contiguous in window */
  215. from += wnext - op;
  216. if (op < len) { /* some from window */
  217. len -= op;
  218. do {
  219. *out++ = *from++;
  220. } while (--op);
  221. from = out - dist; /* rest from output */
  222. }
  223. }
  224. while (len > 2) {
  225. *out++ = *from++;
  226. *out++ = *from++;
  227. *out++ = *from++;
  228. len -= 3;
  229. }
  230. if (len) {
  231. *out++ = *from++;
  232. if (len > 1)
  233. *out++ = *from++;
  234. }
  235. }
  236. else {
  237. from = out - dist; /* copy direct from output */
  238. do { /* minimum length is three */
  239. *out++ = *from++;
  240. *out++ = *from++;
  241. *out++ = *from++;
  242. len -= 3;
  243. } while (len > 2);
  244. if (len) {
  245. *out++ = *from++;
  246. if (len > 1)
  247. *out++ = *from++;
  248. }
  249. }
  250. }
  251. else if ((op & 64) == 0) { /* 2nd level distance code */
  252. here = dcode + here->val + (hold & ((1U << op) - 1));
  253. goto dodist;
  254. }
  255. else {
  256. strm->msg = (char *)"invalid distance code";
  257. state->mode = BAD;
  258. break;
  259. }
  260. }
  261. else if ((op & 64) == 0) { /* 2nd level length code */
  262. here = lcode + here->val + (hold & ((1U << op) - 1));
  263. goto dolen;
  264. }
  265. else if (op & 32) { /* end-of-block */
  266. Tracevv((stderr, "inflate: end of block\n"));
  267. state->mode = TYPE;
  268. break;
  269. }
  270. else {
  271. strm->msg = (char *)"invalid literal/length code";
  272. state->mode = BAD;
  273. break;
  274. }
  275. } while (in < last && out < end);
  276. /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
  277. len = bits >> 3;
  278. in -= len;
  279. bits -= len << 3;
  280. hold &= (1U << bits) - 1;
  281. /* update state and return */
  282. strm->next_in = in;
  283. strm->next_out = out;
  284. strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
  285. strm->avail_out = (unsigned)(out < end ?
  286. 257 + (end - out) : 257 - (out - end));
  287. state->hold = hold;
  288. state->bits = bits;
  289. return;
  290. }
  291. /*
  292. inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
  293. - Using bit fields for code structure
  294. - Different op definition to avoid & for extra bits (do & for table bits)
  295. - Three separate decoding do-loops for direct, window, and wnext == 0
  296. - Special case for distance > 1 copies to do overlapped load and store copy
  297. - Explicit branch predictions (based on measured branch probabilities)
  298. - Deferring match copy and interspersed it with decoding subsequent codes
  299. - Swapping literal/length else
  300. - Swapping window/direct else
  301. - Larger unrolled copy loops (three is about right)
  302. - Moving len -= 3 statement into middle of loop
  303. */
  304. #endif /* !ASMINF */