2
0

Quick.Compression.LZO.pas 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. { ***************************************************************************
  2. Copyright (c) 2016-2019 Kike Pérez
  3. Unit : Quick.Compression.LZO
  4. Description : LZO Compressor
  5. Author : Kike Pérez
  6. Version : 1.8
  7. Created : 15/09/2019
  8. Modified : 22/09/2019
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.Compression.LZO;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. SysUtils;
  26. const
  27. {$IFDEF MSWINDOWS}
  28. LIBPATH = '.\lzo.dll';
  29. {$ELSE}
  30. LIBPATH = './lzo.so';
  31. {$ENDIF}
  32. type
  33. TLZOCompressor = class
  34. private
  35. fWorkMemory : Pointer;
  36. public
  37. constructor Create;
  38. destructor Destroy; override;
  39. function Version : string;
  40. function VersionDate : string;
  41. function Compress(const aUncompressed : string) : string;
  42. function Decompress(const aCompressed : string) : string;
  43. end;
  44. ELZOCompressor = class(Exception);
  45. implementation
  46. function __lzo_init3 : Integer; stdcall; external LIBPATH;
  47. function lzo_version_string : PChar; stdcall; external LIBPATH;
  48. function lzo_version_date : PChar; stdcall; external LIBPATH;
  49. function lzo1x_1_compress(src : Pointer; src_len : LongWord; dest : Pointer; var dest_len : LongWord; wrkmem : Pointer) : Integer; stdcall; external LIBPATH;
  50. function lzo1x_decompress(src : Pointer; src_len : LongWord; dest : Pointer; var dest_len : LongWord; wrkmem : Pointer) : Integer; stdcall; external LIBPATH;
  51. { TLZOCompressor }
  52. function TLZOCompressor.Compress(const aUncompressed: string): string;
  53. var
  54. srclen : Integer;
  55. outlen : LongWord;
  56. uncompressed : TBytes;
  57. compressed : TBytes;
  58. byteslen : array[1.. sizeof(integer)] of byte;
  59. begin
  60. outlen := 0;
  61. try
  62. SetLength(compressed, Round(aUncompressed.Length + aUncompressed.Length / 64 + 16 + 3 + 4));
  63. uncompressed := TEncoding.UTF8.GetBytes(aUncompressed);
  64. lzo1x_1_compress(uncompressed, High(uncompressed)+1, compressed, outlen, fWorkMemory);
  65. Finalize(uncompressed);
  66. srclen := aUncompressed.Length;
  67. Move(srclen,byteslen[1], SizeOf(Integer));
  68. SetLength(compressed,outlen + 4);
  69. Move(byteslen,compressed[outlen],4);
  70. Result := TEncoding.ANSI.GetString(compressed,0,outlen + 4);
  71. except
  72. on E : Exception do raise ELZOCompressor.CreateFmt('LZO Compression error: %s',[e.Message]);
  73. end;
  74. end;
  75. constructor TLZOCompressor.Create;
  76. begin
  77. if __lzo_init3 <> 0 then raise Exception.Create('Initialization LZO-Compressor failed');
  78. GetMem(fWorkMemory,16384 * 4);
  79. end;
  80. function TLZOCompressor.Decompress(const aCompressed: string): string;
  81. var
  82. srclen : LongWord;
  83. dstlen : LongWord;
  84. uncompressed : TBytes;
  85. compressed : TBytes;
  86. i : Integer;
  87. begin
  88. try
  89. compressed := TEncoding.ANSI.GetBytes(aCompressed);
  90. //get src length
  91. srclen := PLongInt(@compressed[High(compressed)-3])^;
  92. SetLength(uncompressed,srclen);
  93. dstlen := 0;
  94. lzo1x_decompress(compressed, High(compressed) - 4, uncompressed, dstlen, fWorkMemory);
  95. Result := TEncoding.UTF8.GetString(uncompressed,0,dstlen);
  96. except
  97. on E : Exception do raise ELZOCompressor.CreateFmt('LZO Descompression error: %s',[e.Message]);
  98. end;
  99. end;
  100. destructor TLZOCompressor.Destroy;
  101. begin
  102. FreeMem(fWorkMemory);
  103. inherited;
  104. end;
  105. function TLZOCompressor.Version: string;
  106. begin
  107. Result := string(lzo_version_string);
  108. end;
  109. function TLZOCompressor.VersionDate: string;
  110. begin
  111. Result := string(lzo_version_date^);
  112. end;
  113. end.