laforge has uploaded this change for review. ( 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, 10 insertions(+), 1 deletion(-)
git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/32/41732/1
diff --git a/pySim/ts_51_011.py b/pySim/ts_51_011.py index f87683f..dcac60b 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 @@ -288,7 +289,8 @@
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: bytes_for_nibbles(len(ctx.call_number)) + 1), + '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."""