| 1 | /* |
|---|
| 2 | * $Id: //suprahd/releases/suprahd_163/suprahd_ztvapp640_163/drivers/graphics/ZLIB/inffast.c#1 $ |
|---|
| 3 | * $Revision: #1 $ |
|---|
| 4 | * $DateTime: 2006/02/24 17:51:46 $ |
|---|
| 5 | * $Change: 42566 $ |
|---|
| 6 | * $Author: pryush.sharma $ |
|---|
| 7 | */ |
|---|
| 8 | |
|---|
| 9 | /* inffast.c -- process literals and length/distance pairs fast |
|---|
| 10 | * Copyright (C) 1995-1998 Mark Adler |
|---|
| 11 | * For conditions of distribution and use, see copyright notice in zlib.h |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #include "zutil.h" |
|---|
| 15 | #include "inftrees.h" |
|---|
| 16 | #include "infblock.h" |
|---|
| 17 | #include "infcodes.h" |
|---|
| 18 | #include "infutil.h" |
|---|
| 19 | #include "inffast.h" |
|---|
| 20 | |
|---|
| 21 | struct inflate_codes_state {int dummy;}; /* for buggy compilers */ |
|---|
| 22 | |
|---|
| 23 | /* simplify the use of the inflate_huft type with some defines */ |
|---|
| 24 | #define exop word.what.Exop |
|---|
| 25 | #define bits word.what.Bits |
|---|
| 26 | |
|---|
| 27 | /* macros for bit input with no checking and for returning unused bytes */ |
|---|
| 28 | #define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}} |
|---|
| 29 | #define UNGRAB {c=z->avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;} |
|---|
| 30 | |
|---|
| 31 | /* Called with number of bytes left to write in window at least 258 |
|---|
| 32 | (the maximum string length) and number of input bytes available |
|---|
| 33 | at least ten. The ten bytes are six bytes for the longest length/ |
|---|
| 34 | distance pair plus four bytes for overloading the bit buffer. */ |
|---|
| 35 | |
|---|
| 36 | int inflate_fast( |
|---|
| 37 | uInt bl, uInt bd, |
|---|
| 38 | inflate_huft *tl, |
|---|
| 39 | inflate_huft *td, /* need separate declaration for Borland C++ */ |
|---|
| 40 | inflate_blocks_statef *s, |
|---|
| 41 | z_streamp z) |
|---|
| 42 | { |
|---|
| 43 | inflate_huft *t; /* temporary pointer */ |
|---|
| 44 | uInt e; /* extra bits or operation */ |
|---|
| 45 | uLong b; /* bit buffer */ |
|---|
| 46 | uInt k; /* bits in bit buffer */ |
|---|
| 47 | Bytef *p; /* input data pointer */ |
|---|
| 48 | uInt n; /* bytes available there */ |
|---|
| 49 | Bytef *q; /* output window write pointer */ |
|---|
| 50 | uInt m; /* bytes to end of window or read pointer */ |
|---|
| 51 | uInt ml; /* mask for literal/length tree */ |
|---|
| 52 | uInt md; /* mask for distance tree */ |
|---|
| 53 | uInt c; /* bytes to copy */ |
|---|
| 54 | uInt d; /* distance back to copy from */ |
|---|
| 55 | Bytef *r; /* copy source pointer */ |
|---|
| 56 | |
|---|
| 57 | /* load input, output, bit values */ |
|---|
| 58 | LOAD |
|---|
| 59 | |
|---|
| 60 | /* initialize masks */ |
|---|
| 61 | ml = inflate_mask[bl]; |
|---|
| 62 | md = inflate_mask[bd]; |
|---|
| 63 | |
|---|
| 64 | /* do until not enough input or output space for fast loop */ |
|---|
| 65 | do { /* assume called with m >= 258 && n >= 10 */ |
|---|
| 66 | /* get literal/length code */ |
|---|
| 67 | GRABBITS(20) /* max bits for literal/length code */ |
|---|
| 68 | if ((e = (t = tl + ((uInt)b & ml))->exop) == 0) |
|---|
| 69 | { |
|---|
| 70 | DUMPBITS(t->bits) |
|---|
| 71 | Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ? |
|---|
| 72 | "inflate: * literal '%c'\n" : |
|---|
| 73 | "inflate: * literal 0x%02x\n", t->base)); |
|---|
| 74 | *q++ = (Byte)t->base; |
|---|
| 75 | m--; |
|---|
| 76 | continue; |
|---|
| 77 | } |
|---|
| 78 | do { |
|---|
| 79 | DUMPBITS(t->bits) |
|---|
| 80 | if (e & 16) |
|---|
| 81 | { |
|---|
| 82 | /* get extra bits for length */ |
|---|
| 83 | e &= 15; |
|---|
| 84 | c = t->base + ((uInt)b & inflate_mask[e]); |
|---|
| 85 | DUMPBITS(e) |
|---|
| 86 | Tracevv((stderr, "inflate: * length %u\n", c)); |
|---|
| 87 | |
|---|
| 88 | /* decode distance base of block to copy */ |
|---|
| 89 | GRABBITS(15); /* max bits for distance code */ |
|---|
| 90 | e = (t = td + ((uInt)b & md))->exop; |
|---|
| 91 | do { |
|---|
| 92 | DUMPBITS(t->bits) |
|---|
| 93 | if (e & 16) |
|---|
| 94 | { |
|---|
| 95 | /* get extra bits to add to distance base */ |
|---|
| 96 | e &= 15; |
|---|
| 97 | GRABBITS(e) /* get extra bits (up to 13) */ |
|---|
| 98 | d = t->base + ((uInt)b & inflate_mask[e]); |
|---|
| 99 | DUMPBITS(e) |
|---|
| 100 | Tracevv((stderr, "inflate: * distance %u\n", d)); |
|---|
| 101 | |
|---|
| 102 | /* do the copy */ |
|---|
| 103 | m -= c; |
|---|
| 104 | if ((uInt)(q - s->window) >= d) /* offset before dest */ |
|---|
| 105 | { /* just copy */ |
|---|
| 106 | r = q - d; |
|---|
| 107 | *q++ = *r++; c--; /* minimum count is three, */ |
|---|
| 108 | *q++ = *r++; c--; /* so unroll loop a little */ |
|---|
| 109 | } |
|---|
| 110 | else /* else offset after destination */ |
|---|
| 111 | { |
|---|
| 112 | e = d - (uInt)(q - s->window); /* bytes from offset to end */ |
|---|
| 113 | r = s->end - e; /* pointer to offset */ |
|---|
| 114 | if (c > e) /* if source crosses, */ |
|---|
| 115 | { |
|---|
| 116 | c -= e; /* copy to end of window */ |
|---|
| 117 | do { |
|---|
| 118 | *q++ = *r++; |
|---|
| 119 | } while (--e); |
|---|
| 120 | r = s->window; /* copy rest from start of window */ |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | do { /* copy all or what's left */ |
|---|
| 124 | *q++ = *r++; |
|---|
| 125 | } while (--c); |
|---|
| 126 | break; |
|---|
| 127 | } |
|---|
| 128 | else if ((e & 64) == 0) |
|---|
| 129 | { |
|---|
| 130 | t += t->base; |
|---|
| 131 | e = (t += ((uInt)b & inflate_mask[e]))->exop; |
|---|
| 132 | } |
|---|
| 133 | else |
|---|
| 134 | { |
|---|
| 135 | z->msg = (char*)"invalid distance code"; |
|---|
| 136 | UNGRAB |
|---|
| 137 | UPDATE_ZLIB |
|---|
| 138 | return Z_DATA_ERROR; |
|---|
| 139 | } |
|---|
| 140 | } while (1); |
|---|
| 141 | break; |
|---|
| 142 | } |
|---|
| 143 | if ((e & 64) == 0) |
|---|
| 144 | { |
|---|
| 145 | t += t->base; |
|---|
| 146 | if ((e = (t += ((uInt)b & inflate_mask[e]))->exop) == 0) |
|---|
| 147 | { |
|---|
| 148 | DUMPBITS(t->bits) |
|---|
| 149 | Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ? |
|---|
| 150 | "inflate: * literal '%c'\n" : |
|---|
| 151 | "inflate: * literal 0x%02x\n", t->base)); |
|---|
| 152 | *q++ = (Byte)t->base; |
|---|
| 153 | m--; |
|---|
| 154 | break; |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | else if (e & 32) |
|---|
| 158 | { |
|---|
| 159 | Tracevv((stderr, "inflate: * end of block\n")); |
|---|
| 160 | UNGRAB |
|---|
| 161 | UPDATE_ZLIB |
|---|
| 162 | return Z_STREAM_END; |
|---|
| 163 | } |
|---|
| 164 | else |
|---|
| 165 | { |
|---|
| 166 | z->msg = (char*)"invalid literal/length code"; |
|---|
| 167 | UNGRAB |
|---|
| 168 | UPDATE_ZLIB |
|---|
| 169 | return Z_DATA_ERROR; |
|---|
| 170 | } |
|---|
| 171 | } while (1); |
|---|
| 172 | } while (m >= 258 && n >= 10); |
|---|
| 173 | |
|---|
| 174 | /* not enough input or output--restore pointers and return */ |
|---|
| 175 | UNGRAB |
|---|
| 176 | UPDATE_ZLIB |
|---|
| 177 | return Z_OK; |
|---|
| 178 | } |
|---|