Chris@43: {*******************************************************} Chris@43: { } Chris@43: { Borland Delphi Supplemental Components } Chris@43: { ZLIB Data Compression Interface Unit } Chris@43: { } Chris@43: { Copyright (c) 1997,99 Borland Corporation } Chris@43: { } Chris@43: {*******************************************************} Chris@43: Chris@43: { Updated for zlib 1.2.x by Cosmin Truta } Chris@43: Chris@43: unit ZLib; Chris@43: Chris@43: interface Chris@43: Chris@43: uses SysUtils, Classes; Chris@43: Chris@43: type Chris@43: TAlloc = function (AppData: Pointer; Items, Size: Integer): Pointer; cdecl; Chris@43: TFree = procedure (AppData, Block: Pointer); cdecl; Chris@43: Chris@43: // Internal structure. Ignore. Chris@43: TZStreamRec = packed record Chris@43: next_in: PChar; // next input byte Chris@43: avail_in: Integer; // number of bytes available at next_in Chris@43: total_in: Longint; // total nb of input bytes read so far Chris@43: Chris@43: next_out: PChar; // next output byte should be put here Chris@43: avail_out: Integer; // remaining free space at next_out Chris@43: total_out: Longint; // total nb of bytes output so far Chris@43: Chris@43: msg: PChar; // last error message, NULL if no error Chris@43: internal: Pointer; // not visible by applications Chris@43: Chris@43: zalloc: TAlloc; // used to allocate the internal state Chris@43: zfree: TFree; // used to free the internal state Chris@43: AppData: Pointer; // private data object passed to zalloc and zfree Chris@43: Chris@43: data_type: Integer; // best guess about the data type: ascii or binary Chris@43: adler: Longint; // adler32 value of the uncompressed data Chris@43: reserved: Longint; // reserved for future use Chris@43: end; Chris@43: Chris@43: // Abstract ancestor class Chris@43: TCustomZlibStream = class(TStream) Chris@43: private Chris@43: FStrm: TStream; Chris@43: FStrmPos: Integer; Chris@43: FOnProgress: TNotifyEvent; Chris@43: FZRec: TZStreamRec; Chris@43: FBuffer: array [Word] of Char; Chris@43: protected Chris@43: procedure Progress(Sender: TObject); dynamic; Chris@43: property OnProgress: TNotifyEvent read FOnProgress write FOnProgress; Chris@43: constructor Create(Strm: TStream); Chris@43: end; Chris@43: Chris@43: { TCompressionStream compresses data on the fly as data is written to it, and Chris@43: stores the compressed data to another stream. Chris@43: Chris@43: TCompressionStream is write-only and strictly sequential. Reading from the Chris@43: stream will raise an exception. Using Seek to move the stream pointer Chris@43: will raise an exception. Chris@43: Chris@43: Output data is cached internally, written to the output stream only when Chris@43: the internal output buffer is full. All pending output data is flushed Chris@43: when the stream is destroyed. Chris@43: Chris@43: The Position property returns the number of uncompressed bytes of Chris@43: data that have been written to the stream so far. Chris@43: Chris@43: CompressionRate returns the on-the-fly percentage by which the original Chris@43: data has been compressed: (1 - (CompressedBytes / UncompressedBytes)) * 100 Chris@43: If raw data size = 100 and compressed data size = 25, the CompressionRate Chris@43: is 75% Chris@43: Chris@43: The OnProgress event is called each time the output buffer is filled and Chris@43: written to the output stream. This is useful for updating a progress Chris@43: indicator when you are writing a large chunk of data to the compression Chris@43: stream in a single call.} Chris@43: Chris@43: Chris@43: TCompressionLevel = (clNone, clFastest, clDefault, clMax); Chris@43: Chris@43: TCompressionStream = class(TCustomZlibStream) Chris@43: private Chris@43: function GetCompressionRate: Single; Chris@43: public Chris@43: constructor Create(CompressionLevel: TCompressionLevel; Dest: TStream); Chris@43: destructor Destroy; override; Chris@43: function Read(var Buffer; Count: Longint): Longint; override; Chris@43: function Write(const Buffer; Count: Longint): Longint; override; Chris@43: function Seek(Offset: Longint; Origin: Word): Longint; override; Chris@43: property CompressionRate: Single read GetCompressionRate; Chris@43: property OnProgress; Chris@43: end; Chris@43: Chris@43: { TDecompressionStream decompresses data on the fly as data is read from it. Chris@43: Chris@43: Compressed data comes from a separate source stream. TDecompressionStream Chris@43: is read-only and unidirectional; you can seek forward in the stream, but not Chris@43: backwards. The special case of setting the stream position to zero is Chris@43: allowed. Seeking forward decompresses data until the requested position in Chris@43: the uncompressed data has been reached. Seeking backwards, seeking relative Chris@43: to the end of the stream, requesting the size of the stream, and writing to Chris@43: the stream will raise an exception. Chris@43: Chris@43: The Position property returns the number of bytes of uncompressed data that Chris@43: have been read from the stream so far. Chris@43: Chris@43: The OnProgress event is called each time the internal input buffer of Chris@43: compressed data is exhausted and the next block is read from the input stream. Chris@43: This is useful for updating a progress indicator when you are reading a Chris@43: large chunk of data from the decompression stream in a single call.} Chris@43: Chris@43: TDecompressionStream = class(TCustomZlibStream) Chris@43: public Chris@43: constructor Create(Source: TStream); Chris@43: destructor Destroy; override; Chris@43: function Read(var Buffer; Count: Longint): Longint; override; Chris@43: function Write(const Buffer; Count: Longint): Longint; override; Chris@43: function Seek(Offset: Longint; Origin: Word): Longint; override; Chris@43: property OnProgress; Chris@43: end; Chris@43: Chris@43: Chris@43: Chris@43: { CompressBuf compresses data, buffer to buffer, in one call. Chris@43: In: InBuf = ptr to compressed data Chris@43: InBytes = number of bytes in InBuf Chris@43: Out: OutBuf = ptr to newly allocated buffer containing decompressed data Chris@43: OutBytes = number of bytes in OutBuf } Chris@43: procedure CompressBuf(const InBuf: Pointer; InBytes: Integer; Chris@43: out OutBuf: Pointer; out OutBytes: Integer); Chris@43: Chris@43: Chris@43: { DecompressBuf decompresses data, buffer to buffer, in one call. Chris@43: In: InBuf = ptr to compressed data Chris@43: InBytes = number of bytes in InBuf Chris@43: OutEstimate = zero, or est. size of the decompressed data Chris@43: Out: OutBuf = ptr to newly allocated buffer containing decompressed data Chris@43: OutBytes = number of bytes in OutBuf } Chris@43: procedure DecompressBuf(const InBuf: Pointer; InBytes: Integer; Chris@43: OutEstimate: Integer; out OutBuf: Pointer; out OutBytes: Integer); Chris@43: Chris@43: { DecompressToUserBuf decompresses data, buffer to buffer, in one call. Chris@43: In: InBuf = ptr to compressed data Chris@43: InBytes = number of bytes in InBuf Chris@43: Out: OutBuf = ptr to user-allocated buffer to contain decompressed data Chris@43: BufSize = number of bytes in OutBuf } Chris@43: procedure DecompressToUserBuf(const InBuf: Pointer; InBytes: Integer; Chris@43: const OutBuf: Pointer; BufSize: Integer); Chris@43: Chris@43: const Chris@43: zlib_version = '1.2.8'; Chris@43: Chris@43: type Chris@43: EZlibError = class(Exception); Chris@43: ECompressionError = class(EZlibError); Chris@43: EDecompressionError = class(EZlibError); Chris@43: Chris@43: implementation Chris@43: Chris@43: uses ZLibConst; Chris@43: Chris@43: const Chris@43: Z_NO_FLUSH = 0; Chris@43: Z_PARTIAL_FLUSH = 1; Chris@43: Z_SYNC_FLUSH = 2; Chris@43: Z_FULL_FLUSH = 3; Chris@43: Z_FINISH = 4; Chris@43: Chris@43: Z_OK = 0; Chris@43: Z_STREAM_END = 1; Chris@43: Z_NEED_DICT = 2; Chris@43: Z_ERRNO = (-1); Chris@43: Z_STREAM_ERROR = (-2); Chris@43: Z_DATA_ERROR = (-3); Chris@43: Z_MEM_ERROR = (-4); Chris@43: Z_BUF_ERROR = (-5); Chris@43: Z_VERSION_ERROR = (-6); Chris@43: Chris@43: Z_NO_COMPRESSION = 0; Chris@43: Z_BEST_SPEED = 1; Chris@43: Z_BEST_COMPRESSION = 9; Chris@43: Z_DEFAULT_COMPRESSION = (-1); Chris@43: Chris@43: Z_FILTERED = 1; Chris@43: Z_HUFFMAN_ONLY = 2; Chris@43: Z_RLE = 3; Chris@43: Z_DEFAULT_STRATEGY = 0; Chris@43: Chris@43: Z_BINARY = 0; Chris@43: Z_ASCII = 1; Chris@43: Z_UNKNOWN = 2; Chris@43: Chris@43: Z_DEFLATED = 8; Chris@43: Chris@43: Chris@43: {$L adler32.obj} Chris@43: {$L compress.obj} Chris@43: {$L crc32.obj} Chris@43: {$L deflate.obj} Chris@43: {$L infback.obj} Chris@43: {$L inffast.obj} Chris@43: {$L inflate.obj} Chris@43: {$L inftrees.obj} Chris@43: {$L trees.obj} Chris@43: {$L uncompr.obj} Chris@43: {$L zutil.obj} Chris@43: Chris@43: procedure adler32; external; Chris@43: procedure compressBound; external; Chris@43: procedure crc32; external; Chris@43: procedure deflateInit2_; external; Chris@43: procedure deflateParams; external; Chris@43: Chris@43: function _malloc(Size: Integer): Pointer; cdecl; Chris@43: begin Chris@43: Result := AllocMem(Size); Chris@43: end; Chris@43: Chris@43: procedure _free(Block: Pointer); cdecl; Chris@43: begin Chris@43: FreeMem(Block); Chris@43: end; Chris@43: Chris@43: procedure _memset(P: Pointer; B: Byte; count: Integer); cdecl; Chris@43: begin Chris@43: FillChar(P^, count, B); Chris@43: end; Chris@43: Chris@43: procedure _memcpy(dest, source: Pointer; count: Integer); cdecl; Chris@43: begin Chris@43: Move(source^, dest^, count); Chris@43: end; Chris@43: Chris@43: Chris@43: Chris@43: // deflate compresses data Chris@43: function deflateInit_(var strm: TZStreamRec; level: Integer; version: PChar; Chris@43: recsize: Integer): Integer; external; Chris@43: function deflate(var strm: TZStreamRec; flush: Integer): Integer; external; Chris@43: function deflateEnd(var strm: TZStreamRec): Integer; external; Chris@43: Chris@43: // inflate decompresses data Chris@43: function inflateInit_(var strm: TZStreamRec; version: PChar; Chris@43: recsize: Integer): Integer; external; Chris@43: function inflate(var strm: TZStreamRec; flush: Integer): Integer; external; Chris@43: function inflateEnd(var strm: TZStreamRec): Integer; external; Chris@43: function inflateReset(var strm: TZStreamRec): Integer; external; Chris@43: Chris@43: Chris@43: function zlibAllocMem(AppData: Pointer; Items, Size: Integer): Pointer; cdecl; Chris@43: begin Chris@43: // GetMem(Result, Items*Size); Chris@43: Result := AllocMem(Items * Size); Chris@43: end; Chris@43: Chris@43: procedure zlibFreeMem(AppData, Block: Pointer); cdecl; Chris@43: begin Chris@43: FreeMem(Block); Chris@43: end; Chris@43: Chris@43: {function zlibCheck(code: Integer): Integer; Chris@43: begin Chris@43: Result := code; Chris@43: if code < 0 then Chris@43: raise EZlibError.Create('error'); //!! Chris@43: end;} Chris@43: Chris@43: function CCheck(code: Integer): Integer; Chris@43: begin Chris@43: Result := code; Chris@43: if code < 0 then Chris@43: raise ECompressionError.Create('error'); //!! Chris@43: end; Chris@43: Chris@43: function DCheck(code: Integer): Integer; Chris@43: begin Chris@43: Result := code; Chris@43: if code < 0 then Chris@43: raise EDecompressionError.Create('error'); //!! Chris@43: end; Chris@43: Chris@43: procedure CompressBuf(const InBuf: Pointer; InBytes: Integer; Chris@43: out OutBuf: Pointer; out OutBytes: Integer); Chris@43: var Chris@43: strm: TZStreamRec; Chris@43: P: Pointer; Chris@43: begin Chris@43: FillChar(strm, sizeof(strm), 0); Chris@43: strm.zalloc := zlibAllocMem; Chris@43: strm.zfree := zlibFreeMem; Chris@43: OutBytes := ((InBytes + (InBytes div 10) + 12) + 255) and not 255; Chris@43: GetMem(OutBuf, OutBytes); Chris@43: try Chris@43: strm.next_in := InBuf; Chris@43: strm.avail_in := InBytes; Chris@43: strm.next_out := OutBuf; Chris@43: strm.avail_out := OutBytes; Chris@43: CCheck(deflateInit_(strm, Z_BEST_COMPRESSION, zlib_version, sizeof(strm))); Chris@43: try Chris@43: while CCheck(deflate(strm, Z_FINISH)) <> Z_STREAM_END do Chris@43: begin Chris@43: P := OutBuf; Chris@43: Inc(OutBytes, 256); Chris@43: ReallocMem(OutBuf, OutBytes); Chris@43: strm.next_out := PChar(Integer(OutBuf) + (Integer(strm.next_out) - Integer(P))); Chris@43: strm.avail_out := 256; Chris@43: end; Chris@43: finally Chris@43: CCheck(deflateEnd(strm)); Chris@43: end; Chris@43: ReallocMem(OutBuf, strm.total_out); Chris@43: OutBytes := strm.total_out; Chris@43: except Chris@43: FreeMem(OutBuf); Chris@43: raise Chris@43: end; Chris@43: end; Chris@43: Chris@43: Chris@43: procedure DecompressBuf(const InBuf: Pointer; InBytes: Integer; Chris@43: OutEstimate: Integer; out OutBuf: Pointer; out OutBytes: Integer); Chris@43: var Chris@43: strm: TZStreamRec; Chris@43: P: Pointer; Chris@43: BufInc: Integer; Chris@43: begin Chris@43: FillChar(strm, sizeof(strm), 0); Chris@43: strm.zalloc := zlibAllocMem; Chris@43: strm.zfree := zlibFreeMem; Chris@43: BufInc := (InBytes + 255) and not 255; Chris@43: if OutEstimate = 0 then Chris@43: OutBytes := BufInc Chris@43: else Chris@43: OutBytes := OutEstimate; Chris@43: GetMem(OutBuf, OutBytes); Chris@43: try Chris@43: strm.next_in := InBuf; Chris@43: strm.avail_in := InBytes; Chris@43: strm.next_out := OutBuf; Chris@43: strm.avail_out := OutBytes; Chris@43: DCheck(inflateInit_(strm, zlib_version, sizeof(strm))); Chris@43: try Chris@43: while DCheck(inflate(strm, Z_NO_FLUSH)) <> Z_STREAM_END do Chris@43: begin Chris@43: P := OutBuf; Chris@43: Inc(OutBytes, BufInc); Chris@43: ReallocMem(OutBuf, OutBytes); Chris@43: strm.next_out := PChar(Integer(OutBuf) + (Integer(strm.next_out) - Integer(P))); Chris@43: strm.avail_out := BufInc; Chris@43: end; Chris@43: finally Chris@43: DCheck(inflateEnd(strm)); Chris@43: end; Chris@43: ReallocMem(OutBuf, strm.total_out); Chris@43: OutBytes := strm.total_out; Chris@43: except Chris@43: FreeMem(OutBuf); Chris@43: raise Chris@43: end; Chris@43: end; Chris@43: Chris@43: procedure DecompressToUserBuf(const InBuf: Pointer; InBytes: Integer; Chris@43: const OutBuf: Pointer; BufSize: Integer); Chris@43: var Chris@43: strm: TZStreamRec; Chris@43: begin Chris@43: FillChar(strm, sizeof(strm), 0); Chris@43: strm.zalloc := zlibAllocMem; Chris@43: strm.zfree := zlibFreeMem; Chris@43: strm.next_in := InBuf; Chris@43: strm.avail_in := InBytes; Chris@43: strm.next_out := OutBuf; Chris@43: strm.avail_out := BufSize; Chris@43: DCheck(inflateInit_(strm, zlib_version, sizeof(strm))); Chris@43: try Chris@43: if DCheck(inflate(strm, Z_FINISH)) <> Z_STREAM_END then Chris@43: raise EZlibError.CreateRes(@sTargetBufferTooSmall); Chris@43: finally Chris@43: DCheck(inflateEnd(strm)); Chris@43: end; Chris@43: end; Chris@43: Chris@43: // TCustomZlibStream Chris@43: Chris@43: constructor TCustomZLibStream.Create(Strm: TStream); Chris@43: begin Chris@43: inherited Create; Chris@43: FStrm := Strm; Chris@43: FStrmPos := Strm.Position; Chris@43: FZRec.zalloc := zlibAllocMem; Chris@43: FZRec.zfree := zlibFreeMem; Chris@43: end; Chris@43: Chris@43: procedure TCustomZLibStream.Progress(Sender: TObject); Chris@43: begin Chris@43: if Assigned(FOnProgress) then FOnProgress(Sender); Chris@43: end; Chris@43: Chris@43: Chris@43: // TCompressionStream Chris@43: Chris@43: constructor TCompressionStream.Create(CompressionLevel: TCompressionLevel; Chris@43: Dest: TStream); Chris@43: const Chris@43: Levels: array [TCompressionLevel] of ShortInt = Chris@43: (Z_NO_COMPRESSION, Z_BEST_SPEED, Z_DEFAULT_COMPRESSION, Z_BEST_COMPRESSION); Chris@43: begin Chris@43: inherited Create(Dest); Chris@43: FZRec.next_out := FBuffer; Chris@43: FZRec.avail_out := sizeof(FBuffer); Chris@43: CCheck(deflateInit_(FZRec, Levels[CompressionLevel], zlib_version, sizeof(FZRec))); Chris@43: end; Chris@43: Chris@43: destructor TCompressionStream.Destroy; Chris@43: begin Chris@43: FZRec.next_in := nil; Chris@43: FZRec.avail_in := 0; Chris@43: try Chris@43: if FStrm.Position <> FStrmPos then FStrm.Position := FStrmPos; Chris@43: while (CCheck(deflate(FZRec, Z_FINISH)) <> Z_STREAM_END) Chris@43: and (FZRec.avail_out = 0) do Chris@43: begin Chris@43: FStrm.WriteBuffer(FBuffer, sizeof(FBuffer)); Chris@43: FZRec.next_out := FBuffer; Chris@43: FZRec.avail_out := sizeof(FBuffer); Chris@43: end; Chris@43: if FZRec.avail_out < sizeof(FBuffer) then Chris@43: FStrm.WriteBuffer(FBuffer, sizeof(FBuffer) - FZRec.avail_out); Chris@43: finally Chris@43: deflateEnd(FZRec); Chris@43: end; Chris@43: inherited Destroy; Chris@43: end; Chris@43: Chris@43: function TCompressionStream.Read(var Buffer; Count: Longint): Longint; Chris@43: begin Chris@43: raise ECompressionError.CreateRes(@sInvalidStreamOp); Chris@43: end; Chris@43: Chris@43: function TCompressionStream.Write(const Buffer; Count: Longint): Longint; Chris@43: begin Chris@43: FZRec.next_in := @Buffer; Chris@43: FZRec.avail_in := Count; Chris@43: if FStrm.Position <> FStrmPos then FStrm.Position := FStrmPos; Chris@43: while (FZRec.avail_in > 0) do Chris@43: begin Chris@43: CCheck(deflate(FZRec, 0)); Chris@43: if FZRec.avail_out = 0 then Chris@43: begin Chris@43: FStrm.WriteBuffer(FBuffer, sizeof(FBuffer)); Chris@43: FZRec.next_out := FBuffer; Chris@43: FZRec.avail_out := sizeof(FBuffer); Chris@43: FStrmPos := FStrm.Position; Chris@43: Progress(Self); Chris@43: end; Chris@43: end; Chris@43: Result := Count; Chris@43: end; Chris@43: Chris@43: function TCompressionStream.Seek(Offset: Longint; Origin: Word): Longint; Chris@43: begin Chris@43: if (Offset = 0) and (Origin = soFromCurrent) then Chris@43: Result := FZRec.total_in Chris@43: else Chris@43: raise ECompressionError.CreateRes(@sInvalidStreamOp); Chris@43: end; Chris@43: Chris@43: function TCompressionStream.GetCompressionRate: Single; Chris@43: begin Chris@43: if FZRec.total_in = 0 then Chris@43: Result := 0 Chris@43: else Chris@43: Result := (1.0 - (FZRec.total_out / FZRec.total_in)) * 100.0; Chris@43: end; Chris@43: Chris@43: Chris@43: // TDecompressionStream Chris@43: Chris@43: constructor TDecompressionStream.Create(Source: TStream); Chris@43: begin Chris@43: inherited Create(Source); Chris@43: FZRec.next_in := FBuffer; Chris@43: FZRec.avail_in := 0; Chris@43: DCheck(inflateInit_(FZRec, zlib_version, sizeof(FZRec))); Chris@43: end; Chris@43: Chris@43: destructor TDecompressionStream.Destroy; Chris@43: begin Chris@43: FStrm.Seek(-FZRec.avail_in, 1); Chris@43: inflateEnd(FZRec); Chris@43: inherited Destroy; Chris@43: end; Chris@43: Chris@43: function TDecompressionStream.Read(var Buffer; Count: Longint): Longint; Chris@43: begin Chris@43: FZRec.next_out := @Buffer; Chris@43: FZRec.avail_out := Count; Chris@43: if FStrm.Position <> FStrmPos then FStrm.Position := FStrmPos; Chris@43: while (FZRec.avail_out > 0) do Chris@43: begin Chris@43: if FZRec.avail_in = 0 then Chris@43: begin Chris@43: FZRec.avail_in := FStrm.Read(FBuffer, sizeof(FBuffer)); Chris@43: if FZRec.avail_in = 0 then Chris@43: begin Chris@43: Result := Count - FZRec.avail_out; Chris@43: Exit; Chris@43: end; Chris@43: FZRec.next_in := FBuffer; Chris@43: FStrmPos := FStrm.Position; Chris@43: Progress(Self); Chris@43: end; Chris@43: CCheck(inflate(FZRec, 0)); Chris@43: end; Chris@43: Result := Count; Chris@43: end; Chris@43: Chris@43: function TDecompressionStream.Write(const Buffer; Count: Longint): Longint; Chris@43: begin Chris@43: raise EDecompressionError.CreateRes(@sInvalidStreamOp); Chris@43: end; Chris@43: Chris@43: function TDecompressionStream.Seek(Offset: Longint; Origin: Word): Longint; Chris@43: var Chris@43: I: Integer; Chris@43: Buf: array [0..4095] of Char; Chris@43: begin Chris@43: if (Offset = 0) and (Origin = soFromBeginning) then Chris@43: begin Chris@43: DCheck(inflateReset(FZRec)); Chris@43: FZRec.next_in := FBuffer; Chris@43: FZRec.avail_in := 0; Chris@43: FStrm.Position := 0; Chris@43: FStrmPos := 0; Chris@43: end Chris@43: else if ( (Offset >= 0) and (Origin = soFromCurrent)) or Chris@43: ( ((Offset - FZRec.total_out) > 0) and (Origin = soFromBeginning)) then Chris@43: begin Chris@43: if Origin = soFromBeginning then Dec(Offset, FZRec.total_out); Chris@43: if Offset > 0 then Chris@43: begin Chris@43: for I := 1 to Offset div sizeof(Buf) do Chris@43: ReadBuffer(Buf, sizeof(Buf)); Chris@43: ReadBuffer(Buf, Offset mod sizeof(Buf)); Chris@43: end; Chris@43: end Chris@43: else Chris@43: raise EDecompressionError.CreateRes(@sInvalidStreamOp); Chris@43: Result := FZRec.total_out; Chris@43: end; Chris@43: Chris@43: Chris@43: end.