inffast.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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 "zutil.h"
  6. #include "inftrees.h"
  7. #include "inflate.h"
  8. #include "inffast.h"
  9. #ifdef __FALSE__
  10. void inflate_fast(strm, start)
  11. z_streamp strm;
  12. unsigned start; /* inflate()'s starting value for strm->avail_out */
  13. {
  14. struct inflate_state FAR *state;
  15. struct inffast_ar {
  16. void *esp; /* esp save */
  17. unsigned char FAR *in; /* local strm->next_in */
  18. unsigned char FAR *last; /* while in < last, enough input available */
  19. unsigned char FAR *out; /* local strm->next_out */
  20. unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
  21. unsigned char FAR *end; /* while out < end, enough space available */
  22. unsigned wsize; /* window size or zero if not using window */
  23. unsigned write; /* window write index */
  24. unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
  25. unsigned long hold; /* local strm->hold */
  26. unsigned bits; /* local strm->bits */
  27. code const FAR *lcode; /* local strm->lencode */
  28. code const FAR *dcode; /* local strm->distcode */
  29. unsigned lmask; /* mask for first level of length codes */
  30. unsigned dmask; /* mask for first level of distance codes */
  31. unsigned len; /* match length, unused bytes */
  32. unsigned dist; /* match distance */
  33. unsigned status; /* this is set when state changes */
  34. } ar;
  35. /* copy state to local variables */
  36. state = (struct inflate_state FAR *)strm->state;
  37. ar.in = strm->next_in;
  38. ar.last = ar.in + (strm->avail_in - 5);
  39. ar.out = strm->next_out;
  40. ar.beg = ar.out - (start - strm->avail_out);
  41. ar.end = ar.out + (strm->avail_out - 257);
  42. ar.wsize = state->wsize;
  43. ar.write = state->write;
  44. ar.window = state->window;
  45. ar.hold = state->hold;
  46. ar.bits = state->bits;
  47. ar.lcode = state->lencode;
  48. ar.dcode = state->distcode;
  49. ar.lmask = (1U << state->lenbits) - 1;
  50. ar.dmask = (1U << state->distbits) - 1;
  51. /* decode literals and length/distances until end-of-block or not enough
  52. input data or output space */
  53. /* align in on 2 byte boundary */
  54. if (((unsigned long)(void *)ar.in & 0x1) != 0) {
  55. ar.hold += (unsigned long)*ar.in++ << ar.bits;
  56. ar.bits += 8;
  57. }
  58. __asm__ __volatile__ (
  59. " leal %0, %%eax\n"
  60. " pushf\n"
  61. " pushl %%ebp\n"
  62. " movl %%esp, (%%eax)\n"
  63. " movl %%eax, %%esp\n"
  64. " movl 4(%%esp), %%esi\n" /* esi = in */
  65. " movl 12(%%esp), %%edi\n" /* edi = out */
  66. " movl 36(%%esp), %%edx\n" /* edx = hold */
  67. " movl 40(%%esp), %%ebx\n" /* ebx = bits */
  68. " movl 44(%%esp), %%ebp\n" /* ebp = lcode */
  69. " cld\n"
  70. " jmp .L_do_loop\n"
  71. ".L_while_test:\n"
  72. " cmpl %%edi, 20(%%esp)\n"
  73. " jbe .L_break_loop\n"
  74. " cmpl %%esi, 8(%%esp)\n"
  75. " jbe .L_break_loop\n"
  76. ".L_do_loop:\n"
  77. " cmpb $15, %%bl\n"
  78. " ja .L_get_length_code\n" /* if (15 < bits) */
  79. " xorl %%eax, %%eax\n"
  80. " lodsw\n" /* al = *(ushort *)in++ */
  81. " movb %%bl, %%cl\n" /* cl = bits, needs it for shifting */
  82. " addb $16, %%bl\n" /* bits += 16 */
  83. " shll %%cl, %%eax\n"
  84. " orl %%eax, %%edx\n" /* hold |= *((ushort *)in)++ << bits */
  85. ".L_get_length_code:\n"
  86. " movl 52(%%esp), %%eax\n" /* eax = lmask */
  87. " andl %%edx, %%eax\n" /* eax &= hold */
  88. " movl (%%ebp,%%eax,4), %%eax\n" /* eax = lcode[hold & lmask] */
  89. ".L_dolen:\n"
  90. " movb %%ah, %%cl\n" /* cl = this.bits */
  91. " subb %%ah, %%bl\n" /* bits -= this.bits */
  92. " shrl %%cl, %%edx\n" /* hold >>= this.bits */
  93. " testb %%al, %%al\n"
  94. " jnz .L_test_for_length_base\n" /* if (op != 0) 45.7% */
  95. " shrl $16, %%eax\n" /* output this.val char */
  96. " stosb\n"
  97. " jmp .L_while_test\n"
  98. ".L_test_for_length_base:\n"
  99. " movl %%eax, %%ecx\n" /* len = this */
  100. " shrl $16, %%ecx\n" /* len = this.val */
  101. " movl %%ecx, 60(%%esp)\n" /* len = this */
  102. " movb %%al, %%cl\n"
  103. " testb $16, %%al\n"
  104. " jz .L_test_for_second_level_length\n" /* if ((op & 16) == 0) 8% */
  105. " andb $15, %%cl\n" /* op &= 15 */
  106. " jz .L_decode_distance\n" /* if (!op) */
  107. " cmpb %%cl, %%bl\n"
  108. " jae .L_add_bits_to_len\n" /* if (op <= bits) */
  109. " movb %%cl, %%ch\n" /* stash op in ch, freeing cl */
  110. " xorl %%eax, %%eax\n"
  111. " lodsw\n" /* al = *(ushort *)in++ */
  112. " movb %%bl, %%cl\n" /* cl = bits, needs it for shifting */
  113. " addb $16, %%bl\n" /* bits += 16 */
  114. " shll %%cl, %%eax\n"
  115. " orl %%eax, %%edx\n" /* hold |= *((ushort *)in)++ << bits */
  116. " movb %%ch, %%cl\n" /* move op back to ecx */
  117. ".L_add_bits_to_len:\n"
  118. " movl $1, %%eax\n"
  119. " shll %%cl, %%eax\n"
  120. " decl %%eax\n"
  121. " subb %%cl, %%bl\n"
  122. " andl %%edx, %%eax\n" /* eax &= hold */
  123. " shrl %%cl, %%edx\n"
  124. " addl %%eax, 60(%%esp)\n" /* len += hold & mask[op] */
  125. ".L_decode_distance:\n"
  126. " cmpb $15, %%bl\n"
  127. " ja .L_get_distance_code\n" /* if (15 < bits) */
  128. " xorl %%eax, %%eax\n"
  129. " lodsw\n" /* al = *(ushort *)in++ */
  130. " movb %%bl, %%cl\n" /* cl = bits, needs it for shifting */
  131. " addb $16, %%bl\n" /* bits += 16 */
  132. " shll %%cl, %%eax\n"
  133. " orl %%eax, %%edx\n" /* hold |= *((ushort *)in)++ << bits */
  134. ".L_get_distance_code:\n"
  135. " movl 56(%%esp), %%eax\n" /* eax = dmask */
  136. " movl 48(%%esp), %%ecx\n" /* ecx = dcode */
  137. " andl %%edx, %%eax\n" /* eax &= hold */
  138. " movl (%%ecx,%%eax,4), %%eax\n"/* eax = dcode[hold & dmask] */
  139. ".L_dodist:\n"
  140. " movl %%eax, %%ebp\n" /* dist = this */
  141. " shrl $16, %%ebp\n" /* dist = this.val */
  142. " movb %%ah, %%cl\n"
  143. " subb %%ah, %%bl\n" /* bits -= this.bits */
  144. " shrl %%cl, %%edx\n" /* hold >>= this.bits */
  145. " movb %%al, %%cl\n" /* cl = this.op */
  146. " testb $16, %%al\n" /* if ((op & 16) == 0) */
  147. " jz .L_test_for_second_level_dist\n"
  148. " andb $15, %%cl\n" /* op &= 15 */
  149. " jz .L_check_dist_one\n"
  150. " cmpb %%cl, %%bl\n"
  151. " jae .L_add_bits_to_dist\n" /* if (op <= bits) 97.6% */
  152. " movb %%cl, %%ch\n" /* stash op in ch, freeing cl */
  153. " xorl %%eax, %%eax\n"
  154. " lodsw\n" /* al = *(ushort *)in++ */
  155. " movb %%bl, %%cl\n" /* cl = bits, needs it for shifting */
  156. " addb $16, %%bl\n" /* bits += 16 */
  157. " shll %%cl, %%eax\n"
  158. " orl %%eax, %%edx\n" /* hold |= *((ushort *)in)++ << bits */
  159. " movb %%ch, %%cl\n" /* move op back to ecx */
  160. ".L_add_bits_to_dist:\n"
  161. " movl $1, %%eax\n"
  162. " shll %%cl, %%eax\n"
  163. " decl %%eax\n" /* (1 << op) - 1 */
  164. " subb %%cl, %%bl\n"
  165. " andl %%edx, %%eax\n" /* eax &= hold */
  166. " shrl %%cl, %%edx\n"
  167. " addl %%eax, %%ebp\n" /* dist += hold & ((1 << op) - 1) */
  168. ".L_check_window:\n"
  169. " movl %%esi, 4(%%esp)\n" /* save in so from can use it's reg */
  170. " movl %%edi, %%eax\n"
  171. " subl 16(%%esp), %%eax\n" /* nbytes = out - beg */
  172. " cmpl %%ebp, %%eax\n"
  173. " jb .L_clip_window\n" /* if (dist > nbytes) 4.2% */
  174. " movl 60(%%esp), %%ecx\n"
  175. " movl %%edi, %%esi\n"
  176. " subl %%ebp, %%esi\n" /* from = out - dist */
  177. " subl $3, %%ecx\n" /* copy from to out */
  178. " movb (%%esi), %%al\n"
  179. " movb %%al, (%%edi)\n"
  180. " movb 1(%%esi), %%al\n"
  181. " movb 2(%%esi), %%ah\n"
  182. " addl $3, %%esi\n"
  183. " movb %%al, 1(%%edi)\n"
  184. " movb %%ah, 2(%%edi)\n"
  185. " addl $3, %%edi\n"
  186. " rep movsb\n"
  187. " movl 4(%%esp), %%esi\n" /* move in back to %esi, toss from */
  188. " movl 44(%%esp), %%ebp\n" /* ebp = lcode */
  189. " jmp .L_while_test\n"
  190. ".L_check_dist_one:\n"
  191. " cmpl $1, %%ebp\n" /* if dist 1, is a memset */
  192. " jne .L_check_window\n"
  193. " cmpl %%edi, 16(%%esp)\n"
  194. " je .L_check_window\n"
  195. " decl %%edi\n"
  196. " movl 60(%%esp), %%ecx\n"
  197. " movb (%%edi), %%al\n"
  198. " subl $3, %%ecx\n"
  199. " movb %%al, 1(%%edi)\n" /* memset out with from[-1] */
  200. " movb %%al, 2(%%edi)\n"
  201. " movb %%al, 3(%%edi)\n"
  202. " addl $4, %%edi\n"
  203. " rep stosb\n"
  204. " movl 44(%%esp), %%ebp\n" /* ebp = lcode */
  205. " jmp .L_while_test\n"
  206. ".L_test_for_second_level_length:\n"
  207. " testb $64, %%al\n"
  208. " jnz .L_test_for_end_of_block\n" /* if ((op & 64) != 0) */
  209. " movl $1, %%eax\n"
  210. " shll %%cl, %%eax\n"
  211. " decl %%eax\n"
  212. " andl %%edx, %%eax\n" /* eax &= hold */
  213. " addl 60(%%esp), %%eax\n" /* eax += this.val */
  214. " movl (%%ebp,%%eax,4), %%eax\n" /* eax = lcode[val+(hold&mask[op])]*/
  215. " jmp .L_dolen\n"
  216. ".L_test_for_second_level_dist:\n"
  217. " testb $64, %%al\n"
  218. " jnz .L_invalid_distance_code\n" /* if ((op & 64) != 0) */
  219. " movl $1, %%eax\n"
  220. " shll %%cl, %%eax\n"
  221. " decl %%eax\n"
  222. " andl %%edx, %%eax\n" /* eax &= hold */
  223. " addl %%ebp, %%eax\n" /* eax += this.val */
  224. " movl 48(%%esp), %%ecx\n" /* ecx = dcode */
  225. " movl (%%ecx,%%eax,4), %%eax\n" /* eax = dcode[val+(hold&mask[op])]*/
  226. " jmp .L_dodist\n"
  227. ".L_clip_window:\n"
  228. " movl %%eax, %%ecx\n"
  229. " movl 24(%%esp), %%eax\n" /* prepare for dist compare */
  230. " negl %%ecx\n" /* nbytes = -nbytes */
  231. " movl 32(%%esp), %%esi\n" /* from = window */
  232. " cmpl %%ebp, %%eax\n"
  233. " jb .L_invalid_distance_too_far\n" /* if (dist > wsize) */
  234. " addl %%ebp, %%ecx\n" /* nbytes = dist - nbytes */
  235. " cmpl $0, 28(%%esp)\n"
  236. " jne .L_wrap_around_window\n" /* if (write != 0) */
  237. " subl %%ecx, %%eax\n"
  238. " addl %%eax, %%esi\n" /* from += wsize - nbytes */
  239. " movl 60(%%esp), %%eax\n"
  240. " cmpl %%ecx, %%eax\n"
  241. " jbe .L_do_copy1\n" /* if (nbytes >= len) */
  242. " subl %%ecx, %%eax\n" /* len -= nbytes */
  243. " rep movsb\n"
  244. " movl %%edi, %%esi\n"
  245. " subl %%ebp, %%esi\n" /* from = out - dist */
  246. " jmp .L_do_copy1\n"
  247. " cmpl %%ecx, %%eax\n"
  248. " jbe .L_do_copy1\n" /* if (nbytes >= len) */
  249. " subl %%ecx, %%eax\n" /* len -= nbytes */
  250. " rep movsb\n"
  251. " movl %%edi, %%esi\n"
  252. " subl %%ebp, %%esi\n" /* from = out - dist */
  253. " jmp .L_do_copy1\n"
  254. ".L_wrap_around_window:\n"
  255. " movl 28(%%esp), %%eax\n"
  256. " cmpl %%eax, %%ecx\n"
  257. " jbe .L_contiguous_in_window\n" /* if (write >= nbytes) */
  258. " addl 24(%%esp), %%esi\n"
  259. " addl %%eax, %%esi\n"
  260. " subl %%ecx, %%esi\n" /* from += wsize + write - nbytes */
  261. " subl %%eax, %%ecx\n" /* nbytes -= write */
  262. " movl 60(%%esp), %%eax\n"
  263. " cmpl %%ecx, %%eax\n"
  264. " jbe .L_do_copy1\n" /* if (nbytes >= len) */
  265. " subl %%ecx, %%eax\n" /* len -= nbytes */
  266. " rep movsb\n"
  267. " movl 32(%%esp), %%esi\n" /* from = window */
  268. " movl 28(%%esp), %%ecx\n" /* nbytes = write */
  269. " cmpl %%ecx, %%eax\n"
  270. " jbe .L_do_copy1\n" /* if (nbytes >= len) */
  271. " subl %%ecx, %%eax\n" /* len -= nbytes */
  272. " rep movsb\n"
  273. " movl %%edi, %%esi\n"
  274. " subl %%ebp, %%esi\n" /* from = out - dist */
  275. " jmp .L_do_copy1\n"
  276. ".L_contiguous_in_window:\n"
  277. " addl %%eax, %%esi\n"
  278. " subl %%ecx, %%esi\n" /* from += write - nbytes */
  279. " movl 60(%%esp), %%eax\n"
  280. " cmpl %%ecx, %%eax\n"
  281. " jbe .L_do_copy1\n" /* if (nbytes >= len) */
  282. " subl %%ecx, %%eax\n" /* len -= nbytes */
  283. " rep movsb\n"
  284. " movl %%edi, %%esi\n"
  285. " subl %%ebp, %%esi\n" /* from = out - dist */
  286. ".L_do_copy1:\n"
  287. " movl %%eax, %%ecx\n"
  288. " rep movsb\n"
  289. " movl 4(%%esp), %%esi\n" /* move in back to %esi, toss from */
  290. " movl 44(%%esp), %%ebp\n" /* ebp = lcode */
  291. " jmp .L_while_test\n"
  292. ".L_test_for_end_of_block:\n"
  293. " testb $32, %%al\n"
  294. " jz .L_invalid_literal_length_code\n"
  295. " movl $1, 68(%%esp)\n"
  296. " jmp .L_break_loop_with_status\n"
  297. ".L_invalid_literal_length_code:\n"
  298. " movl $2, 68(%%esp)\n"
  299. " jmp .L_break_loop_with_status\n"
  300. ".L_invalid_distance_code:\n"
  301. " movl $3, 68(%%esp)\n"
  302. " jmp .L_break_loop_with_status\n"
  303. ".L_invalid_distance_too_far:\n"
  304. " movl 4(%%esp), %%esi\n"
  305. " movl $4, 68(%%esp)\n"
  306. " jmp .L_break_loop_with_status\n"
  307. ".L_break_loop:\n"
  308. " movl $0, 68(%%esp)\n"
  309. ".L_break_loop_with_status:\n"
  310. /* put in, out, bits, and hold back into ar and pop esp */
  311. " movl %%esi, 4(%%esp)\n"
  312. " movl %%edi, 12(%%esp)\n"
  313. " movl %%ebx, 40(%%esp)\n"
  314. " movl %%edx, 36(%%esp)\n"
  315. " movl (%%esp), %%esp\n"
  316. " popl %%ebp\n"
  317. " popf\n"
  318. :
  319. : "m" (ar)
  320. : "memory", "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi"
  321. );
  322. if (ar.status > 1) {
  323. if (ar.status == 2)
  324. strm->msg = "invalid literal/length code";
  325. else if (ar.status == 3)
  326. strm->msg = "invalid distance code";
  327. else
  328. strm->msg = "invalid distance too far back";
  329. state->mode = BAD;
  330. }
  331. else if ( ar.status == 1 ) {
  332. state->mode = TYPE;
  333. }
  334. /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
  335. ar.len = ar.bits >> 3;
  336. ar.in -= ar.len;
  337. ar.bits -= ar.len << 3;
  338. ar.hold &= (1U << ar.bits) - 1;
  339. /* update state and return */
  340. strm->next_in = ar.in;
  341. strm->next_out = ar.out;
  342. strm->avail_in = (unsigned)(ar.in < ar.last ? 5 + (ar.last - ar.in) :
  343. 5 - (ar.in - ar.last));
  344. strm->avail_out = (unsigned)(ar.out < ar.end ? 257 + (ar.end - ar.out) :
  345. 257 - (ar.out - ar.end));
  346. state->hold = ar.hold;
  347. state->bits = ar.bits;
  348. return;
  349. }
  350. #else
  351. /* Allow machine dependent optimization for post-increment or pre-increment.
  352. Based on testing to date,
  353. Pre-increment preferred for:
  354. - PowerPC G3 (Adler)
  355. - MIPS R5000 (Randers-Pehrson)
  356. Post-increment preferred for:
  357. - none
  358. No measurable difference:
  359. - Pentium III (Anderson)
  360. - M68060 (Nikl)
  361. */
  362. #ifdef POSTINC
  363. # define OFF 0
  364. # define PUP(a) *(a)++
  365. #else
  366. # define OFF 1
  367. # define PUP(a) *++(a)
  368. #endif
  369. /*
  370. Decode literal, length, and distance codes and write out the resulting
  371. literal and match bytes until either not enough input or output is
  372. available, an end-of-block is encountered, or a data error is encountered.
  373. When large enough input and output buffers are supplied to inflate(), for
  374. example, a 16K input buffer and a 64K output buffer, more than 95% of the
  375. inflate execution time is spent in this routine.
  376. Entry assumptions:
  377. state->mode == LEN
  378. strm->avail_in >= 6
  379. strm->avail_out >= 258
  380. start >= strm->avail_out
  381. state->bits < 8
  382. On return, state->mode is one of:
  383. LEN -- ran out of enough output space or enough available input
  384. TYPE -- reached end of block code, inflate() to interpret next block
  385. BAD -- error in block data
  386. Notes:
  387. - The maximum input bits used by a length/distance pair is 15 bits for the
  388. length code, 5 bits for the length extra, 15 bits for the distance code,
  389. and 13 bits for the distance extra. This totals 48 bits, or six bytes.
  390. Therefore if strm->avail_in >= 6, then there is enough input to avoid
  391. checking for available input while decoding.
  392. - The maximum bytes that a single length/distance pair can output is 258
  393. bytes, which is the maximum length that can be coded. inflate_fast()
  394. requires strm->avail_out >= 258 for each loop to avoid checking for
  395. output space.
  396. */
  397. void inflate_fast(strm, start)
  398. z_streamp strm;
  399. unsigned start; /* inflate()'s starting value for strm->avail_out */
  400. {
  401. struct inflate_state FAR *state;
  402. unsigned char FAR *in; /* local strm->next_in */
  403. unsigned char FAR *last; /* while in < last, enough input available */
  404. unsigned char FAR *out; /* local strm->next_out */
  405. unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
  406. unsigned char FAR *end; /* while out < end, enough space available */
  407. #ifdef INFLATE_STRICT
  408. unsigned dmax; /* maximum distance from zlib header */
  409. #endif
  410. unsigned wsize; /* window size or zero if not using window */
  411. unsigned whave; /* valid bytes in the window */
  412. unsigned write; /* window write index */
  413. unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
  414. unsigned long hold; /* local strm->hold */
  415. unsigned bits; /* local strm->bits */
  416. code const FAR *lcode; /* local strm->lencode */
  417. code const FAR *dcode; /* local strm->distcode */
  418. unsigned lmask; /* mask for first level of length codes */
  419. unsigned dmask; /* mask for first level of distance codes */
  420. code this; /* retrieved table entry */
  421. unsigned op; /* code bits, operation, extra bits, or */
  422. /* window position, window bytes to copy */
  423. unsigned len; /* match length, unused bytes */
  424. unsigned dist; /* match distance */
  425. unsigned char FAR *from; /* where to copy match from */
  426. /* copy state to local variables */
  427. state = (struct inflate_state FAR *)strm->state;
  428. in = strm->next_in - OFF;
  429. last = in + (strm->avail_in - 5);
  430. out = strm->next_out - OFF;
  431. beg = out - (start - strm->avail_out);
  432. end = out + (strm->avail_out - 257);
  433. #ifdef INFLATE_STRICT
  434. dmax = state->dmax;
  435. #endif
  436. wsize = state->wsize;
  437. whave = state->whave;
  438. write = state->write;
  439. window = state->window;
  440. hold = state->hold;
  441. bits = state->bits;
  442. lcode = state->lencode;
  443. dcode = state->distcode;
  444. lmask = (1U << state->lenbits) - 1;
  445. dmask = (1U << state->distbits) - 1;
  446. /* decode literals and length/distances until end-of-block or not enough
  447. input data or output space */
  448. do {
  449. if (bits < 15) {
  450. hold += (unsigned long)(PUP(in)) << bits;
  451. bits += 8;
  452. hold += (unsigned long)(PUP(in)) << bits;
  453. bits += 8;
  454. }
  455. this = lcode[hold & lmask];
  456. dolen:
  457. op = (unsigned)(this.bits);
  458. hold >>= op;
  459. bits -= op;
  460. op = (unsigned)(this.op);
  461. if (op == 0) { /* literal */
  462. Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
  463. "inflate: literal '%c'\n" :
  464. "inflate: literal 0x%02x\n", this.val));
  465. PUP(out) = (unsigned char)(this.val);
  466. }
  467. else if (op & 16) { /* length base */
  468. len = (unsigned)(this.val);
  469. op &= 15; /* number of extra bits */
  470. if (op) {
  471. if (bits < op) {
  472. hold += (unsigned long)(PUP(in)) << bits;
  473. bits += 8;
  474. }
  475. len += (unsigned)hold & ((1U << op) - 1);
  476. hold >>= op;
  477. bits -= op;
  478. }
  479. Tracevv((stderr, "inflate: length %u\n", len));
  480. if (bits < 15) {
  481. hold += (unsigned long)(PUP(in)) << bits;
  482. bits += 8;
  483. hold += (unsigned long)(PUP(in)) << bits;
  484. bits += 8;
  485. }
  486. this = dcode[hold & dmask];
  487. dodist:
  488. op = (unsigned)(this.bits);
  489. hold >>= op;
  490. bits -= op;
  491. op = (unsigned)(this.op);
  492. if (op & 16) { /* distance base */
  493. dist = (unsigned)(this.val);
  494. op &= 15; /* number of extra bits */
  495. if (bits < op) {
  496. hold += (unsigned long)(PUP(in)) << bits;
  497. bits += 8;
  498. if (bits < op) {
  499. hold += (unsigned long)(PUP(in)) << bits;
  500. bits += 8;
  501. }
  502. }
  503. dist += (unsigned)hold & ((1U << op) - 1);
  504. #ifdef INFLATE_STRICT
  505. if (dist > dmax) {
  506. strm->msg = (char *)"invalid distance too far back";
  507. state->mode = BAD;
  508. break;
  509. }
  510. #endif
  511. hold >>= op;
  512. bits -= op;
  513. Tracevv((stderr, "inflate: distance %u\n", dist));
  514. op = (unsigned)(out - beg); /* max distance in output */
  515. if (dist > op) { /* see if copy from window */
  516. op = dist - op; /* distance back in window */
  517. if (op > whave) {
  518. strm->msg = (char *)"invalid distance too far back";
  519. state->mode = BAD;
  520. break;
  521. }
  522. from = window - OFF;
  523. if (write == 0) { /* very common case */
  524. from += wsize - op;
  525. if (op < len) { /* some from window */
  526. len -= op;
  527. do {
  528. PUP(out) = PUP(from);
  529. } while (--op);
  530. from = out - dist; /* rest from output */
  531. }
  532. }
  533. else if (write < op) { /* wrap around window */
  534. from += wsize + write - op;
  535. op -= write;
  536. if (op < len) { /* some from end of window */
  537. len -= op;
  538. do {
  539. PUP(out) = PUP(from);
  540. } while (--op);
  541. from = window - OFF;
  542. if (write < len) { /* some from start of window */
  543. op = write;
  544. len -= op;
  545. do {
  546. PUP(out) = PUP(from);
  547. } while (--op);
  548. from = out - dist; /* rest from output */
  549. }
  550. }
  551. }
  552. else { /* contiguous in window */
  553. from += write - op;
  554. if (op < len) { /* some from window */
  555. len -= op;
  556. do {
  557. PUP(out) = PUP(from);
  558. } while (--op);
  559. from = out - dist; /* rest from output */
  560. }
  561. }
  562. while (len > 2) {
  563. PUP(out) = PUP(from);
  564. PUP(out) = PUP(from);
  565. PUP(out) = PUP(from);
  566. len -= 3;
  567. }
  568. if (len) {
  569. PUP(out) = PUP(from);
  570. if (len > 1)
  571. PUP(out) = PUP(from);
  572. }
  573. }
  574. else {
  575. from = out - dist; /* copy direct from output */
  576. do { /* minimum length is three */
  577. PUP(out) = PUP(from);
  578. PUP(out) = PUP(from);
  579. PUP(out) = PUP(from);
  580. len -= 3;
  581. } while (len > 2);
  582. if (len) {
  583. PUP(out) = PUP(from);
  584. if (len > 1)
  585. PUP(out) = PUP(from);
  586. }
  587. }
  588. }
  589. else if ((op & 64) == 0) { /* 2nd level distance code */
  590. this = dcode[this.val + (hold & ((1U << op) - 1))];
  591. goto dodist;
  592. }
  593. else {
  594. strm->msg = (char *)"invalid distance code";
  595. state->mode = BAD;
  596. break;
  597. }
  598. }
  599. else if ((op & 64) == 0) { /* 2nd level length code */
  600. this = lcode[this.val + (hold & ((1U << op) - 1))];
  601. goto dolen;
  602. }
  603. else if (op & 32) { /* end-of-block */
  604. Tracevv((stderr, "inflate: end of block\n"));
  605. state->mode = TYPE;
  606. break;
  607. }
  608. else {
  609. strm->msg = (char *)"invalid literal/length code";
  610. state->mode = BAD;
  611. break;
  612. }
  613. } while (in < last && out < end);
  614. /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
  615. len = bits >> 3;
  616. in -= len;
  617. bits -= len << 3;
  618. hold &= (1U << bits) - 1;
  619. /* update state and return */
  620. strm->next_in = in + OFF;
  621. strm->next_out = out + OFF;
  622. strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
  623. strm->avail_out = (unsigned)(out < end ?
  624. 257 + (end - out) : 257 - (out - end));
  625. state->hold = hold;
  626. state->bits = bits;
  627. return;
  628. }
  629. /*
  630. inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
  631. - Using bit fields for code structure
  632. - Different op definition to avoid & for extra bits (do & for table bits)
  633. - Three separate decoding do-loops for direct, window, and write == 0
  634. - Special case for distance > 1 copies to do overlapped load and store copy
  635. - Explicit branch predictions (based on measured branch probabilities)
  636. - Deferring match copy and interspersed it with decoding subsequent codes
  637. - Swapping literal/length else
  638. - Swapping window/direct else
  639. - Larger unrolled copy loops (three is about right)
  640. - Moving len -= 3 statement into middle of loop
  641. */
  642. #endif /* !i386 */