istereo2

annotate libs/zlib/inffast.c @ 20:2b85d05df3f2

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