123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724 |
- /* inffast.c -- fast decoding
- * Copyright (C) 1995-2004 Mark Adler
- * For conditions of distribution and use, see copyright notice in zlib.h
- */
- #include "zutil.h"
- #include "inftrees.h"
- #include "inflate.h"
- #include "inffast.h"
- #ifdef __FALSE__
- void inflate_fast(strm, start)
- z_streamp strm;
- unsigned start; /* inflate()'s starting value for strm->avail_out */
- {
- struct inflate_state FAR *state;
- struct inffast_ar {
- void *esp; /* esp save */
- unsigned char FAR *in; /* local strm->next_in */
- unsigned char FAR *last; /* while in < last, enough input available */
- unsigned char FAR *out; /* local strm->next_out */
- unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
- unsigned char FAR *end; /* while out < end, enough space available */
- unsigned wsize; /* window size or zero if not using window */
- unsigned write; /* window write index */
- unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
- unsigned long hold; /* local strm->hold */
- unsigned bits; /* local strm->bits */
- code const FAR *lcode; /* local strm->lencode */
- code const FAR *dcode; /* local strm->distcode */
- unsigned lmask; /* mask for first level of length codes */
- unsigned dmask; /* mask for first level of distance codes */
- unsigned len; /* match length, unused bytes */
- unsigned dist; /* match distance */
- unsigned status; /* this is set when state changes */
- } ar;
- /* copy state to local variables */
- state = (struct inflate_state FAR *)strm->state;
- ar.in = strm->next_in;
- ar.last = ar.in + (strm->avail_in - 5);
- ar.out = strm->next_out;
- ar.beg = ar.out - (start - strm->avail_out);
- ar.end = ar.out + (strm->avail_out - 257);
- ar.wsize = state->wsize;
- ar.write = state->write;
- ar.window = state->window;
- ar.hold = state->hold;
- ar.bits = state->bits;
- ar.lcode = state->lencode;
- ar.dcode = state->distcode;
- ar.lmask = (1U << state->lenbits) - 1;
- ar.dmask = (1U << state->distbits) - 1;
- /* decode literals and length/distances until end-of-block or not enough
- input data or output space */
- /* align in on 2 byte boundary */
- if (((unsigned long)(void *)ar.in & 0x1) != 0) {
- ar.hold += (unsigned long)*ar.in++ << ar.bits;
- ar.bits += 8;
- }
- __asm__ __volatile__ (
- " leal %0, %%eax\n"
- " pushf\n"
- " pushl %%ebp\n"
- " movl %%esp, (%%eax)\n"
- " movl %%eax, %%esp\n"
- " movl 4(%%esp), %%esi\n" /* esi = in */
- " movl 12(%%esp), %%edi\n" /* edi = out */
- " movl 36(%%esp), %%edx\n" /* edx = hold */
- " movl 40(%%esp), %%ebx\n" /* ebx = bits */
- " movl 44(%%esp), %%ebp\n" /* ebp = lcode */
- " cld\n"
- " jmp .L_do_loop\n"
- ".L_while_test:\n"
- " cmpl %%edi, 20(%%esp)\n"
- " jbe .L_break_loop\n"
- " cmpl %%esi, 8(%%esp)\n"
- " jbe .L_break_loop\n"
- ".L_do_loop:\n"
- " cmpb $15, %%bl\n"
- " ja .L_get_length_code\n" /* if (15 < bits) */
- " xorl %%eax, %%eax\n"
- " lodsw\n" /* al = *(ushort *)in++ */
- " movb %%bl, %%cl\n" /* cl = bits, needs it for shifting */
- " addb $16, %%bl\n" /* bits += 16 */
- " shll %%cl, %%eax\n"
- " orl %%eax, %%edx\n" /* hold |= *((ushort *)in)++ << bits */
- ".L_get_length_code:\n"
- " movl 52(%%esp), %%eax\n" /* eax = lmask */
- " andl %%edx, %%eax\n" /* eax &= hold */
- " movl (%%ebp,%%eax,4), %%eax\n" /* eax = lcode[hold & lmask] */
- ".L_dolen:\n"
- " movb %%ah, %%cl\n" /* cl = this.bits */
- " subb %%ah, %%bl\n" /* bits -= this.bits */
- " shrl %%cl, %%edx\n" /* hold >>= this.bits */
- " testb %%al, %%al\n"
- " jnz .L_test_for_length_base\n" /* if (op != 0) 45.7% */
- " shrl $16, %%eax\n" /* output this.val char */
- " stosb\n"
- " jmp .L_while_test\n"
- ".L_test_for_length_base:\n"
- " movl %%eax, %%ecx\n" /* len = this */
- " shrl $16, %%ecx\n" /* len = this.val */
- " movl %%ecx, 60(%%esp)\n" /* len = this */
- " movb %%al, %%cl\n"
- " testb $16, %%al\n"
- " jz .L_test_for_second_level_length\n" /* if ((op & 16) == 0) 8% */
- " andb $15, %%cl\n" /* op &= 15 */
- " jz .L_decode_distance\n" /* if (!op) */
- " cmpb %%cl, %%bl\n"
- " jae .L_add_bits_to_len\n" /* if (op <= bits) */
- " movb %%cl, %%ch\n" /* stash op in ch, freeing cl */
- " xorl %%eax, %%eax\n"
- " lodsw\n" /* al = *(ushort *)in++ */
- " movb %%bl, %%cl\n" /* cl = bits, needs it for shifting */
- " addb $16, %%bl\n" /* bits += 16 */
- " shll %%cl, %%eax\n"
- " orl %%eax, %%edx\n" /* hold |= *((ushort *)in)++ << bits */
- " movb %%ch, %%cl\n" /* move op back to ecx */
- ".L_add_bits_to_len:\n"
- " movl $1, %%eax\n"
- " shll %%cl, %%eax\n"
- " decl %%eax\n"
- " subb %%cl, %%bl\n"
- " andl %%edx, %%eax\n" /* eax &= hold */
- " shrl %%cl, %%edx\n"
- " addl %%eax, 60(%%esp)\n" /* len += hold & mask[op] */
- ".L_decode_distance:\n"
- " cmpb $15, %%bl\n"
- " ja .L_get_distance_code\n" /* if (15 < bits) */
- " xorl %%eax, %%eax\n"
- " lodsw\n" /* al = *(ushort *)in++ */
- " movb %%bl, %%cl\n" /* cl = bits, needs it for shifting */
- " addb $16, %%bl\n" /* bits += 16 */
- " shll %%cl, %%eax\n"
- " orl %%eax, %%edx\n" /* hold |= *((ushort *)in)++ << bits */
- ".L_get_distance_code:\n"
- " movl 56(%%esp), %%eax\n" /* eax = dmask */
- " movl 48(%%esp), %%ecx\n" /* ecx = dcode */
- " andl %%edx, %%eax\n" /* eax &= hold */
- " movl (%%ecx,%%eax,4), %%eax\n"/* eax = dcode[hold & dmask] */
- ".L_dodist:\n"
- " movl %%eax, %%ebp\n" /* dist = this */
- " shrl $16, %%ebp\n" /* dist = this.val */
- " movb %%ah, %%cl\n"
- " subb %%ah, %%bl\n" /* bits -= this.bits */
- " shrl %%cl, %%edx\n" /* hold >>= this.bits */
- " movb %%al, %%cl\n" /* cl = this.op */
- " testb $16, %%al\n" /* if ((op & 16) == 0) */
- " jz .L_test_for_second_level_dist\n"
- " andb $15, %%cl\n" /* op &= 15 */
- " jz .L_check_dist_one\n"
- " cmpb %%cl, %%bl\n"
- " jae .L_add_bits_to_dist\n" /* if (op <= bits) 97.6% */
- " movb %%cl, %%ch\n" /* stash op in ch, freeing cl */
- " xorl %%eax, %%eax\n"
- " lodsw\n" /* al = *(ushort *)in++ */
- " movb %%bl, %%cl\n" /* cl = bits, needs it for shifting */
- " addb $16, %%bl\n" /* bits += 16 */
- " shll %%cl, %%eax\n"
- " orl %%eax, %%edx\n" /* hold |= *((ushort *)in)++ << bits */
- " movb %%ch, %%cl\n" /* move op back to ecx */
- ".L_add_bits_to_dist:\n"
- " movl $1, %%eax\n"
- " shll %%cl, %%eax\n"
- " decl %%eax\n" /* (1 << op) - 1 */
- " subb %%cl, %%bl\n"
- " andl %%edx, %%eax\n" /* eax &= hold */
- " shrl %%cl, %%edx\n"
- " addl %%eax, %%ebp\n" /* dist += hold & ((1 << op) - 1) */
- ".L_check_window:\n"
- " movl %%esi, 4(%%esp)\n" /* save in so from can use it's reg */
- " movl %%edi, %%eax\n"
- " subl 16(%%esp), %%eax\n" /* nbytes = out - beg */
- " cmpl %%ebp, %%eax\n"
- " jb .L_clip_window\n" /* if (dist > nbytes) 4.2% */
- " movl 60(%%esp), %%ecx\n"
- " movl %%edi, %%esi\n"
- " subl %%ebp, %%esi\n" /* from = out - dist */
- " subl $3, %%ecx\n" /* copy from to out */
- " movb (%%esi), %%al\n"
- " movb %%al, (%%edi)\n"
- " movb 1(%%esi), %%al\n"
- " movb 2(%%esi), %%ah\n"
- " addl $3, %%esi\n"
- " movb %%al, 1(%%edi)\n"
- " movb %%ah, 2(%%edi)\n"
- " addl $3, %%edi\n"
- " rep movsb\n"
- " movl 4(%%esp), %%esi\n" /* move in back to %esi, toss from */
- " movl 44(%%esp), %%ebp\n" /* ebp = lcode */
- " jmp .L_while_test\n"
- ".L_check_dist_one:\n"
- " cmpl $1, %%ebp\n" /* if dist 1, is a memset */
- " jne .L_check_window\n"
- " cmpl %%edi, 16(%%esp)\n"
- " je .L_check_window\n"
- " decl %%edi\n"
- " movl 60(%%esp), %%ecx\n"
- " movb (%%edi), %%al\n"
- " subl $3, %%ecx\n"
- " movb %%al, 1(%%edi)\n" /* memset out with from[-1] */
- " movb %%al, 2(%%edi)\n"
- " movb %%al, 3(%%edi)\n"
- " addl $4, %%edi\n"
- " rep stosb\n"
- " movl 44(%%esp), %%ebp\n" /* ebp = lcode */
- " jmp .L_while_test\n"
- ".L_test_for_second_level_length:\n"
- " testb $64, %%al\n"
- " jnz .L_test_for_end_of_block\n" /* if ((op & 64) != 0) */
- " movl $1, %%eax\n"
- " shll %%cl, %%eax\n"
- " decl %%eax\n"
- " andl %%edx, %%eax\n" /* eax &= hold */
- " addl 60(%%esp), %%eax\n" /* eax += this.val */
- " movl (%%ebp,%%eax,4), %%eax\n" /* eax = lcode[val+(hold&mask[op])]*/
- " jmp .L_dolen\n"
- ".L_test_for_second_level_dist:\n"
- " testb $64, %%al\n"
- " jnz .L_invalid_distance_code\n" /* if ((op & 64) != 0) */
- " movl $1, %%eax\n"
- " shll %%cl, %%eax\n"
- " decl %%eax\n"
- " andl %%edx, %%eax\n" /* eax &= hold */
- " addl %%ebp, %%eax\n" /* eax += this.val */
- " movl 48(%%esp), %%ecx\n" /* ecx = dcode */
- " movl (%%ecx,%%eax,4), %%eax\n" /* eax = dcode[val+(hold&mask[op])]*/
- " jmp .L_dodist\n"
- ".L_clip_window:\n"
- " movl %%eax, %%ecx\n"
- " movl 24(%%esp), %%eax\n" /* prepare for dist compare */
- " negl %%ecx\n" /* nbytes = -nbytes */
- " movl 32(%%esp), %%esi\n" /* from = window */
- " cmpl %%ebp, %%eax\n"
- " jb .L_invalid_distance_too_far\n" /* if (dist > wsize) */
- " addl %%ebp, %%ecx\n" /* nbytes = dist - nbytes */
- " cmpl $0, 28(%%esp)\n"
- " jne .L_wrap_around_window\n" /* if (write != 0) */
- " subl %%ecx, %%eax\n"
- " addl %%eax, %%esi\n" /* from += wsize - nbytes */
- " movl 60(%%esp), %%eax\n"
- " cmpl %%ecx, %%eax\n"
- " jbe .L_do_copy1\n" /* if (nbytes >= len) */
- " subl %%ecx, %%eax\n" /* len -= nbytes */
- " rep movsb\n"
- " movl %%edi, %%esi\n"
- " subl %%ebp, %%esi\n" /* from = out - dist */
- " jmp .L_do_copy1\n"
- " cmpl %%ecx, %%eax\n"
- " jbe .L_do_copy1\n" /* if (nbytes >= len) */
- " subl %%ecx, %%eax\n" /* len -= nbytes */
- " rep movsb\n"
- " movl %%edi, %%esi\n"
- " subl %%ebp, %%esi\n" /* from = out - dist */
- " jmp .L_do_copy1\n"
- ".L_wrap_around_window:\n"
- " movl 28(%%esp), %%eax\n"
- " cmpl %%eax, %%ecx\n"
- " jbe .L_contiguous_in_window\n" /* if (write >= nbytes) */
- " addl 24(%%esp), %%esi\n"
- " addl %%eax, %%esi\n"
- " subl %%ecx, %%esi\n" /* from += wsize + write - nbytes */
- " subl %%eax, %%ecx\n" /* nbytes -= write */
- " movl 60(%%esp), %%eax\n"
- " cmpl %%ecx, %%eax\n"
- " jbe .L_do_copy1\n" /* if (nbytes >= len) */
- " subl %%ecx, %%eax\n" /* len -= nbytes */
- " rep movsb\n"
- " movl 32(%%esp), %%esi\n" /* from = window */
- " movl 28(%%esp), %%ecx\n" /* nbytes = write */
- " cmpl %%ecx, %%eax\n"
- " jbe .L_do_copy1\n" /* if (nbytes >= len) */
- " subl %%ecx, %%eax\n" /* len -= nbytes */
- " rep movsb\n"
- " movl %%edi, %%esi\n"
- " subl %%ebp, %%esi\n" /* from = out - dist */
- " jmp .L_do_copy1\n"
- ".L_contiguous_in_window:\n"
- " addl %%eax, %%esi\n"
- " subl %%ecx, %%esi\n" /* from += write - nbytes */
- " movl 60(%%esp), %%eax\n"
- " cmpl %%ecx, %%eax\n"
- " jbe .L_do_copy1\n" /* if (nbytes >= len) */
- " subl %%ecx, %%eax\n" /* len -= nbytes */
- " rep movsb\n"
- " movl %%edi, %%esi\n"
- " subl %%ebp, %%esi\n" /* from = out - dist */
- ".L_do_copy1:\n"
- " movl %%eax, %%ecx\n"
- " rep movsb\n"
- " movl 4(%%esp), %%esi\n" /* move in back to %esi, toss from */
- " movl 44(%%esp), %%ebp\n" /* ebp = lcode */
- " jmp .L_while_test\n"
- ".L_test_for_end_of_block:\n"
- " testb $32, %%al\n"
- " jz .L_invalid_literal_length_code\n"
- " movl $1, 68(%%esp)\n"
- " jmp .L_break_loop_with_status\n"
- ".L_invalid_literal_length_code:\n"
- " movl $2, 68(%%esp)\n"
- " jmp .L_break_loop_with_status\n"
- ".L_invalid_distance_code:\n"
- " movl $3, 68(%%esp)\n"
- " jmp .L_break_loop_with_status\n"
- ".L_invalid_distance_too_far:\n"
- " movl 4(%%esp), %%esi\n"
- " movl $4, 68(%%esp)\n"
- " jmp .L_break_loop_with_status\n"
- ".L_break_loop:\n"
- " movl $0, 68(%%esp)\n"
- ".L_break_loop_with_status:\n"
- /* put in, out, bits, and hold back into ar and pop esp */
- " movl %%esi, 4(%%esp)\n"
- " movl %%edi, 12(%%esp)\n"
- " movl %%ebx, 40(%%esp)\n"
- " movl %%edx, 36(%%esp)\n"
- " movl (%%esp), %%esp\n"
- " popl %%ebp\n"
- " popf\n"
- :
- : "m" (ar)
- : "memory", "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi"
- );
- if (ar.status > 1) {
- if (ar.status == 2)
- strm->msg = "invalid literal/length code";
- else if (ar.status == 3)
- strm->msg = "invalid distance code";
- else
- strm->msg = "invalid distance too far back";
- state->mode = BAD;
- }
- else if ( ar.status == 1 ) {
- state->mode = TYPE;
- }
- /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
- ar.len = ar.bits >> 3;
- ar.in -= ar.len;
- ar.bits -= ar.len << 3;
- ar.hold &= (1U << ar.bits) - 1;
- /* update state and return */
- strm->next_in = ar.in;
- strm->next_out = ar.out;
- strm->avail_in = (unsigned)(ar.in < ar.last ? 5 + (ar.last - ar.in) :
- 5 - (ar.in - ar.last));
- strm->avail_out = (unsigned)(ar.out < ar.end ? 257 + (ar.end - ar.out) :
- 257 - (ar.out - ar.end));
- state->hold = ar.hold;
- state->bits = ar.bits;
- return;
- }
- #else
- /* Allow machine dependent optimization for post-increment or pre-increment.
- Based on testing to date,
- Pre-increment preferred for:
- - PowerPC G3 (Adler)
- - MIPS R5000 (Randers-Pehrson)
- Post-increment preferred for:
- - none
- No measurable difference:
- - Pentium III (Anderson)
- - M68060 (Nikl)
- */
- #ifdef POSTINC
- # define OFF 0
- # define PUP(a) *(a)++
- #else
- # define OFF 1
- # define PUP(a) *++(a)
- #endif
- /*
- Decode literal, length, and distance codes and write out the resulting
- literal and match bytes until either not enough input or output is
- available, an end-of-block is encountered, or a data error is encountered.
- When large enough input and output buffers are supplied to inflate(), for
- example, a 16K input buffer and a 64K output buffer, more than 95% of the
- inflate execution time is spent in this routine.
- Entry assumptions:
- state->mode == LEN
- strm->avail_in >= 6
- strm->avail_out >= 258
- start >= strm->avail_out
- state->bits < 8
- On return, state->mode is one of:
- LEN -- ran out of enough output space or enough available input
- TYPE -- reached end of block code, inflate() to interpret next block
- BAD -- error in block data
- Notes:
- - The maximum input bits used by a length/distance pair is 15 bits for the
- length code, 5 bits for the length extra, 15 bits for the distance code,
- and 13 bits for the distance extra. This totals 48 bits, or six bytes.
- Therefore if strm->avail_in >= 6, then there is enough input to avoid
- checking for available input while decoding.
- - The maximum bytes that a single length/distance pair can output is 258
- bytes, which is the maximum length that can be coded. inflate_fast()
- requires strm->avail_out >= 258 for each loop to avoid checking for
- output space.
- */
- void inflate_fast(strm, start)
- z_streamp strm;
- unsigned start; /* inflate()'s starting value for strm->avail_out */
- {
- struct inflate_state FAR *state;
- unsigned char FAR *in; /* local strm->next_in */
- unsigned char FAR *last; /* while in < last, enough input available */
- unsigned char FAR *out; /* local strm->next_out */
- unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
- unsigned char FAR *end; /* while out < end, enough space available */
- #ifdef INFLATE_STRICT
- unsigned dmax; /* maximum distance from zlib header */
- #endif
- unsigned wsize; /* window size or zero if not using window */
- unsigned whave; /* valid bytes in the window */
- unsigned write; /* window write index */
- unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
- unsigned long hold; /* local strm->hold */
- unsigned bits; /* local strm->bits */
- code const FAR *lcode; /* local strm->lencode */
- code const FAR *dcode; /* local strm->distcode */
- unsigned lmask; /* mask for first level of length codes */
- unsigned dmask; /* mask for first level of distance codes */
- code this; /* retrieved table entry */
- unsigned op; /* code bits, operation, extra bits, or */
- /* window position, window bytes to copy */
- unsigned len; /* match length, unused bytes */
- unsigned dist; /* match distance */
- unsigned char FAR *from; /* where to copy match from */
- /* copy state to local variables */
- state = (struct inflate_state FAR *)strm->state;
- in = strm->next_in - OFF;
- last = in + (strm->avail_in - 5);
- out = strm->next_out - OFF;
- beg = out - (start - strm->avail_out);
- end = out + (strm->avail_out - 257);
- #ifdef INFLATE_STRICT
- dmax = state->dmax;
- #endif
- wsize = state->wsize;
- whave = state->whave;
- write = state->write;
- window = state->window;
- hold = state->hold;
- bits = state->bits;
- lcode = state->lencode;
- dcode = state->distcode;
- lmask = (1U << state->lenbits) - 1;
- dmask = (1U << state->distbits) - 1;
- /* decode literals and length/distances until end-of-block or not enough
- input data or output space */
- do {
- if (bits < 15) {
- hold += (unsigned long)(PUP(in)) << bits;
- bits += 8;
- hold += (unsigned long)(PUP(in)) << bits;
- bits += 8;
- }
- this = lcode[hold & lmask];
- dolen:
- op = (unsigned)(this.bits);
- hold >>= op;
- bits -= op;
- op = (unsigned)(this.op);
- if (op == 0) { /* literal */
- Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
- "inflate: literal '%c'\n" :
- "inflate: literal 0x%02x\n", this.val));
- PUP(out) = (unsigned char)(this.val);
- }
- else if (op & 16) { /* length base */
- len = (unsigned)(this.val);
- op &= 15; /* number of extra bits */
- if (op) {
- if (bits < op) {
- hold += (unsigned long)(PUP(in)) << bits;
- bits += 8;
- }
- len += (unsigned)hold & ((1U << op) - 1);
- hold >>= op;
- bits -= op;
- }
- Tracevv((stderr, "inflate: length %u\n", len));
- if (bits < 15) {
- hold += (unsigned long)(PUP(in)) << bits;
- bits += 8;
- hold += (unsigned long)(PUP(in)) << bits;
- bits += 8;
- }
- this = dcode[hold & dmask];
- dodist:
- op = (unsigned)(this.bits);
- hold >>= op;
- bits -= op;
- op = (unsigned)(this.op);
- if (op & 16) { /* distance base */
- dist = (unsigned)(this.val);
- op &= 15; /* number of extra bits */
- if (bits < op) {
- hold += (unsigned long)(PUP(in)) << bits;
- bits += 8;
- if (bits < op) {
- hold += (unsigned long)(PUP(in)) << bits;
- bits += 8;
- }
- }
- dist += (unsigned)hold & ((1U << op) - 1);
- #ifdef INFLATE_STRICT
- if (dist > dmax) {
- strm->msg = (char *)"invalid distance too far back";
- state->mode = BAD;
- break;
- }
- #endif
- hold >>= op;
- bits -= op;
- Tracevv((stderr, "inflate: distance %u\n", dist));
- op = (unsigned)(out - beg); /* max distance in output */
- if (dist > op) { /* see if copy from window */
- op = dist - op; /* distance back in window */
- if (op > whave) {
- strm->msg = (char *)"invalid distance too far back";
- state->mode = BAD;
- break;
- }
- from = window - OFF;
- if (write == 0) { /* very common case */
- from += wsize - op;
- if (op < len) { /* some from window */
- len -= op;
- do {
- PUP(out) = PUP(from);
- } while (--op);
- from = out - dist; /* rest from output */
- }
- }
- else if (write < op) { /* wrap around window */
- from += wsize + write - op;
- op -= write;
- if (op < len) { /* some from end of window */
- len -= op;
- do {
- PUP(out) = PUP(from);
- } while (--op);
- from = window - OFF;
- if (write < len) { /* some from start of window */
- op = write;
- len -= op;
- do {
- PUP(out) = PUP(from);
- } while (--op);
- from = out - dist; /* rest from output */
- }
- }
- }
- else { /* contiguous in window */
- from += write - op;
- if (op < len) { /* some from window */
- len -= op;
- do {
- PUP(out) = PUP(from);
- } while (--op);
- from = out - dist; /* rest from output */
- }
- }
- while (len > 2) {
- PUP(out) = PUP(from);
- PUP(out) = PUP(from);
- PUP(out) = PUP(from);
- len -= 3;
- }
- if (len) {
- PUP(out) = PUP(from);
- if (len > 1)
- PUP(out) = PUP(from);
- }
- }
- else {
- from = out - dist; /* copy direct from output */
- do { /* minimum length is three */
- PUP(out) = PUP(from);
- PUP(out) = PUP(from);
- PUP(out) = PUP(from);
- len -= 3;
- } while (len > 2);
- if (len) {
- PUP(out) = PUP(from);
- if (len > 1)
- PUP(out) = PUP(from);
- }
- }
- }
- else if ((op & 64) == 0) { /* 2nd level distance code */
- this = dcode[this.val + (hold & ((1U << op) - 1))];
- goto dodist;
- }
- else {
- strm->msg = (char *)"invalid distance code";
- state->mode = BAD;
- break;
- }
- }
- else if ((op & 64) == 0) { /* 2nd level length code */
- this = lcode[this.val + (hold & ((1U << op) - 1))];
- goto dolen;
- }
- else if (op & 32) { /* end-of-block */
- Tracevv((stderr, "inflate: end of block\n"));
- state->mode = TYPE;
- break;
- }
- else {
- strm->msg = (char *)"invalid literal/length code";
- state->mode = BAD;
- break;
- }
- } while (in < last && out < end);
- /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
- len = bits >> 3;
- in -= len;
- bits -= len << 3;
- hold &= (1U << bits) - 1;
- /* update state and return */
- strm->next_in = in + OFF;
- strm->next_out = out + OFF;
- strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
- strm->avail_out = (unsigned)(out < end ?
- 257 + (end - out) : 257 - (out - end));
- state->hold = hold;
- state->bits = bits;
- return;
- }
- /*
- inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
- - Using bit fields for code structure
- - Different op definition to avoid & for extra bits (do & for table bits)
- - Three separate decoding do-loops for direct, window, and write == 0
- - Special case for distance > 1 copies to do overlapped load and store copy
- - Explicit branch predictions (based on measured branch probabilities)
- - Deferring match copy and interspersed it with decoding subsequent codes
- - Swapping literal/length else
- - Swapping window/direct else
- - Larger unrolled copy loops (three is about right)
- - Moving len -= 3 statement into middle of loop
- */
- #endif /* !i386 */
|