2023-02-19 07:53:39 +03:00
|
|
|
/* zran.c -- example of deflate stream indexing and random access
|
2024-02-03 09:53:54 +03:00
|
|
|
* Copyright (C) 2005, 2012, 2018, 2023, 2024 Mark Adler
|
2011-09-10 10:24:52 +04:00
|
|
|
* For conditions of distribution and use, see copyright notice in zlib.h
|
2024-08-03 09:28:57 +03:00
|
|
|
* Version 1.6 2 Aug 2024 Mark Adler */
|
2012-09-30 08:48:18 +04:00
|
|
|
|
|
|
|
/* Version History:
|
|
|
|
1.0 29 May 2005 First version
|
|
|
|
1.1 29 Sep 2012 Fix memory reallocation error
|
2018-10-08 07:26:43 +03:00
|
|
|
1.2 14 Oct 2018 Handle gzip streams with multiple members
|
|
|
|
Add a header file to facilitate usage in applications
|
2023-02-19 07:53:39 +03:00
|
|
|
1.3 18 Feb 2023 Permit raw deflate streams as well as zlib and gzip
|
|
|
|
Permit crossing gzip member boundaries when extracting
|
|
|
|
Support a size_t size when extracting (was an int)
|
|
|
|
Do a binary search over the index for an access point
|
|
|
|
Expose the access point type to enable save and load
|
2023-04-14 01:33:03 +03:00
|
|
|
1.4 13 Apr 2023 Add a NOPRIME define to not use inflatePrime()
|
2024-02-03 09:53:54 +03:00
|
|
|
1.5 4 Feb 2024 Set returned index to NULL on an index build error
|
|
|
|
Stop decoding once request is satisfied
|
|
|
|
Provide a reusable inflate engine in the index
|
|
|
|
Allocate the dictionaries to reduce memory usage
|
2024-08-03 09:28:57 +03:00
|
|
|
1.6 2 Aug 2024 Remove unneeded dependency on limits.h
|
2012-09-30 08:48:18 +04:00
|
|
|
*/
|
2011-09-10 10:24:52 +04:00
|
|
|
|
2023-02-19 07:53:39 +03:00
|
|
|
// Illustrate the use of Z_BLOCK, inflatePrime(), and inflateSetDictionary()
|
|
|
|
// for random access of a compressed file. A file containing a raw deflate
|
|
|
|
// stream is provided on the command line. The compressed stream is decoded in
|
|
|
|
// its entirety, and an index built with access points about every SPAN bytes
|
|
|
|
// in the uncompressed output. The compressed file is left open, and can then
|
|
|
|
// be read randomly, having to decompress on the average SPAN/2 uncompressed
|
|
|
|
// bytes before getting to the desired block of data.
|
|
|
|
//
|
|
|
|
// An access point can be created at the start of any deflate block, by saving
|
|
|
|
// the starting file offset and bit of that block, and the 32K bytes of
|
|
|
|
// uncompressed data that precede that block. Also the uncompressed offset of
|
|
|
|
// that block is saved to provide a reference for locating a desired starting
|
|
|
|
// point in the uncompressed stream. deflate_index_build() decompresses the
|
|
|
|
// input raw deflate stream a block at a time, and at the end of each block
|
|
|
|
// decides if enough uncompressed data has gone by to justify the creation of a
|
|
|
|
// new access point. If so, that point is saved in a data structure that grows
|
|
|
|
// as needed to accommodate the points.
|
|
|
|
//
|
|
|
|
// To use the index, an offset in the uncompressed data is provided, for which
|
|
|
|
// the latest access point at or preceding that offset is located in the index.
|
|
|
|
// The input file is positioned to the specified location in the index, and if
|
|
|
|
// necessary the first few bits of the compressed data is read from the file.
|
|
|
|
// inflate is initialized with those bits and the 32K of uncompressed data, and
|
|
|
|
// decompression then proceeds until the desired offset in the file is reached.
|
|
|
|
// Then decompression continues to read the requested uncompressed data from
|
|
|
|
// the file.
|
|
|
|
//
|
|
|
|
// There is some fair bit of overhead to starting inflation for the random
|
|
|
|
// access, mainly copying the 32K byte dictionary. If small pieces of the file
|
|
|
|
// are being accessed, it would make sense to implement a cache to hold some
|
|
|
|
// lookahead to avoid many calls to deflate_index_extract() for small lengths.
|
|
|
|
//
|
|
|
|
// Another way to build an index would be to use inflateCopy(). That would not
|
|
|
|
// be constrained to have access points at block boundaries, but would require
|
|
|
|
// more memory per access point, and could not be saved to a file due to the
|
|
|
|
// use of pointers in the state. The approach here allows for storage of the
|
|
|
|
// index in a file.
|
2011-09-10 10:24:52 +04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "zlib.h"
|
2018-10-08 07:26:43 +03:00
|
|
|
#include "zran.h"
|
2011-09-10 10:24:52 +04:00
|
|
|
|
2023-02-19 07:53:39 +03:00
|
|
|
#define WINSIZE 32768U // sliding window size
|
|
|
|
#define CHUNK 16384 // file input buffer size
|
2011-09-10 10:24:52 +04:00
|
|
|
|
2023-02-19 07:53:39 +03:00
|
|
|
// See comments in zran.h.
|
|
|
|
void deflate_index_free(struct deflate_index *index) {
|
2011-09-10 10:24:52 +04:00
|
|
|
if (index != NULL) {
|
2024-02-05 00:10:44 +03:00
|
|
|
size_t i = index->have;
|
|
|
|
while (i)
|
|
|
|
free(index->list[--i].window);
|
2011-09-10 10:24:52 +04:00
|
|
|
free(index->list);
|
2024-02-04 22:33:11 +03:00
|
|
|
inflateEnd(&index->strm);
|
2011-09-10 10:24:52 +04:00
|
|
|
free(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-19 07:53:39 +03:00
|
|
|
// Add an access point to the list. If out of memory, deallocate the existing
|
|
|
|
// list and return NULL. index->mode is temporarily the allocated number of
|
|
|
|
// access points, until it is time for deflate_index_build() to return. Then
|
|
|
|
// index->mode is set to the mode of inflation.
|
2024-02-05 00:10:44 +03:00
|
|
|
static struct deflate_index *add_point(struct deflate_index *index, off_t in,
|
|
|
|
off_t out, off_t beg,
|
2023-02-19 07:53:39 +03:00
|
|
|
unsigned char *window) {
|
2024-02-04 22:33:11 +03:00
|
|
|
if (index->have == index->mode) {
|
2023-02-19 07:53:39 +03:00
|
|
|
// The list is full. Make it bigger.
|
2024-02-04 22:33:11 +03:00
|
|
|
index->mode = index->mode ? index->mode << 1 : 8;
|
2023-02-19 07:53:39 +03:00
|
|
|
point_t *next = realloc(index->list, sizeof(point_t) * index->mode);
|
2011-09-10 10:24:52 +04:00
|
|
|
if (next == NULL) {
|
2018-10-08 07:26:43 +03:00
|
|
|
deflate_index_free(index);
|
2011-09-10 10:24:52 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
index->list = next;
|
|
|
|
}
|
|
|
|
|
2023-02-19 07:53:39 +03:00
|
|
|
// Fill in the access point and increment how many we have.
|
|
|
|
point_t *next = (point_t *)(index->list) + index->have++;
|
|
|
|
if (index->have < 0) {
|
|
|
|
// Overflowed the int!
|
|
|
|
deflate_index_free(index);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-09-10 10:24:52 +04:00
|
|
|
next->out = out;
|
2023-02-19 07:53:39 +03:00
|
|
|
next->in = in;
|
2024-02-05 00:10:44 +03:00
|
|
|
next->bits = index->strm.data_type & 7;
|
|
|
|
next->dict = out - beg > WINSIZE ? WINSIZE : (unsigned)(out - beg);
|
|
|
|
next->window = malloc(next->dict);
|
|
|
|
if (next->window == NULL) {
|
|
|
|
deflate_index_free(index);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
unsigned recent = WINSIZE - index->strm.avail_out;
|
|
|
|
unsigned copy = recent > next->dict ? next->dict : recent;
|
|
|
|
memcpy(next->window + next->dict - copy, window + recent - copy, copy);
|
|
|
|
copy = next->dict - copy;
|
|
|
|
memcpy(next->window, window + WINSIZE - copy, copy);
|
2011-09-10 10:24:52 +04:00
|
|
|
|
2023-02-19 07:53:39 +03:00
|
|
|
// Return the index, which may have been newly allocated or destroyed.
|
2011-09-10 10:24:52 +04:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2023-02-19 07:53:39 +03:00
|
|
|
// Decompression modes. These are the inflateInit2() windowBits parameter.
|
|
|
|
#define RAW -15
|
|
|
|
#define ZLIB 15
|
|
|
|
#define GZIP 31
|
|
|
|
|
|
|
|
// See comments in zran.h.
|
|
|
|
int deflate_index_build(FILE *in, off_t span, struct deflate_index **built) {
|
2024-02-03 00:39:10 +03:00
|
|
|
// If this returns with an error, any attempt to use the index will cleanly
|
|
|
|
// return an error.
|
|
|
|
*built = NULL;
|
|
|
|
|
2024-02-04 22:33:11 +03:00
|
|
|
// Create and initialize the index list.
|
|
|
|
struct deflate_index *index = malloc(sizeof(struct deflate_index));
|
|
|
|
if (index == NULL)
|
|
|
|
return Z_MEM_ERROR;
|
|
|
|
index->have = 0;
|
|
|
|
index->mode = 0; // entries in index->list allocation
|
|
|
|
index->list = NULL;
|
|
|
|
index->strm.state = Z_NULL; // so inflateEnd() can work
|
|
|
|
|
|
|
|
// Set up the inflation state.
|
|
|
|
index->strm.avail_in = 0;
|
|
|
|
index->strm.avail_out = 0;
|
2023-02-19 07:53:39 +03:00
|
|
|
unsigned char buf[CHUNK]; // input buffer
|
|
|
|
unsigned char win[WINSIZE] = {0}; // output sliding window
|
|
|
|
off_t totin = 0; // total bytes read from input
|
|
|
|
off_t totout = 0; // total bytes uncompressed
|
2024-02-05 00:10:44 +03:00
|
|
|
off_t beg = 0; // starting offset of last history reset
|
2023-02-19 07:53:39 +03:00
|
|
|
int mode = 0; // mode: RAW, ZLIB, or GZIP (0 => not set yet)
|
|
|
|
|
|
|
|
// Decompress from in, generating access points along the way.
|
|
|
|
int ret; // the return value from zlib, or Z_ERRNO
|
|
|
|
off_t last; // last access point uncompressed offset
|
2011-09-10 10:24:52 +04:00
|
|
|
do {
|
2023-02-19 07:53:39 +03:00
|
|
|
// Assure available input, at least until reaching EOF.
|
2024-02-04 22:33:11 +03:00
|
|
|
if (index->strm.avail_in == 0) {
|
|
|
|
index->strm.avail_in = fread(buf, 1, sizeof(buf), in);
|
|
|
|
totin += index->strm.avail_in;
|
|
|
|
index->strm.next_in = buf;
|
|
|
|
if (index->strm.avail_in < sizeof(buf) && ferror(in)) {
|
2023-02-19 07:53:39 +03:00
|
|
|
ret = Z_ERRNO;
|
2011-09-10 10:24:52 +04:00
|
|
|
break;
|
2018-10-08 07:26:43 +03:00
|
|
|
}
|
2011-09-10 10:24:52 +04:00
|
|
|
|
2023-02-19 07:53:39 +03:00
|
|
|
if (mode == 0) {
|
|
|
|
// At the start of the input -- determine the type. Assume raw
|
|
|
|
// if it is neither zlib nor gzip. This could in theory result
|
|
|
|
// in a false positive for zlib, but in practice the fill bits
|
|
|
|
// after a stored block are always zeros, so a raw stream won't
|
|
|
|
// start with an 8 in the low nybble.
|
2024-02-04 22:33:11 +03:00
|
|
|
mode = index->strm.avail_in == 0 ? RAW : // will fail
|
|
|
|
(index->strm.next_in[0] & 0xf) == 8 ? ZLIB :
|
|
|
|
index->strm.next_in[0] == 0x1f ? GZIP :
|
2023-02-19 07:53:39 +03:00
|
|
|
/* else */ RAW;
|
2024-02-04 22:33:11 +03:00
|
|
|
index->strm.zalloc = Z_NULL;
|
|
|
|
index->strm.zfree = Z_NULL;
|
|
|
|
index->strm.opaque = Z_NULL;
|
|
|
|
ret = inflateInit2(&index->strm, mode);
|
2023-02-19 07:53:39 +03:00
|
|
|
if (ret != Z_OK)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assure available output. This rotates the output through, for use as
|
|
|
|
// a sliding window on the uncompressed data.
|
2024-02-04 22:33:11 +03:00
|
|
|
if (index->strm.avail_out == 0) {
|
|
|
|
index->strm.avail_out = sizeof(win);
|
|
|
|
index->strm.next_out = win;
|
2023-02-19 07:53:39 +03:00
|
|
|
}
|
|
|
|
|
2024-02-04 22:33:11 +03:00
|
|
|
if (mode == RAW && index->have == 0)
|
2023-02-19 07:53:39 +03:00
|
|
|
// We skip the inflate() call at the start of raw deflate data in
|
|
|
|
// order generate an access point there. Set data_type to imitate
|
|
|
|
// the end of a header.
|
2024-02-04 22:33:11 +03:00
|
|
|
index->strm.data_type = 0x80;
|
2023-02-19 07:53:39 +03:00
|
|
|
else {
|
|
|
|
// Inflate and update the number of uncompressed bytes.
|
2024-02-04 22:33:11 +03:00
|
|
|
unsigned before = index->strm.avail_out;
|
|
|
|
ret = inflate(&index->strm, Z_BLOCK);
|
|
|
|
totout += before - index->strm.avail_out;
|
2023-02-19 07:53:39 +03:00
|
|
|
}
|
|
|
|
|
2024-02-04 22:33:11 +03:00
|
|
|
if ((index->strm.data_type & 0xc0) == 0x80 &&
|
|
|
|
(index->have == 0 || totout - last >= span)) {
|
2023-02-19 07:53:39 +03:00
|
|
|
// We are at the end of a header or a non-last deflate block, so we
|
|
|
|
// can add an access point here. Furthermore, we are either at the
|
|
|
|
// very start for the first access point, or there has been span or
|
|
|
|
// more uncompressed bytes since the last access point, so we want
|
|
|
|
// to add an access point here.
|
2024-02-05 00:10:44 +03:00
|
|
|
index = add_point(index, totin - index->strm.avail_in, totout, beg,
|
|
|
|
win);
|
2023-02-19 07:53:39 +03:00
|
|
|
if (index == NULL) {
|
|
|
|
ret = Z_MEM_ERROR;
|
|
|
|
break;
|
2011-09-10 10:24:52 +04:00
|
|
|
}
|
2023-02-19 07:53:39 +03:00
|
|
|
last = totout;
|
|
|
|
}
|
2011-09-10 10:24:52 +04:00
|
|
|
|
2023-02-19 07:53:39 +03:00
|
|
|
if (ret == Z_STREAM_END && mode == GZIP &&
|
2024-02-05 00:10:44 +03:00
|
|
|
(index->strm.avail_in || ungetc(getc(in), in) != EOF)) {
|
2023-02-19 07:53:39 +03:00
|
|
|
// There is more input after the end of a gzip member. Reset the
|
|
|
|
// inflate state to read another gzip member. On success, this will
|
|
|
|
// set ret to Z_OK to continue decompressing.
|
2024-02-04 22:33:11 +03:00
|
|
|
ret = inflateReset2(&index->strm, GZIP);
|
2024-02-05 00:10:44 +03:00
|
|
|
beg = totout; // reset history
|
|
|
|
}
|
2023-02-19 07:53:39 +03:00
|
|
|
|
|
|
|
// Keep going until Z_STREAM_END or error. If the compressed data ends
|
|
|
|
// prematurely without a file read error, Z_BUF_ERROR is returned.
|
|
|
|
} while (ret == Z_OK);
|
|
|
|
|
|
|
|
if (ret != Z_STREAM_END) {
|
|
|
|
// An error was encountered. Discard the index and return a negative
|
|
|
|
// error code.
|
|
|
|
deflate_index_free(index);
|
|
|
|
return ret == Z_NEED_DICT ? Z_DATA_ERROR : ret;
|
|
|
|
}
|
|
|
|
|
2024-02-05 00:10:44 +03:00
|
|
|
// Return the index.
|
2023-02-19 07:53:39 +03:00
|
|
|
index->mode = mode;
|
2018-10-08 07:26:43 +03:00
|
|
|
index->length = totout;
|
2011-09-10 10:24:52 +04:00
|
|
|
*built = index;
|
2018-10-08 07:26:43 +03:00
|
|
|
return index->have;
|
2011-09-10 10:24:52 +04:00
|
|
|
}
|
|
|
|
|
2023-04-14 01:33:03 +03:00
|
|
|
#ifdef NOPRIME
|
|
|
|
// Support zlib versions before 1.2.3 (July 2005), or incomplete zlib clones
|
|
|
|
// that do not have inflatePrime().
|
|
|
|
|
|
|
|
# define INFLATEPRIME inflatePreface
|
|
|
|
|
|
|
|
// Append the low bits bits of value to in[] at bit position *have, updating
|
|
|
|
// *have. value must be zero above its low bits bits. bits must be positive.
|
|
|
|
// This assumes that any bits above the *have bits in the last byte are zeros.
|
|
|
|
// That assumption is preserved on return, as any bits above *have + bits in
|
|
|
|
// the last byte written will be set to zeros.
|
|
|
|
static inline void append_bits(unsigned value, int bits,
|
|
|
|
unsigned char *in, int *have) {
|
|
|
|
in += *have >> 3; // where the first bits from value will go
|
|
|
|
int k = *have & 7; // the number of bits already there
|
|
|
|
*have += bits;
|
|
|
|
if (k)
|
|
|
|
*in |= value << k; // write value above the low k bits
|
|
|
|
else
|
|
|
|
*in = value;
|
|
|
|
k = 8 - k; // the number of bits just appended
|
|
|
|
while (bits > k) {
|
|
|
|
value >>= k; // drop the bits appended
|
|
|
|
bits -= k;
|
|
|
|
k = 8; // now at a byte boundary
|
|
|
|
*++in = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-03 01:56:47 +03:00
|
|
|
// Insert enough bits in the form of empty deflate blocks in front of the
|
2023-04-14 01:33:03 +03:00
|
|
|
// low bits bits of value, in order to bring the sequence to a byte boundary.
|
|
|
|
// Then feed that to inflate(). This does what inflatePrime() does, except that
|
|
|
|
// a negative value of bits is not supported. bits must be in 0..16. If the
|
|
|
|
// arguments are invalid, Z_STREAM_ERROR is returned. Otherwise the return
|
|
|
|
// value from inflate() is returned.
|
|
|
|
static int inflatePreface(z_stream *strm, int bits, int value) {
|
|
|
|
// Check input.
|
|
|
|
if (strm == Z_NULL || bits < 0 || bits > 16)
|
|
|
|
return Z_STREAM_ERROR;
|
|
|
|
if (bits == 0)
|
|
|
|
return Z_OK;
|
|
|
|
value &= (2 << (bits - 1)) - 1;
|
|
|
|
|
|
|
|
// An empty dynamic block with an odd number of bits (95). The high bit of
|
|
|
|
// the last byte is unused.
|
|
|
|
static const unsigned char dyn[] = {
|
|
|
|
4, 0xe0, 0x81, 8, 0, 0, 0, 0, 0x20, 0xa8, 0xab, 0x1f
|
|
|
|
};
|
|
|
|
const int dynlen = 95; // number of bits in the block
|
|
|
|
|
|
|
|
// Build an input buffer for inflate that is a multiple of eight bits in
|
|
|
|
// length, and that ends with the low bits bits of value.
|
|
|
|
unsigned char in[(dynlen + 3 * 10 + 16 + 7) / 8];
|
|
|
|
int have = 0;
|
|
|
|
if (bits & 1) {
|
|
|
|
// Insert an empty dynamic block to get to an odd number of bits, so
|
|
|
|
// when bits bits from value are appended, we are at an even number of
|
|
|
|
// bits.
|
|
|
|
memcpy(in, dyn, sizeof(dyn));
|
|
|
|
have = dynlen;
|
|
|
|
}
|
|
|
|
while ((have + bits) & 7)
|
|
|
|
// Insert empty fixed blocks until appending bits bits would put us on
|
|
|
|
// a byte boundary. This will insert at most three fixed blocks.
|
|
|
|
append_bits(2, 10, in, &have);
|
|
|
|
|
|
|
|
// Append the bits bits from value, which takes us to a byte boundary.
|
|
|
|
append_bits(value, bits, in, &have);
|
|
|
|
|
|
|
|
// Deliver the input to inflate(). There is no output space provided, but
|
|
|
|
// inflate() can't get stuck waiting on output not ingesting all of the
|
|
|
|
// provided input. The reason is that there will be at most 16 bits of
|
|
|
|
// input from value after the empty deflate blocks (which themselves
|
|
|
|
// generate no output). At least ten bits are needed to generate the first
|
|
|
|
// output byte from a fixed block. The last two bytes of the buffer have to
|
|
|
|
// be ingested in order to get ten bits, which is the most that value can
|
|
|
|
// occupy.
|
|
|
|
strm->avail_in = have >> 3;
|
|
|
|
strm->next_in = in;
|
|
|
|
strm->avail_out = 0;
|
|
|
|
strm->next_out = in; // not used, but can't be NULL
|
|
|
|
return inflate(strm, Z_NO_FLUSH);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
# define INFLATEPRIME inflatePrime
|
|
|
|
#endif
|
|
|
|
|
2023-02-19 07:53:39 +03:00
|
|
|
// See comments in zran.h.
|
|
|
|
ptrdiff_t deflate_index_extract(FILE *in, struct deflate_index *index,
|
|
|
|
off_t offset, unsigned char *buf, size_t len) {
|
|
|
|
// Do a quick sanity check on the index.
|
2024-02-04 22:33:11 +03:00
|
|
|
if (index == NULL || index->have < 1 || index->list[0].out != 0 ||
|
|
|
|
index->strm.state == Z_NULL)
|
2023-02-19 07:53:39 +03:00
|
|
|
return Z_STREAM_ERROR;
|
2011-09-10 10:24:52 +04:00
|
|
|
|
2023-02-19 07:53:39 +03:00
|
|
|
// If nothing to extract, return zero bytes extracted.
|
|
|
|
if (len == 0 || offset < 0 || offset >= index->length)
|
2011-09-10 10:24:52 +04:00
|
|
|
return 0;
|
|
|
|
|
2023-02-19 07:53:39 +03:00
|
|
|
// Find the access point closest to but not after offset.
|
|
|
|
int lo = -1, hi = index->have;
|
|
|
|
point_t *point = index->list;
|
|
|
|
while (hi - lo > 1) {
|
|
|
|
int mid = (lo + hi) >> 1;
|
|
|
|
if (offset < point[mid].out)
|
|
|
|
hi = mid;
|
|
|
|
else
|
|
|
|
lo = mid;
|
|
|
|
}
|
|
|
|
point += lo;
|
|
|
|
|
|
|
|
// Initialize the input file and prime the inflate engine to start there.
|
|
|
|
int ret = fseeko(in, point->in - (point->bits ? 1 : 0), SEEK_SET);
|
|
|
|
if (ret == -1)
|
|
|
|
return Z_ERRNO;
|
|
|
|
int ch = 0;
|
|
|
|
if (point->bits && (ch = getc(in)) == EOF)
|
|
|
|
return ferror(in) ? Z_ERRNO : Z_BUF_ERROR;
|
2024-02-04 22:33:11 +03:00
|
|
|
index->strm.avail_in = 0;
|
|
|
|
ret = inflateReset2(&index->strm, RAW);
|
2011-09-10 10:24:52 +04:00
|
|
|
if (ret != Z_OK)
|
|
|
|
return ret;
|
2023-02-19 07:53:39 +03:00
|
|
|
if (point->bits)
|
2024-02-04 22:33:11 +03:00
|
|
|
INFLATEPRIME(&index->strm, point->bits, ch >> (8 - point->bits));
|
2024-02-05 00:10:44 +03:00
|
|
|
inflateSetDictionary(&index->strm, point->window, point->dict);
|
2011-09-10 10:24:52 +04:00
|
|
|
|
2023-02-19 07:53:39 +03:00
|
|
|
// Skip uncompressed bytes until offset reached, then satisfy request.
|
|
|
|
unsigned char input[CHUNK];
|
|
|
|
unsigned char discard[WINSIZE];
|
|
|
|
offset -= point->out; // number of bytes to skip to get to offset
|
|
|
|
size_t left = len; // number of bytes left to read after offset
|
2011-09-10 10:24:52 +04:00
|
|
|
do {
|
2023-02-19 07:53:39 +03:00
|
|
|
if (offset) {
|
|
|
|
// Discard up to offset uncompressed bytes.
|
2024-02-04 22:33:11 +03:00
|
|
|
index->strm.avail_out = offset < WINSIZE ? (unsigned)offset :
|
|
|
|
WINSIZE;
|
|
|
|
index->strm.next_out = discard;
|
2011-09-10 10:24:52 +04:00
|
|
|
}
|
2023-02-19 07:53:39 +03:00
|
|
|
else {
|
|
|
|
// Uncompress up to left bytes into buf.
|
2024-08-03 09:28:57 +03:00
|
|
|
index->strm.avail_out = left < (unsigned)-1 ? (unsigned)left :
|
|
|
|
(unsigned)-1;
|
2024-02-04 22:33:11 +03:00
|
|
|
index->strm.next_out = buf + len - left;
|
2018-10-08 07:26:43 +03:00
|
|
|
}
|
2011-09-10 10:24:52 +04:00
|
|
|
|
2023-02-19 07:53:39 +03:00
|
|
|
// Uncompress, setting got to the number of bytes uncompressed.
|
2024-02-04 22:33:11 +03:00
|
|
|
if (index->strm.avail_in == 0) {
|
2023-02-19 07:53:39 +03:00
|
|
|
// Assure available input.
|
2024-02-04 22:33:11 +03:00
|
|
|
index->strm.avail_in = fread(input, 1, CHUNK, in);
|
|
|
|
if (index->strm.avail_in < CHUNK && ferror(in)) {
|
2023-02-19 07:53:39 +03:00
|
|
|
ret = Z_ERRNO;
|
|
|
|
break;
|
|
|
|
}
|
2024-02-04 22:33:11 +03:00
|
|
|
index->strm.next_in = input;
|
2023-02-19 07:53:39 +03:00
|
|
|
}
|
2024-02-04 22:33:11 +03:00
|
|
|
unsigned got = index->strm.avail_out;
|
|
|
|
ret = inflate(&index->strm, Z_NO_FLUSH);
|
|
|
|
got -= index->strm.avail_out;
|
2023-02-19 07:53:39 +03:00
|
|
|
|
|
|
|
// Update the appropriate count.
|
|
|
|
if (offset)
|
|
|
|
offset -= got;
|
2024-02-03 03:13:24 +03:00
|
|
|
else {
|
2023-02-19 07:53:39 +03:00
|
|
|
left -= got;
|
2024-02-03 03:13:24 +03:00
|
|
|
if (left == 0)
|
|
|
|
// Request satisfied.
|
|
|
|
break;
|
|
|
|
}
|
2023-02-19 07:53:39 +03:00
|
|
|
|
|
|
|
// If we're at the end of a gzip member and there's more to read,
|
|
|
|
// continue to the next gzip member.
|
|
|
|
if (ret == Z_STREAM_END && index->mode == GZIP) {
|
|
|
|
// Discard the gzip trailer.
|
|
|
|
unsigned drop = 8; // length of gzip trailer
|
2024-02-04 22:33:11 +03:00
|
|
|
if (index->strm.avail_in >= drop) {
|
|
|
|
index->strm.avail_in -= drop;
|
|
|
|
index->strm.next_in += drop;
|
2023-02-19 07:53:39 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Read and discard the remainder of the gzip trailer.
|
2024-02-04 22:33:11 +03:00
|
|
|
drop -= index->strm.avail_in;
|
|
|
|
index->strm.avail_in = 0;
|
2023-02-19 07:53:39 +03:00
|
|
|
do {
|
|
|
|
if (getc(in) == EOF)
|
|
|
|
// The input does not have a complete trailer.
|
|
|
|
return ferror(in) ? Z_ERRNO : Z_BUF_ERROR;
|
|
|
|
} while (--drop);
|
2011-09-10 10:24:52 +04:00
|
|
|
}
|
2018-10-08 07:26:43 +03:00
|
|
|
|
2024-02-04 22:33:11 +03:00
|
|
|
if (index->strm.avail_in || ungetc(getc(in), in) != EOF) {
|
2023-02-19 07:53:39 +03:00
|
|
|
// There's more after the gzip trailer. Use inflate to skip the
|
|
|
|
// gzip header and resume the raw inflate there.
|
2024-02-04 22:33:11 +03:00
|
|
|
inflateReset2(&index->strm, GZIP);
|
2018-10-08 07:26:43 +03:00
|
|
|
do {
|
2024-02-04 22:33:11 +03:00
|
|
|
if (index->strm.avail_in == 0) {
|
|
|
|
index->strm.avail_in = fread(input, 1, CHUNK, in);
|
|
|
|
if (index->strm.avail_in < CHUNK && ferror(in)) {
|
2018-10-08 07:26:43 +03:00
|
|
|
ret = Z_ERRNO;
|
2023-02-19 07:53:39 +03:00
|
|
|
break;
|
2018-10-08 07:26:43 +03:00
|
|
|
}
|
2024-02-04 22:33:11 +03:00
|
|
|
index->strm.next_in = input;
|
2018-10-08 07:26:43 +03:00
|
|
|
}
|
2024-02-04 22:33:11 +03:00
|
|
|
index->strm.avail_out = WINSIZE;
|
|
|
|
index->strm.next_out = discard;
|
|
|
|
ret = inflate(&index->strm, Z_BLOCK); // stop after header
|
|
|
|
} while (ret == Z_OK && (index->strm.data_type & 0x80) == 0);
|
2018-10-08 07:26:43 +03:00
|
|
|
if (ret != Z_OK)
|
2023-02-19 07:53:39 +03:00
|
|
|
break;
|
2024-02-04 22:33:11 +03:00
|
|
|
inflateReset2(&index->strm, RAW);
|
2018-10-08 07:26:43 +03:00
|
|
|
}
|
2023-02-19 07:53:39 +03:00
|
|
|
}
|
2018-10-08 07:26:43 +03:00
|
|
|
|
2023-02-19 07:53:39 +03:00
|
|
|
// Continue until we have the requested data, the deflate data has
|
|
|
|
// ended, or an error is encountered.
|
2024-02-03 03:13:24 +03:00
|
|
|
} while (ret == Z_OK);
|
2011-09-10 10:24:52 +04:00
|
|
|
|
2023-02-19 07:53:39 +03:00
|
|
|
// Return the number of uncompressed bytes read into buf, or the error.
|
|
|
|
return ret == Z_OK || ret == Z_STREAM_END ? len - left : ret;
|
2011-09-10 10:24:52 +04:00
|
|
|
}
|
|
|
|
|
2018-10-08 07:26:43 +03:00
|
|
|
#ifdef TEST
|
|
|
|
|
2023-02-19 07:53:39 +03:00
|
|
|
#define SPAN 1048576L // desired distance between access points
|
|
|
|
#define LEN 16384 // number of bytes to extract
|
2011-09-10 10:24:52 +04:00
|
|
|
|
2023-02-19 07:53:39 +03:00
|
|
|
// Demonstrate the use of deflate_index_build() and deflate_index_extract() by
|
|
|
|
// processing the file provided on the command line, and extracting LEN bytes
|
|
|
|
// from 2/3rds of the way through the uncompressed output, writing that to
|
|
|
|
// stdout. An offset can be provided as the second argument, in which case the
|
|
|
|
// data is extracted from there instead.
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
// Open the input file.
|
2018-10-08 07:26:43 +03:00
|
|
|
if (argc < 2 || argc > 3) {
|
2023-02-19 07:53:39 +03:00
|
|
|
fprintf(stderr, "usage: zran file.raw [offset]\n");
|
2011-09-10 10:24:52 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2023-02-19 07:53:39 +03:00
|
|
|
FILE *in = fopen(argv[1], "rb");
|
2011-09-10 10:24:52 +04:00
|
|
|
if (in == NULL) {
|
|
|
|
fprintf(stderr, "zran: could not open %s for reading\n", argv[1]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-02-19 07:53:39 +03:00
|
|
|
// Get optional offset.
|
|
|
|
off_t offset = -1;
|
2018-10-08 07:26:43 +03:00
|
|
|
if (argc == 3) {
|
|
|
|
char *end;
|
|
|
|
offset = strtoll(argv[2], &end, 10);
|
|
|
|
if (*end || offset < 0) {
|
|
|
|
fprintf(stderr, "zran: %s is not a valid offset\n", argv[2]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-19 07:53:39 +03:00
|
|
|
// Build index.
|
|
|
|
struct deflate_index *index = NULL;
|
|
|
|
int len = deflate_index_build(in, SPAN, &index);
|
2011-09-10 10:24:52 +04:00
|
|
|
if (len < 0) {
|
|
|
|
fclose(in);
|
|
|
|
switch (len) {
|
|
|
|
case Z_MEM_ERROR:
|
|
|
|
fprintf(stderr, "zran: out of memory\n");
|
|
|
|
break;
|
2023-02-19 07:53:39 +03:00
|
|
|
case Z_BUF_ERROR:
|
|
|
|
fprintf(stderr, "zran: %s ended prematurely\n", argv[1]);
|
|
|
|
break;
|
2011-09-10 10:24:52 +04:00
|
|
|
case Z_DATA_ERROR:
|
|
|
|
fprintf(stderr, "zran: compressed data error in %s\n", argv[1]);
|
|
|
|
break;
|
|
|
|
case Z_ERRNO:
|
|
|
|
fprintf(stderr, "zran: read error on %s\n", argv[1]);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "zran: error %d while building index\n", len);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "zran: built index with %d access points\n", len);
|
|
|
|
|
2023-02-19 07:53:39 +03:00
|
|
|
// Use index by reading some bytes from an arbitrary offset.
|
|
|
|
unsigned char buf[LEN];
|
2018-10-08 07:26:43 +03:00
|
|
|
if (offset == -1)
|
2023-02-19 07:53:39 +03:00
|
|
|
offset = ((index->length + 1) << 1) / 3;
|
|
|
|
ptrdiff_t got = deflate_index_extract(in, index, offset, buf, LEN);
|
|
|
|
if (got < 0)
|
2011-09-10 10:24:52 +04:00
|
|
|
fprintf(stderr, "zran: extraction failed: %s error\n",
|
2023-02-19 07:53:39 +03:00
|
|
|
got == Z_MEM_ERROR ? "out of memory" : "input corrupted");
|
2011-09-10 10:24:52 +04:00
|
|
|
else {
|
2023-02-19 07:53:39 +03:00
|
|
|
fwrite(buf, 1, got, stdout);
|
|
|
|
fprintf(stderr, "zran: extracted %ld bytes at %lld\n", got, offset);
|
2011-09-10 10:24:52 +04:00
|
|
|
}
|
|
|
|
|
2023-02-19 07:53:39 +03:00
|
|
|
// Clean up and exit.
|
2018-10-08 07:26:43 +03:00
|
|
|
deflate_index_free(index);
|
2011-09-10 10:24:52 +04:00
|
|
|
fclose(in);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-10-08 07:26:43 +03:00
|
|
|
|
|
|
|
#endif
|