Recent

Author Topic: Infernal error when decompressing GZIP stream  (Read 250 times)

hedgehog

  • Full Member
  • ***
  • Posts: 124
Infernal error when decompressing GZIP stream
« on: May 18, 2026, 11:17:15 pm »
Hi!

I've always decompressed compressed streams using the PASZLIB.
But it didn't work for this file.

I tried to use TDecompressionStream... EDecompressionError!

Finally, I tried using the ZFlate library (author: Fibonacci).

In the first case (ZFlate1 button), the function returns the correct result, the size of the decompressed data is 32768 bytes.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.btnZFlate1Click(Sender: TObject);
  2. var
  3.   bs: TBytesStream;
  4.   byOut: TBytes;
  5. begin
  6.     bs:= TBytesStream.Create;
  7.     bs.LoadFromFile('myfile1.gz');
  8.     byOut:= zdecompress(bs.Bytes);
  9.     Memo1.Lines.Add('Size of uncompressed data: '+IntToStr(Length(byOut))); // 32768 <-- OK!
  10.     bs.Free;
  11. end;
I noticed something strange here:
Code: Pascal  [Select][+][-]
  1. bs.size =  20086
  2. Length(bs.Bytes = 20480
Shouldn't the sizes match?

In the second case (the ZFlate2 button), the function returns an incorrect result, the size of the decompressed data is 65536 bytes. That is, double the data!

Code: Pascal  [Select][+][-]
  1. procedure TForm1.btnZFlate2Click(Sender: TObject);
  2. var
  3.   ms: TMemoryStream;
  4.   byIn, byOut: TBytes;
  5. begin
  6.     ms:= TMemoryStream.Create;
  7.     ms.LoadFromFile('myfile1.gz');
  8.     SetLength(byIn, ms.Size);
  9.     ms.ReadBuffer(byIn[0], ms.Size);
  10.     byOut:= zdecompress(byIn);
  11.     Memo1.Lines.Add('Size of uncompressed data: '+IntToStr(Length(byOut))); // 65536 <-- VERY BAD !
  12.     bs.Free;
  13. end;

What am I doing wrong?
Is there a problem with the compressed data?

I am attaching a test project.
(Window 10 64 bit + Laz 4.4 64 bit)



440bx

  • Hero Member
  • *****
  • Posts: 6542
Re: Infernal error when decompressing GZIP stream
« Reply #1 on: May 19, 2026, 12:40:15 am »
"infernal error", that must be an error from hell.  I didn't know FPC could detect those. ;)
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Fibonacci

  • Hero Member
  • *****
  • Posts: 1002
  • Behold, I bring salvation - FPC Unleashed
Re: Infernal error when decompressing GZIP stream
« Reply #2 on: May 19, 2026, 12:56:28 am »
This is not .gz (GZIP) - it's ZLIB format, and the file is corrupted (invalid checksum). Since the file is broken, ZFlate "assumed" it should still try to inflate the data, which worked - but that doesn't change the fact that the file isn't GZIP and is corrupted.
FPC Unleashed - inline vars, tuples, statement expressions, array equality, compound assignments, indexed/lazy labels, no-RTTI & more. ⭐ Star it on GitHub!

hedgehog

  • Full Member
  • ***
  • Posts: 124
Re: Infernal error when decompressing GZIP stream
« Reply #3 on: May 19, 2026, 05:36:27 am »
Hi, Fibonacci
I came up with an arbitrary file extension. This is a data stream from industrial equipment.
What confuses me is that in the second case, the result is doubled.

I also noticed that if you add an extra byte to the array of bytes, everything works fine.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.btnZFlate2Click(Sender: TObject);
  2. var
  3.   ms: TMemoryStream;
  4.   byIn, byOut: TBytes;
  5. begin
  6.     ms:= TMemoryStream.Create;
  7.     ms.LoadFromFile('myfile1.gz');
  8.     SetLength(byIn, ms.Size + 1); // <<------------------ this
  9.     ms.ReadBuffer(byIn[0], ms.Size);
  10.     byOut:= zdecompress(byIn);
  11.     Memo1.Lines.Add('Size of uncompressed data: '+IntToStr(Length(byOut))); //
  12.     bs.Free;
  13. end;

ginoo

  • Full Member
  • ***
  • Posts: 166
Re: Infernal error when decompressing GZIP stream
« Reply #4 on: May 19, 2026, 09:52:43 am »
The new generation of fpc errors, the infernal errors, the most terrible, the most frightening

 

TinyPortal © 2005-2018