mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-09-21 11:28:58 +03:00
Restoring bad exif handling
This commit is contained in:
parent
47a963c2a4
commit
5f9fff0215
|
@ -468,13 +468,22 @@ class ImageFileDirectory_v2(collections.MutableMapping):
|
||||||
return b"".join(self._pack("2L", *_limit_rational(frac, 2 ** 30))
|
return b"".join(self._pack("2L", *_limit_rational(frac, 2 ** 30))
|
||||||
for frac in values)
|
for frac in values)
|
||||||
|
|
||||||
|
def _ensure_read(self, fp, size):
|
||||||
|
ret = fp.read(size)
|
||||||
|
if len(ret) != size:
|
||||||
|
raise IOError("Corrupt EXIF data. " +
|
||||||
|
"Expecting to read %d bytes but only got %d. " %
|
||||||
|
(size, len(ret)))
|
||||||
|
return ret
|
||||||
|
|
||||||
def load(self, fp):
|
def load(self, fp):
|
||||||
|
|
||||||
self.reset()
|
self.reset()
|
||||||
self._offset = fp.tell()
|
self._offset = fp.tell()
|
||||||
|
|
||||||
for i in range(self._unpack("H", fp.read(2))[0]):
|
try:
|
||||||
tag, typ, count, data = self._unpack("HHL4s", fp.read(12))
|
for i in range(self._unpack("H", self._ensure_read(fp,2))[0]):
|
||||||
|
tag, typ, count, data = self._unpack("HHL4s", self._ensure_read(fp,12))
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
tagname = TAGS.get(tag, TagInfo()).name
|
tagname = TAGS.get(tag, TagInfo()).name
|
||||||
typname = TYPES.get(typ, "unknown")
|
typname = TYPES.get(typ, "unknown")
|
||||||
|
@ -515,7 +524,10 @@ class ImageFileDirectory_v2(collections.MutableMapping):
|
||||||
else:
|
else:
|
||||||
print("- value:", self[tag])
|
print("- value:", self[tag])
|
||||||
|
|
||||||
self.next, = self._unpack("L", fp.read(4))
|
self.next, = self._unpack("L", self._ensure_read(fp,4))
|
||||||
|
except IOError as msg:
|
||||||
|
warnings.warn(str(msg))
|
||||||
|
return
|
||||||
|
|
||||||
def save(self, fp):
|
def save(self, fp):
|
||||||
|
|
||||||
|
|
|
@ -99,9 +99,11 @@ class TestFileTiff(PillowTestCase):
|
||||||
lambda: TiffImagePlugin.TiffImageFile(invalid_file))
|
lambda: TiffImagePlugin.TiffImageFile(invalid_file))
|
||||||
|
|
||||||
def test_bad_exif(self):
|
def test_bad_exif(self):
|
||||||
image = Image.open('Tests/images/hopper_bad_exif.jpg')
|
try:
|
||||||
image._getexif()
|
Image.open('Tests/images/hopper_bad_exif.jpg')._getexif()
|
||||||
self.assertRaises(Exception, image._getexif)
|
except struct.error:
|
||||||
|
self.fail(
|
||||||
|
"Bad EXIF data passed incorrect values to _binary unpack")
|
||||||
|
|
||||||
def test_save_unsupported_mode(self):
|
def test_save_unsupported_mode(self):
|
||||||
im = hopper("HSV")
|
im = hopper("HSV")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user