aboutsummaryrefslogtreecommitdiff
path: root/zlib/examples
diff options
context:
space:
mode:
authorNick Clifton <nickc@redhat.com>2022-04-12 16:24:10 +0100
committerNick Clifton <nickc@redhat.com>2022-04-12 16:24:10 +0100
commit8e6b35366073a1a71df805061ecf016cc915a9f9 (patch)
tree6ddbc8123ab4bdbcd3fba1a4c6e0f1091f66964e /zlib/examples
parent63e0ee15a327b933a9e325908f6e5b334106290c (diff)
downloadbinutils-8e6b35366073a1a71df805061ecf016cc915a9f9.zip
binutils-8e6b35366073a1a71df805061ecf016cc915a9f9.tar.gz
binutils-8e6b35366073a1a71df805061ecf016cc915a9f9.tar.bz2
Rebase the zlib sources to the 1.2.12 release
Diffstat (limited to 'zlib/examples')
-rw-r--r--zlib/examples/README.examples5
-rw-r--r--zlib/examples/enough.c745
-rw-r--r--zlib/examples/gzappend.c2
-rw-r--r--zlib/examples/gzlog.c6
-rw-r--r--zlib/examples/gznorm.c470
-rw-r--r--zlib/examples/zran.c256
-rw-r--r--zlib/examples/zran.h40
7 files changed, 1068 insertions, 456 deletions
diff --git a/zlib/examples/README.examples b/zlib/examples/README.examples
index 56a3171..e3a4b88 100644
--- a/zlib/examples/README.examples
+++ b/zlib/examples/README.examples
@@ -34,6 +34,10 @@ gzlog.h
and deflateSetDictionary()
- illustrates use of a gzip header extra field
+gznorm.c
+ normalize a gzip file by combining members into a single member
+ - demonstrates how to concatenate deflate streams using Z_BLOCK
+
zlib_how.html
painfully comprehensive description of zpipe.c (see below)
- describes in excruciating detail the use of deflate() and inflate()
@@ -44,6 +48,7 @@ zpipe.c
- deeply commented in zlib_how.html (see above)
zran.c
+zran.h
index a zlib or gzip stream and randomly access it
- illustrates the use of Z_BLOCK, inflatePrime(), and
inflateSetDictionary() to provide random access
diff --git a/zlib/examples/enough.c b/zlib/examples/enough.c
index b991144..19cf08c 100644
--- a/zlib/examples/enough.c
+++ b/zlib/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 Huffman codes, subject to a length limit.
- * Copyright (C) 2007, 2008, 2012 Mark Adler
- * Version 1.4 18 August 2012 Mark Adler
+ * 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
*/
/* Version history:
@@ -17,101 +17,107 @@
1.4 18 Aug 2012 Avoid shifts more than bits in type (caused endless loop!)
Clean up comparisons of different types
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
*/
/*
- Examine all possible Huffman codes for a given number of symbols and a
- maximum code length in bits to determine the maximum table size for zilb's
- inflate. Only complete Huffman codes are counted.
+ Examine all possible prefix codes for a given number of symbols and a
+ maximum code length in bits to determine the maximum table size for zlib's
+ inflate. Only complete prefix codes are counted.
Two codes are considered distinct if the vectors of the number of codes per
- length are not identical. So permutations of the symbol assignments result
+ length are not identical. So permutations of the symbol assignments result
in the same code for the counting, as do permutations of the assignments of
the bit values to the codes (i.e. only canonical codes are counted).
We build a code from shorter to longer lengths, determining how many symbols
- are coded at each length. At each step, we have how many symbols remain to
+ are coded at each length. At each step, we have how many symbols remain to
be coded, what the last code length used was, and how many bit patterns of
that length remain unused. Then we add one to the code length and double the
- number of unused patterns to graduate to the next code length. We then
+ number of unused patterns to graduate to the next code length. We then
assign all portions of the remaining symbols to that code length that
- preserve the properties of a correct and eventually complete code. Those
+ preserve the properties of a correct and eventually complete code. Those
properties are: we cannot use more bit patterns than are available; and when
- all the symbols are used, there are exactly zero possible bit patterns
- remaining.
+ all the symbols are used, there are exactly zero possible bit patterns left
+ unused.
The inflate Huffman decoding algorithm uses two-level lookup tables for
- speed. There is a single first-level table to decode codes up to root bits
- in length (root == 9 in the current inflate implementation). The table
- has 1 << root entries and is indexed by the next root bits of input. Codes
- shorter than root bits have replicated table entries, so that the correct
- entry is pointed to regardless of the bits that follow the short code. If
- the code is longer than root bits, then the table entry points to a second-
- level table. The size of that table is determined by the longest code with
- that root-bit prefix. If that longest code has length len, then the table
- has size 1 << (len - root), to index the remaining bits in that set of
- codes. Each subsequent root-bit prefix then has its own sub-table. The
- total number of table entries required by the code is calculated
- incrementally as the number of codes at each bit length is populated. When
- all of the codes are shorter than root bits, then root is reduced to the
- longest code length, resulting in a single, smaller, one-level table.
+ speed. There is a single first-level table to decode codes up to root bits
+ in length (root == 9 for literal/length codes and root == 6 for distance
+ codes, in the current inflate implementation). The base table has 1 << root
+ entries and is indexed by the next root bits of input. Codes shorter than
+ root bits have replicated table entries, so that the correct entry is
+ pointed to regardless of the bits that follow the short code. If the code is
+ longer than root bits, then the table entry points to a second-level table.
+ The size of that table is determined by the longest code with that root-bit
+ prefix. If that longest code has length len, then the table has size 1 <<
+ (len - root), to index the remaining bits in that set of codes. Each
+ subsequent root-bit prefix then has its own sub-table. The total number of
+ table entries required by the code is calculated incrementally as the number
+ of codes at each bit length is populated. When all of the codes are shorter
+ than root bits, then root is reduced to the longest code length, resulting
+ in a single, smaller, one-level table.
The inflate algorithm also provides for small values of root (relative to
the log2 of the number of symbols), where the shortest code has more bits
- than root. In that case, root is increased to the length of the shortest
- code. This program, by design, does not handle that case, so it is verified
- that the number of symbols is less than 2^(root + 1).
+ than root. In that case, root is increased to the length of the shortest
+ code. This program, by design, does not handle that case, so it is verified
+ that the number of symbols is less than 1 << (root + 1).
In order to speed up the examination (by about ten orders of magnitude for
the default arguments), the intermediate states in the build-up of a code
- are remembered and previously visited branches are pruned. The memory
+ are remembered and previously visited branches are pruned. The memory
required for this will increase rapidly with the total number of symbols and
- the maximum code length in bits. However this is a very small price to pay
+ the maximum code length in bits. However this is a very small price to pay
for the vast speedup.
- First, all of the possible Huffman codes are counted, and reachable
+ First, all of the possible prefix codes are counted, and reachable
intermediate states are noted by a non-zero count in a saved-results array.
Second, the intermediate states that lead to (root + 1) bit or longer codes
are used to look at all sub-codes from those junctures for their inflate
- memory usage. (The amount of memory used is not affected by the number of
+ memory usage. (The amount of memory used is not affected by the number of
codes of root bits or less in length.) Third, the visited states in the
construction of those sub-codes and the associated calculation of the table
size is recalled in order to avoid recalculating from the same juncture.
Beginning the code examination at (root + 1) bit codes, which is enabled by
identifying the reachable nodes, accounts for about six of the orders of
- magnitude of improvement for the default arguments. About another four
- orders of magnitude come from not revisiting previous states. Out of
- approximately 2x10^16 possible Huffman codes, only about 2x10^6 sub-codes
+ magnitude of improvement for the default arguments. About another four
+ orders of magnitude come from not revisiting previous states. Out of
+ approximately 2x10^16 possible prefix codes, only about 2x10^6 sub-codes
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 an unsigned long long type 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 unsigned long long arithmetic in that case. This
- program will abort if an overflow occurs. The big_t type identifies where
- the counting takes place.
-
- An 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.
+ Note that the uintmax_t type 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.
*/
#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 unsigned long long big_t; /* type for code counting */
-typedef unsigned long long code_t; /* type for bit pattern counting */
-struct tab { /* type for been here check */
- size_t len; /* length of bit vector in char's */
- char *vec; /* allocated bit vector */
+// 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
+struct tab { // type for been-here check
+ size_t len; // allocated length of bit vector in octets
+ char *vec; // allocated bit vector
};
/* The array for saving results, num[], is indexed with this triplet:
@@ -126,447 +132,466 @@ struct tab { /* type for been here check */
left: 2..syms - 1, but only the evens (so syms == 8 -> 2, 4, 6)
len: 1..max - 1 (max == maximum code length in bits)
- syms == 2 is not saved since that immediately leads to a single code. left
+ syms == 2 is not saved since that immediately leads to a single code. left
must be even, since it represents the number of available bit patterns at
- the current length, which is double the number at the previous length.
- left ends at syms-1 since left == syms immediately results in a single code.
+ the current length, which is double the number at the previous length. left
+ ends at syms-1 since left == syms immediately results in a single code.
(left > sym is not allowed since that would result in an incomplete code.)
len is less than max, since the code completes immediately when len == max.
- The offset into the array is calculated for the three indices with the
- first one (syms) being outermost, and the last one (len) being innermost.
- We build the array with length max-1 lists for the len index, with syms-3
- of those for each symbol. There are totsym-2 of those, with each one
- varying in length as a function of sym. See the calculation of index in
- count() for the index, and the calculation of size in main() for the size
- of the array.
+ The offset into the array is calculated for the three indices with the first
+ one (syms) being outermost, and the last one (len) being innermost. We build
+ the array with length max-1 lists for the len index, with syms-3 of those
+ for each symbol. There are totsym-2 of those, with each one varying in
+ length as a function of sym. See the calculation of index in map() for the
+ index, and the calculation of size in main() for the size of the array.
For the deflate example of 286 symbols limited to 15-bit codes, the array
- has 284,284 entries, taking up 2.17 MB for an 8-byte big_t. More than
- half of the space allocated for saved results is actually used -- not all
- possible triplets are reached in the generation of valid Huffman codes.
+ has 284,284 entries, taking up 2.17 MB for an 8-byte big_t. More than half
+ of the space allocated for saved results is actually used -- not all
+ possible triplets are reached in the generation of valid prefix codes.
*/
/* The array for tracking visited states, done[], is itself indexed identically
to the num[] array as described above for the (syms, left, len) triplet.
Each element in the array is further indexed by the (mem, rem) doublet,
where mem is the amount of inflate table space used so far, and rem is the
- remaining unused entries in the current inflate sub-table. Each indexed
+ remaining unused entries in the current inflate sub-table. Each indexed
element is simply one bit indicating whether the state has been visited or
- not. Since the ranges for mem and rem are not known a priori, each bit
+ not. Since the ranges for mem and rem are not known a priori, each bit
vector is of a variable size, and grows as needed to accommodate the visited
- states. mem and rem are used to calculate a single index in a triangular
- array. Since the range of mem is expected in the default case to be about
+ states. mem and rem are used to calculate a single index in a triangular
+ array. Since the range of mem is expected in the default case to be about
ten times larger than the range of rem, the array is skewed to reduce the
- memory usage, with eight times the range for mem than for rem. See the
- calculations for offset and bit in beenhere() for the details.
+ memory usage, with eight times the range for mem than for rem. See the
+ calculations for offset and bit in been_here() for the details.
For the deflate example of 286 symbols limited to 15-bit codes, the bit
- vectors grow to total approximately 21 MB, in addition to the 4.3 MB done[]
- array itself.
+ vectors grow to total 5.5 MB, in addition to the 4.3 MB done array itself.
*/
-/* Globals to avoid propagating constants or constant pointers recursively */
-local int max; /* maximum allowed bit length for the codes */
-local int root; /* size of base code table in bits */
-local int large; /* largest code table so far */
-local size_t size; /* number of elements in num and done */
-local int *code; /* number of symbols assigned to each bit length */
-local big_t *num; /* saved results array for code counting */
-local struct tab *done; /* states already evaluated array */
-
-/* Index function for num[] and done[] */
-#define INDEX(i,j,k) (((size_t)((i-1)>>1)*((i-2)>>1)+(j>>1)-1)*(max-1)+k-1)
-
-/* Free allocated space. Uses globals code, num, and done. */
-local void cleanup(void)
-{
- size_t n;
-
- if (done != NULL) {
- for (n = 0; n < size; n++)
- if (done[n].len)
- free(done[n].vec);
- free(done);
+// Type for a variable-length, allocated string.
+typedef struct {
+ char *str; // pointer to allocated string
+ size_t size; // size of allocation
+ size_t len; // length of string, not including terminating zero
+} string_t;
+
+// Clear a string_t.
+local void string_clear(string_t *s) {
+ s->str[0] = 0;
+ s->len = 0;
+}
+
+// Initialize a string_t.
+local void string_init(string_t *s) {
+ s->size = 16;
+ s->str = malloc(s->size);
+ assert(s->str != NULL && "out of memory");
+ string_clear(s);
+}
+
+// Release the allocation of a string_t.
+local void string_free(string_t *s) {
+ free(s->str);
+ s->str = NULL;
+ s->size = 0;
+ s->len = 0;
+}
+
+// Save the results of printf with fmt and the subsequent argument list to s.
+// Each call appends to s. The allocated space for s is increased as needed.
+local void string_printf(string_t *s, char *fmt, ...) {
+ va_list ap;
+ va_start(ap, fmt);
+ size_t len = s->len;
+ int ret = vsnprintf(s->str + len, s->size - len, fmt, ap);
+ assert(ret >= 0 && "out of memory");
+ s->len += ret;
+ if (s->size < s->len + 1) {
+ do {
+ s->size <<= 1;
+ assert(s->size != 0 && "overflow");
+ } while (s->size < s->len + 1);
+ s->str = realloc(s->str, s->size);
+ assert(s->str != NULL && "out of memory");
+ vsnprintf(s->str + len, s->size - len, fmt, ap);
}
- if (num != NULL)
- free(num);
- if (code != NULL)
- free(code);
+ va_end(ap);
}
-/* Return the number of possible Huffman codes using bit patterns of lengths
- len through max inclusive, coding syms symbols, with left bit patterns of
- length len unused -- return -1 if there is an overflow in the counting.
- Keep a record of previous results in num to prevent repeating the same
- calculation. Uses the globals max and num. */
-local big_t count(int syms, int len, int left)
-{
- big_t sum; /* number of possible codes from this juncture */
- big_t got; /* value returned from count() */
- int least; /* least number of syms to use at this juncture */
- int most; /* most number of syms to use at this juncture */
- int use; /* number of bit patterns to use in next call */
- size_t index; /* index of this case in *num */
-
- /* see if only one possible code */
+// Globals to avoid propagating constants or constant pointers recursively.
+struct {
+ int max; // maximum allowed bit length for the codes
+ int root; // size of base code table in bits
+ int large; // largest code table so far
+ size_t size; // number of elements in num and done
+ big_t tot; // total number of codes with maximum tables size
+ string_t out; // display of subcodes for maximum tables size
+ int *code; // number of symbols assigned to each bit length
+ big_t *num; // saved results array for code counting
+ struct tab *done; // states already evaluated array
+} g;
+
+// Index function for num[] and done[].
+local inline size_t map(int syms, int left, int len) {
+ return ((size_t)((syms - 1) >> 1) * ((syms - 2) >> 1) +
+ (left >> 1) - 1) * (g.max - 1) +
+ len - 1;
+}
+
+// Free allocated space in globals.
+local void cleanup(void) {
+ if (g.done != NULL) {
+ for (size_t n = 0; n < g.size; n++)
+ if (g.done[n].len)
+ free(g.done[n].vec);
+ g.size = 0;
+ free(g.done); g.done = NULL;
+ }
+ free(g.num); g.num = NULL;
+ free(g.code); g.code = NULL;
+ string_free(&g.out);
+}
+
+// Return the number of possible prefix codes using bit patterns of lengths len
+// through max inclusive, coding syms symbols, with left bit patterns of length
+// len unused -- return -1 if there is an overflow in the counting. Keep a
+// record of previous results in num to prevent repeating the same calculation.
+local big_t count(int syms, int left, int len) {
+ // see if only one possible code
if (syms == left)
return 1;
- /* note and verify the expected state */
- assert(syms > left && left > 0 && len < max);
+ // note and verify the expected state
+ assert(syms > left && left > 0 && len < g.max);
- /* see if we've done this one already */
- index = INDEX(syms, left, len);
- got = num[index];
+ // see if we've done this one already
+ size_t index = map(syms, left, len);
+ big_t got = g.num[index];
if (got)
- return got; /* we have -- return the saved result */
+ return got; // we have -- return the saved result
- /* we need to use at least this many bit patterns so that the code won't be
- incomplete at the next length (more bit patterns than symbols) */
- least = (left << 1) - syms;
+ // we need to use at least this many bit patterns so that the code won't be
+ // incomplete at the next length (more bit patterns than symbols)
+ int least = (left << 1) - syms;
if (least < 0)
least = 0;
- /* we can use at most this many bit patterns, lest there not be enough
- available for the remaining symbols at the maximum length (if there were
- no limit to the code length, this would become: most = left - 1) */
- most = (((code_t)left << (max - len)) - syms) /
- (((code_t)1 << (max - len)) - 1);
+ // we can use at most this many bit patterns, lest there not be enough
+ // available for the remaining symbols at the maximum length (if there were
+ // no limit to the code length, this would become: most = left - 1)
+ int most = (((code_t)left << (g.max - len)) - syms) /
+ (((code_t)1 << (g.max - len)) - 1);
- /* count all possible codes from this juncture and add them up */
- sum = 0;
- for (use = least; use <= most; use++) {
- got = count(syms - use, len + 1, (left - use) << 1);
+ // count all possible codes from this juncture and add them up
+ big_t sum = 0;
+ for (int use = least; use <= most; use++) {
+ got = count(syms - use, (left - use) << 1, len + 1);
sum += got;
- if (got == (big_t)0 - 1 || sum < got) /* overflow */
- return (big_t)0 - 1;
+ if (got == (big_t)-1 || sum < got) // overflow
+ return (big_t)-1;
}
- /* verify that all recursive calls are productive */
+ // verify that all recursive calls are productive
assert(sum != 0);
- /* save the result and return it */
- num[index] = sum;
+ // save the result and return it
+ g.num[index] = sum;
return sum;
}
-/* Return true if we've been here before, set to true if not. Set a bit in a
- bit vector to indicate visiting this state. Each (syms,len,left) state
- has a variable size bit vector indexed by (mem,rem). The bit vector is
- lengthened if needed to allow setting the (mem,rem) bit. */
-local int beenhere(int syms, int len, int left, int mem, int rem)
-{
- size_t index; /* index for this state's bit vector */
- size_t offset; /* offset in this state's bit vector */
- int bit; /* mask for this state's bit */
- size_t length; /* length of the bit vector in bytes */
- char *vector; /* new or enlarged bit vector */
-
- /* point to vector for (syms,left,len), bit in vector for (mem,rem) */
- index = INDEX(syms, left, len);
- mem -= 1 << root;
- offset = (mem >> 3) + rem;
+// Return true if we've been here before, set to true if not. Set a bit in a
+// bit vector to indicate visiting this state. Each (syms,len,left) state has a
+// variable size bit vector indexed by (mem,rem). The bit vector is lengthened
+// as needed to allow setting the (mem,rem) bit.
+local int been_here(int syms, int left, int len, int mem, int rem) {
+ // point to vector for (syms,left,len), bit in vector for (mem,rem)
+ size_t index = map(syms, left, len);
+ mem -= 1 << g.root; // mem always includes the root table
+ mem >>= 1; // mem and rem are always even
+ rem >>= 1;
+ size_t offset = (mem >> 3) + rem;
offset = ((offset * (offset + 1)) >> 1) + rem;
- bit = 1 << (mem & 7);
+ int bit = 1 << (mem & 7);
- /* see if we've been here */
- length = done[index].len;
- if (offset < length && (done[index].vec[offset] & bit) != 0)
- return 1; /* done this! */
+ // see if we've been here
+ size_t length = g.done[index].len;
+ if (offset < length && (g.done[index].vec[offset] & bit) != 0)
+ return 1; // done this!
- /* we haven't been here before -- set the bit to show we have now */
+ // we haven't been here before -- set the bit to show we have now
- /* see if we need to lengthen the vector in order to set the bit */
+ // see if we need to lengthen the vector in order to set the bit
if (length <= offset) {
- /* if we have one already, enlarge it, zero out the appended space */
+ // if we have one already, enlarge it, zero out the appended space
+ char *vector;
if (length) {
do {
length <<= 1;
} while (length <= offset);
- vector = realloc(done[index].vec, length);
- if (vector != NULL)
- memset(vector + done[index].len, 0, length - done[index].len);
+ vector = realloc(g.done[index].vec, length);
+ assert(vector != NULL && "out of memory");
+ memset(vector + g.done[index].len, 0, length - g.done[index].len);
}
- /* otherwise we need to make a new vector and zero it out */
+ // otherwise we need to make a new vector and zero it out
else {
- length = 1 << (len - root);
+ length = 16;
while (length <= offset)
length <<= 1;
- vector = calloc(length, sizeof(char));
- }
-
- /* in either case, bail if we can't get the memory */
- if (vector == NULL) {
- fputs("abort: unable to allocate enough memory\n", stderr);
- cleanup();
- exit(1);
+ vector = calloc(length, 1);
+ assert(vector != NULL && "out of memory");
}
- /* install the new vector */
- done[index].len = length;
- done[index].vec = vector;
+ // install the new vector
+ g.done[index].len = length;
+ g.done[index].vec = vector;
}
- /* set the bit */
- done[index].vec[offset] |= bit;
+ // set the bit
+ g.done[index].vec[offset] |= bit;
return 0;
}
-/* Examine all possible codes from the given node (syms, len, left). Compute
- the amount of memory required to build inflate's decoding tables, where the
- number of code structures used so far is mem, and the number remaining in
- the current sub-table is rem. Uses the globals max, code, root, large, and
- done. */
-local void examine(int syms, int len, int left, int mem, int rem)
-{
- int least; /* least number of syms to use at this juncture */
- int most; /* most number of syms to use at this juncture */
- int use; /* number of bit patterns to use in next call */
-
- /* see if we have a complete code */
+// Examine all possible codes from the given node (syms, len, left). Compute
+// the amount of memory required to build inflate's decoding tables, where the
+// number of code structures used so far is mem, and the number remaining in
+// the current sub-table is rem.
+local void examine(int syms, int left, int len, int mem, int rem) {
+ // see if we have a complete code
if (syms == left) {
- /* set the last code entry */
- code[len] = left;
+ // set the last code entry
+ g.code[len] = left;
- /* complete computation of memory used by this code */
+ // complete computation of memory used by this code
while (rem < left) {
left -= rem;
- rem = 1 << (len - root);
+ rem = 1 << (len - g.root);
mem += rem;
}
assert(rem == left);
- /* if this is a new maximum, show the entries used and the sub-code */
- if (mem > large) {
- large = mem;
- printf("max %d: ", mem);
- for (use = root + 1; use <= max; use++)
- if (code[use])
- printf("%d[%d] ", code[use], use);
- putchar('\n');
- fflush(stdout);
+ // if this is at the maximum, show the sub-code
+ if (mem >= g.large) {
+ // if this is a new maximum, update the maximum and clear out the
+ // printed sub-codes from the previous maximum
+ if (mem > g.large) {
+ g.large = mem;
+ string_clear(&g.out);
+ }
+
+ // compute the starting state for this sub-code
+ syms = 0;
+ left = 1 << g.max;
+ for (int bits = g.max; bits > g.root; bits--) {
+ syms += g.code[bits];
+ left -= g.code[bits];
+ assert((left & 1) == 0);
+ left >>= 1;
+ }
+
+ // print the starting state and the resulting sub-code to g.out
+ string_printf(&g.out, "<%u, %u, %u>:",
+ syms, g.root + 1, ((1 << g.root) - left) << 1);
+ for (int bits = g.root + 1; bits <= g.max; bits++)
+ if (g.code[bits])
+ string_printf(&g.out, " %d[%d]", g.code[bits], bits);
+ string_printf(&g.out, "\n");
}
- /* remove entries as we drop back down in the recursion */
- code[len] = 0;
+ // remove entries as we drop back down in the recursion
+ g.code[len] = 0;
return;
}
- /* prune the tree if we can */
- if (beenhere(syms, len, left, mem, rem))
+ // prune the tree if we can
+ if (been_here(syms, left, len, mem, rem))
return;
- /* we need to use at least this many bit patterns so that the code won't be
- incomplete at the next length (more bit patterns than symbols) */
- least = (left << 1) - syms;
+ // we need to use at least this many bit patterns so that the code won't be
+ // incomplete at the next length (more bit patterns than symbols)
+ int least = (left << 1) - syms;
if (least < 0)
least = 0;
- /* we can use at most this many bit patterns, lest there not be enough
- available for the remaining symbols at the maximum length (if there were
- no limit to the code length, this would become: most = left - 1) */
- most = (((code_t)left << (max - len)) - syms) /
- (((code_t)1 << (max - len)) - 1);
+ // we can use at most this many bit patterns, lest there not be enough
+ // available for the remaining symbols at the maximum length (if there were
+ // no limit to the code length, this would become: most = left - 1)
+ int most = (((code_t)left << (g.max - len)) - syms) /
+ (((code_t)1 << (g.max - len)) - 1);
- /* occupy least table spaces, creating new sub-tables as needed */
- use = least;
+ // occupy least table spaces, creating new sub-tables as needed
+ int use = least;
while (rem < use) {
use -= rem;
- rem = 1 << (len - root);
+ rem = 1 << (len - g.root);
mem += rem;
}
rem -= use;
- /* examine codes from here, updating table space as we go */
+ // examine codes from here, updating table space as we go
for (use = least; use <= most; use++) {
- code[len] = use;
- examine(syms - use, len + 1, (left - use) << 1,
- mem + (rem ? 1 << (len - root) : 0), rem << 1);
+ g.code[len] = use;
+ examine(syms - use, (left - use) << 1, len + 1,
+ mem + (rem ? 1 << (len - g.root) : 0), rem << 1);
if (rem == 0) {
- rem = 1 << (len - root);
+ rem = 1 << (len - g.root);
mem += rem;
}
rem--;
}
- /* remove entries as we drop back down in the recursion */
- code[len] = 0;
+ // remove entries as we drop back down in the recursion
+ g.code[len] = 0;
}
-/* Look at all sub-codes starting with root + 1 bits. Look at only the valid
- intermediate code states (syms, left, len). For each completed code,
- calculate the amount of memory required by inflate to build the decoding
- tables. Find the maximum amount of memory required and show the code that
- requires that maximum. Uses the globals max, root, and num. */
-local void enough(int syms)
-{
- int n; /* number of remaing symbols for this node */
- int left; /* number of unused bit patterns at this length */
- size_t index; /* index of this case in *num */
-
- /* clear code */
- for (n = 0; n <= max; n++)
- code[n] = 0;
-
- /* look at all (root + 1) bit and longer codes */
- large = 1 << root; /* base table */
- if (root < max) /* otherwise, there's only a base table */
- for (n = 3; n <= syms; n++)
- for (left = 2; left < n; left += 2)
- {
- /* look at all reachable (root + 1) bit nodes, and the
- resulting codes (complete at root + 2 or more) */
- index = INDEX(n, left, root + 1);
- if (root + 1 < max && num[index]) /* reachable node */
- examine(n, root + 1, left, 1 << root, 0);
-
- /* also look at root bit codes with completions at root + 1
- bits (not saved in num, since complete), just in case */
- if (num[index - 1] && n <= left << 1)
- examine((n - left) << 1, root + 1, (n - left) << 1,
- 1 << root, 0);
+// Look at all sub-codes starting with root + 1 bits. Look at only the valid
+// intermediate code states (syms, left, len). For each completed code,
+// calculate the amount of memory required by inflate to build the decoding
+// tables. Find the maximum amount of memory required and show the codes that
+// require that maximum.
+local void enough(int syms) {
+ // clear code
+ for (int n = 0; n <= g.max; n++)
+ g.code[n] = 0;
+
+ // look at all (root + 1) bit and longer codes
+ string_clear(&g.out); // empty saved results
+ g.large = 1 << g.root; // base table
+ if (g.root < g.max) // otherwise, there's only a base table
+ for (int n = 3; n <= syms; n++)
+ for (int left = 2; left < n; left += 2) {
+ // look at all reachable (root + 1) bit nodes, and the
+ // resulting codes (complete at root + 2 or more)
+ size_t index = map(n, left, g.root + 1);
+ if (g.root + 1 < g.max && g.num[index]) // reachable node
+ examine(n, left, g.root + 1, 1 << g.root, 0);
+
+ // also look at root bit codes with completions at root + 1
+ // bits (not saved in num, since complete), just in case
+ if (g.num[index - 1] && n <= left << 1)
+ examine((n - left) << 1, (n - left) << 1, g.root + 1,
+ 1 << g.root, 0);
}
- /* done */
- printf("done: maximum of %d table entries\n", large);
+ // done
+ printf("maximum of %d table entries for root = %d\n", g.large, g.root);
+ fputs(g.out.str, stdout);
}
-/*
- Examine and show the total number of possible Huffman codes for a given
- maximum number of symbols, initial root table size, and maximum code length
- in bits -- those are the command arguments in that order. The default
- values are 286, 9, and 15 respectively, for the deflate literal/length code.
- The possible codes are counted for each number of coded symbols from two to
- the maximum. The counts for each of those and the total number of codes are
- shown. The maximum number of inflate table entires is then calculated
- across all possible codes. Each new maximum number of table entries and the
- associated sub-code (starting at root + 1 == 10 bits) is shown.
-
- To count and examine Huffman codes that are not length-limited, provide a
- maximum length equal to the number of symbols minus one.
-
- For the deflate literal/length code, use "enough". For the deflate distance
- code, use "enough 30 6".
-
- This uses the %llu printf format to print big_t numbers, which assumes that
- big_t is an unsigned long long. If the big_t type is changed (for example
- to a multiple precision type), the method of printing will also need to be
- updated.
- */
-int main(int argc, char **argv)
-{
- int syms; /* total number of symbols to code */
- int n; /* number of symbols to code for this run */
- big_t got; /* return value of count() */
- big_t sum; /* accumulated number of codes over n */
- code_t word; /* for counting bits in code_t */
-
- /* set up globals for cleanup() */
- code = NULL;
- num = NULL;
- done = NULL;
-
- /* get arguments -- default to the deflate literal/length code */
- syms = 286;
- root = 9;
- max = 15;
+// Examine and show the total number of possible prefix codes for a given
+// maximum number of symbols, initial root table size, and maximum code length
+// in bits -- those are the command arguments in that order. The default values
+// are 286, 9, and 15 respectively, for the deflate literal/length code. The
+// possible codes are counted for each number of coded symbols from two to the
+// maximum. The counts for each of those and the total number of codes are
+// shown. The maximum number of inflate table entires is then calculated across
+// all possible codes. Each new maximum number of table entries and the
+// associated sub-code (starting at root + 1 == 10 bits) is shown.
+//
+// To count and examine prefix codes that are not length-limited, provide a
+// maximum length equal to the number of symbols minus one.
+//
+// For the deflate literal/length code, use "enough". For the deflate distance
+// code, use "enough 30 6".
+int main(int argc, char **argv) {
+ // set up globals for cleanup()
+ g.code = NULL;
+ g.num = NULL;
+ g.done = NULL;
+ string_init(&g.out);
+
+ // get arguments -- default to the deflate literal/length code
+ int syms = 286;
+ g.root = 9;
+ g.max = 15;
if (argc > 1) {
syms = atoi(argv[1]);
if (argc > 2) {
- root = atoi(argv[2]);
+ g.root = atoi(argv[2]);
if (argc > 3)
- max = atoi(argv[3]);
+ g.max = atoi(argv[3]);
}
}
- if (argc > 4 || syms < 2 || root < 1 || max < 1) {
+ if (argc > 4 || syms < 2 || g.root < 1 || g.max < 1) {
fputs("invalid arguments, need: [sym >= 2 [root >= 1 [max >= 1]]]\n",
stderr);
return 1;
}
- /* if not restricting the code length, the longest is syms - 1 */
- if (max > syms - 1)
- max = syms - 1;
+ // if not restricting the code length, the longest is syms - 1
+ if (g.max > syms - 1)
+ g.max = syms - 1;
- /* determine the number of bits in a code_t */
- for (n = 0, word = 1; word; n++, word <<= 1)
- ;
+ // determine the number of bits in a code_t
+ int bits = 0;
+ for (code_t word = 1; word; word <<= 1)
+ bits++;
- /* make sure that the calculation of most will not overflow */
- if (max > n || (code_t)(syms - 2) >= (((code_t)0 - 1) >> (max - 1))) {
+ // make sure that the calculation of most will not overflow
+ if (g.max > bits || (code_t)(syms - 2) >= ((code_t)-1 >> (g.max - 1))) {
fputs("abort: code length too long for internal types\n", stderr);
return 1;
}
- /* reject impossible code requests */
- if ((code_t)(syms - 1) > ((code_t)1 << max) - 1) {
+ // reject impossible code requests
+ if ((code_t)(syms - 1) > ((code_t)1 << g.max) - 1) {
fprintf(stderr, "%d symbols cannot be coded in %d bits\n",
- syms, max);
+ syms, g.max);
return 1;
}
- /* allocate code vector */
- code = calloc(max + 1, sizeof(int));
- if (code == NULL) {
- fputs("abort: unable to allocate enough memory\n", stderr);
- return 1;
- }
+ // allocate code vector
+ g.code = calloc(g.max + 1, sizeof(int));
+ assert(g.code != NULL && "out of memory");
- /* determine size of saved results array, checking for overflows,
- allocate and clear the array (set all to zero with calloc()) */
- if (syms == 2) /* iff max == 1 */
- num = NULL; /* won't be saving any results */
+ // determine size of saved results array, checking for overflows,
+ // allocate and clear the array (set all to zero with calloc())
+ if (syms == 2) // iff max == 1
+ g.num = NULL; // won't be saving any results
else {
- size = syms >> 1;
- if (size > ((size_t)0 - 1) / (n = (syms - 1) >> 1) ||
- (size *= n, size > ((size_t)0 - 1) / (n = max - 1)) ||
- (size *= n, size > ((size_t)0 - 1) / sizeof(big_t)) ||
- (num = calloc(size, sizeof(big_t))) == NULL) {
- fputs("abort: unable to allocate enough memory\n", stderr);
- cleanup();
- return 1;
- }
+ g.size = syms >> 1;
+ int n = (syms - 1) >> 1;
+ assert(g.size <= (size_t)-1 / n && "overflow");
+ g.size *= n;
+ n = g.max - 1;
+ assert(g.size <= (size_t)-1 / n && "overflow");
+ g.size *= n;
+ g.num = calloc(g.size, sizeof(big_t));
+ assert(g.num != NULL && "out of memory");
}
- /* count possible codes for all numbers of symbols, add up counts */
- sum = 0;
- for (n = 2; n <= syms; n++) {
- got = count(n, 1, 2);
+ // count possible codes for all numbers of symbols, add up counts
+ big_t sum = 0;
+ for (int n = 2; n <= syms; n++) {
+ big_t got = count(n, 2, 1);
sum += got;
- if (got == (big_t)0 - 1 || sum < got) { /* overflow */
- fputs("abort: can't count that high!\n", stderr);
- cleanup();
- return 1;
- }
- printf("%llu %d-codes\n", got, n);
+ assert(got != (big_t)-1 && sum >= got && "overflow");
}
- printf("%llu total codes for 2 to %d symbols", sum, syms);
- if (max < syms - 1)
- printf(" (%d-bit length limit)\n", max);
+ printf("%"PRIbig" total codes for 2 to %d symbols", sum, syms);
+ if (g.max < syms - 1)
+ printf(" (%d-bit length limit)\n", g.max);
else
puts(" (no length limit)");
- /* allocate and clear done array for beenhere() */
+ // allocate and clear done array for been_here()
if (syms == 2)
- done = NULL;
- else if (size > ((size_t)0 - 1) / sizeof(struct tab) ||
- (done = calloc(size, sizeof(struct tab))) == NULL) {
- fputs("abort: unable to allocate enough memory\n", stderr);
- cleanup();
- return 1;
+ g.done = NULL;
+ else {
+ g.done = calloc(g.size, sizeof(struct tab));
+ assert(g.done != NULL && "out of memory");
}
- /* find and show maximum inflate table usage */
- if (root > max) /* reduce root to max length */
- root = max;
- if ((code_t)syms < ((code_t)1 << (root + 1)))
+ // find and show maximum inflate table usage
+ if (g.root > g.max) // reduce root to max length
+ g.root = g.max;
+ if ((code_t)syms < ((code_t)1 << (g.root + 1)))
enough(syms);
else
- puts("cannot handle minimum code lengths > root");
+ fputs("cannot handle minimum code lengths > root", stderr);
- /* done */
+ // done
cleanup();
return 0;
}
diff --git a/zlib/examples/gzappend.c b/zlib/examples/gzappend.c
index 662dec3..d7eea3e 100644
--- a/zlib/examples/gzappend.c
+++ b/zlib/examples/gzappend.c
@@ -137,7 +137,7 @@ local void rotate(unsigned char *list, unsigned len, unsigned rot)
/* do simple left shift by one */
if (rot == 1) {
tmp = *list;
- memcpy(list, list + 1, len - 1);
+ memmove(list, list + 1, len - 1);
*last = tmp;
return;
}
diff --git a/zlib/examples/gzlog.c b/zlib/examples/gzlog.c
index b8c2927..b977802 100644
--- a/zlib/examples/gzlog.c
+++ b/zlib/examples/gzlog.c
@@ -1,8 +1,8 @@
/*
* gzlog.c
- * Copyright (C) 2004, 2008, 2012, 2016 Mark Adler, all rights reserved
+ * Copyright (C) 2004, 2008, 2012, 2016, 2019 Mark Adler, all rights reserved
* For conditions of distribution and use, see copyright notice in gzlog.h
- * version 2.2, 14 Aug 2012
+ * version 2.3, 25 May 2019
*/
/*
@@ -756,12 +756,14 @@ local int log_recover(struct log *log, int op)
return -2;
}
if ((fd = open(log->path, O_RDONLY, 0)) < 0) {
+ free(data);
log_log(log, op, ".add file read failure");
return -1;
}
ret = (size_t)read(fd, data, len) != len;
close(fd);
if (ret) {
+ free(data);
log_log(log, op, ".add file read failure");
return -1;
}
diff --git a/zlib/examples/gznorm.c b/zlib/examples/gznorm.c
new file mode 100644
index 0000000..68e0a0f
--- /dev/null
+++ b/zlib/examples/gznorm.c
@@ -0,0 +1,470 @@
+/* gznorm.c -- normalize a gzip stream
+ * Copyright (C) 2018 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ * Version 1.0 7 Oct 2018 Mark Adler */
+
+// gznorm takes a gzip stream, potentially containing multiple members, and
+// converts it to a gzip stream with a single member. In addition the gzip
+// header is normalized, removing the file name and time stamp, and setting the
+// other header contents (XFL, OS) to fixed values. gznorm does not recompress
+// the data, so it is fast, but no advantage is gained from the history that
+// could be available across member boundaries.
+
+#include <stdio.h> // fread, fwrite, putc, fflush, ferror, fprintf,
+ // vsnprintf, stdout, stderr, NULL, FILE
+#include <stdlib.h> // malloc, free
+#include <string.h> // strerror
+#include <errno.h> // errno
+#include <stdarg.h> // va_list, va_start, va_end
+#include "zlib.h" // inflateInit2, inflate, inflateReset, inflateEnd,
+ // z_stream, z_off_t, crc32_combine, Z_NULL, Z_BLOCK,
+ // Z_OK, Z_STREAM_END, Z_BUF_ERROR, Z_DATA_ERROR,
+ // Z_MEM_ERROR
+
+#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)
+#else
+# define SET_BINARY_MODE(file)
+#endif
+
+#define local static
+
+// printf to an allocated string. Return the string, or NULL if the printf or
+// allocation fails.
+local char *aprintf(char *fmt, ...) {
+ // Get the length of the result of the printf.
+ va_list args;
+ va_start(args, fmt);
+ int len = vsnprintf(NULL, 0, fmt, args);
+ va_end(args);
+ if (len < 0)
+ return NULL;
+
+ // Allocate the required space and printf to it.
+ char *str = malloc(len + 1);
+ if (str == NULL)
+ return NULL;
+ va_start(args, fmt);
+ vsnprintf(str, len + 1, fmt, args);
+ va_end(args);
+ return str;
+}
+
+// Return with an error, putting an allocated error message in *err. Doing an
+// inflateEnd() on an already ended state, or one with state set to Z_NULL, is
+// permitted.
+#define BYE(...) \
+ do { \
+ inflateEnd(&strm); \
+ *err = aprintf(__VA_ARGS__); \
+ return 1; \
+ } while (0)
+
+// Chunk size for buffered reads and for decompression. Twice this many bytes
+// will be allocated on the stack by gzip_normalize(). Must fit in an unsigned.
+#define CHUNK 16384
+
+// Read a gzip stream from in and write an equivalent normalized gzip stream to
+// out. If given no input, an empty gzip stream will be written. If successful,
+// 0 is returned, and *err is set to NULL. On error, 1 is returned, where the
+// details of the error are returned in *err, a pointer to an allocated string.
+//
+// The input may be a stream with multiple gzip members, which is converted to
+// a single gzip member on the output. Each gzip member is decompressed at the
+// level of deflate blocks. This enables clearing the last-block bit, shifting
+// the compressed data to concatenate to the previous member's compressed data,
+// which can end at an arbitrary bit boundary, and identifying stored blocks in
+// order to resynchronize those to byte boundaries. The deflate compressed data
+// is terminated with a 10-bit empty fixed block. If any members on the input
+// end with a 10-bit empty fixed block, then that block is excised from the
+// stream. This avoids appending empty fixed blocks for every normalization,
+// and assures that gzip_normalize applied a second time will not change the
+// input. The pad bits after stored block headers and after the final deflate
+// block are all forced to zeros.
+local int gzip_normalize(FILE *in, FILE *out, char **err) {
+ // initialize the inflate engine to process a gzip member
+ z_stream strm;
+ strm.zalloc = Z_NULL;
+ strm.zfree = Z_NULL;
+ strm.opaque = Z_NULL;
+ strm.avail_in = 0;
+ strm.next_in = Z_NULL;
+ if (inflateInit2(&strm, 15 + 16) != Z_OK)
+ BYE("out of memory");
+
+ // State while processing the input gzip stream.
+ enum { // BETWEEN -> HEAD -> BLOCK -> TAIL -> BETWEEN -> ...
+ BETWEEN, // between gzip members (must end in this state)
+ HEAD, // reading a gzip header
+ BLOCK, // reading deflate blocks
+ TAIL // reading a gzip trailer
+ } state = BETWEEN; // current component being processed
+ unsigned long crc = 0; // accumulated CRC of uncompressed data
+ unsigned long len = 0; // accumulated length of uncompressed data
+ unsigned long buf = 0; // deflate stream bit buffer of num bits
+ int num = 0; // number of bits in buf (at bottom)
+
+ // Write a canonical gzip header (no mod time, file name, comment, extra
+ // block, or extra flags, and OS is marked as unknown).
+ fwrite("\x1f\x8b\x08\0\0\0\0\0\0\xff", 1, 10, out);
+
+ // Process the gzip stream from in until reaching the end of the input,
+ // encountering invalid input, or experiencing an i/o error.
+ int more; // true if not at the end of the input
+ do {
+ // State inside this loop.
+ unsigned char *put; // next input buffer location to process
+ int prev; // number of bits from previous block in
+ // the bit buffer, or -1 if not at the
+ // start of a block
+ unsigned long long memb; // uncompressed length of member
+ size_t tail; // number of trailer bytes read (0..8)
+ unsigned long part; // accumulated trailer component
+
+ // Get the next chunk of input from in.
+ unsigned char dat[CHUNK];
+ strm.avail_in = fread(dat, 1, CHUNK, in);
+ if (strm.avail_in == 0)
+ break;
+ more = strm.avail_in == CHUNK;
+ strm.next_in = put = dat;
+
+ // Run that chunk of input through the inflate engine to exhaustion.
+ do {
+ // At this point it is assured that strm.avail_in > 0.
+
+ // Inflate until the end of a gzip component (header, deflate
+ // block, trailer) is reached, or until all of the chunk is
+ // consumed. The resulting decompressed data is discarded, though
+ // the total size of the decompressed data in each member is
+ // tracked, for the calculation of the total CRC.
+ do {
+ // inflate and handle any errors
+ unsigned char scrap[CHUNK];
+ strm.avail_out = CHUNK;
+ strm.next_out = scrap;
+ int ret = inflate(&strm, Z_BLOCK);
+ if (ret == Z_MEM_ERROR)
+ BYE("out of memory");
+ if (ret == Z_DATA_ERROR)
+ BYE("input invalid: %s", strm.msg);
+ if (ret != Z_OK && ret != Z_BUF_ERROR && ret != Z_STREAM_END)
+ BYE("internal error");
+
+ // Update the number of uncompressed bytes generated in this
+ // member. The actual count (not modulo 2^32) is required to
+ // correctly compute the total CRC.
+ unsigned got = CHUNK - strm.avail_out;
+ memb += got;
+ if (memb < got)
+ BYE("overflow error");
+
+ // Continue to process this chunk until it is consumed, or
+ // until the end of a component (header, deflate block, or
+ // trailer) is reached.
+ } while (strm.avail_out == 0 && (strm.data_type & 0x80) == 0);
+
+ // Since strm.avail_in was > 0 for the inflate call, some input was
+ // just consumed. It is therefore assured that put < strm.next_in.
+
+ // Disposition the consumed component or part of a component.
+ switch (state) {
+ case BETWEEN:
+ state = HEAD;
+ // Fall through to HEAD when some or all of the header is
+ // processed.
+
+ case HEAD:
+ // Discard the header.
+ if (strm.data_type & 0x80) {
+ // End of header reached -- deflate blocks follow.
+ put = strm.next_in;
+ prev = num;
+ memb = 0;
+ state = BLOCK;
+ }
+ break;
+
+ case BLOCK:
+ // Copy the deflate stream to the output, but with the
+ // last-block-bit cleared. Re-synchronize stored block
+ // headers to the output byte boundaries. The bytes at
+ // put..strm.next_in-1 is the compressed data that has been
+ // processed and is ready to be copied to the output.
+
+ // At this point, it is assured that new compressed data is
+ // available, i.e., put < strm.next_in. If prev is -1, then
+ // that compressed data starts in the middle of a deflate
+ // block. If prev is not -1, then the bits in the bit
+ // buffer, possibly combined with the bits in *put, contain
+ // the three-bit header of the new deflate block. In that
+ // case, prev is the number of bits from the previous block
+ // that remain in the bit buffer. Since num is the number
+ // of bits in the bit buffer, we have that num - prev is
+ // the number of bits from the new block currently in the
+ // bit buffer.
+
+ // If strm.data_type & 0xc0 is 0x80, then the last byte of
+ // the available compressed data includes the last bits of
+ // the end of a deflate block. In that case, that last byte
+ // also has strm.data_type & 0x1f bits of the next deflate
+ // block, in the range 0..7. If strm.data_type & 0xc0 is
+ // 0xc0, then the last byte of the compressed data is the
+ // end of the deflate stream, followed by strm.data_type &
+ // 0x1f pad bits, also in the range 0..7.
+
+ // Set bits to the number of bits not yet consumed from the
+ // last byte. If we are at the end of the block, bits is
+ // either the number of bits in the last byte belonging to
+ // the next block, or the number of pad bits after the
+ // final block. In either of those cases, bits is in the
+ // range 0..7.
+ ; // (required due to C syntax oddity)
+ int bits = strm.data_type & 0x1f;
+
+ if (prev != -1) {
+ // We are at the start of a new block. Clear the last
+ // block bit, and check for special cases. If it is a
+ // stored block, then emit the header and pad to the
+ // next byte boundary. If it is a final, empty fixed
+ // block, then excise it.
+
+ // Some or all of the three header bits for this block
+ // may already be in the bit buffer. Load any remaining
+ // header bits into the bit buffer.
+ if (num - prev < 3) {
+ buf += (unsigned long)*put++ << num;
+ num += 8;
+ }
+
+ // Set last to have a 1 in the position of the last
+ // block bit in the bit buffer.
+ unsigned long last = (unsigned long)1 << prev;
+
+ if (((buf >> prev) & 7) == 3) {
+ // This is a final fixed block. Load at least ten
+ // bits from this block, including the header, into
+ // the bit buffer. We already have at least three,
+ // so at most one more byte needs to be loaded.
+ if (num - prev < 10) {
+ if (put == strm.next_in)
+ // Need to go get and process more input.
+ // We'll end up back here to finish this.
+ break;
+ buf += (unsigned long)*put++ << num;
+ num += 8;
+ }
+ if (((buf >> prev) & 0x3ff) == 3) {
+ // That final fixed block is empty. Delete it
+ // to avoid adding an empty block every time a
+ // gzip stream is normalized.
+ num = prev;
+ buf &= last - 1; // zero the pad bits
+ }
+ }
+ else if (((buf >> prev) & 6) == 0) {
+ // This is a stored block. Flush to the next
+ // byte boundary after the three-bit header.
+ num = (prev + 10) & ~7;
+ buf &= last - 1; // zero the pad bits
+ }
+
+ // Clear the last block bit.
+ buf &= ~last;
+
+ // Write out complete bytes in the bit buffer.
+ while (num >= 8) {
+ putc(buf, out);
+ buf >>= 8;
+ num -= 8;
+ }
+
+ // If no more bytes left to process, then we have
+ // consumed the byte that had bits from the next block.
+ if (put == strm.next_in)
+ bits = 0;
+ }
+
+ // We are done handling the deflate block header. Now copy
+ // all or almost all of the remaining compressed data that
+ // has been processed so far. Don't copy one byte at the
+ // end if it contains bits from the next deflate block or
+ // pad bits at the end of a deflate block.
+
+ // mix is 1 if we are at the end of a deflate block, and if
+ // some of the bits in the last byte follow this block. mix
+ // is 0 if we are in the middle of a deflate block, if the
+ // deflate block ended on a byte boundary, or if all of the
+ // compressed data processed so far has been consumed.
+ int mix = (strm.data_type & 0x80) && bits;
+
+ // Copy all of the processed compressed data to the output,
+ // except for the last byte if it contains bits from the
+ // next deflate block or pad bits at the end of the deflate
+ // stream. Copy the data after shifting in num bits from
+ // buf in front of it, leaving num bits from the end of the
+ // compressed data in buf when done.
+ unsigned char *end = strm.next_in - mix;
+ if (put < end) {
+ if (num)
+ // Insert num bits from buf before the data being
+ // copied.
+ do {
+ buf += (unsigned)(*put++) << num;
+ putc(buf, out);
+ buf >>= 8;
+ } while (put < end);
+ else {
+ // No shifting needed -- write directly.
+ fwrite(put, 1, end - put, out);
+ put = end;
+ }
+ }
+
+ // Process the last processed byte if it wasn't written.
+ if (mix) {
+ // Load the last byte into the bit buffer.
+ buf += (unsigned)(*put++) << num;
+ num += 8;
+
+ if (strm.data_type & 0x40) {
+ // We are at the end of the deflate stream and
+ // there are bits pad bits. Discard the pad bits
+ // and write a byte to the output, if available.
+ // Leave the num bits left over in buf to prepend
+ // to the next deflate stream.
+ num -= bits;
+ if (num >= 8) {
+ putc(buf, out);
+ num -= 8;
+ buf >>= 8;
+ }
+
+ // Force the pad bits in the bit buffer to zeros.
+ buf &= ((unsigned long)1 << num) - 1;
+
+ // Don't need to set prev here since going to TAIL.
+ }
+ else
+ // At the end of an internal deflate block. Leave
+ // the last byte in the bit buffer to examine on
+ // the next entry to BLOCK, when more bits from the
+ // next block will be available.
+ prev = num - bits; // number of bits in buffer
+ // from current block
+ }
+
+ // Don't have a byte left over, so we are in the middle of
+ // a deflate block, or the deflate block ended on a byte
+ // boundary. Set prev appropriately for the next entry into
+ // BLOCK.
+ else if (strm.data_type & 0x80)
+ // The block ended on a byte boundary, so no header
+ // bits are in the bit buffer.
+ prev = num;
+ else
+ // In the middle of a deflate block, so no header here.
+ prev = -1;
+
+ // Check for the end of the deflate stream.
+ if ((strm.data_type & 0xc0) == 0xc0) {
+ // That ends the deflate stream on the input side, the
+ // pad bits were discarded, and any remaining bits from
+ // the last block in the stream are saved in the bit
+ // buffer to prepend to the next stream. Process the
+ // gzip trailer next.
+ tail = 0;
+ part = 0;
+ state = TAIL;
+ }
+ break;
+
+ case TAIL:
+ // Accumulate available trailer bytes to update the total
+ // CRC and the total uncompressed length.
+ do {
+ part = (part >> 8) + ((unsigned long)(*put++) << 24);
+ tail++;
+ if (tail == 4) {
+ // Update the total CRC.
+ z_off_t len2 = memb;
+ if (len2 < 0 || (unsigned long long)len2 != memb)
+ BYE("overflow error");
+ crc = crc ? crc32_combine(crc, part, len2) : part;
+ part = 0;
+ }
+ else if (tail == 8) {
+ // Update the total uncompressed length. (It's ok
+ // if this sum is done modulo 2^32.)
+ len += part;
+
+ // At the end of a member. Set up to inflate an
+ // immediately following gzip member. (If we made
+ // it this far, then the trailer was valid.)
+ if (inflateReset(&strm) != Z_OK)
+ BYE("internal error");
+ state = BETWEEN;
+ break;
+ }
+ } while (put < strm.next_in);
+ break;
+ }
+
+ // Process the input buffer until completely consumed.
+ } while (strm.avail_in > 0);
+
+ // Process input until end of file, invalid input, or i/o error.
+ } while (more);
+
+ // Done with the inflate engine.
+ inflateEnd(&strm);
+
+ // Verify the validity of the input.
+ if (state != BETWEEN)
+ BYE("input invalid: incomplete gzip stream");
+
+ // Write the remaining deflate stream bits, followed by a terminating
+ // deflate fixed block.
+ buf += (unsigned long)3 << num;
+ putc(buf, out);
+ putc(buf >> 8, out);
+ if (num > 6)
+ putc(0, out);
+
+ // Write the gzip trailer, which is the CRC and the uncompressed length
+ // modulo 2^32, both in little-endian order.
+ putc(crc, out);
+ putc(crc >> 8, out);
+ putc(crc >> 16, out);
+ putc(crc >> 24, out);
+ putc(len, out);
+ putc(len >> 8, out);
+ putc(len >> 16, out);
+ putc(len >> 24, out);
+ fflush(out);
+
+ // Check for any i/o errors.
+ if (ferror(in) || ferror(out))
+ BYE("i/o error: %s", strerror(errno));
+
+ // All good!
+ *err = NULL;
+ return 0;
+}
+
+// Normalize the gzip stream on stdin, writing the result to stdout.
+int main(void) {
+ // Avoid end-of-line conversions on evil operating systems.
+ SET_BINARY_MODE(stdin);
+ SET_BINARY_MODE(stdout);
+
+ // Normalize from stdin to stdout, returning 1 on error, 0 if ok.
+ char *err;
+ int ret = gzip_normalize(stdin, stdout, &err);
+ if (ret)
+ fprintf(stderr, "gznorm error: %s\n", err);
+ free(err);
+ return ret;
+}
diff --git a/zlib/examples/zran.c b/zlib/examples/zran.c
index 4fec659..f279db7 100644
--- a/zlib/examples/zran.c
+++ b/zlib/examples/zran.c
@@ -1,11 +1,13 @@
/* zran.c -- example of zlib/gzip stream indexing and random access
- * Copyright (C) 2005, 2012 Mark Adler
+ * Copyright (C) 2005, 2012, 2018 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
- Version 1.1 29 Sep 2012 Mark Adler */
+ * Version 1.2 14 Oct 2018 Mark Adler */
/* Version History:
1.0 29 May 2005 First version
1.1 29 Sep 2012 Fix memory reallocation error
+ 1.2 14 Oct 2018 Handle gzip streams with multiple members
+ Add a header file to facilitate usage in applications
*/
/* Illustrate the use of Z_BLOCK, inflatePrime(), and inflateSetDictionary()
@@ -20,11 +22,11 @@
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 referece for locating a desired starting
- point in the uncompressed stream. build_index() works by decompressing the
- input zlib or gzip stream a block at a time, and at the end of each block
- deciding 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.
+ point in the uncompressed stream. deflate_index_build() works by
+ decompressing the input zlib or gzip stream a block at a time, and at the
+ end of each block deciding 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.
@@ -43,7 +45,8 @@
There is some fair bit of overhead to starting inflation for the random
access, mainly copying the 32K byte dictionary. So if small pieces of the
file are being accessed, it would make sense to implement a cache to hold
- some lookahead and avoid many calls to extract() for small lengths.
+ some lookahead and 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 requires
@@ -56,30 +59,21 @@
#include <stdlib.h>
#include <string.h>
#include "zlib.h"
+#include "zran.h"
-#define local static
-
-#define SPAN 1048576L /* desired distance between access points */
#define WINSIZE 32768U /* sliding window size */
#define CHUNK 16384 /* file input buffer size */
-/* access point entry */
+/* Access point entry. */
struct point {
off_t out; /* corresponding offset in uncompressed data */
off_t in; /* offset in input file of first full byte */
- int bits; /* number of bits (1-7) from byte at in - 1, or 0 */
+ int bits; /* number of bits (1-7) from byte at in-1, or 0 */
unsigned char window[WINSIZE]; /* preceding 32K of uncompressed data */
};
-/* access point list */
-struct access {
- int have; /* number of list entries filled in */
- int size; /* number of list entries allocated */
- struct point *list; /* allocated list */
-};
-
-/* Deallocate an index built by build_index() */
-local void free_index(struct access *index)
+/* See comments in zran.h. */
+void deflate_index_free(struct deflate_index *index)
{
if (index != NULL) {
free(index->list);
@@ -87,39 +81,43 @@ local void free_index(struct access *index)
}
}
-/* Add an entry to the access point list. If out of memory, deallocate the
- existing list and return NULL. */
-local struct access *addpoint(struct access *index, int bits,
- off_t in, off_t out, unsigned left, unsigned char *window)
+/* Add an entry to the access point list. If out of memory, deallocate the
+ existing list and return NULL. index->gzip is the allocated size of the
+ index in point entries, until it is time for deflate_index_build() to
+ return, at which point gzip is set to indicate a gzip file or not.
+ */
+static struct deflate_index *addpoint(struct deflate_index *index, int bits,
+ off_t in, off_t out, unsigned left,
+ unsigned char *window)
{
struct point *next;
/* if list is empty, create it (start with eight points) */
if (index == NULL) {
- index = malloc(sizeof(struct access));
+ index = malloc(sizeof(struct deflate_index));
if (index == NULL) return NULL;
index->list = malloc(sizeof(struct point) << 3);
if (index->list == NULL) {
free(index);
return NULL;
}
- index->size = 8;
+ index->gzip = 8;
index->have = 0;
}
/* if list is full, make it bigger */
- else if (index->have == index->size) {
- index->size <<= 1;
- next = realloc(index->list, sizeof(struct point) * index->size);
+ else if (index->have == index->gzip) {
+ index->gzip <<= 1;
+ next = realloc(index->list, sizeof(struct point) * index->gzip);
if (next == NULL) {
- free_index(index);
+ deflate_index_free(index);
return NULL;
}
index->list = next;
}
/* fill in entry and increment how many we have */
- next = index->list + index->have;
+ next = (struct point *)(index->list) + index->have;
next->bits = bits;
next->in = in;
next->out = out;
@@ -133,20 +131,14 @@ local struct access *addpoint(struct access *index, int bits,
return index;
}
-/* Make one entire pass through the compressed stream and build an index, with
- access points about every span bytes of uncompressed output -- span is
- chosen to balance the speed of random access against the memory requirements
- of the list, about 32K bytes per access point. Note that data after the end
- of the first zlib or gzip stream in the file is ignored. build_index()
- returns the number of access points on success (>= 1), Z_MEM_ERROR for out
- of memory, Z_DATA_ERROR for an error in the input file, or Z_ERRNO for a
- file read error. On success, *built points to the resulting index. */
-local int build_index(FILE *in, off_t span, struct access **built)
+/* See comments in zran.h. */
+int deflate_index_build(FILE *in, off_t span, struct deflate_index **built)
{
int ret;
+ int gzip = 0; /* true if reading a gzip file */
off_t totin, totout; /* our own total counters to avoid 4GB limit */
off_t last; /* totout value of last access point */
- struct access *index; /* access points being generated */
+ struct deflate_index *index; /* access points being generated */
z_stream strm;
unsigned char input[CHUNK];
unsigned char window[WINSIZE];
@@ -163,7 +155,7 @@ local int build_index(FILE *in, off_t span, struct access **built)
/* inflate the input, maintain a sliding window, and build an index -- this
also validates the integrity of the compressed data using the check
- information at the end of the gzip or zlib stream */
+ information in the gzip or zlib stream */
totin = totout = last = 0;
index = NULL; /* will be allocated by first addpoint() */
strm.avail_out = 0;
@@ -172,14 +164,19 @@ local int build_index(FILE *in, off_t span, struct access **built)
strm.avail_in = fread(input, 1, CHUNK, in);
if (ferror(in)) {
ret = Z_ERRNO;
- goto build_index_error;
+ goto deflate_index_build_error;
}
if (strm.avail_in == 0) {
ret = Z_DATA_ERROR;
- goto build_index_error;
+ goto deflate_index_build_error;
}
strm.next_in = input;
+ /* check for a gzip stream */
+ if (totin == 0 && strm.avail_in >= 3 &&
+ input[0] == 31 && input[1] == 139 && input[2] == 8)
+ gzip = 1;
+
/* process all of that, or until end of stream */
do {
/* reset sliding window if necessary */
@@ -198,9 +195,17 @@ local int build_index(FILE *in, off_t span, struct access **built)
if (ret == Z_NEED_DICT)
ret = Z_DATA_ERROR;
if (ret == Z_MEM_ERROR || ret == Z_DATA_ERROR)
- goto build_index_error;
- if (ret == Z_STREAM_END)
+ goto deflate_index_build_error;
+ if (ret == Z_STREAM_END) {
+ if (gzip &&
+ (strm.avail_in || ungetc(getc(in), in) != EOF)) {
+ ret = inflateReset(&strm);
+ if (ret != Z_OK)
+ goto deflate_index_build_error;
+ continue;
+ }
break;
+ }
/* if at end of block, consider adding an index entry (note that if
data_type indicates an end-of-block, then all of the
@@ -217,7 +222,7 @@ local int build_index(FILE *in, off_t span, struct access **built)
totout, strm.avail_out, window);
if (index == NULL) {
ret = Z_MEM_ERROR;
- goto build_index_error;
+ goto deflate_index_build_error;
}
last = totout;
}
@@ -227,27 +232,21 @@ local int build_index(FILE *in, off_t span, struct access **built)
/* clean up and return index (release unused entries in list) */
(void)inflateEnd(&strm);
index->list = realloc(index->list, sizeof(struct point) * index->have);
- index->size = index->have;
+ index->gzip = gzip;
+ index->length = totout;
*built = index;
- return index->size;
+ return index->have;
/* return error */
- build_index_error:
+ deflate_index_build_error:
(void)inflateEnd(&strm);
- if (index != NULL)
- free_index(index);
+ deflate_index_free(index);
return ret;
}
-/* Use the index to read len bytes from offset into buf, return bytes read or
- negative for error (Z_DATA_ERROR or Z_MEM_ERROR). If data is requested past
- the end of the uncompressed data, then extract() will return a value less
- than len, indicating how much as actually read into buf. This function
- should not return a data error unless the file was modified since the index
- was generated. extract() may also return Z_ERRNO if there is an error on
- reading or seeking the input file. */
-local int extract(FILE *in, struct access *index, off_t offset,
- unsigned char *buf, int len)
+/* See comments in zran.h. */
+int deflate_index_extract(FILE *in, struct deflate_index *index, off_t offset,
+ unsigned char *buf, int len)
{
int ret, skip;
z_stream strm;
@@ -276,12 +275,12 @@ local int extract(FILE *in, struct access *index, off_t offset,
return ret;
ret = fseeko(in, here->in - (here->bits ? 1 : 0), SEEK_SET);
if (ret == -1)
- goto extract_ret;
+ goto deflate_index_extract_ret;
if (here->bits) {
ret = getc(in);
if (ret == -1) {
ret = ferror(in) ? Z_ERRNO : Z_DATA_ERROR;
- goto extract_ret;
+ goto deflate_index_extract_ret;
}
(void)inflatePrime(&strm, here->bits, ret >> (8 - here->bits));
}
@@ -293,21 +292,21 @@ local int extract(FILE *in, struct access *index, off_t offset,
skip = 1; /* while skipping to offset */
do {
/* define where to put uncompressed data, and how much */
- if (offset == 0 && skip) { /* at offset now */
- strm.avail_out = len;
- strm.next_out = buf;
- skip = 0; /* only do this once */
- }
if (offset > WINSIZE) { /* skip WINSIZE bytes */
strm.avail_out = WINSIZE;
strm.next_out = discard;
offset -= WINSIZE;
}
- else if (offset != 0) { /* last skip */
+ else if (offset > 0) { /* last skip */
strm.avail_out = (unsigned)offset;
strm.next_out = discard;
offset = 0;
}
+ else if (skip) { /* at offset now */
+ strm.avail_out = len;
+ strm.next_out = buf;
+ skip = 0; /* only do this once */
+ }
/* uncompress until avail_out filled, or end of stream */
do {
@@ -315,11 +314,11 @@ local int extract(FILE *in, struct access *index, off_t offset,
strm.avail_in = fread(input, 1, CHUNK, in);
if (ferror(in)) {
ret = Z_ERRNO;
- goto extract_ret;
+ goto deflate_index_extract_ret;
}
if (strm.avail_in == 0) {
ret = Z_DATA_ERROR;
- goto extract_ret;
+ goto deflate_index_extract_ret;
}
strm.next_in = input;
}
@@ -327,41 +326,99 @@ local int extract(FILE *in, struct access *index, off_t offset,
if (ret == Z_NEED_DICT)
ret = Z_DATA_ERROR;
if (ret == Z_MEM_ERROR || ret == Z_DATA_ERROR)
- goto extract_ret;
- if (ret == Z_STREAM_END)
- break;
+ goto deflate_index_extract_ret;
+ if (ret == Z_STREAM_END) {
+ /* the raw deflate stream has ended */
+ if (index->gzip == 0)
+ /* this is a zlib stream that has ended -- done */
+ break;
+
+ /* near the end of a gzip member, which might be followed by
+ another gzip member -- skip the gzip trailer and see if
+ there is more input after it */
+ if (strm.avail_in < 8) {
+ fseeko(in, 8 - strm.avail_in, SEEK_CUR);
+ strm.avail_in = 0;
+ }
+ else {
+ strm.avail_in -= 8;
+ strm.next_in += 8;
+ }
+ if (strm.avail_in == 0 && ungetc(getc(in), in) == EOF)
+ /* the input ended after the gzip trailer -- done */
+ break;
+
+ /* there is more input, so another gzip member should follow --
+ validate and skip the gzip header */
+ ret = inflateReset2(&strm, 31);
+ if (ret != Z_OK)
+ goto deflate_index_extract_ret;
+ do {
+ if (strm.avail_in == 0) {
+ strm.avail_in = fread(input, 1, CHUNK, in);
+ if (ferror(in)) {
+ ret = Z_ERRNO;
+ goto deflate_index_extract_ret;
+ }
+ if (strm.avail_in == 0) {
+ ret = Z_DATA_ERROR;
+ goto deflate_index_extract_ret;
+ }
+ strm.next_in = input;
+ }
+ ret = inflate(&strm, Z_BLOCK);
+ if (ret == Z_MEM_ERROR || ret == Z_DATA_ERROR)
+ goto deflate_index_extract_ret;
+ } while ((strm.data_type & 128) == 0);
+
+ /* set up to continue decompression of the raw deflate stream
+ that follows the gzip header */
+ ret = inflateReset2(&strm, -15);
+ if (ret != Z_OK)
+ goto deflate_index_extract_ret;
+ }
+
+ /* continue to process the available input before reading more */
} while (strm.avail_out != 0);
- /* if reach end of stream, then don't keep trying to get more */
if (ret == Z_STREAM_END)
+ /* reached the end of the compressed data -- return the data that
+ was available, possibly less than requested */
break;
- /* do until offset reached and requested data read, or stream ends */
+ /* do until offset reached and requested data read */
} while (skip);
- /* compute number of uncompressed bytes read after offset */
+ /* compute the number of uncompressed bytes read after the offset */
ret = skip ? 0 : len - strm.avail_out;
- /* clean up and return bytes read or error */
- extract_ret:
+ /* clean up and return the bytes read, or the negative error */
+ deflate_index_extract_ret:
(void)inflateEnd(&strm);
return ret;
}
-/* Demonstrate the use of build_index() and extract() by processing the file
- provided on the command line, and the extracting 16K from about 2/3rds of
- the way through the uncompressed output, and writing that to stdout. */
+#ifdef TEST
+
+#define SPAN 1048576L /* desired distance between access points */
+#define LEN 16384 /* number of bytes to extract */
+
+/* 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)
{
int len;
- off_t offset;
+ off_t offset = -1;
FILE *in;
- struct access *index = NULL;
- unsigned char buf[CHUNK];
+ struct deflate_index *index = NULL;
+ unsigned char buf[LEN];
/* open input file */
- if (argc != 2) {
- fprintf(stderr, "usage: zran file.gz\n");
+ if (argc < 2 || argc > 3) {
+ fprintf(stderr, "usage: zran file.gz [offset]\n");
return 1;
}
in = fopen(argv[1], "rb");
@@ -370,8 +427,18 @@ int main(int argc, char **argv)
return 1;
}
+ /* get optional offset */
+ 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;
+ }
+ }
+
/* build index */
- len = build_index(in, SPAN, &index);
+ len = deflate_index_build(in, SPAN, &index);
if (len < 0) {
fclose(in);
switch (len) {
@@ -392,8 +459,9 @@ int main(int argc, char **argv)
fprintf(stderr, "zran: built index with %d access points\n", len);
/* use index by reading some bytes from an arbitrary offset */
- offset = (index->list[index->have - 1].out << 1) / 3;
- len = extract(in, index, offset, buf, CHUNK);
+ if (offset == -1)
+ offset = (index->length << 1) / 3;
+ len = deflate_index_extract(in, index, offset, buf, LEN);
if (len < 0)
fprintf(stderr, "zran: extraction failed: %s error\n",
len == Z_MEM_ERROR ? "out of memory" : "input corrupted");
@@ -403,7 +471,9 @@ int main(int argc, char **argv)
}
/* clean up and exit */
- free_index(index);
+ deflate_index_free(index);
fclose(in);
return 0;
}
+
+#endif
diff --git a/zlib/examples/zran.h b/zlib/examples/zran.h
new file mode 100644
index 0000000..2314125
--- /dev/null
+++ b/zlib/examples/zran.h
@@ -0,0 +1,40 @@
+/* zran.h -- example of zlib/gzip stream indexing and random access
+ * Copyright (C) 2005, 2012, 2018 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ * Version 1.2 14 Oct 2018 Mark Adler */
+
+#include <stdio.h>
+#include "zlib.h"
+
+/* Access point list. */
+struct deflate_index {
+ int have; /* number of list entries */
+ int gzip; /* 1 if the index is of a gzip file, 0 if it is of a
+ zlib stream */
+ off_t length; /* total length of uncompressed data */
+ void *list; /* allocated list of entries */
+};
+
+/* Make one entire pass through a zlib or gzip compressed stream and build an
+ index, with access points about every span bytes of uncompressed output.
+ gzip files with multiple members are indexed in their entirety. span should
+ be chosen to balance the speed of random access against the memory
+ requirements of the list, about 32K bytes per access point. The return value
+ is the number of access points on success (>= 1), Z_MEM_ERROR for out of
+ memory, Z_DATA_ERROR for an error in the input file, or Z_ERRNO for a file
+ read error. On success, *built points to the resulting index. */
+int deflate_index_build(FILE *in, off_t span, struct deflate_index **built);
+
+/* Deallocate an index built by deflate_index_build() */
+void deflate_index_free(struct deflate_index *index);
+
+/* Use the index to read len bytes from offset into buf. Return bytes read or
+ negative for error (Z_DATA_ERROR or Z_MEM_ERROR). If data is requested past
+ the end of the uncompressed data, then deflate_index_extract() will return a
+ value less than len, indicating how much was actually read into buf. This
+ function should not return a data error unless the file was modified since
+ the index was generated, since deflate_index_build() validated all of the
+ input. deflate_index_extract() will return Z_ERRNO if there is an error on
+ reading or seeking the input file. */
+int deflate_index_extract(FILE *in, struct deflate_index *index, off_t offset,
+ unsigned char *buf, int len);