-
Notifications
You must be signed in to change notification settings - Fork 195
Fix reading for long MakerNote and UserComment #165
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
Open
Lessica
wants to merge
3
commits into
ianare:develop
Choose a base branch
from
Lessica:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,6 +97,8 @@ def s2n(self, offset, length: int, signed=False) -> int: | |
raise ValueError('unexpected unpacking length: %d' % length) from err | ||
self.file_handle.seek(self.offset + offset) | ||
buf = self.file_handle.read(length) | ||
if len(buf) != length: | ||
raise ValueError("cannot read enough bytes, something elsewhere is wrong") | ||
|
||
if buf: | ||
# https://github.com/ianare/exif-py/pull/158 | ||
|
@@ -145,47 +147,36 @@ def list_ifd(self) -> list: | |
def _process_field(self, tag_name, count, field_type, type_length, offset): | ||
values = [] | ||
signed = (field_type in [6, 8, 9, 10]) | ||
# XXX investigate | ||
# some entries get too big to handle could be malformed | ||
# file or problem with self.s2n | ||
if count < 1000: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We cannot just ignore large entries other than |
||
for _ in range(count): | ||
if field_type in (5, 10): | ||
# a ratio | ||
value = Ratio( | ||
self.s2n(offset, 4, signed), | ||
self.s2n(offset + 4, 4, signed) | ||
) | ||
elif field_type in (11, 12): | ||
# a float or double | ||
unpack_format = '' | ||
if self.endian == 'I': | ||
unpack_format += '<' | ||
else: | ||
unpack_format += '>' | ||
if field_type == 11: | ||
unpack_format += 'f' | ||
else: | ||
unpack_format += 'd' | ||
self.file_handle.seek(self.offset + offset) | ||
byte_str = self.file_handle.read(type_length) | ||
try: | ||
value = struct.unpack(unpack_format, byte_str) | ||
except struct.error: | ||
logger.warning('Possibly corrupted field %s', tag_name) | ||
# -1 means corrupted | ||
value = -1 | ||
for _ in range(count): | ||
if field_type in (5, 10): | ||
# a ratio | ||
value = Ratio( | ||
self.s2n(offset, 4, signed), | ||
self.s2n(offset + 4, 4, signed) | ||
) | ||
elif field_type in (11, 12): | ||
# a float or double | ||
unpack_format = '' | ||
if self.endian == 'I': | ||
unpack_format += '<' | ||
else: | ||
value = self.s2n(offset, type_length, signed) | ||
values.append(value) | ||
offset = offset + type_length | ||
# The test above causes problems with tags that are | ||
# supposed to have long values! Fix up one important case. | ||
elif tag_name in ('MakerNote', makernote.canon.CAMERA_INFO_TAG_NAME): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved to function |
||
for _ in range(count): | ||
unpack_format += '>' | ||
if field_type == 11: | ||
unpack_format += 'f' | ||
else: | ||
unpack_format += 'd' | ||
self.file_handle.seek(self.offset + offset) | ||
byte_str = self.file_handle.read(type_length) | ||
try: | ||
value = struct.unpack(unpack_format, byte_str) | ||
except struct.error: | ||
logger.warning('Possibly corrupted field %s', tag_name) | ||
# -1 means corrupted | ||
value = -1 | ||
else: | ||
value = self.s2n(offset, type_length, signed) | ||
values.append(value) | ||
offset = offset + type_length | ||
values.append(value) | ||
offset = offset + type_length | ||
return values | ||
|
||
def _process_field2(self, ifd_name, tag_name, count, offset): | ||
|
@@ -214,6 +205,15 @@ def _process_field2(self, ifd_name, tag_name, count, offset): | |
values = '' | ||
return values | ||
|
||
def _process_field7(self, count, offset): | ||
# undefined types e.g. MakerNote/UserComment | ||
values = [] | ||
for _ in range(count): | ||
value = self.s2n(offset, 1, signed=False) | ||
values.append(value) | ||
offset += 1 | ||
return values | ||
|
||
def _process_tag(self, ifd, ifd_name: str, tag_entry, entry, tag: int, tag_name, relative, stop_tag) -> None: | ||
field_type = self.s2n(entry + 2, 2) | ||
|
||
|
@@ -248,9 +248,11 @@ def _process_tag(self, ifd, ifd_name: str, tag_entry, entry, tag: int, tag_name, | |
|
||
field_offset = offset | ||
values = None | ||
if field_type == 2: | ||
if field_type == 2: # ascii strings | ||
values = self._process_field2(ifd_name, tag_name, count, offset) | ||
else: | ||
elif field_type == 7: # undefined | ||
values = self._process_field7(count, offset) | ||
else: # numbers | ||
values = self._process_field(tag_name, count, field_type, type_length, offset) | ||
# now 'values' is either a string or an array | ||
# TODO: use only one type | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This prevents infinite loop in some tests. But it will not be fixed until the parsing of Canon's
MakerNote
being fixed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This requires further investigation.