inffast.c 13 KB

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