diff options
author | Harald Welte <laforge@osmocom.org> | 2024-01-04 21:18:31 +0100 |
---|---|---|
committer | Harald Welte <laforge@osmocom.org> | 2024-01-04 21:20:19 +0100 |
commit | b0c9ccba66ebe861a7ff8b18e2f778a0d365670b (patch) | |
tree | 73db1c119f02a5cf85961827772cb2ad6d1663a6 | |
parent | e13403b2065f53c648929861132b7b77fef6e6e7 (diff) |
construct: avoid StreamError exceptions due to files containing all-ff
In smart cards, files/records containing all-ff means they are simply
not used/initialized. Let's avoid raising exceptions when interpreting
0xff as length value and reading less bytes as value.
Change-Id: I09c3cb82063fc094eb047749996a6eceff757ea2
-rw-r--r-- | pySim/construct.py | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/pySim/construct.py b/pySim/construct.py index 1ed3576..0c82c41 100644 --- a/pySim/construct.py +++ b/pySim/construct.py @@ -402,7 +402,16 @@ def parse_construct(c, raw_bin_data: bytes, length: typing.Optional[int] = None, """Helper function to wrap around normalize_construct() and filter_dict().""" if not length: length = len(raw_bin_data) - parsed = c.parse(raw_bin_data, total_len=length, **context) + try: + parsed = c.parse(raw_bin_data, total_len=length, **context) + except StreamError as e: + # if the input is all-ff, this means the content is undefined. Let's avoid passing StreamError + # exceptions in those situations (which might occur if a length field 0xff is 255 but then there's + # actually less bytes in the remainder of the file. + if all([v == 0xff for v in raw_bin_data]): + return None + else: + raise e return normalize_construct(parsed) def build_construct(c, decoded_data, context: dict = {}): |