I want to decompress a LZ4 raw stream inside binary data. I read the documentation and came up with the following function. According to the code, it should keep decompressing until it runs out of compressed data, but it doesn't. What am I doing wrong? Also, I want to add a writeln for the compressed size.
function Try(inStream: TMemorySTream): Boolean;
var
i, j, k: int64;
orig_buff: Pansichar;
compressed_buff: Pansichar;
lz4StreamDecode: PLZ4_streamDecode_t;
begin
result := false;
lz4StreamDecode := LZ4_createStreamDecode();
k := 0;
while True do
begin
orig_buff := allocmem(100);
compressed_buff := allocmem(4064);
j := inStream.Read(orig_buff^, 100);
i := lz4.LZ4_decompress_safe_continue(lz4StreamDecode, orig_buff,
compressed_buff, j, 4064);
Freemem(orig_buff);
Freemem(compressed_buff);
if i <= 0 then
break;
inc(k);
end;
if k <> 0 then
result := True;
LZ4_freeStreamDecode(lz4StreamDecode);
end;
EDIT 1: Removed the part where it freed the output buffer (temp buffer), still the problem remains, instead of decompressing entire stream and then quitting, its just exiting the loop beforehand.
function Try(inStream: TMemorySTream): Boolean;
var
i, j, k: int64;
orig_buff: Pansichar;
compressed_buff: Pansichar;
lz4StreamDecode: PLZ4_streamDecode_t;
begin
result := false;
lz4StreamDecode := LZ4_createStreamDecode();
compressed_buff := allocmem(64 * 1024 * 1024);
k := 0;
while True do
begin
orig_buff := allocmem(100);
j := inStream.Read(orig_buff^, 100);
i := lz4.LZ4_decompress_safe_continue(lz4StreamDecode, orig_buff,
compressed_buff, j, (64 * 1024 * 1024));
Freemem(orig_buff);
if i <= 0 then
break;
inc(k);
end;
if k <> 0 then
result := True;
Freemem(compressed_buff);
LZ4_freeStreamDecode(lz4StreamDecode);
end;