2010-10-09 11:24:38 +00:00
|
|
|
/* The file is based of zpipe.c + minor rename and minor adaptation
|
|
|
|
*
|
|
|
|
* zpipe.c: example of proper use of zlib's inflate() and deflate()
|
|
|
|
* Not copyrighted -- provided to the public domain
|
|
|
|
* Version 1.4 11 December 2005 Mark Adler
|
2014-04-13 08:41:24 +00:00
|
|
|
*
|
|
|
|
* This file is in the public domain
|
2010-10-09 11:24:38 +00:00
|
|
|
*/
|
2010-03-19 00:31:15 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
//#define ZLIB_WINAPI
|
2010-06-11 13:51:43 +00:00
|
|
|
#include "zlib.h"
|
2010-03-19 00:31:15 +00:00
|
|
|
|
|
|
|
int def(char *src, char *dst, int bytes_to_compress, int *bytes_after_compressed)
|
|
|
|
{
|
|
|
|
z_stream strm;
|
|
|
|
|
2010-10-09 11:24:38 +00:00
|
|
|
int ret;
|
2010-03-19 00:31:15 +00:00
|
|
|
unsigned have;
|
|
|
|
|
|
|
|
/* allocate deflate state */
|
|
|
|
strm.zalloc = Z_NULL;
|
|
|
|
strm.zfree = Z_NULL;
|
|
|
|
strm.opaque = Z_NULL;
|
|
|
|
ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION) ;
|
2010-05-01 20:33:53 +00:00
|
|
|
|
2010-03-19 00:31:15 +00:00
|
|
|
if (ret != Z_OK)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* compress */
|
|
|
|
strm.avail_in = bytes_to_compress ;
|
|
|
|
strm.avail_out = bytes_to_compress ;
|
2010-05-01 20:33:53 +00:00
|
|
|
|
2010-03-19 00:31:15 +00:00
|
|
|
strm.next_in = (Bytef *)src ;
|
|
|
|
strm.next_out = (Bytef *)dst ;
|
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
ret = deflate(&strm, Z_FINISH) ;
|
2010-05-01 20:33:53 +00:00
|
|
|
|
2010-03-19 00:31:15 +00:00
|
|
|
have = bytes_to_compress - strm.avail_out ;
|
2010-05-01 20:33:53 +00:00
|
|
|
|
2010-03-19 00:31:15 +00:00
|
|
|
*bytes_after_compressed = have ;
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2010-03-19 00:31:15 +00:00
|
|
|
assert(ret == Z_STREAM_END); /* stream will be complete */
|
|
|
|
|
|
|
|
/* clean up and return */
|
|
|
|
(void)deflateEnd(&strm);
|
2010-05-01 20:33:53 +00:00
|
|
|
|
2010-03-19 00:31:15 +00:00
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
int inf(char *src, char *dst, int bytes_to_decompress, int maximum_after_decompress, int* outbytes)
|
2010-03-19 00:31:15 +00:00
|
|
|
{
|
|
|
|
z_stream strm;
|
|
|
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* allocate inflate state */
|
|
|
|
strm.zalloc = Z_NULL;
|
|
|
|
strm.zfree = Z_NULL;
|
|
|
|
strm.opaque = Z_NULL;
|
|
|
|
strm.avail_in = 0;
|
|
|
|
strm.next_in = Z_NULL;
|
|
|
|
ret = inflateInit(&strm);
|
2010-05-01 20:33:53 +00:00
|
|
|
|
|
|
|
if (ret != Z_OK) return ret;
|
2010-03-19 00:31:15 +00:00
|
|
|
|
|
|
|
/* decompress */
|
|
|
|
strm.avail_in = bytes_to_decompress ;
|
|
|
|
strm.next_in = (Bytef *)src ;
|
|
|
|
strm.next_out = (Bytef *)dst ;
|
|
|
|
strm.avail_out = maximum_after_decompress ;
|
|
|
|
|
|
|
|
ret = inflate(&strm, Z_NO_FLUSH) ;
|
2010-05-01 20:33:53 +00:00
|
|
|
|
2010-03-19 00:31:15 +00:00
|
|
|
assert(ret != Z_STREAM_ERROR); /* state not clobbered */
|
2010-05-01 20:33:53 +00:00
|
|
|
|
|
|
|
switch (ret)
|
|
|
|
{
|
|
|
|
|
|
|
|
case Z_NEED_DICT:
|
|
|
|
ret = Z_DATA_ERROR; /* and fall through */
|
|
|
|
|
|
|
|
case Z_DATA_ERROR:
|
|
|
|
|
|
|
|
case Z_MEM_ERROR:
|
|
|
|
(void)inflateEnd(&strm);
|
|
|
|
return ret;
|
2010-03-19 00:31:15 +00:00
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2010-03-19 00:31:15 +00:00
|
|
|
assert(strm.avail_in == 0); /* all input will be used */
|
|
|
|
|
2010-05-01 20:33:53 +00:00
|
|
|
if (outbytes != NULL) *outbytes = strm.total_out;
|
2010-03-19 00:31:15 +00:00
|
|
|
|
|
|
|
/* clean up and return */
|
|
|
|
(void)inflateEnd(&strm);
|
2010-05-01 20:33:53 +00:00
|
|
|
|
2010-03-19 00:31:15 +00:00
|
|
|
return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* report a zlib or i/o error */
|
|
|
|
void zerr(int ret)
|
|
|
|
{
|
|
|
|
fputs("zpipe: ", stderr);
|
2010-05-01 20:33:53 +00:00
|
|
|
|
|
|
|
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_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);
|
2010-03-19 00:31:15 +00:00
|
|
|
}
|
|
|
|
}
|