From 5e96c3d910fbec87f699d16ff25b87018a4a687a Mon Sep 17 00:00:00 2001 From: Alexander Chemeris Date: Thu, 4 Jul 2013 17:33:33 +0400 Subject: [PATCH] utils: Add functions to decode IMSI and ICCID from EF raw data. --- pySim/utils.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pySim/utils.py b/pySim/utils.py index d8b9518c..12ae94eb 100644 --- a/pySim/utils.py +++ b/pySim/utils.py @@ -42,3 +42,20 @@ def rpad(s, l, c='f'): def lpad(s, l, c='f'): return c * (l - len(s)) + s + +def dec_imsi(ef): + """Converts an EF value to the imsi string representation""" + if len(ef) < 4: + return None + l = int(ef[0:2]) * 2 # Length of the IMSI string + swapped = swap_nibbles(ef[2:]) + oe = (int(swapped[0])>>3) & 1 # Odd (1) / Even (0) + if oe: + l = l-1 + if l+1 > len(swapped): + return None + imsi = swapped[1:l+2] + return imsi + +def dec_iccid(ef): + return swap_nibbles(ef).strip('f')