diff --git a/ctaocrypt/src/compress.c b/ctaocrypt/src/compress.c index 35291092b..b7c9efd68 100644 --- a/ctaocrypt/src/compress.c +++ b/ctaocrypt/src/compress.c @@ -37,6 +37,12 @@ #include #endif +#include + + +#define DEFLATE_DEFAULT_WINDOWBITS 15 +#define DEFLATE_DEFAULT_MEMLEVEL 8 + int Compress(byte* out, word32 outSz, const byte* in, word32 inSz, word32 flags) /* @@ -56,20 +62,84 @@ int Compress(byte* out, word32 outSz, const byte* in, word32 inSz, word32 flags) * buffer should be srcSz + 0.1% + 12. */ { - word32 copySz = outSz <= inSz ? outSz : inSz; - (void)flags; - XMEMMOVE(out, in, copySz); + z_stream stream; + int result = 0; - return (int)copySz; + stream.next_in = (Bytef*)in; + stream.avail_in = (uInt)inSz; +#ifdef MAXSEG_64K + /* Check for source > 64K on 16-bit machine: */ + if ((uLong)stream.avail_in != inSz) return COMPRESS_INIT_E; +#endif + stream.next_out = out; + 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.opaque = (voidpf)0; + + if (deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, + DEFLATE_DEFAULT_WINDOWBITS, DEFLATE_DEFAULT_MEMLEVEL, + flags ? Z_FIXED : Z_DEFAULT_STRATEGY) != Z_OK) + return COMPRESS_INIT_E; + + if (deflate(&stream, Z_FINISH) != Z_STREAM_END) { + deflateEnd(&stream); + return COMPRESS_E; + } + + result = (int)stream.total_out; + + if (deflateEnd(&stream) != Z_OK) + result = COMPRESS_E; + + return result; } int DeCompress(byte* out, word32 outSz, const byte* in, word32 inSz) +/* + * out - pointer to destination buffer + * outSz - size of destination buffer + * in - pointer to source buffer to compress + * inSz - size of source to compress + * flags - flags to control how compress operates + * + * return: + * negative - error code + * positive - bytes stored in out buffer + */ { - word32 copySz = outSz <= inSz ? outSz : inSz; - XMEMMOVE(out, in, copySz); + z_stream stream; + int result = 0; - return (int)copySz; + stream.next_in = (Bytef*)in; + stream.avail_in = (uInt)inSz; + /* Check for source > 64K on 16-bit machine: */ + if ((uLong)stream.avail_in != inSz) return DECOMPRESS_INIT_E; + + stream.next_out = out; + 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; + + if (inflateInit(&stream) != Z_OK) + return DECOMPRESS_INIT_E; + + if (inflate(&stream, Z_FINISH) != Z_STREAM_END) { + inflateEnd(&stream); + return DECOMPRESS_E; + } + + result = (int)stream.total_out; + + if (inflateEnd(&stream) != Z_OK) + result = DECOMPRESS_E; + + return result; } diff --git a/ctaocrypt/src/error.c b/ctaocrypt/src/error.c index b98db04ca..b1041659e 100644 --- a/ctaocrypt/src/error.c +++ b/ctaocrypt/src/error.c @@ -281,6 +281,22 @@ void CTaoCryptErrorString(int error, char* buffer) XSTRNCPY(buffer, "Cavium Init type error", max); break; + case COMPRESS_INIT_E: + XSTRNCPY(buffer, "Compress Init error", max); + break; + + case COMPRESS_E: + XSTRNCPY(buffer, "Compress error", max); + break; + + case DECOMPRESS_INIT_E: + XSTRNCPY(buffer, "DeCompress Init error", max); + break; + + case DECOMPRESS_E: + XSTRNCPY(buffer, "DeCompress error", max); + break; + default: XSTRNCPY(buffer, "unknown error number", max); diff --git a/ctaocrypt/test/test.c b/ctaocrypt/test/test.c index 411b5435c..628827247 100644 --- a/ctaocrypt/test/test.c +++ b/ctaocrypt/test/test.c @@ -3170,9 +3170,15 @@ int compress_test(void) if (c == NULL || d == NULL) ret = -300; - if (ret == 0 && 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) { + cSz = (word32)ret; + ret = 0; + } + if (ret == 0 && DeCompress(d, dSz, c, cSz) != (int)dSz) ret = -302; diff --git a/cyassl/ctaocrypt/error.h b/cyassl/ctaocrypt/error.h index c7b24eb83..265019822 100644 --- a/cyassl/ctaocrypt/error.h +++ b/cyassl/ctaocrypt/error.h @@ -103,6 +103,11 @@ enum { CAVIUM_INIT_E = -182, /* Cavium Init type error */ + COMPRESS_INIT_E = -183, /* Compress init error */ + COMPRESS_E = -184, /* Compress error */ + DECOMPRESS_INIT_E = -185, /* DeCompress init error */ + DECOMPRESS_E = -186, /* DeCompress error */ + MIN_CODE_E = -200 /* errors -101 - -199 */ };