From 1da34c1a4f0d178117aba063f16bcdf161206573 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Fri, 19 Dec 2025 18:38:38 +0100 Subject: [PATCH] Fix more odd-length digit sequences via PaddedBcdAdapter There are more files where trailing digits are indicated using 'f' and should be stripped during decode, including EF.MSISDN and EF.VGCS This is not just a presentation issue, but actually rendered wrong data before, see the modified test output where our "read_record_uicc.ok" file contained "bcd_len: 7" but then only 6 BCD digits due to this bug. Change-Id: I4571482da924a3d645caa297108279d182448d21 --- pySim/ts_31_102.py | 6 +++--- pySim/ts_51_011.py | 12 ++++++------ .../file_content/test_record_uicc.ok | 8 ++++---- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/pySim/ts_31_102.py b/pySim/ts_31_102.py index 3ae178ce..a93f1e85 100644 --- a/pySim/ts_31_102.py +++ b/pySim/ts_31_102.py @@ -596,7 +596,7 @@ class EF_ICI(CyclicEF): self._construct = Struct('alpha_id'/Bytes(this._.total_len-28), 'len_of_bcd_contents'/Int8ub, 'ton_npi'/Int8ub, - 'call_number'/BcdAdapter(Bytes(10)), + 'call_number'/PaddedBcdAdapter(Rpad(Bytes(10))), 'cap_cfg2_record_id'/Int8ub, 'ext5_record_id'/Int8ub, 'date_and_time'/BcdAdapter(Bytes(7)), @@ -612,7 +612,7 @@ class EF_OCI(CyclicEF): self._construct = Struct('alpha_id'/Bytes(this._.total_len-27), 'len_of_bcd_contents'/Int8ub, 'ton_npi'/Int8ub, - 'call_number'/BcdAdapter(Bytes(10)), + 'call_number'/PaddedBcdAdapter(Rpad(Bytes(10))), 'cap_cfg2_record_id'/Int8ub, 'ext5_record_id'/Int8ub, 'date_and_time'/BcdAdapter(Bytes(7)), @@ -1118,7 +1118,7 @@ class EF_Routing_Indicator(TransparentEF): # responsibility of home network operator but BCD coding shall be used. If a network # operator decides to assign less than 4 digits to Routing Indicator, the remaining digits # shall be coded as "1111" to fill the 4 digits coding of Routing Indicator - self._construct = Struct('routing_indicator'/Rpad(BcdAdapter(Bytes(2)), 'f', 2), + self._construct = Struct('routing_indicator'/PaddedBcdAdapter(Rpad(Bytes(2))), 'rfu'/Bytes(2)) # TS 31.102 Section 4.4.11.13 (Rel 16) diff --git a/pySim/ts_51_011.py b/pySim/ts_51_011.py index d37715f8..70c38b18 100644 --- a/pySim/ts_51_011.py +++ b/pySim/ts_51_011.py @@ -152,7 +152,7 @@ class EF_ADN(LinFixedEF): self._construct = Struct('alpha_id'/COptional(GsmOrUcs2Adapter(Rpad(Bytes(this._.total_len-14)))), 'len_of_bcd'/Int8ub, 'ton_npi'/TonNpi, - 'dialing_nr'/ExtendedBcdAdapter(BcdAdapter(Rpad(Bytes(10)))), + 'dialing_nr'/ExtendedBcdAdapter(PaddedBcdAdapter(Rpad(Bytes(10)))), 'cap_conf_id'/Int8ub, ext_name/Int8ub) @@ -193,11 +193,11 @@ class EF_MSISDN(LinFixedEF): ( 'ffffffffffffffffffffffffffffffffffffffff04b12143f5ffffffffffffffffff', {"alpha_id": "", "len_of_bcd": 4, "ton_npi": {"ext": True, "type_of_number": "network_specific", "numbering_plan_id": "isdn_e164"}, - "dialing_nr": "12345f"}), + "dialing_nr": "12345"}), ( '456967656e65205275666e756d6d6572ffffffff0891947172199181f3ffffffffff', {"alpha_id": "Eigene Rufnummer", "len_of_bcd": 8, "ton_npi": {"ext": True, "type_of_number": "international", "numbering_plan_id": "isdn_e164"}, - "dialing_nr": "4917279119183f"}), + "dialing_nr": "4917279119183"}), ] # Ensure deprecated representations still work @@ -215,7 +215,7 @@ class EF_MSISDN(LinFixedEF): self._construct = Struct('alpha_id'/COptional(GsmOrUcs2Adapter(Rpad(Bytes(this._.total_len-14)))), 'len_of_bcd'/Int8ub, 'ton_npi'/TonNpi, - 'dialing_nr'/ExtendedBcdAdapter(BcdAdapter(Rpad(Bytes(10)))), + 'dialing_nr'/ExtendedBcdAdapter(PaddedBcdAdapter(Rpad(Bytes(10)))), Padding(2, pattern=b'\xff')) # Maintain compatibility with deprecated representations @@ -661,12 +661,12 @@ class EF_AD(TransparentEF): # TS 51.011 Section 10.3.20 / 10.3.22 class EF_VGCS(TransRecEF): _test_de_encode = [ - ( "92f9ffff", "299fffff" ), + ( "92f9ffff", "299" ), ] def __init__(self, fid='6fb1', sfid=None, name='EF.VGCS', size=(4, 200), rec_len=4, desc='Voice Group Call Service', **kwargs): super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len, **kwargs) - self._construct = BcdAdapter(Bytes(4)) + self._construct = PaddedBcdAdapter(Rpad(Bytes(4))) # TS 51.011 Section 10.3.21 / 10.3.23 class EF_VGCSS(TransparentEF): diff --git a/tests/pySim-shell_test/file_content/test_record_uicc.ok b/tests/pySim-shell_test/file_content/test_record_uicc.ok index 14183ebb..5a4e7472 100644 --- a/tests/pySim-shell_test/file_content/test_record_uicc.ok +++ b/tests/pySim-shell_test/file_content/test_record_uicc.ok @@ -19,7 +19,7 @@ "type_of_number": "reserved_for_extension", "numbering_plan_id": "reserved_for_extension" }, - "dialing_nr": "123456", + "dialing_nr": "1234567", "cap_conf_id": 42, "ext4_record_id": 23 }, @@ -67,7 +67,7 @@ "type_of_number": "reserved_for_extension", "numbering_plan_id": "reserved_for_extension" }, - "dialing_nr": "123456", + "dialing_nr": "1234567", "cap_conf_id": 42, "ext4_record_id": 23 }, @@ -127,7 +127,7 @@ "type_of_number": "reserved_for_extension", "numbering_plan_id": "reserved_for_extension" }, - "dialing_nr": "123456", + "dialing_nr": "1234567", "cap_conf_id": 42, "ext4_record_id": 23 } @@ -140,7 +140,7 @@ "type_of_number": "reserved_for_extension", "numbering_plan_id": "reserved_for_extension" }, - "dialing_nr": "123456", + "dialing_nr": "1234567", "cap_conf_id": 42, "ext4_record_id": 23 }