cannam@89: /* infcover.c -- test zlib's inflate routines with full code coverage cannam@89: * Copyright (C) 2011 Mark Adler cannam@89: * For conditions of distribution and use, see copyright notice in zlib.h cannam@89: */ cannam@89: cannam@89: /* to use, do: ./configure --cover && make cover */ cannam@89: cannam@89: #include cannam@89: #include cannam@89: #include cannam@89: #include cannam@89: #include "zlib.h" cannam@89: cannam@89: /* get definition of internal structure so we can mess with it (see pull()), cannam@89: and so we can call inflate_trees() (see cover5()) */ cannam@89: #define ZLIB_INTERNAL cannam@89: #include "inftrees.h" cannam@89: #include "inflate.h" cannam@89: cannam@89: #define local static cannam@89: cannam@89: /* -- memory tracking routines -- */ cannam@89: cannam@89: /* cannam@89: These memory tracking routines are provided to zlib and track all of zlib's cannam@89: allocations and deallocations, check for LIFO operations, keep a current cannam@89: and high water mark of total bytes requested, optionally set a limit on the cannam@89: total memory that can be allocated, and when done check for memory leaks. cannam@89: cannam@89: They are used as follows: cannam@89: cannam@89: z_stream strm; cannam@89: mem_setup(&strm) initializes the memory tracking and sets the cannam@89: zalloc, zfree, and opaque members of strm to use cannam@89: memory tracking for all zlib operations on strm cannam@89: mem_limit(&strm, limit) sets a limit on the total bytes requested -- a cannam@89: request that exceeds this limit will result in an cannam@89: allocation failure (returns NULL) -- setting the cannam@89: limit to zero means no limit, which is the default cannam@89: after mem_setup() cannam@89: mem_used(&strm, "msg") prints to stderr "msg" and the total bytes used cannam@89: mem_high(&strm, "msg") prints to stderr "msg" and the high water mark cannam@89: mem_done(&strm, "msg") ends memory tracking, releases all allocations cannam@89: for the tracking as well as leaked zlib blocks, if cannam@89: any. If there was anything unusual, such as leaked cannam@89: blocks, non-FIFO frees, or frees of addresses not cannam@89: allocated, then "msg" and information about the cannam@89: problem is printed to stderr. If everything is cannam@89: normal, nothing is printed. mem_done resets the cannam@89: strm members to Z_NULL to use the default memory cannam@89: allocation routines on the next zlib initialization cannam@89: using strm. cannam@89: */ cannam@89: cannam@89: /* these items are strung together in a linked list, one for each allocation */ cannam@89: struct mem_item { cannam@89: void *ptr; /* pointer to allocated memory */ cannam@89: size_t size; /* requested size of allocation */ cannam@89: struct mem_item *next; /* pointer to next item in list, or NULL */ cannam@89: }; cannam@89: cannam@89: /* this structure is at the root of the linked list, and tracks statistics */ cannam@89: struct mem_zone { cannam@89: struct mem_item *first; /* pointer to first item in list, or NULL */ cannam@89: size_t total, highwater; /* total allocations, and largest total */ cannam@89: size_t limit; /* memory allocation limit, or 0 if no limit */ cannam@89: int notlifo, rogue; /* counts of non-LIFO frees and rogue frees */ cannam@89: }; cannam@89: cannam@89: /* memory allocation routine to pass to zlib */ cannam@89: local void *mem_alloc(void *mem, unsigned count, unsigned size) cannam@89: { cannam@89: void *ptr; cannam@89: struct mem_item *item; cannam@89: struct mem_zone *zone = mem; cannam@89: size_t len = count * (size_t)size; cannam@89: cannam@89: /* induced allocation failure */ cannam@89: if (zone == NULL || (zone->limit && zone->total + len > zone->limit)) cannam@89: return NULL; cannam@89: cannam@89: /* perform allocation using the standard library, fill memory with a cannam@89: non-zero value to make sure that the code isn't depending on zeros */ cannam@89: ptr = malloc(len); cannam@89: if (ptr == NULL) cannam@89: return NULL; cannam@89: memset(ptr, 0xa5, len); cannam@89: cannam@89: /* create a new item for the list */ cannam@89: item = malloc(sizeof(struct mem_item)); cannam@89: if (item == NULL) { cannam@89: free(ptr); cannam@89: return NULL; cannam@89: } cannam@89: item->ptr = ptr; cannam@89: item->size = len; cannam@89: cannam@89: /* insert item at the beginning of the list */ cannam@89: item->next = zone->first; cannam@89: zone->first = item; cannam@89: cannam@89: /* update the statistics */ cannam@89: zone->total += item->size; cannam@89: if (zone->total > zone->highwater) cannam@89: zone->highwater = zone->total; cannam@89: cannam@89: /* return the allocated memory */ cannam@89: return ptr; cannam@89: } cannam@89: cannam@89: /* memory free routine to pass to zlib */ cannam@89: local void mem_free(void *mem, void *ptr) cannam@89: { cannam@89: struct mem_item *item, *next; cannam@89: struct mem_zone *zone = mem; cannam@89: cannam@89: /* if no zone, just do a free */ cannam@89: if (zone == NULL) { cannam@89: free(ptr); cannam@89: return; cannam@89: } cannam@89: cannam@89: /* point next to the item that matches ptr, or NULL if not found -- remove cannam@89: the item from the linked list if found */ cannam@89: next = zone->first; cannam@89: if (next) { cannam@89: if (next->ptr == ptr) cannam@89: zone->first = next->next; /* first one is it, remove from list */ cannam@89: else { cannam@89: do { /* search the linked list */ cannam@89: item = next; cannam@89: next = item->next; cannam@89: } while (next != NULL && next->ptr != ptr); cannam@89: if (next) { /* if found, remove from linked list */ cannam@89: item->next = next->next; cannam@89: zone->notlifo++; /* not a LIFO free */ cannam@89: } cannam@89: cannam@89: } cannam@89: } cannam@89: cannam@89: /* if found, update the statistics and free the item */ cannam@89: if (next) { cannam@89: zone->total -= next->size; cannam@89: free(next); cannam@89: } cannam@89: cannam@89: /* if not found, update the rogue count */ cannam@89: else cannam@89: zone->rogue++; cannam@89: cannam@89: /* in any case, do the requested free with the standard library function */ cannam@89: free(ptr); cannam@89: } cannam@89: cannam@89: /* set up a controlled memory allocation space for monitoring, set the stream cannam@89: parameters to the controlled routines, with opaque pointing to the space */ cannam@89: local void mem_setup(z_stream *strm) cannam@89: { cannam@89: struct mem_zone *zone; cannam@89: cannam@89: zone = malloc(sizeof(struct mem_zone)); cannam@89: assert(zone != NULL); cannam@89: zone->first = NULL; cannam@89: zone->total = 0; cannam@89: zone->highwater = 0; cannam@89: zone->limit = 0; cannam@89: zone->notlifo = 0; cannam@89: zone->rogue = 0; cannam@89: strm->opaque = zone; cannam@89: strm->zalloc = mem_alloc; cannam@89: strm->zfree = mem_free; cannam@89: } cannam@89: cannam@89: /* set a limit on the total memory allocation, or 0 to remove the limit */ cannam@89: local void mem_limit(z_stream *strm, size_t limit) cannam@89: { cannam@89: struct mem_zone *zone = strm->opaque; cannam@89: cannam@89: zone->limit = limit; cannam@89: } cannam@89: cannam@89: /* show the current total requested allocations in bytes */ cannam@89: local void mem_used(z_stream *strm, char *prefix) cannam@89: { cannam@89: struct mem_zone *zone = strm->opaque; cannam@89: cannam@89: fprintf(stderr, "%s: %lu allocated\n", prefix, zone->total); cannam@89: } cannam@89: cannam@89: /* show the high water allocation in bytes */ cannam@89: local void mem_high(z_stream *strm, char *prefix) cannam@89: { cannam@89: struct mem_zone *zone = strm->opaque; cannam@89: cannam@89: fprintf(stderr, "%s: %lu high water mark\n", prefix, zone->highwater); cannam@89: } cannam@89: cannam@89: /* release the memory allocation zone -- if there are any surprises, notify */ cannam@89: local void mem_done(z_stream *strm, char *prefix) cannam@89: { cannam@89: int count = 0; cannam@89: struct mem_item *item, *next; cannam@89: struct mem_zone *zone = strm->opaque; cannam@89: cannam@89: /* show high water mark */ cannam@89: mem_high(strm, prefix); cannam@89: cannam@89: /* free leftover allocations and item structures, if any */ cannam@89: item = zone->first; cannam@89: while (item != NULL) { cannam@89: free(item->ptr); cannam@89: next = item->next; cannam@89: free(item); cannam@89: item = next; cannam@89: count++; cannam@89: } cannam@89: cannam@89: /* issue alerts about anything unexpected */ cannam@89: if (count || zone->total) cannam@89: fprintf(stderr, "** %s: %lu bytes in %d blocks not freed\n", cannam@89: prefix, zone->total, count); cannam@89: if (zone->notlifo) cannam@89: fprintf(stderr, "** %s: %d frees not LIFO\n", prefix, zone->notlifo); cannam@89: if (zone->rogue) cannam@89: fprintf(stderr, "** %s: %d frees not recognized\n", cannam@89: prefix, zone->rogue); cannam@89: cannam@89: /* free the zone and delete from the stream */ cannam@89: free(zone); cannam@89: strm->opaque = Z_NULL; cannam@89: strm->zalloc = Z_NULL; cannam@89: strm->zfree = Z_NULL; cannam@89: } cannam@89: cannam@89: /* -- inflate test routines -- */ cannam@89: cannam@89: /* Decode a hexadecimal string, set *len to length, in[] to the bytes. This cannam@89: decodes liberally, in that hex digits can be adjacent, in which case two in cannam@89: a row writes a byte. Or they can delimited by any non-hex character, where cannam@89: the delimiters are ignored except when a single hex digit is followed by a cannam@89: delimiter in which case that single digit writes a byte. The returned cannam@89: data is allocated and must eventually be freed. NULL is returned if out of cannam@89: memory. If the length is not needed, then len can be NULL. */ cannam@89: local unsigned char *h2b(const char *hex, unsigned *len) cannam@89: { cannam@89: unsigned char *in; cannam@89: unsigned next, val; cannam@89: cannam@89: in = malloc((strlen(hex) + 1) >> 1); cannam@89: if (in == NULL) cannam@89: return NULL; cannam@89: next = 0; cannam@89: val = 1; cannam@89: do { cannam@89: if (*hex >= '0' && *hex <= '9') cannam@89: val = (val << 4) + *hex - '0'; cannam@89: else if (*hex >= 'A' && *hex <= 'F') cannam@89: val = (val << 4) + *hex - 'A' + 10; cannam@89: else if (*hex >= 'a' && *hex <= 'f') cannam@89: val = (val << 4) + *hex - 'a' + 10; cannam@89: else if (val != 1 && val < 32) /* one digit followed by delimiter */ cannam@89: val += 240; /* make it look like two digits */ cannam@89: if (val > 255) { /* have two digits */ cannam@89: in[next++] = val & 0xff; /* save the decoded byte */ cannam@89: val = 1; /* start over */ cannam@89: } cannam@89: } while (*hex++); /* go through the loop with the terminating null */ cannam@89: if (len != NULL) cannam@89: *len = next; cannam@89: in = reallocf(in, next); cannam@89: return in; cannam@89: } cannam@89: cannam@89: /* generic inflate() run, where hex is the hexadecimal input data, what is the cannam@89: text to include in an error message, step is how much input data to feed cannam@89: inflate() on each call, or zero to feed it all, win is the window bits cannam@89: parameter to inflateInit2(), len is the size of the output buffer, and err cannam@89: is the error code expected from the first inflate() call (the second cannam@89: inflate() call is expected to return Z_STREAM_END). If win is 47, then cannam@89: header information is collected with inflateGetHeader(). If a zlib stream cannam@89: is looking for a dictionary, then an empty dictionary is provided. cannam@89: inflate() is run until all of the input data is consumed. */ cannam@89: local void inf(char *hex, char *what, unsigned step, int win, unsigned len, cannam@89: int err) cannam@89: { cannam@89: int ret; cannam@89: unsigned have; cannam@89: unsigned char *in, *out; cannam@89: z_stream strm, copy; cannam@89: gz_header head; cannam@89: cannam@89: mem_setup(&strm); cannam@89: strm.avail_in = 0; cannam@89: strm.next_in = Z_NULL; cannam@89: ret = inflateInit2(&strm, win); cannam@89: if (ret != Z_OK) { cannam@89: mem_done(&strm, what); cannam@89: return; cannam@89: } cannam@89: out = malloc(len); assert(out != NULL); cannam@89: if (win == 47) { cannam@89: head.extra = out; cannam@89: head.extra_max = len; cannam@89: head.name = out; cannam@89: head.name_max = len; cannam@89: head.comment = out; cannam@89: head.comm_max = len; cannam@89: ret = inflateGetHeader(&strm, &head); assert(ret == Z_OK); cannam@89: } cannam@89: in = h2b(hex, &have); assert(in != NULL); cannam@89: if (step == 0 || step > have) cannam@89: step = have; cannam@89: strm.avail_in = step; cannam@89: have -= step; cannam@89: strm.next_in = in; cannam@89: do { cannam@89: strm.avail_out = len; cannam@89: strm.next_out = out; cannam@89: ret = inflate(&strm, Z_NO_FLUSH); assert(err == 9 || ret == err); cannam@89: if (ret != Z_OK && ret != Z_BUF_ERROR && ret != Z_NEED_DICT) cannam@89: break; cannam@89: if (ret == Z_NEED_DICT) { cannam@89: ret = inflateSetDictionary(&strm, in, 1); cannam@89: assert(ret == Z_DATA_ERROR); cannam@89: mem_limit(&strm, 1); cannam@89: ret = inflateSetDictionary(&strm, out, 0); cannam@89: assert(ret == Z_MEM_ERROR); cannam@89: mem_limit(&strm, 0); cannam@89: ((struct inflate_state *)strm.state)->mode = DICT; cannam@89: ret = inflateSetDictionary(&strm, out, 0); cannam@89: assert(ret == Z_OK); cannam@89: ret = inflate(&strm, Z_NO_FLUSH); assert(ret == Z_BUF_ERROR); cannam@89: } cannam@89: ret = inflateCopy(©, &strm); assert(ret == Z_OK); cannam@89: ret = inflateEnd(©); assert(ret == Z_OK); cannam@89: err = 9; /* don't care next time around */ cannam@89: have += strm.avail_in; cannam@89: strm.avail_in = step > have ? have : step; cannam@89: have -= strm.avail_in; cannam@89: } while (strm.avail_in); cannam@89: free(in); cannam@89: free(out); cannam@89: ret = inflateReset2(&strm, -8); assert(ret == Z_OK); cannam@89: ret = inflateEnd(&strm); assert(ret == Z_OK); cannam@89: mem_done(&strm, what); cannam@89: } cannam@89: cannam@89: /* cover all of the lines in inflate.c up to inflate() */ cannam@89: local void cover_support(void) cannam@89: { cannam@89: int ret; cannam@89: z_stream strm; cannam@89: cannam@89: mem_setup(&strm); cannam@89: strm.avail_in = 0; cannam@89: strm.next_in = Z_NULL; cannam@89: ret = inflateInit(&strm); assert(ret == Z_OK); cannam@89: mem_used(&strm, "inflate init"); cannam@89: ret = inflatePrime(&strm, 5, 31); assert(ret == Z_OK); cannam@89: ret = inflatePrime(&strm, -1, 0); assert(ret == Z_OK); cannam@89: ret = inflateSetDictionary(&strm, Z_NULL, 0); cannam@89: assert(ret == Z_STREAM_ERROR); cannam@89: ret = inflateEnd(&strm); assert(ret == Z_OK); cannam@89: mem_done(&strm, "prime"); cannam@89: cannam@89: inf("63 0", "force window allocation", 0, -15, 1, Z_OK); cannam@89: inf("63 18 5", "force window replacement", 0, -8, 259, Z_OK); cannam@89: inf("63 18 68 30 d0 0 0", "force split window update", 4, -8, 259, Z_OK); cannam@89: inf("3 0", "use fixed blocks", 0, -15, 1, Z_STREAM_END); cannam@89: inf("", "bad window size", 0, 1, 0, Z_STREAM_ERROR); cannam@89: cannam@89: mem_setup(&strm); cannam@89: strm.avail_in = 0; cannam@89: strm.next_in = Z_NULL; cannam@89: ret = inflateInit_(&strm, ZLIB_VERSION - 1, (int)sizeof(z_stream)); cannam@89: assert(ret == Z_VERSION_ERROR); cannam@89: mem_done(&strm, "wrong version"); cannam@89: cannam@89: strm.avail_in = 0; cannam@89: strm.next_in = Z_NULL; cannam@89: ret = inflateInit(&strm); assert(ret == Z_OK); cannam@89: ret = inflateEnd(&strm); assert(ret == Z_OK); cannam@89: fputs("inflate built-in memory routines\n", stderr); cannam@89: } cannam@89: cannam@89: /* cover all inflate() header and trailer cases and code after inflate() */ cannam@89: local void cover_wrap(void) cannam@89: { cannam@89: int ret; cannam@89: z_stream strm, copy; cannam@89: unsigned char dict[257]; cannam@89: cannam@89: ret = inflate(Z_NULL, 0); assert(ret == Z_STREAM_ERROR); cannam@89: ret = inflateEnd(Z_NULL); assert(ret == Z_STREAM_ERROR); cannam@89: ret = inflateCopy(Z_NULL, Z_NULL); assert(ret == Z_STREAM_ERROR); cannam@89: fputs("inflate bad parameters\n", stderr); cannam@89: cannam@89: inf("1f 8b 0 0", "bad gzip method", 0, 31, 0, Z_DATA_ERROR); cannam@89: inf("1f 8b 8 80", "bad gzip flags", 0, 31, 0, Z_DATA_ERROR); cannam@89: inf("77 85", "bad zlib method", 0, 15, 0, Z_DATA_ERROR); cannam@89: inf("8 99", "set window size from header", 0, 0, 0, Z_OK); cannam@89: inf("78 9c", "bad zlib window size", 0, 8, 0, Z_DATA_ERROR); cannam@89: inf("78 9c 63 0 0 0 1 0 1", "check adler32", 0, 15, 1, Z_STREAM_END); cannam@89: 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: Z_DATA_ERROR); cannam@89: 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: 0, 47, 0, Z_STREAM_END); cannam@89: inf("78 90", "bad zlib header check", 0, 47, 0, Z_DATA_ERROR); cannam@89: inf("8 b8 0 0 0 1", "need dictionary", 0, 8, 0, Z_NEED_DICT); cannam@89: inf("78 9c 63 0", "compute adler32", 0, 15, 1, Z_OK); cannam@89: cannam@89: mem_setup(&strm); cannam@89: strm.avail_in = 0; cannam@89: strm.next_in = Z_NULL; cannam@89: ret = inflateInit2(&strm, -8); cannam@89: strm.avail_in = 2; cannam@89: strm.next_in = (void *)"\x63"; cannam@89: strm.avail_out = 1; cannam@89: strm.next_out = (void *)&ret; cannam@89: mem_limit(&strm, 1); cannam@89: ret = inflate(&strm, Z_NO_FLUSH); assert(ret == Z_MEM_ERROR); cannam@89: ret = inflate(&strm, Z_NO_FLUSH); assert(ret == Z_MEM_ERROR); cannam@89: mem_limit(&strm, 0); cannam@89: memset(dict, 0, 257); cannam@89: ret = inflateSetDictionary(&strm, dict, 257); cannam@89: assert(ret == Z_OK); cannam@89: mem_limit(&strm, (sizeof(struct inflate_state) << 1) + 256); cannam@89: ret = inflatePrime(&strm, 16, 0); assert(ret == Z_OK); cannam@89: strm.avail_in = 2; cannam@89: strm.next_in = (void *)"\x80"; cannam@89: ret = inflateSync(&strm); assert(ret == Z_DATA_ERROR); cannam@89: ret = inflate(&strm, Z_NO_FLUSH); assert(ret == Z_STREAM_ERROR); cannam@89: strm.avail_in = 4; cannam@89: strm.next_in = (void *)"\0\0\xff\xff"; cannam@89: ret = inflateSync(&strm); assert(ret == Z_OK); cannam@89: (void)inflateSyncPoint(&strm); cannam@89: ret = inflateCopy(©, &strm); assert(ret == Z_MEM_ERROR); cannam@89: mem_limit(&strm, 0); cannam@89: ret = inflateUndermine(&strm, 1); assert(ret == Z_DATA_ERROR); cannam@89: (void)inflateMark(&strm); cannam@89: ret = inflateEnd(&strm); assert(ret == Z_OK); cannam@89: mem_done(&strm, "miscellaneous, force memory errors"); cannam@89: } cannam@89: cannam@89: /* input and output functions for inflateBack() */ cannam@89: local unsigned pull(void *desc, unsigned char **buf) cannam@89: { cannam@89: static unsigned int next = 0; cannam@89: static unsigned char dat[] = {0x63, 0, 2, 0}; cannam@89: struct inflate_state *state; cannam@89: cannam@89: if (desc == Z_NULL) { cannam@89: next = 0; cannam@89: return 0; /* no input (already provided at next_in) */ cannam@89: } cannam@89: state = (void *)((z_stream *)desc)->state; cannam@89: if (state != Z_NULL) cannam@89: state->mode = SYNC; /* force an otherwise impossible situation */ cannam@89: return next < sizeof(dat) ? (*buf = dat + next++, 1) : 0; cannam@89: } cannam@89: cannam@89: local int push(void *desc, unsigned char *buf, unsigned len) cannam@89: { cannam@89: buf += len; cannam@89: return desc != Z_NULL; /* force error if desc not null */ cannam@89: } cannam@89: cannam@89: /* cover inflateBack() up to common deflate data cases and after those */ cannam@89: local void cover_back(void) cannam@89: { cannam@89: int ret; cannam@89: z_stream strm; cannam@89: unsigned char win[32768]; cannam@89: cannam@89: ret = inflateBackInit_(Z_NULL, 0, win, 0, 0); cannam@89: assert(ret == Z_VERSION_ERROR); cannam@89: ret = inflateBackInit(Z_NULL, 0, win); assert(ret == Z_STREAM_ERROR); cannam@89: ret = inflateBack(Z_NULL, Z_NULL, Z_NULL, Z_NULL, Z_NULL); cannam@89: assert(ret == Z_STREAM_ERROR); cannam@89: ret = inflateBackEnd(Z_NULL); assert(ret == Z_STREAM_ERROR); cannam@89: fputs("inflateBack bad parameters\n", stderr); cannam@89: cannam@89: mem_setup(&strm); cannam@89: ret = inflateBackInit(&strm, 15, win); assert(ret == Z_OK); cannam@89: strm.avail_in = 2; cannam@89: strm.next_in = (void *)"\x03"; cannam@89: ret = inflateBack(&strm, pull, Z_NULL, push, Z_NULL); cannam@89: assert(ret == Z_STREAM_END); cannam@89: /* force output error */ cannam@89: strm.avail_in = 3; cannam@89: strm.next_in = (void *)"\x63\x00"; cannam@89: ret = inflateBack(&strm, pull, Z_NULL, push, &strm); cannam@89: assert(ret == Z_BUF_ERROR); cannam@89: /* force mode error by mucking with state */ cannam@89: ret = inflateBack(&strm, pull, &strm, push, Z_NULL); cannam@89: assert(ret == Z_STREAM_ERROR); cannam@89: ret = inflateBackEnd(&strm); assert(ret == Z_OK); cannam@89: mem_done(&strm, "inflateBack bad state"); cannam@89: cannam@89: ret = inflateBackInit(&strm, 15, win); assert(ret == Z_OK); cannam@89: ret = inflateBackEnd(&strm); assert(ret == Z_OK); cannam@89: fputs("inflateBack built-in memory routines\n", stderr); cannam@89: } cannam@89: cannam@89: /* do a raw inflate of data in hexadecimal with both inflate and inflateBack */ cannam@89: local int try(char *hex, char *id, int err) cannam@89: { cannam@89: int ret; cannam@89: unsigned len, size; cannam@89: unsigned char *in, *out, *win; cannam@89: char *prefix; cannam@89: z_stream strm; cannam@89: cannam@89: /* convert to hex */ cannam@89: in = h2b(hex, &len); cannam@89: assert(in != NULL); cannam@89: cannam@89: /* allocate work areas */ cannam@89: size = len << 3; cannam@89: out = malloc(size); cannam@89: assert(out != NULL); cannam@89: win = malloc(32768); cannam@89: assert(win != NULL); cannam@89: prefix = malloc(strlen(id) + 6); cannam@89: assert(prefix != NULL); cannam@89: cannam@89: /* first with inflate */ cannam@89: strcpy(prefix, id); cannam@89: strcat(prefix, "-late"); cannam@89: mem_setup(&strm); cannam@89: strm.avail_in = 0; cannam@89: strm.next_in = Z_NULL; cannam@89: ret = inflateInit2(&strm, err < 0 ? 47 : -15); cannam@89: assert(ret == Z_OK); cannam@89: strm.avail_in = len; cannam@89: strm.next_in = in; cannam@89: do { cannam@89: strm.avail_out = size; cannam@89: strm.next_out = out; cannam@89: ret = inflate(&strm, Z_TREES); cannam@89: assert(ret != Z_STREAM_ERROR && ret != Z_MEM_ERROR); cannam@89: if (ret == Z_DATA_ERROR || ret == Z_NEED_DICT) cannam@89: break; cannam@89: } while (strm.avail_in || strm.avail_out == 0); cannam@89: if (err) { cannam@89: assert(ret == Z_DATA_ERROR); cannam@89: assert(strcmp(id, strm.msg) == 0); cannam@89: } cannam@89: inflateEnd(&strm); cannam@89: mem_done(&strm, prefix); cannam@89: cannam@89: /* then with inflateBack */ cannam@89: if (err >= 0) { cannam@89: strcpy(prefix, id); cannam@89: strcat(prefix, "-back"); cannam@89: mem_setup(&strm); cannam@89: ret = inflateBackInit(&strm, 15, win); cannam@89: assert(ret == Z_OK); cannam@89: strm.avail_in = len; cannam@89: strm.next_in = in; cannam@89: ret = inflateBack(&strm, pull, Z_NULL, push, Z_NULL); cannam@89: assert(ret != Z_STREAM_ERROR); cannam@89: if (err) { cannam@89: assert(ret == Z_DATA_ERROR); cannam@89: assert(strcmp(id, strm.msg) == 0); cannam@89: } cannam@89: inflateBackEnd(&strm); cannam@89: mem_done(&strm, prefix); cannam@89: } cannam@89: cannam@89: /* clean up */ cannam@89: free(prefix); cannam@89: free(win); cannam@89: free(out); cannam@89: free(in); cannam@89: return ret; cannam@89: } cannam@89: cannam@89: /* cover deflate data cases in both inflate() and inflateBack() */ cannam@89: local void cover_inflate(void) cannam@89: { cannam@89: try("0 0 0 0 0", "invalid stored block lengths", 1); cannam@89: try("3 0", "fixed", 0); cannam@89: try("6", "invalid block type", 1); cannam@89: try("1 1 0 fe ff 0", "stored", 0); cannam@89: try("fc 0 0", "too many length or distance symbols", 1); cannam@89: try("4 0 fe ff", "invalid code lengths set", 1); cannam@89: try("4 0 24 49 0", "invalid bit length repeat", 1); cannam@89: try("4 0 24 e9 ff ff", "invalid bit length repeat", 1); cannam@89: try("4 0 24 e9 ff 6d", "invalid code -- missing end-of-block", 1); cannam@89: try("4 80 49 92 24 49 92 24 71 ff ff 93 11 0", cannam@89: "invalid literal/lengths set", 1); cannam@89: try("4 80 49 92 24 49 92 24 f b4 ff ff c3 84", "invalid distances set", 1); cannam@89: try("4 c0 81 8 0 0 0 0 20 7f eb b 0 0", "invalid literal/length code", 1); cannam@89: try("2 7e ff ff", "invalid distance code", 1); cannam@89: try("c c0 81 0 0 0 0 0 90 ff 6b 4 0", "invalid distance too far back", 1); cannam@89: cannam@89: /* also trailer mismatch just in inflate() */ cannam@89: try("1f 8b 8 0 0 0 0 0 0 0 3 0 0 0 0 1", "incorrect data check", -1); cannam@89: try("1f 8b 8 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 1", cannam@89: "incorrect length check", -1); cannam@89: try("5 c0 21 d 0 0 0 80 b0 fe 6d 2f 91 6c", "pull 17", 0); cannam@89: 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: "long code", 0); cannam@89: try("ed c0 1 1 0 0 0 40 20 ff 57 1b 42 2c 4f", "length extra", 0); cannam@89: 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: "long distance and extra", 0); cannam@89: 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: "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6", "window end", 0); cannam@89: inf("2 8 20 80 0 3 0", "inflate_fast TYPE return", 0, -15, 258, cannam@89: Z_STREAM_END); cannam@89: inf("63 18 5 40 c 0", "window wrap", 3, -8, 300, Z_OK); cannam@89: } cannam@89: cannam@89: /* cover remaining lines in inftrees.c */ cannam@89: local void cover_trees(void) cannam@89: { cannam@89: int ret; cannam@89: unsigned bits; cannam@89: unsigned short lens[16], work[16]; cannam@89: code *next, table[ENOUGH_DISTS]; cannam@89: cannam@89: /* we need to call inflate_table() directly in order to manifest not- cannam@89: enough errors, since zlib insures that enough is always enough */ cannam@89: for (bits = 0; bits < 15; bits++) cannam@89: lens[bits] = (unsigned short)(bits + 1); cannam@89: lens[15] = 15; cannam@89: next = table; cannam@89: bits = 15; cannam@89: ret = inflate_table(DISTS, lens, 16, &next, &bits, work); cannam@89: assert(ret == 1); cannam@89: next = table; cannam@89: bits = 1; cannam@89: ret = inflate_table(DISTS, lens, 16, &next, &bits, work); cannam@89: assert(ret == 1); cannam@89: fputs("inflate_table not enough errors\n", stderr); cannam@89: } cannam@89: cannam@89: /* cover remaining inffast.c decoding and window copying */ cannam@89: local void cover_fast(void) cannam@89: { cannam@89: 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: " ff 7f 0f 0 0 0", "fast length extra bits", 0, -8, 258, Z_DATA_ERROR); cannam@89: 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: " 50 fe ff ff 3f 0 0", "fast distance extra bits", 0, -8, 258, cannam@89: Z_DATA_ERROR); cannam@89: inf("3 7e 0 0 0 0 0", "fast invalid distance code", 0, -8, 258, cannam@89: Z_DATA_ERROR); cannam@89: inf("1b 7 0 0 0 0 0", "fast invalid literal/length code", 0, -8, 258, cannam@89: Z_DATA_ERROR); cannam@89: 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: "fast 2nd level codes and too far back", 0, -8, 258, Z_DATA_ERROR); cannam@89: inf("63 18 5 8c 10 8 0 0 0 0", "very common case", 0, -8, 259, Z_OK); cannam@89: inf("63 60 60 18 c9 0 8 18 18 18 26 c0 28 0 29 0 0 0", cannam@89: "contiguous and wrap around window", 6, -8, 259, Z_OK); cannam@89: inf("63 0 3 0 0 0 0 0", "copy direct from output", 0, -8, 259, cannam@89: Z_STREAM_END); cannam@89: } cannam@89: cannam@89: int main(void) cannam@89: { cannam@89: fprintf(stderr, "%s\n", zlibVersion()); cannam@89: cover_support(); cannam@89: cover_wrap(); cannam@89: cover_back(); cannam@89: cover_inflate(); cannam@89: cover_trees(); cannam@89: cover_fast(); cannam@89: return 0; cannam@89: }