annotate src/zlib-1.2.8/test/infcover.c @ 169:223a55898ab9 tip default

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