From 42804d7803eab3bba11e6503892e8202ffff4bb0 Mon Sep 17 00:00:00 2001 From: Philipp Maier Date: Fri, 30 Apr 2021 11:56:23 +0200 Subject: [PATCH] commands: pad short data input in update_record() The method update_record as a "force_len" parameter, which is somewhat irretatating. Some explainatory comments and a reformat of the if statement will help to make it more understandable to the api user. In the non force_len case the method determines the record length from the select response and throws an exception if the data input does not match that length. This makes sense if the data input exceeds the record length of the file but if the data input is less then the record length the situation is fixable by padding the input with 0xff. This also a quite common case because in some situation it is not guaranteed that the data will fill the entire record. Change-Id: I9a5df0e46c3dd2e87d447c5c01cf15844b0eed07 Related: OS#4963 --- pySim/commands.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/pySim/commands.py b/pySim/commands.py index 83ad167b..48c66bd2 100644 --- a/pySim/commands.py +++ b/pySim/commands.py @@ -216,13 +216,20 @@ class SimCardCommands(object): def update_record(self, ef, rec_no:int, data:str, force_len:bool=False, verify:bool=False, conserve:bool=False): - r = self.select_path(ef) - if not force_len: - rec_length = self.__record_len(r) - if (len(data) // 2 != rec_length): - raise ValueError('Invalid data length (expected %d, got %d)' % (rec_length, len(data) // 2)) - else: + res = self.select_path(ef) + + if force_len: + # enforce the record length by the actual length of the given data input rec_length = len(data) // 2 + else: + # determine the record length from the select response of the file and pad + # the input data with 0xFF if necessary. In cases where the input data + # exceed we throw an exception. + rec_length = self.__record_len(res) + if (len(data) // 2 > rec_length): + raise ValueError('Data length exceeds record length (expected max %d, got %d)' % (rec_length, len(data) // 2)) + elif (len(data) // 2 < rec_length): + data = rpad(data, rec_length * 2) # Save write cycles by reading+comparing before write if conserve: