inffast.c 12 KB

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