/lib/zlib_inflate/inffast.c

https://github.com/psyke83/kernel_samsung_europa · C · 312 lines · 231 code · 10 blank · 71 comment · 46 complexity · d7b4ef5aa7b8c9cc0344acefe58dfa9f MD5 · raw file

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