Change in pysim[master]: filesystem: Introduce support for TLV parser

laforge gerrit-no-reply at lists.osmocom.org
Sat Jun 5 12:25:51 UTC 2021


laforge has submitted this change. ( https://gerrit.osmocom.org/c/pysim/+/24454 )

Change subject: filesystem: Introduce support for TLV parser
......................................................................

filesystem: Introduce support for TLV parser

This adds an easy way for files to make use of the pySim.tlv parser.

All a file has to do is to specify a _tlv member which points to
either a TLV_IE or a TLV_IE_Collection instance.

Change-Id: I59f456b4223ec88081e91cee168b654c69bcb5f4
---
M pySim/filesystem.py
M pySim/tlv.py
2 files changed, 40 insertions(+), 0 deletions(-)

Approvals:
  Jenkins Builder: Verified
  laforge: Looks good to me, approved



diff --git a/pySim/filesystem.py b/pySim/filesystem.py
index 9a2f4e1..170429b 100644
--- a/pySim/filesystem.py
+++ b/pySim/filesystem.py
@@ -468,6 +468,7 @@
         """
         super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, parent=parent)
         self._construct = None
+        self._tlv = None
         self.size = size
         self.shell_commands = [self.ShellCommands()]
 
@@ -491,6 +492,9 @@
             return method(b2h(raw_bin_data))
         if self._construct:
             return parse_construct(self._construct, raw_bin_data)
+        elif self._tlv:
+            self._tlv.from_tlv(raw_bin_data)
+            return self._tlv.to_dict()
         return {'raw': raw_bin_data.hex()}
 
     def decode_hex(self, raw_hex_data:str) -> dict:
@@ -514,6 +518,9 @@
             return method(raw_bin_data)
         if self._construct:
             return parse_construct(self._construct, raw_bin_data)
+        elif self._tlv:
+            self._tlv.from_tlv(raw_bin_data)
+            return self._tlv.to_dict()
         return {'raw': raw_bin_data.hex()}
 
     def encode_bin(self, abstract_data:dict) -> bytearray:
@@ -536,6 +543,9 @@
             return h2b(method(abstract_data))
         if self._construct:
             return self._construct.build(abstract_data)
+        elif self._tlv:
+            self._tlv.from_dict(abstract_data)
+            return self._tlv.to_tlv()
         raise NotImplementedError
 
     def encode_hex(self, abstract_data:dict) -> str:
@@ -559,6 +569,9 @@
             return b2h(raw_bin_data)
         if self._construct:
             return b2h(self._construct.build(abstract_data))
+        elif self._tlv:
+            self._tlv.from_dict(abstract_data)
+            return b2h(self._tlv.to_tlv())
         raise NotImplementedError
 
 
@@ -691,6 +704,7 @@
         self.rec_len = rec_len
         self.shell_commands = [self.ShellCommands()]
         self._construct = None
+        self._tlv = None
 
     def decode_record_hex(self, raw_hex_data:str) -> dict:
         """Decode raw (hex string) data into abstract representation.
@@ -713,6 +727,9 @@
             return method(raw_bin_data)
         if self._construct:
             return parse_construct(self._construct, raw_bin_data)
+        elif self._tlv:
+            self._tlv.from_tlv(raw_bin_data)
+            return self._tlv.to_dict()
         return {'raw': raw_bin_data.hex()}
 
     def decode_record_bin(self, raw_bin_data:bytearray) -> dict:
@@ -736,6 +753,9 @@
             return method(raw_hex_data)
         if self._construct:
             return parse_construct(self._construct, raw_bin_data)
+        elif self._tlv:
+            self._tlv.from_tlv(raw_bin_data)
+            return self._tlv.to_dict()
         return {'raw': raw_hex_data}
 
     def encode_record_hex(self, abstract_data:dict) -> str:
@@ -759,6 +779,9 @@
             return b2h(raw_bin_data)
         if self._construct:
             return b2h(self._construct.build(abstract_data))
+        elif self._tlv:
+            self._tlv.from_dict(abstract_data)
+            return b2h(self._tlv.to_tlv())
         raise NotImplementedError
 
     def encode_record_bin(self, abstract_data:dict) -> bytearray:
@@ -781,6 +804,9 @@
             return h2b(method(abstract_data))
         if self._construct:
             return self._construct.build(abstract_data)
+        elif self._tlv:
+            self._tlv.from_dict(abstract_data)
+            return self._tlv.to_tlv()
         raise NotImplementedError
 
 class CyclicEF(LinFixedEF):
@@ -835,6 +861,9 @@
             return method(raw_bin_data)
         if self._construct:
             return parse_construct(self._construct, raw_bin_data)
+        elif self._tlv:
+            self._tlv.from_tlv(raw_bin_data)
+            return self._tlv.to_dict()
         return {'raw': raw_hex_data}
 
     def decode_record_bin(self, raw_bin_data:bytearray) -> dict:
@@ -858,6 +887,9 @@
             return method(raw_hex_data)
         if self._construct:
             return parse_construct(self._construct, raw_bin_data)
+        elif self._tlv:
+            self._tlv.from_tlv(raw_bin_data)
+            return self._tlv.to_dict()
         return {'raw': raw_hex_data}
 
     def encode_record_hex(self, abstract_data:dict) -> str:
@@ -880,6 +912,9 @@
             return b2h(method(abstract_data))
         if self._construct:
             return b2h(filter_dict(self._construct.build(abstract_data)))
+        elif self._tlv:
+            self._tlv.from_dict(abstract_data)
+            return b2h(self._tlv.to_tlv())
         raise NotImplementedError
 
     def encode_record_bin(self, abstract_data:dict) -> bytearray:
@@ -902,6 +937,9 @@
             return h2b(method(abstract_data))
         if self._construct:
             return filter_dict(self._construct.build(abstract_data))
+        elif self._tlv:
+            self._tlv.from_dict(abstract_data)
+            return self._tlv.to_tlv()
         raise NotImplementedError
 
     def _decode_bin(self, raw_bin_data:bytearray):
diff --git a/pySim/tlv.py b/pySim/tlv.py
index 3b27b5e..05f824e 100644
--- a/pySim/tlv.py
+++ b/pySim/tlv.py
@@ -338,6 +338,8 @@
         while len(remainder):
             # obtain the tag at the start of the remainder
             tag, r = first._parse_tag_raw(remainder)
+            if tag == None:
+                return res
             if tag in self.members_by_tag:
                 cls = self.members_by_tag[tag]
                 # create an instance and parse accordingly

-- 
To view, visit https://gerrit.osmocom.org/c/pysim/+/24454
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I59f456b4223ec88081e91cee168b654c69bcb5f4
Gerrit-Change-Number: 24454
Gerrit-PatchSet: 5
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter <pmaier at sysmocom.de>
Gerrit-Reviewer: fixeria <vyanitskiy at sysmocom.de>
Gerrit-Reviewer: laforge <laforge at osmocom.org>
Gerrit-Reviewer: pespin <pespin at sysmocom.de>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210605/ceb66038/attachment.htm>


More information about the gerrit-log mailing list