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