Skip to content

Commit b04895a

Browse files
committed
add gz compress/decompress
1 parent 1698e46 commit b04895a

File tree

5 files changed

+79
-25
lines changed

5 files changed

+79
-25
lines changed

Source/script/imports/simba.import_encoding.pas

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ procedure _LapeBaseDecode(const Params: PParamArray; const Result: Pointer); LAP
144144
ECompressAlgo
145145
-------------
146146
```
147-
type ECompressAlgo = enum(ZLIB, SYNLZ);
147+
type ECompressAlgo = enum(ZLIB, SYNLZ, GZ);
148148
```
149149
150150
```{note}
@@ -229,20 +229,16 @@ procedure _LapeDecompressString(const Params: PParamArray; const Result: Pointer
229229
------------------
230230
```
231231
function HOTPCalculateToken(const Secret: String; const Counter: Integer): Integer;
232+
function TOTPCalculateToken(const Secret: String): Integer;
232233
```
234+
235+
HOTP and TOTP One-time password algorithms. Compatible with Google Authenticator.
233236
*)
234237
procedure _LapeHOTPCalculateToken(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
235238
begin
236239
PInteger(Result)^ := HOTPCalculateToken(PString(Params^[0])^, PInteger(Params^[1])^);
237240
end;
238241

239-
(*
240-
TOTPCalculateToken
241-
------------------
242-
```
243-
function TOTPCalculateToken(const Secret: String): Integer;
244-
```
245-
*)
246242
procedure _LapeTOTPCalculateToken(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
247243
begin
248244
PInteger(Result)^ := TOTPCalculateToken(PString(Params^[0])^);
@@ -400,7 +396,7 @@ procedure ImportEncoding(Script: TSimbaScript);
400396
addGlobalFunc('function Hash32(Data: Pointer; Len: Int32; Seed: UInt32 = 0): UInt32; overload', @_LapeHash32);
401397
addGlobalFunc('function Hash32(S: String; Seed: UInt32 = 0): UInt32; overload', @_LapeHash32String);
402398

403-
addGlobalType('enum(ZLIB, SYNLZ)', 'ECompressAlgo');
399+
addGlobalType('enum(ZLIB, SYNLZ, GZ)', 'ECompressAlgo');
404400

405401
addGlobalFunc('procedure CompressData(Algo: ECompressAlgo; InData: Pointer; InSize: Int64; out OutData: Pointer; out OutSize: Int64);', @_LapeCompressData);
406402
addGlobalFunc('function CompressBytes(Algo: ECompressAlgo; Bytes: TByteArray): TByteArray', @_LapeCompressBytes);

Source/simba.compress.pas

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ interface
1818
type
1919
ESimbaCompressAlgo = (
2020
ZLIB,
21-
SYNLZ
21+
SYNLZ,
22+
GZ
2223
);
2324
{$POP}
2425

@@ -35,6 +36,7 @@ implementation
3536

3637
uses
3738
ZStream,
39+
gz,
3840
mormot2_synlz,
3941
mormot2_rle;
4042

@@ -47,6 +49,7 @@ procedure CompressData(Algo: ESimbaCompressAlgo; InData: PByte; InSize: Int64; o
4749
begin
4850
OutStream := TMemoryStream.Create();
4951
InStream := TCompressionStream.Create(clDefault, OutStream);
52+
InStream.SourceOwner := False;
5053
try
5154
InStream.Write(InData^, InSize);
5255
InStream.Flush();
@@ -89,13 +92,38 @@ procedure CompressData(Algo: ESimbaCompressAlgo; InData: PByte; InSize: Int64; o
8992
OutData := ReAllocMem(OutData, OutSize);
9093
end;
9194

95+
procedure CompressWithGz;
96+
var
97+
InStream: TGZFileStream;
98+
OutStream: TMemoryStream;
99+
begin
100+
OutStream := TMemoryStream.Create();
101+
try
102+
InStream := TGZFileStream.Create(OutStream, True);
103+
InStream.SourceOwner := False;
104+
try
105+
InStream.Write(InData^, InSize);
106+
finally
107+
InStream.Free(); // flushes on free
108+
end;
109+
110+
OutSize := OutStream.Position;
111+
OutData := GetMem(OutSize);
112+
113+
Move(OutStream.Memory^, OutData^, OutSize);
114+
finally
115+
OutStream.Free();
116+
end;
117+
end;
118+
92119
begin
93120
OutSize := 0;
94121
OutData := nil;
95122

96123
case Algo of
97124
ESimbaCompressAlgo.ZLIB: CompressWithZLib();
98125
ESimbaCompressAlgo.SYNLZ: CompressWithSynLZ();
126+
ESimbaCompressAlgo.GZ: CompressWithGZ();
99127
end;
100128
end;
101129

@@ -113,6 +141,7 @@ procedure DecompressData(Algo: ESimbaCompressAlgo; InData: PByte; InSize: Int64;
113141
InStream.Position := 0;
114142
OutStream := TMemoryStream.Create();
115143
DecompressStream := TDeCompressionStream.Create(InStream);
144+
DecompressStream.SourceOwner := False;
116145
try
117146
repeat
118147
Count := DecompressStream.Read(Chunk[0], Length(Chunk));
@@ -156,13 +185,44 @@ procedure DecompressData(Algo: ESimbaCompressAlgo; InData: PByte; InSize: Int64;
156185
end;
157186
end;
158187

188+
procedure DecompressWithGZ;
189+
var
190+
InStream, OutStream: TMemoryStream;
191+
GzStream: TGZFileStream;
192+
Count: Integer;
193+
Chunk: array[0..4095] of Byte;
194+
begin
195+
InStream := TMemoryStream.Create();
196+
InStream.Write(InData^, InSize);
197+
InStream.Position := 0;
198+
OutStream := TMemoryStream.Create();
199+
GzStream := TGZFileStream.Create(InStream, False);
200+
GzStream.SourceOwner := False;
201+
try
202+
repeat
203+
Count := GzStream.Read(Chunk[0], Length(Chunk));
204+
if (Count > 0) then
205+
OutStream.Write(Chunk[0], Count);
206+
until (Count = 0);
207+
OutSize := OutStream.Position;
208+
OutData := GetMem(OutSize);
209+
210+
Move(OutStream.Memory^, OutData^, OutSize);
211+
finally
212+
InStream.Free();
213+
OutStream.Free();
214+
GzStream.Free();
215+
end;
216+
end;
217+
159218
begin
160219
OutData := nil;
161220
OutSize := 0;
162221

163222
case Algo of
164223
ESimbaCompressAlgo.ZLIB: DecompressWithZLib();
165224
ESimbaCompressAlgo.SYNLZ: DecompressWithSynLZ();
225+
ESimbaCompressAlgo.GZ: DecompressWithGZ();
166226
end;
167227
end;
168228

Source/simba.encoding.pas

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ interface
1313
Classes, SysUtils,
1414
simba.base;
1515

16-
{$scopedenums on}
16+
{$PUSH}
17+
{$SCOPEDENUMS ON}
1718
type
1819
EBaseEncoding = (b64URL, b64, b32, b32Hex, b16);
19-
{$scopedenums off}
20+
{$POP}
2021

2122
function BaseEncode(Encoding: EBaseEncoding; const Data: String): String;
2223
function BaseDecode(Encoding: EBaseEncoding; const Data: String): String;

Tests/compress.simba

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,31 +48,28 @@ end;
4848

4949
var
5050
testData: String;
51+
algo: ECompressAlgo;
52+
encoding: EBaseEncoding;
5153
begin
5254
// test with some binary data, where rle might be used
5355
testData := '0000000000000000011111111111111111111111111' * 1234;
5456

5557
testD(ECompressAlgo.ZLIB, @testData[1], Length(testData));
5658
testD(ECompressAlgo.SYNLZ, @testData[1], Length(testData));
59+
testD(ECompressAlgo.GZ, @testData[1], Length(testData));
5760

5861
// non binary data
5962
testData := 'ABCDEF111222333000' * 1234;
6063

6164
testD(ECompressAlgo.ZLIB, @testData[1], Length(testData));
6265
testD(ECompressAlgo.SYNLZ, @testData[1], Length(testData));
66+
testD(ECompressAlgo.GZ, @testData[1], Length(testData));
6367

6468
testB(ECompressAlgo.ZLIB);
6569
testB(ECompressAlgo.SYNLZ);
70+
testB(ECompressAlgo.GZ);
6671

67-
testS(ECompressAlgo.SYNLZ, EBaseEncoding.b16);
68-
testS(ECompressAlgo.SYNLZ, EBaseEncoding.b32);
69-
testS(ECompressAlgo.SYNLZ, EBaseEncoding.b32Hex);
70-
testS(ECompressAlgo.SYNLZ, EBaseEncoding.b64);
71-
testS(ECompressAlgo.SYNLZ, EBaseEncoding.b64URL);
72-
73-
testS(ECompressAlgo.ZLIB, EBaseEncoding.b16);
74-
testS(ECompressAlgo.ZLIB, EBaseEncoding.b32);
75-
testS(ECompressAlgo.ZLIB, EBaseEncoding.b32Hex);
76-
testS(ECompressAlgo.ZLIB, EBaseEncoding.b64);
77-
testS(ECompressAlgo.ZLIB, EBaseEncoding.b64URL);
72+
for algo in ECompressAlgo do
73+
for encoding in EBaseEncoding do
74+
testS(algo, encoding);
7875
end.

Third-Party/gz.pas

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,7 @@ destructor TGZFileStream.Destroy;
10871087
inherited Destroy();
10881088
end;
10891089

1090-
function TGZFileStream.Read(Var Buffer; Count : longint): longint;
1090+
function TGZFileStream.Read(var Buffer; Count: LongInt): LongInt;
10911091
begin
10921092
If FWriteMode then
10931093
Raise ezliberror.create(SWriteOnlyStream);
@@ -1096,7 +1096,7 @@ function TGZFileStream.Read(Var Buffer; Count : longint): longint;
10961096
raise EZlibError.Create('Gzip decompression error: ' + gzerror(FFile));
10971097
end;
10981098

1099-
function TGZFileStream.Write(const Buffer; Count: Longint): Longint;
1099+
function TGZFileStream.Write(const Buffer; Count: LongInt): Longint;
11001100
begin
11011101
If not FWriteMode then
11021102
Raise EzlibError.Create(SReadonlyStream);

0 commit comments

Comments
 (0)