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

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

laforge gerrit-no-reply at lists.osmocom.org
Sat May 29 20:42:28 UTC 2021


laforge has uploaded this change for review. ( 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(-)



  git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/54/24454/1

diff --git a/pySim/filesystem.py b/pySim/filesystem.py
index 657a1f2..e7aac5d 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 3bb1828..f22e155 100644
--- a/pySim/tlv.py
+++ b/pySim/tlv.py
@@ -337,6 +337,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: 1
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210529/972d158b/attachment.htm>


More information about the gerrit-log mailing list