diff options
author | Harald Welte <laforge@osmocom.org> | 2021-02-01 17:51:56 +0100 |
---|---|---|
committer | Harald Welte <laforge@osmocom.org> | 2021-03-03 08:37:50 +0100 |
commit | 4f6ca43e1f6726f00cfc91ff6d17db6878316c4d (patch) | |
tree | 8c93b64d182c25266c7481211df75b101a9b0b21 | |
parent | 85484a977d46c9fa27c05b7a81ee02c5c1dffa35 (diff) |
start using python3 bytearray for our b2h/h2b types
The code was written long ago, when the python3 bytearray type
probably didn't exist yet, or was at least not known. Let's stop
using string types with binary bytes inside, and instead standardize
on two types:
* bytearray for binary data
* string for hexadecimal nibbles representing that binary data
Change-Id: I8aca84b6280f9702b0e2aba2c9759b4f312ab6a9
-rw-r--r-- | pySim/cards.py | 2 | ||||
-rw-r--r-- | pySim/utils.py | 18 |
2 files changed, 12 insertions, 8 deletions
diff --git a/pySim/cards.py b/pySim/cards.py index 61d2624..8b51787 100644 --- a/pySim/cards.py +++ b/pySim/cards.py @@ -631,7 +631,7 @@ class FakeMagicSim(Card): # Set first entry entry = ( '81' + # 1b Status: Valid & Active - rpad(b2h(p['name'][0:14]), 28) + # 14b Entry Name + rpad(s2h(p['name'][0:14]), 28) + # 14b Entry Name enc_iccid(p['iccid']) + # 10b ICCID enc_imsi(p['imsi']) + # 9b IMSI_len + id_type(9) + IMSI p['ki'] + # 16b Ki diff --git a/pySim/utils.py b/pySim/utils.py index bfa147b..5320b59 100644 --- a/pySim/utils.py +++ b/pySim/utils.py @@ -22,10 +22,12 @@ def h2b(s): - return ''.join([chr((int(x,16)<<4)+int(y,16)) for x,y in zip(s[0::2], s[1::2])]) + """convert from a string of hex nibbles to a sequence of bytes""" + return bytearray.fromhex(s) -def b2h(s): - return ''.join(['%02x'%ord(x) for x in s]) +def b2h(b): + """convert from a sequence of bytes to a string of hex nibbles""" + return ''.join(['%02x'%(x) for x in b]) def h2i(s): return [(int(x,16)<<4)+int(y,16) for x,y in zip(s[0::2], s[1::2])] @@ -38,7 +40,9 @@ def h2s(s): if int(x + y, 16) != 0xff]) def s2h(s): - return b2h(s) + b = bytearray() + b.extend(map(ord, s)) + return b2h(b) # List of bytes to string def i2s(s): @@ -334,7 +338,7 @@ def dec_msisdn(ef_msisdn): msisdn_lhv = ef_msisdn[xlen:] # Parse the length (in bytes) of the BCD encoded number - bcd_len = ord(msisdn_lhv[0]) + bcd_len = msisdn_lhv[0] # BCD length = length of dial num (max. 10 bytes) + 1 byte ToN and NPI if bcd_len == 0xff: return None @@ -342,8 +346,8 @@ def dec_msisdn(ef_msisdn): raise ValueError("Length of MSISDN (%d bytes) is out of range" % bcd_len) # Parse ToN / NPI - ton = (ord(msisdn_lhv[1]) >> 4) & 0x07 - npi = ord(msisdn_lhv[1]) & 0x0f + ton = (msisdn_lhv[1] >> 4) & 0x07 + npi = msisdn_lhv[1] & 0x0f bcd_len -= 1 # No MSISDN? |