123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <assert.h>
- #include "zlib.h"
- #define ZLIB_INTERNAL
- #include "inftrees.h"
- #include "inflate.h"
- #define local static
- struct mem_item {
- void *ptr;
- size_t size;
- struct mem_item *next;
- };
- struct mem_zone {
- struct mem_item *first;
- size_t total, highwater;
- size_t limit;
- int notlifo, rogue;
- };
- local void *mem_alloc(void *mem, unsigned count, unsigned size)
- {
- void *ptr;
- struct mem_item *item;
- struct mem_zone *zone = mem;
- size_t len = count * (size_t)size;
-
- if (zone == NULL || (zone->limit && zone->total + len > zone->limit))
- return NULL;
-
- ptr = malloc(len);
- if (ptr == NULL)
- return NULL;
- memset(ptr, 0xa5, len);
-
- item = malloc(sizeof(struct mem_item));
- if (item == NULL) {
- free(ptr);
- return NULL;
- }
- item->ptr = ptr;
- item->size = len;
-
- item->next = zone->first;
- zone->first = item;
-
- zone->total += item->size;
- if (zone->total > zone->highwater)
- zone->highwater = zone->total;
-
- return ptr;
- }
- local void mem_free(void *mem, void *ptr)
- {
- struct mem_item *item, *next;
- struct mem_zone *zone = mem;
-
- if (zone == NULL) {
- free(ptr);
- return;
- }
-
- next = zone->first;
- if (next) {
- if (next->ptr == ptr)
- zone->first = next->next;
- else {
- do {
- item = next;
- next = item->next;
- } while (next != NULL && next->ptr != ptr);
- if (next) {
- item->next = next->next;
- zone->notlifo++;
- }
- }
- }
-
- if (next) {
- zone->total -= next->size;
- free(next);
- }
-
- else
- zone->rogue++;
-
- free(ptr);
- }
- local void mem_setup(z_stream *strm)
- {
- struct mem_zone *zone;
- zone = malloc(sizeof(struct mem_zone));
- assert(zone != NULL);
- zone->first = NULL;
- zone->total = 0;
- zone->highwater = 0;
- zone->limit = 0;
- zone->notlifo = 0;
- zone->rogue = 0;
- strm->opaque = zone;
- strm->zalloc = mem_alloc;
- strm->zfree = mem_free;
- }
- local void mem_limit(z_stream *strm, size_t limit)
- {
- struct mem_zone *zone = strm->opaque;
- zone->limit = limit;
- }
- local void mem_used(z_stream *strm, char *prefix)
- {
- struct mem_zone *zone = strm->opaque;
- fprintf(stderr, "%s: %lu allocated\n", prefix, zone->total);
- }
- local void mem_high(z_stream *strm, char *prefix)
- {
- struct mem_zone *zone = strm->opaque;
- fprintf(stderr, "%s: %lu high water mark\n", prefix, zone->highwater);
- }
- local void mem_done(z_stream *strm, char *prefix)
- {
- int count = 0;
- struct mem_item *item, *next;
- struct mem_zone *zone = strm->opaque;
-
- mem_high(strm, prefix);
-
- item = zone->first;
- while (item != NULL) {
- free(item->ptr);
- next = item->next;
- free(item);
- item = next;
- count++;
- }
-
- if (count || zone->total)
- fprintf(stderr, "** %s: %lu bytes in %d blocks not freed\n",
- prefix, zone->total, count);
- if (zone->notlifo)
- fprintf(stderr, "** %s: %d frees not LIFO\n", prefix, zone->notlifo);
- if (zone->rogue)
- fprintf(stderr, "** %s: %d frees not recognized\n",
- prefix, zone->rogue);
-
- free(zone);
- strm->opaque = Z_NULL;
- strm->zalloc = Z_NULL;
- strm->zfree = Z_NULL;
- }
- local unsigned char *h2b(const char *hex, unsigned *len)
- {
- unsigned char *in, *re;
- unsigned next, val;
- in = malloc((strlen(hex) + 1) >> 1);
- if (in == NULL)
- return NULL;
- next = 0;
- val = 1;
- do {
- if (*hex >= '0' && *hex <= '9')
- val = (val << 4) + *hex - '0';
- else if (*hex >= 'A' && *hex <= 'F')
- val = (val << 4) + *hex - 'A' + 10;
- else if (*hex >= 'a' && *hex <= 'f')
- val = (val << 4) + *hex - 'a' + 10;
- else if (val != 1 && val < 32)
- val += 240;
- if (val > 255) {
- in[next++] = val & 0xff;
- val = 1;
- }
- } while (*hex++);
- if (len != NULL)
- *len = next;
- re = realloc(in, next);
- return re == NULL ? in : re;
- }
- local void inf(char *hex, char *what, unsigned step, int win, unsigned len,
- int err)
- {
- int ret;
- unsigned have;
- unsigned char *in, *out;
- z_stream strm, copy;
- gz_header head;
- mem_setup(&strm);
- strm.avail_in = 0;
- strm.next_in = Z_NULL;
- ret = inflateInit2(&strm, win);
- if (ret != Z_OK) {
- mem_done(&strm, what);
- return;
- }
- out = malloc(len); assert(out != NULL);
- if (win == 47) {
- head.extra = out;
- head.extra_max = len;
- head.name = out;
- head.name_max = len;
- head.comment = out;
- head.comm_max = len;
- ret = inflateGetHeader(&strm, &head); assert(ret == Z_OK);
- }
- in = h2b(hex, &have); assert(in != NULL);
- if (step == 0 || step > have)
- step = have;
- strm.avail_in = step;
- have -= step;
- strm.next_in = in;
- do {
- strm.avail_out = len;
- strm.next_out = out;
- ret = inflate(&strm, Z_NO_FLUSH); assert(err == 9 || ret == err);
- if (ret != Z_OK && ret != Z_BUF_ERROR && ret != Z_NEED_DICT)
- break;
- if (ret == Z_NEED_DICT) {
- ret = inflateSetDictionary(&strm, in, 1);
- assert(ret == Z_DATA_ERROR);
- mem_limit(&strm, 1);
- ret = inflateSetDictionary(&strm, out, 0);
- assert(ret == Z_MEM_ERROR);
- mem_limit(&strm, 0);
- ((struct inflate_state *)strm.state)->mode = DICT;
- ret = inflateSetDictionary(&strm, out, 0);
- assert(ret == Z_OK);
- ret = inflate(&strm, Z_NO_FLUSH); assert(ret == Z_BUF_ERROR);
- }
- ret = inflateCopy(©, &strm); assert(ret == Z_OK);
- ret = inflateEnd(©); assert(ret == Z_OK);
- err = 9;
- have += strm.avail_in;
- strm.avail_in = step > have ? have : step;
- have -= strm.avail_in;
- } while (strm.avail_in);
- free(in);
- free(out);
- ret = inflateReset2(&strm, -8); assert(ret == Z_OK);
- ret = inflateEnd(&strm); assert(ret == Z_OK);
- mem_done(&strm, what);
- }
- local void cover_support(void)
- {
- int ret;
- z_stream strm;
- mem_setup(&strm);
- strm.avail_in = 0;
- strm.next_in = Z_NULL;
- ret = inflateInit(&strm); assert(ret == Z_OK);
- mem_used(&strm, "inflate init");
- ret = inflatePrime(&strm, 5, 31); assert(ret == Z_OK);
- ret = inflatePrime(&strm, -1, 0); assert(ret == Z_OK);
- ret = inflateSetDictionary(&strm, Z_NULL, 0);
- assert(ret == Z_STREAM_ERROR);
- ret = inflateEnd(&strm); assert(ret == Z_OK);
- mem_done(&strm, "prime");
- inf("63 0", "force window allocation", 0, -15, 1, Z_OK);
- inf("63 18 5", "force window replacement", 0, -8, 259, Z_OK);
- inf("63 18 68 30 d0 0 0", "force split window update", 4, -8, 259, Z_OK);
- inf("3 0", "use fixed blocks", 0, -15, 1, Z_STREAM_END);
- inf("", "bad window size", 0, 1, 0, Z_STREAM_ERROR);
- mem_setup(&strm);
- strm.avail_in = 0;
- strm.next_in = Z_NULL;
- ret = inflateInit_(&strm, ZLIB_VERSION - 1, (int)sizeof(z_stream));
- assert(ret == Z_VERSION_ERROR);
- mem_done(&strm, "wrong version");
- strm.avail_in = 0;
- strm.next_in = Z_NULL;
- ret = inflateInit(&strm); assert(ret == Z_OK);
- ret = inflateEnd(&strm); assert(ret == Z_OK);
- fputs("inflate built-in memory routines\n", stderr);
- }
- local void cover_wrap(void)
- {
- int ret;
- z_stream strm, copy;
- unsigned char dict[257];
- ret = inflate(Z_NULL, 0); assert(ret == Z_STREAM_ERROR);
- ret = inflateEnd(Z_NULL); assert(ret == Z_STREAM_ERROR);
- ret = inflateCopy(Z_NULL, Z_NULL); assert(ret == Z_STREAM_ERROR);
- fputs("inflate bad parameters\n", stderr);
- inf("1f 8b 0 0", "bad gzip method", 0, 31, 0, Z_DATA_ERROR);
- inf("1f 8b 8 80", "bad gzip flags", 0, 31, 0, Z_DATA_ERROR);
- inf("77 85", "bad zlib method", 0, 15, 0, Z_DATA_ERROR);
- inf("8 99", "set window size from header", 0, 0, 0, Z_OK);
- inf("78 9c", "bad zlib window size", 0, 8, 0, Z_DATA_ERROR);
- inf("78 9c 63 0 0 0 1 0 1", "check adler32", 0, 15, 1, Z_STREAM_END);
- inf("1f 8b 8 1e 0 0 0 0 0 0 1 0 0 0 0 0 0", "bad header crc", 0, 47, 1,
- Z_DATA_ERROR);
- 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",
- 0, 47, 0, Z_STREAM_END);
- inf("78 90", "bad zlib header check", 0, 47, 0, Z_DATA_ERROR);
- inf("8 b8 0 0 0 1", "need dictionary", 0, 8, 0, Z_NEED_DICT);
- inf("78 9c 63 0", "compute adler32", 0, 15, 1, Z_OK);
- mem_setup(&strm);
- strm.avail_in = 0;
- strm.next_in = Z_NULL;
- ret = inflateInit2(&strm, -8);
- strm.avail_in = 2;
- strm.next_in = (void *)"\x63";
- strm.avail_out = 1;
- strm.next_out = (void *)&ret;
- mem_limit(&strm, 1);
- ret = inflate(&strm, Z_NO_FLUSH); assert(ret == Z_MEM_ERROR);
- ret = inflate(&strm, Z_NO_FLUSH); assert(ret == Z_MEM_ERROR);
- mem_limit(&strm, 0);
- memset(dict, 0, 257);
- ret = inflateSetDictionary(&strm, dict, 257);
- assert(ret == Z_OK);
- mem_limit(&strm, (sizeof(struct inflate_state) << 1) + 256);
- ret = inflatePrime(&strm, 16, 0); assert(ret == Z_OK);
- strm.avail_in = 2;
- strm.next_in = (void *)"\x80";
- ret = inflateSync(&strm); assert(ret == Z_DATA_ERROR);
- ret = inflate(&strm, Z_NO_FLUSH); assert(ret == Z_STREAM_ERROR);
- strm.avail_in = 4;
- strm.next_in = (void *)"\0\0\xff\xff";
- ret = inflateSync(&strm); assert(ret == Z_OK);
- (void)inflateSyncPoint(&strm);
- ret = inflateCopy(©, &strm); assert(ret == Z_MEM_ERROR);
- mem_limit(&strm, 0);
- ret = inflateUndermine(&strm, 1); assert(ret == Z_DATA_ERROR);
- (void)inflateMark(&strm);
- ret = inflateEnd(&strm); assert(ret == Z_OK);
- mem_done(&strm, "miscellaneous, force memory errors");
- }
- local unsigned pull(void *desc, unsigned char **buf)
- {
- static unsigned int next = 0;
- static unsigned char dat[] = {0x63, 0, 2, 0};
- struct inflate_state *state;
- if (desc == Z_NULL) {
- next = 0;
- return 0;
- }
- state = (void *)((z_stream *)desc)->state;
- if (state != Z_NULL)
- state->mode = SYNC;
- return next < sizeof(dat) ? (*buf = dat + next++, 1) : 0;
- }
- local int push(void *desc, unsigned char *buf, unsigned len)
- {
- buf += len;
- return desc != Z_NULL;
- }
- local void cover_back(void)
- {
- int ret;
- z_stream strm;
- unsigned char win[32768];
- ret = inflateBackInit_(Z_NULL, 0, win, 0, 0);
- assert(ret == Z_VERSION_ERROR);
- ret = inflateBackInit(Z_NULL, 0, win); assert(ret == Z_STREAM_ERROR);
- ret = inflateBack(Z_NULL, Z_NULL, Z_NULL, Z_NULL, Z_NULL);
- assert(ret == Z_STREAM_ERROR);
- ret = inflateBackEnd(Z_NULL); assert(ret == Z_STREAM_ERROR);
- fputs("inflateBack bad parameters\n", stderr);
- mem_setup(&strm);
- ret = inflateBackInit(&strm, 15, win); assert(ret == Z_OK);
- strm.avail_in = 2;
- strm.next_in = (void *)"\x03";
- ret = inflateBack(&strm, pull, Z_NULL, push, Z_NULL);
- assert(ret == Z_STREAM_END);
-
- strm.avail_in = 3;
- strm.next_in = (void *)"\x63\x00";
- ret = inflateBack(&strm, pull, Z_NULL, push, &strm);
- assert(ret == Z_BUF_ERROR);
-
- ret = inflateBack(&strm, pull, &strm, push, Z_NULL);
- assert(ret == Z_STREAM_ERROR);
- ret = inflateBackEnd(&strm); assert(ret == Z_OK);
- mem_done(&strm, "inflateBack bad state");
- ret = inflateBackInit(&strm, 15, win); assert(ret == Z_OK);
- ret = inflateBackEnd(&strm); assert(ret == Z_OK);
- fputs("inflateBack built-in memory routines\n", stderr);
- }
- local int try(char *hex, char *id, int err)
- {
- int ret;
- unsigned len, size;
- unsigned char *in, *out, *win;
- char *prefix;
- z_stream strm;
-
- in = h2b(hex, &len);
- assert(in != NULL);
-
- size = len << 3;
- out = malloc(size);
- assert(out != NULL);
- win = malloc(32768);
- assert(win != NULL);
- prefix = malloc(strlen(id) + 6);
- assert(prefix != NULL);
-
- strcpy(prefix, id);
- strcat(prefix, "-late");
- mem_setup(&strm);
- strm.avail_in = 0;
- strm.next_in = Z_NULL;
- ret = inflateInit2(&strm, err < 0 ? 47 : -15);
- assert(ret == Z_OK);
- strm.avail_in = len;
- strm.next_in = in;
- do {
- strm.avail_out = size;
- strm.next_out = out;
- ret = inflate(&strm, Z_TREES);
- assert(ret != Z_STREAM_ERROR && ret != Z_MEM_ERROR);
- if (ret == Z_DATA_ERROR || ret == Z_NEED_DICT)
- break;
- } while (strm.avail_in || strm.avail_out == 0);
- if (err) {
- assert(ret == Z_DATA_ERROR);
- assert(strcmp(id, strm.msg) == 0);
- }
- inflateEnd(&strm);
- mem_done(&strm, prefix);
-
- if (err >= 0) {
- strcpy(prefix, id);
- strcat(prefix, "-back");
- mem_setup(&strm);
- ret = inflateBackInit(&strm, 15, win);
- assert(ret == Z_OK);
- strm.avail_in = len;
- strm.next_in = in;
- ret = inflateBack(&strm, pull, Z_NULL, push, Z_NULL);
- assert(ret != Z_STREAM_ERROR);
- if (err) {
- assert(ret == Z_DATA_ERROR);
- assert(strcmp(id, strm.msg) == 0);
- }
- inflateBackEnd(&strm);
- mem_done(&strm, prefix);
- }
-
- free(prefix);
- free(win);
- free(out);
- free(in);
- return ret;
- }
- local void cover_inflate(void)
- {
- try("0 0 0 0 0", "invalid stored block lengths", 1);
- try("3 0", "fixed", 0);
- try("6", "invalid block type", 1);
- try("1 1 0 fe ff 0", "stored", 0);
- try("fc 0 0", "too many length or distance symbols", 1);
- try("4 0 fe ff", "invalid code lengths set", 1);
- try("4 0 24 49 0", "invalid bit length repeat", 1);
- try("4 0 24 e9 ff ff", "invalid bit length repeat", 1);
- try("4 0 24 e9 ff 6d", "invalid code -- missing end-of-block", 1);
- try("4 80 49 92 24 49 92 24 71 ff ff 93 11 0",
- "invalid literal/lengths set", 1);
- try("4 80 49 92 24 49 92 24 f b4 ff ff c3 84", "invalid distances set", 1);
- try("4 c0 81 8 0 0 0 0 20 7f eb b 0 0", "invalid literal/length code", 1);
- try("2 7e ff ff", "invalid distance code", 1);
- try("c c0 81 0 0 0 0 0 90 ff 6b 4 0", "invalid distance too far back", 1);
-
- try("1f 8b 8 0 0 0 0 0 0 0 3 0 0 0 0 1", "incorrect data check", -1);
- 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 c0 21 d 0 0 0 80 b0 fe 6d 2f 91 6c", "pull 17", 0);
- 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 c0 1 1 0 0 0 40 20 ff 57 1b 42 2c 4f", "length extra", 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);
- inf("2 8 20 80 0 3 0", "inflate_fast TYPE return", 0, -15, 258,
- Z_STREAM_END);
- inf("63 18 5 40 c 0", "window wrap", 3, -8, 300, Z_OK);
- }
- local void cover_trees(void)
- {
- int ret;
- unsigned bits;
- unsigned short lens[16], work[16];
- code *next, table[ENOUGH_DISTS];
-
- for (bits = 0; bits < 15; bits++)
- lens[bits] = (unsigned short)(bits + 1);
- lens[15] = 15;
- next = table;
- bits = 15;
- ret = inflate_table(DISTS, lens, 16, &next, &bits, work);
- assert(ret == 1);
- next = table;
- bits = 1;
- ret = inflate_table(DISTS, lens, 16, &next, &bits, work);
- assert(ret == 1);
- fputs("inflate_table not enough errors\n", stderr);
- }
- local void cover_fast(void)
- {
- inf("e5 e0 81 ad 6d cb b2 2c c9 01 1e 59 63 ae 7d ee fb 4d fd b5 35 41 68"
- " ff 7f 0f 0 0 0", "fast length extra bits", 0, -8, 258, Z_DATA_ERROR);
- 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,
- Z_DATA_ERROR);
- inf("3 7e 0 0 0 0 0", "fast invalid distance code", 0, -8, 258,
- Z_DATA_ERROR);
- inf("1b 7 0 0 0 0 0", "fast invalid literal/length code", 0, -8, 258,
- Z_DATA_ERROR);
- 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",
- "fast 2nd level codes and too far back", 0, -8, 258, Z_DATA_ERROR);
- inf("63 18 5 8c 10 8 0 0 0 0", "very common case", 0, -8, 259, Z_OK);
- inf("63 60 60 18 c9 0 8 18 18 18 26 c0 28 0 29 0 0 0",
- "contiguous and wrap around window", 6, -8, 259, Z_OK);
- inf("63 0 3 0 0 0 0 0", "copy direct from output", 0, -8, 259,
- Z_STREAM_END);
- }
- int main(void)
- {
- fprintf(stderr, "%s\n", zlibVersion());
- cover_support();
- cover_wrap();
- cover_back();
- cover_inflate();
- cover_trees();
- cover_fast();
- return 0;
- }
|