Solved

Decompression of gzip files....

Posted on 1998-08-12
6
635 Views
Last Modified: 2010-04-04

I have some gzip compressed files, all I want to do is to decompress these files without using any external program. I want to include a dll in my own program and make function calls to decompress these files .To do that I got a dll from zlib. The dll name is zlib.dll but there is no explanation for the exported functions and the function arguments of the dll. I forwarded this question to the guys who had written the dll but the answer was not satisfactory

Any suggestions,

Thanx
0
Comment
Question by:ms99y
  • 4
  • 2
6 Comments
 
LVL 4

Accepted Solution

by:
erajoj earned 60 total points
ID: 1361962
Is it this one?:

{*******************************************************}
{                                                       }
{       Delphi Supplemental Components                  }
{       ZLIB Data Compression Interface Unit            }
{                                                       }
{       Copyright (c) 1997 Borland International        }
{                                                       }
{*******************************************************}

{ Modified for zlib 1.1.3 by Davide Moretti <dave@rimini.com }

unit zlib;

interface

uses Sysutils, Classes;

type
  TAlloc = function (AppData: Pointer; Items, Size: Integer): Pointer;
  TFree = procedure (AppData, Block: Pointer);

  // Internal structure.  Ignore.
  TZStreamRec = packed record
    next_in: PChar;       // next input byte
    avail_in: Integer;    // number of bytes available at next_in
    total_in: Integer;    // total nb of input bytes read so far

    next_out: PChar;      // next output byte should be put here
    avail_out: Integer;   // remaining free space at next_out
    total_out: Integer;   // total nb of bytes output so far

    msg: PChar;           // last error message, NULL if no error
    internal: Pointer;    // not visible by applications

    zalloc: TAlloc;       // used to allocate the internal state
    zfree: TFree;         // used to free the internal state
    AppData: Pointer;     // private data object passed to zalloc and zfree

    data_type: Integer;   //  best guess about the data type: ascii or binary
    adler: Integer;       // adler32 value of the uncompressed data
    reserved: Integer;    // reserved for future use
  end;

  // Abstract ancestor class
  TCustomZlibStream = class(TStream)
  private
    FStrm: TStream;
    FStrmPos: Integer;
    FOnProgress: TNotifyEvent;
    FZRec: TZStreamRec;
    FBuffer: array [Word] of Char;
  protected
    procedure Progress(Sender: TObject); dynamic;
    property OnProgress: TNotifyEvent read FOnProgress write FOnProgress;
    constructor Create(Strm: TStream);
  end;

{ TCompressionStream compresses data on the fly as data is written to it, and
  stores the compressed data to another stream.

  TCompressionStream is write-only and strictly sequential. Reading from the
  stream will raise an exception. Using Seek to move the stream pointer
  will raise an exception.

  Output data is cached internally, written to the output stream only when
  the internal output buffer is full.  All pending output data is flushed
  when the stream is destroyed.

  The Position property returns the number of uncompressed bytes of
  data that have been written to the stream so far.

  CompressionRate returns the on-the-fly percentage by which the original
  data has been compressed:  (1 - (CompressedBytes / UncompressedBytes)) * 100
  If raw data size = 100 and compressed data size = 25, the CompressionRate
  is 75%

  The OnProgress event is called each time the output buffer is filled and
  written to the output stream.  This is useful for updating a progress
  indicator when you are writing a large chunk of data to the compression
  stream in a single call.}


  TCompressionLevel = (clNone, clFastest, clDefault, clMax);

  TCompressionStream = class(TCustomZlibStream)
  private
    function GetCompressionRate: Single;
  public
    constructor Create(CompressionLevel: TCompressionLevel; Dest: TStream);
    destructor Destroy; override;
    function Read(var Buffer; Count: Longint): Longint; override;
    function Write(const Buffer; Count: Longint): Longint; override;
    function Seek(Offset: Longint; Origin: Word): Longint; override;
    property CompressionRate: Single read GetCompressionRate;
    property OnProgress;
  end;

{ TDecompressionStream decompresses data on the fly as data is read from it.

  Compressed data comes from a separate source stream.  TDecompressionStream
  is read-only and unidirectional; you can seek forward in the stream, but not
  backwards.  The special case of setting the stream position to zero is
  allowed.  Seeking forward decompresses data until the requested position in
  the uncompressed data has been reached.  Seeking backwards, seeking relative
  to the end of the stream, requesting the size of the stream, and writing to
  the stream will raise an exception.

  The Position property returns the number of bytes of uncompressed data that
  have been read from the stream so far.

  The OnProgress event is called each time the internal input buffer of
  compressed data is exhausted and the next block is read from the input stream.
  This is useful for updating a progress indicator when you are reading a
  large chunk of data from the decompression stream in a single call.}

  TDecompressionStream = class(TCustomZlibStream)
  public
    constructor Create(Source: TStream);
    destructor Destroy; override;
    function Read(var Buffer; Count: Longint): Longint; override;
    function Write(const Buffer; Count: Longint): Longint; override;
    function Seek(Offset: Longint; Origin: Word): Longint; override;
    property OnProgress;
  end;



{ CompressBuf compresses data, buffer to buffer, in one call.
   In: InBuf = ptr to compressed data
       InBytes = number of bytes in InBuf
  Out: OutBuf = ptr to newly allocated buffer containing decompressed data
       OutBytes = number of bytes in OutBuf   }
