annotate src/zlib-1.2.7/test/infcover.c @ 94:d278df1123f9

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