Skip to content

Instantly share code, notes, and snippets.

@pmqs
Created August 6, 2017 19:32
Show Gist options
  • Save pmqs/190945c2b2fab9c3993cabeff6cab3d3 to your computer and use it in GitHub Desktop.
Save pmqs/190945c2b2fab9c3993cabeff6cab3d3 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "zlib.h"
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
# include <fcntl.h>
# include <io.h>
# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
#else
# define SET_BINARY_MODE(file)
#endif
// #define CHUNK 100
#define CHUNK 10
void zerr(int ret) ;
char * flush_flags[] = {
"Z_NO_FLUSH",
"Z_PARTIAL_FLUSH",
"Z_SYNC_FLUSH",
"Z_FULL_FLUSH",
"Z_FINISH",
"Z_BLOCK",
"Z_TREES"
} ;
static void
DispHex(void * ptr, int length)
{
char * p = (char*)ptr;
int i;
for (i = 0; i < length; ++i) {
fprintf(stderr, " %02x", 0xFF & *(p+i));
}
fprintf(stderr, "\n") ;
}
/* Compress from file source to file dest until EOF on source.
def() returns Z_OK on success, Z_MEM_ERROR if memory could not be
allocated for processing, Z_STREAM_ERROR if an invalid compression
level is supplied, Z_VERSION_ERROR if the version of zlib.h and the
version of the library linked do not match, or Z_ERRNO if there is
an error reading or writing the files. */
int def(FILE* dest)
{
int ret, flush;
unsigned have;
z_stream strm;
unsigned char in[CHUNK];
unsigned char out[CHUNK];
int avail_in = 0;
int index = -1 ;
int strategy = Z_DEFAULT_STRATEGY;
int level = Z_DEFAULT_COMPRESSION ;
/* allocate deflate state */
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
strm.avail_out = 0;
strm.next_out = Z_NULL;
level = 0;
fprintf(stderr, "Chunk size is %d\n", CHUNK) ;
ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -15, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
if (ret != Z_OK)
return ret;
for (index = 0; index < 2 ; ++ index) {
strm.next_in = "Hello";
strm.avail_in = 5;
/* DEFLATE */
do {
strm.avail_out = CHUNK;
strm.next_out = out;
ret = deflate(&strm, Z_NO_FLUSH); /* no bad return value */
assert(ret != Z_STREAM_ERROR); /* state not clobbered */
have = CHUNK - strm.avail_out;
fprintf(stderr, "\nDEFLATE out %d, return %d\n", have, ret);
if (have) {
DispHex(out, have);
}
if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
(void)deflateEnd(&strm);
return Z_ERRNO;
}
} while (strm.avail_out == 0);
assert(strm.avail_in == 0); /* all input will be used */
/* FLUSH */
char * FLUSH ;
switch (index % 2)
{
case 0:
// fprintf(stderr, "\n%d SETTING Z_SYNC_FLUSH\n", index);
flush = Z_SYNC_FLUSH;
FLUSH = "Z_SYNC_FLUSH" ;
break;
case 1:
// fprintf(stderr, "\n%d SETTING Z_FULL_FLUSH\n", index);
flush = Z_FULL_FLUSH;
FLUSH = "Z_FULL_FLUSH" ;
break;
}
do {
strm.avail_in = 0;
strm.next_in = Z_NULL;
strm.avail_out = CHUNK;
strm.next_out = out;
ret = deflate(&strm, flush); /* no bad return value */
assert(ret != Z_STREAM_ERROR); /* state not clobbered */
have = CHUNK - strm.avail_out;
fprintf(stderr, "FLUSH [%s] out %d, return %d\n", FLUSH, have, ret);
if (have) {
DispHex(out, have);
}
if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
(void)deflateEnd(&strm);
return Z_ERRNO;
}
} while (strm.avail_out == 0);
}
fprintf(stderr, "\nRET %d\n", ret);
// zerr(ret);
// assert(ret == Z_STREAM_END); /* stream will be complete */
/* clean up and return */
// (void)deflateEnd(&strm);
return Z_OK;
}
/* report a zlib or i/o error */
void zerr(int ret)
{
fputs("zpipe: ", stderr);
switch (ret) {
case Z_ERRNO:
if (ferror(stdin))
fputs("error reading stdin\n", stderr);
if (ferror(stdout))
fputs("error writing stdout\n", stderr);
break;
case Z_BUF_ERROR:
fputs("buffer error\n", stderr);
break;
case Z_STREAM_ERROR:
fputs("invalid compression level\n", stderr);
break;
case Z_DATA_ERROR:
fputs("invalid or incomplete deflate data\n", stderr);
break;
case Z_MEM_ERROR:
fputs("out of memory\n", stderr);
break;
case Z_VERSION_ERROR:
fputs("zlib version mismatch!\n", stderr);
}
}
/* compress or decompress from stdin to stdout */
int main(int argc, char **argv)
{
int ret;
fprintf(stderr, "This is [%s][%s]\n", ZLIB_VERSION, zlibVersion());
/* avoid end-of-line conversions */
SET_BINARY_MODE(stdin);
SET_BINARY_MODE(stdout);
def(stdout) ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment