aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSupreeth Herle <herlesupreeth@gmail.com>2020-05-11 09:04:41 +0200
committerSupreeth Herle <herlesupreeth@gmail.com>2020-05-11 09:04:41 +0200
commit9837055f4f2153732a1006820f84ce439152e918 (patch)
tree467c98a65aedd0dbfaa98118e3ff00392f3ef4ed
parent32f0d415afcdeb53f712e18b7e51d50f0b6efaa6 (diff)
Import TLV parsing related function from https://github.com/mitshell/card
The functions are imported from the git commit 2a81963790e27eb6b188359af169c45afb6d3aaf from master branch Change-Id: I5c7fdbd122e696d272f7480785d0c17ad2af138c
-rw-r--r--pySim/utils.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/pySim/utils.py b/pySim/utils.py
index 1980685..eb7a56b 100644
--- a/pySim/utils.py
+++ b/pySim/utils.py
@@ -359,3 +359,42 @@ def dec_st(st, table="sim"):
avail_st += '\tService %d - %s\n' % ((8*i) + j, lookup_map[(8*i) + j])
byte = byte >> 1
return avail_st
+
+def first_TLV_parser(bytelist):
+ '''
+ first_TLV_parser([0xAA, 0x02, 0xAB, 0xCD, 0xFF, 0x00]) -> (170, 2, [171, 205])
+
+ parses first TLV format record in a list of bytelist
+ returns a 3-Tuple: Tag, Length, Value
+ Value is a list of bytes
+ parsing of length is ETSI'style 101.220
+ '''
+ Tag = bytelist[0]
+ if bytelist[1] == 0xFF:
+ Len = bytelist[2]*256 + bytelist[3]
+ Val = bytelist[4:4+Len]
+ else:
+ Len = bytelist[1]
+ Val = bytelist[2:2+Len]
+ return (Tag, Len, Val)
+
+def TLV_parser(bytelist):
+ '''
+ TLV_parser([0xAA, ..., 0xFF]) -> [(T, L, [V]), (T, L, [V]), ...]
+
+ loops on the input list of bytes with the "first_TLV_parser()" function
+ returns a list of 3-Tuples
+ '''
+ ret = []
+ while len(bytelist) > 0:
+ T, L, V = first_TLV_parser(bytelist)
+ if T == 0xFF:
+ # padding bytes
+ break
+ ret.append( (T, L, V) )
+ # need to manage length of L
+ if L > 0xFE:
+ bytelist = bytelist[ L+4 : ]
+ else:
+ bytelist = bytelist[ L+2 : ]
+ return ret