mirror of
https://gitea.osmocom.org/sim-card/pysim.git
synced 2026-03-17 02:48:34 +03:00
utils: fix encoding of EF.MSISDN
The encoding of EF.MSISDN is a bit unstrutured. The encoder function does not return a valid result since it lacks the parameters Capability/Configuration2 Record Identifier and Extension5 Record Identifier, which are mandatory but can be set to 0xFF. Also the encoder gets its input from pySim-shell, so it should have some more input validation, especially when the user encodes an empty string. The encoder and decoder function also do not have unit-tests. Since the encoder now adds the missing two bytes by isself this does not have to be done manually anymore, so cards.py needs to be re-aligned. For pySim-shell.py the encoder is used from ts_51_011.py. Unfortunately it is used wrongly there. The optional Alpha Identifier is required here as well. Related: OS#4963 Change-Id: Iee5369b3e3ba7fa1155facc8fa824bc60e33b55b
This commit is contained in:
@@ -400,18 +400,27 @@ def dec_msisdn(ef_msisdn:Hexstr) -> Optional[Tuple[int,int,Optional[str]]]:
|
||||
def enc_msisdn(msisdn:str, npi:int=0x01, ton:int=0x03) -> Hexstr:
|
||||
"""
|
||||
Encode MSISDN as LHV so it can be stored to EF.MSISDN.
|
||||
See 3GPP TS 31.102, section 4.2.26 and 4.4.2.3.
|
||||
See 3GPP TS 31.102, section 4.2.26 and 4.4.2.3. (The result
|
||||
will not contain the optional Alpha Identifier at the beginning.)
|
||||
|
||||
Default NPI / ToN values:
|
||||
- NPI: ISDN / telephony numbering plan (E.164 / E.163),
|
||||
- ToN: network specific or international number (if starts with '+').
|
||||
"""
|
||||
|
||||
# If no MSISDN is supplied then encode the file contents as all "ff"
|
||||
if msisdn == "" or msisdn == "+":
|
||||
return "ff" * 14
|
||||
|
||||
# Leading '+' indicates International Number
|
||||
if msisdn[0] == '+':
|
||||
msisdn = msisdn[1:]
|
||||
ton = 0x01
|
||||
|
||||
# An MSISDN must not exceed 20 digits
|
||||
if len(msisdn) > 20:
|
||||
raise ValueError("msisdn must not be longer than 20 digits")
|
||||
|
||||
# Append 'f' padding if number of digits is odd
|
||||
if len(msisdn) % 2 > 0:
|
||||
msisdn += 'f'
|
||||
@@ -421,7 +430,8 @@ def enc_msisdn(msisdn:str, npi:int=0x01, ton:int=0x03) -> Hexstr:
|
||||
npi_ton = (npi & 0x0f) | ((ton & 0x07) << 4) | 0x80
|
||||
bcd = rpad(swap_nibbles(msisdn), 10 * 2) # pad to 10 octets
|
||||
|
||||
return ('%02x' % bcd_len) + ('%02x' % npi_ton) + bcd
|
||||
return ('%02x' % bcd_len) + ('%02x' % npi_ton) + bcd + ("ff" * 2)
|
||||
|
||||
|
||||
def dec_st(st, table="sim") -> str:
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user