annotate src/zlib-1.2.8/test/infcover.c @ 56:af97cad61ff0

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