aboutsummaryrefslogtreecommitdiffstats
path: root/pySim
diff options
context:
space:
mode:
authorHarald Welte <laforge@osmocom.org>2021-01-21 14:50:01 +0100
committerHarald Welte <laforge@osmocom.org>2021-01-21 16:11:39 +0100
commit14b2e2c9dbd826116cb5aab8d12981c1bf6c96f1 (patch)
tree65e2a3e4dfc290e858a6ccfe78ad9738142f67fc /pySim
parentfd72b95dd6d4c5572b7826ce5889aac863a8a543 (diff)
move SW matching to a generic utility function
This will allow using it outside the transport/__init__.py Change-Id: Id26dfefa85d91e3b3a23e0049f3b833e29cb1cef
Diffstat (limited to 'pySim')
-rw-r--r--pySim/transport/__init__.py13
-rw-r--r--pySim/utils.py14
2 files changed, 17 insertions, 10 deletions
diff --git a/pySim/transport/__init__.py b/pySim/transport/__init__.py
index 398c49f..d3a5ba9 100644
--- a/pySim/transport/__init__.py
+++ b/pySim/transport/__init__.py
@@ -5,6 +5,7 @@
"""
from pySim.exceptions import *
+from pySim.utils import sw_match
#
# Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com>
@@ -94,14 +95,6 @@ class LinkBase(object):
"""
rv = self.send_apdu(pdu)
- # Create a masked version of the returned status word
- sw_masked = ""
- for i in range(0, 4):
- if sw.lower()[i] == '?':
- sw_masked = sw_masked + '?'
- else:
- sw_masked = sw_masked + rv[1][i].lower()
-
- if sw.lower() != sw_masked:
- raise SwMatchError(sw.lower(), rv[1])
+ if not sw_match(rv[1], pattern=sw):
+ raise SwMatchError(rv[1], sw.lower())
return rv
diff --git a/pySim/utils.py b/pySim/utils.py
index 9062284..16d70f0 100644
--- a/pySim/utils.py
+++ b/pySim/utils.py
@@ -756,3 +756,17 @@ def get_addr_type(addr):
return 0x00
return None
+
+def sw_match(sw, pattern):
+ """Match given SW against given pattern."""
+ # Create a masked version of the returned status word
+ sw_lower = sw.lower()
+ sw_masked = ""
+ for i in range(0, 4):
+ if sw_lower[i] == '?':
+ sw_masked = sw_masked + '?'
+ elif sw_lower[i] == 'x':
+ sw_masked = sw_masked + 'x'
+ else:
+ sw_masked = sw_masked + sw_lower[i]
+ return sw == pattern