/* Minimal traced Zlib inflate example. You'll need to build zlib with DEBUG defined. Trace messages are printed to stderr, as are genuine errors. Inflated data is printed to stdout. This example uses a single shot call to inflate, rather than the usual block-by-block operation. You'll get an error if the compressed data inflates to more than a megabyte. */ #include "zlib.h" #include "zutil.h" #include #include /* Check a condition holds, printing an error message to the supplied stream and exiting the program on failure. */ void check(int condition, FILE * err, char const * where) { if (!condition) { fprintf(err, "Error: %s\n", where); exit(-1); } } int main() { FILE * out = stdout; FILE * err = stderr; /* Compressed chessboard pixel data. */ unsigned char comp[] = { 0x68,0x81,0xed,0xce,0xb1,0x0d,0x00,0x20,0x0c,0x03,0x41,0xf6,0x5f,0x1a,0x58, 0x80,0x3a,0x2f,0x74,0x6e,0x52,0xe4,0x24,0x7b,0xed,0x9b,0x75,0xf3,0xba,0xcf, 0x07,0x00,0x00,0xdf,0x83,0xca,0x0e,0x00,0x00,0x7a,0x60,0xba,0x1f,0x00,0x80, 0x2e,0xa8,0xec,0x00,0x00,0xa0,0x07,0xa6,0xfb,0x01,0x00,0xe8,0x82,0xca,0x0e, 0x00,0x00,0x7a,0x60,0xba,0x1f,0x00,0x80,0x2e,0xa8,0xec,0x00,0x00,0x20,0x07, 0x0e,0x8a,0x69,0xf0,0xe2, }; size_t const ncomp = sizeof(comp); unsigned char decomp[1024 * 1024]; size_t const ndecomp = sizeof(decomp); z_stream zs = { comp, ncomp, 0, decomp, ndecomp, 0 }; z_verbose = 2; check(inflateInit(&zs) == Z_OK, err, "inflateInit"); check(inflate(&zs, Z_FINISH) == Z_STREAM_END, err, "inflate"); check(inflateEnd(&zs) == Z_OK, err, "inflateEnd"); check(zs.total_in == ncomp, err, "end of data"); fwrite(decomp, 1, zs.total_out, out); return EXIT_SUCCESS; }