filesystem: Introduce 'construct' support

This allows for pure declarative encoders/decoders for linear fixed
and transparent EFs.

Change-Id: Id0765ec3ddb8c044838ce47d0ff8740720a32b15
This commit is contained in:
Harald Welte
2021-04-10 19:05:37 +02:00
parent 703f933b40
commit 2db5cfb525
2 changed files with 36 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
from construct import *
from pySim.utils import b2h, h2b
from pySim.utils import b2h, h2b, swap_nibbles
"""Utility code related to the integration of the 'construct' declarative parser."""
@@ -26,6 +26,13 @@ class HexAdapter(Adapter):
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))
def filter_dict(d, exclude_prefix='_'):
"""filter the input dict to ensure no keys starting with 'exclude_prefix' remain."""
res = {}