Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix zlib parsing of length for large uncompressed block #100

Merged
merged 3 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions hachoir/parser/archive/zlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""

from hachoir.parser import Parser
from hachoir.field import (Bit, Bits, Field, Int16, UInt32,
from hachoir.field import (Bit, Bits, Field, UInt16, UInt32,
Enum, FieldSet, GenericFieldSet,
PaddingBits, ParserError, RawBytes)
from hachoir.core.endian import LITTLE_ENDIAN
Expand Down Expand Up @@ -149,9 +149,9 @@ def createFields(self):
padding = paddingSize(self.current_size + self.absolute_address, 8)
if padding:
yield PaddingBits(self, "padding[]", padding)
yield Int16(self, "len")
yield Int16(self, "nlen", "One's complement of len")
if self["len"].value != ~self["nlen"].value:
yield UInt16(self, "len")
yield UInt16(self, "nlen", "One's complement of len")
if self["len"].value != ((~self["nlen"].value) & 0xFFFF):
raise ParserError(
"len must be equal to the one's complement of nlen!")
# null stored blocks produced by some encoders (e.g. PIL)
Expand Down
Binary file added tests/files/usa_railroad.jpg.0.zlib
Binary file not shown.
Binary file added tests/files/usa_railroad.jpg.6.zlib
Binary file not shown.
31 changes: 31 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,37 @@ def test_arj2(self):
self.checkValue(parser, "/file_header[15]/filename", "usr/bin/groups")
self.checkValue(parser, "/file_header[15]/original_size", 35000)

def test_zlib(self):
parser = self.parse("usa_railroad.jpg.6.zlib")
self.checkValue(parser, "/compression_method", 8)
self.checkValue(parser, "/compression_info", 7)
self.checkValue(parser, "/flag_check_bits", 30)
self.checkValue(parser, "/flag_dictionary_present", False)
self.checkValue(parser, "/flag_compression_level", 1)
self.checkValue(parser, "/data/compressed_block[0]/final", False)
self.checkValue(parser, "/data/compressed_block[0]/compression_type", 2)
self.checkValue(parser, "/data/compressed_block[0]/huff_num_length_codes", 29)
self.checkValue(parser, "/data/compressed_block[0]/length_code[16383]", 16380)
self.checkValue(parser, "/data/compressed_block[5]/final", True)
self.checkValue(parser, "/data/compressed_block[5]/compression_type", 0)
self.checkValue(parser, "/data/compressed_block[5]/len", 8844)
self.checkValue(parser, "/data/compressed_block[5]/nlen", 56691)
self.checkValue(parser, "/data_checksum", 0xe85c1f89)

def test_zlib_large_uncompressed_block(self):
parser = self.parse("usa_railroad.jpg.0.zlib")
self.checkValue(parser, "/data/compressed_block[0]/final", False)
self.checkValue(parser, "/data/compressed_block[0]/compression_type", 0)
self.checkValue(parser, "/data/compressed_block[0]/padding[0]", 0)
self.checkValue(parser, "/data/compressed_block[0]/len", 65535)
self.checkValue(parser, "/data/compressed_block[0]/nlen", 0)
self.checkValue(parser, "/data/compressed_block[1]/final", True)
self.checkValue(parser, "/data/compressed_block[1]/compression_type", 0)
self.checkValue(parser, "/data/compressed_block[1]/padding[0]", 0)
self.checkValue(parser, "/data/compressed_block[1]/len", 38213)
self.checkValue(parser, "/data/compressed_block[1]/nlen", 27322)
self.checkValue(parser, "/data_checksum", 0xe85c1f89)


class TestParserRandomStream(unittest.TestCase):

Expand Down
Loading