-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpuff.patch
More file actions
160 lines (152 loc) · 5.82 KB
/
Copy pathpuff.patch
File metadata and controls
160 lines (152 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
diff --git a/puff.original.c b/puff.c
index 6737ff6..605022a 100644
--- a/puff.original.c
+++ b/puff.c
@@ -1,3 +1,5 @@
+#define INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR 1
+
/*
* puff.c
* Copyright (C) 2002-2013 Mark Adler
@@ -80,7 +82,7 @@
*/
#include <setjmp.h> /* for setjmp(), longjmp(), and jmp_buf */
-#include "puff.h" /* prototype for puff() */
+#define NIL 0
#define local static /* for local function definitions */
@@ -176,16 +178,17 @@ local int stored(struct state *s)
len |= s->in[s->incnt++] << 8;
if (s->in[s->incnt++] != (~len & 0xff) ||
s->in[s->incnt++] != ((~len >> 8) & 0xff))
- return -2; /* didn't match complement! */
+ {} /* didn't match complement; we don't care */
/* copy len bytes from in to out */
if (s->incnt + len > s->inlen)
return 2; /* not enough input */
if (s->out != NIL) {
- if (s->outcnt + len > s->outlen)
- return 1; /* not enough output space */
- while (len--)
+ while (len--) {
+ if (s->outcnt == s->outlen) /* we've produced all the output we need */
+ return 1;
s->out[s->outcnt++] = s->in[s->incnt++];
+ }
}
else { /* just scanning */
s->outcnt += len;
@@ -231,7 +234,6 @@ struct huffman {
* - Incomplete codes are handled by this decoder, since they are permitted
* in the deflate format. See the format notes for fixed() and dynamic().
*/
-#ifdef SLOW
local int decode(struct state *s, const struct huffman *h)
{
int len; /* current number of bits in code */
@@ -251,60 +253,9 @@ local int decode(struct state *s, const struct huffman *h)
first <<= 1;
code <<= 1;
}
- return -10; /* ran out of codes */
+ return decode(s, h); /* invalid code; ignore and retry */
}
-/*
- * A faster version of decode() for real applications of this code. It's not
- * as readable, but it makes puff() twice as fast. And it only makes the code
- * a few percent larger.
- */
-#else /* !SLOW */
-local int decode(struct state *s, const struct huffman *h)
-{
- int len; /* current number of bits in code */
- int code; /* len bits being decoded */
- int first; /* first code of length len */
- int count; /* number of codes of length len */
- int index; /* index of first code of length len in symbol table */
- int bitbuf; /* bits from stream */
- int left; /* bits left in next or left to process */
- short *next; /* next number of codes */
-
- bitbuf = s->bitbuf;
- left = s->bitcnt;
- code = first = index = 0;
- len = 1;
- next = h->count + 1;
- while (1) {
- while (left--) {
- code |= bitbuf & 1;
- bitbuf >>= 1;
- count = *next++;
- if (code - count < first) { /* if length len, return symbol */
- s->bitbuf = bitbuf;
- s->bitcnt = (s->bitcnt - len) & 7;
- return h->symbol[index + (code - first)];
- }
- index += count; /* else update for next length */
- first += count;
- first <<= 1;
- code <<= 1;
- len++;
- }
- left = (MAXBITS+1) - len;
- if (left == 0)
- break;
- if (s->incnt == s->inlen)
- longjmp(s->env, 1); /* out of input */
- bitbuf = s->in[s->incnt++];
- if (left > 8)
- left = 8;
- }
- return -10; /* ran out of codes */
-}
-#endif /* SLOW */
-
/*
* Given the list of code lengths length[0..n-1] representing a canonical
* Huffman code for n symbols, construct the tables required to decode those
@@ -473,7 +424,7 @@ local int codes(struct state *s,
/* get and compute length */
symbol -= 257;
if (symbol >= 29)
- return -10; /* invalid fixed code */
+ continue; /* invalid fixed code */
len = lens[symbol] + bits(s, lext[symbol]);
/* get and check distance */
@@ -488,9 +439,9 @@ local int codes(struct state *s,
/* copy length bytes from distance bytes back */
if (s->out != NIL) {
- if (s->outcnt + len > s->outlen)
- return 1;
while (len--) {
+ if (s->outcnt == s->outlen)
+ return 1;
s->out[s->outcnt] =
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
dist > s->outcnt ?
@@ -818,23 +769,20 @@ int puff(unsigned char *dest, /* pointer to destination pointer */
/* process blocks until last block or error */
do {
last = bits(&s, 1); /* one if last block */
+ last = 0; /* jk there's no last block */
type = bits(&s, 2); /* block type 0..3 */
err = type == 0 ?
stored(&s) :
(type == 1 ?
fixed(&s) :
- (type == 2 ?
- dynamic(&s) :
- -1)); /* type == 3, invalid */
+ 0); /* types 2 and 3 ignored */
if (err != 0)
break; /* return with error */
} while (!last);
}
/* update the lengths and return */
- if (err <= 0) {
- *destlen = s.outcnt;
- *sourcelen = s.incnt;
- }
+ *destlen = s.outcnt;
+ *sourcelen = s.incnt;
return err;
}