Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Source/script/imports/simba.import_encoding.pas
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ procedure ImportEncoding(Script: TSimbaScript);
addGlobalFunc('function Hash32(Data: Pointer; Len: Int32; Seed: UInt32 = 0): UInt32; overload', @_LapeHash32);
addGlobalFunc('function Hash32(S: String; Seed: UInt32 = 0): UInt32; overload', @_LapeHash32String);

addGlobalType('enum(ZLIB, SYNLZ, GZ, RLE)', 'ECompressAlgo');
addGlobalType('enum(ZLIB, SYNLZ, GZ, RLE, BZIP2)', 'ECompressAlgo');

addGlobalFunc('procedure CompressData(Algo: ECompressAlgo; InData: Pointer; InSize: Int64; var OutData: Pointer; out OutSize: Int64; Truncate: Boolean = True);', @_LapeCompressData);
addGlobalFunc('function CompressBytes(Algo: ECompressAlgo; Bytes: TByteArray): TByteArray', @_LapeCompressBytes);
Expand Down
37 changes: 35 additions & 2 deletions Source/simba.compress.pas
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ interface
ZLIB,
SYNLZ,
GZ,
RLE
RLE,
BZIP2
);
{$POP}

Expand All @@ -42,7 +43,8 @@ implementation
ZStream,
castle_gz,
mormot2_synlz,
mormot2_rle;
mormot2_rle,
bzip2stream;

type
TOutStream = class(TStream)
Expand Down Expand Up @@ -178,7 +180,9 @@ procedure CompressData(Algo: ESimbaCompressAlgo; InData: PByte; InSize: Int64; v
ESimbaCompressAlgo.SYNLZ: CompressWithSynLZ();
ESimbaCompressAlgo.GZ: CompressWithGZ();
ESimbaCompressAlgo.RLE: CompressWithRle();
ESimbaCompressAlgo.BZIP2: SimbaException('BZip2 compression is not supported.');
end;

if Truncate then
ReAllocMem(OutData, OutSize);
end;
Expand Down Expand Up @@ -269,6 +273,34 @@ procedure DecompressData(Algo: ESimbaCompressAlgo; InData: PByte; InSize: Int64;
end;
end;

procedure DecompressWithBZip2;
var
InStream: TMemoryStream;
OutStream: TOutStream;
BZStream: TDecompressBzip2Stream;
Count: Integer;
Chunk: array[0..4095] of Byte;
begin
InStream := TMemoryStream.Create();
InStream.Write(InData^, InSize);
InStream.Position := 0;
OutStream := TOutStream.Create(OutData);
BZStream := TDecompressBzip2Stream.Create(InStream);
try
repeat
Count := BZStream.Read(Chunk[0], Length(Chunk));
if (Count > 0) then
OutStream.Write(Chunk[0], Count);
until (Count = 0);
OutSize := OutStream.Position;
OutData := OutStream.Data;
finally
InStream.Free();
OutStream.Free();
BZStream.Free();
end;
end;

begin
OutSize := 0;
if (InSize < 0) or (InSize > High(Int32)) then
Expand All @@ -279,6 +311,7 @@ procedure DecompressData(Algo: ESimbaCompressAlgo; InData: PByte; InSize: Int64;
ESimbaCompressAlgo.SYNLZ: DecompressWithSynLZ();
ESimbaCompressAlgo.GZ: DecompressWithGZ();
ESimbaCompressAlgo.RLE: DecompressWithRLE();
ESimbaCompressAlgo.BZIP2: DecompressWithBZip2();
end;
if Truncate then
ReAllocMem(OutData, OutSize);
Expand Down
Loading