procedure CompressBuf(const InBuf: Pointer; InBytes: Integer;
                      out OutBuf: Pointer; out OutBytes: Integer);


{ DecompressBuf decompresses data, buffer to buffer, in one call.
   In: InBuf = ptr to compressed data
       InBytes = number of bytes in InBuf
       OutEstimate = zero, or est. size of the decompressed data
  Out: OutBuf = ptr to newly allocated buffer containing decompressed data
       OutBytes = number of bytes in OutBuf   }
procedure DecompressBuf(const InBuf: Pointer; InBytes: Integer;
 OutEstimate: Integer; out OutBuf: Pointer; out OutBytes: Integer);

const
  zlib_version = '1.1.3';

type
  EZlibError = class(Exception);
  ECompressionError = class(EZlibError);
  EDecompressionError = class(EZlibError);

function adler32(adler: Integer; buf: PChar; len: Integer): Integer;

implementation

const
  Z_NO_FLUSH      = 0;
  Z_PARTIAL_FLUSH = 1;
  Z_SYNC_FLUSH    = 2;
  Z_FULL_FLUSH    = 3;
  Z_FINISH        = 4;

  Z_OK            = 0;
  Z_STREAM_END    = 1;
  Z_NEED_DICT     = 2;
  Z_ERRNO         = (-1);
  Z_STREAM_ERROR  = (-2);
  Z_DATA_ERROR    = (-3);
  Z_MEM_ERROR     = (-4);
  Z_BUF_ERROR     = (-5);
  Z_VERSION_ERROR = (-6);

  Z_NO_COMPRESSION       =   0;
  Z_BEST_SPEED           =   1;
  Z_BEST_COMPRESSION     =   9;
  Z_DEFAULT_COMPRESSION  = (-1);

  Z_FILTERED            = 1;
  Z_HUFFMAN_ONLY        = 2;
  Z_DEFAULT_STRATEGY    = 0;

  Z_BINARY   = 0;
  Z_ASCII    = 1;
  Z_UNKNOWN  = 2;

  Z_DEFLATED = 8;

  _z_errmsg: array[0..9] of PChar = (
    'need dictionary',      // Z_NEED_DICT      (2)
    'stream end',           // Z_STREAM_END     (1)
    '',                     // Z_OK             (0)
    'file error',           // Z_ERRNO          (-1)
    'stream error',         // Z_STREAM_ERROR   (-2)
    'data error',           // Z_DATA_ERROR     (-3)
    'insufficient memory',  // Z_MEM_ERROR      (-4)
    'buffer error',         // Z_BUF_ERROR      (-5)
    'incompatible version', // Z_VERSION_ERROR  (-6)
    ''
  );

{$L deflate.obj}
{$L inflate.obj}
{$L inftrees.obj}
{$L trees.obj}
{$L adler32.obj}
{$L infblock.obj}
{$L infcodes.obj}
{$L infutil.obj}
{$L inffast.obj}

procedure _tr_init; external;
procedure _tr_tally; external;
procedure _tr_flush_block; external;
procedure _tr_align; external;
procedure _tr_stored_block; external;
function adler32; external;
procedure inflate_blocks_new; external;
procedure inflate_blocks; external;
procedure inflate_blocks_reset; external;
procedure inflate_blocks_free; external;
procedure inflate_set_dictionary; external;
procedure inflate_trees_bits; external;
procedure inflate_trees_dynamic; external;
procedure inflate_trees_fixed; external;
procedure inflate_codes_new; external;
procedure inflate_codes; external;
procedure inflate_codes_free; external;
procedure _inflate_mask; external;
procedure inflate_flush; external;
procedure inflate_fast; external;

procedure _memset(P: Pointer; B: Byte; count: Integer);cdecl;
begin
  FillChar(P^, count, B);
end;

procedure _memcpy(dest, source: Pointer; count: Integer);cdecl;
begin
  Move(source^, dest^, count);
end;



// deflate compresses data
function deflateInit_(var strm: TZStreamRec; level: Integer; version: PChar;
  recsize: Integer): Integer; external;
function deflate(var strm: TZStreamRec; flush: Integer): Integer; external;
function deflateEnd(var strm: TZStreamRec): Integer; external;

// inflate decompresses data
function inflateInit_(var strm: TZStreamRec; version: PChar;
  recsize: Integer): Integer; external;
function inflate(var strm: TZStreamRec; flush: Integer): Integer; external;
function inflateEnd(var strm: TZStreamRec): Integer; external;
function inflateReset(var strm: TZStreamRec): Integer; external;


function zcalloc(AppData: Pointer; Items, Size: Integer): Pointer;
begin
  GetMem(Result, Items*Size);
end;

procedure zcfree(AppData, Block: Pointer);
begin
  FreeMem(Block);
end;

function zlibCheck(code: Integer): Integer;
begin
  Result := code;
  if code < 0 then
    raise EZlibError.Create('error');    //!!
end;

function CCheck(code: Integer): Integer;
begin
  Result := code;
  if code < 0 then
    raise ECompressionError.Create('error'); //!!
end;

function DCheck(code: Integer): Integer;
begin
  Result := code;
  if code < 0 then
    raise EDecompressionError.Create('error');  //!!
end;

