inffast.c 13 KB

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