Using miniLZO in Delphi

How to use the miniLZO library written in ANSI C in your delphi application without a dll

Introduction

LZO is a lossless compression algorithm that offers EXTREMELY fast decompression speed while giving you a decent compression ratio. I've read that LZO is twice as fast as ZLib at decompressing while only have about 5% less compression ratio.



miniLZO

Is a library from the author of LZO that includes LZO1X_1 compression and LZO1x decompression. I've compiled it to an .obj file that can be linked into a delphi unit using the {$LINK}/{$L} compiler directive. Below you'll find the complete unit. miniLZO is distributed under the GNU Public License, so naturally this code is GNU licensed too. Note, for detailed instructions on how to USE these functions please visit the official LZO homepage at http://www.oberhumer.com/opensource/lzo/.



//------------------------- BEGIN UNIT -------------------------//

unit Lzo;



interface



// "C" routines needed by the linked LZO OBJ file

function _memcmp (s1,s2: Pointer; numBytes: LongWord): integer; cdecl;

procedure _memcpy (s1, s2: Pointer; n: Integer); cdecl;

procedure _memmove(dstP, srcP: pointer; numBytes: LongWord); cdecl;

procedure _memset (s: Pointer; c: Byte; n: Integer); cdecl;



{$LINK 'minilzo.obj'}



function lzo1x_1_compress(const Source: Pointer; SourceLength: LongWord; Dest: Pointer; var DestLength: LongWord; WorkMem: Pointer): integer; stdcall; external;

function lzo1x_decompress(const Source: Pointer; SourceLength: LongWord; Dest: Pointer; var DestLength: LongWord; WorkMem: Pointer (* NOT USED! *)): Integer; stdcall; external;

function lzo1x_decompress_safe(const Source: Pointer; SourceLength: LongWord; Dest: Pointer; var DestLength: LongWord; WorkMem: Pointer (* NOT USED! *)): Integer; stdcall; external;


function lzo_adler32(Adler: LongWord; const Buf: Pointer; Len: LongWord): LongWord; stdcall; external;

function lzo_version: word; stdcall; external;

function lzo_version_string: PChar; stdcall; external;

function lzo_version_date: PChar; stdcall; external;



implementation



procedure _memset(s: Pointer; c: Byte; n: Integer); cdecl;

begin

  FillChar(s^, n, c);

end;



procedure _memcpy(s1, s2: Pointer; n: Integer); cdecl;

begin

  Move(s2^, s1^, n);

end;



function _memcmp (s1, s2: Pointer; numBytes: LongWord): integer; cdecl;

var

  i: integer;

  p1, p2: ^byte;

begin

  p1 := s1;

  p2 := s2;

  for i := 0 to numBytes -1 do

  begin

    if p1^ <> p2^ then

    begin

      if p1^ < p2^ then

        Result := -1

      else

        Result := 1;

      exit;

    end;

    inc(p1);

    inc(p2);

  end;

  Result := 0;

end;



procedure _memmove(dstP, srcP: pointer; numBytes: LongWord); cdecl;

begin

  Move(srcP^, dstP^, numBytes);

  FreeMem(srcP, numBytes);

end;



end.



//------------------------- END UNIT -------------------------//





The C miniLZO source, minilzo.obj and this unit can be downloade from here.



That's it! I appologize for my somtimes bad english and writing style.

 

Share this article!

Follow us!

Find more helpful articles: