Home | History | Annotate | Line # | Download | only in examples
zran.c revision 1.1
      1  1.1  christos /*	$NetBSD: zran.c,v 1.1 2006/01/14 20:11:10 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /* zran.c -- example of zlib/gzip stream indexing and random access
      4  1.1  christos  * Copyright (C) 2005 Mark Adler
      5  1.1  christos  * For conditions of distribution and use, see copyright notice in zlib.h
      6  1.1  christos    Version 1.0  29 May 2005  Mark Adler */
      7  1.1  christos 
      8  1.1  christos /* Illustrate the use of Z_BLOCK, inflatePrime(), and inflateSetDictionary()
      9  1.1  christos    for random access of a compressed file.  A file containing a zlib or gzip
     10  1.1  christos    stream is provided on the command line.  The compressed stream is decoded in
     11  1.1  christos    its entirety, and an index built with access points about every SPAN bytes
     12  1.1  christos    in the uncompressed output.  The compressed file is left open, and can then
     13  1.1  christos    be read randomly, having to decompress on the average SPAN/2 uncompressed
     14  1.1  christos    bytes before getting to the desired block of data.
     15  1.1  christos 
     16  1.1  christos    An access point can be created at the start of any deflate block, by saving
     17  1.1  christos    the starting file offset and bit of that block, and the 32K bytes of
     18  1.1  christos    uncompressed data that precede that block.  Also the uncompressed offset of
     19  1.1  christos    that block is saved to provide a referece for locating a desired starting
     20  1.1  christos    point in the uncompressed stream.  build_index() works by decompressing the
     21  1.1  christos    input zlib or gzip stream a block at a time, and at the end of each block
     22  1.1  christos    deciding if enough uncompressed data has gone by to justify the creation of
     23  1.1  christos    a new access point.  If so, that point is saved in a data structure that
     24  1.1  christos    grows as needed to accommodate the points.
     25  1.1  christos 
     26  1.1  christos    To use the index, an offset in the uncompressed data is provided, for which
     27  1.1  christos    the latest accees point at or preceding that offset is located in the index.
     28  1.1  christos    The input file is positioned to the specified location in the index, and if
     29  1.1  christos    necessary the first few bits of the compressed data is read from the file.
     30  1.1  christos    inflate is initialized with those bits and the 32K of uncompressed data, and
     31  1.1  christos    the decompression then proceeds until the desired offset in the file is
     32  1.1  christos    reached.  Then the decompression continues to read the desired uncompressed
     33  1.1  christos    data from the file.
     34  1.1  christos 
     35  1.1  christos    Another approach would be to generate the index on demand.  In that case,
     36  1.1  christos    requests for random access reads from the compressed data would try to use
     37  1.1  christos    the index, but if a read far enough past the end of the index is required,
     38  1.1  christos    then further index entries would be generated and added.
     39  1.1  christos 
     40  1.1  christos    There is some fair bit of overhead to starting inflation for the random
     41  1.1  christos    access, mainly copying the 32K byte dictionary.  So if small pieces of the
     42  1.1  christos    file are being accessed, it would make sense to implement a cache to hold
     43  1.1  christos    some lookahead and avoid many calls to extract() for small lengths.
     44  1.1  christos 
     45  1.1  christos    Another way to build an index would be to use inflateCopy().  That would
     46  1.1  christos    not be constrained to have access points at block boundaries, but requires
     47  1.1  christos    more memory per access point, and also cannot be saved to file due to the
     48  1.1  christos    use of pointers in the state.  The approach here allows for storage of the
     49  1.1  christos    index in a file.
     50  1.1  christos  */
     51  1.1  christos 
     52  1.1  christos #include <stdio.h>
     53  1.1  christos #include <stdlib.h>
     54  1.1  christos #include <string.h>
     55  1.1  christos #include "zlib.h"
     56  1.1  christos 
     57  1.1  christos #define local static
     58  1.1  christos 
     59  1.1  christos #define SPAN 1048576L       /* desired distance between access points */
     60  1.1  christos #define WINSIZE 32768U      /* sliding window size */
     61  1.1  christos #define CHUNK 16384         /* file input buffer size */
     62  1.1  christos 
     63  1.1  christos /* access point entry */
     64  1.1  christos struct point {
     65  1.1  christos     off_t out;          /* corresponding offset in uncompressed data */
     66  1.1  christos     off_t in;           /* offset in input file of first full byte */
     67  1.1  christos     int bits;           /* number of bits (1-7) from byte at in - 1, or 0 */
     68  1.1  christos     unsigned char window[WINSIZE];  /* preceding 32K of uncompressed data */
     69  1.1  christos };
     70  1.1  christos 
     71  1.1  christos /* access point list */
     72  1.1  christos struct access {
     73  1.1  christos     int have;           /* number of list entries filled in */
     74  1.1  christos     int size;           /* number of list entries allocated */
     75  1.1  christos     struct point *list; /* allocated list */
     76  1.1  christos };
     77  1.1  christos 
     78  1.1  christos /* Deallocate an index built by build_index() */
     79  1.1  christos local void free_index(struct access *index)
     80  1.1  christos {
     81  1.1  christos     if (index != NULL) {
     82  1.1  christos         free(index->list);
     83  1.1  christos         free(index);
     84  1.1  christos     }
     85  1.1  christos }
     86  1.1  christos 
     87  1.1  christos /* Add an entry to the access point list.  If out of memory, deallocate the
     88  1.1  christos    existing list and return NULL. */
     89  1.1  christos local struct access *addpoint(struct access *index, int bits,
     90  1.1  christos     off_t in, off_t out, unsigned left, unsigned char *window)
     91  1.1  christos {
     92  1.1  christos     struct point *next;
     93  1.1  christos 
     94  1.1  christos     /* if list is empty, create it (start with eight points) */
     95  1.1  christos     if (index == NULL) {
     96  1.1  christos         index = malloc(sizeof(struct access));
     97  1.1  christos         if (index == NULL) return NULL;
     98  1.1  christos         index->list = malloc(sizeof(struct point) << 3);
     99  1.1  christos         if (index->list == NULL) {
    100  1.1  christos             free(index);
    101  1.1  christos             return NULL;
    102  1.1  christos         }
    103  1.1  christos         index->size = 8;
    104  1.1  christos         index->have = 0;
    105  1.1  christos     }
    106  1.1  christos 
    107  1.1  christos     /* if list is full, make it bigger */
    108  1.1  christos     else if (index->have == index->size) {
    109  1.1  christos         index->size <<= 1;
    110  1.1  christos         next = realloc(index->list, sizeof(struct point) * index->size);
    111  1.1  christos         if (next == NULL) {
    112  1.1  christos             free_index(index);
    113  1.1  christos             return NULL;
    114  1.1  christos         }
    115  1.1  christos         index->list = next;
    116  1.1  christos     }
    117  1.1  christos 
    118  1.1  christos     /* fill in entry and increment how many we have */
    119  1.1  christos     next = index->list + index->have;
    120  1.1  christos     next->bits = bits;
    121  1.1  christos     next->in = in;
    122  1.1  christos     next->out = out;
    123  1.1  christos     if (left)
    124  1.1  christos         memcpy(next->window, window + WINSIZE - left, left);
    125  1.1  christos     if (left < WINSIZE)
    126  1.1  christos         memcpy(next->window + left, window, WINSIZE - left);
    127  1.1  christos     index->have++;
    128  1.1  christos 
    129  1.1  christos     /* return list, possibly reallocated */
    130  1.1  christos     return index;
    131  1.1  christos }
    132  1.1  christos 
    133  1.1  christos /* Make one entire pass through the compressed stream and build an index, with
    134  1.1  christos    access points about every span bytes of uncompressed output -- span is
    135  1.1  christos    chosen to balance the speed of random access against the memory requirements
    136  1.1  christos    of the list, about 32K bytes per access point.  Note that data after the end
    137  1.1  christos    of the first zlib or gzip stream in the file is ignored.  build_index()
    138  1.1  christos    returns the number of access points on success (>= 1), Z_MEM_ERROR for out
    139  1.1  christos    of memory, Z_DATA_ERROR for an error in the input file, or Z_ERRNO for a
    140  1.1  christos    file read error.  On success, *built points to the resulting index. */
    141  1.1  christos local int build_index(FILE *in, off_t span, struct access **built)
    142  1.1  christos {
    143  1.1  christos     int ret;
    144  1.1  christos     off_t totin, totout;        /* our own total counters to avoid 4GB limit */
    145  1.1  christos     off_t last;                 /* totout value of last access point */
    146  1.1  christos     struct access *index;       /* access points being generated */
    147  1.1  christos     z_stream strm;
    148  1.1  christos     unsigned char input[CHUNK];
    149  1.1  christos     unsigned char window[WINSIZE];
    150  1.1  christos 
    151  1.1  christos     /* initialize inflate */
    152  1.1  christos     strm.zalloc = Z_NULL;
    153  1.1  christos     strm.zfree = Z_NULL;
    154  1.1  christos     strm.opaque = Z_NULL;
    155  1.1  christos     strm.avail_in = 0;
    156  1.1  christos     strm.next_in = Z_NULL;
    157  1.1  christos     ret = inflateInit2(&strm, 47);      /* automatic zlib or gzip decoding */
    158  1.1  christos     if (ret != Z_OK)
    159  1.1  christos         return ret;
    160  1.1  christos 
    161  1.1  christos     /* inflate the input, maintain a sliding window, and build an index -- this
    162  1.1  christos        also validates the integrity of the compressed data using the check
    163  1.1  christos        information at the end of the gzip or zlib stream */
    164  1.1  christos     totin = totout = last = 0;
    165  1.1  christos     index = NULL;               /* will be allocated by first addpoint() */
    166  1.1  christos     strm.avail_out = 0;
    167  1.1  christos     do {
    168  1.1  christos         /* get some compressed data from input file */
    169  1.1  christos         strm.avail_in = fread(input, 1, CHUNK, in);
    170  1.1  christos         if (ferror(in)) {
    171  1.1  christos             ret = Z_ERRNO;
    172  1.1  christos             goto build_index_error;
    173  1.1  christos         }
    174  1.1  christos         if (strm.avail_in == 0) {
    175  1.1  christos             ret = Z_DATA_ERROR;
    176  1.1  christos             goto build_index_error;
    177  1.1  christos         }
    178  1.1  christos         strm.next_in = input;
    179  1.1  christos 
    180  1.1  christos         /* process all of that, or until end of stream */
    181  1.1  christos         do {
    182  1.1  christos             /* reset sliding window if necessary */
    183  1.1  christos             if (strm.avail_out == 0) {
    184  1.1  christos                 strm.avail_out = WINSIZE;
    185  1.1  christos                 strm.next_out = window;
    186  1.1  christos             }
    187  1.1  christos 
    188  1.1  christos             /* inflate until out of input, output, or at end of block --
    189  1.1  christos                update the total input and output counters */
    190  1.1  christos             totin += strm.avail_in;
    191  1.1  christos             totout += strm.avail_out;
    192  1.1  christos             ret = inflate(&strm, Z_BLOCK);      /* return at end of block */
    193  1.1  christos             totin -= strm.avail_in;
    194  1.1  christos             totout -= strm.avail_out;
    195  1.1  christos             if (ret == Z_NEED_DICT)
    196  1.1  christos                 ret = Z_DATA_ERROR;
    197  1.1  christos             if (ret == Z_MEM_ERROR || ret == Z_DATA_ERROR)
    198  1.1  christos                 goto build_index_error;
    199  1.1  christos             if (ret == Z_STREAM_END)
    200  1.1  christos                 break;
    201  1.1  christos 
    202  1.1  christos             /* if at end of block, consider adding an index entry (note that if
    203  1.1  christos                data_type indicates an end-of-block, then all of the
    204  1.1  christos                uncompressed data from that block has been delivered, and none
    205  1.1  christos                of the compressed data after that block has been consumed,
    206  1.1  christos                except for up to seven bits) -- the totout == 0 provides an
    207  1.1  christos                entry point after the zlib or gzip header, and assures that the
    208  1.1  christos                index always has at least one access point; we avoid creating an
    209  1.1  christos                access point after the last block by checking bit 6 of data_type
    210  1.1  christos              */
    211  1.1  christos             if ((strm.data_type & 128) && !(strm.data_type & 64) &&
    212  1.1  christos                 (totout == 0 || totout - last > span)) {
    213  1.1  christos                 index = addpoint(index, strm.data_type & 7, totin,
    214  1.1  christos                                  totout, strm.avail_out, window);
    215  1.1  christos                 if (index == NULL) {
    216  1.1  christos                     ret = Z_MEM_ERROR;
    217  1.1  christos                     goto build_index_error;
    218  1.1  christos                 }
    219  1.1  christos                 last = totout;
    220  1.1  christos             }
    221  1.1  christos         } while (strm.avail_in != 0);
    222  1.1  christos     } while (ret != Z_STREAM_END);
    223  1.1  christos 
    224  1.1  christos     /* clean up and return index (release unused entries in list) */
    225  1.1  christos     (void)inflateEnd(&strm);
    226  1.1  christos     index = realloc(index, sizeof(struct point) * index->have);
    227  1.1  christos     index->size = index->have;
    228  1.1  christos     *built = index;
    229  1.1  christos     return index->size;
    230  1.1  christos 
    231  1.1  christos     /* return error */
    232  1.1  christos   build_index_error:
    233  1.1  christos     (void)inflateEnd(&strm);
    234  1.1  christos     if (index != NULL)
    235  1.1  christos         free_index(index);
    236  1.1  christos     return ret;
    237  1.1  christos }
    238  1.1  christos 
    239  1.1  christos /* Use the index to read len bytes from offset into buf, return bytes read or
    240  1.1  christos    negative for error (Z_DATA_ERROR or Z_MEM_ERROR).  If data is requested past
    241  1.1  christos    the end of the uncompressed data, then extract() will return a value less
    242  1.1  christos    than len, indicating how much as actually read into buf.  This function
    243  1.1  christos    should not return a data error unless the file was modified since the index
    244  1.1  christos    was generated.  extract() may also return Z_ERRNO if there is an error on
    245  1.1  christos    reading or seeking the input file. */
    246  1.1  christos local int extract(FILE *in, struct access *index, off_t offset,
    247  1.1  christos                   unsigned char *buf, int len)
    248  1.1  christos {
    249  1.1  christos     int ret, skip;
    250  1.1  christos     z_stream strm;
    251  1.1  christos     struct point *here;
    252  1.1  christos     unsigned char input[CHUNK];
    253  1.1  christos     unsigned char discard[WINSIZE];
    254  1.1  christos 
    255  1.1  christos     /* proceed only if something reasonable to do */
    256  1.1  christos     if (len < 0)
    257  1.1  christos         return 0;
    258  1.1  christos 
    259  1.1  christos     /* find where in stream to start */
    260  1.1  christos     here = index->list;
    261  1.1  christos     ret = index->have;
    262  1.1  christos     while (--ret && here[1].out <= offset)
    263  1.1  christos         here++;
    264  1.1  christos 
    265  1.1  christos     /* initialize file and inflate state to start there */
    266  1.1  christos     strm.zalloc = Z_NULL;
    267  1.1  christos     strm.zfree = Z_NULL;
    268  1.1  christos     strm.opaque = Z_NULL;
    269  1.1  christos     strm.avail_in = 0;
    270  1.1  christos     strm.next_in = Z_NULL;
    271  1.1  christos     ret = inflateInit2(&strm, -15);         /* raw inflate */
    272  1.1  christos     if (ret != Z_OK)
    273  1.1  christos         return ret;
    274  1.1  christos     ret = fseeko(in, here->in - (here->bits ? 1 : 0), SEEK_SET);
    275  1.1  christos     if (ret == -1)
    276  1.1  christos         goto extract_ret;
    277  1.1  christos     if (here->bits) {
    278  1.1  christos         ret = getc(in);
    279  1.1  christos         if (ret == -1) {
    280  1.1  christos             ret = ferror(in) ? Z_ERRNO : Z_DATA_ERROR;
    281  1.1  christos             goto extract_ret;
    282  1.1  christos         }
    283  1.1  christos         (void)inflatePrime(&strm, here->bits, ret >> (8 - here->bits));
    284  1.1  christos     }
    285  1.1  christos     (void)inflateSetDictionary(&strm, here->window, WINSIZE);
    286  1.1  christos 
    287  1.1  christos     /* skip uncompressed bytes until offset reached, then satisfy request */
    288  1.1  christos     offset -= here->out;
    289  1.1  christos     strm.avail_in = 0;
    290  1.1  christos     skip = 1;                               /* while skipping to offset */
    291  1.1  christos     do {
    292  1.1  christos         /* define where to put uncompressed data, and how much */
    293  1.1  christos         if (offset == 0 && skip) {          /* at offset now */
    294  1.1  christos             strm.avail_out = len;
    295  1.1  christos             strm.next_out = buf;
    296  1.1  christos             skip = 0;                       /* only do this once */
    297  1.1  christos         }
    298  1.1  christos         if (offset > WINSIZE) {             /* skip WINSIZE bytes */
    299  1.1  christos             strm.avail_out = WINSIZE;
    300  1.1  christos             strm.next_out = discard;
    301  1.1  christos             offset -= WINSIZE;
    302  1.1  christos         }
    303  1.1  christos         else if (offset != 0) {             /* last skip */
    304  1.1  christos             strm.avail_out = (unsigned)offset;
    305  1.1  christos             strm.next_out = discard;
    306  1.1  christos             offset = 0;
    307  1.1  christos         }
    308  1.1  christos 
    309  1.1  christos         /* uncompress until avail_out filled, or end of stream */
    310  1.1  christos         do {
    311  1.1  christos             if (strm.avail_in == 0) {
    312  1.1  christos                 strm.avail_in = fread(input, 1, CHUNK, in);
    313  1.1  christos                 if (ferror(in)) {
    314  1.1  christos                     ret = Z_ERRNO;
    315  1.1  christos                     goto extract_ret;
    316  1.1  christos                 }
    317  1.1  christos                 if (strm.avail_in == 0) {
    318  1.1  christos                     ret = Z_DATA_ERROR;
    319  1.1  christos                     goto extract_ret;
    320  1.1  christos                 }
    321  1.1  christos                 strm.next_in = input;
    322  1.1  christos             }
    323  1.1  christos             ret = inflate(&strm, Z_NO_FLUSH);       /* normal inflate */
    324  1.1  christos             if (ret == Z_NEED_DICT)
    325  1.1  christos                 ret = Z_DATA_ERROR;
    326  1.1  christos             if (ret == Z_MEM_ERROR || ret == Z_DATA_ERROR)
    327  1.1  christos                 goto extract_ret;
    328  1.1  christos             if (ret == Z_STREAM_END)
    329  1.1  christos                 break;
    330  1.1  christos         } while (strm.avail_out != 0);
    331  1.1  christos 
    332  1.1  christos         /* if reach end of stream, then don't keep trying to get more */
    333  1.1  christos         if (ret == Z_STREAM_END)
    334  1.1  christos             break;
    335  1.1  christos 
    336  1.1  christos         /* do until offset reached and requested data read, or stream ends */
    337  1.1  christos     } while (skip);
    338  1.1  christos 
    339  1.1  christos     /* compute number of uncompressed bytes read after offset */
    340  1.1  christos     ret = skip ? 0 : len - strm.avail_out;
    341  1.1  christos 
    342  1.1  christos     /* clean up and return bytes read or error */
    343  1.1  christos   extract_ret:
    344  1.1  christos     (void)inflateEnd(&strm);
    345  1.1  christos     return ret;
    346  1.1  christos }
    347  1.1  christos 
    348  1.1  christos /* Demonstrate the use of build_index() and extract() by processing the file
    349  1.1  christos    provided on the command line, and the extracting 16K from about 2/3rds of
    350  1.1  christos    the way through the uncompressed output, and writing that to stdout. */
    351  1.1  christos int main(int argc, char **argv)
    352  1.1  christos {
    353  1.1  christos     int len;
    354  1.1  christos     off_t offset;
    355  1.1  christos     FILE *in;
    356  1.1  christos     struct access *index;
    357  1.1  christos     unsigned char buf[CHUNK];
    358  1.1  christos 
    359  1.1  christos     /* open input file */
    360  1.1  christos     if (argc != 2) {
    361  1.1  christos         fprintf(stderr, "usage: zran file.gz\n");
    362  1.1  christos         return 1;
    363  1.1  christos     }
    364  1.1  christos     in = fopen(argv[1], "rb");
    365  1.1  christos     if (in == NULL) {
    366  1.1  christos         fprintf(stderr, "zran: could not open %s for reading\n", argv[1]);
    367  1.1  christos         return 1;
    368  1.1  christos     }
    369  1.1  christos 
    370  1.1  christos     /* build index */
    371  1.1  christos     len = build_index(in, SPAN, &index);
    372  1.1  christos     if (len < 0) {
    373  1.1  christos         fclose(in);
    374  1.1  christos         switch (len) {
    375  1.1  christos         case Z_MEM_ERROR:
    376  1.1  christos             fprintf(stderr, "zran: out of memory\n");
    377  1.1  christos             break;
    378  1.1  christos         case Z_DATA_ERROR:
    379  1.1  christos             fprintf(stderr, "zran: compressed data error in %s\n", argv[1]);
    380  1.1  christos             break;
    381  1.1  christos         case Z_ERRNO:
    382  1.1  christos             fprintf(stderr, "zran: read error on %s\n", argv[1]);
    383  1.1  christos             break;
    384  1.1  christos         default:
    385  1.1  christos             fprintf(stderr, "zran: error %d while building index\n", len);
    386  1.1  christos         }
    387  1.1  christos         return 1;
    388  1.1  christos     }
    389  1.1  christos     fprintf(stderr, "zran: built index with %d access points\n", len);
    390  1.1  christos 
    391  1.1  christos     /* use index by reading some bytes from an arbitrary offset */
    392  1.1  christos     offset = (index->list[index->have - 1].out << 1) / 3;
    393  1.1  christos     len = extract(in, index, offset, buf, CHUNK);
    394  1.1  christos     if (len < 0)
    395  1.1  christos         fprintf(stderr, "zran: extraction failed: %s error\n",
    396  1.1  christos                 len == Z_MEM_ERROR ? "out of memory" : "input corrupted");
    397  1.1  christos     else {
    398  1.1  christos         fwrite(buf, 1, len, stdout);
    399  1.1  christos         fprintf(stderr, "zran: extracted %d bytes at %llu\n", len, offset);
    400  1.1  christos     }
    401  1.1  christos 
    402  1.1  christos     /* clean up and exit */
    403  1.1  christos     free_index(index);
    404  1.1  christos     fclose(in);
    405  1.1  christos     return 0;
    406  1.1  christos }
    407