Path: blob/main/sys/contrib/zstd/zlibWrapper/examples/example.c
48378 views
/* example.c contains minimal changes required to be compiled with zlibWrapper:1* - #include "zlib.h" was changed to #include "zstd_zlibwrapper.h"2* - test_flush() and test_sync() use functions not supported by zlibWrapper3therefore they are disabled while zstd compression is turned on */45/* example.c -- usage example of the zlib compression library6*/7/*8Copyright (c) 1995-2006, 2011 Jean-loup Gailly910This software is provided 'as-is', without any express or implied11warranty. In no event will the authors be held liable for any damages12arising from the use of this software.1314Permission is granted to anyone to use this software for any purpose,15including commercial applications, and to alter it and redistribute it16freely, subject to the following restrictions:17181. The origin of this software must not be misrepresented; you must not19claim that you wrote the original software. If you use this software20in a product, an acknowledgement in the product documentation would be21appreciated but is not required.222. Altered source versions must be plainly marked as such, and must not be23misrepresented as being the original software.243. This notice may not be removed or altered from any source distribution.25*/2627/* @(#) $Id$ */2829#include "zstd_zlibwrapper.h"30#include <stdio.h>3132#ifdef STDC33# include <string.h>34# include <stdlib.h>35#endif3637#if defined(VMS) || defined(RISCOS)38# define TESTFILE "foo-gz"39#else40# define TESTFILE "foo.gz"41#endif4243#define CHECK_ERR(err, msg) { \44if (err != Z_OK) { \45fprintf(stderr, "%s error: %d\n", msg, err); \46exit(1); \47} \48}4950z_const char hello[] = "hello, hello! I said hello, hello!";51/* "hello world" would be more standard, but the repeated "hello"52* stresses the compression code better, sorry...53*/5455const char dictionary[] = "hello, hello!";56uLong dictId; /* Adler32 value of the dictionary */5758void test_deflate OF((Byte *compr, uLong comprLen));59void test_inflate OF((Byte *compr, uLong comprLen,60Byte *uncompr, uLong uncomprLen));61void test_large_deflate OF((Byte *compr, uLong comprLen,62Byte *uncompr, uLong uncomprLen));63void test_large_inflate OF((Byte *compr, uLong comprLen,64Byte *uncompr, uLong uncomprLen));65void test_flush OF((Byte *compr, uLong *comprLen));66void test_sync OF((Byte *compr, uLong comprLen,67Byte *uncompr, uLong uncomprLen));68void test_dict_deflate OF((Byte *compr, uLong comprLen));69void test_dict_inflate OF((Byte *compr, uLong comprLen,70Byte *uncompr, uLong uncomprLen));71int main OF((int argc, char *argv[]));727374#ifdef Z_SOLO7576void *myalloc OF((void *, unsigned, unsigned));77void myfree OF((void *, void *));7879void *myalloc(q, n, m)80void *q;81unsigned n, m;82{83void *buf = calloc(n, m);84q = Z_NULL;85/* printf("myalloc %p n=%d m=%d\n", buf, n, m); */86return buf;87}8889void myfree(void *q, void *p)90{91/* printf("myfree %p\n", p); */92q = Z_NULL;93free(p);94}9596static alloc_func zalloc = myalloc;97static free_func zfree = myfree;9899#else /* !Z_SOLO */100101static alloc_func zalloc = (alloc_func)0;102static free_func zfree = (free_func)0;103104void test_compress OF((Byte *compr, uLong comprLen,105Byte *uncompr, uLong uncomprLen));106void test_gzio OF((const char *fname,107Byte *uncompr, uLong uncomprLen));108109/* ===========================================================================110* Test compress() and uncompress()111*/112void test_compress(compr, comprLen, uncompr, uncomprLen)113Byte *compr, *uncompr;114uLong comprLen, uncomprLen;115{116int err;117uLong len = (uLong)strlen(hello)+1;118119err = compress(compr, &comprLen, (const Bytef*)hello, len);120CHECK_ERR(err, "compress");121122strcpy((char*)uncompr, "garbage");123124err = uncompress(uncompr, &uncomprLen, compr, comprLen);125CHECK_ERR(err, "uncompress");126127if (strcmp((char*)uncompr, hello)) {128fprintf(stderr, "bad uncompress\n");129exit(1);130} else {131printf("uncompress(): %s\n", (char *)uncompr);132}133}134135/* ===========================================================================136* Test read/write of .gz files137*/138void test_gzio(fname, uncompr, uncomprLen)139const char *fname; /* compressed file name */140Byte *uncompr;141uLong uncomprLen;142{143#ifdef NO_GZCOMPRESS144fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n");145#else146int err;147int len = (int)strlen(hello)+1;148gzFile file;149z_off_t pos;150151file = gzopen(fname, "wb");152if (file == NULL) {153fprintf(stderr, "gzopen error\n");154exit(1);155}156gzputc(file, 'h');157if (gzputs(file, "ello") != 4) {158fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err));159exit(1);160}161if (gzprintf(file, ", %s! I said hello, hello!", "hello") != 8+21) {162fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));163exit(1);164}165gzseek(file, 1L, SEEK_CUR); /* add one zero byte */166gzclose(file);167168file = gzopen(fname, "rb");169if (file == NULL) {170fprintf(stderr, "gzopen error\n");171exit(1);172}173strcpy((char*)uncompr, "garbage");174175if (gzread(file, uncompr, (unsigned)uncomprLen) != len) {176fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));177exit(1);178}179if (strcmp((char*)uncompr, hello)) {180fprintf(stderr, "bad gzread: %s\n", (char*)uncompr);181exit(1);182} else {183printf("gzread(): %s\n", (char*)uncompr);184}185186pos = gzseek(file, -8L, SEEK_CUR);187if (pos != 6+21 || gztell(file) != pos) {188fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n",189(long)pos, (long)gztell(file));190exit(1);191}192193if (gzgetc(file) != ' ') {194fprintf(stderr, "gzgetc error\n");195exit(1);196}197198if (gzungetc(' ', file) != ' ') {199fprintf(stderr, "gzungetc error\n");200exit(1);201}202203gzgets(file, (char*)uncompr, (int)uncomprLen);204if (strlen((char*)uncompr) != 7) { /* " hello!" */205fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err));206exit(1);207}208if (strcmp((char*)uncompr, hello + 6+21)) {209fprintf(stderr, "bad gzgets after gzseek\n");210exit(1);211} else {212printf("gzgets() after gzseek: %s\n", (char*)uncompr);213}214215gzclose(file);216#endif217}218219#endif /* Z_SOLO */220221/* ===========================================================================222* Test deflate() with small buffers223*/224void test_deflate(compr, comprLen)225Byte *compr;226uLong comprLen;227{228z_stream c_stream; /* compression stream */229int err;230uLong len = (uLong)strlen(hello)+1;231232c_stream.zalloc = zalloc;233c_stream.zfree = zfree;234c_stream.opaque = (voidpf)0;235236err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);237CHECK_ERR(err, "deflateInit");238239c_stream.next_in = (z_const unsigned char *)hello;240c_stream.next_out = compr;241242while (c_stream.total_in != len && c_stream.total_out < comprLen) {243c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */244err = deflate(&c_stream, Z_NO_FLUSH);245CHECK_ERR(err, "deflate");246}247/* Finish the stream, still forcing small buffers: */248for (;;) {249c_stream.avail_out = 1;250err = deflate(&c_stream, Z_FINISH);251if (err == Z_STREAM_END) break;252CHECK_ERR(err, "deflate");253}254255err = deflateEnd(&c_stream);256CHECK_ERR(err, "deflateEnd");257}258259/* ===========================================================================260* Test inflate() with small buffers261*/262void test_inflate(compr, comprLen, uncompr, uncomprLen)263Byte *compr, *uncompr;264uLong comprLen, uncomprLen;265{266int err;267z_stream d_stream; /* decompression stream */268269strcpy((char*)uncompr, "garbage");270271d_stream.zalloc = zalloc;272d_stream.zfree = zfree;273d_stream.opaque = (voidpf)0;274275d_stream.next_in = compr;276d_stream.avail_in = 0;277d_stream.next_out = uncompr;278279err = inflateInit(&d_stream);280CHECK_ERR(err, "inflateInit");281282while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {283d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */284err = inflate(&d_stream, Z_NO_FLUSH);285if (err == Z_STREAM_END) break;286CHECK_ERR(err, "inflate");287}288289err = inflateEnd(&d_stream);290CHECK_ERR(err, "inflateEnd");291292if (strcmp((char*)uncompr, hello)) {293fprintf(stderr, "bad inflate\n");294exit(1);295} else {296printf("inflate(): %s\n", (char *)uncompr);297}298}299300/* ===========================================================================301* Test deflate() with large buffers and dynamic change of compression level302*/303void test_large_deflate(compr, comprLen, uncompr, uncomprLen)304Byte *compr, *uncompr;305uLong comprLen, uncomprLen;306{307z_stream c_stream; /* compression stream */308int err;309310c_stream.zalloc = zalloc;311c_stream.zfree = zfree;312c_stream.opaque = (voidpf)0;313314err = deflateInit(&c_stream, Z_BEST_SPEED);315CHECK_ERR(err, "deflateInit");316317c_stream.next_out = compr;318c_stream.avail_out = (uInt)comprLen;319320/* At this point, uncompr is still mostly zeroes, so it should compress321* very well:322*/323c_stream.next_in = uncompr;324c_stream.avail_in = (uInt)uncomprLen;325err = deflate(&c_stream, Z_NO_FLUSH);326CHECK_ERR(err, "deflate");327if (c_stream.avail_in != 0) {328fprintf(stderr, "deflate not greedy\n");329exit(1);330}331332/* Feed in already compressed data and switch to no compression: */333deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);334c_stream.next_in = compr;335c_stream.avail_in = (uInt)comprLen/2;336err = deflate(&c_stream, Z_NO_FLUSH);337CHECK_ERR(err, "deflate");338339/* Switch back to compressing mode: */340deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);341c_stream.next_in = uncompr;342c_stream.avail_in = (uInt)uncomprLen;343err = deflate(&c_stream, Z_NO_FLUSH);344CHECK_ERR(err, "deflate");345346err = deflate(&c_stream, Z_FINISH);347if (err != Z_STREAM_END) {348fprintf(stderr, "deflate should report Z_STREAM_END\n");349exit(1);350}351err = deflateEnd(&c_stream);352CHECK_ERR(err, "deflateEnd");353}354355/* ===========================================================================356* Test inflate() with large buffers357*/358void test_large_inflate(compr, comprLen, uncompr, uncomprLen)359Byte *compr, *uncompr;360uLong comprLen, uncomprLen;361{362int err;363z_stream d_stream; /* decompression stream */364365strcpy((char*)uncompr, "garbage");366367d_stream.zalloc = zalloc;368d_stream.zfree = zfree;369d_stream.opaque = (voidpf)0;370371d_stream.next_in = compr;372d_stream.avail_in = (uInt)comprLen;373374err = inflateInit(&d_stream);375CHECK_ERR(err, "inflateInit");376377for (;;) {378d_stream.next_out = uncompr; /* discard the output */379d_stream.avail_out = (uInt)uncomprLen;380err = inflate(&d_stream, Z_NO_FLUSH);381if (err == Z_STREAM_END) break;382CHECK_ERR(err, "large inflate");383}384385err = inflateEnd(&d_stream);386CHECK_ERR(err, "inflateEnd");387388if (d_stream.total_out != 2*uncomprLen + comprLen/2) {389fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out);390exit(1);391} else {392printf("large_inflate(): OK\n");393}394}395396/* ===========================================================================397* Test deflate() with full flush398*/399void test_flush(compr, comprLen)400Byte *compr;401uLong *comprLen;402{403z_stream c_stream; /* compression stream */404int err;405uInt len = (uInt)strlen(hello)+1;406407c_stream.zalloc = zalloc;408c_stream.zfree = zfree;409c_stream.opaque = (voidpf)0;410411err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);412CHECK_ERR(err, "deflateInit");413414c_stream.next_in = (z_const unsigned char *)hello;415c_stream.next_out = compr;416c_stream.avail_in = 3;417c_stream.avail_out = (uInt)*comprLen;418err = deflate(&c_stream, Z_FULL_FLUSH);419CHECK_ERR(err, "deflate");420421compr[3]++; /* force an error in first compressed block */422c_stream.avail_in = len - 3;423424err = deflate(&c_stream, Z_FINISH);425if (err != Z_STREAM_END) {426CHECK_ERR(err, "deflate");427}428err = deflateEnd(&c_stream);429CHECK_ERR(err, "deflateEnd");430431*comprLen = c_stream.total_out;432}433434/* ===========================================================================435* Test inflateSync()436*/437void test_sync(compr, comprLen, uncompr, uncomprLen)438Byte *compr, *uncompr;439uLong comprLen, uncomprLen;440{441int err;442z_stream d_stream; /* decompression stream */443444strcpy((char*)uncompr, "garbage");445446d_stream.zalloc = zalloc;447d_stream.zfree = zfree;448d_stream.opaque = (voidpf)0;449450d_stream.next_in = compr;451d_stream.avail_in = 2; /* just read the zlib header */452453err = inflateInit(&d_stream);454CHECK_ERR(err, "inflateInit");455456d_stream.next_out = uncompr;457d_stream.avail_out = (uInt)uncomprLen;458459inflate(&d_stream, Z_NO_FLUSH);460CHECK_ERR(err, "inflate");461462d_stream.avail_in = (uInt)comprLen-2; /* read all compressed data */463err = inflateSync(&d_stream); /* but skip the damaged part */464CHECK_ERR(err, "inflateSync");465466err = inflate(&d_stream, Z_FINISH);467if (err != Z_DATA_ERROR) {468fprintf(stderr, "inflate should report DATA_ERROR\n");469/* Because of incorrect adler32 */470exit(1);471}472err = inflateEnd(&d_stream);473CHECK_ERR(err, "inflateEnd");474475printf("after inflateSync(): hel%s\n", (char *)uncompr);476}477478/* ===========================================================================479* Test deflate() with preset dictionary480*/481void test_dict_deflate(compr, comprLen)482Byte *compr;483uLong comprLen;484{485z_stream c_stream; /* compression stream */486int err;487488c_stream.zalloc = zalloc;489c_stream.zfree = zfree;490c_stream.opaque = (voidpf)0;491492err = deflateInit(&c_stream, Z_BEST_COMPRESSION);493CHECK_ERR(err, "deflateInit");494495err = deflateSetDictionary(&c_stream,496(const Bytef*)dictionary, (int)sizeof(dictionary));497CHECK_ERR(err, "deflateSetDictionary");498499dictId = c_stream.adler;500c_stream.next_out = compr;501c_stream.avail_out = (uInt)comprLen;502503c_stream.next_in = (z_const unsigned char *)hello;504c_stream.avail_in = (uInt)strlen(hello)+1;505506err = deflate(&c_stream, Z_FINISH);507if (err != Z_STREAM_END) {508fprintf(stderr, "deflate should report Z_STREAM_END\n");509exit(1);510}511err = deflateEnd(&c_stream);512CHECK_ERR(err, "deflateEnd");513}514515/* ===========================================================================516* Test inflate() with a preset dictionary517*/518void test_dict_inflate(compr, comprLen, uncompr, uncomprLen)519Byte *compr, *uncompr;520uLong comprLen, uncomprLen;521{522int err;523z_stream d_stream; /* decompression stream */524525strcpy((char*)uncompr, "garbage");526527d_stream.zalloc = zalloc;528d_stream.zfree = zfree;529d_stream.opaque = (voidpf)0;530531d_stream.next_in = compr;532d_stream.avail_in = (uInt)comprLen;533534err = inflateInit(&d_stream);535CHECK_ERR(err, "inflateInit");536537d_stream.next_out = uncompr;538d_stream.avail_out = (uInt)uncomprLen;539540for (;;) {541err = inflate(&d_stream, Z_NO_FLUSH);542if (err == Z_STREAM_END) break;543if (err == Z_NEED_DICT) {544if (d_stream.adler != dictId) {545fprintf(stderr, "unexpected dictionary");546exit(1);547}548err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,549(int)sizeof(dictionary));550}551CHECK_ERR(err, "inflate with dict");552}553554err = inflateEnd(&d_stream);555CHECK_ERR(err, "inflateEnd");556557if (strcmp((char*)uncompr, hello)) {558fprintf(stderr, "bad inflate with dict\n");559exit(1);560} else {561printf("inflate with dictionary: %s\n", (char *)uncompr);562}563}564565/* ===========================================================================566* Usage: example [output.gz [input.gz]]567*/568569int main(argc, argv)570int argc;571char *argv[];572{573Byte *compr, *uncompr;574uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */575uLong uncomprLen = comprLen;576static const char* myVersion = ZLIB_VERSION;577578if (zlibVersion()[0] != myVersion[0]) {579fprintf(stderr, "incompatible zlib version\n");580exit(1);581582} else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {583fprintf(stderr, "warning: different zlib version\n");584}585586printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n",587ZLIB_VERSION, ZLIB_VERNUM, zlibCompileFlags());588if (ZWRAP_isUsingZSTDcompression()) printf("zstd version %s\n", zstdVersion());589590compr = (Byte*)calloc((uInt)comprLen, 1);591uncompr = (Byte*)calloc((uInt)uncomprLen, 1);592/* compr and uncompr are cleared to avoid reading uninitialized593* data and to ensure that uncompr compresses well.594*/595if (compr == Z_NULL || uncompr == Z_NULL) {596printf("out of memory\n");597exit(1);598}599600#ifdef Z_SOLO601argc = strlen(argv[0]);602#else603test_compress(compr, comprLen, uncompr, uncomprLen);604605test_gzio((argc > 1 ? argv[1] : TESTFILE),606uncompr, uncomprLen);607#endif608609test_deflate(compr, comprLen);610test_inflate(compr, comprLen, uncompr, uncomprLen);611612test_large_deflate(compr, comprLen, uncompr, uncomprLen);613test_large_inflate(compr, comprLen, uncompr, uncomprLen);614615if (!ZWRAP_isUsingZSTDcompression()) {616test_flush(compr, &comprLen);617test_sync(compr, comprLen, uncompr, uncomprLen);618}619comprLen = uncomprLen;620621test_dict_deflate(compr, comprLen);622test_dict_inflate(compr, comprLen, uncompr, uncomprLen);623624free(compr);625free(uncompr);626627return 0;628}629630631