laforge has submitted this change. ( https://gerrit.osmocom.org/c/pysim/+/41732?usp=email )
Change subject: pySim/ts_51_011: Properly re-compute ScAddr length ......................................................................
pySim/ts_51_011: Properly re-compute ScAddr length
EF.SMSP contains up to two addresses: Both are stored in a fixed-length field of 12 octets. However, the actually used size depends on the number of digits in the respective number. Let's compute that length field properly
Change-Id: Idef54a3545d1a5367a1efa2f0a6f7f0c1f860105 --- M pySim/ts_51_011.py M pySim/utils.py 2 files changed, 18 insertions(+), 1 deletion(-)
Approvals: laforge: Looks good to me, approved fixeria: Looks good to me, but someone else must approve Jenkins Builder: Verified
diff --git a/pySim/ts_51_011.py b/pySim/ts_51_011.py index f87683f..0161f7d 100644 --- a/pySim/ts_51_011.py +++ b/pySim/ts_51_011.py @@ -40,6 +40,7 @@ from osmocom.construct import *
from pySim.utils import dec_iccid, enc_iccid, dec_imsi, enc_imsi, dec_plmn, enc_plmn, dec_xplmn_w_act +from pySim.utils import bytes_for_nibbles from pySim.profile import CardProfile, CardProfileAddon from pySim.filesystem import * from pySim.ts_31_102_telecom import DF_PHONEBOOK, DF_MULTIMEDIA, DF_MCS, DF_V2X @@ -286,9 +287,18 @@ else: raise ValueError
+ @staticmethod + def sc_addr_len(ctx): + """Compute the length field for an address field (like TP-DestAddr or TP-ScAddr).""" + if not hasattr(ctx, 'call_number') or len(ctx.call_number) == 0: + return 0xff + else: + return bytes_for_nibbles(len(ctx.call_number)) + 1 + def __init__(self, fid='6f42', sfid=None, name='EF.SMSP', desc='Short message service parameters', **kwargs): super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=(28, None), **kwargs) - ScAddr = Struct('length'/Int8ub, 'ton_npi'/TonNpi, 'call_number'/BcdAdapter(Rpad(Bytes(10)))) + ScAddr = Struct('length'/Rebuild(Int8ub, lambda ctx: EF_SMSP.sc_addr_len(ctx)), + 'ton_npi'/TonNpi, 'call_number'/BcdAdapter(Rpad(Bytes(10)))) self._construct = Struct('alpha_id'/COptional(GsmOrUcs2Adapter(Rpad(Bytes(this._.total_len-28)))), 'parameter_indicators'/InvertAdapter(BitStruct( Const(7, BitsInteger(3)), diff --git a/pySim/utils.py b/pySim/utils.py index a8fd30c..7d8e3fa 100644 --- a/pySim/utils.py +++ b/pySim/utils.py @@ -526,6 +526,13 @@ # no change return hexstring
+def bytes_for_nibbles(num_nibbles: int) -> int: + """compute the number of bytes needed to store the given number of nibbles.""" + n_bytes = num_nibbles // 2 + if num_nibbles & 1: + n_bytes += 1 + return n_bytes +
def boxed_heading_str(heading, width=80): """Generate a string that contains a boxed heading."""