procedure CompressBuf(const InBuf: Pointer; InBytes: Integer;
                      out OutBuf: Pointer; out OutBytes: Integer);
var
  strm: TZStreamRec;
  P: Pointer;
begin
  FillChar(strm, sizeof(strm), 0);
  OutBytes := ((InBytes + (InBytes div 10) + 12) + 255) and not 255;
  GetMem(OutBuf, OutBytes);
  try
    strm.next_in := InBuf;
    strm.avail_in := InBytes;
    strm.next_out := OutBuf;
    strm.avail_out := OutBytes;
    CCheck(deflateInit_(strm, Z_BEST_COMPRESSION, zlib_version, sizeof(strm)));
    try
      while CCheck(deflate(strm, Z_FINISH)) <> Z_STREAM_END do
      begin
        P := OutBuf;
        Inc(OutBytes, 256);
        ReallocMem(OutBuf, OutBytes);
        strm.next_out := PChar(Integer(OutBuf) + (Integer(strm.next_out) - Integer(P)));
        strm.avail_out := 256;
      end;
    finally
      CCheck(deflateEnd(strm));
    end;
    ReallocMem(OutBuf, strm.total_out);
    OutBytes := strm.total_out;
  except
    FreeMem(OutBuf);
    raise
  end;
end;


procedure DecompressBuf(const InBuf: Pointer; InBytes: Integer;
  OutEstimate: Integer; out OutBuf: Pointer; out OutBytes: Integer);
var
  strm: TZStreamRec;
  P: Pointer;
  BufInc: Integer;
begin
  FillChar(strm, sizeof(strm), 0);
  BufInc := (InBytes + 255) and not 255;
  if OutEstimate = 0 then
    OutBytes := BufInc
  else
    OutBytes := OutEstimate;
  GetMem(OutBuf, OutBytes);
  try
    strm.next_in := InBuf;
    strm.avail_in := InBytes;
    strm.next_out := OutBuf;
    strm.avail_out := OutBytes;
    DCheck(inflateInit_(strm, zlib_version, sizeof(strm)));
    try
      while DCheck(inflate(strm, Z_FINISH)) <> Z_STREAM_END do
      begin
        P := OutBuf;
        Inc(OutBytes, BufInc);
        ReallocMem(OutBuf, OutBytes);
        strm.next_out := PChar(Integer(OutBuf) + (Integer(strm.next_out) - Integer(P)));
        strm.avail_out := BufInc;
      end;
    finally
      DCheck(inflateEnd(strm));
    end;
    ReallocMem(OutBuf, strm.total_out);
    OutBytes := strm.total_out;
  except
    FreeMem(OutBuf);
    raise
  end;
end;


// TCustomZlibStream

constructor TCustomZLibStream.Create(Strm: TStream);
begin
  inherited Create;
  FStrm := Strm;
  FStrmPos := Strm.Position;
end;

procedure TCustomZLibStream.Progress(Sender: TObject);
begin
  if Assigned(FOnProgress) then FOnProgress(Sender);
end;


// TCompressionStream

constructor TCompressionStream.Create(CompressionLevel: TCompressionLevel;
  Dest: TStream);
const
  Levels: array [TCompressionLevel] of ShortInt =
    (Z_NO_COMPRESSION, Z_BEST_SPEED, Z_DEFAULT_COMPRESSION, Z_BEST_COMPRESSION);
begin
  inherited Create(Dest);
  FZRec.next_out := FBuffer;
  FZRec.avail_out := sizeof(FBuffer);
  CCheck(deflateInit_(FZRec, Levels[CompressionLevel], zlib_version, sizeof(FZRec)));
end;

destructor TCompressionStream.Destroy;
begin
  FZRec.next_in := nil;
  FZRec.avail_in := 0;
  try
    if FStrm.Position <> FStrmPos then FStrm.Position := FStrmPos;
    while (CCheck(deflate(FZRec, Z_FINISH)) <> Z_STREAM_END)
      and (FZRec.avail_out = 0) do
    begin
      FStrm.WriteBuffer(FBuffer, sizeof(FBuffer));
      FZRec.next_out := FBuffer;
      FZRec.avail_out := sizeof(FBuffer);
    end;
    if FZRec.avail_out < sizeof(FBuffer) then
      FStrm.WriteBuffer(FBuffer, sizeof(FBuffer) - FZRec.avail_out);
  finally
    deflateEnd(FZRec);
  end;
  inherited Destroy;
end;

function TCompressionStream.Read(var Buffer; Count: Longint): Longint;
begin
  raise ECompressionError.Create('Invalid stream operation');
end;

function TCompressionStream.Write(const Buffer; Count: Longint): Longint;
begin
  FZRec.next_in := @Buffer;
  FZRec.avail_in := Count;
  if FStrm.Position <> FStrmPos then FStrm.Position := FStrmPos;
  while (FZRec.avail_in > 0) do
  begin
    CCheck(deflate(FZRec, 0));
    if FZRec.avail_out = 0 then
    begin
      FStrm.WriteBuffer(FBuffer, sizeof(FBuffer));
      FZRec.next_out := FBuffer;
      FZRec.avail_out := sizeof(FBuffer);
      FStrmPos := FStrm.Position;
      Progress(Self);
    end;
  end;
  Result := Count;
end;

