1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/compressionlibs/ziplib/test/rtest/inflateprimetest/zran.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,548 @@
1.4 +/* Portions Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 + * All rights reserved.
1.6 + */
1.7 +
1.8 +/* zran.c -- example of zlib/gzip stream indexing and random access
1.9 + * Copyright (C) 2005 Mark Adler
1.10 + * For conditions of distribution and use, see copyright notice in zlib.h
1.11 + Version 1.0 29 May 2005 Mark Adler */
1.12 +
1.13 +/* Illustrate the use of Z_BLOCK, inflatePrime(), and inflateSetDictionary()
1.14 + for random access of a compressed file. A file containing a zlib or gzip
1.15 + stream is provided on the command line. The compressed stream is decoded in
1.16 + its entirety, and an index built with access points about every SPAN bytes
1.17 + in the uncompressed output. The compressed file is left open, and can then
1.18 + be read randomly, having to decompress on the average SPAN/2 uncompressed
1.19 + bytes before getting to the desired block of data.
1.20 +
1.21 + An access point can be created at the start of any deflate block, by saving
1.22 + the starting file offset and bit of that block, and the 32K bytes of
1.23 + uncompressed data that precede that block. Also the uncompressed offset of
1.24 + that block is saved to provide a referece for locating a desired starting
1.25 + point in the uncompressed stream. build_index() works by decompressing the
1.26 + input zlib or gzip stream a block at a time, and at the end of each block
1.27 + deciding if enough uncompressed data has gone by to justify the creation of
1.28 + a new access point. If so, that point is saved in a data structure that
1.29 + grows as needed to accommodate the points.
1.30 +
1.31 + To use the index, an offset in the uncompressed data is provided, for which
1.32 + the latest access point at or preceding that offset is located in the index.
1.33 + The input file is positioned to the specified location in the index, and if
1.34 + necessary the first few bits of the compressed data is read from the file.
1.35 + inflate is initialized with those bits and the 32K of uncompressed data, and
1.36 + the decompression then proceeds until the desired offset in the file is
1.37 + reached. Then the decompression continues to read the desired uncompressed
1.38 + data from the file.
1.39 +
1.40 + Another approach would be to generate the index on demand. In that case,
1.41 + requests for random access reads from the compressed data would try to use
1.42 + the index, but if a read far enough past the end of the index is required,
1.43 + then further index entries would be generated and added.
1.44 +
1.45 + There is some fair bit of overhead to starting inflation for the random
1.46 + access, mainly copying the 32K byte dictionary. So if small pieces of the
1.47 + file are being accessed, it would make sense to implement a cache to hold
1.48 + some lookahead and avoid many calls to extract() for small lengths.
1.49 +
1.50 + Another way to build an index would be to use inflateCopy(). That would
1.51 + not be constrained to have access points at block boundaries, but requires
1.52 + more memory per access point, and also cannot be saved to file due to the
1.53 + use of pointers in the state. The approach here allows for storage of the
1.54 + index in a file.
1.55 + */
1.56 +
1.57 +#include <e32test.h>
1.58 +#include <stdio.h>
1.59 +#include <stdlib.h>
1.60 +#include <string.h>
1.61 +#include <fcntl.h>
1.62 +#include <zlib.h>
1.63 +
1.64 +_LIT(KTestTitle, "inflatePrime() Test.");
1.65 +
1.66 +RTest test(_L("inflateprimetest.exe"));
1.67 +const int numTestFiles = 2;
1.68 +const char *filePath = "z:\\test\\inflateprimetest\\\0";
1.69 +const char *testFile[numTestFiles] = {"gzipped.gz\0", "zipped.zip\0"};
1.70 +
1.71 +/* Test macro and function */
1.72 +void Check(TInt aValue, TInt aExpected, TInt aLine)
1.73 + {
1.74 + if (aValue != aExpected)
1.75 + {
1.76 + test.Printf(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
1.77 + test.operator()(EFalse, aLine);
1.78 + }
1.79 + }
1.80 +#define test2(a, b) Check(a, b, __LINE__)
1.81 +
1.82 +#define SPAN 1048576L /* desired distance between access points */
1.83 +#define WINSIZE 32768U /* sliding window size */
1.84 +#define CHUNK 128 /* file input buffer size */
1.85 +
1.86 +/* access point entry */
1.87 +struct point {
1.88 + off_t out; /* corresponding offset in uncompressed data */
1.89 + off_t in; /* offset in input file of first full byte */
1.90 + int bits; /* number of bits (1-7) from byte at in - 1, or 0 */
1.91 + unsigned char window[WINSIZE]; /* preceding 32K of uncompressed data */
1.92 +};
1.93 +
1.94 +/* access point list */
1.95 +struct access {
1.96 + int have; /* number of list entries filled in */
1.97 + int size; /* number of list entries allocated */
1.98 + struct point *list; /* allocated list */
1.99 +};
1.100 +
1.101 +/* Deallocate an index built by build_index() */
1.102 +void free_index(struct access *index)
1.103 +{
1.104 + if (index != NULL) {
1.105 + free(index->list);
1.106 + free(index);
1.107 + }
1.108 +}
1.109 +
1.110 +/* Add an entry to the access point list. If out of memory, deallocate the
1.111 + existing list and return NULL. */
1.112 +struct access *addpoint(struct access *index, int bits,
1.113 + off_t in, off_t out, unsigned left, unsigned char *window)
1.114 +{
1.115 + struct point *next;
1.116 +
1.117 + // if list is empty, create it (start with eight points)
1.118 + if (index == NULL) {
1.119 + index = (struct access *)malloc(sizeof(struct access));
1.120 + if (index == NULL) return NULL;
1.121 + index->list = (struct point *)malloc(sizeof(struct point) << 3);
1.122 + if (index->list == NULL) {
1.123 + free(index);
1.124 + return NULL;
1.125 + }
1.126 + index->size = 8;
1.127 + index->have = 0;
1.128 + }
1.129 +
1.130 + // if list is full, make it bigger
1.131 + else if (index->have == index->size) {
1.132 + index->size <<= 1;
1.133 + next = (struct point *)realloc(index->list, sizeof(struct point) * index->size);
1.134 + if (next == NULL) {
1.135 + free_index(index);
1.136 + return NULL;
1.137 + }
1.138 + index->list = next;
1.139 + }
1.140 +
1.141 + // fill in entry and increment how many we have
1.142 + next = index->list + index->have;
1.143 + next->bits = bits;
1.144 + next->in = in;
1.145 + next->out = out;
1.146 + if (left)
1.147 + memcpy(next->window, window + WINSIZE - left, left);
1.148 + if (left < WINSIZE)
1.149 + memcpy(next->window + left, window, WINSIZE - left);
1.150 + index->have++;
1.151 +
1.152 + /* return list, possibly reallocated */
1.153 + return index;
1.154 +}
1.155 +
1.156 +/* Make one entire pass through the compressed stream and build an index, with
1.157 + access points about every span bytes of uncompressed output -- span is
1.158 + chosen to balance the speed of random access against the memory requirements
1.159 + of the list, about 32K bytes per access point. Note that data after the end
1.160 + of the first zlib or gzip stream in the file is ignored. build_index()
1.161 + returns the number of access points on success (>= 1), Z_MEM_ERROR for out
1.162 + of memory, Z_DATA_ERROR for an error in the input file, or Z_ERRNO for a
1.163 + file read error. On success, *built points to the resulting index. */
1.164 +int build_index(FILE *in, off_t span, struct access **built)
1.165 +{
1.166 + int ret;
1.167 + off_t totin, totout; /* our own total counters to avoid 4GB limit */
1.168 + off_t last; /* totout value of last access point */
1.169 + struct access *index; /* access points being generated */
1.170 + z_stream strm;
1.171 + unsigned char input[CHUNK];
1.172 + unsigned char window[WINSIZE];
1.173 + struct point *next = NULL;
1.174 +
1.175 + /* initialize inflate */
1.176 + strm.zalloc = Z_NULL;
1.177 + strm.zfree = Z_NULL;
1.178 + strm.opaque = Z_NULL;
1.179 + strm.avail_in = 0;
1.180 + strm.next_in = Z_NULL;
1.181 + ret = inflateInit2(&strm, 47); /* automatic zlib or gzip decoding */
1.182 + if (ret != Z_OK)
1.183 + return ret;
1.184 +
1.185 + /* inflate the input, maintain a sliding window, and build an index -- this
1.186 + also validates the integrity of the compressed data using the check
1.187 + information at the end of the gzip or zlib stream */
1.188 + totin = totout = last = 0;
1.189 + index = NULL; /* will be allocated by first addpoint() */
1.190 + strm.avail_out = 0;
1.191 + do {
1.192 + /* get some compressed data from input file */
1.193 + strm.avail_in = fread(input, 1, CHUNK, in);
1.194 + if (ferror(in)) {
1.195 + ret = Z_ERRNO;
1.196 + goto build_index_error;
1.197 + }
1.198 + if (strm.avail_in == 0) {
1.199 + ret = Z_DATA_ERROR;
1.200 + goto build_index_error;
1.201 + }
1.202 + strm.next_in = input;
1.203 +
1.204 + /* process all of that, or until end of stream */
1.205 + do {
1.206 + /* reset sliding window if necessary */
1.207 + if (strm.avail_out == 0) {
1.208 + strm.avail_out = WINSIZE;
1.209 + strm.next_out = window;
1.210 + }
1.211 +
1.212 + /* inflate until out of input, output, or at end of block --
1.213 + update the total input and output counters */
1.214 + totin += strm.avail_in;
1.215 + totout += strm.avail_out;
1.216 + ret = inflate(&strm, Z_BLOCK); /* return at end of block */
1.217 + totin -= strm.avail_in;
1.218 + totout -= strm.avail_out;
1.219 + if (ret == Z_NEED_DICT)
1.220 + ret = Z_DATA_ERROR;
1.221 + if (ret == Z_MEM_ERROR || ret == Z_DATA_ERROR)
1.222 + goto build_index_error;
1.223 + if (ret == Z_STREAM_END)
1.224 + break;
1.225 +
1.226 + /* if at end of block, consider adding an index entry (note that if
1.227 + data_type indicates an end-of-block, then all of the
1.228 + uncompressed data from that block has been delivered, and none
1.229 + of the compressed data after that block has been consumed,
1.230 + except for up to seven bits) -- the totout == 0 provides an
1.231 + entry point after the zlib or gzip header, and assures that the
1.232 + index always has at least one access point; we avoid creating an
1.233 + access point after the last block by checking bit 6 of data_type
1.234 + */
1.235 + if ((strm.data_type & 128) && !(strm.data_type & 64) &&
1.236 + (totout == 0 || totout - last > span)) {
1.237 + index = addpoint(index, strm.data_type & 7, totin,
1.238 + totout, strm.avail_out, window);
1.239 + if (index == NULL) {
1.240 + ret = Z_MEM_ERROR;
1.241 + goto build_index_error;
1.242 + }
1.243 + last = totout;
1.244 + }
1.245 + } while (strm.avail_in != 0);
1.246 + } while (ret != Z_STREAM_END);
1.247 +
1.248 + /* clean up and return index (release unused entries in list) */
1.249 + (void)inflateEnd(&strm);
1.250 +
1.251 + next = (struct point *)realloc(index->list, sizeof(struct point) * index->have);
1.252 + if (next == NULL) {
1.253 + free_index(index);
1.254 + return Z_MEM_ERROR;
1.255 + }
1.256 + index->list = next;
1.257 + index->size = index->have;
1.258 + *built = index;
1.259 + return index->size;
1.260 +
1.261 + /* return error */
1.262 + build_index_error:
1.263 + (void)inflateEnd(&strm);
1.264 + if (index != NULL)
1.265 + free_index(index);
1.266 + return ret;
1.267 +}
1.268 +
1.269 +/* Use the index to read len bytes from offset into buf, return bytes read or
1.270 + negative for error (Z_DATA_ERROR or Z_MEM_ERROR). If data is requested past
1.271 + the end of the uncompressed data, then extract() will return a value less
1.272 + than len, indicating how much as actually read into buf. This function
1.273 + should not return a data error unless the file was modified since the index
1.274 + was generated. extract() may also return Z_ERRNO if there is an error on
1.275 + reading or seeking the input file. */
1.276 +int extract(FILE *in, struct access *index, off_t offset,
1.277 + unsigned char *buf, int len)
1.278 +{
1.279 + int ret, skip, value;
1.280 + z_stream strm;
1.281 + struct point *here;
1.282 + unsigned char input[CHUNK];
1.283 + //unsigned char discard[WINSIZE]; /* No longer required. See comments below. */
1.284 +
1.285 + /* proceed only if something reasonable to do */
1.286 + if (len < 0)
1.287 + return 0;
1.288 +
1.289 + /* find where in stream to start */
1.290 + here = index->list;
1.291 + ret = index->have;
1.292 + while (--ret && here[1].out <= offset)
1.293 + here++;
1.294 +
1.295 + /* initialize file and inflate state to start there */
1.296 + strm.zalloc = Z_NULL;
1.297 + strm.zfree = Z_NULL;
1.298 + strm.opaque = Z_NULL;
1.299 + strm.avail_in = 0;
1.300 + strm.next_in = Z_NULL;
1.301 + ret = inflateInit2(&strm, -15); /* raw inflate */
1.302 + if (ret != Z_OK)
1.303 + return ret;
1.304 + ret = fseek(in, here->in - (here->bits ? 1 : 0), SEEK_SET);
1.305 + if (ret == -1)
1.306 + goto extract_ret;
1.307 +
1.308 + ret = getc(in);
1.309 + if (ret == -1) {
1.310 + ret = ferror(in) ? Z_ERRNO : Z_DATA_ERROR;
1.311 + goto extract_ret;
1.312 + }
1.313 +
1.314 + // If bits is > 0 set the value as done in the original zran.c
1.315 + // else set the value to the next byte to prove that inflatePrime
1.316 + // is not adding anything to the start of the stream when bits is
1.317 + // set to 0. It is then necessary to unget the byte.
1.318 + if(here->bits) {
1.319 + value = ret >> (8 - here->bits);
1.320 + }
1.321 + else {
1.322 + value = ret;
1.323 + ungetc(ret, in);
1.324 + }
1.325 +
1.326 + ret = inflatePrime(&strm, here->bits, value);
1.327 + if(ret != Z_OK) {
1.328 + goto extract_ret;
1.329 + }
1.330 + test.Printf(_L("zran: bits = %d\n"), here->bits);
1.331 + test.Printf(_L("zran: value = %d\n"), value);
1.332 +
1.333 + (void)inflateSetDictionary(&strm, here->window, WINSIZE);
1.334 +
1.335 + /* No longer required. See comment below.
1.336 + *
1.337 + * skip uncompressed bytes until offset reached, then satisfy request
1.338 + offset -= here->out;
1.339 + */
1.340 + strm.avail_in = 0;
1.341 + skip = 1; /* while skipping to offset */
1.342 + do {
1.343 + /* define where to put uncompressed data, and how much */
1.344 + if (skip) { /* at offset now */
1.345 + strm.avail_out = len;
1.346 + strm.next_out = buf;
1.347 + skip = 0; /* only do this once */
1.348 + }
1.349 +
1.350 + /* This code is not required in this test as it is used
1.351 + * to discard decompressed data between the current
1.352 + * access point and the offset(place in the file from
1.353 + * which we wish to decompress data).
1.354 + *
1.355 + if (offset > WINSIZE) { // skip WINSIZE bytes
1.356 + strm.avail_out = WINSIZE;
1.357 + strm.next_out = discard;
1.358 + offset -= WINSIZE;
1.359 + }
1.360 + else if (offset != 0) { // last skip
1.361 + strm.avail_out = (unsigned)offset;
1.362 + strm.next_out = discard;
1.363 + offset = 0;
1.364 + }
1.365 + */
1.366 +
1.367 + /* uncompress until avail_out filled, or end of stream */
1.368 + do {
1.369 + if (strm.avail_in == 0) {
1.370 + strm.avail_in = fread(input, 1, CHUNK, in);
1.371 + if (ferror(in)) {
1.372 + ret = Z_ERRNO;
1.373 + goto extract_ret;
1.374 + }
1.375 + if (strm.avail_in == 0) {
1.376 + ret = Z_DATA_ERROR;
1.377 + goto extract_ret;
1.378 + }
1.379 + strm.next_in = input;
1.380 + }
1.381 + ret = inflate(&strm, Z_NO_FLUSH); /* normal inflate */
1.382 + if (ret == Z_NEED_DICT)
1.383 + ret = Z_DATA_ERROR;
1.384 + if (ret == Z_MEM_ERROR || ret == Z_DATA_ERROR)
1.385 + goto extract_ret;
1.386 + if (ret == Z_STREAM_END)
1.387 + break;
1.388 + } while (strm.avail_out != 0);
1.389 +
1.390 + /* if reach end of stream, then don't keep trying to get more */
1.391 + if (ret == Z_STREAM_END)
1.392 + break;
1.393 +
1.394 + /* do until offset reached and requested data read, or stream ends */
1.395 + } while (skip);
1.396 +
1.397 + /* compute number of uncompressed bytes read after offset */
1.398 + ret = skip ? 0 : len - strm.avail_out;
1.399 +
1.400 + /* clean up and return bytes read or error */
1.401 + extract_ret:
1.402 + (void)inflateEnd(&strm);
1.403 + return ret;
1.404 +}
1.405 +
1.406 +/* Demonstrate the use of build_index() and extract() by processing the file
1.407 + provided and then extracting CHUNK bytes at each access point. */
1.408 +int TestInflatePrime(char *file)
1.409 + {
1.410 + int len;
1.411 + FILE *in;
1.412 + struct access *index;
1.413 + unsigned char buf[CHUNK];
1.414 +
1.415 + in = fopen(file, "rb");
1.416 + if (in == NULL)
1.417 + {
1.418 + return KErrPathNotFound;
1.419 + }
1.420 +
1.421 + // build index
1.422 + len = build_index(in, SPAN, &index);
1.423 + if (len < 0)
1.424 + {
1.425 + fclose(in);
1.426 + test.Printf(_L("error: %d\n"), len);
1.427 + return KErrGeneral;
1.428 + }
1.429 + test.Printf(_L("zran: built index with %d access points\n"), len);
1.430 +
1.431 + // Extract some data at the start of each access point. This is done
1.432 + // so that we can try extracting some data that does not necessarily
1.433 + // start at a byte boundary ie it might start mid byte.
1.434 + for(int i = 0; i < index->have; i++)
1.435 + {
1.436 + len = extract(in, index, index->list[i].out, buf, CHUNK);
1.437 + if (len < 0)
1.438 + {
1.439 + test.Printf(_L("zran: extraction failed: "));
1.440 +
1.441 + if(len == Z_MEM_ERROR)
1.442 + {
1.443 + test.Printf(_L("out of memory error\n"));
1.444 + }
1.445 + else
1.446 + {
1.447 + test.Printf(_L("input corrupted error\n"));
1.448 + }
1.449 + }
1.450 + else
1.451 + {
1.452 + test.Printf(_L("zran: extracted %d bytes at %Lu\n"), len, index->list[i].out);
1.453 + }
1.454 + }
1.455 +
1.456 + // clean up and exit
1.457 + free_index(index);
1.458 + fclose(in);
1.459 +
1.460 + return KErrNone;
1.461 + }
1.462 +
1.463 +/**
1.464 +@SYMTestCaseID SYSLIB-EZLIB2-UT-4273
1.465 +@SYMTestCaseDesc To check that data can be decompressed at various points in a
1.466 + compressed file (i.e. decompression may start part of the way
1.467 + through a byte) via the use of inflatePrime().
1.468 +@SYMTestPriority Low
1.469 +@SYMTestActions 1. Open a compressed file for reading.
1.470 + 2. Create an inflate stream and initialise it using inflateInit2(),
1.471 + setting windowBits to 47 (automatic gzip/zip header detection).
1.472 + 3. Inflate the data in the file using inflate(). During inflation
1.473 + create access points using structure Point which maps points
1.474 + in the uncompressed data with points in the compressed data.
1.475 + The first access point should be at the start of the data
1.476 + i.e. after the header.
1.477 +
1.478 + Structure Point consist of :
1.479 + • UPoint(in bytes) – this is the point in the uncompressed data
1.480 + • CPoint(in bytes) – this is the point in the compressed data
1.481 + • bits(in bits) – this is the point in the compressed data
1.482 + 4. Cleanup the inflate stream using inflateEnd().
1.483 + 5. For each access point do the following:
1.484 + a. Initialise the inflate stream using inflateInit2(),
1.485 + setting windowBits to -15.
1.486 + b. Move the file pointer to CPoint - 1 in the input file.
1.487 + c. Calculate the value which will be passed to inflatePrime().
1.488 + The algorithm used to calculate value can be seen in the
1.489 + attached diagram (in the test spec).
1.490 + d. Call inflatePrime() with the bits and value.
1.491 + e. Inflate a small section of in the input file using inflate().
1.492 + f. Cleanup the inflate stream using inflateEnd().
1.493 + 6. Close the compressed file and cleanup any allocated memory.
1.494 +
1.495 + Note: This test should be completed using a zlib file and a gzip
1.496 + file. These files should be 500 – 1000KB in size.
1.497 +@SYMTestExpectedResults inflatePrime() should return Z_OK and the data should be
1.498 + decompressed with no errors.
1.499 +@SYMDEF REQ7362
1.500 +*/
1.501 +void RunTestL()
1.502 + {
1.503 + test.Next(_L(" @SYMTestCaseID:SYSLIB-EZLIB2-UT-4273 "));
1.504 + int err;
1.505 + char file[KMaxFileName];
1.506 +
1.507 + for(int i = 0; i < numTestFiles; i++)
1.508 + {
1.509 + TBuf<40> testName(_L("inflatePrime test using file "));
1.510 + testName.AppendNum(i);
1.511 + test.Next(testName);
1.512 +
1.513 + strcpy(file, filePath);
1.514 + strcat(file, testFile[i]);
1.515 +
1.516 + err = TestInflatePrime(file);
1.517 +
1.518 + if(err == KErrPathNotFound)
1.519 + {
1.520 + test.Printf(_L("zran: could not open file number %d for reading\n"), i);
1.521 + User::Leave(err);
1.522 + }
1.523 + else if(err != KErrNone)
1.524 + {
1.525 + User::Leave(err);
1.526 + }
1.527 +
1.528 + test.Printf(_L("\n"));
1.529 + }
1.530 + }
1.531 +
1.532 +TInt E32Main()
1.533 + {
1.534 + __UHEAP_MARK;
1.535 +
1.536 + test.Printf(_L("\n"));
1.537 + test.Title();
1.538 + test.Start(KTestTitle);
1.539 +
1.540 + CTrapCleanup* cleanup = CTrapCleanup::New();
1.541 +
1.542 + TRAPD(err, RunTestL());
1.543 + test2(err, KErrNone);
1.544 +
1.545 + test.End();
1.546 + test.Close();
1.547 + delete cleanup;
1.548 +
1.549 + __UHEAP_MARKEND;
1.550 + return KErrNone;
1.551 + }