From 6bf2d5f21609547537f7fd9eb58d3b1be74ab418 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Tue, 24 Jan 2023 15:54:26 +0100 Subject: [PATCH] gsm_r: EF_Predefined: Decode first record different from others In their infinite wisdom, the authors of the EIRENE FFFIS for GSM-R SIM cards invented yet a new way of encoding data in SIM card files: The first record of a file may be encoded differently than further records of files. This patch implements the feature based on the newly-introduced way by which we pass the record number to the encoder and decoder methods. Change-Id: Ib526f6c3c2ac9a945b8242e2e54536628376efc0 Related: OS#5784 --- pySim/gsm_r.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/pySim/gsm_r.py b/pySim/gsm_r.py index 5ddb04b9..347c4e68 100644 --- a/pySim/gsm_r.py +++ b/pySim/gsm_r.py @@ -237,17 +237,29 @@ class EF_Switching(LinFixedEF): class EF_Predefined(LinFixedEF): """Section 8.5""" + # header and other records have different structure. WTF !?! + construct_first = Struct('next_table_type'/NextTableType, + 'id_of_next_table'/HexAdapter(Bytes(2))) + construct_others = Struct('predefined_value1'/HexAdapter(Bytes(2)), + 'string_table_index1'/Int8ub) def __init__(self, fid, name, desc): super().__init__(fid=fid, sfid=None, name=name, desc=desc, rec_len=(3, 3)) - # header and other records have different structure. WTF !?! - self._construct = Struct('next_table_type'/NextTableType, - 'id_of_next_table'/HexAdapter(Bytes(2)), - 'predefined_value1'/HexAdapter(Bytes(2)), - 'string_table_index1'/Int8ub) - # TODO: predefined value n, ... + def _decode_record_bin(self, raw_bin_data : bytes, record_nr : int) -> dict: + if record_nr == 1: + return parse_construct(self.construct_first, raw_bin_data) + else: + return parse_construct(self.construct_others, raw_bin_data) + + def _encode_record_bin(self, abstract_data : dict, record_nr : int) -> bytearray: + r = None + if record_nr == 1: + r = self.construct_first.build(abstract_data) + else: + r = self.construct_others.build(abstract_data) + return filter_dict(r) class EF_DialledVals(TransparentEF): """Section 8.6"""