From 7e717b50370f56dd0895922196098ef6c72c347c Mon Sep 17 00:00:00 2001 From: jevans Date: Sat, 26 Oct 2013 18:05:44 -0400 Subject: [PATCH] Made Exif handling more resilient. #104 --- glymur/_uuid_io/Exif.py | 6 +++- glymur/jp2box.py | 23 +++++++++---- glymur/test/test_jp2box_uuid.py | 57 ++++++++++++++++++++++++++++++++- 3 files changed, 77 insertions(+), 9 deletions(-) diff --git a/glymur/_uuid_io/Exif.py b/glymur/_uuid_io/Exif.py index 3fb24b1..0a86c18 100644 --- a/glymur/_uuid_io/Exif.py +++ b/glymur/_uuid_io/Exif.py @@ -39,9 +39,13 @@ class UUIDExif(object): if data[0] == 73 and data[1] == 73: # little endian self.endian = '<' - else: + elif data[0] == 77 and data[1] == 77: # big endian self.endian = '>' + else: + msg = "Bad byte order indication: {0}".format(read_buffer[6:8]) + raise RuntimeError(msg) + offset = data[3] # This is the 'Exif Image' portion. diff --git a/glymur/jp2box.py b/glymur/jp2box.py index 681cf51..074d18e 100644 --- a/glymur/jp2box.py +++ b/glymur/jp2box.py @@ -19,6 +19,7 @@ import os import pprint import struct import sys +import traceback import uuid import warnings import xml.etree.cElementTree as ET @@ -2078,15 +2079,23 @@ class UUIDBox(Jp2kBox): Jp2kBox.__init__(self, box_id='uuid', longname='UUID') self.uuid = the_uuid - if the_uuid == uuid.UUID('be7acfcb-97a9-42e8-9c71-999491e3afac'): - self.data = _uuid_io.UUIDXMP(raw_data) - self._type = 'XMP' - elif the_uuid.bytes == b'JpgTiffExif->JP2': - self.data = _uuid_io.UUIDExif(raw_data) - self._type = 'Exif' - else: + try: + if the_uuid == uuid.UUID('be7acfcb-97a9-42e8-9c71-999491e3afac'): + self.data = _uuid_io.UUIDXMP(raw_data) + self._type = 'XMP' + elif the_uuid.bytes == b'JpgTiffExif->JP2': + self.data = _uuid_io.UUIDExif(raw_data) + self._type = 'Exif' + else: + self.data = _uuid_io.UUIDGeneric(raw_data) + self._type = 'unknown' + except Exception as err: + # In case of any exception, create the generic UUID. self.data = _uuid_io.UUIDGeneric(raw_data) self._type = 'unknown' + msg = "Error encountered during UUID processing, " + msg += "the UUID will be treated as generic.\n\n{0}" + warnings.warn(msg.format(traceback.format_exc())) self.raw_data = raw_data diff --git a/glymur/test/test_jp2box_uuid.py b/glymur/test/test_jp2box_uuid.py index 6278f57..a1d93a4 100644 --- a/glymur/test/test_jp2box_uuid.py +++ b/glymur/test/test_jp2box_uuid.py @@ -63,7 +63,7 @@ class TestUUIDExif(unittest.TestCase): xbuffer = struct.pack('I4s', 52, b'uuid')) + tfile.write(b'JpgTiffExif->JP2') + + tfile.write(b'Exif\x00\x00') + xbuffer = struct.pack('I4s', 52, b'uuid')) + tfile.write(b'JpgTiffExif->JP2') + + tfile.write(b'Exif\x00\x00') + xbuffer = struct.pack('