aboutsummaryrefslogtreecommitdiffstats
path: root/pySim/construct.py
diff options
context:
space:
mode:
authorHarald Welte <laforge@osmocom.org>2022-02-10 18:05:45 +0100
committerHarald Welte <laforge@osmocom.org>2022-02-11 13:32:58 +0100
commitc91085e744fa5fd136e73117e720af86c8418a2e (patch)
treea0f34952a6c254f9a9b63cd228e215582a32b7b6 /pySim/construct.py
parent181c7c5930a4b9bee27ab803cfa89cd508cfa7ed (diff)
cosmetic: Switch to consistent four-spaces indent; run autopep8
We had a mixture of tab and 4space based indenting, which is a bad idea. 4space is the standard in python, so convert all our code to that. The result unfortuantely still shoed even more inconsistencies, so I've decided to run autopep8 on the entire code base. Change-Id: I4a4b1b444a2f43fab05fc5d2c8a7dd6ddecb5f07
Diffstat (limited to 'pySim/construct.py')
-rw-r--r--pySim/construct.py24
1 files changed, 19 insertions, 5 deletions
diff --git a/pySim/construct.py b/pySim/construct.py
index 2a3efd3..e3f6a88 100644
--- a/pySim/construct.py
+++ b/pySim/construct.py
@@ -1,3 +1,5 @@
+from construct.lib.containers import Container, ListContainer
+from construct.core import EnumIntegerString
import typing
from construct import *
from pySim.utils import b2h, h2b, swap_nibbles
@@ -23,18 +25,24 @@ import gsm0338
class HexAdapter(Adapter):
"""convert a bytes() type to a string of hex nibbles."""
+
def _decode(self, obj, context, path):
return b2h(obj)
+
def _encode(self, obj, context, path):
return h2b(obj)
+
class BcdAdapter(Adapter):
"""convert a bytes() type to a string of BCD nibbles."""
+
def _decode(self, obj, context, path):
return swap_nibbles(b2h(obj))
+
def _encode(self, obj, context, path):
return h2b(swap_nibbles(obj))
+
class Rpad(Adapter):
"""
Encoder appends padding bytes (b'\\xff') up to target size.
@@ -54,9 +62,11 @@ class Rpad(Adapter):
def _encode(self, obj, context, path):
if len(obj) > self.sizeof():
- raise SizeofError("Input ({}) exceeds target size ({})".format(len(obj), self.sizeof()))
+ raise SizeofError("Input ({}) exceeds target size ({})".format(
+ len(obj), self.sizeof()))
return obj + self.pattern * (self.sizeof() - len(obj))
+
class GsmStringAdapter(Adapter):
"""Convert GSM 03.38 encoded bytes to a string."""
@@ -71,6 +81,7 @@ class GsmStringAdapter(Adapter):
def _encode(self, obj, context, path):
return obj.encode(self.codec, self.err)
+
def filter_dict(d, exclude_prefix='_'):
"""filter the input dict to ensure no keys starting with 'exclude_prefix' remain."""
if not isinstance(d, dict):
@@ -85,8 +96,6 @@ def filter_dict(d, exclude_prefix='_'):
res[key] = value
return res
-from construct.lib.containers import Container, ListContainer
-from construct.core import EnumIntegerString
def normalize_construct(c):
"""Convert a construct specific type to a related base type, mostly useful
@@ -95,7 +104,7 @@ def normalize_construct(c):
# in the dict: '_io': <_io.BytesIO object at 0x7fdb64e05860> which we cannot json-serialize
c = filter_dict(c)
if isinstance(c, Container) or isinstance(c, dict):
- r = {k : normalize_construct(v) for (k, v) in c.items()}
+ r = {k: normalize_construct(v) for (k, v) in c.items()}
elif isinstance(c, ListContainer):
r = [normalize_construct(x) for x in c]
elif isinstance(c, list):
@@ -106,13 +115,15 @@ def normalize_construct(c):
r = c
return r
-def parse_construct(c, raw_bin_data:bytes, length:typing.Optional[int]=None, exclude_prefix:str='_'):
+
+def parse_construct(c, raw_bin_data: bytes, length: typing.Optional[int] = None, exclude_prefix: str = '_'):
"""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)
return normalize_construct(parsed)
+
# here we collect some shared / common definitions of data types
LV = Prefixed(Int8ub, HexAdapter(GreedyBytes))
@@ -129,6 +140,7 @@ ByteRFU = Default(Byte, __RFU_VALUE)
# Field that packs all remaining Reserved for Future Use (RFU) bytes
GreedyBytesRFU = Default(GreedyBytes, b'')
+
def BitsRFU(n=1):
'''
Field that packs Reserved for Future Use (RFU) bit(s)
@@ -143,6 +155,7 @@ def BitsRFU(n=1):
'''
return Default(BitsInteger(n), __RFU_VALUE)
+
def BytesRFU(n=1):
'''
Field that packs Reserved for Future Use (RFU) byte(s)
@@ -157,6 +170,7 @@ def BytesRFU(n=1):
'''
return Default(Bytes(n), __RFU_VALUE)
+
def GsmString(n):
'''
GSM 03.38 encoded byte string of fixed length n.