From 32a029c8decc30d6056e22399af518cff869f96c Mon Sep 17 00:00:00 2001 From: Christian Sandberg Date: Sat, 8 Feb 2020 19:44:41 +0100 Subject: [PATCH 1/2] Fix some BLF files can't be read Fixes #763 --- can/io/blf.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/can/io/blf.py b/can/io/blf.py index 8df41ed5a..d91ca5028 100644 --- a/can/io/blf.py +++ b/can/io/blf.py @@ -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 @@ -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() @@ -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 From c60a44829442f3a1009daf1a117da4269edf3852 Mon Sep 17 00:00:00 2001 From: Christian Sandberg Date: Sat, 8 Feb 2020 19:51:58 +0100 Subject: [PATCH 2/2] Fix last message not read --- can/io/blf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/can/io/blf.py b/can/io/blf.py index d91ca5028..8ac79ddb8 100644 --- a/can/io/blf.py +++ b/can/io/blf.py @@ -230,7 +230,7 @@ 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: + if next_pos > max_pos: # This object continues in the next container return pos += obj_header_base_size