example.pas revision 1.1 1 1.1 christos (* example.c -- usage example of the zlib compression library
2 1.1 christos * Copyright (C) 1995-2003 Jean-loup Gailly.
3 1.1 christos * For conditions of distribution and use, see copyright notice in zlib.h
4 1.1 christos *
5 1.1 christos * Pascal translation
6 1.1 christos * Copyright (C) 1998 by Jacques Nomssi Nzali.
7 1.1 christos * For conditions of distribution and use, see copyright notice in readme.txt
8 1.1 christos *
9 1.1 christos * Adaptation to the zlibpas interface
10 1.1 christos * Copyright (C) 2003 by Cosmin Truta.
11 1.1 christos * For conditions of distribution and use, see copyright notice in readme.txt
12 1.1 christos *)
13 1.1 christos
14 1.1 christos program example;
15 1.1 christos
16 1.1 christos {$DEFINE TEST_COMPRESS}
17 1.1 christos {DO NOT $DEFINE TEST_GZIO}
18 1.1 christos {$DEFINE TEST_DEFLATE}
19 1.1 christos {$DEFINE TEST_INFLATE}
20 1.1 christos {$DEFINE TEST_FLUSH}
21 1.1 christos {$DEFINE TEST_SYNC}
22 1.1 christos {$DEFINE TEST_DICT}
23 1.1 christos
24 1.1 christos uses SysUtils, zlibpas;
25 1.1 christos
26 1.1 christos const TESTFILE = 'foo.gz';
27 1.1 christos
28 1.1 christos (* "hello world" would be more standard, but the repeated "hello"
29 1.1 christos * stresses the compression code better, sorry...
30 1.1 christos *)
31 1.1 christos const hello: PChar = 'hello, hello!';
32 1.1 christos
33 1.1 christos const dictionary: PChar = 'hello';
34 1.1 christos
35 1.1 christos var dictId: LongInt; (* Adler32 value of the dictionary *)
36 1.1 christos
37 1.1 christos procedure CHECK_ERR(err: Integer; msg: String);
38 1.1 christos begin
39 1.1 christos if err <> Z_OK then
40 1.1 christos begin
41 1.1 christos WriteLn(msg, ' error: ', err);
42 1.1 christos Halt(1);
43 1.1 christos end;
44 1.1 christos end;
45 1.1 christos
46 1.1 christos procedure EXIT_ERR(const msg: String);
47 1.1 christos begin
48 1.1 christos WriteLn('Error: ', msg);
49 1.1 christos Halt(1);
50 1.1 christos end;
51 1.1 christos
52 1.1 christos (* ===========================================================================
53 1.1 christos * Test compress and uncompress
54 1.1 christos *)
55 1.1 christos {$IFDEF TEST_COMPRESS}
56 1.1 christos procedure test_compress(compr: Pointer; comprLen: LongInt;
57 1.1 christos uncompr: Pointer; uncomprLen: LongInt);
58 1.1 christos var err: Integer;
59 1.1 christos len: LongInt;
60 1.1 christos begin
61 1.1 christos len := StrLen(hello)+1;
62 1.1 christos
63 1.1 christos err := compress(compr, comprLen, hello, len);
64 1.1 christos CHECK_ERR(err, 'compress');
65 1.1 christos
66 1.1 christos StrCopy(PChar(uncompr), 'garbage');
67 1.1 christos
68 1.1 christos err := uncompress(uncompr, uncomprLen, compr, comprLen);
69 1.1 christos CHECK_ERR(err, 'uncompress');
70 1.1 christos
71 1.1 christos if StrComp(PChar(uncompr), hello) <> 0 then
72 1.1 christos EXIT_ERR('bad uncompress')
73 1.1 christos else
74 1.1 christos WriteLn('uncompress(): ', PChar(uncompr));
75 1.1 christos end;
76 1.1 christos {$ENDIF}
77 1.1 christos
78 1.1 christos (* ===========================================================================
79 1.1 christos * Test read/write of .gz files
80 1.1 christos *)
81 1.1 christos {$IFDEF TEST_GZIO}
82 1.1 christos procedure test_gzio(const fname: PChar; (* compressed file name *)
83 1.1 christos uncompr: Pointer;
84 1.1 christos uncomprLen: LongInt);
85 1.1 christos var err: Integer;
86 1.1 christos len: Integer;
87 1.1 christos zfile: gzFile;
88 1.1 christos pos: LongInt;
89 1.1 christos begin
90 1.1 christos len := StrLen(hello)+1;
91 1.1 christos
92 1.1 christos zfile := gzopen(fname, 'wb');
93 1.1 christos if zfile = NIL then
94 1.1 christos begin
95 1.1 christos WriteLn('gzopen error');
96 1.1 christos Halt(1);
97 1.1 christos end;
98 1.1 christos gzputc(zfile, 'h');
99 1.1 christos if gzputs(zfile, 'ello') <> 4 then
100 1.1 christos begin
101 1.1 christos WriteLn('gzputs err: ', gzerror(zfile, err));
102 1.1 christos Halt(1);
103 1.1 christos end;
104 1.1 christos {$IFDEF GZ_FORMAT_STRING}
105 1.1 christos if gzprintf(zfile, ', %s!', 'hello') <> 8 then
106 1.1 christos begin
107 1.1 christos WriteLn('gzprintf err: ', gzerror(zfile, err));
108 1.1 christos Halt(1);
109 1.1 christos end;
110 1.1 christos {$ELSE}
111 1.1 christos if gzputs(zfile, ', hello!') <> 8 then
112 1.1 christos begin
113 1.1 christos WriteLn('gzputs err: ', gzerror(zfile, err));
114 1.1 christos Halt(1);
115 1.1 christos end;
116 1.1 christos {$ENDIF}
117 1.1 christos gzseek(zfile, 1, SEEK_CUR); (* add one zero byte *)
118 1.1 christos gzclose(zfile);
119 1.1 christos
120 1.1 christos zfile := gzopen(fname, 'rb');
121 1.1 christos if zfile = NIL then
122 1.1 christos begin
123 1.1 christos WriteLn('gzopen error');
124 1.1 christos Halt(1);
125 1.1 christos end;
126 1.1 christos
127 1.1 christos StrCopy(PChar(uncompr), 'garbage');
128 1.1 christos
129 1.1 christos if gzread(zfile, uncompr, uncomprLen) <> len then
130 1.1 christos begin
131 1.1 christos WriteLn('gzread err: ', gzerror(zfile, err));
132 1.1 christos Halt(1);
133 1.1 christos end;
134 1.1 christos if StrComp(PChar(uncompr), hello) <> 0 then
135 1.1 christos begin
136 1.1 christos WriteLn('bad gzread: ', PChar(uncompr));
137 1.1 christos Halt(1);
138 1.1 christos end
139 1.1 christos else
140 1.1 christos WriteLn('gzread(): ', PChar(uncompr));
141 1.1 christos
142 1.1 christos pos := gzseek(zfile, -8, SEEK_CUR);
143 1.1 christos if (pos <> 6) or (gztell(zfile) <> pos) then
144 1.1 christos begin
145 1.1 christos WriteLn('gzseek error, pos=', pos, ', gztell=', gztell(zfile));
146 1.1 christos Halt(1);
147 1.1 christos end;
148 1.1 christos
149 1.1 christos if gzgetc(zfile) <> ' ' then
150 1.1 christos begin
151 1.1 christos WriteLn('gzgetc error');
152 1.1 christos Halt(1);
153 1.1 christos end;
154 1.1 christos
155 1.1 christos if gzungetc(' ', zfile) <> ' ' then
156 1.1 christos begin
157 1.1 christos WriteLn('gzungetc error');
158 1.1 christos Halt(1);
159 1.1 christos end;
160 1.1 christos
161 1.1 christos gzgets(zfile, PChar(uncompr), uncomprLen);
162 1.1 christos uncomprLen := StrLen(PChar(uncompr));
163 1.1 christos if uncomprLen <> 7 then (* " hello!" *)
164 1.1 christos begin
165 1.1 christos WriteLn('gzgets err after gzseek: ', gzerror(zfile, err));
166 1.1 christos Halt(1);
167 1.1 christos end;
168 1.1 christos if StrComp(PChar(uncompr), hello + 6) <> 0 then
169 1.1 christos begin
170 1.1 christos WriteLn('bad gzgets after gzseek');
171 1.1 christos Halt(1);
172 1.1 christos end
173 1.1 christos else
174 1.1 christos WriteLn('gzgets() after gzseek: ', PChar(uncompr));
175 1.1 christos
176 1.1 christos gzclose(zfile);
177 1.1 christos end;
178 1.1 christos {$ENDIF}
179 1.1 christos
180 1.1 christos (* ===========================================================================
181 1.1 christos * Test deflate with small buffers
182 1.1 christos *)
183 1.1 christos {$IFDEF TEST_DEFLATE}
184 1.1 christos procedure test_deflate(compr: Pointer; comprLen: LongInt);
185 1.1 christos var c_stream: z_stream; (* compression stream *)
186 1.1 christos err: Integer;
187 1.1 christos len: LongInt;
188 1.1 christos begin
189 1.1 christos len := StrLen(hello)+1;
190 1.1 christos
191 1.1 christos c_stream.zalloc := NIL;
192 1.1 christos c_stream.zfree := NIL;
193 1.1 christos c_stream.opaque := NIL;
194 1.1 christos
195 1.1 christos err := deflateInit(c_stream, Z_DEFAULT_COMPRESSION);
196 1.1 christos CHECK_ERR(err, 'deflateInit');
197 1.1 christos
198 1.1 christos c_stream.next_in := hello;
199 1.1 christos c_stream.next_out := compr;
200 1.1 christos
201 1.1 christos while (c_stream.total_in <> len) and
202 1.1 christos (c_stream.total_out < comprLen) do
203 1.1 christos begin
204 1.1 christos c_stream.avail_out := 1; { force small buffers }
205 1.1 christos c_stream.avail_in := 1;
206 1.1 christos err := deflate(c_stream, Z_NO_FLUSH);
207 1.1 christos CHECK_ERR(err, 'deflate');
208 1.1 christos end;
209 1.1 christos
210 1.1 christos (* Finish the stream, still forcing small buffers: *)
211 1.1 christos while TRUE do
212 1.1 christos begin
213 1.1 christos c_stream.avail_out := 1;
214 1.1 christos err := deflate(c_stream, Z_FINISH);
215 1.1 christos if err = Z_STREAM_END then
216 1.1 christos break;
217 1.1 christos CHECK_ERR(err, 'deflate');
218 1.1 christos end;
219 1.1 christos
220 1.1 christos err := deflateEnd(c_stream);
221 1.1 christos CHECK_ERR(err, 'deflateEnd');
222 1.1 christos end;
223 1.1 christos {$ENDIF}
224 1.1 christos
225 1.1 christos (* ===========================================================================
226 1.1 christos * Test inflate with small buffers
227 1.1 christos *)
228 1.1 christos {$IFDEF TEST_INFLATE}
229 1.1 christos procedure test_inflate(compr: Pointer; comprLen : LongInt;
230 1.1 christos uncompr: Pointer; uncomprLen : LongInt);
231 1.1 christos var err: Integer;
232 1.1 christos d_stream: z_stream; (* decompression stream *)
233 1.1 christos begin
234 1.1 christos StrCopy(PChar(uncompr), 'garbage');
235 1.1 christos
236 1.1 christos d_stream.zalloc := NIL;
237 1.1 christos d_stream.zfree := NIL;
238 1.1 christos d_stream.opaque := NIL;
239 1.1 christos
240 1.1 christos d_stream.next_in := compr;
241 1.1 christos d_stream.avail_in := 0;
242 1.1 christos d_stream.next_out := uncompr;
243 1.1 christos
244 1.1 christos err := inflateInit(d_stream);
245 1.1 christos CHECK_ERR(err, 'inflateInit');
246 1.1 christos
247 1.1 christos while (d_stream.total_out < uncomprLen) and
248 1.1 christos (d_stream.total_in < comprLen) do
249 1.1 christos begin
250 1.1 christos d_stream.avail_out := 1; (* force small buffers *)
251 1.1 christos d_stream.avail_in := 1;
252 1.1 christos err := inflate(d_stream, Z_NO_FLUSH);
253 1.1 christos if err = Z_STREAM_END then
254 1.1 christos break;
255 1.1 christos CHECK_ERR(err, 'inflate');
256 1.1 christos end;
257 1.1 christos
258 1.1 christos err := inflateEnd(d_stream);
259 1.1 christos CHECK_ERR(err, 'inflateEnd');
260 1.1 christos
261 1.1 christos if StrComp(PChar(uncompr), hello) <> 0 then
262 1.1 christos EXIT_ERR('bad inflate')
263 1.1 christos else
264 1.1 christos WriteLn('inflate(): ', PChar(uncompr));
265 1.1 christos end;
266 1.1 christos {$ENDIF}
267 1.1 christos
268 1.1 christos (* ===========================================================================
269 1.1 christos * Test deflate with large buffers and dynamic change of compression level
270 1.1 christos *)
271 1.1 christos {$IFDEF TEST_DEFLATE}
272 1.1 christos procedure test_large_deflate(compr: Pointer; comprLen: LongInt;
273 1.1 christos uncompr: Pointer; uncomprLen: LongInt);
274 1.1 christos var c_stream: z_stream; (* compression stream *)
275 1.1 christos err: Integer;
276 1.1 christos begin
277 1.1 christos c_stream.zalloc := NIL;
278 1.1 christos c_stream.zfree := NIL;
279 1.1 christos c_stream.opaque := NIL;
280 1.1 christos
281 1.1 christos err := deflateInit(c_stream, Z_BEST_SPEED);
282 1.1 christos CHECK_ERR(err, 'deflateInit');
283 1.1 christos
284 1.1 christos c_stream.next_out := compr;
285 1.1 christos c_stream.avail_out := Integer(comprLen);
286 1.1 christos
287 1.1 christos (* At this point, uncompr is still mostly zeroes, so it should compress
288 1.1 christos * very well:
289 1.1 christos *)
290 1.1 christos c_stream.next_in := uncompr;
291 1.1 christos c_stream.avail_in := Integer(uncomprLen);
292 1.1 christos err := deflate(c_stream, Z_NO_FLUSH);
293 1.1 christos CHECK_ERR(err, 'deflate');
294 1.1 christos if c_stream.avail_in <> 0 then
295 1.1 christos EXIT_ERR('deflate not greedy');
296 1.1 christos
297 1.1 christos (* Feed in already compressed data and switch to no compression: *)
298 1.1 christos deflateParams(c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
299 1.1 christos c_stream.next_in := compr;
300 1.1 christos c_stream.avail_in := Integer(comprLen div 2);
301 1.1 christos err := deflate(c_stream, Z_NO_FLUSH);
302 1.1 christos CHECK_ERR(err, 'deflate');
303 1.1 christos
304 1.1 christos (* Switch back to compressing mode: *)
305 1.1 christos deflateParams(c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
306 1.1 christos c_stream.next_in := uncompr;
307 1.1 christos c_stream.avail_in := Integer(uncomprLen);
308 1.1 christos err := deflate(c_stream, Z_NO_FLUSH);
309 1.1 christos CHECK_ERR(err, 'deflate');
310 1.1 christos
311 1.1 christos err := deflate(c_stream, Z_FINISH);
312 1.1 christos if err <> Z_STREAM_END then
313 1.1 christos EXIT_ERR('deflate should report Z_STREAM_END');
314 1.1 christos
315 1.1 christos err := deflateEnd(c_stream);
316 1.1 christos CHECK_ERR(err, 'deflateEnd');
317 1.1 christos end;
318 1.1 christos {$ENDIF}
319 1.1 christos
320 1.1 christos (* ===========================================================================
321 1.1 christos * Test inflate with large buffers
322 1.1 christos *)
323 1.1 christos {$IFDEF TEST_INFLATE}
324 1.1 christos procedure test_large_inflate(compr: Pointer; comprLen: LongInt;
325 1.1 christos uncompr: Pointer; uncomprLen: LongInt);
326 1.1 christos var err: Integer;
327 1.1 christos d_stream: z_stream; (* decompression stream *)
328 1.1 christos begin
329 1.1 christos StrCopy(PChar(uncompr), 'garbage');
330 1.1 christos
331 1.1 christos d_stream.zalloc := NIL;
332 1.1 christos d_stream.zfree := NIL;
333 1.1 christos d_stream.opaque := NIL;
334 1.1 christos
335 1.1 christos d_stream.next_in := compr;
336 1.1 christos d_stream.avail_in := Integer(comprLen);
337 1.1 christos
338 1.1 christos err := inflateInit(d_stream);
339 1.1 christos CHECK_ERR(err, 'inflateInit');
340 1.1 christos
341 1.1 christos while TRUE do
342 1.1 christos begin
343 1.1 christos d_stream.next_out := uncompr; (* discard the output *)
344 1.1 christos d_stream.avail_out := Integer(uncomprLen);
345 1.1 christos err := inflate(d_stream, Z_NO_FLUSH);
346 1.1 christos if err = Z_STREAM_END then
347 1.1 christos break;
348 1.1 christos CHECK_ERR(err, 'large inflate');
349 1.1 christos end;
350 1.1 christos
351 1.1 christos err := inflateEnd(d_stream);
352 1.1 christos CHECK_ERR(err, 'inflateEnd');
353 1.1 christos
354 1.1 christos if d_stream.total_out <> 2 * uncomprLen + comprLen div 2 then
355 1.1 christos begin
356 1.1 christos WriteLn('bad large inflate: ', d_stream.total_out);
357 1.1 christos Halt(1);
358 1.1 christos end
359 1.1 christos else
360 1.1 christos WriteLn('large_inflate(): OK');
361 1.1 christos end;
362 1.1 christos {$ENDIF}
363 1.1 christos
364 1.1 christos (* ===========================================================================
365 1.1 christos * Test deflate with full flush
366 1.1 christos *)
367 1.1 christos {$IFDEF TEST_FLUSH}
368 1.1 christos procedure test_flush(compr: Pointer; var comprLen : LongInt);
369 1.1 christos var c_stream: z_stream; (* compression stream *)
370 1.1 christos err: Integer;
371 1.1 christos len: Integer;
372 1.1 christos begin
373 1.1 christos len := StrLen(hello)+1;
374 1.1 christos
375 1.1 christos c_stream.zalloc := NIL;
376 1.1 christos c_stream.zfree := NIL;
377 1.1 christos c_stream.opaque := NIL;
378 1.1 christos
379 1.1 christos err := deflateInit(c_stream, Z_DEFAULT_COMPRESSION);
380 1.1 christos CHECK_ERR(err, 'deflateInit');
381 1.1 christos
382 1.1 christos c_stream.next_in := hello;
383 1.1 christos c_stream.next_out := compr;
384 1.1 christos c_stream.avail_in := 3;
385 1.1 christos c_stream.avail_out := Integer(comprLen);
386 1.1 christos err := deflate(c_stream, Z_FULL_FLUSH);
387 1.1 christos CHECK_ERR(err, 'deflate');
388 1.1 christos
389 1.1 christos Inc(PByteArray(compr)^[3]); (* force an error in first compressed block *)
390 1.1 christos c_stream.avail_in := len - 3;
391 1.1 christos
392 1.1 christos err := deflate(c_stream, Z_FINISH);
393 1.1 christos if err <> Z_STREAM_END then
394 1.1 christos CHECK_ERR(err, 'deflate');
395 1.1 christos
396 1.1 christos err := deflateEnd(c_stream);
397 1.1 christos CHECK_ERR(err, 'deflateEnd');
398 1.1 christos
399 1.1 christos comprLen := c_stream.total_out;
400 1.1 christos end;
401 1.1 christos {$ENDIF}
402 1.1 christos
403 1.1 christos (* ===========================================================================
404 1.1 christos * Test inflateSync()
405 1.1 christos *)
406 1.1 christos {$IFDEF TEST_SYNC}
407 1.1 christos procedure test_sync(compr: Pointer; comprLen: LongInt;
408 1.1 christos uncompr: Pointer; uncomprLen : LongInt);
409 1.1 christos var err: Integer;
410 1.1 christos d_stream: z_stream; (* decompression stream *)
411 1.1 christos begin
412 1.1 christos StrCopy(PChar(uncompr), 'garbage');
413 1.1 christos
414 1.1 christos d_stream.zalloc := NIL;
415 1.1 christos d_stream.zfree := NIL;
416 1.1 christos d_stream.opaque := NIL;
417 1.1 christos
418 1.1 christos d_stream.next_in := compr;
419 1.1 christos d_stream.avail_in := 2; (* just read the zlib header *)
420 1.1 christos
421 1.1 christos err := inflateInit(d_stream);
422 1.1 christos CHECK_ERR(err, 'inflateInit');
423 1.1 christos
424 1.1 christos d_stream.next_out := uncompr;
425 1.1 christos d_stream.avail_out := Integer(uncomprLen);
426 1.1 christos
427 1.1 christos inflate(d_stream, Z_NO_FLUSH);
428 1.1 christos CHECK_ERR(err, 'inflate');
429 1.1 christos
430 1.1 christos d_stream.avail_in := Integer(comprLen-2); (* read all compressed data *)
431 1.1 christos err := inflateSync(d_stream); (* but skip the damaged part *)
432 1.1 christos CHECK_ERR(err, 'inflateSync');
433 1.1 christos
434 1.1 christos err := inflate(d_stream, Z_FINISH);
435 1.1 christos if err <> Z_DATA_ERROR then
436 1.1 christos EXIT_ERR('inflate should report DATA_ERROR');
437 1.1 christos (* Because of incorrect adler32 *)
438 1.1 christos
439 1.1 christos err := inflateEnd(d_stream);
440 1.1 christos CHECK_ERR(err, 'inflateEnd');
441 1.1 christos
442 1.1 christos WriteLn('after inflateSync(): hel', PChar(uncompr));
443 1.1 christos end;
444 1.1 christos {$ENDIF}
445 1.1 christos
446 1.1 christos (* ===========================================================================
447 1.1 christos * Test deflate with preset dictionary
448 1.1 christos *)
449 1.1 christos {$IFDEF TEST_DICT}
450 1.1 christos procedure test_dict_deflate(compr: Pointer; comprLen: LongInt);
451 1.1 christos var c_stream: z_stream; (* compression stream *)
452 1.1 christos err: Integer;
453 1.1 christos begin
454 1.1 christos c_stream.zalloc := NIL;
455 1.1 christos c_stream.zfree := NIL;
456 1.1 christos c_stream.opaque := NIL;
457 1.1 christos
458 1.1 christos err := deflateInit(c_stream, Z_BEST_COMPRESSION);
459 1.1 christos CHECK_ERR(err, 'deflateInit');
460 1.1 christos
461 1.1 christos err := deflateSetDictionary(c_stream, dictionary, StrLen(dictionary));
462 1.1 christos CHECK_ERR(err, 'deflateSetDictionary');
463 1.1 christos
464 1.1 christos dictId := c_stream.adler;
465 1.1 christos c_stream.next_out := compr;
466 1.1 christos c_stream.avail_out := Integer(comprLen);
467 1.1 christos
468 1.1 christos c_stream.next_in := hello;
469 1.1 christos c_stream.avail_in := StrLen(hello)+1;
470 1.1 christos
471 1.1 christos err := deflate(c_stream, Z_FINISH);
472 1.1 christos if err <> Z_STREAM_END then
473 1.1 christos EXIT_ERR('deflate should report Z_STREAM_END');
474 1.1 christos
475 1.1 christos err := deflateEnd(c_stream);
476 1.1 christos CHECK_ERR(err, 'deflateEnd');
477 1.1 christos end;
478 1.1 christos {$ENDIF}
479 1.1 christos
480 1.1 christos (* ===========================================================================
481 1.1 christos * Test inflate with a preset dictionary
482 1.1 christos *)
483 1.1 christos {$IFDEF TEST_DICT}
484 1.1 christos procedure test_dict_inflate(compr: Pointer; comprLen: LongInt;
485 1.1 christos uncompr: Pointer; uncomprLen: LongInt);
486 1.1 christos var err: Integer;
487 1.1 christos d_stream: z_stream; (* decompression stream *)
488 1.1 christos begin
489 1.1 christos StrCopy(PChar(uncompr), 'garbage');
490 1.1 christos
491 1.1 christos d_stream.zalloc := NIL;
492 1.1 christos d_stream.zfree := NIL;
493 1.1 christos d_stream.opaque := NIL;
494 1.1 christos
495 1.1 christos d_stream.next_in := compr;
496 1.1 christos d_stream.avail_in := Integer(comprLen);
497 1.1 christos
498 1.1 christos err := inflateInit(d_stream);
499 1.1 christos CHECK_ERR(err, 'inflateInit');
500 1.1 christos
501 1.1 christos d_stream.next_out := uncompr;
502 1.1 christos d_stream.avail_out := Integer(uncomprLen);
503 1.1 christos
504 1.1 christos while TRUE do
505 1.1 christos begin
506 1.1 christos err := inflate(d_stream, Z_NO_FLUSH);
507 1.1 christos if err = Z_STREAM_END then
508 1.1 christos break;
509 1.1 christos if err = Z_NEED_DICT then
510 1.1 christos begin
511 1.1 christos if d_stream.adler <> dictId then
512 1.1 christos EXIT_ERR('unexpected dictionary');
513 1.1 christos err := inflateSetDictionary(d_stream, dictionary, StrLen(dictionary));
514 1.1 christos end;
515 1.1 christos CHECK_ERR(err, 'inflate with dict');
516 1.1 christos end;
517 1.1 christos
518 1.1 christos err := inflateEnd(d_stream);
519 1.1 christos CHECK_ERR(err, 'inflateEnd');
520 1.1 christos
521 1.1 christos if StrComp(PChar(uncompr), hello) <> 0 then
522 1.1 christos EXIT_ERR('bad inflate with dict')
523 1.1 christos else
524 1.1 christos WriteLn('inflate with dictionary: ', PChar(uncompr));
525 1.1 christos end;
526 1.1 christos {$ENDIF}
527 1.1 christos
528 1.1 christos var compr, uncompr: Pointer;
529 1.1 christos comprLen, uncomprLen: LongInt;
530 1.1 christos
531 1.1 christos begin
532 1.1 christos if zlibVersion^ <> ZLIB_VERSION[1] then
533 1.1 christos EXIT_ERR('Incompatible zlib version');
534 1.1 christos
535 1.1 christos WriteLn('zlib version: ', zlibVersion);
536 1.1 christos WriteLn('zlib compile flags: ', Format('0x%x', [zlibCompileFlags]));
537 1.1 christos
538 1.1 christos comprLen := 10000 * SizeOf(Integer); (* don't overflow on MSDOS *)
539 1.1 christos uncomprLen := comprLen;
540 1.1 christos GetMem(compr, comprLen);
541 1.1 christos GetMem(uncompr, uncomprLen);
542 1.1 christos if (compr = NIL) or (uncompr = NIL) then
543 1.1 christos EXIT_ERR('Out of memory');
544 1.1 christos (* compr and uncompr are cleared to avoid reading uninitialized
545 1.1 christos * data and to ensure that uncompr compresses well.
546 1.1 christos *)
547 1.1 christos FillChar(compr^, comprLen, 0);
548 1.1 christos FillChar(uncompr^, uncomprLen, 0);
549 1.1 christos
550 1.1 christos {$IFDEF TEST_COMPRESS}
551 1.1 christos WriteLn('** Testing compress');
552 1.1 christos test_compress(compr, comprLen, uncompr, uncomprLen);
553 1.1 christos {$ENDIF}
554 1.1 christos
555 1.1 christos {$IFDEF TEST_GZIO}
556 1.1 christos WriteLn('** Testing gzio');
557 1.1 christos if ParamCount >= 1 then
558 1.1 christos test_gzio(ParamStr(1), uncompr, uncomprLen)
559 1.1 christos else
560 1.1 christos test_gzio(TESTFILE, uncompr, uncomprLen);
561 1.1 christos {$ENDIF}
562 1.1 christos
563 1.1 christos {$IFDEF TEST_DEFLATE}
564 1.1 christos WriteLn('** Testing deflate with small buffers');
565 1.1 christos test_deflate(compr, comprLen);
566 1.1 christos {$ENDIF}
567 1.1 christos {$IFDEF TEST_INFLATE}
568 1.1 christos WriteLn('** Testing inflate with small buffers');
569 1.1 christos test_inflate(compr, comprLen, uncompr, uncomprLen);
570 1.1 christos {$ENDIF}
571 1.1 christos
572 1.1 christos {$IFDEF TEST_DEFLATE}
573 1.1 christos WriteLn('** Testing deflate with large buffers');
574 1.1 christos test_large_deflate(compr, comprLen, uncompr, uncomprLen);
575 1.1 christos {$ENDIF}
576 1.1 christos {$IFDEF TEST_INFLATE}
577 1.1 christos WriteLn('** Testing inflate with large buffers');
578 1.1 christos test_large_inflate(compr, comprLen, uncompr, uncomprLen);
579 1.1 christos {$ENDIF}
580 1.1 christos
581 1.1 christos {$IFDEF TEST_FLUSH}
582 1.1 christos WriteLn('** Testing deflate with full flush');
583 1.1 christos test_flush(compr, comprLen);
584 1.1 christos {$ENDIF}
585 1.1 christos {$IFDEF TEST_SYNC}
586 1.1 christos WriteLn('** Testing inflateSync');
587 1.1 christos test_sync(compr, comprLen, uncompr, uncomprLen);
588 1.1 christos {$ENDIF}
589 1.1 christos comprLen := uncomprLen;
590 1.1 christos
591 1.1 christos {$IFDEF TEST_DICT}
592 1.1 christos WriteLn('** Testing deflate and inflate with preset dictionary');
593 1.1 christos test_dict_deflate(compr, comprLen);
594 1.1 christos test_dict_inflate(compr, comprLen, uncompr, uncomprLen);
595 1.1 christos {$ENDIF}
596 1.1 christos
597 1.1 christos FreeMem(compr, comprLen);
598 1.1 christos FreeMem(uncompr, uncomprLen);
599 1.1 christos end.
600