inffast.c 12 KB

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