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