inffast.c 12 KB

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