annotate lib/zlib/inffast.c @ 149:13b2a2cd4552

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