annotate lib/zlib/inffast.c @ 2201:34035536bbf5

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