inffast.c 13 KB

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