Path: blob/main/sys/contrib/zstd/zlibWrapper/examples/example_original.c
48378 views
/* example.c -- usage example of the zlib compression library1*/2/*3Copyright (c) 1995-2006, 2011 Jean-loup Gailly45This software is provided 'as-is', without any express or implied6warranty. In no event will the authors be held liable for any damages7arising from the use of this software.89Permission is granted to anyone to use this software for any purpose,10including commercial applications, and to alter it and redistribute it11freely, subject to the following restrictions:12131. The origin of this software must not be misrepresented; you must not14claim that you wrote the original software. If you use this software15in a product, an acknowledgement in the product documentation would be16appreciated but is not required.172. Altered source versions must be plainly marked as such, and must not be18misrepresented as being the original software.193. This notice may not be removed or altered from any source distribution.20*/2122/* @(#) $Id$ */2324#include "zlib.h"25#include <stdio.h>2627#ifdef STDC28# include <string.h>29# include <stdlib.h>30#endif3132#if defined(VMS) || defined(RISCOS)33# define TESTFILE "foo-gz"34#else35# define TESTFILE "foo.gz"36#endif3738#define CHECK_ERR(err, msg) { \39if (err != Z_OK) { \40fprintf(stderr, "%s error: %d\n", msg, err); \41exit(1); \42} \43}4445z_const char hello[] = "hello, hello!";46/* "hello world" would be more standard, but the repeated "hello"47* stresses the compression code better, sorry...48*/4950const char dictionary[] = "hello";51uLong dictId; /* Adler32 value of the dictionary */5253void test_deflate OF((Byte *compr, uLong comprLen));54void test_inflate OF((Byte *compr, uLong comprLen,55Byte *uncompr, uLong uncomprLen));56void test_large_deflate OF((Byte *compr, uLong comprLen,57Byte *uncompr, uLong uncomprLen));58void test_large_inflate OF((Byte *compr, uLong comprLen,59Byte *uncompr, uLong uncomprLen));60void test_flush OF((Byte *compr, uLong *comprLen));61void test_sync OF((Byte *compr, uLong comprLen,62Byte *uncompr, uLong uncomprLen));63void test_dict_deflate OF((Byte *compr, uLong comprLen));64void test_dict_inflate OF((Byte *compr, uLong comprLen,65Byte *uncompr, uLong uncomprLen));66int main OF((int argc, char *argv[]));676869#ifdef Z_SOLO7071void *myalloc OF((void *, unsigned, unsigned));72void myfree OF((void *, void *));7374void *myalloc(q, n, m)75void *q;76unsigned n, m;77{78q = Z_NULL;79return calloc(n, m);80}8182void myfree(void *q, void *p)83{84q = Z_NULL;85free(p);86}8788static alloc_func zalloc = myalloc;89static free_func zfree = myfree;9091#else /* !Z_SOLO */9293static alloc_func zalloc = (alloc_func)0;94static free_func zfree = (free_func)0;9596void test_compress OF((Byte *compr, uLong comprLen,97Byte *uncompr, uLong uncomprLen));98void test_gzio OF((const char *fname,99Byte *uncompr, uLong uncomprLen));100101/* ===========================================================================102* Test compress() and uncompress()103*/104void test_compress(compr, comprLen, uncompr, uncomprLen)105Byte *compr, *uncompr;106uLong comprLen, uncomprLen;107{108int err;109uLong len = (uLong)strlen(hello)+1;110111err = compress(compr, &comprLen, (const Bytef*)hello, len);112CHECK_ERR(err, "compress");113114strcpy((char*)uncompr, "garbage");115116err = uncompress(uncompr, &uncomprLen, compr, comprLen);117CHECK_ERR(err, "uncompress");118119if (strcmp((char*)uncompr, hello)) {120fprintf(stderr, "bad uncompress\n");121exit(1);122} else {123printf("uncompress(): %s\n", (char *)uncompr);124}125}126127/* ===========================================================================128* Test read/write of .gz files129*/130void test_gzio(fname, uncompr, uncomprLen)131const char *fname; /* compressed file name */132Byte *uncompr;133uLong uncomprLen;134{135#ifdef NO_GZCOMPRESS136fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n");137#else138int err;139int len = (int)strlen(hello)+1;140gzFile file;141z_off_t pos;142143file = gzopen(fname, "wb");144if (file == NULL) {145fprintf(stderr, "gzopen error\n");146exit(1);147}148gzputc(file, 'h');149if (gzputs(file, "ello") != 4) {150fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err));151exit(1);152}153if (gzprintf(file, ", %s!", "hello") != 8) {154fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));155exit(1);156}157gzseek(file, 1L, SEEK_CUR); /* add one zero byte */158gzclose(file);159160file = gzopen(fname, "rb");161if (file == NULL) {162fprintf(stderr, "gzopen error\n");163exit(1);164}165strcpy((char*)uncompr, "garbage");166167if (gzread(file, uncompr, (unsigned)uncomprLen) != len) {168fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));169exit(1);170}171if (strcmp((char*)uncompr, hello)) {172fprintf(stderr, "bad gzread: %s\n", (char*)uncompr);173exit(1);174} else {175printf("gzread(): %s\n", (char*)uncompr);176}177178pos = gzseek(file, -8L, SEEK_CUR);179if (pos != 6 || gztell(file) != pos) {180fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n",181(long)pos, (long)gztell(file));182exit(1);183}184185if (gzgetc(file) != ' ') {186fprintf(stderr, "gzgetc error\n");187exit(1);188}189190if (gzungetc(' ', file) != ' ') {191fprintf(stderr, "gzungetc error\n");192exit(1);193}194195gzgets(file, (char*)uncompr, (int)uncomprLen);196if (strlen((char*)uncompr) != 7) { /* " hello!" */197fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err));198exit(1);199}200if (strcmp((char*)uncompr, hello + 6)) {201fprintf(stderr, "bad gzgets after gzseek\n");202exit(1);203} else {204printf("gzgets() after gzseek: %s\n", (char*)uncompr);205}206207gzclose(file);208#endif209}210211#endif /* Z_SOLO */212213/* ===========================================================================214* Test deflate() with small buffers215*/216void test_deflate(compr, comprLen)217Byte *compr;218uLong comprLen;219{220z_stream c_stream; /* compression stream */221int err;222uLong len = (uLong)strlen(hello)+1;223224c_stream.zalloc = zalloc;225c_stream.zfree = zfree;226c_stream.opaque = (voidpf)0;227228err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);229CHECK_ERR(err, "deflateInit");230231c_stream.next_in = (z_const unsigned char *)hello;232c_stream.next_out = compr;233234while (c_stream.total_in != len && c_stream.total_out < comprLen) {235c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */236err = deflate(&c_stream, Z_NO_FLUSH);237CHECK_ERR(err, "deflate");238}239/* Finish the stream, still forcing small buffers: */240for (;;) {241c_stream.avail_out = 1;242err = deflate(&c_stream, Z_FINISH);243if (err == Z_STREAM_END) break;244CHECK_ERR(err, "deflate");245}246247err = deflateEnd(&c_stream);248CHECK_ERR(err, "deflateEnd");249}250251/* ===========================================================================252* Test inflate() with small buffers253*/254void test_inflate(compr, comprLen, uncompr, uncomprLen)255Byte *compr, *uncompr;256uLong comprLen, uncomprLen;257{258int err;259z_stream d_stream; /* decompression stream */260261strcpy((char*)uncompr, "garbage");262263d_stream.zalloc = zalloc;264d_stream.zfree = zfree;265d_stream.opaque = (voidpf)0;266267d_stream.next_in = compr;268d_stream.avail_in = 0;269d_stream.next_out = uncompr;270271err = inflateInit(&d_stream);272CHECK_ERR(err, "inflateInit");273274while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {275d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */276err = inflate(&d_stream, Z_NO_FLUSH);277if (err == Z_STREAM_END) break;278CHECK_ERR(err, "inflate");279}280281err = inflateEnd(&d_stream);282CHECK_ERR(err, "inflateEnd");283284if (strcmp((char*)uncompr, hello)) {285fprintf(stderr, "bad inflate\n");286exit(1);287} else {288printf("inflate(): %s\n", (char *)uncompr);289}290}291292/* ===========================================================================293* Test deflate() with large buffers and dynamic change of compression level294*/295void test_large_deflate(compr, comprLen, uncompr, uncomprLen)296Byte *compr, *uncompr;297uLong comprLen, uncomprLen;298{299z_stream c_stream; /* compression stream */300int err;301302c_stream.zalloc = zalloc;303c_stream.zfree = zfree;304c_stream.opaque = (voidpf)0;305306err = deflateInit(&c_stream, Z_BEST_SPEED);307CHECK_ERR(err, "deflateInit");308309c_stream.next_out = compr;310c_stream.avail_out = (uInt)comprLen;311312/* At this point, uncompr is still mostly zeroes, so it should compress313* very well:314*/315c_stream.next_in = uncompr;316c_stream.avail_in = (uInt)uncomprLen;317err = deflate(&c_stream, Z_NO_FLUSH);318CHECK_ERR(err, "deflate");319if (c_stream.avail_in != 0) {320fprintf(stderr, "deflate not greedy\n");321exit(1);322}323324/* Feed in already compressed data and switch to no compression: */325deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);326c_stream.next_in = compr;327c_stream.avail_in = (uInt)comprLen/2;328err = deflate(&c_stream, Z_NO_FLUSH);329CHECK_ERR(err, "deflate");330331/* Switch back to compressing mode: */332deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);333c_stream.next_in = uncompr;334c_stream.avail_in = (uInt)uncomprLen;335err = deflate(&c_stream, Z_NO_FLUSH);336CHECK_ERR(err, "deflate");337338err = deflate(&c_stream, Z_FINISH);339if (err != Z_STREAM_END) {340fprintf(stderr, "deflate should report Z_STREAM_END\n");341exit(1);342}343err = deflateEnd(&c_stream);344CHECK_ERR(err, "deflateEnd");345}346347/* ===========================================================================348* Test inflate() with large buffers349*/350void test_large_inflate(compr, comprLen, uncompr, uncomprLen)351Byte *compr, *uncompr;352uLong comprLen, uncomprLen;353{354int err;355z_stream d_stream; /* decompression stream */356357strcpy((char*)uncompr, "garbage");358359d_stream.zalloc = zalloc;360d_stream.zfree = zfree;361d_stream.opaque = (voidpf)0;362363d_stream.next_in = compr;364d_stream.avail_in = (uInt)comprLen;365366err = inflateInit(&d_stream);367CHECK_ERR(err, "inflateInit");368369for (;;) {370d_stream.next_out = uncompr; /* discard the output */371d_stream.avail_out = (uInt)uncomprLen;372err = inflate(&d_stream, Z_NO_FLUSH);373if (err == Z_STREAM_END) break;374CHECK_ERR(err, "large inflate");375}376377err = inflateEnd(&d_stream);378CHECK_ERR(err, "inflateEnd");379380if (d_stream.total_out != 2*uncomprLen + comprLen/2) {381fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out);382exit(1);383} else {384printf("large_inflate(): OK\n");385}386}387388/* ===========================================================================389* Test deflate() with full flush390*/391void test_flush(compr, comprLen)392Byte *compr;393uLong *comprLen;394{395z_stream c_stream; /* compression stream */396int err;397uInt len = (uInt)strlen(hello)+1;398399c_stream.zalloc = zalloc;400c_stream.zfree = zfree;401c_stream.opaque = (voidpf)0;402403err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);404CHECK_ERR(err, "deflateInit");405406c_stream.next_in = (z_const unsigned char *)hello;407c_stream.next_out = compr;408c_stream.avail_in = 3;409c_stream.avail_out = (uInt)*comprLen;410err = deflate(&c_stream, Z_FULL_FLUSH);411CHECK_ERR(err, "deflate");412413compr[3]++; /* force an error in first compressed block */414c_stream.avail_in = len - 3;415416err = deflate(&c_stream, Z_FINISH);417if (err != Z_STREAM_END) {418CHECK_ERR(err, "deflate");419}420err = deflateEnd(&c_stream);421CHECK_ERR(err, "deflateEnd");422423*comprLen = c_stream.total_out;424}425426/* ===========================================================================427* Test inflateSync()428*/429void test_sync(compr, comprLen, uncompr, uncomprLen)430Byte *compr, *uncompr;431uLong comprLen, uncomprLen;432{433int err;434z_stream d_stream; /* decompression stream */435436strcpy((char*)uncompr, "garbage");437438d_stream.zalloc = zalloc;439d_stream.zfree = zfree;440d_stream.opaque = (voidpf)0;441442d_stream.next_in = compr;443d_stream.avail_in = 2; /* just read the zlib header */444445err = inflateInit(&d_stream);446CHECK_ERR(err, "inflateInit");447448d_stream.next_out = uncompr;449d_stream.avail_out = (uInt)uncomprLen;450451inflate(&d_stream, Z_NO_FLUSH);452CHECK_ERR(err, "inflate");453454d_stream.avail_in = (uInt)comprLen-2; /* read all compressed data */455err = inflateSync(&d_stream); /* but skip the damaged part */456CHECK_ERR(err, "inflateSync");457458err = inflate(&d_stream, Z_FINISH);459if (err != Z_DATA_ERROR) {460fprintf(stderr, "inflate should report DATA_ERROR\n");461/* Because of incorrect adler32 */462exit(1);463}464err = inflateEnd(&d_stream);465CHECK_ERR(err, "inflateEnd");466467printf("after inflateSync(): hel%s\n", (char *)uncompr);468}469470/* ===========================================================================471* Test deflate() with preset dictionary472*/473void test_dict_deflate(compr, comprLen)474Byte *compr;475uLong comprLen;476{477z_stream c_stream; /* compression stream */478int err;479480c_stream.zalloc = zalloc;481c_stream.zfree = zfree;482c_stream.opaque = (voidpf)0;483484err = deflateInit(&c_stream, Z_BEST_COMPRESSION);485CHECK_ERR(err, "deflateInit");486487err = deflateSetDictionary(&c_stream,488(const Bytef*)dictionary, (int)sizeof(dictionary));489CHECK_ERR(err, "deflateSetDictionary");490491dictId = c_stream.adler;492c_stream.next_out = compr;493c_stream.avail_out = (uInt)comprLen;494495c_stream.next_in = (z_const unsigned char *)hello;496c_stream.avail_in = (uInt)strlen(hello)+1;497498err = deflate(&c_stream, Z_FINISH);499if (err != Z_STREAM_END) {500fprintf(stderr, "deflate should report Z_STREAM_END\n");501exit(1);502}503err = deflateEnd(&c_stream);504CHECK_ERR(err, "deflateEnd");505}506507/* ===========================================================================508* Test inflate() with a preset dictionary509*/510void test_dict_inflate(compr, comprLen, uncompr, uncomprLen)511Byte *compr, *uncompr;512uLong comprLen, uncomprLen;513{514int err;515z_stream d_stream; /* decompression stream */516517strcpy((char*)uncompr, "garbage");518519d_stream.zalloc = zalloc;520d_stream.zfree = zfree;521d_stream.opaque = (voidpf)0;522523d_stream.next_in = compr;524d_stream.avail_in = (uInt)comprLen;525526err = inflateInit(&d_stream);527CHECK_ERR(err, "inflateInit");528529d_stream.next_out = uncompr;530d_stream.avail_out = (uInt)uncomprLen;531532for (;;) {533err = inflate(&d_stream, Z_NO_FLUSH);534if (err == Z_STREAM_END) break;535if (err == Z_NEED_DICT) {536if (d_stream.adler != dictId) {537fprintf(stderr, "unexpected dictionary");538exit(1);539}540err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,541(int)sizeof(dictionary));542}543CHECK_ERR(err, "inflate with dict");544}545546err = inflateEnd(&d_stream);547CHECK_ERR(err, "inflateEnd");548549if (strcmp((char*)uncompr, hello)) {550fprintf(stderr, "bad inflate with dict\n");551exit(1);552} else {553printf("inflate with dictionary: %s\n", (char *)uncompr);554}555}556557/* ===========================================================================558* Usage: example [output.gz [input.gz]]559*/560561int main(argc, argv)562int argc;563char *argv[];564{565Byte *compr, *uncompr;566uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */567uLong uncomprLen = comprLen;568static const char* myVersion = ZLIB_VERSION;569570if (zlibVersion()[0] != myVersion[0]) {571fprintf(stderr, "incompatible zlib version\n");572exit(1);573574} else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {575fprintf(stderr, "warning: different zlib version\n");576}577578printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n",579ZLIB_VERSION, ZLIB_VERNUM, zlibCompileFlags());580581compr = (Byte*)calloc((uInt)comprLen, 1);582uncompr = (Byte*)calloc((uInt)uncomprLen, 1);583/* compr and uncompr are cleared to avoid reading uninitialized584* data and to ensure that uncompr compresses well.585*/586if (compr == Z_NULL || uncompr == Z_NULL) {587printf("out of memory\n");588exit(1);589}590591#ifdef Z_SOLO592argc = strlen(argv[0]);593#else594test_compress(compr, comprLen, uncompr, uncomprLen);595596test_gzio((argc > 1 ? argv[1] : TESTFILE),597uncompr, uncomprLen);598#endif599600test_deflate(compr, comprLen);601test_inflate(compr, comprLen, uncompr, uncomprLen);602603test_large_deflate(compr, comprLen, uncompr, uncomprLen);604test_large_inflate(compr, comprLen, uncompr, uncomprLen);605606test_flush(compr, &comprLen);607test_sync(compr, comprLen, uncompr, uncomprLen);608comprLen = uncomprLen;609610test_dict_deflate(compr, comprLen);611test_dict_inflate(compr, comprLen, uncompr, uncomprLen);612613free(compr);614free(uncompr);615616return 0;617}618619620