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