From 02581a3da251f848bab1c593639aa633073fe3db Mon Sep 17 00:00:00 2001 From: John Safranek Date: Wed, 20 Mar 2013 11:28:45 -0700 Subject: [PATCH] added control of compress memory usage via build setting --- ctaocrypt/src/compress.c | 35 ++++++++++++++++++++++++++++------- ctaocrypt/test/test.c | 3 +-- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/ctaocrypt/src/compress.c b/ctaocrypt/src/compress.c index b7c9efd68..6e6e05d08 100644 --- a/ctaocrypt/src/compress.c +++ b/ctaocrypt/src/compress.c @@ -40,8 +40,28 @@ #include -#define DEFLATE_DEFAULT_WINDOWBITS 15 -#define DEFLATE_DEFAULT_MEMLEVEL 8 +/* alloc user allocs to work with zlib */ +static void* myAlloc(void* opaque, unsigned int item, unsigned int size) +{ + (void)opaque; + return XMALLOC(item * size, opaque, DYNAMIC_TYPE_LIBZ); +} + + +static void myFree(void* opaque, void* memory) +{ + (void)opaque; + XFREE(memory, opaque, DYNAMIC_TYPE_LIBZ); +} + + +#ifdef HAVE_MCAPI + #define DEFLATE_DEFAULT_WINDOWBITS 11 + #define DEFLATE_DEFAULT_MEMLEVEL 1 +#else + #define DEFLATE_DEFAULT_WINDOWBITS 15 + #define DEFLATE_DEFAULT_MEMLEVEL 8 +#endif int Compress(byte* out, word32 outSz, const byte* in, word32 inSz, word32 flags) @@ -75,8 +95,8 @@ int Compress(byte* out, word32 outSz, const byte* in, word32 inSz, word32 flags) stream.avail_out = (uInt)outSz; if ((uLong)stream.avail_out != outSz) return COMPRESS_INIT_E; - stream.zalloc = (alloc_func)0; - stream.zfree = (free_func)0; + stream.zalloc = (alloc_func)myAlloc; + stream.zfree = (free_func)myFree; stream.opaque = (voidpf)0; if (deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, @@ -123,10 +143,11 @@ int DeCompress(byte* out, word32 outSz, const byte* in, word32 inSz) stream.avail_out = (uInt)outSz; if ((uLong)stream.avail_out != outSz) return DECOMPRESS_INIT_E; - stream.zalloc = (alloc_func)0; - stream.zfree = (free_func)0; + stream.zalloc = (alloc_func)myAlloc; + stream.zfree = (free_func)myFree; + stream.opaque = (voidpf)0; - if (inflateInit(&stream) != Z_OK) + if (inflateInit2(&stream, DEFLATE_DEFAULT_WINDOWBITS) != Z_OK) return DECOMPRESS_INIT_E; if (inflate(&stream, Z_FINISH) != Z_STREAM_END) { diff --git a/ctaocrypt/test/test.c b/ctaocrypt/test/test.c index 628827247..5f7eba869 100644 --- a/ctaocrypt/test/test.c +++ b/ctaocrypt/test/test.c @@ -3170,8 +3170,7 @@ int compress_test(void) if (c == NULL || d == NULL) ret = -300; - if (ret == 0 && - (ret = Compress(c, cSz, sample_text, dSz, 0)) < 0) + if (ret == 0 && (ret = Compress(c, cSz, sample_text, dSz, 0)) < 0) ret = -301; if (ret > 0) {