annotate src/zlib-1.2.8/contrib/delphi/ZLib.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 {*******************************************************}
Chris@43 2 { }
Chris@43 3 { Borland Delphi Supplemental Components }
Chris@43 4 { ZLIB Data Compression Interface Unit }
Chris@43 5 { }
Chris@43 6 { Copyright (c) 1997,99 Borland Corporation }
Chris@43 7 { }
Chris@43 8 {*******************************************************}
Chris@43 9
Chris@43 10 { Updated for zlib 1.2.x by Cosmin Truta <cosmint@cs.ubbcluj.ro> }
Chris@43 11
Chris@43 12 unit ZLib;
Chris@43 13
Chris@43 14 interface
Chris@43 15
Chris@43 16 uses SysUtils, Classes;
Chris@43 17
Chris@43 18 type
Chris@43 19 TAlloc = function (AppData: Pointer; Items, Size: Integer): Pointer; cdecl;
Chris@43 20 TFree = procedure (AppData, Block: Pointer); cdecl;
Chris@43 21
Chris@43 22 // Internal structure. Ignore.
Chris@43 23 TZStreamRec = packed record
Chris@43 24 next_in: PChar; // next input byte
Chris@43 25 avail_in: Integer; // number of bytes available at next_in
Chris@43 26 total_in: Longint; // total nb of input bytes read so far
Chris@43 27
Chris@43 28 next_out: PChar; // next output byte should be put here
Chris@43 29 avail_out: Integer; // remaining free space at next_out
Chris@43 30 total_out: Longint; // total nb of bytes output so far
Chris@43 31
Chris@43 32 msg: PChar; // last error message, NULL if no error
Chris@43 33 internal: Pointer; // not visible by applications
Chris@43 34
Chris@43 35 zalloc: TAlloc; // used to allocate the internal state
Chris@43 36 zfree: TFree; // used to free the internal state
Chris@43 37 AppData: Pointer; // private data object passed to zalloc and zfree
Chris@43 38
Chris@43 39 data_type: Integer; // best guess about the data type: ascii or binary
Chris@43 40 adler: Longint; // adler32 value of the uncompressed data
Chris@43 41 reserved: Longint; // reserved for future use
Chris@43 42 end;
Chris@43 43
Chris@43 44 // Abstract ancestor class
Chris@43 45 TCustomZlibStream = class(TStream)
Chris@43 46 private
Chris@43 47 FStrm: TStream;
Chris@43 48 FStrmPos: Integer;
Chris@43 49 FOnProgress: TNotifyEvent;
Chris@43 50 FZRec: TZStreamRec;
Chris@43 51 FBuffer: array [Word] of Char;
Chris@43 52 protected
Chris@43 53 procedure Progress(Sender: TObject); dynamic;
Chris@43 54 property OnProgress: TNotifyEvent read FOnProgress write FOnProgress;
Chris@43 55 constructor Create(Strm: TStream);
Chris@43 56 end;
Chris@43 57
Chris@43 58 { TCompressionStream compresses data on the fly as data is written to it, and
Chris@43 59 stores the compressed data to another stream.
Chris@43 60
Chris@43 61 TCompressionStream is write-only and strictly sequential. Reading from the
Chris@43 62 stream will raise an exception. Using Seek to move the stream pointer
Chris@43 63 will raise an exception.
Chris@43 64
Chris@43 65 Output data is cached internally, written to the output stream only when
Chris@43 66 the internal output buffer is full. All pending output data is flushed
Chris@43 67 when the stream is destroyed.
Chris@43 68
Chris@43 69 The Position property returns the number of uncompressed bytes of
Chris@43 70 data that have been written to the stream so far.
Chris@43 71
Chris@43 72 CompressionRate returns the on-the-fly percentage by which the original
Chris@43 73 data has been compressed: (1 - (CompressedBytes / UncompressedBytes)) * 100
Chris@43 74 If raw data size = 100 and compressed data size = 25, the CompressionRate
Chris@43 75 is 75%
Chris@43 76
Chris@43 77 The OnProgress event is called each time the output buffer is filled and
Chris@43 78 written to the output stream. This is useful for updating a progress
Chris@43 79 indicator when you are writing a large chunk of data to the compression
Chris@43 80 stream in a single call.}
Chris@43 81
Chris@43 82
Chris@43 83 TCompressionLevel = (clNone, clFastest, clDefault, clMax);
Chris@43 84
Chris@43 85 TCompressionStream = class(TCustomZlibStream)
Chris@43 86 private
Chris@43 87 function GetCompressionRate: Single;
Chris@43 88 public
Chris@43 89 constructor Create(CompressionLevel: TCompressionLevel; Dest: TStream);
Chris@43 90 destructor Destroy; override;
Chris@43 91 function Read(var Buffer; Count: Longint): Longint; override;
Chris@43 92 function Write(const Buffer; Count: Longint): Longint; override;
Chris@43 93 function Seek(Offset: Longint; Origin: Word): Longint; override;
Chris@43 94 property CompressionRate: Single read GetCompressionRate;
Chris@43 95 property OnProgress;
Chris@43 96 end;
Chris@43 97
Chris@43 98 { TDecompressionStream decompresses data on the fly as data is read from it.
Chris@43 99
Chris@43 100 Compressed data comes from a separate source stream. TDecompressionStream
Chris@43 101 is read-only and unidirectional; you can seek forward in the stream, but not
Chris@43 102 backwards. The special case of setting the stream position to zero is
Chris@43 103 allowed. Seeking forward decompresses data until the requested position in
Chris@43 104 the uncompressed data has been reached. Seeking backwards, seeking relative
Chris@43 105 to the end of the stream, requesting the size of the stream, and writing to
Chris@43 106 the stream will raise an exception.
Chris@43 107
Chris@43 108 The Position property returns the number of bytes of uncompressed data that
Chris@43 109 have been read from the stream so far.
Chris@43 110
Chris@43 111 The OnProgress event is called each time the internal input buffer of
Chris@43 112 compressed data is exhausted and the next block is read from the input stream.
Chris@43 113 This is useful for updating a progress indicator when you are reading a
Chris@43 114 large chunk of data from the decompression stream in a single call.}
Chris@43 115
Chris@43 116 TDecompressionStream = class(TCustomZlibStream)
Chris@43 117 public
Chris@43 118 constructor Create(Source: TStream);
Chris@43 119 destructor Destroy; override;
Chris@43 120 function Read(var Buffer; Count: Longint): Longint; override;
Chris@43 121 function Write(const Buffer; Count: Longint): Longint; override;
Chris@43 122 function Seek(Offset: Longint; Origin: Word): Longint; override;
Chris@43 123 property OnProgress;
Chris@43 124 end;
Chris@43 125
Chris@43 126
Chris@43 127
Chris@43 128 { CompressBuf compresses data, buffer to buffer, in one call.
Chris@43 129 In: InBuf = ptr to compressed data
Chris@43 130 InBytes = number of bytes in InBuf
Chris@43 131 Out: OutBuf = ptr to newly allocated buffer containing decompressed data
Chris@43 132 OutBytes = number of bytes in OutBuf }
Chris@43 133 procedure CompressBuf(const InBuf: Pointer; InBytes: Integer;
Chris@43 134 out OutBuf: Pointer; out OutBytes: Integer);
Chris@43 135
Chris@43 136
Chris@43 137 { DecompressBuf decompresses data, buffer to buffer, in one call.
Chris@43 138 In: InBuf = ptr to compressed data
Chris@43 139 InBytes = number of bytes in InBuf
Chris@43 140 OutEstimate = zero, or est. size of the decompressed data
Chris@43 141 Out: OutBuf = ptr to newly allocated buffer containing decompressed data
Chris@43 142 OutBytes = number of bytes in OutBuf }
Chris@43 143 procedure DecompressBuf(const InBuf: Pointer; InBytes: Integer;
Chris@43 144 OutEstimate: Integer; out OutBuf: Pointer; out OutBytes: Integer);
Chris@43 145
Chris@43 146 { DecompressToUserBuf decompresses data, buffer to buffer, in one call.
Chris@43 147 In: InBuf = ptr to compressed data
Chris@43 148 InBytes = number of bytes in InBuf
Chris@43 149 Out: OutBuf = ptr to user-allocated buffer to contain decompressed data
Chris@43 150 BufSize = number of bytes in OutBuf }
Chris@43 151 procedure DecompressToUserBuf(const InBuf: Pointer; InBytes: Integer;
Chris@43 152 const OutBuf: Pointer; BufSize: Integer);
Chris@43 153
Chris@43 154 const
Chris@43 155 zlib_version = '1.2.8';
Chris@43 156
Chris@43 157 type
Chris@43 158 EZlibError = class(Exception);
Chris@43 159 ECompressionError = class(EZlibError);
Chris@43 160 EDecompressionError = class(EZlibError);
Chris@43 161
Chris@43 162 implementation
Chris@43 163
Chris@43 164 uses ZLibConst;
Chris@43 165
Chris@43 166 const
Chris@43 167 Z_NO_FLUSH = 0;
Chris@43 168 Z_PARTIAL_FLUSH = 1;
Chris@43 169 Z_SYNC_FLUSH = 2;
Chris@43 170 Z_FULL_FLUSH = 3;
Chris@43 171 Z_FINISH = 4;
Chris@43 172
Chris@43 173 Z_OK = 0;
Chris@43 174 Z_STREAM_END = 1;
Chris@43 175 Z_NEED_DICT = 2;
Chris@43 176 Z_ERRNO = (-1);
Chris@43 177 Z_STREAM_ERROR = (-2);
Chris@43 178 Z_DATA_ERROR = (-3);
Chris@43 179 Z_MEM_ERROR = (-4);
Chris@43 180 Z_BUF_ERROR = (-5);
Chris@43 181 Z_VERSION_ERROR = (-6);
Chris@43 182
Chris@43 183 Z_NO_COMPRESSION = 0;
Chris@43 184 Z_BEST_SPEED = 1;
Chris@43 185 Z_BEST_COMPRESSION = 9;
Chris@43 186 Z_DEFAULT_COMPRESSION = (-1);
Chris@43 187
Chris@43 188 Z_FILTERED = 1;
Chris@43 189 Z_HUFFMAN_ONLY = 2;
Chris@43 190 Z_RLE = 3;
Chris@43 191 Z_DEFAULT_STRATEGY = 0;
Chris@43 192
Chris@43 193 Z_BINARY = 0;
Chris@43 194 Z_ASCII = 1;
Chris@43 195 Z_UNKNOWN = 2;
Chris@43 196
Chris@43 197 Z_DEFLATED = 8;
Chris@43 198
Chris@43 199
Chris@43 200 {$L adler32.obj}
Chris@43 201 {$L compress.obj}
Chris@43 202 {$L crc32.obj}
Chris@43 203 {$L deflate.obj}
Chris@43 204 {$L infback.obj}
Chris@43 205 {$L inffast.obj}
Chris@43 206 {$L inflate.obj}
Chris@43 207 {$L inftrees.obj}
Chris@43 208 {$L trees.obj}
Chris@43 209 {$L uncompr.obj}
Chris@43 210 {$L zutil.obj}
Chris@43 211
Chris@43 212 procedure adler32; external;
Chris@43 213 procedure compressBound; external;
Chris@43 214 procedure crc32; external;
Chris@43 215 procedure deflateInit2_; external;
Chris@43 216 procedure deflateParams; external;
Chris@43 217
Chris@43 218 function _malloc(Size: Integer): Pointer; cdecl;
Chris@43 219 begin
Chris@43 220 Result := AllocMem(Size);
Chris@43 221 end;
Chris@43 222
Chris@43 223 procedure _free(Block: Pointer); cdecl;
Chris@43 224 begin
Chris@43 225 FreeMem(Block);
Chris@43 226 end;
Chris@43 227
Chris@43 228 procedure _memset(P: Pointer; B: Byte; count: Integer); cdecl;
Chris@43 229 begin
Chris@43 230 FillChar(P^, count, B);
Chris@43 231 end;
Chris@43 232
Chris@43 233 procedure _memcpy(dest, source: Pointer; count: Integer); cdecl;
Chris@43 234 begin
Chris@43 235 Move(source^, dest^, count);
Chris@43 236 end;
Chris@43 237
Chris@43 238
Chris@43 239
Chris@43 240 // deflate compresses data
Chris@43 241 function deflateInit_(var strm: TZStreamRec; level: Integer; version: PChar;
Chris@43 242 recsize: Integer): Integer; external;
Chris@43 243 function deflate(var strm: TZStreamRec; flush: Integer): Integer; external;
Chris@43 244 function deflateEnd(var strm: TZStreamRec): Integer; external;
Chris@43 245
Chris@43 246 // inflate decompresses data
Chris@43 247 function inflateInit_(var strm: TZStreamRec; version: PChar;
Chris@43 248 recsize: Integer): Integer; external;
Chris@43 249 function inflate(var strm: TZStreamRec; flush: Integer): Integer; external;
Chris@43 250 function inflateEnd(var strm: TZStreamRec): Integer; external;
Chris@43 251 function inflateReset(var strm: TZStreamRec): Integer; external;
Chris@43 252
Chris@43 253
Chris@43 254 function zlibAllocMem(AppData: Pointer; Items, Size: Integer): Pointer; cdecl;
Chris@43 255 begin
Chris@43 256 // GetMem(Result, Items*Size);
Chris@43 257 Result := AllocMem(Items * Size);
Chris@43 258 end;
Chris@43 259
Chris@43 260 procedure zlibFreeMem(AppData, Block: Pointer); cdecl;
Chris@43 261 begin
Chris@43 262 FreeMem(Block);
Chris@43 263 end;
Chris@43 264
Chris@43 265 {function zlibCheck(code: Integer): Integer;
Chris@43 266 begin
Chris@43 267 Result := code;
Chris@43 268 if code < 0 then
Chris@43 269 raise EZlibError.Create('error'); //!!
Chris@43 270 end;}
Chris@43 271
Chris@43 272 function CCheck(code: Integer): Integer;
Chris@43 273 begin
Chris@43 274 Result := code;
Chris@43 275 if code < 0 then
Chris@43 276 raise ECompressionError.Create('error'); //!!
Chris@43 277 end;
Chris@43 278
Chris@43 279 function DCheck(code: Integer): Integer;
Chris@43 280 begin
Chris@43 281 Result := code;
Chris@43 282 if code < 0 then
Chris@43 283 raise EDecompressionError.Create('error'); //!!
Chris@43 284 end;
Chris@43 285
Chris@43 286 procedure CompressBuf(const InBuf: Pointer; InBytes: Integer;
Chris@43 287 out OutBuf: Pointer; out OutBytes: Integer);
Chris@43 288 var
Chris@43 289 strm: TZStreamRec;
Chris@43 290 P: Pointer;
Chris@43 291 begin
Chris@43 292 FillChar(strm, sizeof(strm), 0);
Chris@43 293 strm.zalloc := zlibAllocMem;
Chris@43 294 strm.zfree := zlibFreeMem;
Chris@43 295 OutBytes := ((InBytes + (InBytes div 10) + 12) + 255) and not 255;
Chris@43 296 GetMem(OutBuf, OutBytes);
Chris@43 297 try
Chris@43 298 strm.next_in := InBuf;
Chris@43 299 strm.avail_in := InBytes;
Chris@43 300 strm.next_out := OutBuf;
Chris@43 301 strm.avail_out := OutBytes;
Chris@43 302 CCheck(deflateInit_(strm, Z_BEST_COMPRESSION, zlib_version, sizeof(strm)));
Chris@43 303 try
Chris@43 304 while CCheck(deflate(strm, Z_FINISH)) <> Z_STREAM_END do
Chris@43 305 begin
Chris@43 306 P := OutBuf;
Chris@43 307 Inc(OutBytes, 256);
Chris@43 308 ReallocMem(OutBuf, OutBytes);
Chris@43 309 strm.next_out := PChar(Integer(OutBuf) + (Integer(strm.next_out) - Integer(P)));
Chris@43 310 strm.avail_out := 256;
Chris@43 311 end;
Chris@43 312 finally
Chris@43 313 CCheck(deflateEnd(strm));
Chris@43 314 end;
Chris@43 315 ReallocMem(OutBuf, strm.total_out);
Chris@43 316 OutBytes := strm.total_out;
Chris@43 317 except
Chris@43 318 FreeMem(OutBuf);
Chris@43 319 raise
Chris@43 320 end;
Chris@43 321 end;
Chris@43 322
Chris@43 323
Chris@43 324 procedure DecompressBuf(const InBuf: Pointer; InBytes: Integer;
Chris@43 325 OutEstimate: Integer; out OutBuf: Pointer; out OutBytes: Integer);
Chris@43 326 var
Chris@43 327 strm: TZStreamRec;
Chris@43 328 P: Pointer;
Chris@43 329 BufInc: Integer;
Chris@43 330 begin
Chris@43 331 FillChar(strm, sizeof(strm), 0);
Chris@43 332 strm.zalloc := zlibAllocMem;
Chris@43 333 strm.zfree := zlibFreeMem;
Chris@43 334 BufInc := (InBytes + 255) and not 255;
Chris@43 335 if OutEstimate = 0 then
Chris@43 336 OutBytes := BufInc
Chris@43 337 else
Chris@43 338 OutBytes := OutEstimate;
Chris@43 339 GetMem(OutBuf, OutBytes);
Chris@43 340 try
Chris@43 341 strm.next_in := InBuf;
Chris@43 342 strm.avail_in := InBytes;
Chris@43 343 strm.next_out := OutBuf;
Chris@43 344 strm.avail_out := OutBytes;
Chris@43 345 DCheck(inflateInit_(strm, zlib_version, sizeof(strm)));
Chris@43 346 try
Chris@43 347 while DCheck(inflate(strm, Z_NO_FLUSH)) <> Z_STREAM_END do
Chris@43 348 begin
Chris@43 349 P := OutBuf;
Chris@43 350 Inc(OutBytes, BufInc);
Chris@43 351 ReallocMem(OutBuf, OutBytes);
Chris@43 352 strm.next_out := PChar(Integer(OutBuf) + (Integer(strm.next_out) - Integer(P)));
Chris@43 353 strm.avail_out := BufInc;
Chris@43 354 end;
Chris@43 355 finally
Chris@43 356 DCheck(inflateEnd(strm));
Chris@43 357 end;
Chris@43 358 ReallocMem(OutBuf, strm.total_out);
Chris@43 359 OutBytes := strm.total_out;
Chris@43 360 except
Chris@43 361 FreeMem(OutBuf);
Chris@43 362 raise
Chris@43 363 end;
Chris@43 364 end;
Chris@43 365
Chris@43 366 procedure DecompressToUserBuf(const InBuf: Pointer; InBytes: Integer;
Chris@43 367 const OutBuf: Pointer; BufSize: Integer);
Chris@43 368 var
Chris@43 369 strm: TZStreamRec;
Chris@43 370 begin
Chris@43 371 FillChar(strm, sizeof(strm), 0);
Chris@43 372 strm.zalloc := zlibAllocMem;
Chris@43 373 strm.zfree := zlibFreeMem;
Chris@43 374 strm.next_in := InBuf;
Chris@43 375 strm.avail_in := InBytes;
Chris@43 376 strm.next_out := OutBuf;
Chris@43 377 strm.avail_out := BufSize;
Chris@43 378 DCheck(inflateInit_(strm, zlib_version, sizeof(strm)));
Chris@43 379 try
Chris@43 380 if DCheck(inflate(strm, Z_FINISH)) <> Z_STREAM_END then
Chris@43 381 raise EZlibError.CreateRes(@sTargetBufferTooSmall);
Chris@43 382 finally
Chris@43 383 DCheck(inflateEnd(strm));
Chris@43 384 end;
Chris@43 385 end;
Chris@43 386
Chris@43 387 // TCustomZlibStream
Chris@43 388
Chris@43 389 constructor TCustomZLibStream.Create(Strm: TStream);
Chris@43 390 begin
Chris@43 391 inherited Create;
Chris@43 392 FStrm := Strm;
Chris@43 393 FStrmPos := Strm.Position;
Chris@43 394 FZRec.zalloc := zlibAllocMem;
Chris@43 395 FZRec.zfree := zlibFreeMem;
Chris@43 396 end;
Chris@43 397
Chris@43 398 procedure TCustomZLibStream.Progress(Sender: TObject);
Chris@43 399 begin
Chris@43 400 if Assigned(FOnProgress) then FOnProgress(Sender);
Chris@43 401 end;
Chris@43 402
Chris@43 403
Chris@43 404 // TCompressionStream
Chris@43 405
Chris@43 406 constructor TCompressionStream.Create(CompressionLevel: TCompressionLevel;
Chris@43 407 Dest: TStream);
Chris@43 408 const
Chris@43 409 Levels: array [TCompressionLevel] of ShortInt =
Chris@43 410 (Z_NO_COMPRESSION, Z_BEST_SPEED, Z_DEFAULT_COMPRESSION, Z_BEST_COMPRESSION);
Chris@43 411 begin
Chris@43 412 inherited Create(Dest);
Chris@43 413 FZRec.next_out := FBuffer;
Chris@43 414 FZRec.avail_out := sizeof(FBuffer);
Chris@43 415 CCheck(deflateInit_(FZRec, Levels[CompressionLevel], zlib_version, sizeof(FZRec)));
Chris@43 416 end;
Chris@43 417
Chris@43 418 destructor TCompressionStream.Destroy;
Chris@43 419 begin
Chris@43 420 FZRec.next_in := nil;
Chris@43 421 FZRec.avail_in := 0;
Chris@43 422 try
Chris@43 423 if FStrm.Position <> FStrmPos then FStrm.Position := FStrmPos;
Chris@43 424 while (CCheck(deflate(FZRec, Z_FINISH)) <> Z_STREAM_END)
Chris@43 425 and (FZRec.avail_out = 0) do
Chris@43 426 begin
Chris@43 427 FStrm.WriteBuffer(FBuffer, sizeof(FBuffer));
Chris@43 428 FZRec.next_out := FBuffer;
Chris@43 429 FZRec.avail_out := sizeof(FBuffer);
Chris@43 430 end;
Chris@43 431 if FZRec.avail_out < sizeof(FBuffer) then
Chris@43 432 FStrm.WriteBuffer(FBuffer, sizeof(FBuffer) - FZRec.avail_out);
Chris@43 433 finally
Chris@43 434 deflateEnd(FZRec);
Chris@43 435 end;
Chris@43 436 inherited Destroy;
Chris@43 437 end;
Chris@43 438
Chris@43 439 function TCompressionStream.Read(var Buffer; Count: Longint): Longint;
Chris@43 440 begin
Chris@43 441 raise ECompressionError.CreateRes(@sInvalidStreamOp);
Chris@43 442 end;
Chris@43 443
Chris@43 444 function TCompressionStream.Write(const Buffer; Count: Longint): Longint;
Chris@43 445 begin
Chris@43 446 FZRec.next_in := @Buffer;
Chris@43 447 FZRec.avail_in := Count;
Chris@43 448 if FStrm.Position <> FStrmPos then FStrm.Position := FStrmPos;
Chris@43 449 while (FZRec.avail_in > 0) do
Chris@43 450 begin
Chris@43 451 CCheck(deflate(FZRec, 0));
Chris@43 452 if FZRec.avail_out = 0 then
Chris@43 453 begin
Chris@43 454 FStrm.WriteBuffer(FBuffer, sizeof(FBuffer));
Chris@43 455 FZRec.next_out := FBuffer;
Chris@43 456 FZRec.avail_out := sizeof(FBuffer);
Chris@43 457 FStrmPos := FStrm.Position;
Chris@43 458 Progress(Self);
Chris@43 459 end;
Chris@43 460 end;
Chris@43 461 Result := Count;
Chris@43 462 end;
Chris@43 463
Chris@43 464 function TCompressionStream.Seek(Offset: Longint; Origin: Word): Longint;
Chris@43 465 begin
Chris@43 466 if (Offset = 0) and (Origin = soFromCurrent) then
Chris@43 467 Result := FZRec.total_in
Chris@43 468 else
Chris@43 469 raise ECompressionError.CreateRes(@sInvalidStreamOp);
Chris@43 470 end;
Chris@43 471
Chris@43 472 function TCompressionStream.GetCompressionRate: Single;
Chris@43 473 begin
Chris@43 474 if FZRec.total_in = 0 then
Chris@43 475 Result := 0
Chris@43 476 else
Chris@43 477 Result := (1.0 - (FZRec.total_out / FZRec.total_in)) * 100.0;
Chris@43 478 end;
Chris@43 479
Chris@43 480
Chris@43 481 // TDecompressionStream
Chris@43 482
Chris@43 483 constructor TDecompressionStream.Create(Source: TStream);
Chris@43 484 begin
Chris@43 485 inherited Create(Source);
Chris@43 486 FZRec.next_in := FBuffer;
Chris@43 487 FZRec.avail_in := 0;
Chris@43 488 DCheck(inflateInit_(FZRec, zlib_version, sizeof(FZRec)));
Chris@43 489 end;
Chris@43 490
Chris@43 491 destructor TDecompressionStream.Destroy;
Chris@43 492 begin
Chris@43 493 FStrm.Seek(-FZRec.avail_in, 1);
Chris@43 494 inflateEnd(FZRec);
Chris@43 495 inherited Destroy;
Chris@43 496 end;
Chris@43 497
Chris@43 498 function TDecompressionStream.Read(var Buffer; Count: Longint): Longint;
Chris@43 499 begin
Chris@43 500 FZRec.next_out := @Buffer;
Chris@43 501 FZRec.avail_out := Count;
Chris@43 502 if FStrm.Position <> FStrmPos then FStrm.Position := FStrmPos;
Chris@43 503 while (FZRec.avail_out > 0) do
Chris@43 504 begin
Chris@43 505 if FZRec.avail_in = 0 then
Chris@43 506 begin
Chris@43 507 FZRec.avail_in := FStrm.Read(FBuffer, sizeof(FBuffer));
Chris@43 508 if FZRec.avail_in = 0 then
Chris@43 509 begin
Chris@43 510 Result := Count - FZRec.avail_out;
Chris@43 511 Exit;
Chris@43 512 end;
Chris@43 513 FZRec.next_in := FBuffer;
Chris@43 514 FStrmPos := FStrm.Position;
Chris@43 515 Progress(Self);
Chris@43 516 end;
Chris@43 517 CCheck(inflate(FZRec, 0));
Chris@43 518 end;
Chris@43 519 Result := Count;
Chris@43 520 end;
Chris@43 521
Chris@43 522 function TDecompressionStream.Write(const Buffer; Count: Longint): Longint;
Chris@43 523 begin
Chris@43 524 raise EDecompressionError.CreateRes(@sInvalidStreamOp);
Chris@43 525 end;
Chris@43 526
Chris@43 527 function TDecompressionStream.Seek(Offset: Longint; Origin: Word): Longint;
Chris@43 528 var
Chris@43 529 I: Integer;
Chris@43 530 Buf: array [0..4095] of Char;
Chris@43 531 begin
Chris@43 532 if (Offset = 0) and (Origin = soFromBeginning) then
Chris@43 533 begin
Chris@43 534 DCheck(inflateReset(FZRec));
Chris@43 535 FZRec.next_in := FBuffer;
Chris@43 536 FZRec.avail_in := 0;
Chris@43 537 FStrm.Position := 0;
Chris@43 538 FStrmPos := 0;
Chris@43 539 end
Chris@43 540 else if ( (Offset >= 0) and (Origin = soFromCurrent)) or
Chris@43 541 ( ((Offset - FZRec.total_out) > 0) and (Origin = soFromBeginning)) then
Chris@43 542 begin
Chris@43 543 if Origin = soFromBeginning then Dec(Offset, FZRec.total_out);
Chris@43 544 if Offset > 0 then
Chris@43 545 begin
Chris@43 546 for I := 1 to Offset div sizeof(Buf) do
Chris@43 547 ReadBuffer(Buf, sizeof(Buf));
Chris@43 548 ReadBuffer(Buf, Offset mod sizeof(Buf));
Chris@43 549 end;
Chris@43 550 end
Chris@43 551 else
Chris@43 552 raise EDecompressionError.CreateRes(@sInvalidStreamOp);
Chris@43 553 Result := FZRec.total_out;
Chris@43 554 end;
Chris@43 555
Chris@43 556
Chris@43 557 end.