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