laforge submitted this change.

View Change


Approvals: laforge: Looks good to me, approved fixeria: Looks good to me, but someone else must approve Jenkins Builder: Verified
pySim.apdu: Allow TLV based decoders for APDU command and response body

So far we only supported construct.

Change-Id: Ibb80d328c9a1f464aa5338ca0ca1d6bfb00734e1
---
M pySim/apdu/__init__.py
1 file changed, 26 insertions(+), 3 deletions(-)

diff --git a/pySim/apdu/__init__.py b/pySim/apdu/__init__.py
index f5b7852..02ccbad 100644
--- a/pySim/apdu/__init__.py
+++ b/pySim/apdu/__init__.py
@@ -9,7 +9,7 @@
we already know in pySim about the filesystem structure, file encoding, etc.
"""

-# (C) 2022 by Harald Welte <laforge@osmocom.org>
+# (C) 2022-2024 by Harald Welte <laforge@osmocom.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -152,6 +152,8 @@
_construct_p2 = Byte
_construct = HexAdapter(GreedyBytes)
_construct_rsp = HexAdapter(GreedyBytes)
+ _tlv = None
+ _tlv_rsp = None

def __init__(self, cmd: BytesOrHex, rsp: Optional[BytesOrHex] = None):
"""Instantiate a new ApduCommand from give cmd + resp."""
@@ -299,7 +301,12 @@
r['p2'] = parse_construct(self._construct_p2, self.p2.to_bytes(1, 'big'))
r['p3'] = self.p3
if self.cmd_data:
- r['body'] = parse_construct(self._construct, self.cmd_data)
+ if self._tlv:
+ ie = self._tlv()
+ ie.from_tlv(self.cmd_data)
+ r['body'] = ie.to_dict()
+ else:
+ r['body'] = parse_construct(self._construct, self.cmd_data)
return r

def rsp_to_dict(self) -> Dict:
@@ -310,7 +317,12 @@
else:
r = {}
if self.rsp_data:
- r['body'] = parse_construct(self._construct_rsp, self.rsp_data)
+ if self._tlv_rsp:
+ ie = self._tlv_rsp()
+ ie.from_tlv(self.rsp_data)
+ r['body'] = ie.to_dict()
+ else:
+ r['body'] = parse_construct(self._construct_rsp, self.rsp_data)
r['sw'] = b2h(self.sw)
return r


To view, visit change 37131. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Ibb80d328c9a1f464aa5338ca0ca1d6bfb00734e1
Gerrit-Change-Number: 37131
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge@osmocom.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: fixeria <vyanitskiy@sysmocom.de>
Gerrit-Reviewer: laforge <laforge@osmocom.org>
Gerrit-MessageType: merged