annotate src/zlib-1.2.8/contrib/pascal/example.pas @ 56:af97cad61ff0

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