/* infcover.c -- test zlib's inflate routines with full code coverage
* Copyright (C) 2011 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/* to use, do: ./configure --cover && make cover */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "zlib.h"
/* get definition of internal structure so we can mess with it (see pull()),
and so we can call inflate_trees() (see cover5()) */
#define ZLIB_INTERNAL
#include "inftrees.h"
#include "inflate.h"
#define local static
/* -- memory tracking routines -- */
/*
These memory tracking routines are provided to zlib and track all of zlib's
allocations and deallocations, check for LIFO operations, keep a current
and high water mark of total bytes requested, optionally set a limit on the
total memory that can be allocated, and when done check for memory leaks.
They are used as follows:
z_stream strm;
mem_setup(&strm) initializes the memory tracking and sets the
zalloc, zfree, and opaque members of strm to use
memory tracking for all zlib operations on strm
mem_limit(&strm, limit) sets a limit on the total bytes requested -- a
request that exceeds this limit will result in an
allocation failure (returns NULL) -- setting the
limit to zero means no limit, which is the default
after mem_setup()
mem_used(&strm, "msg") prints to stderr "msg" and the total bytes used
mem_high(&strm, "msg") prints to stderr "msg" and the high water mark
mem_done(&strm, "msg") ends memory tracking, releases all allocations
for the tracking as well as leaked zlib blocks, if
any. If there was anything unusual, such as leaked
blocks, non-FIFO frees, or frees of addresses not
allocated, then "msg" and information about the
problem is printed to stderr. If everything is
normal, nothing is printed. mem_done resets the
strm members to Z_NULL to use the default memory
allocation routines on the next zlib initialization
using strm.
*/
/* these items are strung together in a linked list, one for each allocation */
struct mem_item {
};
/* this structure is at the root of the linked list, and tracks statistics */
struct mem_zone {
};
/* memory allocation routine to pass to zlib */
{
void *ptr;
/* induced allocation failure */
return NULL;
/* perform allocation using the standard library, fill memory with a
non-zero value to make sure that the code isn't depending on zeros */
return NULL;
/* create a new item for the list */
return NULL;
}
/* insert item at the beginning of the list */
/* update the statistics */
/* return the allocated memory */
return ptr;
}
/* memory free routine to pass to zlib */
{
/* if no zone, just do a free */
return;
}
/* point next to the item that matches ptr, or NULL if not found -- remove
the item from the linked list if found */
if (next) {
else {
do { /* search the linked list */
if (next) { /* if found, remove from linked list */
}
}
}
/* if found, update the statistics and free the item */
if (next) {
}
/* if not found, update the rogue count */
else
/* in any case, do the requested free with the standard library function */
}
/* set up a controlled memory allocation space for monitoring, set the stream
parameters to the controlled routines, with opaque pointing to the space */
{
}
/* set a limit on the total memory allocation, or 0 to remove the limit */
{
}
/* show the current total requested allocations in bytes */
{
}
/* show the high water allocation in bytes */
{
}
/* release the memory allocation zone -- if there are any surprises, notify */
{
int count = 0;
/* show high water mark */
/* free leftover allocations and item structures, if any */
count++;
}
/* issue alerts about anything unexpected */
/* free the zone and delete from the stream */
}
/* -- inflate test routines -- */
/* Decode a hexadecimal string, set *len to length, in[] to the bytes. This
decodes liberally, in that hex digits can be adjacent, in which case two in
a row writes a byte. Or they can delimited by any non-hex character, where
the delimiters are ignored except when a single hex digit is followed by a
delimiter in which case that single digit writes a byte. The returned
data is allocated and must eventually be freed. NULL is returned if out of
memory. If the length is not needed, then len can be NULL. */
{
unsigned char *in;
return NULL;
next = 0;
val = 1;
do {
}
} while (*hex++); /* go through the loop with the terminating null */
return in;
}
/* generic inflate() run, where hex is the hexadecimal input data, what is the
text to include in an error message, step is how much input data to feed
inflate() on each call, or zero to feed it all, win is the window bits
parameter to inflateInit2(), len is the size of the output buffer, and err
is the error code expected from the first inflate() call (the second
inflate() call is expected to return Z_STREAM_END). If win is 47, then
header information is collected with inflateGetHeader(). If a zlib stream
is looking for a dictionary, then an empty dictionary is provided.
inflate() is run until all of the input data is consumed. */
int err)
{
int ret;
unsigned have;
return;
}
if (win == 47) {
}
do {
break;
if (ret == Z_NEED_DICT) {
}
}
/* cover all of the lines in inflate.c up to inflate() */
{
int ret;
}
/* cover all inflate() header and trailer cases and code after inflate() */
{
int ret;
0, 47, 0, Z_STREAM_END);
(void)inflateSyncPoint(&strm);
(void)inflateMark(&strm);
}
/* input and output functions for inflateBack() */
{
static unsigned int next = 0;
next = 0;
return 0; /* no input (already provided at next_in) */
}
}
{
}
/* cover inflateBack() up to common deflate data cases and after those */
{
int ret;
/* force output error */
/* force mode error by mucking with state */
}
/* do a raw inflate of data in hexadecimal with both inflate and inflateBack */
{
int ret;
char *prefix;
/* convert to hex */
/* allocate work areas */
/* first with inflate */
do {
break;
if (err) {
}
inflateEnd(&strm);
/* then with inflateBack */
if (err >= 0) {
if (err) {
}
}
/* clean up */
return ret;
}
/* cover deflate data cases in both inflate() and inflateBack() */
{
try("4 80 49 92 24 49 92 24 71 ff ff 93 11 0",
/* also trailer mismatch just in inflate() */
try("1f 8b 8 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 1",
"incorrect length check", -1);
try("5 e0 81 91 24 cb b2 2c 49 e2 f 2e 8b 9a 47 56 9f fb fe ec d2 ff 1f",
"long code", 0);
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",
"long distance and extra", 0);
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 "
"0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6", "window end", 0);
}
/* cover remaining lines in inftrees.c */
{
int ret;
unsigned bits;
/* we need to call inflate_table() directly in order to manifest not-
enough errors, since zlib insures that enough is always enough */
bits = 15;
bits = 1;
}
/* cover remaining inffast.c decoding and window copying */
{
inf("e5 e0 81 ad 6d cb b2 2c c9 01 1e 59 63 ae 7d ee fb 4d fd b5 35 41 68"
inf("25 fd 81 b5 6d 59 b6 6a 49 ea af 35 6 34 eb 8c b9 f6 b9 1e ef 67 49"
" 50 fe ff ff 3f 0 0", "fast distance extra bits", 0, -8, 258,
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",
inf("63 60 60 18 c9 0 8 18 18 18 26 c0 28 0 29 0 0 0",
}
int main(void)
{
cover_wrap();
cover_back();
cover_trees();
cover_fast();
return 0;
}