inffast.c 13 KB

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