mirror of
https://github.com/wolfSSL/wolfssl.git
synced 2025-08-02 04:04:39 +02:00
filled out our Compress and DeCompress functions, updated the test case
This commit is contained in:
@@ -37,6 +37,12 @@
|
|||||||
#include <ctaocrypt/src/misc.c>
|
#include <ctaocrypt/src/misc.c>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <zlib.h>
|
||||||
|
|
||||||
|
|
||||||
|
#define DEFLATE_DEFAULT_WINDOWBITS 15
|
||||||
|
#define DEFLATE_DEFAULT_MEMLEVEL 8
|
||||||
|
|
||||||
|
|
||||||
int Compress(byte* out, word32 outSz, const byte* in, word32 inSz, word32 flags)
|
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.
|
* buffer should be srcSz + 0.1% + 12.
|
||||||
*/
|
*/
|
||||||
{
|
{
|
||||||
word32 copySz = outSz <= inSz ? outSz : inSz;
|
z_stream stream;
|
||||||
(void)flags;
|
int result = 0;
|
||||||
XMEMMOVE(out, in, copySz);
|
|
||||||
|
|
||||||
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)
|
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;
|
z_stream stream;
|
||||||
XMEMMOVE(out, in, copySz);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -281,6 +281,22 @@ void CTaoCryptErrorString(int error, char* buffer)
|
|||||||
XSTRNCPY(buffer, "Cavium Init type error", max);
|
XSTRNCPY(buffer, "Cavium Init type error", max);
|
||||||
break;
|
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:
|
default:
|
||||||
XSTRNCPY(buffer, "unknown error number", max);
|
XSTRNCPY(buffer, "unknown error number", max);
|
||||||
|
|
||||||
|
@@ -3170,9 +3170,15 @@ int compress_test(void)
|
|||||||
if (c == NULL || d == NULL)
|
if (c == NULL || d == NULL)
|
||||||
ret = -300;
|
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;
|
ret = -301;
|
||||||
|
|
||||||
|
if (ret > 0) {
|
||||||
|
cSz = (word32)ret;
|
||||||
|
ret = 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (ret == 0 && DeCompress(d, dSz, c, cSz) != (int)dSz)
|
if (ret == 0 && DeCompress(d, dSz, c, cSz) != (int)dSz)
|
||||||
ret = -302;
|
ret = -302;
|
||||||
|
|
||||||
|
@@ -103,6 +103,11 @@ enum {
|
|||||||
|
|
||||||
CAVIUM_INIT_E = -182, /* Cavium Init type error */
|
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 */
|
MIN_CODE_E = -200 /* errors -101 - -199 */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user