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