Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make iHex ignore space-only lines #441

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions cle/backends/ihex.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
import logging
import re
import struct
from typing import List, Optional, Tuple
from typing import TYPE_CHECKING, List, Optional, Tuple

import archinfo

from cle.errors import CLEError

from .backend import Backend, register_backend

if TYPE_CHECKING:
from io import BytesIO

log = logging.getLogger(name=__name__)

__all__ = ("Hex",)
Expand Down Expand Up @@ -97,7 +100,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
Expand Down Expand Up @@ -160,11 +164,28 @@ 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: "BytesIO", length=0x10, max_search=0x50):
data = b""
stream.seek(0)
s = stream.read(0x10)
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
break
if re.match(rb"\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)
Loading