ZlibStream fails to check checksum correctly #241

Closed
opened 2026-01-29 22:08:55 +00:00 by claunia · 2 comments
Owner

Originally created by @ied206 on GitHub (Sep 15, 2017).

ZlibStreams occasionally fails to decompress correct zlib files.

.zz files in TestSample.zip are the zlib streams which causes ZlibStream to malfunction.
When ZlibStream tries to decompress those files, it throws ZlibException with 'invalid data check' message.

However, those files can be successfully extracted in pigz with --zlib flag, so the problem resides in SharpCompress.

To prove this, I converted zlib streams into pure deflate streams by removing first 2 byte (zlib magic number) and last 4 byte (Adler32 checksum), and tried to decompress with DeflateStream. In this case, file was successfully decompressed, so I suspect SharpCompress' Adler32 checksum checking routine is faulty.

I wrote some Tests to help debugging, please have a look.

Tested Environment

OS : Windows 10 x64 v1703
IDE : Visual Studio 2017
DotNet : .Net Framework 4.6.2, .Net Framework 4.7
Package : SharpCompress 0.18.1

Originally created by @ied206 on GitHub (Sep 15, 2017). ZlibStreams occasionally fails to decompress correct zlib files. .zz files in [TestSample.zip](https://github.com/adamhathcock/sharpcompress/files/1307402/TestSample.zip) are the zlib streams which causes ZlibStream to malfunction. When ZlibStream tries to decompress those files, it throws ZlibException with 'invalid data check' message. However, those files can be successfully extracted in [pigz](https://zlib.net/pigz/) with --zlib flag, so the problem resides in SharpCompress. To prove this, I converted zlib streams into pure deflate streams by removing first 2 byte (zlib magic number) and last 4 byte (Adler32 checksum), and tried to decompress with DeflateStream. In this case, file was successfully decompressed, so I suspect SharpCompress' Adler32 checksum checking routine is faulty. I wrote some [Tests](https://github.com/ied206/SharpCompressBug) to help debugging, please have a look. ### Tested Environment OS : Windows 10 x64 v1703 IDE : Visual Studio 2017 DotNet : .Net Framework 4.6.2, .Net Framework 4.7 Package : SharpCompress 0.18.1
claunia added the bug label 2026-01-29 22:08:55 +00:00
Author
Owner

@ied206 commented on GitHub (Sep 16, 2017):

More addtional information, I checked the internal adler32 checksum in InflateBlocks.Process().

ex1_zlib_sharpcompress.jpg.zz

  • computedCheck : 0xd2367044
  • expectedCheck : 0xd77c7044

ex2_zlib_sharpcompress.jpg.zz

  • computedCheck : 0x99d5edad
  • expectedCheck : 0x9b97edad

So only the high two bytes are miscalculated.

@ied206 commented on GitHub (Sep 16, 2017): More addtional information, I checked the internal adler32 checksum in InflateBlocks.Process(). #### ex1_zlib_sharpcompress.jpg.zz - computedCheck : 0xd2367044 - expectedCheck : 0xd77c7044 #### ex2_zlib_sharpcompress.jpg.zz - computedCheck : 0x99d5edad - expectedCheck : 0x9b97edad So only the high two bytes are miscalculated.
Author
Owner

@adamhathcock commented on GitHub (Sep 17, 2017):

Fixed by https://github.com/adamhathcock/sharpcompress/pull/301

@adamhathcock commented on GitHub (Sep 17, 2017): Fixed by https://github.com/adamhathcock/sharpcompress/pull/301
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#241