inffast.c 13 KB

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