Seek to the beginning of the file if reading the header fails

pull/37/head
tuxxy 2017-09-13 15:18:38 -07:00
parent 5ea05a0e0e
commit ec3b6903c1
1 changed files with 9 additions and 8 deletions

View File

@ -68,17 +68,18 @@ class EncryptedFile(object):
"""
Reads the header from the self.file_obj.
"""
# Read last four bytes (header length) of file.
self.file_obj.seek(-4, os.SEEK_END)
# The first four bytes of the file are the header length
self.header_length = int.from_bytes(
self.file_obj.read(4), byteorder='big')
# Seek to the beginning of the header and read it
self.file_obj.seek(-(self.header_length + 4), os.SEEK_END)
try:
# Read last four bytes (header length) of file.
self.file_obj.seek(-4, os.SEEK_END)
# The first four bytes of the file are the header length
self.header_length = int.from_bytes(
self.file_obj.read(4), byteorder='big')
# Seek to the beginning of the header and read it
self.file_obj.seek(-(self.header_length + 4), os.SEEK_END)
self.header = msgpack.loads(self.file_obj.read(self.header_length))
except ValueError as e:
self.file_obj.seek(0)
raise e
else:
# Seek to the end of the ciphertext