3 { inflate.c -- zlib interface to inflate modules
\r
4 Copyright (C) 1995-1998 Mark Adler
\r
7 Copyright (C) 1998 by Jacques Nomssi Nzali
\r
8 For conditions of distribution and use, see copyright notice in readme.paszlib
\r
16 zutil, zlib, infblock, infutil;
\r
18 function inflateInit(var z : z_stream) : int;
\r
20 { Initializes the internal stream state for decompression. The fields
\r
21 zalloc, zfree and opaque must be initialized before by the caller. If
\r
22 zalloc and zfree are set to Z_NULL, inflateInit updates them to use default
\r
23 allocation functions.
\r
25 inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
\r
26 enough memory, Z_VERSION_ERROR if the zlib library version is incompatible
\r
27 with the version assumed by the caller. msg is set to null if there is no
\r
28 error message. inflateInit does not perform any decompression: this will be
\r
29 done by inflate(). }
\r
33 function inflateInit_(z : z_streamp;
\r
34 const version : string;
\r
35 stream_size : int) : int;
\r
38 function inflateInit2_(var z: z_stream;
\r
40 const version : string;
\r
41 stream_size : int) : int;
\r
43 function inflateInit2(var z: z_stream;
\r
44 windowBits : int) : int;
\r
47 This is another version of inflateInit with an extra parameter. The
\r
48 fields next_in, avail_in, zalloc, zfree and opaque must be initialized
\r
49 before by the caller.
\r
51 The windowBits parameter is the base two logarithm of the maximum window
\r
52 size (the size of the history buffer). It should be in the range 8..15 for
\r
53 this version of the library. The default value is 15 if inflateInit is used
\r
54 instead. If a compressed stream with a larger window size is given as
\r
55 input, inflate() will return with the error code Z_DATA_ERROR instead of
\r
56 trying to allocate a larger window.
\r
58 inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
\r
59 memory, Z_STREAM_ERROR if a parameter is invalid (such as a negative
\r
60 memLevel). msg is set to null if there is no error message. inflateInit2
\r
61 does not perform any decompression apart from reading the zlib header if
\r
62 present: this will be done by inflate(). (So next_in and avail_in may be
\r
63 modified, but next_out and avail_out are unchanged.)
\r
68 function inflateEnd(var z : z_stream) : int;
\r
71 All dynamically allocated data structures for this stream are freed.
\r
72 This function discards any unprocessed input and does not flush any
\r
75 inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
\r
76 was inconsistent. In the error case, msg may be set but then points to a
\r
77 static string (which must not be deallocated).
\r
80 function inflateReset(var z : z_stream) : int;
\r
83 This function is equivalent to inflateEnd followed by inflateInit,
\r
84 but does not free and reallocate all the internal decompression state.
\r
85 The stream will keep attributes that may have been set by inflateInit2.
\r
87 inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
\r
88 stream state was inconsistent (such as zalloc or state being NULL).
\r
92 function inflate(var z : z_stream;
\r
95 inflate decompresses as much data as possible, and stops when the input
\r
96 buffer becomes empty or the output buffer becomes full. It may introduce
\r
97 some output latency (reading input without producing any output)
\r
98 except when forced to flush.
\r
100 The detailed semantics are as follows. inflate performs one or both of the
\r
103 - Decompress more input starting at next_in and update next_in and avail_in
\r
104 accordingly. If not all input can be processed (because there is not
\r
105 enough room in the output buffer), next_in is updated and processing
\r
106 will resume at this point for the next call of inflate().
\r
108 - Provide more output starting at next_out and update next_out and avail_out
\r
109 accordingly. inflate() provides as much output as possible, until there
\r
110 is no more input data or no more space in the output buffer (see below
\r
111 about the flush parameter).
\r
113 Before the call of inflate(), the application should ensure that at least
\r
114 one of the actions is possible, by providing more input and/or consuming
\r
115 more output, and updating the next_* and avail_* values accordingly.
\r
116 The application can consume the uncompressed output when it wants, for
\r
117 example when the output buffer is full (avail_out == 0), or after each
\r
118 call of inflate(). If inflate returns Z_OK and with zero avail_out, it
\r
119 must be called again after making room in the output buffer because there
\r
120 might be more output pending.
\r
122 If the parameter flush is set to Z_SYNC_FLUSH, inflate flushes as much
\r
123 output as possible to the output buffer. The flushing behavior of inflate is
\r
124 not specified for values of the flush parameter other than Z_SYNC_FLUSH
\r
125 and Z_FINISH, but the current implementation actually flushes as much output
\r
126 as possible anyway.
\r
128 inflate() should normally be called until it returns Z_STREAM_END or an
\r
129 error. However if all decompression is to be performed in a single step
\r
130 (a single call of inflate), the parameter flush should be set to
\r
131 Z_FINISH. In this case all pending input is processed and all pending
\r
132 output is flushed; avail_out must be large enough to hold all the
\r
133 uncompressed data. (The size of the uncompressed data may have been saved
\r
134 by the compressor for this purpose.) The next operation on this stream must
\r
135 be inflateEnd to deallocate the decompression state. The use of Z_FINISH
\r
136 is never required, but can be used to inform inflate that a faster routine
\r
137 may be used for the single inflate() call.
\r
139 If a preset dictionary is needed at this point (see inflateSetDictionary
\r
140 below), inflate sets strm-adler to the adler32 checksum of the
\r
141 dictionary chosen by the compressor and returns Z_NEED_DICT; otherwise
\r
142 it sets strm->adler to the adler32 checksum of all output produced
\r
143 so far (that is, total_out bytes) and returns Z_OK, Z_STREAM_END or
\r
144 an error code as described below. At the end of the stream, inflate()
\r
145 checks that its computed adler32 checksum is equal to that saved by the
\r
146 compressor and returns Z_STREAM_END only if the checksum is correct.
\r
148 inflate() returns Z_OK if some progress has been made (more input processed
\r
149 or more output produced), Z_STREAM_END if the end of the compressed data has
\r
150 been reached and all uncompressed output has been produced, Z_NEED_DICT if a
\r
151 preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
\r
152 corrupted (input stream not conforming to the zlib format or incorrect
\r
153 adler32 checksum), Z_STREAM_ERROR if the stream structure was inconsistent
\r
154 (for example if next_in or next_out was NULL), Z_MEM_ERROR if there was not
\r
155 enough memory, Z_BUF_ERROR if no progress is possible or if there was not
\r
156 enough room in the output buffer when Z_FINISH is used. In the Z_DATA_ERROR
\r
157 case, the application may then call inflateSync to look for a good
\r
162 function inflateSetDictionary(var z : z_stream;
\r
163 dictionary : pBytef; {const array of byte}
\r
164 dictLength : uInt) : int;
\r
167 Initializes the decompression dictionary from the given uncompressed byte
\r
168 sequence. This function must be called immediately after a call of inflate
\r
169 if this call returned Z_NEED_DICT. The dictionary chosen by the compressor
\r
170 can be determined from the Adler32 value returned by this call of
\r
171 inflate. The compressor and decompressor must use exactly the same
\r
172 dictionary (see deflateSetDictionary).
\r
174 inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
\r
175 parameter is invalid (such as NULL dictionary) or the stream state is
\r
176 inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
\r
177 expected one (incorrect Adler32 value). inflateSetDictionary does not
\r
178 perform any decompression: this will be done by subsequent calls of
\r
182 function inflateSync(var z : z_stream) : int;
\r
185 Skips invalid compressed data until a full flush point (see above the
\r
186 description of deflate with Z_FULL_FLUSH) can be found, or until all
\r
187 available input is skipped. No output is provided.
\r
189 inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR
\r
190 if no more input was provided, Z_DATA_ERROR if no flush point has been found,
\r
191 or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
\r
192 case, the application may save the current current value of total_in which
\r
193 indicates where valid compressed data was found. In the error case, the
\r
194 application may repeatedly call inflateSync, providing more input each time,
\r
195 until success or end of the input data.
\r
199 function inflateSyncPoint(var z : z_stream) : int;
\r
207 function inflateReset(var z : z_stream) : int;
\r
209 if (z.state = Z_NULL) then
\r
211 inflateReset := Z_STREAM_ERROR;
\r
217 if z.state^.nowrap then
\r
218 z.state^.mode := BLOCKS
\r
220 z.state^.mode := METHOD;
\r
221 inflate_blocks_reset(z.state^.blocks^, z, Z_NULL);
\r
223 Tracev('inflate: reset');
\r
225 inflateReset := Z_OK;
\r
229 function inflateEnd(var z : z_stream) : int;
\r
231 if (z.state = Z_NULL) or not Assigned(z.zfree) then
\r
233 inflateEnd := Z_STREAM_ERROR;
\r
236 if (z.state^.blocks <> Z_NULL) then
\r
237 inflate_blocks_free(z.state^.blocks, z);
\r
241 Tracev('inflate: end');
\r
243 inflateEnd := Z_OK;
\r
247 function inflateInit2_(var z: z_stream;
\r
249 const version : string;
\r
250 stream_size : int) : int;
\r
252 if (version = '') or (version[1] <> ZLIB_VERSION[1]) or
\r
253 (stream_size <> sizeof(z_stream)) then
\r
255 inflateInit2_ := Z_VERSION_ERROR;
\r
258 { initialize state }
\r
259 { SetLength(strm.msg, 255); }
\r
261 if not Assigned(z.zalloc) then
\r
263 {$IFDEF FPC} z.zalloc := @zcalloc; {$ELSE}
\r
264 z.zalloc := zcalloc;
\r
266 z.opaque := voidpf(0);
\r
268 if not Assigned(z.zfree) then
\r
269 {$IFDEF FPC} z.zfree := @zcfree; {$ELSE}
\r
273 z.state := pInternal_state( ZALLOC(z,1,sizeof(internal_state)) );
\r
274 if (z.state = Z_NULL) then
\r
276 inflateInit2_ := Z_MEM_ERROR;
\r
280 z.state^.blocks := Z_NULL;
\r
282 { handle undocumented nowrap option (no zlib header or check) }
\r
283 z.state^.nowrap := FALSE;
\r
287 z.state^.nowrap := TRUE;
\r
290 { set window size }
\r
291 if (w < 8) or (w > 15) then
\r
294 inflateInit2_ := Z_STREAM_ERROR;
\r
297 z.state^.wbits := uInt(w);
\r
299 { create inflate_blocks state }
\r
300 if z.state^.nowrap then
\r
301 z.state^.blocks := inflate_blocks_new(z, NIL, uInt(1) shl w)
\r
304 z.state^.blocks := inflate_blocks_new(z, @adler32, uInt(1) shl w);
\r
306 z.state^.blocks := inflate_blocks_new(z, adler32, uInt(1) shl w);
\r
308 if (z.state^.blocks = Z_NULL) then
\r
311 inflateInit2_ := Z_MEM_ERROR;
\r
315 Tracev('inflate: allocated');
\r
319 inflateInit2_ := Z_OK;
\r
322 function inflateInit2(var z: z_stream; windowBits : int) : int;
\r
324 inflateInit2 := inflateInit2_(z, windowBits, ZLIB_VERSION, sizeof(z_stream));
\r
328 function inflateInit(var z : z_stream) : int;
\r
329 { inflateInit is a macro to allow checking the zlib version
\r
330 and the compiler's view of z_stream: }
\r
332 inflateInit := inflateInit2_(z, DEF_WBITS, ZLIB_VERSION, sizeof(z_stream));
\r
335 function inflateInit_(z : z_streamp;
\r
336 const version : string;
\r
337 stream_size : int) : int;
\r
339 { initialize state }
\r
340 if (z = Z_NULL) then
\r
341 inflateInit_ := Z_STREAM_ERROR
\r
343 inflateInit_ := inflateInit2_(z^, DEF_WBITS, version, stream_size);
\r
346 function inflate(var z : z_stream;
\r
352 if (z.state = Z_NULL) or (z.next_in = Z_NULL) then
\r
354 inflate := Z_STREAM_ERROR;
\r
357 if f = Z_FINISH then
\r
363 case (z.state^.mode) of
\r
366 r := inflate_blocks(z.state^.blocks^, z, r);
\r
367 if (r = Z_DATA_ERROR) then
\r
369 z.state^.mode := BAD;
\r
370 z.state^.sub.marker := 0; { can try inflateSync }
\r
371 continue; { break C-switch }
\r
375 if (r <> Z_STREAM_END) then
\r
381 inflate_blocks_reset(z.state^.blocks^, z, @z.state^.sub.check.was);
\r
382 if (z.state^.nowrap) then
\r
384 z.state^.mode := DONE;
\r
385 continue; { break C-switch }
\r
387 z.state^.mode := CHECK4; { falltrough }
\r
392 if (z.avail_in = 0) then
\r
399 {z.state^.sub.check.need := uLong(NEXTBYTE(z)) shl 24;}
\r
402 z.state^.sub.check.need := uLong(z.next_in^) shl 24;
\r
405 z.state^.mode := CHECK3; { falltrough }
\r
410 if (z.avail_in = 0) then
\r
416 {Inc( z.state^.sub.check.need, uLong(NEXTBYTE(z)) shl 16);}
\r
419 Inc(z.state^.sub.check.need, uLong(z.next_in^) shl 16);
\r
422 z.state^.mode := CHECK2; { falltrough }
\r
427 if (z.avail_in = 0) then
\r
434 {Inc( z.state^.sub.check.need, uLong(NEXTBYTE(z)) shl 8);}
\r
437 Inc(z.state^.sub.check.need, uLong(z.next_in^) shl 8);
\r
440 z.state^.mode := CHECK1; { falltrough }
\r
445 if (z.avail_in = 0) then
\r
451 {Inc( z.state^.sub.check.need, uLong(NEXTBYTE(z)) );}
\r
454 Inc(z.state^.sub.check.need, uLong(z.next_in^) );
\r
458 if (z.state^.sub.check.was <> z.state^.sub.check.need) then
\r
460 z.state^.mode := BAD;
\r
461 z.msg := 'incorrect data check';
\r
462 z.state^.sub.marker := 5; { can't try inflateSync }
\r
463 continue; { break C-switch }
\r
466 Tracev('inflate: zlib check ok');
\r
468 z.state^.mode := DONE; { falltrough }
\r
472 inflate := Z_STREAM_END;
\r
478 if (z.avail_in = 0) then
\r
485 {z.state^.sub.method := NEXTBYTE(z);}
\r
488 z.state^.sub.method := z.next_in^;
\r
491 if ((z.state^.sub.method and $0f) <> Z_DEFLATED) then
\r
493 z.state^.mode := BAD;
\r
494 z.msg := 'unknown compression method';
\r
495 z.state^.sub.marker := 5; { can't try inflateSync }
\r
496 continue; { break C-switch }
\r
498 if ((z.state^.sub.method shr 4) + 8 > z.state^.wbits) then
\r
500 z.state^.mode := BAD;
\r
501 z.msg := 'invalid window size';
\r
502 z.state^.sub.marker := 5; { can't try inflateSync }
\r
503 continue; { break C-switch }
\r
505 z.state^.mode := FLAG;
\r
511 if (z.avail_in = 0) then
\r
517 {b := NEXTBYTE(z);}
\r
523 if (((z.state^.sub.method shl 8) + b) mod 31) <> 0 then {% mod ?}
\r
525 z.state^.mode := BAD;
\r
526 z.msg := 'incorrect header check';
\r
527 z.state^.sub.marker := 5; { can't try inflateSync }
\r
528 continue; { break C-switch }
\r
531 Tracev('inflate: zlib header ok');
\r
533 if ((b and PRESET_DICT) = 0) then
\r
535 z.state^.mode := BLOCKS;
\r
536 continue; { break C-switch }
\r
538 z.state^.mode := DICT4;
\r
543 if (z.avail_in = 0) then
\r
550 {z.state^.sub.check.need := uLong(NEXTBYTE(z)) shl 24;}
\r
553 z.state^.sub.check.need := uLong(z.next_in^) shl 24;
\r
556 z.state^.mode := DICT3; { falltrough }
\r
560 if (z.avail_in = 0) then
\r
566 {Inc(z.state^.sub.check.need, uLong(NEXTBYTE(z)) shl 16);}
\r
569 Inc(z.state^.sub.check.need, uLong(z.next_in^) shl 16);
\r
572 z.state^.mode := DICT2; { falltrough }
\r
576 if (z.avail_in = 0) then
\r
583 {Inc(z.state^.sub.check.need, uLong(NEXTBYTE(z)) shl 8);}
\r
586 Inc(z.state^.sub.check.need, uLong(z.next_in^) shl 8);
\r
589 z.state^.mode := DICT1; { falltrough }
\r
593 if (z.avail_in = 0) then
\r
598 { r := f; --- wird niemals benutzt }
\r
599 {Inc(z.state^.sub.check.need, uLong(NEXTBYTE(z)) );}
\r
602 Inc(z.state^.sub.check.need, uLong(z.next_in^) );
\r
605 z.adler := z.state^.sub.check.need;
\r
606 z.state^.mode := DICT0;
\r
607 inflate := Z_NEED_DICT;
\r
612 z.state^.mode := BAD;
\r
613 z.msg := 'need dictionary';
\r
614 z.state^.sub.marker := 0; { can try inflateSync }
\r
615 inflate := Z_STREAM_ERROR;
\r
620 inflate := Z_DATA_ERROR;
\r
625 inflate := Z_STREAM_ERROR;
\r
629 {$ifdef NEED_DUMMY_result}
\r
630 result := Z_STREAM_ERROR; { Some dumb compilers complain without this }
\r
634 function inflateSetDictionary(var z : z_stream;
\r
635 dictionary : pBytef; {const array of byte}
\r
636 dictLength : uInt) : int;
\r
640 length := dictLength;
\r
642 if (z.state = Z_NULL) or (z.state^.mode <> DICT0) then
\r
644 inflateSetDictionary := Z_STREAM_ERROR;
\r
647 if (adler32(Long(1), dictionary, dictLength) <> z.adler) then
\r
649 inflateSetDictionary := Z_DATA_ERROR;
\r
652 z.adler := Long(1);
\r
654 if (length >= (uInt(1) shl z.state^.wbits)) then
\r
656 length := (1 shl z.state^.wbits)-1;
\r
657 Inc( dictionary, dictLength - length);
\r
659 inflate_set_dictionary(z.state^.blocks^, dictionary^, length);
\r
660 z.state^.mode := BLOCKS;
\r
661 inflateSetDictionary := Z_OK;
\r
665 function inflateSync(var z : z_stream) : int;
\r
667 mark : packed array[0..3] of byte = (0, 0, $ff, $ff);
\r
669 n : uInt; { number of bytes to look at }
\r
670 p : pBytef; { pointer to bytes }
\r
671 m : uInt; { number of marker bytes found in a row }
\r
672 r, w : uLong; { temporaries to save total_in and total_out }
\r
675 if (z.state = Z_NULL) then
\r
677 inflateSync := Z_STREAM_ERROR;
\r
680 if (z.state^.mode <> BAD) then
\r
682 z.state^.mode := BAD;
\r
683 z.state^.sub.marker := 0;
\r
688 inflateSync := Z_BUF_ERROR;
\r
692 m := z.state^.sub.marker;
\r
695 while (n <> 0) and (m < 4) do
\r
697 if (p^ = mark[m]) then
\r
709 Inc(z.total_in, ptr2int(p) - ptr2int(z.next_in));
\r
712 z.state^.sub.marker := m;
\r
715 { return no joy or set up to restart on a new block }
\r
718 inflateSync := Z_DATA_ERROR;
\r
726 z.state^.mode := BLOCKS;
\r
727 inflateSync := Z_OK;
\r
732 returns true if inflate is currently at the end of a block generated
\r
733 by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
\r
734 implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
\r
735 but removes the length bytes of the resulting empty stored block. When
\r
736 decompressing, PPP checks that at the end of input packet, inflate is
\r
737 waiting for these length bytes.
\r
740 function inflateSyncPoint(var z : z_stream) : int;
\r
742 if (z.state = Z_NULL) or (z.state^.blocks = Z_NULL) then
\r
744 inflateSyncPoint := Z_STREAM_ERROR;
\r
747 inflateSyncPoint := inflate_blocks_sync_point(z.state^.blocks^);
\r