1 /* example.c contains minimal changes required to be compiled with zlibWrapper:
2 * - #include "zlib.h" was changed to #include "zstd_zlibwrapper.h"
3 * - test_flush() and test_sync() use functions not supported by zlibWrapper
4 therefore they are disabled while zstd compression is turned on */
6 /* example.c -- usage example of the zlib compression library
9 Copyright (c) 1995-2006, 2011 Jean-loup Gailly
11 This software is provided 'as-is', without any express or implied
12 warranty. In no event will the authors be held liable for any damages
13 arising from the use of this software.
15 Permission is granted to anyone to use this software for any purpose,
16 including commercial applications, and to alter it and redistribute it
17 freely, subject to the following restrictions:
19 1. The origin of this software must not be misrepresented; you must not
20 claim that you wrote the original software. If you use this software
21 in a product, an acknowledgement in the product documentation would be
22 appreciated but is not required.
23 2. Altered source versions must be plainly marked as such, and must not be
24 misrepresented as being the original software.
25 3. This notice may not be removed or altered from any source distribution.
30 #include "zstd_zlibwrapper.h"
38 #if defined(VMS) || defined(RISCOS)
39 # define TESTFILE "foo-gz"
41 # define TESTFILE "foo.gz"
44 #define CHECK_ERR(err, msg) { \
46 fprintf(stderr, "%s error: %d\n", msg, err); \
51 z_const char hello[] = "hello, hello! I said hello, hello!";
52 /* "hello world" would be more standard, but the repeated "hello"
53 * stresses the compression code better, sorry...
56 const char dictionary[] = "hello, hello!";
57 uLong dictId; /* Adler32 value of the dictionary */
59 void test_deflate _Z_OF((Byte *compr, uLong comprLen));
60 void test_inflate _Z_OF((Byte *compr, uLong comprLen,
61 Byte *uncompr, uLong uncomprLen));
62 void test_large_deflate _Z_OF((Byte *compr, uLong comprLen,
63 Byte *uncompr, uLong uncomprLen));
64 void test_large_inflate _Z_OF((Byte *compr, uLong comprLen,
65 Byte *uncompr, uLong uncomprLen));
66 void test_flush _Z_OF((Byte *compr, uLong *comprLen));
67 void test_sync _Z_OF((Byte *compr, uLong comprLen,
68 Byte *uncompr, uLong uncomprLen));
69 void test_dict_deflate _Z_OF((Byte *compr, uLong comprLen));
70 void test_dict_inflate _Z_OF((Byte *compr, uLong comprLen,
71 Byte *uncompr, uLong uncomprLen));
72 int main _Z_OF((int argc, char *argv[]));
77 void *myalloc _Z_OF((void *, unsigned, unsigned));
78 void myfree _Z_OF((void *, void *));
80 void *myalloc(q, n, m)
84 void *buf = calloc(n, m);
86 /* printf("myalloc %p n=%d m=%d\n", buf, n, m); */
90 void myfree(void *q, void *p)
92 /* printf("myfree %p\n", p); */
97 static alloc_func zalloc = myalloc;
98 static free_func zfree = myfree;
102 static alloc_func zalloc = (alloc_func)0;
103 static free_func zfree = (free_func)0;
105 void test_compress _Z_OF((Byte *compr, uLong comprLen,
106 Byte *uncompr, uLong uncomprLen));
107 void test_gzio _Z_OF((const char *fname,
108 Byte *uncompr, uLong uncomprLen));
110 /* ===========================================================================
111 * Test compress() and uncompress()
113 void test_compress(compr, comprLen, uncompr, uncomprLen)
114 Byte *compr, *uncompr;
115 uLong comprLen, uncomprLen;
118 uLong len = (uLong)strlen(hello)+1;
120 err = compress(compr, &comprLen, (const Bytef*)hello, len);
121 CHECK_ERR(err, "compress");
123 strcpy((char*)uncompr, "garbage");
125 err = uncompress(uncompr, &uncomprLen, compr, comprLen);
126 CHECK_ERR(err, "uncompress");
128 if (strcmp((char*)uncompr, hello)) {
129 fprintf(stderr, "bad uncompress\n");
132 printf("uncompress(): %s\n", (char *)uncompr);
136 /* ===========================================================================
137 * Test read/write of .gz files
139 void test_gzio(fname, uncompr, uncomprLen)
140 const char *fname; /* compressed file name */
145 fprintf(stderr, "NO_GZCOMPRESS -- gz* functions cannot compress\n");
148 int len = (int)strlen(hello)+1;
152 file = gzopen(fname, "wb");
154 fprintf(stderr, "gzopen error\n");
158 if (gzputs(file, "ello") != 4) {
159 fprintf(stderr, "gzputs err: %s\n", gzerror(file, &err));
162 if (gzprintf(file, ", %s! I said hello, hello!", "hello") != 8+21) {
163 fprintf(stderr, "gzprintf err: %s\n", gzerror(file, &err));
166 gzseek(file, 1L, SEEK_CUR); /* add one zero byte */
169 file = gzopen(fname, "rb");
171 fprintf(stderr, "gzopen error\n");
174 strcpy((char*)uncompr, "garbage");
176 if (gzread(file, uncompr, (unsigned)uncomprLen) != len) {
177 fprintf(stderr, "gzread err: %s\n", gzerror(file, &err));
180 if (strcmp((char*)uncompr, hello)) {
181 fprintf(stderr, "bad gzread: %s\n", (char*)uncompr);
184 printf("gzread(): %s\n", (char*)uncompr);
187 pos = gzseek(file, -8L, SEEK_CUR);
188 if (pos != 6+21 || gztell(file) != pos) {
189 fprintf(stderr, "gzseek error, pos=%ld, gztell=%ld\n",
190 (long)pos, (long)gztell(file));
194 if (gzgetc(file) != ' ') {
195 fprintf(stderr, "gzgetc error\n");
199 if (gzungetc(' ', file) != ' ') {
200 fprintf(stderr, "gzungetc error\n");
204 gzgets(file, (char*)uncompr, (int)uncomprLen);
205 if (strlen((char*)uncompr) != 7) { /* " hello!" */
206 fprintf(stderr, "gzgets err after gzseek: %s\n", gzerror(file, &err));
209 if (strcmp((char*)uncompr, hello + 6+21)) {
210 fprintf(stderr, "bad gzgets after gzseek\n");
213 printf("gzgets() after gzseek: %s\n", (char*)uncompr);
222 /* ===========================================================================
223 * Test deflate() with small buffers
225 void test_deflate(compr, comprLen)
229 z_stream c_stream; /* compression stream */
231 uLong len = (uLong)strlen(hello)+1;
233 c_stream.zalloc = zalloc;
234 c_stream.zfree = zfree;
235 c_stream.opaque = (voidpf)0;
237 err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
238 CHECK_ERR(err, "deflateInit");
240 c_stream.next_in = (z_const unsigned char *)hello;
241 c_stream.next_out = compr;
243 while (c_stream.total_in != len && c_stream.total_out < comprLen) {
244 c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
245 err = deflate(&c_stream, Z_NO_FLUSH);
246 CHECK_ERR(err, "deflate");
248 /* Finish the stream, still forcing small buffers: */
250 c_stream.avail_out = 1;
251 err = deflate(&c_stream, Z_FINISH);
252 if (err == Z_STREAM_END) break;
253 CHECK_ERR(err, "deflate");
256 err = deflateEnd(&c_stream);
257 CHECK_ERR(err, "deflateEnd");
260 /* ===========================================================================
261 * Test inflate() with small buffers
263 void test_inflate(compr, comprLen, uncompr, uncomprLen)
264 Byte *compr, *uncompr;
265 uLong comprLen, uncomprLen;
268 z_stream d_stream; /* decompression stream */
270 strcpy((char*)uncompr, "garbage");
272 d_stream.zalloc = zalloc;
273 d_stream.zfree = zfree;
274 d_stream.opaque = (voidpf)0;
276 d_stream.next_in = compr;
277 d_stream.avail_in = 0;
278 d_stream.next_out = uncompr;
280 err = inflateInit(&d_stream);
281 CHECK_ERR(err, "inflateInit");
283 while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
284 d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
285 err = inflate(&d_stream, Z_NO_FLUSH);
286 if (err == Z_STREAM_END) break;
287 CHECK_ERR(err, "inflate");
290 err = inflateEnd(&d_stream);
291 CHECK_ERR(err, "inflateEnd");
293 if (strcmp((char*)uncompr, hello)) {
294 fprintf(stderr, "bad inflate\n");
297 printf("inflate(): %s\n", (char *)uncompr);
301 /* ===========================================================================
302 * Test deflate() with large buffers and dynamic change of compression level
304 void test_large_deflate(compr, comprLen, uncompr, uncomprLen)
305 Byte *compr, *uncompr;
306 uLong comprLen, uncomprLen;
308 z_stream c_stream; /* compression stream */
311 c_stream.zalloc = zalloc;
312 c_stream.zfree = zfree;
313 c_stream.opaque = (voidpf)0;
315 err = deflateInit(&c_stream, Z_BEST_SPEED);
316 CHECK_ERR(err, "deflateInit");
318 c_stream.next_out = compr;
319 c_stream.avail_out = (uInt)comprLen;
321 /* At this point, uncompr is still mostly zeroes, so it should compress
324 c_stream.next_in = uncompr;
325 c_stream.avail_in = (uInt)uncomprLen;
326 err = deflate(&c_stream, Z_NO_FLUSH);
327 CHECK_ERR(err, "deflate");
328 if (c_stream.avail_in != 0) {
329 fprintf(stderr, "deflate not greedy\n");
333 /* Feed in already compressed data and switch to no compression: */
334 deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
335 c_stream.next_in = compr;
336 c_stream.avail_in = (uInt)comprLen/2;
337 err = deflate(&c_stream, Z_NO_FLUSH);
338 CHECK_ERR(err, "deflate");
340 /* Switch back to compressing mode: */
341 deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
342 c_stream.next_in = uncompr;
343 c_stream.avail_in = (uInt)uncomprLen;
344 err = deflate(&c_stream, Z_NO_FLUSH);
345 CHECK_ERR(err, "deflate");
347 err = deflate(&c_stream, Z_FINISH);
348 if (err != Z_STREAM_END) {
349 fprintf(stderr, "deflate should report Z_STREAM_END\n");
352 err = deflateEnd(&c_stream);
353 CHECK_ERR(err, "deflateEnd");
356 /* ===========================================================================
357 * Test inflate() with large buffers
359 void test_large_inflate(compr, comprLen, uncompr, uncomprLen)
360 Byte *compr, *uncompr;
361 uLong comprLen, uncomprLen;
364 z_stream d_stream; /* decompression stream */
366 strcpy((char*)uncompr, "garbage");
368 d_stream.zalloc = zalloc;
369 d_stream.zfree = zfree;
370 d_stream.opaque = (voidpf)0;
372 d_stream.next_in = compr;
373 d_stream.avail_in = (uInt)comprLen;
375 err = inflateInit(&d_stream);
376 CHECK_ERR(err, "inflateInit");
379 d_stream.next_out = uncompr; /* discard the output */
380 d_stream.avail_out = (uInt)uncomprLen;
381 err = inflate(&d_stream, Z_NO_FLUSH);
382 if (err == Z_STREAM_END) break;
383 CHECK_ERR(err, "large inflate");
386 err = inflateEnd(&d_stream);
387 CHECK_ERR(err, "inflateEnd");
389 if (d_stream.total_out != 2*uncomprLen + comprLen/2) {
390 fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out);
393 printf("large_inflate(): OK\n");
397 /* ===========================================================================
398 * Test deflate() with full flush
400 void test_flush(compr, comprLen)
404 z_stream c_stream; /* compression stream */
406 uInt len = (uInt)strlen(hello)+1;
408 c_stream.zalloc = zalloc;
409 c_stream.zfree = zfree;
410 c_stream.opaque = (voidpf)0;
412 err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
413 CHECK_ERR(err, "deflateInit");
415 c_stream.next_in = (z_const unsigned char *)hello;
416 c_stream.next_out = compr;
417 c_stream.avail_in = 3;
418 c_stream.avail_out = (uInt)*comprLen;
419 err = deflate(&c_stream, Z_FULL_FLUSH);
420 CHECK_ERR(err, "deflate");
422 compr[3]++; /* force an error in first compressed block */
423 c_stream.avail_in = len - 3;
425 err = deflate(&c_stream, Z_FINISH);
426 if (err != Z_STREAM_END) {
427 CHECK_ERR(err, "deflate");
429 err = deflateEnd(&c_stream);
430 CHECK_ERR(err, "deflateEnd");
432 *comprLen = c_stream.total_out;
435 /* ===========================================================================
438 void test_sync(compr, comprLen, uncompr, uncomprLen)
439 Byte *compr, *uncompr;
440 uLong comprLen, uncomprLen;
443 z_stream d_stream; /* decompression stream */
445 strcpy((char*)uncompr, "garbage");
447 d_stream.zalloc = zalloc;
448 d_stream.zfree = zfree;
449 d_stream.opaque = (voidpf)0;
451 d_stream.next_in = compr;
452 d_stream.avail_in = 2; /* just read the zlib header */
454 err = inflateInit(&d_stream);
455 CHECK_ERR(err, "inflateInit");
457 d_stream.next_out = uncompr;
458 d_stream.avail_out = (uInt)uncomprLen;
460 inflate(&d_stream, Z_NO_FLUSH);
461 CHECK_ERR(err, "inflate");
463 d_stream.avail_in = (uInt)comprLen-2; /* read all compressed data */
464 err = inflateSync(&d_stream); /* but skip the damaged part */
465 CHECK_ERR(err, "inflateSync");
467 err = inflate(&d_stream, Z_FINISH);
468 if (err != Z_DATA_ERROR) {
469 fprintf(stderr, "inflate should report DATA_ERROR\n");
470 /* Because of incorrect adler32 */
473 err = inflateEnd(&d_stream);
474 CHECK_ERR(err, "inflateEnd");
476 printf("after inflateSync(): hel%s\n", (char *)uncompr);
479 /* ===========================================================================
480 * Test deflate() with preset dictionary
482 void test_dict_deflate(compr, comprLen)
486 z_stream c_stream; /* compression stream */
489 c_stream.zalloc = zalloc;
490 c_stream.zfree = zfree;
491 c_stream.opaque = (voidpf)0;
493 err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
494 CHECK_ERR(err, "deflateInit");
496 err = deflateSetDictionary(&c_stream,
497 (const Bytef*)dictionary, (int)sizeof(dictionary));
498 CHECK_ERR(err, "deflateSetDictionary");
500 dictId = c_stream.adler;
501 c_stream.next_out = compr;
502 c_stream.avail_out = (uInt)comprLen;
504 c_stream.next_in = (z_const unsigned char *)hello;
505 c_stream.avail_in = (uInt)strlen(hello)+1;
507 err = deflate(&c_stream, Z_FINISH);
508 if (err != Z_STREAM_END) {
509 fprintf(stderr, "deflate should report Z_STREAM_END\n");
512 err = deflateEnd(&c_stream);
513 CHECK_ERR(err, "deflateEnd");
516 /* ===========================================================================
517 * Test inflate() with a preset dictionary
519 void test_dict_inflate(compr, comprLen, uncompr, uncomprLen)
520 Byte *compr, *uncompr;
521 uLong comprLen, uncomprLen;
524 z_stream d_stream; /* decompression stream */
526 strcpy((char*)uncompr, "garbage");
528 d_stream.zalloc = zalloc;
529 d_stream.zfree = zfree;
530 d_stream.opaque = (voidpf)0;
532 d_stream.next_in = compr;
533 d_stream.avail_in = (uInt)comprLen;
535 err = inflateInit(&d_stream);
536 CHECK_ERR(err, "inflateInit");
538 d_stream.next_out = uncompr;
539 d_stream.avail_out = (uInt)uncomprLen;
542 err = inflate(&d_stream, Z_NO_FLUSH);
543 if (err == Z_STREAM_END) break;
544 if (err == Z_NEED_DICT) {
545 if (d_stream.adler != dictId) {
546 fprintf(stderr, "unexpected dictionary");
549 err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,
550 (int)sizeof(dictionary));
552 CHECK_ERR(err, "inflate with dict");
555 err = inflateEnd(&d_stream);
556 CHECK_ERR(err, "inflateEnd");
558 if (strcmp((char*)uncompr, hello)) {
559 fprintf(stderr, "bad inflate with dict\n");
562 printf("inflate with dictionary: %s\n", (char *)uncompr);
566 /* ===========================================================================
567 * Usage: example [output.gz [input.gz]]
574 Byte *compr, *uncompr;
575 uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */
576 uLong uncomprLen = comprLen;
577 static const char* myVersion = ZLIB_VERSION;
579 if (zlibVersion()[0] != myVersion[0]) {
580 fprintf(stderr, "incompatible zlib version\n");
583 } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {
584 fprintf(stderr, "warning: different zlib version\n");
587 printf("zlib version %s = 0x%04x, compile flags = 0x%lx\n",
588 ZLIB_VERSION, ZLIB_VERNUM, zlibCompileFlags());
589 if (ZWRAP_isUsingZSTDcompression()) printf("zstd version %s\n", zstdVersion());
591 compr = (Byte*)calloc((uInt)comprLen, 1);
592 uncompr = (Byte*)calloc((uInt)uncomprLen, 1);
593 /* compr and uncompr are cleared to avoid reading uninitialized
594 * data and to ensure that uncompr compresses well.
596 if (compr == Z_NULL || uncompr == Z_NULL) {
597 printf("out of memory\n");
602 argc = strlen(argv[0]);
604 test_compress(compr, comprLen, uncompr, uncomprLen);
606 test_gzio((argc > 1 ? argv[1] : TESTFILE),
607 uncompr, uncomprLen);
610 test_deflate(compr, comprLen);
611 test_inflate(compr, comprLen, uncompr, uncomprLen);
613 test_large_deflate(compr, comprLen, uncompr, uncomprLen);
614 test_large_inflate(compr, comprLen, uncompr, uncomprLen);
616 if (!ZWRAP_isUsingZSTDcompression()) {
617 test_flush(compr, &comprLen);
618 test_sync(compr, comprLen, uncompr, uncomprLen);
620 comprLen = uncomprLen;
622 test_dict_deflate(compr, comprLen);
623 test_dict_inflate(compr, comprLen, uncompr, uncomprLen);