Chris@4
|
1 /* infcover.c -- test zlib's inflate routines with full code coverage
|
Chris@4
|
2 * Copyright (C) 2011 Mark Adler
|
Chris@4
|
3 * For conditions of distribution and use, see copyright notice in zlib.h
|
Chris@4
|
4 */
|
Chris@4
|
5
|
Chris@4
|
6 /* to use, do: ./configure --cover && make cover */
|
Chris@4
|
7
|
Chris@4
|
8 #include <stdio.h>
|
Chris@4
|
9 #include <stdlib.h>
|
Chris@4
|
10 #include <string.h>
|
Chris@4
|
11 #include <assert.h>
|
Chris@4
|
12 #include "zlib.h"
|
Chris@4
|
13
|
Chris@4
|
14 /* get definition of internal structure so we can mess with it (see pull()),
|
Chris@4
|
15 and so we can call inflate_trees() (see cover5()) */
|
Chris@4
|
16 #define ZLIB_INTERNAL
|
Chris@4
|
17 #include "inftrees.h"
|
Chris@4
|
18 #include "inflate.h"
|
Chris@4
|
19
|
Chris@4
|
20 #define local static
|
Chris@4
|
21
|
Chris@4
|
22 /* -- memory tracking routines -- */
|
Chris@4
|
23
|
Chris@4
|
24 /*
|
Chris@4
|
25 These memory tracking routines are provided to zlib and track all of zlib's
|
Chris@4
|
26 allocations and deallocations, check for LIFO operations, keep a current
|
Chris@4
|
27 and high water mark of total bytes requested, optionally set a limit on the
|
Chris@4
|
28 total memory that can be allocated, and when done check for memory leaks.
|
Chris@4
|
29
|
Chris@4
|
30 They are used as follows:
|
Chris@4
|
31
|
Chris@4
|
32 z_stream strm;
|
Chris@4
|
33 mem_setup(&strm) initializes the memory tracking and sets the
|
Chris@4
|
34 zalloc, zfree, and opaque members of strm to use
|
Chris@4
|
35 memory tracking for all zlib operations on strm
|
Chris@4
|
36 mem_limit(&strm, limit) sets a limit on the total bytes requested -- a
|
Chris@4
|
37 request that exceeds this limit will result in an
|
Chris@4
|
38 allocation failure (returns NULL) -- setting the
|
Chris@4
|
39 limit to zero means no limit, which is the default
|
Chris@4
|
40 after mem_setup()
|
Chris@4
|
41 mem_used(&strm, "msg") prints to stderr "msg" and the total bytes used
|
Chris@4
|
42 mem_high(&strm, "msg") prints to stderr "msg" and the high water mark
|
Chris@4
|
43 mem_done(&strm, "msg") ends memory tracking, releases all allocations
|
Chris@4
|
44 for the tracking as well as leaked zlib blocks, if
|
Chris@4
|
45 any. If there was anything unusual, such as leaked
|
Chris@4
|
46 blocks, non-FIFO frees, or frees of addresses not
|
Chris@4
|
47 allocated, then "msg" and information about the
|
Chris@4
|
48 problem is printed to stderr. If everything is
|
Chris@4
|
49 normal, nothing is printed. mem_done resets the
|
Chris@4
|
50 strm members to Z_NULL to use the default memory
|
Chris@4
|
51 allocation routines on the next zlib initialization
|
Chris@4
|
52 using strm.
|
Chris@4
|
53 */
|
Chris@4
|
54
|
Chris@4
|
55 /* these items are strung together in a linked list, one for each allocation */
|
Chris@4
|
56 struct mem_item {
|
Chris@4
|
57 void *ptr; /* pointer to allocated memory */
|
Chris@4
|
58 size_t size; /* requested size of allocation */
|
Chris@4
|
59 struct mem_item *next; /* pointer to next item in list, or NULL */
|
Chris@4
|
60 };
|
Chris@4
|
61
|
Chris@4
|
62 /* this structure is at the root of the linked list, and tracks statistics */
|
Chris@4
|
63 struct mem_zone {
|
Chris@4
|
64 struct mem_item *first; /* pointer to first item in list, or NULL */
|
Chris@4
|
65 size_t total, highwater; /* total allocations, and largest total */
|
Chris@4
|
66 size_t limit; /* memory allocation limit, or 0 if no limit */
|
Chris@4
|
67 int notlifo, rogue; /* counts of non-LIFO frees and rogue frees */
|
Chris@4
|
68 };
|
Chris@4
|
69
|
Chris@4
|
70 /* memory allocation routine to pass to zlib */
|
Chris@4
|
71 local void *mem_alloc(void *mem, unsigned count, unsigned size)
|
Chris@4
|
72 {
|
Chris@4
|
73 void *ptr;
|
Chris@4
|
74 struct mem_item *item;
|
Chris@4
|
75 struct mem_zone *zone = mem;
|
Chris@4
|
76 size_t len = count * (size_t)size;
|
Chris@4
|
77
|
Chris@4
|
78 /* induced allocation failure */
|
Chris@4
|
79 if (zone == NULL || (zone->limit && zone->total + len > zone->limit))
|
Chris@4
|
80 return NULL;
|
Chris@4
|
81
|
Chris@4
|
82 /* perform allocation using the standard library, fill memory with a
|
Chris@4
|
83 non-zero value to make sure that the code isn't depending on zeros */
|
Chris@4
|
84 ptr = malloc(len);
|
Chris@4
|
85 if (ptr == NULL)
|
Chris@4
|
86 return NULL;
|
Chris@4
|
87 memset(ptr, 0xa5, len);
|
Chris@4
|
88
|
Chris@4
|
89 /* create a new item for the list */
|
Chris@4
|
90 item = malloc(sizeof(struct mem_item));
|
Chris@4
|
91 if (item == NULL) {
|
Chris@4
|
92 free(ptr);
|
Chris@4
|
93 return NULL;
|
Chris@4
|
94 }
|
Chris@4
|
95 item->ptr = ptr;
|
Chris@4
|
96 item->size = len;
|
Chris@4
|
97
|
Chris@4
|
98 /* insert item at the beginning of the list */
|
Chris@4
|
99 item->next = zone->first;
|
Chris@4
|
100 zone->first = item;
|
Chris@4
|
101
|
Chris@4
|
102 /* update the statistics */
|
Chris@4
|
103 zone->total += item->size;
|
Chris@4
|
104 if (zone->total > zone->highwater)
|
Chris@4
|
105 zone->highwater = zone->total;
|
Chris@4
|
106
|
Chris@4
|
107 /* return the allocated memory */
|
Chris@4
|
108 return ptr;
|
Chris@4
|
109 }
|
Chris@4
|
110
|
Chris@4
|
111 /* memory free routine to pass to zlib */
|
Chris@4
|
112 local void mem_free(void *mem, void *ptr)
|
Chris@4
|
113 {
|
Chris@4
|
114 struct mem_item *item, *next;
|
Chris@4
|
115 struct mem_zone *zone = mem;
|
Chris@4
|
116
|
Chris@4
|
117 /* if no zone, just do a free */
|
Chris@4
|
118 if (zone == NULL) {
|
Chris@4
|
119 free(ptr);
|
Chris@4
|
120 return;
|
Chris@4
|
121 }
|
Chris@4
|
122
|
Chris@4
|
123 /* point next to the item that matches ptr, or NULL if not found -- remove
|
Chris@4
|
124 the item from the linked list if found */
|
Chris@4
|
125 next = zone->first;
|
Chris@4
|
126 if (next) {
|
Chris@4
|
127 if (next->ptr == ptr)
|
Chris@4
|
128 zone->first = next->next; /* first one is it, remove from list */
|
Chris@4
|
129 else {
|
Chris@4
|
130 do { /* search the linked list */
|
Chris@4
|
131 item = next;
|
Chris@4
|
132 next = item->next;
|
Chris@4
|
133 } while (next != NULL && next->ptr != ptr);
|
Chris@4
|
134 if (next) { /* if found, remove from linked list */
|
Chris@4
|
135 item->next = next->next;
|
Chris@4
|
136 zone->notlifo++; /* not a LIFO free */
|
Chris@4
|
137 }
|
Chris@4
|
138
|
Chris@4
|
139 }
|
Chris@4
|
140 }
|
Chris@4
|
141
|
Chris@4
|
142 /* if found, update the statistics and free the item */
|
Chris@4
|
143 if (next) {
|
Chris@4
|
144 zone->total -= next->size;
|
Chris@4
|
145 free(next);
|
Chris@4
|
146 }
|
Chris@4
|
147
|
Chris@4
|
148 /* if not found, update the rogue count */
|
Chris@4
|
149 else
|
Chris@4
|
150 zone->rogue++;
|
Chris@4
|
151
|
Chris@4
|
152 /* in any case, do the requested free with the standard library function */
|
Chris@4
|
153 free(ptr);
|
Chris@4
|
154 }
|
Chris@4
|
155
|
Chris@4
|
156 /* set up a controlled memory allocation space for monitoring, set the stream
|
Chris@4
|
157 parameters to the controlled routines, with opaque pointing to the space */
|
Chris@4
|
158 local void mem_setup(z_stream *strm)
|
Chris@4
|
159 {
|
Chris@4
|
160 struct mem_zone *zone;
|
Chris@4
|
161
|
Chris@4
|
162 zone = malloc(sizeof(struct mem_zone));
|
Chris@4
|
163 assert(zone != NULL);
|
Chris@4
|
164 zone->first = NULL;
|
Chris@4
|
165 zone->total = 0;
|
Chris@4
|
166 zone->highwater = 0;
|
Chris@4
|
167 zone->limit = 0;
|
Chris@4
|
168 zone->notlifo = 0;
|
Chris@4
|
169 zone->rogue = 0;
|
Chris@4
|
170 strm->opaque = zone;
|
Chris@4
|
171 strm->zalloc = mem_alloc;
|
Chris@4
|
172 strm->zfree = mem_free;
|
Chris@4
|
173 }
|
Chris@4
|
174
|
Chris@4
|
175 /* set a limit on the total memory allocation, or 0 to remove the limit */
|
Chris@4
|
176 local void mem_limit(z_stream *strm, size_t limit)
|
Chris@4
|
177 {
|
Chris@4
|
178 struct mem_zone *zone = strm->opaque;
|
Chris@4
|
179
|
Chris@4
|
180 zone->limit = limit;
|
Chris@4
|
181 }
|
Chris@4
|
182
|
Chris@4
|
183 /* show the current total requested allocations in bytes */
|
Chris@4
|
184 local void mem_used(z_stream *strm, char *prefix)
|
Chris@4
|
185 {
|
Chris@4
|
186 struct mem_zone *zone = strm->opaque;
|
Chris@4
|
187
|
Chris@4
|
188 fprintf(stderr, "%s: %lu allocated\n", prefix, zone->total);
|
Chris@4
|
189 }
|
Chris@4
|
190
|
Chris@4
|
191 /* show the high water allocation in bytes */
|
Chris@4
|
192 local void mem_high(z_stream *strm, char *prefix)
|
Chris@4
|
193 {
|
Chris@4
|
194 struct mem_zone *zone = strm->opaque;
|
Chris@4
|
195
|
Chris@4
|
196 fprintf(stderr, "%s: %lu high water mark\n", prefix, zone->highwater);
|
Chris@4
|
197 }
|
Chris@4
|
198
|
Chris@4
|
199 /* release the memory allocation zone -- if there are any surprises, notify */
|
Chris@4
|
200 local void mem_done(z_stream *strm, char *prefix)
|
Chris@4
|
201 {
|
Chris@4
|
202 int count = 0;
|
Chris@4
|
203 struct mem_item *item, *next;
|
Chris@4
|
204 struct mem_zone *zone = strm->opaque;
|
Chris@4
|
205
|
Chris@4
|
206 /* show high water mark */
|
Chris@4
|
207 mem_high(strm, prefix);
|
Chris@4
|
208
|
Chris@4
|
209 /* free leftover allocations and item structures, if any */
|
Chris@4
|
210 item = zone->first;
|
Chris@4
|
211 while (item != NULL) {
|
Chris@4
|
212 free(item->ptr);
|
Chris@4
|
213 next = item->next;
|
Chris@4
|
214 free(item);
|
Chris@4
|
215 item = next;
|
Chris@4
|
216 count++;
|
Chris@4
|
217 }
|
Chris@4
|
218
|
Chris@4
|
219 /* issue alerts about anything unexpected */
|
Chris@4
|
220 if (count || zone->total)
|
Chris@4
|
221 fprintf(stderr, "** %s: %lu bytes in %d blocks not freed\n",
|
Chris@4
|
222 prefix, zone->total, count);
|
Chris@4
|
223 if (zone->notlifo)
|
Chris@4
|
224 fprintf(stderr, "** %s: %d frees not LIFO\n", prefix, zone->notlifo);
|
Chris@4
|
225 if (zone->rogue)
|
Chris@4
|
226 fprintf(stderr, "** %s: %d frees not recognized\n",
|
Chris@4
|
227 prefix, zone->rogue);
|
Chris@4
|
228
|
Chris@4
|
229 /* free the zone and delete from the stream */
|
Chris@4
|
230 free(zone);
|
Chris@4
|
231 strm->opaque = Z_NULL;
|
Chris@4
|
232 strm->zalloc = Z_NULL;
|
Chris@4
|
233 strm->zfree = Z_NULL;
|
Chris@4
|
234 }
|
Chris@4
|
235
|
Chris@4
|
236 /* -- inflate test routines -- */
|
Chris@4
|
237
|
Chris@4
|
238 /* Decode a hexadecimal string, set *len to length, in[] to the bytes. This
|
Chris@4
|
239 decodes liberally, in that hex digits can be adjacent, in which case two in
|
Chris@4
|
240 a row writes a byte. Or they can delimited by any non-hex character, where
|
Chris@4
|
241 the delimiters are ignored except when a single hex digit is followed by a
|
Chris@4
|
242 delimiter in which case that single digit writes a byte. The returned
|
Chris@4
|
243 data is allocated and must eventually be freed. NULL is returned if out of
|
Chris@4
|
244 memory. If the length is not needed, then len can be NULL. */
|
Chris@4
|
245 local unsigned char *h2b(const char *hex, unsigned *len)
|
Chris@4
|
246 {
|
Chris@4
|
247 unsigned char *in;
|
Chris@4
|
248 unsigned next, val;
|
Chris@4
|
249
|
Chris@4
|
250 in = malloc((strlen(hex) + 1) >> 1);
|
Chris@4
|
251 if (in == NULL)
|
Chris@4
|
252 return NULL;
|
Chris@4
|
253 next = 0;
|
Chris@4
|
254 val = 1;
|
Chris@4
|
255 do {
|
Chris@4
|
256 if (*hex >= '0' && *hex <= '9')
|
Chris@4
|
257 val = (val << 4) + *hex - '0';
|
Chris@4
|
258 else if (*hex >= 'A' && *hex <= 'F')
|
Chris@4
|
259 val = (val << 4) + *hex - 'A' + 10;
|
Chris@4
|
260 else if (*hex >= 'a' && *hex <= 'f')
|
Chris@4
|
261 val = (val << 4) + *hex - 'a' + 10;
|
Chris@4
|
262 else if (val != 1 && val < 32) /* one digit followed by delimiter */
|
Chris@4
|
263 val += 240; /* make it look like two digits */
|
Chris@4
|
264 if (val > 255) { /* have two digits */
|
Chris@4
|
265 in[next++] = val & 0xff; /* save the decoded byte */
|
Chris@4
|
266 val = 1; /* start over */
|
Chris@4
|
267 }
|
Chris@4
|
268 } while (*hex++); /* go through the loop with the terminating null */
|
Chris@4
|
269 if (len != NULL)
|
Chris@4
|
270 *len = next;
|
Chris@4
|
271 in = reallocf(in, next);
|
Chris@4
|
272 return in;
|
Chris@4
|
273 }
|
Chris@4
|
274
|
Chris@4
|
275 /* generic inflate() run, where hex is the hexadecimal input data, what is the
|
Chris@4
|
276 text to include in an error message, step is how much input data to feed
|
Chris@4
|
277 inflate() on each call, or zero to feed it all, win is the window bits
|
Chris@4
|
278 parameter to inflateInit2(), len is the size of the output buffer, and err
|
Chris@4
|
279 is the error code expected from the first inflate() call (the second
|
Chris@4
|
280 inflate() call is expected to return Z_STREAM_END). If win is 47, then
|
Chris@4
|
281 header information is collected with inflateGetHeader(). If a zlib stream
|
Chris@4
|
282 is looking for a dictionary, then an empty dictionary is provided.
|
Chris@4
|
283 inflate() is run until all of the input data is consumed. */
|
Chris@4
|
284 local void inf(char *hex, char *what, unsigned step, int win, unsigned len,
|
Chris@4
|
285 int err)
|
Chris@4
|
286 {
|
Chris@4
|
287 int ret;
|
Chris@4
|
288 unsigned have;
|
Chris@4
|
289 unsigned char *in, *out;
|
Chris@4
|
290 z_stream strm, copy;
|
Chris@4
|
291 gz_header head;
|
Chris@4
|
292
|
Chris@4
|
293 mem_setup(&strm);
|
Chris@4
|
294 strm.avail_in = 0;
|
Chris@4
|
295 strm.next_in = Z_NULL;
|
Chris@4
|
296 ret = inflateInit2(&strm, win);
|
Chris@4
|
297 if (ret != Z_OK) {
|
Chris@4
|
298 mem_done(&strm, what);
|
Chris@4
|
299 return;
|
Chris@4
|
300 }
|
Chris@4
|
301 out = malloc(len); assert(out != NULL);
|
Chris@4
|
302 if (win == 47) {
|
Chris@4
|
303 head.extra = out;
|
Chris@4
|
304 head.extra_max = len;
|
Chris@4
|
305 head.name = out;
|
Chris@4
|
306 head.name_max = len;
|
Chris@4
|
307 head.comment = out;
|
Chris@4
|
308 head.comm_max = len;
|
Chris@4
|
309 ret = inflateGetHeader(&strm, &head); assert(ret == Z_OK);
|
Chris@4
|
310 }
|
Chris@4
|
311 in = h2b(hex, &have); assert(in != NULL);
|
Chris@4
|
312 if (step == 0 || step > have)
|
Chris@4
|
313 step = have;
|
Chris@4
|
314 strm.avail_in = step;
|
Chris@4
|
315 have -= step;
|
Chris@4
|
316 strm.next_in = in;
|
Chris@4
|
317 do {
|
Chris@4
|
318 strm.avail_out = len;
|
Chris@4
|
319 strm.next_out = out;
|
Chris@4
|
320 ret = inflate(&strm, Z_NO_FLUSH); assert(err == 9 || ret == err);
|
Chris@4
|
321 if (ret != Z_OK && ret != Z_BUF_ERROR && ret != Z_NEED_DICT)
|
Chris@4
|
322 break;
|
Chris@4
|
323 if (ret == Z_NEED_DICT) {
|
Chris@4
|
324 ret = inflateSetDictionary(&strm, in, 1);
|
Chris@4
|
325 assert(ret == Z_DATA_ERROR);
|
Chris@4
|
326 mem_limit(&strm, 1);
|
Chris@4
|
327 ret = inflateSetDictionary(&strm, out, 0);
|
Chris@4
|
328 assert(ret == Z_MEM_ERROR);
|
Chris@4
|
329 mem_limit(&strm, 0);
|
Chris@4
|
330 ((struct inflate_state *)strm.state)->mode = DICT;
|
Chris@4
|
331 ret = inflateSetDictionary(&strm, out, 0);
|
Chris@4
|
332 assert(ret == Z_OK);
|
Chris@4
|
333 ret = inflate(&strm, Z_NO_FLUSH); assert(ret == Z_BUF_ERROR);
|
Chris@4
|
334 }
|
Chris@4
|
335 ret = inflateCopy(©, &strm); assert(ret == Z_OK);
|
Chris@4
|
336 ret = inflateEnd(©); assert(ret == Z_OK);
|
Chris@4
|
337 err = 9; /* don't care next time around */
|
Chris@4
|
338 have += strm.avail_in;
|
Chris@4
|
339 strm.avail_in = step > have ? have : step;
|
Chris@4
|
340 have -= strm.avail_in;
|
Chris@4
|
341 } while (strm.avail_in);
|
Chris@4
|
342 free(in);
|
Chris@4
|
343 free(out);
|
Chris@4
|
344 ret = inflateReset2(&strm, -8); assert(ret == Z_OK);
|
Chris@4
|
345 ret = inflateEnd(&strm); assert(ret == Z_OK);
|
Chris@4
|
346 mem_done(&strm, what);
|
Chris@4
|
347 }
|
Chris@4
|
348
|
Chris@4
|
349 /* cover all of the lines in inflate.c up to inflate() */
|
Chris@4
|
350 local void cover_support(void)
|
Chris@4
|
351 {
|
Chris@4
|
352 int ret;
|
Chris@4
|
353 z_stream strm;
|
Chris@4
|
354
|
Chris@4
|
355 mem_setup(&strm);
|
Chris@4
|
356 strm.avail_in = 0;
|
Chris@4
|
357 strm.next_in = Z_NULL;
|
Chris@4
|
358 ret = inflateInit(&strm); assert(ret == Z_OK);
|
Chris@4
|
359 mem_used(&strm, "inflate init");
|
Chris@4
|
360 ret = inflatePrime(&strm, 5, 31); assert(ret == Z_OK);
|
Chris@4
|
361 ret = inflatePrime(&strm, -1, 0); assert(ret == Z_OK);
|
Chris@4
|
362 ret = inflateSetDictionary(&strm, Z_NULL, 0);
|
Chris@4
|
363 assert(ret == Z_STREAM_ERROR);
|
Chris@4
|
364 ret = inflateEnd(&strm); assert(ret == Z_OK);
|
Chris@4
|
365 mem_done(&strm, "prime");
|
Chris@4
|
366
|
Chris@4
|
367 inf("63 0", "force window allocation", 0, -15, 1, Z_OK);
|
Chris@4
|
368 inf("63 18 5", "force window replacement", 0, -8, 259, Z_OK);
|
Chris@4
|
369 inf("63 18 68 30 d0 0 0", "force split window update", 4, -8, 259, Z_OK);
|
Chris@4
|
370 inf("3 0", "use fixed blocks", 0, -15, 1, Z_STREAM_END);
|
Chris@4
|
371 inf("", "bad window size", 0, 1, 0, Z_STREAM_ERROR);
|
Chris@4
|
372
|
Chris@4
|
373 mem_setup(&strm);
|
Chris@4
|
374 strm.avail_in = 0;
|
Chris@4
|
375 strm.next_in = Z_NULL;
|
Chris@4
|
376 ret = inflateInit_(&strm, ZLIB_VERSION - 1, (int)sizeof(z_stream));
|
Chris@4
|
377 assert(ret == Z_VERSION_ERROR);
|
Chris@4
|
378 mem_done(&strm, "wrong version");
|
Chris@4
|
379
|
Chris@4
|
380 strm.avail_in = 0;
|
Chris@4
|
381 strm.next_in = Z_NULL;
|
Chris@4
|
382 ret = inflateInit(&strm); assert(ret == Z_OK);
|
Chris@4
|
383 ret = inflateEnd(&strm); assert(ret == Z_OK);
|
Chris@4
|
384 fputs("inflate built-in memory routines\n", stderr);
|
Chris@4
|
385 }
|
Chris@4
|
386
|
Chris@4
|
387 /* cover all inflate() header and trailer cases and code after inflate() */
|
Chris@4
|
388 local void cover_wrap(void)
|
Chris@4
|
389 {
|
Chris@4
|
390 int ret;
|
Chris@4
|
391 z_stream strm, copy;
|
Chris@4
|
392 unsigned char dict[257];
|
Chris@4
|
393
|
Chris@4
|
394 ret = inflate(Z_NULL, 0); assert(ret == Z_STREAM_ERROR);
|
Chris@4
|
395 ret = inflateEnd(Z_NULL); assert(ret == Z_STREAM_ERROR);
|
Chris@4
|
396 ret = inflateCopy(Z_NULL, Z_NULL); assert(ret == Z_STREAM_ERROR);
|
Chris@4
|
397 fputs("inflate bad parameters\n", stderr);
|
Chris@4
|
398
|
Chris@4
|
399 inf("1f 8b 0 0", "bad gzip method", 0, 31, 0, Z_DATA_ERROR);
|
Chris@4
|
400 inf("1f 8b 8 80", "bad gzip flags", 0, 31, 0, Z_DATA_ERROR);
|
Chris@4
|
401 inf("77 85", "bad zlib method", 0, 15, 0, Z_DATA_ERROR);
|
Chris@4
|
402 inf("8 99", "set window size from header", 0, 0, 0, Z_OK);
|
Chris@4
|
403 inf("78 9c", "bad zlib window size", 0, 8, 0, Z_DATA_ERROR);
|
Chris@4
|
404 inf("78 9c 63 0 0 0 1 0 1", "check adler32", 0, 15, 1, Z_STREAM_END);
|
Chris@4
|
405 inf("1f 8b 8 1e 0 0 0 0 0 0 1 0 0 0 0 0 0", "bad header crc", 0, 47, 1,
|
Chris@4
|
406 Z_DATA_ERROR);
|
Chris@4
|
407 inf("1f 8b 8 2 0 0 0 0 0 0 1d 26 3 0 0 0 0 0 0 0 0 0", "check gzip length",
|
Chris@4
|
408 0, 47, 0, Z_STREAM_END);
|
Chris@4
|
409 inf("78 90", "bad zlib header check", 0, 47, 0, Z_DATA_ERROR);
|
Chris@4
|
410 inf("8 b8 0 0 0 1", "need dictionary", 0, 8, 0, Z_NEED_DICT);
|
Chris@4
|
411 inf("78 9c 63 0", "compute adler32", 0, 15, 1, Z_OK);
|
Chris@4
|
412
|
Chris@4
|
413 mem_setup(&strm);
|
Chris@4
|
414 strm.avail_in = 0;
|
Chris@4
|
415 strm.next_in = Z_NULL;
|
Chris@4
|
416 ret = inflateInit2(&strm, -8);
|
Chris@4
|
417 strm.avail_in = 2;
|
Chris@4
|
418 strm.next_in = (void *)"\x63";
|
Chris@4
|
419 strm.avail_out = 1;
|
Chris@4
|
420 strm.next_out = (void *)&ret;
|
Chris@4
|
421 mem_limit(&strm, 1);
|
Chris@4
|
422 ret = inflate(&strm, Z_NO_FLUSH); assert(ret == Z_MEM_ERROR);
|
Chris@4
|
423 ret = inflate(&strm, Z_NO_FLUSH); assert(ret == Z_MEM_ERROR);
|
Chris@4
|
424 mem_limit(&strm, 0);
|
Chris@4
|
425 memset(dict, 0, 257);
|
Chris@4
|
426 ret = inflateSetDictionary(&strm, dict, 257);
|
Chris@4
|
427 assert(ret == Z_OK);
|
Chris@4
|
428 mem_limit(&strm, (sizeof(struct inflate_state) << 1) + 256);
|
Chris@4
|
429 ret = inflatePrime(&strm, 16, 0); assert(ret == Z_OK);
|
Chris@4
|
430 strm.avail_in = 2;
|
Chris@4
|
431 strm.next_in = (void *)"\x80";
|
Chris@4
|
432 ret = inflateSync(&strm); assert(ret == Z_DATA_ERROR);
|
Chris@4
|
433 ret = inflate(&strm, Z_NO_FLUSH); assert(ret == Z_STREAM_ERROR);
|
Chris@4
|
434 strm.avail_in = 4;
|
Chris@4
|
435 strm.next_in = (void *)"\0\0\xff\xff";
|
Chris@4
|
436 ret = inflateSync(&strm); assert(ret == Z_OK);
|
Chris@4
|
437 (void)inflateSyncPoint(&strm);
|
Chris@4
|
438 ret = inflateCopy(©, &strm); assert(ret == Z_MEM_ERROR);
|
Chris@4
|
439 mem_limit(&strm, 0);
|
Chris@4
|
440 ret = inflateUndermine(&strm, 1); assert(ret == Z_DATA_ERROR);
|
Chris@4
|
441 (void)inflateMark(&strm);
|
Chris@4
|
442 ret = inflateEnd(&strm); assert(ret == Z_OK);
|
Chris@4
|
443 mem_done(&strm, "miscellaneous, force memory errors");
|
Chris@4
|
444 }
|
Chris@4
|
445
|
Chris@4
|
446 /* input and output functions for inflateBack() */
|
Chris@4
|
447 local unsigned pull(void *desc, unsigned char **buf)
|
Chris@4
|
448 {
|
Chris@4
|
449 static unsigned int next = 0;
|
Chris@4
|
450 static unsigned char dat[] = {0x63, 0, 2, 0};
|
Chris@4
|
451 struct inflate_state *state;
|
Chris@4
|
452
|
Chris@4
|
453 if (desc == Z_NULL) {
|
Chris@4
|
454 next = 0;
|
Chris@4
|
455 return 0; /* no input (already provided at next_in) */
|
Chris@4
|
456 }
|
Chris@4
|
457 state = (void *)((z_stream *)desc)->state;
|
Chris@4
|
458 if (state != Z_NULL)
|
Chris@4
|
459 state->mode = SYNC; /* force an otherwise impossible situation */
|
Chris@4
|
460 return next < sizeof(dat) ? (*buf = dat + next++, 1) : 0;
|
Chris@4
|
461 }
|
Chris@4
|
462
|
Chris@4
|
463 local int push(void *desc, unsigned char *buf, unsigned len)
|
Chris@4
|
464 {
|
Chris@4
|
465 buf += len;
|
Chris@4
|
466 return desc != Z_NULL; /* force error if desc not null */
|
Chris@4
|
467 }
|
Chris@4
|
468
|
Chris@4
|
469 /* cover inflateBack() up to common deflate data cases and after those */
|
Chris@4
|
470 local void cover_back(void)
|
Chris@4
|
471 {
|
Chris@4
|
472 int ret;
|
Chris@4
|
473 z_stream strm;
|
Chris@4
|
474 unsigned char win[32768];
|
Chris@4
|
475
|
Chris@4
|
476 ret = inflateBackInit_(Z_NULL, 0, win, 0, 0);
|
Chris@4
|
477 assert(ret == Z_VERSION_ERROR);
|
Chris@4
|
478 ret = inflateBackInit(Z_NULL, 0, win); assert(ret == Z_STREAM_ERROR);
|
Chris@4
|
479 ret = inflateBack(Z_NULL, Z_NULL, Z_NULL, Z_NULL, Z_NULL);
|
Chris@4
|
480 assert(ret == Z_STREAM_ERROR);
|
Chris@4
|
481 ret = inflateBackEnd(Z_NULL); assert(ret == Z_STREAM_ERROR);
|
Chris@4
|
482 fputs("inflateBack bad parameters\n", stderr);
|
Chris@4
|
483
|
Chris@4
|
484 mem_setup(&strm);
|
Chris@4
|
485 ret = inflateBackInit(&strm, 15, win); assert(ret == Z_OK);
|
Chris@4
|
486 strm.avail_in = 2;
|
Chris@4
|
487 strm.next_in = (void *)"\x03";
|
Chris@4
|
488 ret = inflateBack(&strm, pull, Z_NULL, push, Z_NULL);
|
Chris@4
|
489 assert(ret == Z_STREAM_END);
|
Chris@4
|
490 /* force output error */
|
Chris@4
|
491 strm.avail_in = 3;
|
Chris@4
|
492 strm.next_in = (void *)"\x63\x00";
|
Chris@4
|
493 ret = inflateBack(&strm, pull, Z_NULL, push, &strm);
|
Chris@4
|
494 assert(ret == Z_BUF_ERROR);
|
Chris@4
|
495 /* force mode error by mucking with state */
|
Chris@4
|
496 ret = inflateBack(&strm, pull, &strm, push, Z_NULL);
|
Chris@4
|
497 assert(ret == Z_STREAM_ERROR);
|
Chris@4
|
498 ret = inflateBackEnd(&strm); assert(ret == Z_OK);
|
Chris@4
|
499 mem_done(&strm, "inflateBack bad state");
|
Chris@4
|
500
|
Chris@4
|
501 ret = inflateBackInit(&strm, 15, win); assert(ret == Z_OK);
|
Chris@4
|
502 ret = inflateBackEnd(&strm); assert(ret == Z_OK);
|
Chris@4
|
503 fputs("inflateBack built-in memory routines\n", stderr);
|
Chris@4
|
504 }
|
Chris@4
|
505
|
Chris@4
|
506 /* do a raw inflate of data in hexadecimal with both inflate and inflateBack */
|
Chris@4
|
507 local int try(char *hex, char *id, int err)
|
Chris@4
|
508 {
|
Chris@4
|
509 int ret;
|
Chris@4
|
510 unsigned len, size;
|
Chris@4
|
511 unsigned char *in, *out, *win;
|
Chris@4
|
512 char *prefix;
|
Chris@4
|
513 z_stream strm;
|
Chris@4
|
514
|
Chris@4
|
515 /* convert to hex */
|
Chris@4
|
516 in = h2b(hex, &len);
|
Chris@4
|
517 assert(in != NULL);
|
Chris@4
|
518
|
Chris@4
|
519 /* allocate work areas */
|
Chris@4
|
520 size = len << 3;
|
Chris@4
|
521 out = malloc(size);
|
Chris@4
|
522 assert(out != NULL);
|
Chris@4
|
523 win = malloc(32768);
|
Chris@4
|
524 assert(win != NULL);
|
Chris@4
|
525 prefix = malloc(strlen(id) + 6);
|
Chris@4
|
526 assert(prefix != NULL);
|
Chris@4
|
527
|
Chris@4
|
528 /* first with inflate */
|
Chris@4
|
529 strcpy(prefix, id);
|
Chris@4
|
530 strcat(prefix, "-late");
|
Chris@4
|
531 mem_setup(&strm);
|
Chris@4
|
532 strm.avail_in = 0;
|
Chris@4
|
533 strm.next_in = Z_NULL;
|
Chris@4
|
534 ret = inflateInit2(&strm, err < 0 ? 47 : -15);
|
Chris@4
|
535 assert(ret == Z_OK);
|
Chris@4
|
536 strm.avail_in = len;
|
Chris@4
|
537 strm.next_in = in;
|
Chris@4
|
538 do {
|
Chris@4
|
539 strm.avail_out = size;
|
Chris@4
|
540 strm.next_out = out;
|
Chris@4
|
541 ret = inflate(&strm, Z_TREES);
|
Chris@4
|
542 assert(ret != Z_STREAM_ERROR && ret != Z_MEM_ERROR);
|
Chris@4
|
543 if (ret == Z_DATA_ERROR || ret == Z_NEED_DICT)
|
Chris@4
|
544 break;
|
Chris@4
|
545 } while (strm.avail_in || strm.avail_out == 0);
|
Chris@4
|
546 if (err) {
|
Chris@4
|
547 assert(ret == Z_DATA_ERROR);
|
Chris@4
|
548 assert(strcmp(id, strm.msg) == 0);
|
Chris@4
|
549 }
|
Chris@4
|
550 inflateEnd(&strm);
|
Chris@4
|
551 mem_done(&strm, prefix);
|
Chris@4
|
552
|
Chris@4
|
553 /* then with inflateBack */
|
Chris@4
|
554 if (err >= 0) {
|
Chris@4
|
555 strcpy(prefix, id);
|
Chris@4
|
556 strcat(prefix, "-back");
|
Chris@4
|
557 mem_setup(&strm);
|
Chris@4
|
558 ret = inflateBackInit(&strm, 15, win);
|
Chris@4
|
559 assert(ret == Z_OK);
|
Chris@4
|
560 strm.avail_in = len;
|
Chris@4
|
561 strm.next_in = in;
|
Chris@4
|
562 ret = inflateBack(&strm, pull, Z_NULL, push, Z_NULL);
|
Chris@4
|
563 assert(ret != Z_STREAM_ERROR);
|
Chris@4
|
564 if (err) {
|
Chris@4
|
565 assert(ret == Z_DATA_ERROR);
|
Chris@4
|
566 assert(strcmp(id, strm.msg) == 0);
|
Chris@4
|
567 }
|
Chris@4
|
568 inflateBackEnd(&strm);
|
Chris@4
|
569 mem_done(&strm, prefix);
|
Chris@4
|
570 }
|
Chris@4
|
571
|
Chris@4
|
572 /* clean up */
|
Chris@4
|
573 free(prefix);
|
Chris@4
|
574 free(win);
|
Chris@4
|
575 free(out);
|
Chris@4
|
576 free(in);
|
Chris@4
|
577 return ret;
|
Chris@4
|
578 }
|
Chris@4
|
579
|
Chris@4
|
580 /* cover deflate data cases in both inflate() and inflateBack() */
|
Chris@4
|
581 local void cover_inflate(void)
|
Chris@4
|
582 {
|
Chris@4
|
583 try("0 0 0 0 0", "invalid stored block lengths", 1);
|
Chris@4
|
584 try("3 0", "fixed", 0);
|
Chris@4
|
585 try("6", "invalid block type", 1);
|
Chris@4
|
586 try("1 1 0 fe ff 0", "stored", 0);
|
Chris@4
|
587 try("fc 0 0", "too many length or distance symbols", 1);
|
Chris@4
|
588 try("4 0 fe ff", "invalid code lengths set", 1);
|
Chris@4
|
589 try("4 0 24 49 0", "invalid bit length repeat", 1);
|
Chris@4
|
590 try("4 0 24 e9 ff ff", "invalid bit length repeat", 1);
|
Chris@4
|
591 try("4 0 24 e9 ff 6d", "invalid code -- missing end-of-block", 1);
|
Chris@4
|
592 try("4 80 49 92 24 49 92 24 71 ff ff 93 11 0",
|
Chris@4
|
593 "invalid literal/lengths set", 1);
|
Chris@4
|
594 try("4 80 49 92 24 49 92 24 f b4 ff ff c3 84", "invalid distances set", 1);
|
Chris@4
|
595 try("4 c0 81 8 0 0 0 0 20 7f eb b 0 0", "invalid literal/length code", 1);
|
Chris@4
|
596 try("2 7e ff ff", "invalid distance code", 1);
|
Chris@4
|
597 try("c c0 81 0 0 0 0 0 90 ff 6b 4 0", "invalid distance too far back", 1);
|
Chris@4
|
598
|
Chris@4
|
599 /* also trailer mismatch just in inflate() */
|
Chris@4
|
600 try("1f 8b 8 0 0 0 0 0 0 0 3 0 0 0 0 1", "incorrect data check", -1);
|
Chris@4
|
601 try("1f 8b 8 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 1",
|
Chris@4
|
602 "incorrect length check", -1);
|
Chris@4
|
603 try("5 c0 21 d 0 0 0 80 b0 fe 6d 2f 91 6c", "pull 17", 0);
|
Chris@4
|
604 try("5 e0 81 91 24 cb b2 2c 49 e2 f 2e 8b 9a 47 56 9f fb fe ec d2 ff 1f",
|
Chris@4
|
605 "long code", 0);
|
Chris@4
|
606 try("ed c0 1 1 0 0 0 40 20 ff 57 1b 42 2c 4f", "length extra", 0);
|
Chris@4
|
607 try("ed cf c1 b1 2c 47 10 c4 30 fa 6f 35 1d 1 82 59 3d fb be 2e 2a fc f c",
|
Chris@4
|
608 "long distance and extra", 0);
|
Chris@4
|
609 try("ed c0 81 0 0 0 0 80 a0 fd a9 17 a9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "
|
Chris@4
|
610 "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6", "window end", 0);
|
Chris@4
|
611 inf("2 8 20 80 0 3 0", "inflate_fast TYPE return", 0, -15, 258,
|
Chris@4
|
612 Z_STREAM_END);
|
Chris@4
|
613 inf("63 18 5 40 c 0", "window wrap", 3, -8, 300, Z_OK);
|
Chris@4
|
614 }
|
Chris@4
|
615
|
Chris@4
|
616 /* cover remaining lines in inftrees.c */
|
Chris@4
|
617 local void cover_trees(void)
|
Chris@4
|
618 {
|
Chris@4
|
619 int ret;
|
Chris@4
|
620 unsigned bits;
|
Chris@4
|
621 unsigned short lens[16], work[16];
|
Chris@4
|
622 code *next, table[ENOUGH_DISTS];
|
Chris@4
|
623
|
Chris@4
|
624 /* we need to call inflate_table() directly in order to manifest not-
|
Chris@4
|
625 enough errors, since zlib insures that enough is always enough */
|
Chris@4
|
626 for (bits = 0; bits < 15; bits++)
|
Chris@4
|
627 lens[bits] = (unsigned short)(bits + 1);
|
Chris@4
|
628 lens[15] = 15;
|
Chris@4
|
629 next = table;
|
Chris@4
|
630 bits = 15;
|
Chris@4
|
631 ret = inflate_table(DISTS, lens, 16, &next, &bits, work);
|
Chris@4
|
632 assert(ret == 1);
|
Chris@4
|
633 next = table;
|
Chris@4
|
634 bits = 1;
|
Chris@4
|
635 ret = inflate_table(DISTS, lens, 16, &next, &bits, work);
|
Chris@4
|
636 assert(ret == 1);
|
Chris@4
|
637 fputs("inflate_table not enough errors\n", stderr);
|
Chris@4
|
638 }
|
Chris@4
|
639
|
Chris@4
|
640 /* cover remaining inffast.c decoding and window copying */
|
Chris@4
|
641 local void cover_fast(void)
|
Chris@4
|
642 {
|
Chris@4
|
643 inf("e5 e0 81 ad 6d cb b2 2c c9 01 1e 59 63 ae 7d ee fb 4d fd b5 35 41 68"
|
Chris@4
|
644 " ff 7f 0f 0 0 0", "fast length extra bits", 0, -8, 258, Z_DATA_ERROR);
|
Chris@4
|
645 inf("25 fd 81 b5 6d 59 b6 6a 49 ea af 35 6 34 eb 8c b9 f6 b9 1e ef 67 49"
|
Chris@4
|
646 " 50 fe ff ff 3f 0 0", "fast distance extra bits", 0, -8, 258,
|
Chris@4
|
647 Z_DATA_ERROR);
|
Chris@4
|
648 inf("3 7e 0 0 0 0 0", "fast invalid distance code", 0, -8, 258,
|
Chris@4
|
649 Z_DATA_ERROR);
|
Chris@4
|
650 inf("1b 7 0 0 0 0 0", "fast invalid literal/length code", 0, -8, 258,
|
Chris@4
|
651 Z_DATA_ERROR);
|
Chris@4
|
652 inf("d c7 1 ae eb 38 c 4 41 a0 87 72 de df fb 1f b8 36 b1 38 5d ff ff 0",
|
Chris@4
|
653 "fast 2nd level codes and too far back", 0, -8, 258, Z_DATA_ERROR);
|
Chris@4
|
654 inf("63 18 5 8c 10 8 0 0 0 0", "very common case", 0, -8, 259, Z_OK);
|
Chris@4
|
655 inf("63 60 60 18 c9 0 8 18 18 18 26 c0 28 0 29 0 0 0",
|
Chris@4
|
656 "contiguous and wrap around window", 6, -8, 259, Z_OK);
|
Chris@4
|
657 inf("63 0 3 0 0 0 0 0", "copy direct from output", 0, -8, 259,
|
Chris@4
|
658 Z_STREAM_END);
|
Chris@4
|
659 }
|
Chris@4
|
660
|
Chris@4
|
661 int main(void)
|
Chris@4
|
662 {
|
Chris@4
|
663 fprintf(stderr, "%s\n", zlibVersion());
|
Chris@4
|
664 cover_support();
|
Chris@4
|
665 cover_wrap();
|
Chris@4
|
666 cover_back();
|
Chris@4
|
667 cover_inflate();
|
Chris@4
|
668 cover_trees();
|
Chris@4
|
669 cover_fast();
|
Chris@4
|
670 return 0;
|
Chris@4
|
671 }
|