Change in pysim[master]: commands: pad short data input in update_record()

dexter gerrit-no-reply at lists.osmocom.org
Fri Apr 30 16:20:40 UTC 2021


dexter has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/24008 )


Change subject: commands: pad short data input in update_record()
......................................................................

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
---
M pySim/commands.py
1 file changed, 12 insertions(+), 6 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/08/24008/1

diff --git a/pySim/commands.py b/pySim/commands.py
index 33aec12..df00737 100644
--- a/pySim/commands.py
+++ b/pySim/commands.py
@@ -216,13 +216,19 @@
 
 	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:
+		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.
+			res = self.select_path(ef)
+			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)
 
 		# Save write cycles by reading+comparing before write
 		if conserve:

-- 
To view, visit https://gerrit.osmocom.org/c/pysim/+/24008
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I9a5df0e46c3dd2e87d447c5c01cf15844b0eed07
Gerrit-Change-Number: 24008
Gerrit-PatchSet: 1
Gerrit-Owner: dexter <pmaier at sysmocom.de>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210430/2d3e9cbe/attachment.htm>


More information about the gerrit-log mailing list