Skip to content

Commit 48c3261

Browse files
committed
Fix compress/decompress bytes + test
1 parent 88b0d18 commit 48c3261

File tree

3 files changed

+60
-17
lines changed

3 files changed

+60
-17
lines changed

.github/workflows/archive.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ jobs:
5050
branch=${{ github.event.workflow_run.head_branch }}
5151
sha=${{ github.event.workflow_run.head_sha }}
5252
commit=${sha::10}
53-
echo "$date_year/$date_month_day | $branch | [$commit](https://github.com/Villavu/Simba/commit/$commit) | [Win32](/$date_year/$date_month_day%20$branch%20$commit/Win32.zip?raw=true)<br>[Win64](/$date_year/$date_month_day%20$branch%20$commit/Win64.zip?raw=true)<br>[Win64&nbsp;Debug](/$date_year/$date_month_day%20$branch%20$commit/Win64%20DebugInfo.zip?raw=true)<br>[Mac](/$date_year/$date_month_day%20$branch%20$commit/Mac.zip?raw=true)<br>[Mac Arm](/$date_year/$date_month_day%20$branch%20$commit/Mac%20Arm.zip?raw=true)<br>[Linux64](/$date_year/$date_month_day%20$branch%20$commit/Linux64.zip?raw=true)" > row.txt
53+
echo "$date_year/$date_month_day | $branch | [$commit](https://github.com/Villavu/Simba/commit/$commit) | [Win32](/$date_year/$date_month_day%20$branch%20$commit/Win32.zip?raw=true) - [Win64](/$date_year/$date_month_day%20$branch%20$commit/Win64.zip?raw=true) - [Win64&nbsp;Debug](/$date_year/$date_month_day%20$branch%20$commit/Win64%20DebugInfo.zip?raw=true)<br>[Mac](/$date_year/$date_month_day%20$branch%20$commit/Mac.zip?raw=true) - [Mac Arm](/$date_year/$date_month_day%20$branch%20$commit/Mac%20Arm.zip?raw=true)<br>[Linux64](/$date_year/$date_month_day%20$branch%20$commit/Linux64.zip?raw=true)" > row.txt
5454
5555
cd Simba-Build-Archive
5656
mkdir -p "$date_year/$date_month_day $branch $commit"

Source/simba.compress.pas

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ interface
1414
simba.base, simba.encoding;
1515

1616
// TByteArray
17-
function CompressBytes(Data: TByteArray): TByteArray;
18-
function DecompressBytes(Data: TByteArray): TByteArray;
17+
function CompressBytes(const Data: TByteArray): TByteArray;
18+
function DecompressBytes(const Data: TByteArray): TByteArray;
1919

2020
// String
2121
function CompressString(S: String; Encoding: EBaseEncoding = EBaseEncoding.b64): String;
@@ -26,47 +26,56 @@ implementation
2626
uses
2727
basenenc_simba, ZStream;
2828

29-
function CompressBytes(Data: TByteArray): TByteArray;
29+
function CompressBytes(const Data: TByteArray): TByteArray;
3030
var
31-
InStream: Tcompressionstream;
32-
OutStream: TBytesStream;
31+
InStream: TCompressionstream;
32+
OutStream: TMemoryStream;
3333
begin
34-
OutStream := TBytesStream.Create();
34+
Result := [];
35+
36+
OutStream := TMemoryStream.Create();
3537
InStream := TCompressionStream.Create(clDefault, OutStream);
3638
try
3739
InStream.Write(Data[0], Length(Data));
3840
InStream.Flush();
3941

40-
Result := OutStream.Bytes;
41-
SetLength(Result, OutStream.Size);
42+
SetLength(Result, OutStream.Position);
43+
if (Length(Result) > 0) then
44+
Move(OutStream.Memory^, Result[0], Length(Result));
4245
finally
4346
InStream.Free();
4447
OutStream.Free();
4548
end;
4649
end;
4750

48-
function DecompressBytes(Data: TByteArray): TByteArray;
51+
function DecompressBytes(const Data: TByteArray): TByteArray;
4952
var
5053
InStream: TBytesStream;
5154
OutStream: Tdecompressionstream;
52-
Count, Len: Integer;
55+
ReadCount, TotalCount: Integer;
56+
Chunk: array[0..2048-1] of Byte;
5357
begin
54-
SetLength(Result, 4096);
55-
Len := 0;
58+
TotalCount := 0;
5659

5760
InStream := TBytesStream.Create(Data);
5861
OutStream := TDeCompressionStream.Create(InStream);
5962
try
6063
repeat
61-
Count := OutStream.Read(Result[Len], Length(Result) - Len);
62-
Len := Len + Count;
63-
until (Count = 0);
64+
ReadCount := OutStream.Read(Chunk[0], Length(Chunk));
65+
if (ReadCount > 0) then
66+
begin
67+
if (TotalCount + ReadCount > High(Result)) then
68+
SetLength(Result, 4 + (High(Result) * 2) + ReadCount);
69+
Move(Chunk[0], Result[TotalCount], ReadCount);
70+
Inc(TotalCount, ReadCount);
71+
end;
72+
until (ReadCount = 0);
6473
finally
6574
InStream.Free();
6675
OutStream.Free();
6776
end;
6877

69-
SetLength(Result, Len);
78+
SetLength(Result, TotalCount);
7079
end;
7180

7281
function CompressString(S: String; Encoding: EBaseEncoding): String;

Tests/compress.simba

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{$assertions on}
2+
3+
function BytesOf(s: String): TByteArray;
4+
begin
5+
SetLength(Result, Length(s));
6+
Move(s[1], Result[0], Length(s));
7+
end;
8+
9+
function StringOf(Bytes: TByteArray): String;
10+
begin
11+
SetLength(Result, Length(Bytes));
12+
Move(Bytes[0], Result[1], Length(Bytes));
13+
end;
14+
15+
var
16+
s: String;
17+
compressed,decompressed: TByteArray;
18+
begin
19+
s := StringOfChar(' ', 1234);
20+
s[2] := 'z';
21+
s[4] := 'z';
22+
s[6] := '0';
23+
s[Length(s) div 2] := '0';
24+
s += 'Foo Woo ';
25+
26+
compressed := CompressBytes(BytesOf(s));
27+
decompressed := DecompressBytes(compressed);
28+
Assert(StringOf(decompressed) = s);
29+
30+
// test a just a single byte input
31+
compressed := CompressBytes(TByteArray([123]));
32+
decompressed := DecompressBytes(compressed);
33+
Assert((Length(decompressed) = 1) and (decompressed[0] = 123));
34+
end.

0 commit comments

Comments
 (0)