inffast.c 14 KB

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