Change in pysim[master]: utils: fix mcc/mnc encoding in dec_plmn (EF_PLMNsel)

dexter gerrit-no-reply at lists.osmocom.org
Fri Apr 30 15:05:54 UTC 2021


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


Change subject: utils: fix mcc/mnc encoding in dec_plmn (EF_PLMNsel)
......................................................................

utils: fix mcc/mnc encoding in dec_plmn (EF_PLMNsel)

The dec_plmn function takes an hexstring and returns the decoded MCC and
MNC as integer values. The result is then used by the json encoder in
EF_PLMNsel, which means the json output will contrary to the input, use
integer values instead of strings.

This is not correct since there may be leading zeros (e.g. mnc 01 and
001 both exist are different) which must be retained in order to know
the correct length of the MNC.

Related: OS#4963
Change-Id: I393e04836814d992d2a6d0a4e4e01850976d6e81
---
M pySim/utils.py
M tests/test_utils.py
2 files changed, 55 insertions(+), 5 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/99/23999/1

diff --git a/pySim/utils.py b/pySim/utils.py
index 6034da4..ac28624 100644
--- a/pySim/utils.py
+++ b/pySim/utils.py
@@ -136,14 +136,35 @@
 
 def enc_plmn(mcc:str, mnc:str):
 	"""Converts integer MCC/MNC into 3 bytes for EF"""
-	if len(mnc) == 2:
-		mnc += "F" # pad to 3 digits if needed
+
+	# Make sure there are no excess whitespaces in the input
+	# parameters
+	mcc = mcc.strip()
+	mnc = mnc.strip()
+
+	# Make sure that MCC/MNC are correctly padded with leading
+	# zeros or 'F', depending on the length.
+	if len(mnc) == 0:
+		mnc = "FFF"
+	elif len(mnc) == 1:
+		mnc = "F0" + mnc
+	elif len(mnc) == 2:
+		mnc += "F"
+
+	if len(mcc) == 0:
+		mcc = "FFF"
+	elif len(mcc) == 1:
+		mcc = "00" + mcc
+	elif len(mcc) == 2:
+		mcc = "0" + mcc
+
 	return (mcc[1] + mcc[0]) + (mnc[2] + mcc[2]) + (mnc[1] + mnc[0])
 
 def dec_plmn(threehexbytes:Hexstr) -> dict:
-	res = {'mcc': 0, 'mnc': 0 }
-	res['mcc'] = dec_mcc_from_plmn(threehexbytes)
-	res['mnc'] = dec_mnc_from_plmn(threehexbytes)
+	res = {'mcc': "0", 'mnc': "0" }
+	dec_mcc_from_plmn_str(threehexbytes)
+	res['mcc'] = dec_mcc_from_plmn_str(threehexbytes)
+	res['mnc'] = dec_mnc_from_plmn_str(threehexbytes)
 	return res
 
 def dec_spn(ef):
@@ -172,6 +193,13 @@
 		return 0xFFF # 4095
 	return derive_mcc(digit1, digit2, digit3)
 
+def dec_mcc_from_plmn_str(plmn:Hexstr) -> str:
+	digit1 = plmn[1] # 1st byte, LSB
+	digit2 = plmn[0] # 1st byte, MSB
+	digit3 = plmn[3] # 2nd byte, LSB
+	res = digit1 + digit2 + digit3
+	return res.upper().strip("F")
+
 def dec_mnc_from_plmn(plmn:Hexstr) -> int:
 	ia = h2i(plmn)
 	digit1 = ia[2] & 0x0F		# 3rd byte, LSB
@@ -181,6 +209,13 @@
 		return 0xFFF # 4095
 	return derive_mnc(digit1, digit2, digit3)
 
+def dec_mnc_from_plmn_str(plmn:Hexstr) -> str:
+	digit1 = plmn[5] # 3rd byte, LSB
+	digit2 = plmn[4] # 3rd byte, MSB
+	digit3 = plmn[2] # 2nd byte, MSB
+	res = digit1 + digit2 + digit3
+	return res.upper().strip("F")
+
 def dec_act(twohexbytes:Hexstr) -> List[str]:
 	act_list = [
 		{'bit': 15, 'name': "UTRAN"},
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 71c0eb0..b70b17b 100755
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -37,6 +37,12 @@
 	def testDecMCCfromPLMN_unused(self):
 		self.assertEqual(utils.dec_mcc_from_plmn("ff0f00"), 4095)
 
+	def testDecMCCfromPLMN_str(self):
+		self.assertEqual(utils.dec_mcc_from_plmn_str("92f501"), "295")
+
+	def testDecMCCfromPLMN_unused_str(self):
+		self.assertEqual(utils.dec_mcc_from_plmn_str("ff0f00"), "")
+
 	def testDecMNCfromPLMN_twoDigitMNC(self):
 		self.assertEqual(utils.dec_mnc_from_plmn("92f501"), 10)
 
@@ -46,6 +52,15 @@
 	def testDecMNCfromPLMN_unused(self):
 		self.assertEqual(utils.dec_mnc_from_plmn("00f0ff"), 4095)
 
+	def testDecMNCfromPLMN_twoDigitMNC_str(self):
+		self.assertEqual(utils.dec_mnc_from_plmn_str("92f501"), "10")
+
+	def testDecMNCfromPLMN_threeDigitMNC_str(self):
+		self.assertEqual(utils.dec_mnc_from_plmn_str("031263"), "361")
+
+	def testDecMNCfromPLMN_unused_str(self):
+		self.assertEqual(utils.dec_mnc_from_plmn_str("00f0ff"), "")
+
 	def test_enc_plmn(self):
 		with self.subTest("2-digit MCC"):
 			self.assertEqual(utils.enc_plmn("001", "01F"), "00F110")

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

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I393e04836814d992d2a6d0a4e4e01850976d6e81
Gerrit-Change-Number: 23999
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/7357facb/attachment.htm>


More information about the gerrit-log mailing list