3 { inflate.c -- zlib interface to inflate modules
4 Copyright (C) 1995-1998 Mark Adler
6 Pascal tranlastion
7 Copyright (C) 1998 by Jacques Nomssi Nzali
8 For conditions of distribution and use, see copyright notice in readme.txt
9 }
11 interface
13 {$I imzconf.inc}
15 uses
20 { Initializes the internal stream state for decompression. The fields
21 zalloc, zfree and opaque must be initialized before by the caller. If
22 zalloc and zfree are set to Z_NULL, inflateInit updates them to use default
23 allocation functions.
25 inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
26 enough memory, Z_VERSION_ERROR if the zlib library version is incompatible
27 with the version assumed by the caller. msg is set to null if there is no
28 error message. inflateInit does not perform any decompression: this will be
29 done by inflate(). }
46 {
47 This is another version of inflateInit with an extra parameter. The
48 fields next_in, avail_in, zalloc, zfree and opaque must be initialized
49 before by the caller.
51 The windowBits parameter is the base two logarithm of the maximum window
52 size (the size of the history buffer). It should be in the range 8..15 for
53 this version of the library. The default value is 15 if inflateInit is used
54 instead. If a compressed stream with a larger window size is given as
55 input, inflate() will return with the error code Z_DATA_ERROR instead of
56 trying to allocate a larger window.
58 inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
59 memory, Z_STREAM_ERROR if a parameter is invalid (such as a negative
60 memLevel). msg is set to null if there is no error message. inflateInit2
61 does not perform any decompression apart from reading the zlib header if
62 present: this will be done by inflate(). (So next_in and avail_in may be
63 modified, but next_out and avail_out are unchanged.)
64 }
70 {
71 All dynamically allocated data structures for this stream are freed.
72 This function discards any unprocessed input and does not flush any
73 pending output.
75 inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
76 was inconsistent. In the error case, msg may be set but then points to a
77 static string (which must not be deallocated).
78 }
82 {
83 This function is equivalent to inflateEnd followed by inflateInit,
84 but does not free and reallocate all the internal decompression state.
85 The stream will keep attributes that may have been set by inflateInit2.
87 inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
88 stream state was inconsistent (such as zalloc or state being NULL).
89 }
94 {
95 inflate decompresses as much data as possible, and stops when the input
96 buffer becomes empty or the output buffer becomes full. It may introduce
97 some output latency (reading input without producing any output)
98 except when forced to flush.
100 The detailed semantics are as follows. inflate performs one or both of the
101 following actions:
103 - Decompress more input starting at next_in and update next_in and avail_in
104 accordingly. If not all input can be processed (because there is not
105 enough room in the output buffer), next_in is updated and processing
106 will resume at this point for the next call of inflate().
108 - Provide more output starting at next_out and update next_out and avail_out
109 accordingly. inflate() provides as much output as possible, until there
110 is no more input data or no more space in the output buffer (see below
111 about the flush parameter).
113 Before the call of inflate(), the application should ensure that at least
114 one of the actions is possible, by providing more input and/or consuming
115 more output, and updating the next_* and avail_* values accordingly.
116 The application can consume the uncompressed output when it wants, for
117 example when the output buffer is full (avail_out == 0), or after each
118 call of inflate(). If inflate returns Z_OK and with zero avail_out, it
119 must be called again after making room in the output buffer because there
120 might be more output pending.
122 If the parameter flush is set to Z_SYNC_FLUSH, inflate flushes as much
123 output as possible to the output buffer. The flushing behavior of inflate is
124 not specified for values of the flush parameter other than Z_SYNC_FLUSH
125 and Z_FINISH, but the current implementation actually flushes as much output
126 as possible anyway.
128 inflate() should normally be called until it returns Z_STREAM_END or an
129 error. However if all decompression is to be performed in a single step
130 (a single call of inflate), the parameter flush should be set to
131 Z_FINISH. In this case all pending input is processed and all pending
132 output is flushed; avail_out must be large enough to hold all the
133 uncompressed data. (The size of the uncompressed data may have been saved
134 by the compressor for this purpose.) The next operation on this stream must
135 be inflateEnd to deallocate the decompression state. The use of Z_FINISH
136 is never required, but can be used to inform inflate that a faster routine
137 may be used for the single inflate() call.
139 If a preset dictionary is needed at this point (see inflateSetDictionary
140 below), inflate sets strm-adler to the adler32 checksum of the
141 dictionary chosen by the compressor and returns Z_NEED_DICT; otherwise
142 it sets strm->adler to the adler32 checksum of all output produced
143 so far (that is, total_out bytes) and returns Z_OK, Z_STREAM_END or
144 an error code as described below. At the end of the stream, inflate()
145 checks that its computed adler32 checksum is equal to that saved by the
146 compressor and returns Z_STREAM_END only if the checksum is correct.
148 inflate() returns Z_OK if some progress has been made (more input processed
149 or more output produced), Z_STREAM_END if the end of the compressed data has
150 been reached and all uncompressed output has been produced, Z_NEED_DICT if a
151 preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
152 corrupted (input stream not conforming to the zlib format or incorrect
153 adler32 checksum), Z_STREAM_ERROR if the stream structure was inconsistent
154 (for example if next_in or next_out was NULL), Z_MEM_ERROR if there was not
155 enough memory, Z_BUF_ERROR if no progress is possible or if there was not
156 enough room in the output buffer when Z_FINISH is used. In the Z_DATA_ERROR
157 case, the application may then call inflateSync to look for a good
158 compression block.
159 }
166 {
167 Initializes the decompression dictionary from the given uncompressed byte
168 sequence. This function must be called immediately after a call of inflate
169 if this call returned Z_NEED_DICT. The dictionary chosen by the compressor
170 can be determined from the Adler32 value returned by this call of
171 inflate. The compressor and decompressor must use exactly the same
172 dictionary (see deflateSetDictionary).
174 inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
175 parameter is invalid (such as NULL dictionary) or the stream state is
176 inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
177 expected one (incorrect Adler32 value). inflateSetDictionary does not
178 perform any decompression: this will be done by subsequent calls of
179 inflate().
180 }
184 {
185 Skips invalid compressed data until a full flush point (see above the
186 description of deflate with Z_FULL_FLUSH) can be found, or until all
187 available input is skipped. No output is provided.
189 inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR
190 if no more input was provided, Z_DATA_ERROR if no flush point has been found,
191 or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
192 case, the application may save the current current value of total_in which
193 indicates where valid compressed data was found. In the error case, the
194 application may repeatedly call inflateSync, providing more input each time,
195 until success or end of the input data.
196 }
202 implementation
204 uses
205 imadler;
208 begin
210 begin
212 exit;
219 else
222 {$IFDEF DEBUG}
224 {$ENDIF}
230 begin
232 begin
234 exit;
240 {$IFDEF DEBUG}
242 {$ENDIF}
251 begin
254 begin
256 exit;
258 { initialize state }
259 { SetLength(strm.msg, 255); }
262 begin
265 {$endif}
271 {$ENDIF}
275 begin
277 exit;
282 { handle undocumented nowrap option (no zlib header or check) }
285 begin
290 { set window size }
292 begin
295 exit;
299 { create inflate_blocks state }
302 else
303 {$IFDEF FPC}
305 {$ELSE}
307 {$ENDIF}
309 begin
312 exit;
314 {$IFDEF DEBUG}
316 {$ENDIF}
317 { reset state }
323 begin
329 { inflateInit is a macro to allow checking the zlib version
330 and the compiler's view of z_stream: }
331 begin
338 begin
339 { initialize state }
341 inflateInit_ := Z_STREAM_ERROR
342 else
348 var
351 begin
353 begin
355 exit;
358 f := Z_BUF_ERROR
359 else
364 BLOCKS:
365 begin
368 begin
376 begin
378 exit;
383 begin
389 CHECK4:
390 begin
391 {NEEDBYTE}
393 begin
395 exit;
399 {z.state^.sub.check.need := uLong(NEXTBYTE(z)) shl 24;}
407 CHECK3:
408 begin
409 {NEEDBYTE}
411 begin
413 exit;
416 {Inc( z.state^.sub.check.need, uLong(NEXTBYTE(z)) shl 16);}
424 CHECK2:
425 begin
426 {NEEDBYTE}
428 begin
430 exit;
434 {Inc( z.state^.sub.check.need, uLong(NEXTBYTE(z)) shl 8);}
442 CHECK1:
443 begin
444 {NEEDBYTE}
446 begin
448 exit;
451 {Inc( z.state^.sub.check.need, uLong(NEXTBYTE(z)) );}
459 begin
465 {$IFDEF DEBUG}
467 {$ENDIF}
470 DONE:
471 begin
473 exit;
475 METHOD:
476 begin
477 {NEEDBYTE}
479 begin
481 exit;
485 {z.state^.sub.method := NEXTBYTE(z);}
492 begin
499 begin
506 { fall trough }
508 FLAG:
509 begin
510 {NEEDBYTE}
512 begin
514 exit;
517 {b := NEXTBYTE(z);}
524 begin
530 {$IFDEF DEBUG}
532 {$ENDIF}
534 begin
539 { falltrough }
541 DICT4:
542 begin
544 begin
546 exit;
550 {z.state^.sub.check.need := uLong(NEXTBYTE(z)) shl 24;}
558 DICT3:
559 begin
561 begin
563 exit;
566 {Inc(z.state^.sub.check.need, uLong(NEXTBYTE(z)) shl 16);}
574 DICT2:
575 begin
577 begin
579 exit;
583 {Inc(z.state^.sub.check.need, uLong(NEXTBYTE(z)) shl 8);}
591 DICT1:
592 begin
594 begin
596 exit;
598 { r := f; --- wird niemals benutzt }
599 {Inc(z.state^.sub.check.need, uLong(NEXTBYTE(z)) );}
608 exit;
610 DICT0:
611 begin
616 exit;
618 BAD:
619 begin
621 exit;
623 else
624 begin
626 exit;
629 {$ifdef NEED_DUMMY_result}
631 {$endif}
637 var
639 begin
643 begin
645 exit;
648 begin
650 exit;
655 begin
666 const
668 var
673 begin
674 { set up }
676 begin
678 exit;
681 begin
687 begin
689 exit;
694 { search }
696 begin
699 else
702 else
708 { restore }
715 { return no joy or set up to restart on a new block }
717 begin
719 exit;
731 {
732 returns true if inflate is currently at the end of a block generated
733 by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
734 implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
735 but removes the length bytes of the resulting empty stored block. When
736 decompressing, PPP checks that at the end of input packet, inflate is
737 waiting for these length bytes.
738 }
741 begin
743 begin
745 exit;