function TCompressionStream.Seek(Offset: Longint; Origin: Word): Longint;
begin
  if (Offset = 0) and (Origin = soFromCurrent) then
    Result := FZRec.total_in
  else
    raise ECompressionError.Create('Invalid stream operation');
end;

function TCompressionStream.GetCompressionRate: Single;
begin
  if FZRec.total_in = 0 then
    Result := 0
  else
    Result := (1.0 - (FZRec.total_out / FZRec.total_in)) * 100.0;
end;


// TDecompressionStream

constructor TDecompressionStream.Create(Source: TStream);
begin
  inherited Create(Source);
  FZRec.next_in := FBuffer;
  FZRec.avail_in := 0;
  DCheck(inflateInit_(FZRec, zlib_version, sizeof(FZRec)));
end;

destructor TDecompressionStream.Destroy;
begin
  inflateEnd(FZRec);
  inherited Destroy;
end;

function TDecompressionStream.Read(var Buffer; Count: Longint): Longint;
begin
  FZRec.next_out := @Buffer;
  FZRec.avail_out := Count;
  if FStrm.Position <> FStrmPos then FStrm.Position := FStrmPos;
  while (FZRec.avail_out > 0) do
  begin
    if FZRec.avail_in = 0 then
    begin
      FZRec.avail_in := FStrm.Read(FBuffer, sizeof(FBuffer));
      if FZRec.avail_in = 0 then
        begin
          Result := Count - FZRec.avail_out;
          Exit;
        end;
      FZRec.next_in := FBuffer;
      FStrmPos := FStrm.Position;
      Progress(Self);
    end;
    DCheck(inflate(FZRec, 0));
  end;
  Result := Count;
end;

function TDecompressionStream.Write(const Buffer; Count: Longint): Longint;
begin
  raise EDecompressionError.Create('Invalid stream operation');
end;

function TDecompressionStream.Seek(Offset: Longint; Origin: Word): Longint;
var
  I: Integer;
  Buf: array [0..4095] of Char;
begin
  if (Offset = 0) and (Origin = soFromBeginning) then
  begin
    DCheck(inflateReset(FZRec));
    FZRec.next_in := FBuffer;
    FZRec.avail_in := 0;
    FStrm.Position := 0;
    FStrmPos := 0;
  end
  else if ( (Offset >= 0) and (Origin = soFromCurrent)) or
          ( ((Offset - FZRec.total_out) > 0) and (Origin = soFromBeginning)) then
  begin
    if Origin = soFromBeginning then Dec(Offset, FZRec.total_out);
    if Offset > 0 then
    begin
      for I := 1 to Offset div sizeof(Buf) do
        ReadBuffer(Buf, sizeof(Buf));
      ReadBuffer(Buf, Offset mod sizeof(Buf));
    end;
  end
  else
    raise EDecompressionError.Create('Invalid stream operation');
  Result := FZRec.total_out;
end;

end.

/// John
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1361963
Sorry!
Didnt't mean to send it as an answer.
I see now that it is wrong anyway

/// John
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1361964
Maybe this one?:

unit zlibdef;

interface

uses
  Windows;

const
  ZLIB_VERSION = '1.1.3';

type
  voidpf = Pointer;
  int    = Integer;
  uInt   = Cardinal;
  pBytef = PChar;
  uLong  = Cardinal;

  alloc_func = function(opaque: voidpf; items, size: uInt): voidpf;
                    stdcall;
  free_func  = procedure(opaque, address: voidpf);
                    stdcall;

  internal_state = Pointer;

  z_streamp = ^z_stream;
  z_stream = packed record
    next_in: pBytef;          // next input byte
    avail_in: uInt;           // number of bytes available at next_in
    total_in: uLong;          // total nb of input bytes read so far

    next_out: pBytef;         // next output byte should be put there
    avail_out: uInt;          // remaining free space at next_out
    total_out: uLong;         // total nb of bytes output so far

    msg: PChar;               // last error message, NULL if no error
    state: internal_state;    // not visible by applications

    zalloc: alloc_func;       // used to allocate the internal state
    zfree: free_func;         // used to free the internal state
    opaque: voidpf;           // private data object passed to zalloc and zfree

    data_type: int;           // best guess about the data type: ascii or binary
    adler: uLong;             // adler32 value of the uncompressed data
    reserved: uLong;          // reserved for future use
    end;

const
  Z_NO_FLUSH      = 0;
  Z_SYNC_FLUSH    = 2;
  Z_FULL_FLUSH    = 3;
  Z_FINISH        = 4;

  Z_OK            = 0;
  Z_STREAM_END    = 1;

  Z_NO_COMPRESSION         =  0;
  Z_BEST_SPEED             =  1;
  Z_BEST_COMPRESSION       =  9;
  Z_DEFAULT_COMPRESSION    = -1;

  Z_FILTERED            = 1;
  Z_HUFFMAN_ONLY        = 2;
  Z_DEFAULT_STRATEGY    = 0;

  Z_BINARY   = 0;
  Z_ASCII    = 1;
  Z_UNKNOWN  = 2;

  Z_DEFLATED    = 8;

  MAX_MEM_LEVEL = 9;

function adler32(adler: uLong; const buf: pBytef; len: uInt): uLong;
             stdcall;
function crc32(crc: uLong; const buf: pBytef; len: uInt): uLong;
             stdcall;
function deflate(strm: z_streamp; flush: int): int;
             stdcall;
function deflateCopy(dest, source: z_streamp): int;
             stdcall;
