mirror of
https://gitea.osmocom.org/sim-card/pysim.git
synced 2026-03-24 14:28:32 +03:00
31.102: Fix EF.Routing_Indicator for odd number of digits
The routing indicator is BCD-encoded but has an arbitrary length of 1, 2, 3 or 4 digits. In order to support the odd lengths of 1 or 3, we must not pad on the byte level, but on the nibble level. This requires a slight extension of the Rpad() Adapter. Change-Id: I6c26dccdd570de7b7a4cd48338068e230340ec7c Fixes: OS#6054
This commit is contained in:
@@ -66,26 +66,29 @@ class InvertAdapter(Adapter):
|
|||||||
|
|
||||||
class Rpad(Adapter):
|
class Rpad(Adapter):
|
||||||
"""
|
"""
|
||||||
Encoder appends padding bytes (b'\\xff') up to target size.
|
Encoder appends padding bytes (b'\\xff') or characters up to target size.
|
||||||
Decoder removes trailing padding bytes.
|
Decoder removes trailing padding bytes/characters.
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
subcon: Subconstruct as defined by construct library
|
subcon: Subconstruct as defined by construct library
|
||||||
pattern: set padding pattern (default: b'\\xff')
|
pattern: set padding pattern (default: b'\\xff')
|
||||||
|
num_per_byte: number of 'elements' per byte. E.g. for hex nibbles: 2
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, subcon, pattern=b'\xff'):
|
def __init__(self, subcon, pattern=b'\xff', num_per_byte=1):
|
||||||
super().__init__(subcon)
|
super().__init__(subcon)
|
||||||
self.pattern = pattern
|
self.pattern = pattern
|
||||||
|
self.num_per_byte = num_per_byte
|
||||||
|
|
||||||
def _decode(self, obj, context, path):
|
def _decode(self, obj, context, path):
|
||||||
return obj.rstrip(self.pattern)
|
return obj.rstrip(self.pattern)
|
||||||
|
|
||||||
def _encode(self, obj, context, path):
|
def _encode(self, obj, context, path):
|
||||||
if len(obj) > self.sizeof():
|
target_size = self.sizeof() * self.num_per_byte
|
||||||
|
if len(obj) > target_size:
|
||||||
raise SizeofError("Input ({}) exceeds target size ({})".format(
|
raise SizeofError("Input ({}) exceeds target size ({})".format(
|
||||||
len(obj), self.sizeof()))
|
len(obj), target_size))
|
||||||
return obj + self.pattern * (self.sizeof() - len(obj))
|
return obj + self.pattern * (target_size - len(obj))
|
||||||
|
|
||||||
class MultiplyAdapter(Adapter):
|
class MultiplyAdapter(Adapter):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -1089,7 +1089,8 @@ class EF_Routing_Indicator(TransparentEF):
|
|||||||
# responsibility of home network operator but BCD coding shall be used. If a network
|
# responsibility of home network operator but BCD coding shall be used. If a network
|
||||||
# operator decides to assign less than 4 digits to Routing Indicator, the remaining digits
|
# operator decides to assign less than 4 digits to Routing Indicator, the remaining digits
|
||||||
# shall be coded as "1111" to fill the 4 digits coding of Routing Indicator
|
# shall be coded as "1111" to fill the 4 digits coding of Routing Indicator
|
||||||
self._construct = Struct('routing_indicator'/BcdAdapter(Rpad(Bytes(2))), 'rfu'/HexAdapter(Bytes(2)))
|
self._construct = Struct('routing_indicator'/Rpad(BcdAdapter(Bytes(2)), 'f', 2),
|
||||||
|
'rfu'/HexAdapter(Bytes(2)))
|
||||||
|
|
||||||
# TS 31.102 Section 4.4.11.13
|
# TS 31.102 Section 4.4.11.13
|
||||||
class EF_TN3GPPSNN(TransparentEF):
|
class EF_TN3GPPSNN(TransparentEF):
|
||||||
|
|||||||
Reference in New Issue
Block a user