aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhilipp Maier <pmaier@sysmocom.de>2022-05-25 15:05:58 +0200
committerPhilipp Maier <pmaier@sysmocom.de>2022-05-25 15:05:58 +0200
commitcf079e76650e65d5fce062a4088770298aae506d (patch)
treedf5e73b8611a1fe4d217f11c4f2ccb45c58f766b
parent38402eb0651747592e9beec80e0c7cf0ca2085ae (diff)
ts_102_221: The BTLV IEs FILE SIZE and TOTAL FILE SIZE have a min lengthpmaier/updates
The TLV IEs FILE SIZE and TOTAL FILE SIZE have a minimum length of 2 byte. Even when the length is in the single digit range two bytes must be used. See also: ETSI TS 102 221, section 11.1.1.4.1 and 11.1.1.4.2 Change-Id: Ief113ce8fe3bcae2c9fb2ff4138df9ccf98d26ff
-rw-r--r--pySim/construct.py5
-rw-r--r--pySim/ts_102_221.py4
2 files changed, 6 insertions, 3 deletions
diff --git a/pySim/construct.py b/pySim/construct.py
index fcbadd8..a43cd29 100644
--- a/pySim/construct.py
+++ b/pySim/construct.py
@@ -208,10 +208,11 @@ def GsmString(n):
class GreedyInteger(Construct):
"""A variable-length integer implementation, think of combining GrredyBytes with BytesInteger."""
- def __init__(self, signed=False, swapped=False):
+ def __init__(self, signed=False, swapped=False, minlen=0):
super().__init__()
self.signed = signed
self.swapped = swapped
+ self.minlen = minlen
def _parse(self, stream, context, path):
data = stream_read_entire(stream, path)
@@ -239,6 +240,8 @@ class GreedyInteger(Construct):
if not isinstance(obj, integertypes):
raise IntegerError(f"value {obj} is not an integer", path=path)
length = self.__bytes_required(obj)
+ if length < self.minlen:
+ length = length + (self.minlen - length)
try:
data = integer2bytes(obj, length, self.signed)
except ValueError as e:
diff --git a/pySim/ts_102_221.py b/pySim/ts_102_221.py
index e7aff97..08e836c 100644
--- a/pySim/ts_102_221.py
+++ b/pySim/ts_102_221.py
@@ -80,11 +80,11 @@ ts_102_22x_cmdset = CardCommandSet('TS 102 22x', [
# ETSI TS 102 221 11.1.1.4.2
class FileSize(BER_TLV_IE, tag=0x80):
- _construct = GreedyInteger()
+ _construct = GreedyInteger(minlen=2)
# ETSI TS 102 221 11.1.1.4.2
class TotalFileSize(BER_TLV_IE, tag=0x81):
- _construct = GreedyInteger()
+ _construct = GreedyInteger(minlen=2)
# ETSI TS 102 221 11.1.1.4.3
class FileDescriptor(BER_TLV_IE, tag=0x82):