Skip to content

Commit

Permalink
zlib: fix length parsing for uncompressed data
Browse files Browse the repository at this point in the history
- data length is stored as unsigned short (pigz actually creates blocks of
  65K length)
- when calculating the one's complement of the length, Python will turn it
  into a negative number. Use AND to make it positive (unsigned).
  • Loading branch information
oliver authored and vstinner committed Jan 6, 2025
1 parent 39d7804 commit 9e42b9d
Showing 1 changed file with 4 additions and 4 deletions.
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

0 comments on commit 9e42b9d

Please sign in to comment.