function deflateEnd(strm: z_streamp): int;
             stdcall;
function deflateInit2_(strm: z_streamp; level, method,
                       windowBits, memLevel, strategy: int;
                       const version: PChar; stream_size: int): int;
             stdcall;
function deflateInit_(strm: z_streamp; level: int;
                      const version: PChar; stream_size: int): int;
             stdcall;
function deflateParams(strm: z_streamp; level, strategy: int): int;
             stdcall;
function deflateReset(strm: z_streamp): int;
             stdcall;
function deflateSetDictionary(strm: z_streamp;
                              const dictionary: pBytef;
                              dictLength: uInt): int;
             stdcall;
function inflate(strm: z_streamp; flush: int): int;
             stdcall;
function inflateEnd(strm: z_streamp): int;
             stdcall;
function inflateInit2_(strm: z_streamp; windowBits: int;
                       const version: PChar; stream_size: int): int;
             stdcall;
function inflateInit_(strm: z_streamp; const version: PChar;
                      stream_size: int): int;
             stdcall;
function inflateReset(strm: z_streamp): int;
             stdcall;
function inflateSetDictionary(strm: z_streamp;
                              const dictionary: pBytef;
                              dictLength: uInt): int;
             stdcall;
function inflateSync(strm: z_streamp): int;
             stdcall;

function deflateInit(strm: z_streamp; level: int): int;
function deflateInit2(strm: z_streamp; level, method, windowBits,
                      memLevel, strategy: int): int;
function inflateInit(strm: z_streamp): int;
function inflateInit2(strm: z_streamp; windowBits: int): int;

implementation

function deflateInit(strm: z_streamp; level: int): int;
begin
  Result := deflateInit_(strm, level, ZLIB_VERSION, sizeof(z_stream));
end;

function deflateInit2(strm: z_streamp; level, method, windowBits,
                      memLevel, strategy: int): int;
begin
  Result := deflateInit2_(strm, level, method, windowBits, memLevel,
                          strategy, ZLIB_VERSION, sizeof(z_stream));
end;

function inflateInit(strm: z_streamp): int;
begin
  Result := inflateInit_(strm, ZLIB_VERSION, sizeof(z_stream));
end;

function inflateInit2(strm: z_streamp; windowBits: int): int;
begin
  Result := inflateInit2_(strm, windowBits, ZLIB_VERSION,
                          sizeof(z_stream));
end;

const
  zlibDLL = 'png32bd.dll';

function adler32; external zlibDLL;
function crc32; external zlibDLL;
function deflate; external zlibDLL;
function deflateCopy; external zlibDLL;
function deflateEnd; external zlibDLL;
function deflateInit2_; external zlibDLL;
function deflateInit_; external zlibDLL;
function deflateParams; external zlibDLL;
function deflateReset; external zlibDLL;
function deflateSetDictionary; external zlibDLL;
function inflate; external zlibDLL;
function inflateEnd; external zlibDLL;
function inflateInit2_; external zlibDLL;
function inflateInit_; external zlibDLL;
function inflateReset; external zlibDLL;
function inflateSetDictionary; external zlibDLL;
function inflateSync; external zlibDLL;

end.

/// John
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 4

Expert Comment

by:erajoj
ID: 1361965
Or... maybe yhis one (most likely)?:

unit zLib;

