Skip to content

Commit

Permalink
Merge pull request #146 from psd-tools/v1.8.30
Browse files Browse the repository at this point in the history
workaround for reading malformed psd files
  • Loading branch information
kyamagu committed Sep 24, 2019
2 parents 5bb068d + bb3098d commit 381bda6
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
1.8.30 (2019-09-24)
-------------------

- [psd] workaround for reading less-than-4-byte int in malformed psd files.

1.8.29 (2019-09-10)
-------------------

Expand Down
18 changes: 15 additions & 3 deletions src/psd_tools/psd/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,11 @@ class ShortIntegerElement(IntegerElement):

@classmethod
def read(cls, fp, **kwargs):
return cls(read_fmt('H2x', fp)[0])
try:
return cls(read_fmt('H2x', fp)[0])
except AssertionError as e:
logger.error(e)
return cls(read_fmt('H', fp)[0])

def write(self, fp, **kwargs):
return write_fmt(fp, 'H2x', self.value)
Expand All @@ -367,7 +371,11 @@ class ByteElement(IntegerElement):

@classmethod
def read(cls, fp, **kwargs):
return cls(read_fmt('B3x', fp)[0])
try:
return cls(read_fmt('B3x', fp)[0])
except AssertionError as e:
logger.error(e)
return cls(read_fmt('B', fp)[0])

def write(self, fp, **kwargs):
return write_fmt(fp, 'B3x', self.value)
Expand All @@ -384,7 +392,11 @@ class BooleanElement(IntegerElement):

@classmethod
def read(cls, fp, **kwargs):
return cls(read_fmt('?3x', fp)[0])
try:
return cls(read_fmt('?3x', fp)[0])
except AssertionError as e:
logger.error(e)
return cls(read_fmt('?', fp)[0])

def write(self, fp, **kwargs):
return write_fmt(fp, '?3x', self.value)
Expand Down
8 changes: 6 additions & 2 deletions src/psd_tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ def read_fmt(fmt, fp):
fmt = str(">" + fmt)
fmt_size = struct.calcsize(fmt)
data = fp.read(fmt_size)
assert len(data
) == fmt_size, 'read=%d, expected=%d' % (len(data), fmt_size)
try:
assert len(data
) == fmt_size, 'read=%d, expected=%d' % (len(data), fmt_size)
except:
fp.seek(-len(data), 1)
raise
return struct.unpack(fmt, data)


Expand Down
2 changes: 1 addition & 1 deletion src/psd_tools/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.8.29'
__version__ = '1.8.30'
11 changes: 11 additions & 0 deletions tests/psd_tools/psd/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ def test_numbers(kls, fixture):
assert hash(value) == hash(fixture)


@pytest.mark.parametrize(
'kls, fixture', [
(ByteElement, b'\x01\x00'),
(ShortIntegerElement, b'\x00\x01'),
(BooleanElement, b'\x00\x01'),
]
)
def test_malformed_numbers(kls, fixture):
kls.frombytes(fixture)


def test_boolean():
value = BooleanElement(True)
assert value.value is True
Expand Down

0 comments on commit 381bda6

Please sign in to comment.