From 5014cb8c9e846ceb6616c2611cdb2322414931d9 Mon Sep 17 00:00:00 2001 From: mahaloz Date: Mon, 13 Nov 2023 21:40:35 -0500 Subject: [PATCH 1/5] Make iHex ignore space-only lines --- cle/backends/ihex.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/cle/backends/ihex.py b/cle/backends/ihex.py index bc422eaa..5817c615 100644 --- a/cle/backends/ihex.py +++ b/cle/backends/ihex.py @@ -97,7 +97,8 @@ def __init__(self, *args, ignore_missing_arch: bool = False, **kwargs): got_entry = False self._binary_stream.seek(0) string = self._binary_stream.read() - recs = string.splitlines() + # line exists and is not just spaces + recs = [line for line in string.splitlines() if line.strip()] regions = [] max_addr = 0 min_addr = 0xFFFFFFFFFFFFFFFF @@ -160,11 +161,23 @@ def __init__(self, *args, ignore_missing_arch: bool = False, **kwargs): self._min_addr = min_addr @staticmethod - def is_compatible(stream): + def seek_non_space_bytes(stream, length=0x10): + data = b"" stream.seek(0) - s = stream.read(0x10) + while len(data) < length and stream: + byte = stream.read(1) + if re.match(br"\s", byte) is not None: + continue + + data += byte + stream.seek(0) - return s.startswith(b":") + return data + + @staticmethod + def is_compatible(stream): + s = Hex.seek_non_space_bytes(stream, length=0x10) + return len(s) == 0x10 and s.startswith(b":") register_backend("hex", Hex) From d8845e5610240787940fe7fd4fac8b38dead0b0e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 14 Nov 2023 02:43:56 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- cle/backends/ihex.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cle/backends/ihex.py b/cle/backends/ihex.py index 5817c615..05b16c50 100644 --- a/cle/backends/ihex.py +++ b/cle/backends/ihex.py @@ -166,7 +166,7 @@ def seek_non_space_bytes(stream, length=0x10): stream.seek(0) while len(data) < length and stream: byte = stream.read(1) - if re.match(br"\s", byte) is not None: + if re.match(rb"\s", byte) is not None: continue data += byte From a8929e917af0d09a94efd17461e4edbefe8b8231 Mon Sep 17 00:00:00 2001 From: Fish Date: Wed, 7 Feb 2024 11:37:53 -0700 Subject: [PATCH 3/5] Hex: Fix an infinite loop when the input is less than 16 bytes. --- cle/backends/ihex.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cle/backends/ihex.py b/cle/backends/ihex.py index 05b16c50..b9cf7168 100644 --- a/cle/backends/ihex.py +++ b/cle/backends/ihex.py @@ -2,7 +2,7 @@ import logging import re import struct -from typing import List, Optional, Tuple +from typing import List, Optional, Tuple, TYPE_CHECKING import archinfo @@ -10,6 +10,9 @@ from .backend import Backend, register_backend +if TYPE_CHECKING: + from io import BytesIO + log = logging.getLogger(name=__name__) __all__ = ("Hex",) @@ -161,11 +164,14 @@ def __init__(self, *args, ignore_missing_arch: bool = False, **kwargs): self._min_addr = min_addr @staticmethod - def seek_non_space_bytes(stream, length=0x10): + def seek_non_space_bytes(stream: "BytesIO", length=0x10): data = b"" stream.seek(0) while len(data) < length and stream: byte = stream.read(1) + if not byte: + # we have exhausted the stream + break if re.match(rb"\s", byte) is not None: continue From 0352a41c095364ddf7a130b455d1f524a55fd5db Mon Sep 17 00:00:00 2001 From: mahaloz Date: Tue, 14 Nov 2023 14:50:26 -0500 Subject: [PATCH 4/5] Add a max search param (kill the loop) --- cle/backends/ihex.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cle/backends/ihex.py b/cle/backends/ihex.py index b9cf7168..e2adfcfb 100644 --- a/cle/backends/ihex.py +++ b/cle/backends/ihex.py @@ -164,10 +164,12 @@ def __init__(self, *args, ignore_missing_arch: bool = False, **kwargs): self._min_addr = min_addr @staticmethod - def seek_non_space_bytes(stream: "BytesIO", length=0x10): + def seek_non_space_bytes(stream: "BytesIO", length=0x10, max_search=0x50): data = b"" stream.seek(0) - while len(data) < length and stream: + search_len = 0 + while search_len < max_search and len(data) < length and stream: + search_len += 1 byte = stream.read(1) if not byte: # we have exhausted the stream From fc6ddf1d15625798b1fa06c7d126556856f00cb3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 7 Feb 2024 18:41:56 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- cle/backends/ihex.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cle/backends/ihex.py b/cle/backends/ihex.py index e2adfcfb..e4216980 100644 --- a/cle/backends/ihex.py +++ b/cle/backends/ihex.py @@ -2,7 +2,7 @@ import logging import re import struct -from typing import List, Optional, Tuple, TYPE_CHECKING +from typing import TYPE_CHECKING, List, Optional, Tuple import archinfo