aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/enough.c24
-rw-r--r--examples/gun.c18
-rw-r--r--examples/gznorm.c4
-rw-r--r--examples/zlib_how.html15
-rw-r--r--examples/zpipe.c5
-rw-r--r--examples/zran.c227
-rw-r--r--examples/zran.h12
7 files changed, 163 insertions, 142 deletions
diff --git a/examples/enough.c b/examples/enough.c
index 8a3cade4923a..43112a438c4a 100644
--- a/examples/enough.c
+++ b/examples/enough.c
@@ -1,7 +1,7 @@
/* enough.c -- determine the maximum size of inflate's Huffman code tables over
* all possible valid and complete prefix codes, subject to a length limit.
- * Copyright (C) 2007, 2008, 2012, 2018 Mark Adler
- * Version 1.5 5 August 2018 Mark Adler
+ * Copyright (C) 2007, 2008, 2012, 2018, 2024 Mark Adler
+ * Version 1.6 29 July 2024 Mark Adler
*/
/* Version history:
@@ -19,6 +19,7 @@
Clean up code indentation
1.5 5 Aug 2018 Clean up code style, formatting, and comments
Show all the codes for the maximum, and only the maximum
+ 1.6 29 Jul 2024 Avoid use of uintmax_t
*/
/*
@@ -88,33 +89,32 @@
need to be examined to cover all of the possible table memory usage cases
for the default arguments of 286 symbols limited to 15-bit codes.
- Note that the uintmax_t type is used for counting. It is quite easy to
+ Note that unsigned long long is used for counting. It is quite easy to
exceed the capacity of an eight-byte integer with a large number of symbols
and a large maximum code length, so multiple-precision arithmetic would need
to replace the integer arithmetic in that case. This program will abort if
an overflow occurs. The big_t type identifies where the counting takes
place.
- The uintmax_t type is also used for calculating the number of possible codes
- remaining at the maximum length. This limits the maximum code length to the
- number of bits in a long long minus the number of bits needed to represent
- the symbols in a flat code. The code_t type identifies where the bit-pattern
- counting takes place.
+ The unsigned long long type is also used for calculating the number of
+ possible codes remaining at the maximum length. This limits the maximum code
+ length to the number of bits in a long long minus the number of bits needed
+ to represent the symbols in a flat code. The code_t type identifies where
+ the bit-pattern counting takes place.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
-#include <stdint.h>
#include <assert.h>
#define local static
// Special data types.
-typedef uintmax_t big_t; // type for code counting
-#define PRIbig "ju" // printf format for big_t
-typedef uintmax_t code_t; // type for bit pattern counting
+typedef unsigned long long big_t; // type for code counting
+#define PRIbig "llu" // printf format for big_t
+typedef big_t code_t; // type for bit pattern counting
struct tab { // type for been-here check
size_t len; // allocated length of bit vector in octets
char *vec; // allocated bit vector
diff --git a/examples/gun.c b/examples/gun.c
index bea5497e5c2b..dbd181c07169 100644
--- a/examples/gun.c
+++ b/examples/gun.c
@@ -227,12 +227,12 @@ local int lunpipe(unsigned have, z_const unsigned char *next, struct ind *indp,
if (last == -1)
return Z_BUF_ERROR;
if (flags & 0x60) {
- strm->msg = (char *)"unknown lzw flags set";
+ strm->msg = (z_const char *)"unknown lzw flags set";
return Z_DATA_ERROR;
}
max = flags & 0x1f;
if (max < 9 || max > 16) {
- strm->msg = (char *)"lzw bits out of range";
+ strm->msg = (z_const char *)"lzw bits out of range";
return Z_DATA_ERROR;
}
if (max == 9) /* 9 doesn't really mean 9 */
@@ -252,7 +252,7 @@ local int lunpipe(unsigned have, z_const unsigned char *next, struct ind *indp,
if (NEXT() == -1) /* missing a bit */
return Z_BUF_ERROR;
if (last & 1) { /* code must be < 256 */
- strm->msg = (char *)"invalid lzw code";
+ strm->msg = (z_const char *)"invalid lzw code";
return Z_DATA_ERROR;
}
rem = (unsigned)last >> 1; /* remaining 7 bits */
@@ -319,7 +319,7 @@ local int lunpipe(unsigned have, z_const unsigned char *next, struct ind *indp,
to detect random or corrupted input after a compress header.
In any case, the prev > end check must be retained. */
if (code != end + 1 || prev > end) {
- strm->msg = (char *)"invalid lzw code";
+ strm->msg = (z_const char *)"invalid lzw code";
return Z_DATA_ERROR;
}
match[stack++] = (unsigned char)final;
@@ -404,7 +404,7 @@ local int gunpipe(z_stream *strm, int infile, int outfile)
break; /* empty gzip stream is ok */
}
if (last != 31 || (NEXT() != 139 && last != 157)) {
- strm->msg = (char *)"incorrect header check";
+ strm->msg = (z_const char *)"incorrect header check";
ret = first ? Z_DATA_ERROR : Z_ERRNO;
break; /* not a gzip or compress header */
}
@@ -420,7 +420,7 @@ local int gunpipe(z_stream *strm, int infile, int outfile)
ret = Z_BUF_ERROR;
if (NEXT() != 8) { /* only deflate method allowed */
if (last == -1) break;
- strm->msg = (char *)"unknown compression method";
+ strm->msg = (z_const char *)"unknown compression method";
ret = Z_DATA_ERROR;
break;
}
@@ -433,7 +433,7 @@ local int gunpipe(z_stream *strm, int infile, int outfile)
NEXT();
if (last == -1) break;
if (flags & 0xe0) {
- strm->msg = (char *)"unknown header flags set";
+ strm->msg = (z_const char *)"unknown header flags set";
ret = Z_DATA_ERROR;
break;
}
@@ -486,7 +486,7 @@ local int gunpipe(z_stream *strm, int infile, int outfile)
NEXT() != (int)((outd.crc >> 24) & 0xff)) {
/* crc error */
if (last != -1) {
- strm->msg = (char *)"incorrect data check";
+ strm->msg = (z_const char *)"incorrect data check";
ret = Z_DATA_ERROR;
}
break;
@@ -497,7 +497,7 @@ local int gunpipe(z_stream *strm, int infile, int outfile)
NEXT() != (int)((outd.total >> 24) & 0xff)) {
/* length error */
if (last != -1) {
- strm->msg = (char *)"incorrect length check";
+ strm->msg = (z_const char *)"incorrect length check";
ret = Z_DATA_ERROR;
}
break;
diff --git a/examples/gznorm.c b/examples/gznorm.c
index 68e0a0f29be4..e8d9b480d982 100644
--- a/examples/gznorm.c
+++ b/examples/gznorm.c
@@ -10,6 +10,10 @@
// the data, so it is fast, but no advantage is gained from the history that
// could be available across member boundaries.
+#if defined(_WIN32) && !defined(_CRT_NONSTDC_NO_DEPRECATE)
+# define _CRT_NONSTDC_NO_DEPRECATE
+#endif
+
#include <stdio.h> // fread, fwrite, putc, fflush, ferror, fprintf,
// vsnprintf, stdout, stderr, NULL, FILE
#include <stdlib.h> // malloc, free
diff --git a/examples/zlib_how.html b/examples/zlib_how.html
index 43271b988a72..59af43e25ae9 100644
--- a/examples/zlib_how.html
+++ b/examples/zlib_how.html
@@ -4,7 +4,7 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>zlib Usage Example</title>
-<!-- Copyright (c) 2004-2023 Mark Adler. -->
+<!-- Copyright (c) 2004-2026 Mark Adler. -->
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#00A000">
<h2 align="center"> zlib Usage Example </h2>
@@ -21,7 +21,7 @@ Without further ado, here is the program <a href="zpipe.c"><tt>zpipe.c</tt></a>:
<pre><b>
/* zpipe.c: example of proper use of zlib's inflate() and deflate()
Not copyrighted -- provided to the public domain
- Version 1.4 11 December 2005 Mark Adler */
+ Version 1.5 11 February 2026 Mark Adler */
/* Version history:
1.0 30 Oct 2004 First version
@@ -31,6 +31,7 @@ Without further ado, here is the program <a href="zpipe.c"><tt>zpipe.c</tt></a>:
1.3 6 Apr 2005 Remove incorrect assertion in inf()
1.4 11 Dec 2005 Add hack to avoid MSDOS end-of-line conversions
Avoid some compiler warnings for input and output buffers
+ 1.5 11 Feb 2026 Use underscores for Windows POSIX names
*/
</b></pre><!-- -->
We now include the header files for the required definitions. From
@@ -60,7 +61,7 @@ This sets the input and output to binary which suppresses the end-of-line conver
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
# include &lt;fcntl.h&gt;
# include &lt;io.h&gt;
-# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
+# define SET_BINARY_MODE(file) _setmode(_fileno(file), _O_BINARY)
#else
# define SET_BINARY_MODE(file)
#endif
@@ -538,12 +539,12 @@ int main(int argc, char **argv)
}
</b></pre>
<hr>
-<i>Last modified 24 January 2023<br>
-Copyright &#169; 2004-2023 Mark Adler</i><br>
-<a rel="license" href="http://creativecommons.org/licenses/by-nd/4.0/">
+<i>Last modified 12 February 2026<br>
+Copyright &#169; 2004-2026 Mark Adler</i><br>
+<a rel="license" href="https://creativecommons.org/licenses/by-nd/4.0/">
<img alt="Creative Commons License" style="border-width:0"
src="https://i.creativecommons.org/l/by-nd/4.0/88x31.png"></a>
-<a rel="license" href="http://creativecommons.org/licenses/by-nd/4.0/">
+<a rel="license" href="https://creativecommons.org/licenses/by-nd/4.0/">
Creative Commons Attribution-NoDerivatives 4.0 International License</a>.
</body>
</html>
diff --git a/examples/zpipe.c b/examples/zpipe.c
index 83535d169358..021c85089028 100644
--- a/examples/zpipe.c
+++ b/examples/zpipe.c
@@ -1,6 +1,6 @@
/* zpipe.c: example of proper use of zlib's inflate() and deflate()
Not copyrighted -- provided to the public domain
- Version 1.4 11 December 2005 Mark Adler */
+ Version 1.5 11 February 2026 Mark Adler */
/* Version history:
1.0 30 Oct 2004 First version
@@ -10,6 +10,7 @@
1.3 6 Apr 2005 Remove incorrect assertion in inf()
1.4 11 Dec 2005 Add hack to avoid MSDOS end-of-line conversions
Avoid some compiler warnings for input and output buffers
+ 1.5 11 Feb 2026 Use underscores for Windows POSIX names
*/
#include <stdio.h>
@@ -20,7 +21,7 @@
#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
# include <fcntl.h>
# include <io.h>
-# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
+# define SET_BINARY_MODE(file) _setmode(_fileno(file), _O_BINARY)
#else
# define SET_BINARY_MODE(file)
#endif
diff --git a/examples/zran.c b/examples/zran.c
index d3135955b02b..731d34d67c39 100644
--- a/examples/zran.c
+++ b/examples/zran.c
@@ -1,7 +1,7 @@
/* zran.c -- example of deflate stream indexing and random access
- * Copyright (C) 2005, 2012, 2018, 2023 Mark Adler
+ * Copyright (C) 2005, 2012, 2018, 2023, 2024, 2025 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
- * Version 1.4 13 Apr 2023 Mark Adler */
+ * Version 1.7 16 May 2025 Mark Adler */
/* Version History:
1.0 29 May 2005 First version
@@ -14,6 +14,13 @@
Do a binary search over the index for an access point
Expose the access point type to enable save and load
1.4 13 Apr 2023 Add a NOPRIME define to not use inflatePrime()
+ 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
+ 1.6 2 Aug 2024 Remove unneeded dependency on limits.h
+ 1.7 16 May 2025 Remove redundant frees of point list on error
+ Clean out point list structure when freed
*/
// Illustrate the use of Z_BLOCK, inflatePrime(), and inflateSetDictionary()
@@ -57,7 +64,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <limits.h>
#include "zlib.h"
#include "zran.h"
@@ -67,57 +73,48 @@
// See comments in zran.h.
void deflate_index_free(struct deflate_index *index) {
if (index != NULL) {
+ while (index->have)
+ free(index->list[--index->have].window);
free(index->list);
+ index->list = NULL;
+ inflateEnd(&index->strm);
free(index);
}
}
-// 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.
-static struct deflate_index *add_point(struct deflate_index *index, int bits,
- off_t in, off_t out, unsigned left,
+// Add an access point to the list. If out of memory, 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.
+static struct deflate_index *add_point(struct deflate_index *index, off_t in,
+ off_t out, off_t beg,
unsigned char *window) {
- if (index == NULL) {
- // The list is empty. Create it, starting with eight access points.
- index = malloc(sizeof(struct deflate_index));
- if (index == NULL)
- return NULL;
- index->have = 0;
- index->mode = 8;
- index->list = malloc(sizeof(point_t) * index->mode);
- if (index->list == NULL) {
- free(index);
- return NULL;
- }
- }
-
- else if (index->have == index->mode) {
+ if (index->have == index->mode) {
// The list is full. Make it bigger.
- index->mode <<= 1;
+ index->mode = index->mode ? index->mode << 1 : 8;
point_t *next = realloc(index->list, sizeof(point_t) * index->mode);
- if (next == NULL) {
- deflate_index_free(index);
+ if (next == NULL)
return NULL;
- }
index->list = next;
}
// Fill in the access point and increment how many we have.
point_t *next = (point_t *)(index->list) + index->have++;
- if (index->have < 0) {
+ if (index->have < 0)
// Overflowed the int!
- deflate_index_free(index);
return NULL;
- }
next->out = out;
next->in = in;
- next->bits = bits;
- if (left)
- memcpy(next->window, window + WINSIZE - left, left);
- if (left < WINSIZE)
- memcpy(next->window + left, window, WINSIZE - left);
+ 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)
+ 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);
// Return the index, which may have been newly allocated or destroyed.
return index;
@@ -130,25 +127,39 @@ static struct deflate_index *add_point(struct deflate_index *index, int bits,
// See comments in zran.h.
int deflate_index_build(FILE *in, off_t span, struct deflate_index **built) {
- // Set up inflation state.
- z_stream strm = {0}; // inflate engine (gets fired up later)
+ // If this returns with an error, any attempt to use the index will cleanly
+ // return an error.
+ *built = NULL;
+
+ // 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;
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
+ off_t beg = 0; // starting offset of last history reset
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
- struct deflate_index *index = NULL; // list of access points
do {
// Assure available input, at least until reaching EOF.
- if (strm.avail_in == 0) {
- strm.avail_in = fread(buf, 1, sizeof(buf), in);
- totin += strm.avail_in;
- strm.next_in = buf;
- if (strm.avail_in < sizeof(buf) && ferror(in)) {
+ 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)) {
ret = Z_ERRNO;
break;
}
@@ -159,11 +170,14 @@ int deflate_index_build(FILE *in, off_t span, struct deflate_index **built) {
// 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.
- mode = strm.avail_in == 0 ? RAW : // empty -- will fail
- (strm.next_in[0] & 0xf) == 8 ? ZLIB :
- strm.next_in[0] == 0x1f ? GZIP :
+ mode = index->strm.avail_in == 0 ? RAW : // will fail
+ (index->strm.next_in[0] & 0xf) == 8 ? ZLIB :
+ index->strm.next_in[0] == 0x1f ? GZIP :
/* else */ RAW;
- ret = inflateInit2(&strm, mode);
+ index->strm.zalloc = Z_NULL;
+ index->strm.zfree = Z_NULL;
+ index->strm.opaque = Z_NULL;
+ ret = inflateInit2(&index->strm, mode);
if (ret != Z_OK)
break;
}
@@ -171,32 +185,32 @@ int deflate_index_build(FILE *in, off_t span, struct deflate_index **built) {
// Assure available output. This rotates the output through, for use as
// a sliding window on the uncompressed data.
- if (strm.avail_out == 0) {
- strm.avail_out = sizeof(win);
- strm.next_out = win;
+ if (index->strm.avail_out == 0) {
+ index->strm.avail_out = sizeof(win);
+ index->strm.next_out = win;
}
- if (mode == RAW && index == NULL)
+ if (mode == RAW && index->have == 0)
// 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.
- strm.data_type = 0x80;
+ index->strm.data_type = 0x80;
else {
// Inflate and update the number of uncompressed bytes.
- unsigned before = strm.avail_out;
- ret = inflate(&strm, Z_BLOCK);
- totout += before - strm.avail_out;
+ unsigned before = index->strm.avail_out;
+ ret = inflate(&index->strm, Z_BLOCK);
+ totout += before - index->strm.avail_out;
}
- if ((strm.data_type & 0xc0) == 0x80 &&
- (index == NULL || totout - last >= span)) {
+ if ((index->strm.data_type & 0xc0) == 0x80 &&
+ (index->have == 0 || totout - last >= span)) {
// 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.
- index = add_point(index, strm.data_type & 7, totin - strm.avail_in,
- totout, strm.avail_out, win);
+ index = add_point(index, totin - index->strm.avail_in, totout, beg,
+ win);
if (index == NULL) {
ret = Z_MEM_ERROR;
break;
@@ -205,16 +219,17 @@ int deflate_index_build(FILE *in, off_t span, struct deflate_index **built) {
}
if (ret == Z_STREAM_END && mode == GZIP &&
- (strm.avail_in || ungetc(getc(in), in) != EOF))
+ (index->strm.avail_in || ungetc(getc(in), in) != EOF)) {
// 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.
- ret = inflateReset2(&strm, GZIP);
+ ret = inflateReset2(&index->strm, GZIP);
+ beg = totout; // reset history
+ }
// 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);
- inflateEnd(&strm);
if (ret != Z_STREAM_END) {
// An error was encountered. Discard the index and return a negative
@@ -223,17 +238,9 @@ int deflate_index_build(FILE *in, off_t span, struct deflate_index **built) {
return ret == Z_NEED_DICT ? Z_DATA_ERROR : ret;
}
- // Shrink the index to only the occupied access points and return it.
+ // Return the index.
index->mode = mode;
index->length = totout;
- point_t *list = realloc(index->list, sizeof(point_t) * index->have);
- if (list == NULL) {
- // Seems like a realloc() to make something smaller should always work,
- // but just in case.
- deflate_index_free(index);
- return Z_MEM_ERROR;
- }
- index->list = list;
*built = index;
return index->have;
}
@@ -330,7 +337,8 @@ static int inflatePreface(z_stream *strm, int bits, int value) {
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.
- if (index == NULL || index->have < 1 || index->list[0].out != 0)
+ if (index == NULL || index->have < 1 || index->list[0].out != 0 ||
+ index->strm.state == Z_NULL)
return Z_STREAM_ERROR;
// If nothing to extract, return zero bytes extracted.
@@ -356,13 +364,13 @@ ptrdiff_t deflate_index_extract(FILE *in, struct deflate_index *index,
int ch = 0;
if (point->bits && (ch = getc(in)) == EOF)
return ferror(in) ? Z_ERRNO : Z_BUF_ERROR;
- z_stream strm = {0};
- ret = inflateInit2(&strm, RAW);
+ index->strm.avail_in = 0;
+ ret = inflateReset2(&index->strm, RAW);
if (ret != Z_OK)
return ret;
if (point->bits)
- INFLATEPRIME(&strm, point->bits, ch >> (8 - point->bits));
- inflateSetDictionary(&strm, point->window, WINSIZE);
+ INFLATEPRIME(&index->strm, point->bits, ch >> (8 - point->bits));
+ inflateSetDictionary(&index->strm, point->window, point->dict);
// Skip uncompressed bytes until offset reached, then satisfy request.
unsigned char input[CHUNK];
@@ -372,48 +380,54 @@ ptrdiff_t deflate_index_extract(FILE *in, struct deflate_index *index,
do {
if (offset) {
// Discard up to offset uncompressed bytes.
- strm.avail_out = offset < WINSIZE ? (unsigned)offset : WINSIZE;
- strm.next_out = discard;
+ index->strm.avail_out = offset < WINSIZE ? (unsigned)offset :
+ WINSIZE;
+ index->strm.next_out = discard;
}
else {
// Uncompress up to left bytes into buf.
- strm.avail_out = left < UINT_MAX ? (unsigned)left : UINT_MAX;
- strm.next_out = buf + len - left;
+ index->strm.avail_out = left < (unsigned)-1 ? (unsigned)left :
+ (unsigned)-1;
+ index->strm.next_out = buf + len - left;
}
// Uncompress, setting got to the number of bytes uncompressed.
- if (strm.avail_in == 0) {
+ if (index->strm.avail_in == 0) {
// Assure available input.
- strm.avail_in = fread(input, 1, CHUNK, in);
- if (strm.avail_in < CHUNK && ferror(in)) {
+ index->strm.avail_in = fread(input, 1, CHUNK, in);
+ if (index->strm.avail_in < CHUNK && ferror(in)) {
ret = Z_ERRNO;
break;
}
- strm.next_in = input;
+ index->strm.next_in = input;
}
- unsigned got = strm.avail_out;
- ret = inflate(&strm, Z_NO_FLUSH);
- got -= strm.avail_out;
+ unsigned got = index->strm.avail_out;
+ ret = inflate(&index->strm, Z_NO_FLUSH);
+ got -= index->strm.avail_out;
// Update the appropriate count.
if (offset)
offset -= got;
- else
+ else {
left -= got;
+ if (left == 0)
+ // Request satisfied.
+ break;
+ }
// 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
- if (strm.avail_in >= drop) {
- strm.avail_in -= drop;
- strm.next_in += drop;
+ if (index->strm.avail_in >= drop) {
+ index->strm.avail_in -= drop;
+ index->strm.next_in += drop;
}
else {
// Read and discard the remainder of the gzip trailer.
- drop -= strm.avail_in;
- strm.avail_in = 0;
+ drop -= index->strm.avail_in;
+ index->strm.avail_in = 0;
do {
if (getc(in) == EOF)
// The input does not have a complete trailer.
@@ -421,33 +435,32 @@ ptrdiff_t deflate_index_extract(FILE *in, struct deflate_index *index,
} while (--drop);
}
- if (strm.avail_in || ungetc(getc(in), in) != EOF) {
+ if (index->strm.avail_in || ungetc(getc(in), in) != EOF) {
// There's more after the gzip trailer. Use inflate to skip the
// gzip header and resume the raw inflate there.
- inflateReset2(&strm, GZIP);
+ inflateReset2(&index->strm, GZIP);
do {
- if (strm.avail_in == 0) {
- strm.avail_in = fread(input, 1, CHUNK, in);
- if (strm.avail_in < CHUNK && ferror(in)) {
+ if (index->strm.avail_in == 0) {
+ index->strm.avail_in = fread(input, 1, CHUNK, in);
+ if (index->strm.avail_in < CHUNK && ferror(in)) {
ret = Z_ERRNO;
break;
}
- strm.next_in = input;
+ index->strm.next_in = input;
}
- strm.avail_out = WINSIZE;
- strm.next_out = discard;
- ret = inflate(&strm, Z_BLOCK); // stop at end of header
- } while (ret == Z_OK && (strm.data_type & 0x80) == 0);
+ 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);
if (ret != Z_OK)
break;
- inflateReset2(&strm, RAW);
+ inflateReset2(&index->strm, RAW);
}
}
// Continue until we have the requested data, the deflate data has
// ended, or an error is encountered.
- } while (ret == Z_OK && left);
- inflateEnd(&strm);
+ } while (ret == Z_OK);
// Return the number of uncompressed bytes read into buf, or the error.
return ret == Z_OK || ret == Z_STREAM_END ? len - left : ret;
diff --git a/examples/zran.h b/examples/zran.h
index ebf780d0c1f1..16a84feb9fc0 100644
--- a/examples/zran.h
+++ b/examples/zran.h
@@ -1,7 +1,7 @@
-/* zran.h -- example of deflated stream indexing and random access
- * Copyright (C) 2005, 2012, 2018, 2023 Mark Adler
+/* zran.h -- example of deflate stream indexing and random access
+ * Copyright (C) 2005, 2012, 2018, 2023, 2024, 2025 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
- * Version 1.3 18 Feb 2023 Mark Adler */
+ * Version 1.7 16 May 2025 Mark Adler */
#include <stdio.h>
#include "zlib.h"
@@ -11,7 +11,8 @@ typedef struct point {
off_t out; // offset in uncompressed data
off_t in; // offset in compressed file of first full byte
int bits; // 0, or number of bits (1-7) from byte at in-1
- unsigned char window[32768]; // preceding 32K of uncompressed data
+ unsigned dict; // number of bytes in window to use as a dictionary
+ unsigned char *window; // preceding 32K (or less) of uncompressed data
} point_t;
// Access point list.
@@ -20,6 +21,7 @@ struct deflate_index {
int mode; // -15 for raw, 15 for zlib, or 31 for gzip
off_t length; // total length of uncompressed data
point_t *list; // allocated list of access points
+ z_stream strm; // re-usable inflate engine for extraction
};
// Make one pass through a zlib, gzip, or raw deflate compressed stream and
@@ -30,7 +32,7 @@ struct deflate_index {
// the number of access points on success (>= 1), Z_MEM_ERROR for out of
// memory, Z_BUF_ERROR for a premature end of input, Z_DATA_ERROR for a format
// or verification error in the input file, or Z_ERRNO for a file read error.
-// On success, *built points to the resulting index.
+// On success, *built points to the resulting index, otherwise it's NULL.
int deflate_index_build(FILE *in, off_t span, struct deflate_index **built);
// Use the index to read len bytes from offset into buf. Return the number of