3
{ inflate.c -- zlib interface to inflate modules
4
Copyright (C) 1995-1998 Mark Adler
7
Copyright (C) 1998 by Jacques Nomssi Nzali
8
For conditions of distribution and use, see copyright notice in readme.txt
16
zbase, infblock, infutil;
18
function inflateInit(var z : z_stream) : integer;
20
{ Initializes the internal stream state for decompression.
22
inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
23
enough memory, Z_VERSION_ERROR if the zlib library version is incompatible
24
with the version assumed by the caller. msg is set to null if there is no
25
error message. inflateInit does not perform any decompression: this will be
30
function inflateInit_(z : z_streamp;
31
const version : string;
32
stream_size : integer) : integer;
35
function inflateInit2_(var z: z_stream;
37
const version : string;
38
stream_size : integer) : integer;
40
function inflateInit2(var z: z_stream;
41
windowBits : integer) : integer;
44
This is another version of inflateInit with an extra parameter.
46
The windowBits parameter is the base two logarithm of the maximum window
47
size (the size of the history buffer). It should be in the range 8..15 for
48
this version of the library. The default value is 15 if inflateInit is used
49
instead. If a compressed stream with a larger window size is given as
50
input, inflate() will return with the error code Z_DATA_ERROR instead of
51
trying to allocate a larger window.
53
inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
54
memory, Z_STREAM_ERROR if a parameter is invalid (such as a negative
55
memLevel). msg is set to null if there is no error message. inflateInit2
56
does not perform any decompression apart from reading the zlib header if
57
present: this will be done by inflate(). (So next_in and avail_in may be
58
modified, but next_out and avail_out are unchanged.)
63
function inflateEnd(var z : z_stream) : integer;
66
All dynamically allocated data structures for this stream are freed.
67
This function discards any unprocessed input and does not flush any
70
inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
71
was inconsistent. In the error case, msg may be set but then points to a
72
static string (which must not be deallocated).
75
function inflateReset(var z : z_stream) : integer;
78
This function is equivalent to inflateEnd followed by inflateInit,
79
but does not free and reallocate all the internal decompression state.
80
The stream will keep attributes that may have been set by inflateInit2.
82
inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
83
stream state was inconsistent (such as getmem or state being NULL).
87
function inflate(var z : z_stream;
88
f : integer) : integer;
90
inflate decompresses as much data as possible, and stops when the input
91
buffer becomes empty or the output buffer becomes full. It may introduce
92
some output latency (reading input without producing any output)
93
except when forced to flush.
95
The detailed semantics are as follows. inflate performs one or both of the
98
- Decompress more input starting at next_in and update next_in and avail_in
99
accordingly. If not all input can be processed (because there is not
100
enough room in the output buffer), next_in is updated and processing
101
will resume at this point for the next call of inflate().
103
- Provide more output starting at next_out and update next_out and avail_out
104
accordingly. inflate() provides as much output as possible, until there
105
is no more input data or no more space in the output buffer (see below
106
about the flush parameter).
108
Before the call of inflate(), the application should ensure that at least
109
one of the actions is possible, by providing more input and/or consuming
110
more output, and updating the next_* and avail_* values accordingly.
111
The application can consume the uncompressed output when it wants, for
112
example when the output buffer is full (avail_out == 0), or after each
113
call of inflate(). If inflate returns Z_OK and with zero avail_out, it
114
must be called again after making room in the output buffer because there
115
might be more output pending.
117
If the parameter flush is set to Z_SYNC_FLUSH, inflate flushes as much
118
output as possible to the output buffer. The flushing behavior of inflate is
119
not specified for values of the flush parameter other than Z_SYNC_FLUSH
120
and Z_FINISH, but the current implementation actually flushes as much output
123
inflate() should normally be called until it returns Z_STREAM_END or an
124
error. However if all decompression is to be performed in a single step
125
(a single call of inflate), the parameter flush should be set to
126
Z_FINISH. In this case all pending input is processed and all pending
127
output is flushed; avail_out must be large enough to hold all the
128
uncompressed data. (The size of the uncompressed data may have been saved
129
by the compressor for this purpose.) The next operation on this stream must
130
be inflateEnd to deallocate the decompression state. The use of Z_FINISH
131
is never required, but can be used to inform inflate that a faster routine
132
may be used for the single inflate() call.
134
If a preset dictionary is needed at this point (see inflateSetDictionary
135
below), inflate sets strm-adler to the adler32 checksum of the
136
dictionary chosen by the compressor and returns Z_NEED_DICT; otherwise
137
it sets strm->adler to the adler32 checksum of all output produced
138
so far (that is, total_out bytes) and returns Z_OK, Z_STREAM_END or
139
an error code as described below. At the end of the stream, inflate()
140
checks that its computed adler32 checksum is equal to that saved by the
141
compressor and returns Z_STREAM_END only if the checksum is correct.
143
inflate() returns Z_OK if some progress has been made (more input processed
144
or more output produced), Z_STREAM_END if the end of the compressed data has
145
been reached and all uncompressed output has been produced, Z_NEED_DICT if a
146
preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
147
corrupted (input stream not conforming to the zlib format or incorrect
148
adler32 checksum), Z_STREAM_ERROR if the stream structure was inconsistent
149
(for example if next_in or next_out was NULL), Z_MEM_ERROR if there was not
150
enough memory, Z_BUF_ERROR if no progress is possible or if there was not
151
enough room in the output buffer when Z_FINISH is used. In the Z_DATA_ERROR
152
case, the application may then call inflateSync to look for a good
157
function inflateSetDictionary(var z : z_stream;
158
dictionary : Pbyte; {const array of byte}
159
dictLength : cardinal) : integer;
162
Initializes the decompression dictionary from the given uncompressed byte
163
sequence. This function must be called immediately after a call of inflate
164
if this call returned Z_NEED_DICT. The dictionary chosen by the compressor
165
can be determined from the Adler32 value returned by this call of
166
inflate. The compressor and decompressor must use exactly the same
167
dictionary (see deflateSetDictionary).
169
inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
170
parameter is invalid (such as NULL dictionary) or the stream state is
171
inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
172
expected one (incorrect Adler32 value). inflateSetDictionary does not
173
perform any decompression: this will be done by subsequent calls of
177
function inflateSync(var z : z_stream) : integer;
180
Skips invalid compressed data until a full flush point (see above the
181
description of deflate with Z_FULL_FLUSH) can be found, or until all
182
available input is skipped. No output is provided.
184
inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR
185
if no more input was provided, Z_DATA_ERROR if no flush point has been found,
186
or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
187
case, the application may save the current current value of total_in which
188
indicates where valid compressed data was found. In the error case, the
189
application may repeatedly call inflateSync, providing more input each time,
190
until success or end of the input data.
194
function inflateSyncPoint(var z : z_stream) : integer;
202
function inflateReset(var z : z_stream) : integer;
206
inflateReset := Z_STREAM_ERROR;
212
if z.state^.nowrap then
213
z.state^.mode := BLOCKS
215
z.state^.mode := METHOD;
216
inflate_blocks_reset(z.state^.blocks^, z, nil);
218
Tracev('inflate: reset');
220
inflateReset := Z_OK;
224
function inflateEnd(var z : z_stream) : integer;
228
inflateEnd := Z_STREAM_ERROR;
231
if z.state^.blocks<>nil then
232
inflate_blocks_free(z.state^.blocks, z);
236
Tracev('inflate: end');
242
function inflateInit2_(var z: z_stream;
244
const version : string;
245
stream_size : integer) : integer;
247
if (version = '') or (version[1] <> ZLIB_VERSION[1]) or
248
(stream_size <> sizeof(z_stream)) then
250
inflateInit2_ := Z_VERSION_ERROR;
254
{ SetLength(strm.msg, 255); }
260
inflateInit2_ := Z_MEM_ERROR;
264
z.state^.blocks := nil;
266
{ handle undocumented nowrap option (no zlib header or check) }
267
z.state^.nowrap := FALSE;
271
z.state^.nowrap := TRUE;
275
if (w < 8) or (w > 15) then
278
inflateInit2_ := Z_STREAM_ERROR;
281
z.state^.wbits := cardinal(w);
283
{ create inflate_blocks state }
284
if z.state^.nowrap then
285
z.state^.blocks := inflate_blocks_new(z, nil, cardinal(1) shl w)
287
z.state^.blocks := inflate_blocks_new(z, @adler32, cardinal(1) shl w);
288
if z.state^.blocks=nil then
291
inflateInit2_ := Z_MEM_ERROR;
295
Tracev('inflate: allocated');
299
inflateInit2_ := Z_OK;
302
function inflateInit2(var z: z_stream; windowBits : integer) : integer;
304
inflateInit2 := inflateInit2_(z, windowBits, ZLIB_VERSION, sizeof(z_stream));
308
function inflateInit(var z : z_stream) : integer;
309
{ inflateInit is a macro to allow checking the zlib version
310
and the compiler's view of z_stream: }
312
inflateInit := inflateInit2_(z, DEF_WBITS, ZLIB_VERSION, sizeof(z_stream));
315
function inflateInit_(z : z_streamp;
316
const version : string;
317
stream_size : integer) : integer;
321
inflateInit_ := Z_STREAM_ERROR
323
inflateInit_ := inflateInit2_(z^, DEF_WBITS, version, stream_size);
326
function inflate(var z : z_stream;
327
f : integer) : integer;
332
if (z.state=nil) or (z.next_in=nil) then
334
inflate := Z_STREAM_ERROR;
343
case (z.state^.mode) of
346
r := inflate_blocks(z.state^.blocks^, z, r);
347
if (r = Z_DATA_ERROR) then
349
z.state^.mode := BAD;
350
z.state^.sub.marker := 0; { can try inflateSync }
351
continue; { break C-switch }
355
if (r <> Z_STREAM_END) then
361
inflate_blocks_reset(z.state^.blocks^, z, @z.state^.sub.check.was);
362
if (z.state^.nowrap) then
364
z.state^.mode := DONE;
365
continue; { break C-switch }
367
z.state^.mode := CHECK4; { falltrough }
372
if (z.avail_in = 0) then
379
{z.state^.sub.check.need := cardinal(NEXTBYTE(z)) shl 24;}
382
z.state^.sub.check.need := cardinal(z.next_in^) shl 24;
385
z.state^.mode := CHECK3; { falltrough }
390
if (z.avail_in = 0) then
396
{inc( z.state^.sub.check.need, cardinal(NEXTBYTE(z)) shl 16);}
399
inc(z.state^.sub.check.need, cardinal(z.next_in^) shl 16);
402
z.state^.mode := CHECK2; { falltrough }
407
if (z.avail_in = 0) then
414
{inc( z.state^.sub.check.need, cardinal(NEXTBYTE(z)) shl 8);}
417
inc(z.state^.sub.check.need, cardinal(z.next_in^) shl 8);
420
z.state^.mode := CHECK1; { falltrough }
425
if (z.avail_in = 0) then
431
{inc( z.state^.sub.check.need, cardinal(NEXTBYTE(z)) );}
434
inc(z.state^.sub.check.need, cardinal(z.next_in^) );
438
if (z.state^.sub.check.was <> z.state^.sub.check.need) then
440
z.state^.mode := BAD;
441
z.msg := 'incorrect data check';
442
z.state^.sub.marker := 5; { can't try inflateSync }
443
continue; { break C-switch }
446
Tracev('inflate: zlib check ok');
448
z.state^.mode := DONE; { falltrough }
452
inflate := Z_STREAM_END;
458
if (z.avail_in = 0) then
465
{z.state^.sub.method := NEXTBYTE(z);}
468
z.state^.sub.method := z.next_in^;
471
if ((z.state^.sub.method and $0f) <> Z_DEFLATED) then
473
z.state^.mode := BAD;
474
z.msg := 'unknown compression method';
475
z.state^.sub.marker := 5; { can't try inflateSync }
476
continue; { break C-switch }
478
if ((z.state^.sub.method shr 4) + 8 > z.state^.wbits) then
480
z.state^.mode := BAD;
481
z.msg := 'invalid window size';
482
z.state^.sub.marker := 5; { can't try inflateSync }
483
continue; { break C-switch }
485
z.state^.mode := FLAG;
491
if (z.avail_in = 0) then
503
if (((z.state^.sub.method shl 8) + b) mod 31) <> 0 then {% mod ?}
505
z.state^.mode := BAD;
506
z.msg := 'incorrect header check';
507
z.state^.sub.marker := 5; { can't try inflateSync }
508
continue; { break C-switch }
511
Tracev('inflate: zlib header ok');
513
if ((b and PRESET_DICT) = 0) then
515
z.state^.mode := BLOCKS;
516
continue; { break C-switch }
518
z.state^.mode := DICT4;
523
if (z.avail_in = 0) then
530
{z.state^.sub.check.need := cardinal(NEXTBYTE(z)) shl 24;}
533
z.state^.sub.check.need := cardinal(z.next_in^) shl 24;
536
z.state^.mode := DICT3; { falltrough }
540
if (z.avail_in = 0) then
546
{inc(z.state^.sub.check.need, cardinal(NEXTBYTE(z)) shl 16);}
549
inc(z.state^.sub.check.need, cardinal(z.next_in^) shl 16);
552
z.state^.mode := DICT2; { falltrough }
556
if (z.avail_in = 0) then
563
{inc(z.state^.sub.check.need, cardinal(NEXTBYTE(z)) shl 8);}
566
inc(z.state^.sub.check.need, cardinal(z.next_in^) shl 8);
569
z.state^.mode := DICT1; { falltrough }
573
if (z.avail_in = 0) then
578
{ r := f; --- wird niemals benutzt }
579
{inc(z.state^.sub.check.need, cardinal(NEXTBYTE(z)) );}
582
inc(z.state^.sub.check.need, cardinal(z.next_in^) );
585
z.adler := z.state^.sub.check.need;
586
z.state^.mode := DICT0;
587
inflate := Z_NEED_DICT;
592
z.state^.mode := BAD;
593
z.msg := 'need dictionary';
594
z.state^.sub.marker := 0; { can try inflateSync }
595
inflate := Z_STREAM_ERROR;
600
inflate := Z_DATA_ERROR;
605
inflate := Z_STREAM_ERROR;
609
{$ifdef NEED_DUMMY_result}
610
result := Z_STREAM_ERROR; { Some dumb compilers complain without this }
614
function inflateSetDictionary(var z : z_stream;
615
dictionary : Pbyte; {const array of byte}
616
dictLength : cardinal) : integer;
620
length := dictLength;
622
if (z.state=nil) or (z.state^.mode<>DICT0) then
624
inflateSetDictionary := Z_STREAM_ERROR;
627
if (adler32(1, dictionary, dictLength) <> z.adler) then
629
inflateSetDictionary := Z_DATA_ERROR;
634
if (length >= (1 shl z.state^.wbits)) then
636
length := (1 shl z.state^.wbits)-1;
637
inc( dictionary, dictLength - length);
639
inflate_set_dictionary(z.state^.blocks^, dictionary^, length);
640
z.state^.mode := BLOCKS;
641
inflateSetDictionary := Z_OK;
645
function inflateSync(var z : z_stream) : integer;
647
mark : packed array[0..3] of byte = (0, 0, $ff, $ff);
649
n : cardinal; { number of bytes to look at }
650
p : Pbyte; { pointer to bytes }
651
m : cardinal; { number of marker bytes found in a row }
652
r, w : cardinal; { temporaries to save total_in and total_out }
657
inflateSync := Z_STREAM_ERROR;
660
if (z.state^.mode <> BAD) then
662
z.state^.mode := BAD;
663
z.state^.sub.marker := 0;
668
inflateSync := Z_BUF_ERROR;
672
m := z.state^.sub.marker;
675
while (n <> 0) and (m < 4) do
677
if (p^ = mark[m]) then
689
inc(z.total_in, ptruint(p) - ptruint(z.next_in));
692
z.state^.sub.marker := m;
695
{ return no joy or set up to restart on a new block }
698
inflateSync := Z_DATA_ERROR;
706
z.state^.mode := BLOCKS;
712
returns true if inflate is currently at the end of a block generated
713
by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
714
implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
715
but removes the length bytes of the resulting empty stored block. When
716
decompressing, PPP checks that at the end of input packet, inflate is
717
waiting for these length bytes.
720
function inflateSyncPoint(var z : z_stream) : integer;
722
if (z.state = nil) or (z.state^.blocks = nil) then
724
inflateSyncPoint := Z_STREAM_ERROR;
727
inflateSyncPoint := inflate_blocks_sync_point(z.state^.blocks^);