Skip to content

Commit

Permalink
unpack_from cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mikhail-iurkov committed Jan 20, 2022
1 parent 2c3f6a5 commit 2aada99
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions piexif/_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ def __init__(self, data):
else:
raise InvalidImageDataError("Given file is neither JPEG nor TIFF.")

def _unpack_from(self, format, pointer):
return unpack_from(self.endian_mark + format, self.tiftag, pointer)

def get_ifd_dict(self, pointer, ifd_name, read_unknown=False):
try:
result = self._do_get_ifd_dict(pointer, ifd_name, read_unknown)
Expand All @@ -112,16 +115,12 @@ def get_ifd_dict(self, pointer, ifd_name, read_unknown=False):
return result

def _read_tag(self, pointer):
tag, value_type, value_num = unpack_from(
self.endian_mark + "HHL", self.tiftag, pointer
)
tag, value_type, value_num = self._unpack_from("HHL", pointer)
# Treat unknown types as `Undefined`
value_length = TYPE_LENGTH.get(value_type, 1)
value_length_total = value_length * value_num
if value_length_total > 4:
data_pointer = unpack_from(
self.endian_mark + "L", self.tiftag, pointer + 8
)[0]
data_pointer = self._unpack_from("L", pointer + 8)[0]
else:
data_pointer = pointer + 8

Expand All @@ -136,9 +135,7 @@ def _read_tag(self, pointer):
values = (raw_value, )
else:
# Unpacked types
values = unpack_from(
self.endian_mark + format * value_num, self.tiftag, data_pointer
)
values = self._unpack_from(format * value_num, data_pointer)
# Collate rationals
if len(format) > 1:
stride = len(format)
Expand All @@ -149,8 +146,7 @@ def _read_tag(self, pointer):

def _do_get_ifd_dict(self, pointer, ifd_name, read_unknown=False):
ifd_dict = {}
tag_count = unpack_from(self.endian_mark + "H",
self.tiftag, pointer)[0]
tag_count = self._unpack_from("H", pointer)[0]
offset = pointer + 2
if ifd_name in ["0th", "1st"]:
t = "Image"
Expand All @@ -171,9 +167,7 @@ def _do_get_ifd_dict(self, pointer, ifd_name, read_unknown=False):
values = values[0]
ifd_dict[tag] = values
elif read_unknown:
value_num, = unpack_from(
self.endian_mark + "L", self.tiftag, pointer + 4
)
value_num, = self._unpack_from("L", pointer + 4)
pointer_or_value = self.tiftag[pointer + 8: pointer + 12]
ifd_dict[tag] = value_type, value_num, pointer_or_value, self.tiftag
else:
Expand Down

0 comments on commit 2aada99

Please sign in to comment.