xref: /netbsd-src/external/gpl3/gdb.old/dist/zlib/examples/zran.h (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
1*6881a400Schristos /* zran.h -- example of zlib/gzip stream indexing and random access
2*6881a400Schristos  * Copyright (C) 2005, 2012, 2018 Mark Adler
3*6881a400Schristos  * For conditions of distribution and use, see copyright notice in zlib.h
4*6881a400Schristos  * Version 1.2  14 Oct 2018  Mark Adler */
5*6881a400Schristos 
6*6881a400Schristos #include <stdio.h>
7*6881a400Schristos #include "zlib.h"
8*6881a400Schristos 
9*6881a400Schristos /* Access point list. */
10*6881a400Schristos struct deflate_index {
11*6881a400Schristos     int have;           /* number of list entries */
12*6881a400Schristos     int gzip;           /* 1 if the index is of a gzip file, 0 if it is of a
13*6881a400Schristos                            zlib stream */
14*6881a400Schristos     off_t length;       /* total length of uncompressed data */
15*6881a400Schristos     void *list;         /* allocated list of entries */
16*6881a400Schristos };
17*6881a400Schristos 
18*6881a400Schristos /* Make one entire pass through a zlib or gzip compressed stream and build an
19*6881a400Schristos    index, with access points about every span bytes of uncompressed output.
20*6881a400Schristos    gzip files with multiple members are indexed in their entirety. span should
21*6881a400Schristos    be chosen to balance the speed of random access against the memory
22*6881a400Schristos    requirements of the list, about 32K bytes per access point. The return value
23*6881a400Schristos    is the number of access points on success (>= 1), Z_MEM_ERROR for out of
24*6881a400Schristos    memory, Z_DATA_ERROR for an error in the input file, or Z_ERRNO for a file
25*6881a400Schristos    read error. On success, *built points to the resulting index. */
26*6881a400Schristos int deflate_index_build(FILE *in, off_t span, struct deflate_index **built);
27*6881a400Schristos 
28*6881a400Schristos /* Deallocate an index built by deflate_index_build() */
29*6881a400Schristos void deflate_index_free(struct deflate_index *index);
30*6881a400Schristos 
31*6881a400Schristos /* Use the index to read len bytes from offset into buf. Return bytes read or
32*6881a400Schristos    negative for error (Z_DATA_ERROR or Z_MEM_ERROR). If data is requested past
33*6881a400Schristos    the end of the uncompressed data, then deflate_index_extract() will return a
34*6881a400Schristos    value less than len, indicating how much was actually read into buf. This
35*6881a400Schristos    function should not return a data error unless the file was modified since
36*6881a400Schristos    the index was generated, since deflate_index_build() validated all of the
37*6881a400Schristos    input. deflate_index_extract() will return Z_ERRNO if there is an error on
38*6881a400Schristos    reading or seeking the input file. */
39*6881a400Schristos int deflate_index_extract(FILE *in, struct deflate_index *index, off_t offset,
40*6881a400Schristos                           unsigned char *buf, int len);
41