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