diff options
author | Alexander Chemeris <Alexander.Chemeris@gmail.com> | 2013-07-10 11:18:06 +0400 |
---|---|---|
committer | Sylvain Munaut <tnt@246tNt.com> | 2013-07-13 08:53:21 +0200 |
commit | 7be92ff5d254fb8cedbe83c518cd1ff77543d3dd (patch) | |
tree | cf1fd9df66215c1cb81bde282a4e7a86a775174a /pySim/utils.py | |
parent | 6e58914746af82314361fc9d9eae6cecbbe2e64b (diff) |
Move encoder functions for ICCID, IMSI and PLMN to pySim.utils for consistency.
Diffstat (limited to 'pySim/utils.py')
-rw-r--r-- | pySim/utils.py | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/pySim/utils.py b/pySim/utils.py index 12ae94e..011bd05 100644 --- a/pySim/utils.py +++ b/pySim/utils.py @@ -43,6 +43,13 @@ def rpad(s, l, c='f'): def lpad(s, l, c='f'): return c * (l - len(s)) + s +def enc_imsi(imsi): + """Converts a string imsi into the value of the EF""" + l = (len(imsi) + 1) // 2 # Required bytes + oe = len(imsi) & 1 # Odd (1) / Even (0) + ei = '%02x' % l + swap_nibbles(lpad('%01x%s' % ((oe<<3)|1, imsi), 16)) + return ei + def dec_imsi(ef): """Converts an EF value to the imsi string representation""" if len(ef) < 4: @@ -59,3 +66,10 @@ def dec_imsi(ef): def dec_iccid(ef): return swap_nibbles(ef).strip('f') + +def enc_iccid(iccid): + return swap_nibbles(rpad(iccid, 20)) + +def enc_plmn(mcc, mnc): + """Converts integer MCC/MNC into 6 bytes for EF""" + return swap_nibbles(lpad('%d' % mcc, 3) + lpad('%d' % mnc, 3)) |