zran.h revision 1.1.1.2 1 1.1.1.2 christos /* zran.h -- example of deflated stream indexing and random access
2 1.1.1.2 christos * Copyright (C) 2005, 2012, 2018, 2023 Mark Adler
3 1.1 christos * For conditions of distribution and use, see copyright notice in zlib.h
4 1.1.1.2 christos * Version 1.3 18 Feb 2023 Mark Adler */
5 1.1 christos
6 1.1 christos #include <stdio.h>
7 1.1 christos #include "zlib.h"
8 1.1 christos
9 1.1.1.2 christos // Access point.
10 1.1.1.2 christos typedef struct point {
11 1.1.1.2 christos off_t out; // offset in uncompressed data
12 1.1.1.2 christos off_t in; // offset in compressed file of first full byte
13 1.1.1.2 christos int bits; // 0, or number of bits (1-7) from byte at in-1
14 1.1.1.2 christos unsigned char window[32768]; // preceding 32K of uncompressed data
15 1.1.1.2 christos } point_t;
16 1.1.1.2 christos
17 1.1.1.2 christos // Access point list.
18 1.1 christos struct deflate_index {
19 1.1.1.2 christos int have; // number of access points in list
20 1.1.1.2 christos int mode; // -15 for raw, 15 for zlib, or 31 for gzip
21 1.1.1.2 christos off_t length; // total length of uncompressed data
22 1.1.1.2 christos point_t *list; // allocated list of access points
23 1.1 christos };
24 1.1 christos
25 1.1.1.2 christos // Make one pass through a zlib, gzip, or raw deflate compressed stream and
26 1.1.1.2 christos // build an index, with access points about every span bytes of uncompressed
27 1.1.1.2 christos // output. gzip files with multiple members are fully indexed. span should be
28 1.1.1.2 christos // chosen to balance the speed of random access against the memory requirements
29 1.1.1.2 christos // of the list, which is about 32K bytes per access point. The return value is
30 1.1.1.2 christos // the number of access points on success (>= 1), Z_MEM_ERROR for out of
31 1.1.1.2 christos // memory, Z_BUF_ERROR for a premature end of input, Z_DATA_ERROR for a format
32 1.1.1.2 christos // or verification error in the input file, or Z_ERRNO for a file read error.
33 1.1.1.2 christos // On success, *built points to the resulting index.
34 1.1 christos int deflate_index_build(FILE *in, off_t span, struct deflate_index **built);
35 1.1 christos
36 1.1.1.2 christos // Use the index to read len bytes from offset into buf. Return the number of
37 1.1.1.2 christos // bytes read or a negative error code. If data is requested past the end of
38 1.1.1.2 christos // the uncompressed data, then deflate_index_extract() will return a value less
39 1.1.1.2 christos // than len, indicating how much was actually read into buf. If given a valid
40 1.1.1.2 christos // index, this function should not return an error unless the file was modified
41 1.1.1.2 christos // somehow since the index was generated, given that deflate_index_build() had
42 1.1.1.2 christos // validated all of the input. If nevertheless there is a failure, Z_BUF_ERROR
43 1.1.1.2 christos // is returned if the compressed data ends prematurely, Z_DATA_ERROR if the
44 1.1.1.2 christos // deflate compressed data is not valid, Z_MEM_ERROR if out of memory,
45 1.1.1.2 christos // Z_STREAM_ERROR if the index is not valid, or Z_ERRNO if there is an error
46 1.1.1.2 christos // reading or seeking on the input file.
47 1.1.1.2 christos ptrdiff_t deflate_index_extract(FILE *in, struct deflate_index *index,
48 1.1.1.2 christos off_t offset, unsigned char *buf, size_t len);
49 1.1 christos
50 1.1.1.2 christos // Deallocate an index built by deflate_index_build().
51 1.1.1.2 christos void deflate_index_free(struct deflate_index *index);
52