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