{ zlib.h -- interface of the 'zlib' general purpose compression library
      version 1.1.1, Feb 27th, 1998

      Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler

      This software is provided 'as-is', without any express or implied
      warranty.  In no event will the authors be held liable for any damages
      arising from the use of this software.

      Permission is granted to anyone to use this software for any purpose,
      including commercial applications, and to alter it and redistribute it
      freely, subject to the following restrictions:

      1. The origin of this software must not be misrepresented; you must not
             claim that you wrote the original software. If you use this software
             in a product, an acknowledgment in the product documentation would be
             appreciated but is not required.
      2. Altered source versions must be plainly marked as such, and must not be
             misrepresented as being the original software.
      3. This notice may not be removed or altered from any source distribution.

      Jean-loup Gailly        Mark Adler
      jloup@gzip.org          madler@alumni.caltech.edu


      The data format used by the zlib library is described by RFCs (Request for
      Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
      (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).

      zlib.pas -- Delphi 2.0 interface for the pre-built 'zlib' DLL available at
      http://alessandria.alpcom.it/~iaco/zlib.

      This code is free under the same terms and conditions as 'zlib.'

      Delphi version originally created by Don Hatlestad:

      Copyright (c) 1996 Don Hatlestad, Art Anderson Associates

      Version updated by Lasse Vågsæther Karlsen to conform to changes done to
      the c release of zLib, up to and including version 1.1.1. Also, all
      comments and unnecessary code, constants and structures have been
      removed from this source file, and it has been generally tidied up.

      If you wish to read the comments for each function, look them up in the
      zLib.h file which follows this zLib.pas file. zLib.h is the 1.1.1 header
      file from the original zlib package.

      zLib is available from these sources:
            Official Homepage:                  http://www.cdrom.com/pub/infozip/zlib/zlib.html
            zLib.DLL homepage:                  http://www.winimage.com/zLibDll/
            zLib.pas homepage:                  http://home.sol.no/~lassevk/zlib.htm
}

interface

type
      PInteger                        = ^Integer;
      uInt                                    = Cardinal;
      uLong                                    = LongInt;
      uLongf                              = ULONG;
      puLongf                              = ^ULONG;
      voidpf                              = Pointer;
      voidp                                    = Pointer;
      z_off_t                              = LongInt;
      crc_table                        = array[Byte] of uLongf;
      pcrc_table                  = ^crc_table;
      TGZFILE                              = VOIDP;
      alloc_func                  = function(opaque : voidpf; items : uInt; size : uInt) : Pointer; cdecl;
      free_func                        = procedure(opaque : voidpf; address : voidpf); cdecl;
      internal_state      = record end;
      pinternal_state      = ^internal_state;
      z_stream                        = record
            next_in                        : Pointer;
            avail_in                  : Cardinal;
            total_in                  : LongInt;
            next_out                  : Pointer;
            avail_out                  : Cardinal;
            total_out                  : LongInt;
            msg                                    : PChar;
            state                              : pinternal_state;
            zalloc                        : alloc_func;
            zfree                              : free_func;
            opaque                        : Pointer;
            data_type                  : Integer;
            adler                              : LongInt;
            reserved                  : LongInt;
      end;

const
      SEEK_SET                                          = 0;
      SEEK_CUR                                          = 1;
      ZLIB_VERSION                              : PChar = '1.1.1';

      Z_NO_FLUSH                                    = 0;
      Z_PARTIAL_FLUSH                        = 1;
      Z_SYNC_FLUSH                              = 2;
      Z_FULL_FLUSH                              = 3;
      Z_FINISH                                          = 4;

      Z_OK                                                      = 0;
      Z_STREAM_END                              = 1;
      Z_NEED_DICT                                    = 2;
      Z_ERRNO                                                = (-1);
      Z_STREAM_ERROR                        = (-2);
      Z_DATA_ERROR                              = (-3);
      Z_MEM_ERROR                                    = (-4);
      Z_BUF_ERROR                                    = (-5);
      Z_VERSION_ERROR                        = (-6);

      Z_NO_COMPRESSION                  = 0;
      Z_BEST_SPEED                              = 1;
      Z_BEST_COMPRESSION            = 9;
      Z_DEFAULT_COMPRESSION      = (-1);

      Z_FILTERED                                    = 1;
      Z_HUFFMAN_ONLY                        = 2;
      Z_DEFAULT_STRATEGY            = 0;

      Z_BINARY                                          = 0;
      Z_ASCII                                                = 1;
      Z_UNKNOWN                                          = 2;

      Z_DEFLATED                                    = 8;

      Z_NULL                                                = nil;

      ZLIBDLL                                                = 'zLib.dll';

{ ================================================================= }
function zlibVersion
                        : PChar;
                              cdecl;

{ ================================================================= }
function deflateInit(                                          var      strm            : z_stream;
                                                                                                                  level            : Integer)
                        : Integer;
//                              cdecl; external ZLIBDLL;

{ ================================================================= }
function deflate(                                                      var strm            : z_stream;
                                                                                                                  flush            : Integer)
                        : Integer;
                              cdecl;

{ ================================================================= }
function deflateEnd(                                          var strm            : z_stream)
                        : Integer;
                              cdecl;

{ ================================================================= }
function inflateInit(                                          var strm            : z_stream)
                        : Integer;

{ ================================================================= }
function inflate(                                                      var strm            : z_stream;
                                                                                                                  flush            : Integer)
                        : Integer;
                              cdecl;

{ ================================================================= }
function inflateEnd(                                          var strm            : z_stream)
                        : Integer;
                              cdecl;

{ ================================================================= }
function deflateInit2(                                    var strm                        : z_stream;
                                                                                                                  level                        : Integer;
                                                                                                                  method                  : Integer;
                                                                                                                  windowBits      : Integer;
                                                                                                                  memLevel            : Integer;
                                                                                                                  strategy            : Integer)
                        : Integer;

{ ================================================================= }
function deflateSetDictionary(            var strm                        : z_stream;
                                                                                                                  dictionary      : PChar;
                                                                                                                  dictLength      : uInt)
                        : Integer;
                              cdecl;

{ ================================================================= }
function deflateCopy(                                          var dest                        : z_stream;
                                                                                                const source                  : z_stream)
                        : Integer;
                              cdecl;

{ ================================================================= }
function deflateReset(                                    var strm                        : z_stream)
                        : Integer;
                              cdecl;

{ ================================================================= }
function deflateParams(                                    var strm                        : z_stream;
                                                                                                                  level                        : Integer;
                                                                                                                  strategy            : Integer)
                        : Integer;
                              cdecl;

{ ================================================================= }
function InflateInit2(                                    var strm                        : z_stream;
                                                                                                                  windowBits      : Integer)
                        : Integer;

{ ================================================================= }
function inflateSetDictionary(            var strm                        : z_stream;
                                                                                                                  dictionary      : PChar;
                                                                                                                  dictLength      : uInt)
                        : Integer;
                              cdecl;

{ ================================================================= }
function inflateSync(                                          var strm                        : z_stream)
                        : Integer;
                              cdecl;

{ ================================================================= }
function inflateReset(                                    var strm                        : z_stream)
                        : Integer;
                              cdecl;

{ ================================================================= }
function compress(                                                            dest                        : Pointer;
                                                                                                      var destLen                  : LongInt;
                                                                                                                  source                  : Pointer;
                                                                                                                  sourceLen            : ULONG)
                        : Integer;
                              cdecl;

{ ================================================================= }
function compress2(                                                            dest                        : Pointer;
                                                                                                      var destLen                  : LongInt;
                                                                                                                  source                  : Pointer;
                                                                                                                  sourceLen            : ULONG;
                                                                                                                  level                        : Integer)
                        : Integer;
                              cdecl;

{ ================================================================= }
function uncompress(                                                      dest                        : Pointer;
                                                                                                      var destLen                  : LongInt;
                                                                                                                  source                  : Pointer;
                                                                                                                  sourceLen            : LongInt)
                        : Integer;
                              cdecl;

{ ================================================================= }
function gzopen(                                                                  path                        : PChar;
                                                                                                                  mode                        : PChar)
                        : TGZFILE;
                              cdecl;

{ ================================================================= }
function gzdopen(                                                                  fd                              : Integer;
                                                                                                                  mode                        : PChar)
                        : TGZFILE;
                              cdecl;

{ ================================================================= }
function gzetparams(                                                      gzFile                  : TGZFILE;
                                                                                                                  level                        : Integer;
                                                                                                                  strategy            : Integer)
                        : Integer;
                              cdecl;

{ ================================================================= }
function gzread(                                                                  gzFile                  : TGZFILE;
                                                                                                                  buf                              : VOIDP;
                                                                                                                  len                              : uInt)
                        : Integer;
                              cdecl;

{ ================================================================= }
function gzwrite(                                                                  gzFile                  : TGZFILE;
                                                                                                                  buf                              : VOIDP;
                                                                                                                  len                              : uInt)
                        : Integer;
                              cdecl;

{ ================================================================= }
function gzputs(                                                                  gzFile                  : TGZFILE;
                                                                                                                  s                                    : PChar)
                        : Integer;
                              cdecl;

{ ================================================================= }
function gzgets(                                                                  gzFile                  : TGZFILE;
                                                                                                                  buf                              : PChar;
                                                                                                                  len                              : Integer)
                        : Integer;
                              cdecl;

{ ================================================================= }
function gzputc(                                                                  gzFile                  : TGZFILE;
                                                                                                                  c                                    : Integer)
                        : Integer;
                              cdecl;

{ ================================================================= }
function gzgetc(                                                                  gzFile                  : TGZFILE)
                        : Integer;
                              cdecl;

{ ================================================================= }
function gzflush(                                                                  gzFile                  : TGZFILE;
                                                                                                                  flush                        : Integer)
                        : Integer;
                              cdecl;

{ ================================================================= }
function gzseek(                                                                  gzFile                  : TGZFILE;
                                                                                                                  offset                  : z_off_t;
                                                                                                                  whence                  : Integer)
                        : Integer;
                              cdecl;

{ ================================================================= }
function gzrewind(                                                            gzFile                  : TGZFILE)
                        : Integer;
                              cdecl;

{ ================================================================= }
function gztell(                                                                  gzFile                  : TGZFILE)
                        : z_off_t;
                              cdecl;

{ ================================================================= }
function gzeof(                                                                        gzFile                  : TGZFILE)
                        : WordBool;
                              cdecl;

{ ================================================================= }
function gzclose(                                                                  gzFile                  : TGZFILE)
                        : Integer;
                              cdecl;

{ ================================================================= }
function gzerror(                                                                  gzFile                  : TGZFILE;
                                                                                                                  errnum                  : PInteger)
                        : PChar;
                              cdecl;

{ ================================================================= }
function adler32(                                                                  adler                        : ULONG;
                                                                                                                  buf                              : Pointer;
                                                                                                                  len                              : uInt)
                        : ULONG;
                              cdecl;

{ ================================================================= }
function crc32(                                                                        crc                              : ULONG;
                                                                                                                  buf                              : Pointer;
                                                                                                                  len                              : uInt)
                        : ULONG;
                              cdecl;

{ ================================================================= }
function deflateInit_(                                    var strm                        : z_stream;
                                                                                                                  level                        : Integer;
                                                                                                                  version                  : PChar;
                                                                                                                  stream_size      : Integer)
                        : Integer;
                              cdecl;

{ ================================================================= }
function inflateInit_(                                    var strm                        : z_stream;
                                                                                                                  version                  : PChar;
                                                                                                                  stream_size      : Integer)
                        : Integer;
                              cdecl;

{ ================================================================= }
function deflateInit2_(                                    var strm                        : z_stream;
                                                                                                                  level                        : Integer;
                                                                                                                  method                  : Integer;
                                                                                                                  windowBits      : Integer;
                                                                                                                  memLevel            : Integer;
                                                                                                                  strategy            : Integer;
                                                                                                                  version                  : PChar;
                                                                                                                  stream_size      : Integer)
                        : Integer;
                              cdecl;

{ ================================================================= }
function inflateInit2_(                                    var strm                        : z_stream;
                                                                                                                  windowBits      : Integer;
                                                                                                                  version                  : PChar;
                                                                                                                  stream_size      : Integer)
                        : Integer;
                              cdecl;

{ ================================================================= }
function zError(                                                                  err                              : Integer)
                        : PChar;
                              cdecl;

{ ================================================================= }
function inflateSyncPoint(                        var z                                    : z_stream)
                        : Integer;
                              cdecl;

{ ================================================================= }
function get_crc_table
                        : pcrc_table;
                              cdecl;

{ ================================================================= }
{ Functions added by Lasse Vågsæther Karlsen for easier programming }

function z_check_version
                        : Integer;
      { Performs version check on the dll }
const
      zVersionOK            = 0;      // DLL version is the same version as this unit supports
      zVersionOlder      = 1;      // The DLL is an older version than
      zVersionNewer      = 2;      // The DLL is newer than what this unit supports. The program should probably not attempt
                                                                  // to use the zLib.dll functions

procedure z_init_zstream(                              var strm                        : z_stream);
      { Clears the z_stream variable }

implementation

uses
      SysUtils;

{ Support functions by LVK }

function z_check_version: Integer;
begin
      Result:=StrComp(ZLIB_VERSION,zLibVersion);
      if Result<0 then
            Result:=zVersionNewer
      else if Result>0 then
            Result:=zVersionOlder
      else
            Result:=zVersionOK;
end;

procedure z_init_zstream(var strm: z_stream);
begin
      with strm do begin
            next_in:=nil;
            avail_in:=0;
            total_in:=0;
            next_out:=nil;
            avail_out:=0;
            total_out:=0;
            msg:=nil;
            state:=nil;
            zalloc:=nil;
            zfree:=nil;
            opaque:=nil;
            data_type:=0;
            adler:=0;
            reserved:=0;
      end;
end;

{ Import functions from zLib.dll }

function zlibVersion;                                                            external ZLIBDLL;
function deflate;                                                                        external ZLIBDLL;
function deflateEnd;                                                            external ZLIBDLL;
function inflate;                                                                        external ZLIBDLL;
function inflateEnd;                                                            external ZLIBDLL;
function deflateSetDictionary;                              external ZLIBDLL;
function deflateCopy;                                                            external ZLIBDLL;
function deflateReset;                                                      external ZLIBDLL;
function deflateParams;                                                      external ZLIBDLL;
function inflateSetDictionary;                              external ZLIBDLL;
function inflateSync;                                                            external ZLIBDLL;
function inflateReset;                                                      external ZLIBDLL;
function compress;                                                                  external ZLIBDLL;
function compress2;                                                                  external ZLIBDLL;
function uncompress;                                                            external ZLIBDLL;
function gzopen;                                                                        external ZLIBDLL;
function gzdopen;                                                                        external ZLIBDLL;
function gzetparams;                                                            external ZLIBDLL;
function gzread;                                                                        external ZLIBDLL;
function gzwrite;                                                                        external ZLIBDLL;
function gzputs;                                                                        external ZLIBDLL;
function gzgets;                                                                        external ZLIBDLL;
function gzputc;                                                                        external ZLIBDLL;
function gzgetc;                                                                        external ZLIBDLL;
function gzflush;                                                                        external ZLIBDLL;
function gzseek;                                                                        external ZLIBDLL;
function gzrewind;                                                                  external ZLIBDLL;
function gztell;                                                                        external ZLIBDLL;
function gzeof;                                                                              external ZLIBDLL;
function gzclose;                                                                        external ZLIBDLL;
function gzerror;                                                                        external ZLIBDLL;
function adler32;                                                                        external ZLIBDLL;
function crc32;                                                                              external ZLIBDLL;
function deflateInit_;                                                      external ZLIBDLL;
function inflateInit_;                                                      external ZLIBDLL;
function deflateInit2_;                                                      external ZLIBDLL;
function inflateInit2_;                                                      external ZLIBDLL;
function zError;                                                                        external ZLIBDLL;
function inflateSyncPoint;                                          external ZLIBDLL;
function get_crc_table;                                                      external ZLIBDLL;

{ Support functions }

function deflateInit(var strm: z_stream; level : Integer) : Integer;
begin
      result:=deflateInit_(strm, level, ZLIB_VERSION, SizeOf(z_stream));
end;

function inflateInit(var strm: z_stream) : Integer;
begin
      result:=inflateInit_(strm, ZLIB_VERSION, SizeOf(z_stream));
end;

function deflateInit2(var strm: z_stream; level: Integer; method: Integer; windowBits: Integer;
      memLevel: Integer; strategy: Integer) : Integer;
begin
      result:=deflateInit2_(strm, level, method, windowBits, memLevel, strategy, ZLIB_VERSION, SizeOf(z_stream));
end;

function inflateInit2(var strm: z_stream; windowBits: Integer) : Integer;
begin
      result:=inflateInit2_(strm, windowBits, ZLIB_VERSION, SizeOf(z_stream));
end;

end.


/// John
0
 

Author Comment

by:ms99y
ID: 1361966

Thank you very much for your effort and help..(The Last One...)

0
 

Author Comment

by:ms99y
ID: 1361967

Hi John,

function compress( dest : Pointer;
   var destLen : LongInt;
   source : Pointer;
   sourceLen : ULONG)
   : Integer;
   cdecl;
   {

I tried to this compress function in a loop which has the following steps

  BlockRead to fill the buffer
  call to the compress,

the first call the compress function Ok but in the second call it gives an error. But I could not understand the reason.(May be I can use MemoryMapFile and call the compress function once instead of several times in a loop but anyway I want to know what is wrong with the loop above)

Can you give me a code snippet about this function call. Thanx


Later...





0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now