dbf-halloween2015

annotate libs/zlib/inffast.c @ 1:c3f5c32cb210

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