summaryrefslogtreecommitdiff
path: root/test/example.c
diff options
context:
space:
mode:
authorIlya Leoshkevich <iii@linux.ibm.com>2019-05-23 13:57:43 +0200
committerHans Kristian Rosbach <hk-github@circlestorm.org>2019-07-18 13:19:09 +0200
commitb927c6ab8774424ae006c185d19dfd327c0f3d6e (patch)
treea3ac8bbc40099631f380f744f31a778df7ccadc4 /test/example.c
parent2f56596da84535f0c0967090d6cda7946a47388f (diff)
downloadProject-Tick-b927c6ab8774424ae006c185d19dfd327c0f3d6e.tar.gz
Project-Tick-b927c6ab8774424ae006c185d19dfd327c0f3d6e.zip
Add two new public zng_deflate{Set,Get}Params() functions
These functions allow zlib-ng callers to modify and query the compression parameters in a future-proof way. When the caller requests a parameter, which is not supported by the current zlib-ng version, this situation is detected and reported to the caller. The caller may modify or query multiple parameters at once. Currently only "level" and "strategy" parameters are supported. It is planned to add a "reproducible" parameter, which would affect whether IBM Z DEFLATE CONVERSION CALL is used. Passing enum and void * buffer was chosen over passing strings, because of simplicity for the caller. If strings were used, C callers would have to call snprintf() and strtoul() for setting and getting integer-valued parameters respectively, which is quite tedious. Bulk updates were chosen over updating individual parameters separately, because it might make sense to apply some parameters atomically, e.g. level and strategy. The new functions are defined only for zlib-ng, but not compat zlib.
Diffstat (limited to 'test/example.c')
-rw-r--r--test/example.c65
1 files changed, 60 insertions, 5 deletions
diff --git a/test/example.c b/test/example.c
index 964a380d76..f826b1ab84 100644
--- a/test/example.c
+++ b/test/example.c
@@ -40,7 +40,7 @@ void test_compress (unsigned char *compr, z_size_t comprLen,unsigned char *
void test_gzio (const char *fname, unsigned char *uncompr, z_size_t uncomprLen);
void test_deflate (unsigned char *compr, size_t comprLen);
void test_inflate (unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen);
-void test_large_deflate (unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen);
+void test_large_deflate (unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen, int zng_params);
void test_large_inflate (unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen);
void test_flush (unsigned char *compr, z_size_t *comprLen);
void test_sync (unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen);
@@ -241,10 +241,18 @@ static unsigned int diff;
/* ===========================================================================
* Test deflate() with large buffers and dynamic change of compression level
*/
-void test_large_deflate(unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen)
+void test_large_deflate(unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen, int zng_params)
{
PREFIX3(stream) c_stream; /* compression stream */
int err;
+#ifndef ZLIB_COMPAT
+ int level = -1;
+ int strategy = -1;
+ zng_deflate_param_value params[] = {
+ { .param = Z_DEFLATE_LEVEL, .buf = &level, .size = sizeof(level) },
+ { .param = Z_DEFLATE_STRATEGY, .buf = &strategy, .size = sizeof(strategy) },
+ };
+#endif
c_stream.zalloc = zalloc;
c_stream.zfree = zfree;
@@ -269,7 +277,27 @@ void test_large_deflate(unsigned char *compr, size_t comprLen, unsigned char *un
}
/* Feed in already compressed data and switch to no compression: */
- PREFIX(deflateParams)(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
+ if (zng_params) {
+#ifndef ZLIB_COMPAT
+ zng_deflateGetParams(&c_stream, params, sizeof(params) / sizeof(params[0]));
+ if (level != Z_BEST_SPEED) {
+ fprintf(stderr, "Expected compression level Z_BEST_SPEED, got %d\n", level);
+ exit(1);
+ }
+ if (strategy != Z_DEFAULT_STRATEGY) {
+ fprintf(stderr, "Expected compression strategy Z_DEFAULT_STRATEGY, got %d\n", strategy);
+ exit(1);
+ }
+ level = Z_NO_COMPRESSION;
+ strategy = Z_DEFAULT_STRATEGY;
+ zng_deflateSetParams(&c_stream, params, sizeof(params) / sizeof(params[0]));
+#else
+ fprintf(stderr, "test_large_deflate() called with zng_params=1 in compat mode\n");
+ exit(1);
+#endif
+ } else {
+ PREFIX(deflateParams)(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
+ }
c_stream.next_in = compr;
diff = (unsigned int)(c_stream.next_out - compr);
c_stream.avail_in = diff;
@@ -277,7 +305,29 @@ void test_large_deflate(unsigned char *compr, size_t comprLen, unsigned char *un
CHECK_ERR(err, "deflate");
/* Switch back to compressing mode: */
- PREFIX(deflateParams)(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
+ if (zng_params) {
+#ifndef ZLIB_COMPAT
+ level = -1;
+ strategy = -1;
+ zng_deflateGetParams(&c_stream, params, sizeof(params) / sizeof(params[0]));
+ if (level != Z_NO_COMPRESSION) {
+ fprintf(stderr, "Expected compression level Z_NO_COMPRESSION, got %d\n", level);
+ exit(1);
+ }
+ if (strategy != Z_DEFAULT_STRATEGY) {
+ fprintf(stderr, "Expected compression strategy Z_DEFAULT_STRATEGY, got %d\n", strategy);
+ exit(1);
+ }
+ level = Z_BEST_COMPRESSION;
+ strategy = Z_FILTERED;
+ zng_deflateSetParams(&c_stream, params, sizeof(params) / sizeof(params[0]));
+#else
+ fprintf(stderr, "test_large_deflate() called with zng_params=1 in compat mode\n");
+ exit(1);
+#endif
+ } else {
+ PREFIX(deflateParams)(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
+ }
c_stream.next_in = uncompr;
c_stream.avail_in = (unsigned int)uncomprLen;
err = PREFIX(deflate)(&c_stream, Z_NO_FLUSH);
@@ -535,8 +585,13 @@ int main(int argc, char *argv[])
test_deflate(compr, comprLen);
test_inflate(compr, comprLen, uncompr, uncomprLen);
- test_large_deflate(compr, comprLen, uncompr, uncomprLen);
+ test_large_deflate(compr, comprLen, uncompr, uncomprLen, 0);
+ test_large_inflate(compr, comprLen, uncompr, uncomprLen);
+
+#ifndef ZLIB_COMPAT
+ test_large_deflate(compr, comprLen, uncompr, uncomprLen, 1);
test_large_inflate(compr, comprLen, uncompr, uncomprLen);
+#endif
test_flush(compr, &comprLen);
test_sync(compr, comprLen, uncompr, uncomprLen);