DEADSOFTWARE

hopefully no more windows
[d2df-editor.git] / src / lib / vampimg / ZLib / iminfutil.pas
1 Unit iminfutil;
3 { types and macros common to blocks and codes
4 Copyright (C) 1995-1998 Mark Adler
6 WARNING: this file should *not* be used by applications. It is
7 part of the implementation of the compression library and is
8 subject to change.
10 Pascal tranlastion
11 Copyright (C) 1998 by Jacques Nomssi Nzali
12 For conditions of distribution and use, see copyright notice in readme.txt
13 }
15 interface
17 {$I imzconf.inc}
19 uses
20 imzutil, impaszlib;
22 { copy as much as possible from the sliding window to the output area }
23 function inflate_flush(var s : inflate_blocks_state;
24 var z : z_stream;
25 r : int) : int;
27 { And'ing with mask[n] masks the lower n bits }
28 const
29 inflate_mask : array[0..17-1] of uInt = (
30 $0000,
31 $0001, $0003, $0007, $000f, $001f, $003f, $007f, $00ff,
32 $01ff, $03ff, $07ff, $0fff, $1fff, $3fff, $7fff, $ffff);
34 {procedure GRABBITS(j : int);}
35 {procedure DUMPBITS(j : int);}
36 {procedure NEEDBITS(j : int);}
38 implementation
40 { macros for bit input with no checking and for returning unused bytes }
41 procedure GRABBITS(j : int);
42 begin
43 {while (k < j) do
44 begin
45 Dec(z^.avail_in);
46 Inc(z^.total_in);
47 b := b or (uLong(z^.next_in^) shl k);
48 Inc(z^.next_in);
49 Inc(k, 8);
50 end;}
51 end;
53 procedure DUMPBITS(j : int);
54 begin
55 {b := b shr j;
56 Dec(k, j);}
57 end;
59 procedure NEEDBITS(j : int);
60 begin
61 (*
62 while (k < j) do
63 begin
64 {NEEDBYTE;}
65 if (n <> 0) then
66 r :=Z_OK
67 else
68 begin
69 {UPDATE}
70 s.bitb := b;
71 s.bitk := k;
72 z.avail_in := n;
73 Inc(z.total_in, LongInt(p)-LongInt(z.next_in));
74 z.next_in := p;
75 s.write := q;
76 result := inflate_flush(s,z,r);
77 exit;
78 end;
79 Dec(n);
80 b := b or (uLong(p^) shl k);
81 Inc(p);
82 Inc(k, 8);
83 end;
84 *)
85 end;
87 procedure NEEDOUT;
88 begin
89 (*
90 if (m = 0) then
91 begin
92 {WRAP}
93 if (q = s.zend) and (s.read <> s.window) then
94 begin
95 q := s.window;
96 if LongInt(q) < LongInt(s.read) then
97 m := uInt(LongInt(s.read)-LongInt(q)-1)
98 else
99 m := uInt(LongInt(s.zend)-LongInt(q));
100 end;
102 if (m = 0) then
103 begin
104 {FLUSH}
105 s.write := q;
106 r := inflate_flush(s,z,r);
107 q := s.write;
108 if LongInt(q) < LongInt(s.read) then
109 m := uInt(LongInt(s.read)-LongInt(q)-1)
110 else
111 m := uInt(LongInt(s.zend)-LongInt(q));
113 {WRAP}
114 if (q = s.zend) and (s.read <> s.window) then
115 begin
116 q := s.window;
117 if LongInt(q) < LongInt(s.read) then
118 m := uInt(LongInt(s.read)-LongInt(q)-1)
119 else
120 m := uInt(LongInt(s.zend)-LongInt(q));
121 end;
123 if (m = 0) then
124 begin
125 {UPDATE}
126 s.bitb := b;
127 s.bitk := k;
128 z.avail_in := n;
129 Inc(z.total_in, LongInt(p)-LongInt(z.next_in));
130 z.next_in := p;
131 s.write := q;
132 result := inflate_flush(s,z,r);
133 exit;
134 end;
135 end;
136 end;
137 r := Z_OK;
138 *)
139 end;
141 { copy as much as possible from the sliding window to the output area }
142 function inflate_flush(var s : inflate_blocks_state;
143 var z : z_stream;
144 r : int) : int;
145 var
146 n : uInt;
147 p : pBytef;
148 q : pBytef;
149 begin
150 { local copies of source and destination pointers }
151 p := z.next_out;
152 q := s.read;
154 { compute number of bytes to copy as far as end of window }
155 if ptr2int(q) <= ptr2int(s.write) then
156 n := uInt(ptr2int(s.write) - ptr2int(q))
157 else
158 n := uInt(ptr2int(s.zend) - ptr2int(q));
159 if (n > z.avail_out) then
160 n := z.avail_out;
161 if (n <> 0) and (r = Z_BUF_ERROR) then
162 r := Z_OK;
164 { update counters }
165 Dec(z.avail_out, n);
166 Inc(z.total_out, n);
169 { update check information }
170 if Assigned(s.checkfn) then
171 begin
172 s.check := s.checkfn(s.check, q, n);
173 z.adler := s.check;
174 end;
176 { copy as far as end of window }
177 zmemcpy(p, q, n);
178 Inc(p, n);
179 Inc(q, n);
181 { see if more to copy at beginning of window }
182 if (q = s.zend) then
183 begin
184 { wrap pointers }
185 q := s.window;
186 if (s.write = s.zend) then
187 s.write := s.window;
189 { compute bytes to copy }
190 n := uInt(ptr2int(s.write) - ptr2int(q));
191 if (n > z.avail_out) then
192 n := z.avail_out;
193 if (n <> 0) and (r = Z_BUF_ERROR) then
194 r := Z_OK;
196 { update counters }
197 Dec( z.avail_out, n);
198 Inc( z.total_out, n);
200 { update check information }
201 if Assigned(s.checkfn) then
202 begin
203 s.check := s.checkfn(s.check, q, n);
204 z.adler := s.check;
205 end;
207 { copy }
208 zmemcpy(p, q, n);
209 Inc(p, n);
210 Inc(q, n);
211 end;
214 { update pointers }
215 z.next_out := p;
216 s.read := q;
218 { done }
219 inflate_flush := r;
220 end;
222 end.