413e9b6d74a47114dbbf8cace98bbdb6b321dd1a
3 { infblock.h and
4 infblock.c -- interpret and process block types to last block
5 Copyright (C) 1995-1998 Mark Adler
7 Pascal tranlastion
8 Copyright (C) 1998 by Jacques Nomssi Nzali
9 For conditions of distribution and use, see copyright notice in readme.txt
10 }
12 interface
14 {$I imzconf.inc}
16 uses
17 {$IFDEF DEBUG}
19 {$ENDIF}
46 implementation
48 uses
51 { Tables for deflate from PKZIP's appnote.txt. }
52 Const
56 { Notes beyond the 1.93a appnote.txt:
58 1. Distance pointers never point before the beginning of the output
59 stream.
60 2. Distance pointers can point back across blocks, up to 32k away.
61 3. There is an implied maximum of 7 bits for the bit length table and
62 15 bits for the actual data.
63 4. If only one code exists, then it is encoded using one bit. (Zero
64 would be more efficient, but perhaps a little confusing.) If two
65 codes exist, they are coded using one bit each (0 and 1).
66 5. There is no way of sending zero distance codes--a dummy must be
67 sent if there are none. (History: a pre 2.0 version of PKZIP would
68 store blocks with no distance codes, but this was discovered to be
69 too harsh a criterion.) Valid only for 1.93a. 2.04c does allow
70 zero distance codes, which is sent as one code of zero bits in
71 length.
72 6. There are up to 286 literal/length codes. Code 256 represents the
73 end-of-block. Note however that the static length tree defines
74 288 codes just to fill out the Huffman codes. Codes 286 and 287
75 cannot be used though, since there is no length base or extra bits
76 defined for them. Similarily, there are up to 30 distance codes.
77 However, static trees define 32 codes (all 5 bits) to fill out the
78 Huffman codes, but the last two had better not show up in the data.
79 7. Unzip can check dynamic Huffman blocks for complete code sets.
80 The exception is that a single code would not be complete (see #4).
81 8. The five bits following the block type is really the number of
82 literal codes sent minus 257.
83 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
84 (1+6+6). Therefore, to output three times the length, you output
85 three codes (1+1+1), whereas to output four times the same length,
86 you only need two codes (1+3). Hmm.
87 10. In the tree reconstruction algorithm, Code = Code + Increment
88 only if BitLength(i) is not zero. (Pretty obvious.)
89 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19)
90 12. Note: length code 284 can represent 227-258, but length code 285
91 really is 258. The last length deserves its own, short code
92 since it gets used a lot in very redundant files. The length
93 258 is special since 258 - 3 (the min match length) is 255.
94 13. The literal/length and distance code bit lengths are read as a
95 single stream of lengths. It is possible (and advantageous) for
96 a repeat code (16, 17, or 18) to go across the boundary between
97 the two sets of lengths. }
103 begin
118 begin
122 {$IFDEF DEBUG}
124 {$ENDIF}
132 var
134 begin
137 begin
139 exit;
144 begin
147 exit;
152 begin
156 exit;
162 {$IFDEF DEBUG}
164 {$ENDIF}
173 label
176 start_codes;
178 var
186 { fixed code blocks }
187 var
190 var
193 var
195 begin
196 { copy input/output information to locals }
204 else
207 { decompress an inflated block }
210 { process input based on current state }
213 ZTYPE:
214 begin
215 {NEEDBITS(3);}
217 begin
218 {NEEDBYTE;}
220 r :=Z_OK
221 else
222 begin
223 {UPDATE}
231 exit;
243 begin
244 {$IFDEF DEBUG}
247 else
249 {$ENDIF}
250 {DUMPBITS(3);}
255 {DUMPBITS(t);}
262 begin
263 begin
264 {$IFDEF DEBUG}
267 else
269 {$ENDIF}
273 begin
275 { update pointers and return }
283 exit;
286 {DUMPBITS(3);}
293 begin
294 {$IFDEF DEBUG}
297 else
299 {$ENDIF}
300 {DUMPBITS(3);}
308 {DUMPBITS(3);}
315 { update pointers and return }
323 exit;
327 LENS:
328 begin
329 {NEEDBITS(32);}
331 begin
332 {NEEDBYTE;}
334 r :=Z_OK
335 else
336 begin
337 {UPDATE}
345 exit;
354 begin
358 { update pointers and return }
366 exit;
371 {$IFDEF DEBUG}
373 {$ENDIF}
376 else
379 else
382 STORED:
383 begin
385 begin
386 { update pointers and return }
394 exit;
396 {NEEDOUT}
398 begin
399 {WRAP}
401 begin
405 else
410 begin
411 {FLUSH}
417 else
420 {WRAP}
422 begin
426 else
431 begin
432 {UPDATE}
440 exit;
456 begin
457 {$IFDEF DEBUG}
461 else
465 {$ENDIF}
468 else
472 TABLE:
473 begin
474 {NEEDBITS(14);}
476 begin
477 {NEEDBYTE;}
479 r :=Z_OK
480 else
481 begin
482 {UPDATE}
490 exit;
500 {$ifndef PKZIP_BUG_WORKAROUND}
502 begin
506 { update pointers and return }
514 exit;
516 {$endif}
520 begin
522 { update pointers and return }
530 exit;
532 {DUMPBITS(14);}
537 {$IFDEF DEBUG}
539 {$ENDIF}
541 { fall trough case is handled by the while }
542 { try GOTO for speed - Nomssi }
545 BTREE:
546 begin
547 start_btree:
549 begin
550 {NEEDBITS(3);}
552 begin
553 {NEEDBYTE;}
555 r :=Z_OK
556 else
557 begin
558 {UPDATE}
566 exit;
576 {DUMPBITS(3);}
581 begin
589 begin
594 { update pointers and return }
602 exit;
605 {$IFDEF DEBUG}
607 {$ENDIF}
609 { fall through again }
612 DTREE:
613 begin
614 start_dtree:
616 begin
620 break;
622 {NEEDBITS(t);}
624 begin
625 {NEEDBYTE;}
627 r :=Z_OK
628 else
629 begin
630 {UPDATE}
638 exit;
652 begin
653 {DUMPBITS(t);}
659 end
661 begin
663 begin
666 end
667 else
668 begin
672 {NEEDBITS(t + i);}
674 begin
675 {NEEDBYTE;}
677 r :=Z_OK
678 else
679 begin
680 {UPDATE}
688 exit;
696 {DUMPBITS(t);}
701 {DUMPBITS(i);}
709 begin
714 { update pointers and return }
722 exit;
726 else
728 repeat
737 begin
746 begin
750 { update pointers and return }
758 exit;
760 {$IFDEF DEBUG}
762 {$ENDIF}
763 { c renamed to cs }
766 begin
768 { update pointers and return }
776 exit;
781 { yet another falltrough }
784 CODES:
785 begin
786 start_codes:
787 { update pointers }
797 begin
799 exit;
803 { load local pointers }
811 else
813 {$IFDEF DEBUG}
817 else
821 {$ENDIF}
823 begin
827 {$ifndef patch112}
829 begin
830 {$IFDEF DEBUG}
832 {$ENDIF}
837 {$endif}
839 { another falltrough }
842 DRY:
843 begin
844 start_dry:
845 {FLUSH}
850 { not needed anymore, we are done:
851 if ptr2int(q) < ptr2int(s.read) then
852 m := uInt(ptr2int(s.read)-ptr2int(q)-1)
853 else
854 m := uInt(ptr2int(s.zend)-ptr2int(q));
855 }
858 begin
859 { update pointers and return }
867 exit;
872 BLKDONE:
873 begin
874 start_blkdone:
876 { update pointers and return }
884 exit;
886 BLKBAD:
887 begin
889 { update pointers and return }
897 exit;
899 else
900 begin
902 { update pointers and return }
910 exit;
919 begin
924 {$IFDEF DEBUG}
926 {$ENDIF}
934 begin
942 { Returns true if inflate is currently at the end of a block generated
943 by Z_SYNC_FLUSH or Z_FULL_FLUSH.
944 IN assertion: s <> Z_NULL }
947 begin