os/ossrv/compressionlibs/ziplib/test/rtest/example/example.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /* Portions Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     2  * All rights reserved.
     3  */
     4 
     5 /* example.c -- usage example of the zlib compression library
     6  * Copyright (C) 1995-1998 Jean-loup Gailly.
     7  * For conditions of distribution and use, see copyright notice in zlib.h 
     8  */
     9 
    10 /* @(#) $Id$ */
    11 
    12 #include <stdio.h>
    13 #include <ezlib.h>
    14 
    15 
    16 #ifdef STDC
    17 #  include <string.h>
    18 #  include <stdlib.h>
    19 #else
    20    extern void exit  OF((int));
    21 #endif
    22 
    23 #if defined(VMS) || defined(RISCOS)
    24 #  define TESTFILE "foo-gz"
    25 #else
    26 #  define TESTFILE "foo.gz"
    27 #endif
    28 
    29 #define CHECK_ERR(err, msg) { \
    30     if (err != Z_OK) { \
    31         fprintf(stderr, "%s error: %d\n", msg, err); \
    32         exit(1); \
    33     } \
    34 }
    35 
    36 const char hello[] = "hello, hello!";
    37 /* "hello world" would be more standard, but the repeated "hello"
    38  * stresses the compression code better, sorry...
    39  */
    40 
    41 const char dictionary[] = "hello";
    42 uLong dictId; /* Adler32 value of the dictionary */
    43 
    44 void test_compress      OF((Byte *compr, uLong comprLen,
    45 		            Byte *uncompr, uLong uncomprLen)); 
    46 void test_deflate       OF((Byte *compr, uLong comprLen));
    47 void test_inflate       OF((Byte *compr, uLong comprLen,
    48 		            Byte *uncompr, uLong uncomprLen));
    49 void test_large_deflate OF((Byte *compr, uLong comprLen,
    50 		            Byte *uncompr, uLong uncomprLen));
    51 void test_large_inflate OF((Byte *compr, uLong comprLen,
    52 		            Byte *uncompr, uLong uncomprLen));
    53 void test_flush         OF((Byte *compr, uLong *comprLen));
    54 void test_sync          OF((Byte *compr, uLong comprLen,
    55 		            Byte *uncompr, uLong uncomprLen));
    56 void test_dict_deflate  OF((Byte *compr, uLong comprLen));
    57 void test_dict_inflate  OF((Byte *compr, uLong comprLen,
    58 		            Byte *uncompr, uLong uncomprLen));
    59 int  main               OF((/*int argc, char *argv[]*/));
    60 
    61 /**
    62 Test compress() and uncompress()
    63 
    64 @SYMTestCaseID          SYSLIB-EZLIB-CT-0820
    65 @SYMTestCaseDesc	    Compression and decompression of a buffer test.
    66 @SYMTestPriority 	    High
    67 @SYMTestActions  	    Compress and uncompress string "hello" and check for integrity of the operation done.
    68 @SYMTestExpectedResults The test must not fail.
    69 @SYMREQ                 REQ0000
    70 */
    71 
    72 void test_compress(
    73     Byte *compr, uLong comprLen, Byte *uncompr,
    74      uLong uncomprLen)
    75 {
    76     int err;
    77     uLong len = strlen(hello)+1;
    78 
    79     err = compress(compr, &comprLen, (const Bytef*)hello, len);
    80     CHECK_ERR(err, "compress");
    81 
    82     strcpy((char*)uncompr, "garbage");
    83 
    84     err = uncompress(uncompr, &uncomprLen, compr, comprLen);
    85     CHECK_ERR(err, "uncompress");
    86 
    87     if (strcmp((char*)uncompr, hello)) {
    88         fprintf(stderr, "bad uncompress\n");
    89 	exit(1);
    90     } else {
    91         printf("uncompress(): %s\n", (char *)uncompr);
    92     }
    93 }
    94 
    95 /**
    96 @SYMTestCaseID          SYSLIB-EZLIB-CT-0821
    97 @SYMTestCaseDesc	    Read and write of .gz files test
    98 @SYMTestPriority 	    High
    99 @SYMTestActions  	    Tests deflate() with small buffers
   100 @SYMTestExpectedResults The test must not fail.
   101 @SYMREQ                 REQ0000
   102 */
   103 
   104 void test_deflate(
   105     Byte *compr,
   106     uLong comprLen)
   107 {
   108     z_stream c_stream; /* compression stream */
   109     int err;
   110     int len = strlen(hello)+1;
   111 
   112     c_stream.zalloc = (alloc_func)0;
   113     c_stream.zfree = (free_func)0;
   114     c_stream.opaque = (voidpf)0;
   115 
   116     err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
   117     CHECK_ERR(err, "deflateInit");
   118 
   119     c_stream.next_in  = (Bytef*)hello;
   120     c_stream.next_out = compr;
   121 
   122     while (c_stream.total_in != (uLong)len && c_stream.total_out < comprLen) {
   123         c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */
   124         err = deflate(&c_stream, Z_NO_FLUSH);
   125         CHECK_ERR(err, "deflate");
   126     }
   127     /* Finish the stream, still forcing small buffers: */
   128     for (;;) {
   129         c_stream.avail_out = 1;
   130         err = deflate(&c_stream, Z_FINISH);
   131         if (err == Z_STREAM_END) break;
   132         CHECK_ERR(err, "deflate");
   133     }
   134 
   135     err = deflateEnd(&c_stream);
   136     CHECK_ERR(err, "deflateEnd");
   137 }
   138 
   139 /**
   140 @SYMTestCaseID          SYSLIB-EZLIB-CT-0822
   141 @SYMTestCaseDesc	    Read and write of .gz files test
   142 @SYMTestPriority 	    High
   143 @SYMTestActions  	    Tests inflate() with small buffers
   144 @SYMTestExpectedResults The test must not fail.
   145 @SYMREQ                 REQ0000
   146 */
   147 
   148 void test_inflate(
   149     Byte *compr, uLong comprLen, Byte *uncompr,
   150      uLong uncomprLen)
   151 {
   152     int err;
   153     z_stream d_stream; /* decompression stream */
   154 
   155     strcpy((char*)uncompr, "garbage");
   156 
   157     d_stream.zalloc = (alloc_func)0;
   158     d_stream.zfree = (free_func)0;
   159     d_stream.opaque = (voidpf)0;
   160 
   161     d_stream.next_in  = compr;
   162     d_stream.avail_in = 0;
   163     d_stream.next_out = uncompr;
   164 
   165     err = inflateInit(&d_stream);
   166     CHECK_ERR(err, "inflateInit");
   167 
   168     while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) {
   169         d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */
   170         err = inflate(&d_stream, Z_NO_FLUSH);
   171         if (err == Z_STREAM_END) break;
   172         CHECK_ERR(err, "inflate");
   173     }
   174 
   175     err = inflateEnd(&d_stream);
   176     CHECK_ERR(err, "inflateEnd");
   177 
   178     if (strcmp((char*)uncompr, hello)) {
   179         fprintf(stderr, "bad inflate\n");
   180 	exit(1);
   181     } else {
   182         printf("inflate(): %s\n", (char *)uncompr);
   183     }
   184 }
   185 
   186 /**
   187 @SYMTestCaseID          SYSLIB-EZLIB-CT-0823
   188 @SYMTestCaseDesc	    Deflate with large buffers test
   189 @SYMTestPriority 	    High
   190 @SYMTestActions  	    Test deflate() with large buffers and dynamic change of compression level
   191 @SYMTestExpectedResults The test must not fail.
   192 @SYMREQ                 REQ0000
   193 */
   194 
   195 void test_large_deflate(
   196     Byte *compr, uLong comprLen, Byte *uncompr,
   197      uLong uncomprLen)
   198 {
   199     z_stream c_stream; /* compression stream */
   200     int err;
   201 
   202     c_stream.zalloc = (alloc_func)0;
   203     c_stream.zfree = (free_func)0;
   204     c_stream.opaque = (voidpf)0;
   205 
   206     err = deflateInit(&c_stream, Z_BEST_SPEED);
   207     CHECK_ERR(err, "deflateInit");
   208 
   209     c_stream.next_out = compr;
   210     c_stream.avail_out = (uInt)comprLen;
   211 
   212     /* At this point, uncompr is still mostly zeroes, so it should compress
   213      * very well:
   214      */
   215     c_stream.next_in = uncompr;
   216     c_stream.avail_in = (uInt)uncomprLen;
   217     err = deflate(&c_stream, Z_NO_FLUSH);
   218     CHECK_ERR(err, "deflate");
   219     if (c_stream.avail_in != 0) {
   220         fprintf(stderr, "deflate not greedy\n");
   221 	exit(1);
   222     }
   223 
   224     /* Feed in already compressed data and switch to no compression: */
   225     deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
   226     c_stream.next_in = compr;
   227     c_stream.avail_in = (uInt)comprLen/2;
   228     err = deflate(&c_stream, Z_NO_FLUSH);
   229     CHECK_ERR(err, "deflate");
   230 
   231     /* Switch back to compressing mode: */
   232     deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
   233     c_stream.next_in = uncompr;
   234     c_stream.avail_in = (uInt)uncomprLen;
   235     err = deflate(&c_stream, Z_NO_FLUSH);
   236     CHECK_ERR(err, "deflate");
   237 
   238     err = deflate(&c_stream, Z_FINISH);
   239     if (err != Z_STREAM_END) {
   240         fprintf(stderr, "deflate should report Z_STREAM_END\n");
   241 	exit(1);
   242     }
   243     err = deflateEnd(&c_stream);
   244     CHECK_ERR(err, "deflateEnd");
   245 }
   246 
   247 /**
   248 @SYMTestCaseID          SYSLIB-EZLIB-CT-0824
   249 @SYMTestCaseDesc	    Inflate with large buffers test
   250 @SYMTestPriority 	    High
   251 @SYMTestActions  	    Tests inflate() with large buffers
   252 @SYMTestExpectedResults The test must not fail.
   253 @SYMREQ                 REQ0000
   254 */
   255 
   256 void test_large_inflate(
   257     Byte *compr, uLong comprLen, Byte *uncompr,
   258      uLong uncomprLen)
   259 {
   260     int err;
   261     z_stream d_stream; /* decompression stream */
   262 
   263     strcpy((char*)uncompr, "garbage");
   264 
   265     d_stream.zalloc = (alloc_func)0;
   266     d_stream.zfree = (free_func)0;
   267     d_stream.opaque = (voidpf)0;
   268 
   269     d_stream.next_in  = compr;
   270     d_stream.avail_in = (uInt)comprLen;
   271 
   272     err = inflateInit(&d_stream);
   273     CHECK_ERR(err, "inflateInit");
   274 
   275     for (;;) {
   276         d_stream.next_out = uncompr;            /* discard the output */
   277 	d_stream.avail_out = (uInt)uncomprLen;
   278         err = inflate(&d_stream, Z_NO_FLUSH);
   279         if (err == Z_STREAM_END) break;
   280         CHECK_ERR(err, "large inflate");
   281     }
   282 
   283     err = inflateEnd(&d_stream);
   284     CHECK_ERR(err, "inflateEnd");
   285 
   286     if (d_stream.total_out != 2*uncomprLen + comprLen/2) {
   287         fprintf(stderr, "bad large inflate: %ld\n", d_stream.total_out);
   288 	exit(1);
   289     } else {
   290         printf("large_inflate(): OK\n");
   291     }
   292 }
   293 
   294 /**
   295 @SYMTestCaseID          SYSLIB-EZLIB-CT-0825
   296 @SYMTestCaseDesc	    Test deflate() with full flush
   297 @SYMTestPriority 	    High
   298 @SYMTestActions  	    Test by flushing the output as random access is desired
   299 @SYMTestExpectedResults The test must not fail.
   300 @SYMREQ                 REQ0000
   301 */
   302 
   303 void test_flush(
   304     Byte *compr,
   305     uLong *comprLen)
   306 {
   307     z_stream c_stream; /* compression stream */
   308     int err;
   309     int len = strlen(hello)+1;
   310 
   311     c_stream.zalloc = (alloc_func)0;
   312     c_stream.zfree = (free_func)0;
   313     c_stream.opaque = (voidpf)0;
   314 
   315     err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
   316     CHECK_ERR(err, "deflateInit");
   317 
   318     c_stream.next_in  = (Bytef*)hello;
   319     c_stream.next_out = compr;
   320     c_stream.avail_in = 3;
   321     c_stream.avail_out = (uInt)*comprLen;
   322     err = deflate(&c_stream, Z_FULL_FLUSH);
   323     CHECK_ERR(err, "deflate");
   324 
   325     compr[3]++; /* force an error in first compressed block */
   326     c_stream.avail_in = len - 3;
   327 
   328     err = deflate(&c_stream, Z_FINISH);
   329     if (err != Z_STREAM_END) {
   330         CHECK_ERR(err, "deflate");
   331     }
   332     err = deflateEnd(&c_stream);
   333     CHECK_ERR(err, "deflateEnd");
   334 
   335     *comprLen = c_stream.total_out;
   336 }
   337 
   338 /**
   339 @SYMTestCaseID          SYSLIB-EZLIB-CT-0826
   340 @SYMTestCaseDesc	    Test the inflateSync() function
   341 @SYMTestPriority 	    High
   342 @SYMTestActions  	    Tests for reading of compressed data with damaged parts and check for errors.
   343 @SYMTestExpectedResults The test must not fail.
   344 @SYMREQ                 REQ0000
   345 */
   346 
   347 void test_sync(
   348     Byte *compr, uLong comprLen, Byte *uncompr,
   349      uLong uncomprLen)
   350 {
   351     int err;
   352     z_stream d_stream; /* decompression stream */
   353 
   354     strcpy((char*)uncompr, "garbage");
   355 
   356     d_stream.zalloc = (alloc_func)0;
   357     d_stream.zfree = (free_func)0;
   358     d_stream.opaque = (voidpf)0;
   359 
   360     d_stream.next_in  = compr;
   361     d_stream.avail_in = 2; /* just read the zlib header */
   362 
   363     err = inflateInit(&d_stream);
   364     CHECK_ERR(err, "inflateInit");
   365 
   366     d_stream.next_out = uncompr;
   367     d_stream.avail_out = (uInt)uncomprLen;
   368 
   369     inflate(&d_stream, Z_NO_FLUSH);
   370     CHECK_ERR(err, "inflate");
   371 
   372     d_stream.avail_in = (uInt)comprLen-2;   /* read all compressed data */
   373     err = inflateSync(&d_stream);           /* but skip the damaged part */
   374     CHECK_ERR(err, "inflateSync");
   375 
   376     err = inflate(&d_stream, Z_FINISH);
   377     if (err != Z_DATA_ERROR) {
   378         fprintf(stderr, "inflate should report DATA_ERROR\n");
   379         /* Because of incorrect adler32 */
   380 	exit(1);
   381     }
   382     err = inflateEnd(&d_stream);
   383     CHECK_ERR(err, "inflateEnd");
   384 
   385     printf("after inflateSync(): hel%s\n", (char *)uncompr);
   386 }
   387 
   388 /**
   389 @SYMTestCaseID          SYSLIB-EZLIB-CT-0827
   390 @SYMTestCaseDesc	    Deflation functionality test
   391 @SYMTestPriority 	    High
   392 @SYMTestActions  	    Test deflate() function with preset dictionary
   393 @SYMTestExpectedResults The test must not fail.
   394 @SYMREQ                 REQ0000
   395 */
   396 
   397 void test_dict_deflate(
   398     Byte *compr,
   399     uLong comprLen)
   400 {
   401     z_stream c_stream; /* compression stream */
   402     int err;
   403 
   404     c_stream.zalloc = (alloc_func)0;
   405     c_stream.zfree = (free_func)0;
   406     c_stream.opaque = (voidpf)0;
   407 
   408     err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
   409     CHECK_ERR(err, "deflateInit");
   410 
   411     err = deflateSetDictionary(&c_stream,
   412 			       (const Bytef*)dictionary, sizeof(dictionary));
   413     CHECK_ERR(err, "deflateSetDictionary");
   414 
   415     dictId = c_stream.adler;
   416     c_stream.next_out = compr;
   417     c_stream.avail_out = (uInt)comprLen;
   418 
   419     c_stream.next_in = (Bytef*)hello;
   420     c_stream.avail_in = (uInt)strlen(hello)+1;
   421 
   422     err = deflate(&c_stream, Z_FINISH);
   423     if (err != Z_STREAM_END) {
   424         fprintf(stderr, "deflate should report Z_STREAM_END\n");
   425 	exit(1);
   426     }
   427     err = deflateEnd(&c_stream);
   428     CHECK_ERR(err, "deflateEnd");
   429 }
   430 
   431 /**
   432 @SYMTestCaseID          SYSLIB-EZLIB-CT-0828
   433 @SYMTestCaseDesc	    Inflation functionality test
   434 @SYMTestPriority 	    High
   435 @SYMTestActions  	    Test inflate() with a preset dictionary
   436 @SYMTestExpectedResults The test must not fail.
   437 @SYMREQ                 REQ0000
   438 */
   439 
   440 void test_dict_inflate(
   441     Byte *compr, uLong comprLen, Byte *uncompr,
   442      uLong uncomprLen)
   443 {
   444     int err;
   445     z_stream d_stream; /* decompression stream */
   446 
   447     strcpy((char*)uncompr, "garbage");
   448 
   449     d_stream.zalloc = (alloc_func)0;
   450     d_stream.zfree = (free_func)0;
   451     d_stream.opaque = (voidpf)0;
   452 
   453     d_stream.next_in  = compr;
   454     d_stream.avail_in = (uInt)comprLen;
   455 
   456     err = inflateInit(&d_stream);
   457     CHECK_ERR(err, "inflateInit");
   458 
   459     d_stream.next_out = uncompr;
   460     d_stream.avail_out = (uInt)uncomprLen;
   461 
   462     for (;;) {
   463         err = inflate(&d_stream, Z_NO_FLUSH);
   464         if (err == Z_STREAM_END) break;
   465 	if (err == Z_NEED_DICT) {
   466 	    if (d_stream.adler != dictId) {
   467 		fprintf(stderr, "unexpected dictionary");
   468 		exit(1);
   469 	    }
   470 	    err = inflateSetDictionary(&d_stream, (const Bytef*)dictionary,
   471 				       sizeof(dictionary));
   472 	}
   473         CHECK_ERR(err, "inflate with dict");
   474     }
   475 
   476     err = inflateEnd(&d_stream);
   477     CHECK_ERR(err, "inflateEnd");
   478 
   479     if (strcmp((char*)uncompr, hello)) {
   480         fprintf(stderr, "bad inflate with dict\n");
   481 	exit(1);
   482     } else {
   483         printf("inflate with dictionary: %s\n", (char *)uncompr);
   484     }
   485 }
   486 
   487 /* ===========================================================================
   488  * Usage:  example [output.gz  [input.gz]]
   489  */
   490 
   491 int main(/*
   492     int argc,
   493     char *argv[]*/)
   494 {
   495     Byte *compr, *uncompr;
   496     uLong comprLen = 10000*sizeof(int); /* don't overflow on MSDOS */
   497     uLong uncomprLen = comprLen;
   498     static const char* myVersion = ZLIB_VERSION;
   499 
   500     if (zlibVersion()[0] != myVersion[0]) {
   501         fprintf(stderr, "incompatible zlib version\n");
   502         exit(1);
   503 
   504     } else if (strcmp(zlibVersion(), ZLIB_VERSION) != 0) {
   505         fprintf(stderr, "warning: different zlib version\n");
   506     }
   507 
   508     compr    = (Byte*)calloc((uInt)comprLen, 1);
   509     uncompr  = (Byte*)calloc((uInt)uncomprLen, 1);
   510     /* compr and uncompr are cleared to avoid reading uninitialized
   511      * data and to ensure that uncompr compresses well.
   512      */
   513     if (compr == Z_NULL || uncompr == Z_NULL) {
   514         printf("out of memory\n");
   515 	exit(1);
   516     }
   517     test_compress(compr, comprLen, uncompr, uncomprLen);
   518 
   519     test_deflate(compr, comprLen);
   520     test_inflate(compr, comprLen, uncompr, uncomprLen);
   521 
   522     test_large_deflate(compr, comprLen, uncompr, uncomprLen);
   523     test_large_inflate(compr, comprLen, uncompr, uncomprLen);
   524 
   525     test_flush(compr, &comprLen);
   526     test_sync(compr, comprLen, uncompr, uncomprLen);
   527     comprLen = uncomprLen;
   528 
   529     test_dict_deflate(compr, comprLen);
   530     test_dict_inflate(compr, comprLen, uncompr, uncomprLen);
   531 
   532     exit(0);
   533     return 0; /* to avoid warning */
   534 }