From 8306be3f98aec123096da9e894725be88ae81a58 Mon Sep 17 00:00:00 2001 From: Torwent Date: Mon, 27 Jul 2026 10:35:09 +0100 Subject: [PATCH] feat: add bzip2 decompression --- .../script/imports/simba.import_encoding.pas | 2 +- Source/simba.compress.pas | 37 ++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Source/script/imports/simba.import_encoding.pas b/Source/script/imports/simba.import_encoding.pas index 6b487cca3..740400515 100644 --- a/Source/script/imports/simba.import_encoding.pas +++ b/Source/script/imports/simba.import_encoding.pas @@ -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); diff --git a/Source/simba.compress.pas b/Source/simba.compress.pas index deaa40aed..b74e43d8f 100644 --- a/Source/simba.compress.pas +++ b/Source/simba.compress.pas @@ -20,7 +20,8 @@ interface ZLIB, SYNLZ, GZ, - RLE + RLE, + BZIP2 ); {$POP} @@ -42,7 +43,8 @@ implementation ZStream, castle_gz, mormot2_synlz, - mormot2_rle; + mormot2_rle, + bzip2stream; type TOutStream = class(TStream) @@ -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; @@ -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 @@ -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);