5 inffast.c -- process literals and length/distance pairs fast
6 Copyright (C) 1995-1998 Mark Adler
9 Copyright (C) 1998 by Jacques Nomssi Nzali
10 For conditions of distribution and use, see copyright notice in readme.txt
24 function inflate_fast( bl
: uInt
;
28 var s
: inflate_blocks_state
;
29 var z
: z_stream
) : int
;
38 { Called with number of bytes left to write in window at least 258
39 (the maximum string length) and number of input bytes available
40 at least ten. The ten bytes are six bytes for the longest length/
41 distance pair plus four bytes for overloading the bit buffer. }
43 function inflate_fast( bl
: uInt
;
47 var s
: inflate_blocks_state
;
48 var z
: z_stream
) : int
;
51 t
: pInflate_huft
; { temporary pointer }
52 e
: uInt
; { extra bits or operation }
53 b
: uLong
; { bit buffer }
54 k
: uInt
; { bits in bit buffer }
55 p
: pBytef
; { input data pointer }
56 n
: uInt
; { bytes available there }
57 q
: pBytef
; { output window write pointer }
58 m
: uInt
; { bytes to end of window or read pointer }
59 ml
: uInt
; { mask for literal/length tree }
60 md
: uInt
; { mask for distance tree }
61 c
: uInt
; { bytes to copy }
62 d
: uInt
; { distance back to copy from }
63 r
: pBytef
; { copy source pointer }
65 { load input, output, bit values (macro LOAD) }
71 if ptr2int(q
) < ptr2int(s
.read
) then
72 m
:= uInt(ptr2int(s
.read
)-ptr2int(q
)-1)
74 m
:= uInt(ptr2int(s
.zend
)-ptr2int(q
));
77 ml
:= inflate_mask
[bl
];
78 md
:= inflate_mask
[bd
];
80 { do until not enough input or output space for fast loop }
81 repeat { assume called with (m >= 258) and (n >= 10) }
82 { get literal/length code }
83 {GRABBITS(20);} { max bits for literal/length code }
87 b
:= b
or (uLong(p
^) shl k
);
92 t
:= @(huft_ptr(tl
)^[uInt(b
) and ml
]);
101 if (t
^.base
>= $20) and (t
^.base
< $7f) then
102 Tracevv('inflate: * literal '+AnsiChar(t
^.base
))
104 Tracevv('inflate: * literal '+ IntToStr(t
^.base
));
116 if (e
and 16 <> 0) then
118 { get extra bits for length }
120 c
:= t
^.base
+ (uInt(b
) and inflate_mask
[e
]);
125 Tracevv('inflate: * length ' + IntToStr(c
));
127 { decode distance base of block to copy }
128 {GRABBITS(15);} { max bits for distance code }
132 b
:= b
or (uLong(p
^) shl k
);
137 t
:= @huft_ptr(td
)^[uInt(b
) and md
];
144 if (e
and 16 <> 0) then
146 { get extra bits to add to distance base }
148 {GRABBITS(e);} { get extra bits (up to 13) }
152 b
:= b
or (uLong(p
^) shl k
);
157 d
:= t
^.base
+ (uInt(b
) and inflate_mask
[e
]);
163 Tracevv('inflate: * distance '+IntToStr(d
));
167 if (uInt(ptr2int(q
) - ptr2int(s
.window
)) >= d
) then { offset before dest }
171 q
^ := r
^; Inc(q
); Inc(r
); Dec(c
); { minimum count is three, }
172 q
^ := r
^; Inc(q
); Inc(r
); Dec(c
); { so unroll loop a little }
174 else { else offset after destination }
176 e
:= d
- uInt(ptr2int(q
) - ptr2int(s
.window
)); { bytes from offset to end }
178 Dec(r
, e
); { pointer to offset }
179 if (c
> e
) then { if source crosses, }
181 Dec(c
, e
); { copy to end of window }
188 r
:= s
.window
; { copy rest from start of window }
191 repeat { copy all or what's left }
200 if (e
and 64 = 0) then
202 Inc(t
, t
^.base
+ (uInt(b
) and inflate_mask
[e
]));
207 z
.msg
:= 'invalid distance code';
210 if (k
shr 3) < c
then
219 Inc(z
.total_in
, ptr2int(p
)-ptr2int(z
.next_in
));
223 inflate_fast
:= Z_DATA_ERROR
;
229 if (e
and 64 = 0) then
232 e = (t += ((uInt)b & inflate_mask[e]))->exop;}
234 Inc(t
, t
^.base
+ (uInt(b
) and inflate_mask
[e
]));
243 if (t
^.base
>= $20) and (t
^.base
< $7f) then
244 Tracevv('inflate: * literal '+AnsiChar(t
^.base
))
246 Tracevv('inflate: * literal '+IntToStr(t
^.base
));
255 if (e
and 32 <> 0) then
258 Tracevv('inflate: * end of block');
262 if (k
shr 3) < c
then
271 Inc(z
.total_in
, ptr2int(p
)-ptr2int(z
.next_in
));
274 inflate_fast
:= Z_STREAM_END
;
279 z
.msg
:= 'invalid literal/length code';
282 if (k
shr 3) < c
then
291 Inc(z
.total_in
, ptr2int(p
)-ptr2int(z
.next_in
));
294 inflate_fast
:= Z_DATA_ERROR
;
298 until (m
< 258) or (n
< 10);
300 { not enough input or output--restore pointers and return }
303 if (k
shr 3) < c
then
312 Inc(z
.total_in
, ptr2int(p
)-ptr2int(z
.next_in
));
315 inflate_fast
:= Z_OK
;