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