annotate src/zlib-1.2.7/inffast.c @ 23:619f715526df sv_v2.1

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