Skip to content

Commit

Permalink
Fix some BLF files can't be read
Browse files Browse the repository at this point in the history
Fixes #763
  • Loading branch information
christiansandberg committed Feb 8, 2020
1 parent 2282bfc commit 32a029c
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions can/io/blf.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,13 @@ def __iter__(self):
def _parse_container(self, data):
if self._tail:
data = b"".join((self._tail, data))
self._pos = 0
try:
yield from self._parse_data(data)
except struct.error:
# Container data exhausted
# Save the remaining data that could not be processed
self._tail = data[self._pos :]
# There was not enough data in the container to unpack a struct
pass
# Save the remaining data that could not be processed
self._tail = data[self._pos :]

def _parse_data(self, data):
"""Optimized inner loop by making local copies of global variables
Expand All @@ -213,12 +213,14 @@ def _parse_data(self, data):
unpack_can_error_ext = CAN_ERROR_EXT_STRUCT.unpack_from

start_timestamp = self.start_timestamp
max_pos = len(data)
pos = 0

# Loop until a struct unpack raises an exception
while True:
self._pos = pos
header = unpack_obj_header_base(data, pos)
# print(header)
signature, _, header_version, obj_size, obj_type = header
if signature != b"LOBJ":
raise BLFParseError()
Expand All @@ -228,6 +230,9 @@ def _parse_data(self, data):
if obj_type != CAN_FD_MESSAGE_64:
# Add padding bytes
next_pos += obj_size % 4
if next_pos >= max_pos:
# This object continues in the next container
return
pos += obj_header_base_size

# Read rest of header
Expand Down

0 comments on commit 32a029c

Please sign in to comment.