view release on metacpan or search on metacpan
zlib/contrib/blast/blast.c view on Meta::CPAN
122123124125126127128129130131132133134135136137138139140141142* - The first code
for
the shortest
length
is all ones. Subsequent codes of
* the same
length
are simply integer decrements of the previous code. When
* moving up a
length
, a one bit is appended to the code. For a complete
* code, the
last
code of the longest
length
will be all zeros. To support
* this ordering, the bits pulled during decoding are inverted to apply the
* more
"natural"
ordering starting
with
all zeros and incrementing.
*/
local
int
decode(struct state
*s
, struct huffman
*h
)
{
int
len; /* current number of bits in code */
int
code; /* len bits being decoded */
int
first; /* first code of
length
len */
int
count; /* number of codes of
length
len */
int
index
; /*
index
of first code of
length
len in symbol table */
int
bitbuf; /* bits from stream */
int
left; /* bits left in
next
or left to process */
short
*next
; /*
next
number of codes */
bitbuf = s->bitbuf;
left = s->bitcnt;
code = first =
index
= 0;
zlib/contrib/blast/blast.c view on Meta::CPAN
276277278279280281282283284285286287288289290291292293294295296* allowed and common. For example, a distance of one and a
length
of 518
* simply copies the
last
byte 518
times
. A distance of four and a
length
of
* twelve copies the
last
four bytes three
times
. A simple forward copy
* ignoring whether the
length
is greater than the distance or not implements
* this correctly.
*/
local
int
decomp(struct state
*s
)
{
int
lit; /* true
if
literals are coded */
int
dict; /* log2(dictionary size) - 6 */
int
symbol; /* decoded symbol, extra bits
for
distance */
int
len; /*
length
for
copy */
unsigned dist; /* distance
for
copy */
int
copy; /* copy counter */
unsigned char
*from
,
*to
; /* copy pointers */
static
int
virgin = 1; /* build tables once */
static short litcnt[MAXBITS+1], litsym[256]; /* litcode memory */
static short lencnt[MAXBITS+1], lensym[16]; /* lencode memory */
static short distcnt[MAXBITS+1], distsym[64]; /* distcode memory */
static struct huffman litcode = {litcnt, litsym}; /*
length
code */
static struct huffman lencode = {lencnt, lensym}; /*
length
code */
zlib/contrib/puff/puff.c view on Meta::CPAN
228229230231232233234235236237238239240241242243244245246247248* moving up a
length
, a zero bit is appended to the code. For a complete
* code, the
last
code of the longest
length
will be all ones.
*
* - Incomplete codes are handled by this decoder, since they are permitted
* in the deflate
format
. See the
format
notes
for
fixed() and dynamic().
*/
#ifdef SLOW
local
int
decode(struct state
*s
, const struct huffman
*h
)
{
int
len; /* current number of bits in code */
int
code; /* len bits being decoded */
int
first; /* first code of
length
len */
int
count; /* number of codes of
length
len */
int
index
; /*
index
of first code of
length
len in symbol table */
code = first =
index
= 0;
for
(len = 1; len <= MAXBITS; len++) {
code |= bits(s, 1); /* get
next
bit */
count = h->count[len];
if
(code - count < first) /*
if
length
len,
return
symbol */
return
h->symbol[
index
+ (code - first)];
zlib/contrib/puff/puff.c view on Meta::CPAN
256257258259260261262263264265266267268269270271272273274275276/*
* A faster version of decode()
for
real applications of this code. It's not
* as readable, but it makes puff() twice as fast. And it only makes the code
* a few percent larger.
*/
#else /* !SLOW */
local
int
decode(struct state
*s
, const struct huffman
*h
)
{
int
len; /* current number of bits in code */
int
code; /* len bits being decoded */
int
first; /* first code of
length
len */
int
count; /* number of codes of
length
len */
int
index
; /*
index
of first code of
length
len in symbol table */
int
bitbuf; /* bits from stream */
int
left; /* bits left in
next
or left to process */
short
*next
; /*
next
number of codes */
bitbuf = s->bitbuf;
left = s->bitcnt;
code = first =
index
= 0;
zlib/contrib/puff/puff.c view on Meta::CPAN
398399400401402403404405406407408409410411412413414415416417418419420* to represent all of those. Lengths 3..10 and 258 are in fact represented
* by just a
length
symbol. Lengths 11..257 are represented as a symbol and
* some number of extra bits that are added as an integer to the base
length
* of the
length
symbol. The number of extra bits is determined by the base
*
length
symbol. These are in the static arrays below, lens[]
for
the base
* lengths and lext[]
for
the corresponding number of extra bits.
*
* - The reason that 258 gets its own symbol is that the longest
length
is used
* often in highly redundant files. Note that 258 can also be coded as the
* base value 227 plus the maximum extra value of 31. While a good deflate
* should never
do
this, it is not an error, and should be decoded properly.
*
* - If a
length
is decoded, including its extra bits
if
any, then it is
* followed a distance code. There are up to 30 distance symbols. Again
* there are many more possible distances (1..32768), so extra bits are added
* to a base value represented by the symbol. The distances 1..4 get their
* corresponding number of extra bits are below in the static arrays dist[]
* and dext[].
*
* - Literal bytes are simply written to the output. A
length
/distance pair is
* an instruction to copy previously uncompressed bytes to the output. The
* copy is from distance bytes back in the output stream, copying
for
length
zlib/contrib/puff/puff.c view on Meta::CPAN
430431432433434435436437438439440441442443444445446447448449450* ignoring whether the
length
is greater than the distance or not implements
* since though their behavior -is-
defined
for
overlapping arrays, it is
*
defined
to
do
the wrong thing in this case.
*/
local
int
codes(struct state
*s
,
const struct huffman
*lencode
,
const struct huffman
*distcode
)
{
int
symbol; /* decoded symbol */
int
len; /*
length
for
copy */
unsigned dist; /* distance
for
copy */
static const short lens[29] = { /* Size base
for
length
codes 257..285 */
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258};
static const short lext[29] = { /* Extra bits
for
length
codes 257..285 */
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
static const short dists[30] = { /* Offset base
for
distance codes 0..29 */
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
zlib/contrib/puff/puff.c view on Meta::CPAN
694695696697698699700701702703704705706707708709710711712713714
lengths[order[
index
]] = 0;
err = construct(
&lencode
, lengths, 19);
return
-4;
/*
read
length
/literal and distance code
length
tables */
index
= 0;
while
(
index
< nlen + ndist) {
int
symbol; /* decoded value */
int
len; /*
last
length
to repeat */
symbol = decode(s,
&lencode
);
if
(symbol < 0)
return
symbol; /* invalid symbol */
if
(symbol < 16) /*
length
in 0..15 */
lengths[
index
++] = symbol;
else
{ /* repeat instruction */
len = 0; /* assume repeating zeros */
if
(symbol == 16) { /* repeat
last
length
3..6
times
*/
zlib/contrib/puff/puff.c view on Meta::CPAN
776777778779780781782783784785786787788789790791792793794795796* -6: dynamic block code description: repeat more than specified lengths
* -7: dynamic block code description: invalid literal/
length
code lengths
* -8: dynamic block code description: invalid distance code lengths
* -9: dynamic block code description: missing end-of-block code
* -10: invalid literal/
length
or distance code in fixed or dynamic block
* -11: distance is too far back in fixed or dynamic block
*
* Format notes:
*
* - Three bits are
read
for
each
block to determine the kind of block and
* whether or not it is the
last
block. Then the block is decoded and the
* process repeated
if
it was not the
last
block.
*
* - The leftover bits in the
last
byte of the deflate data
after
the
last
* block (
if
it was a fixed or dynamic block) are undefined and have
no
* expected
values
to check.
*/
int
puff(unsigned char
*dest
, /* pointer to destination pointer */
unsigned long
*destlen
, /* amount of output space */
const unsigned char
*source
, /* pointer to source data pointer */
unsigned long
*sourcelen
) /* amount of input available */
zlib/doc/algorithm.txt view on Meta::CPAN
123124125126127128129130131132133134135136137138139140141142143other extreme, you could make a new table
for
every bit in the code. In fact,
that's essentially a Huffman tree. But then you spend too much
time
traversing the tree
while
decoding, even
for
short symbols.
So the number of bits
for
the first lookup table is a trade of the
time
to
fill out the table vs. the
time
spent looking at the second level and above of
the table.
Here is an example, scaled down:
The code being decoded,
with
10 symbols, from 1 to 6 bits long:
A: 0
B: 10
C: 1100
D: 11010
E: 11011
F: 11100
G: 11101
H: 11110
I: 111110
zlib/doc/rfc1951.txt view on Meta::CPAN
560561562563564565566567568569570571572573574575576577578579Deutsch Informational [Page 10]
RFC 1951 DEFLATE Compressed Data Format Specification May 1996
preset dictionary might discard part of the output stream; a
distance can refer to that part of the output stream anyway)
Note also that the referenced string may overlap the current
position;
for
example,
if
the
last
2 bytes decoded have
values
X and Y, a string reference
with
<
length
= 5, distance = 2>
adds X,Y,X,Y,X to the output stream.
We now specify
each
compression method in turn.
3.2.4. Non-compressed blocks (BTYPE=00)
Any bits of input up to the
next
byte boundary are ignored.
The rest of the block consists of the following information:
zlib/examples/zran.c view on Meta::CPAN
5678910111213141516171819202122232425/* 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
*/
for
random access of a compressed file. A file containing a zlib or gzip
stream is provided on the command line. The compressed stream is decoded in
its entirety, and an
index
built
with
access points about every SPAN bytes
in the uncompressed output. The compressed file is left
open
, and can then
be
read
randomly, having to decompress on the average SPAN/2 uncompressed
bytes
before
getting to the desired block of data.
An access point can be created at the start of any deflate block, by saving
the starting file offset and bit of that block, and the 32K bytes of
uncompressed data that precede that block. Also the uncompressed offset of
that block is saved to provide a reference
for
locating a desired starting
point in the uncompressed stream. deflate_index_build() works by
zlib/test/infcover.c view on Meta::CPAN
255256257258259260261262263264265266267268269270271272273274275
do
{
if
(
*hex
>=
'0'
&&
*hex
<=
'9'
)
val = (val << 4) +
*hex
-
'0'
;
else
if
(
*hex
>=
'A'
&&
*hex
<=
'F'
)
val = (val << 4) +
*hex
-
'A'
+ 10;
else
if
(
*hex
>=
'a'
&&
*hex
<=
'f'
)
val = (val << 4) +
*hex
-
'a'
+ 10;
else
if
(val != 1 && val < 32) /* one digit followed by delimiter */
val += 240; /* make it look like two digits */
if
(val > 255) { /* have two digits */
in[
next
++] = val & 0xff; /* save the decoded byte */
val = 1; /* start over */
}
}
while
(
*hex
++); /* go through the loop
with
the terminating null */
if
(len != NULL)
*len
=
next
;
re = realloc(in,
next
);
return
re == NULL ? in : re;
}
/* generic inflate() run, where
hex
is the hexadecimal input data, what is the
zlib/zlib.h view on Meta::CPAN
447448449450451452453454455456457458459460461462463464465466467stream. The end-of-block will not be indicated
until
all of the uncompressed
data from that block
has
been written to strm->next_out. The number of
unused bits may in general be greater than seven, except
when
bit 7 of
data_type is set, in which case the number of unused bits will be less than
eight. data_type is set as noted here every
time
inflate() returns
for
all
flush options, and so can be used to determine the amount of currently
consumed input in bits.
The Z_TREES option behaves as Z_BLOCK does, but it also returns
when
the
end of
each
deflate block header is reached,
before
any actual data in that
block is decoded. This allows the
caller
to determine the
length
of the
256 is added to the value of strm->data_type
when
inflate() returns
immediately
after
reaching the end of the deflate block header.
inflate() should normally be called
until
it returns Z_STREAM_END or an
error. However
if
all decompression is to be performed in a single step (a
single call of inflate), the parameter flush should be set to Z_FINISH. In
this case all pending input is processed and all pending output is flushed;
avail_out must be large enough to hold all of the uncompressed data
for
the
operation to complete. (The size of the uncompressed data may have been
zlib/zlib.h view on Meta::CPAN
856857858859860861862863864865866867868869870871872873874875876such as zip. Those formats provide their own check
values
. If a custom
format
is developed using the raw deflate
format
for
compressed data, it is
recommended that a check value such as an Adler-32 or a CRC-32 be applied to
the uncompressed data as is done in the zlib, gzip, and zip formats. For
most applications, the zlib
format
should be used as is. Note that comments
windowBits can also be greater than 15
for
optional gzip decoding. Add
32 to windowBits to enable zlib and gzip decoding
with
automatic header
detection, or add 16 to decode only the gzip
format
(the zlib
format
will
return
a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is a
CRC-32 instead of an Adler-32. Unlike the gunzip utility and gzread() (see
below), inflate() will
*not
* automatically decode concatenated gzip members.
inflate() will
return
Z_STREAM_END at the end of the gzip member. The state
would need to be
reset
to
continue
decoding a subsequent gzip member. This
*must
* be done
if
there is more data
after
a gzip member, in order
for
the
decompression to be compliant
with
the gzip standard (RFC 1952).
inflateInit2 returns Z_OK
if
success, Z_MEM_ERROR
if
there was not enough
memory, Z_VERSION_ERROR
if
the zlib library version is incompatible
with
the
version assumed by the
caller
, or Z_STREAM_ERROR
if
the parameters are
zlib/zlib.h view on Meta::CPAN
103010311032103310341035103610371038103910401041104210431044104510461047104810491050*/
ZEXTERN
int
ZEXPORT inflateGetHeader OF((z_streamp strm,
gz_headerp head));
/*
inflateGetHeader() requests that gzip header information be stored in the
provided gz_header structure. inflateGetHeader() may be called
after
inflateInit2() or inflateReset(), and
before
the first call of inflate().
As inflate() processes the gzip stream, head->done is zero
until
the header
is completed, at which
time
head->done is set to one. If a zlib stream is
being decoded, then head->done is set to -1 to indicate that there will be
no
gzip header information forthcoming. Note that Z_BLOCK or Z_TREES can be
used to force inflate() to
return
immediately
after
header processing is
complete and
before
any actual data is decompressed.
The text,
time
, xflags, and os fields are filled in
with
the gzip header
contents. hcrc is set to true
if
there is a header CRC. (The header CRC
was valid
if
done is set to one.) If extra is not Z_NULL, then extra_max
contains the maximum number of bytes to
write
to extra. Once done is true,
extra_len contains the actual extra field
length
, and extra contains the
extra field, or that field truncated
if
extra_max is less than extra_len.