blob: a9033059b7c75967223eb2512e3e2a1e675e3a2c [file] [log] [blame]
Harald Weltee0f9ef12021-04-10 17:22:35 +02001from construct import *
Harald Welte2db5cfb2021-04-10 19:05:37 +02002from pySim.utils import b2h, h2b, swap_nibbles
Robert Falkenbergb07a3e92021-05-07 15:23:20 +02003import gsm0338
Harald Weltee0f9ef12021-04-10 17:22:35 +02004
5"""Utility code related to the integration of the 'construct' declarative parser."""
6
7# (C) 2021 by Harald Welte <laforge@osmocom.org>
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22
23class HexAdapter(Adapter):
24 """convert a bytes() type to a string of hex nibbles."""
25 def _decode(self, obj, context, path):
26 return b2h(obj)
27 def _encode(self, obj, context, path):
28 return h2b(obj)
29
Harald Welte2db5cfb2021-04-10 19:05:37 +020030class BcdAdapter(Adapter):
31 """convert a bytes() type to a string of BCD nibbles."""
32 def _decode(self, obj, context, path):
33 return swap_nibbles(b2h(obj))
34 def _encode(self, obj, context, path):
35 return h2b(swap_nibbles(obj))
36
Robert Falkenbergb07a3e92021-05-07 15:23:20 +020037class Rpad(Adapter):
38 """
39 Encoder appends padding bytes (b'\\xff') up to target size.
40 Decoder removes trailing padding bytes.
41
42 Parameters:
43 subcon: Subconstruct as defined by construct library
44 pattern: set padding pattern (default: b'\\xff')
45 """
46
47 def __init__(self, subcon, pattern=b'\xff'):
48 super().__init__(subcon)
49 self.pattern = pattern
50
51 def _decode(self, obj, context, path):
52 return obj.rstrip(self.pattern)
53
54 def _encode(self, obj, context, path):
55 if len(obj) > self.sizeof():
56 raise SizeofError("Input ({}) exceeds target size ({})".format(len(obj), self.sizeof()))
57 return obj + self.pattern * (self.sizeof() - len(obj))
58
59class GsmStringAdapter(Adapter):
60 """Convert GSM 03.38 encoded bytes to a string."""
61
62 def __init__(self, subcon, codec='gsm03.38', err='strict'):
63 super().__init__(subcon)
64 self.codec = codec
65 self.err = err
66
67 def _decode(self, obj, context, path):
68 return obj.decode(self.codec)
69
70 def _encode(self, obj, context, path):
71 return obj.encode(self.codec, self.err)
72
Harald Weltee0f9ef12021-04-10 17:22:35 +020073def filter_dict(d, exclude_prefix='_'):
74 """filter the input dict to ensure no keys starting with 'exclude_prefix' remain."""
75 res = {}
76 for (key, value) in d.items():
77 if key.startswith(exclude_prefix):
78 continue
79 if type(value) is dict:
80 res[key] = filter_dict(value)
81 else:
82 res[key] = value
83 return res
84
85# here we collect some shared / common definitions of data types
86LV = Prefixed(Int8ub, HexAdapter(GreedyBytes))
Robert Falkenberg9d16fbc2021-04-12 11:43:22 +020087
88# Default value for Reserved for Future Use (RFU) bits/bytes
89# See TS 31.101 Sec. "3.4 Coding Conventions"
90__RFU_VALUE = 0
91
92# Field that packs Reserved for Future Use (RFU) bit
93FlagRFU = Default(Flag, __RFU_VALUE)
94
95# Field that packs Reserved for Future Use (RFU) byte
96ByteRFU = Default(Byte, __RFU_VALUE)
97
98# Field that packs all remaining Reserved for Future Use (RFU) bytes
99GreedyBytesRFU = Default(GreedyBytes, b'')
100
101def BitsRFU(n=1):
102 '''
103 Field that packs Reserved for Future Use (RFU) bit(s)
104 as defined in TS 31.101 Sec. "3.4 Coding Conventions"
105
106 Use this for (currently) unused/reserved bits whose contents
107 should be initialized automatically but should not be cleared
108 in the future or when restoring read data (unlike padding).
109
110 Parameters:
111 n (Integer): Number of bits (default: 1)
112 '''
113 return Default(BitsInteger(n), __RFU_VALUE)
114
115def BytesRFU(n=1):
116 '''
117 Field that packs Reserved for Future Use (RFU) byte(s)
118 as defined in TS 31.101 Sec. "3.4 Coding Conventions"
119
120 Use this for (currently) unused/reserved bytes whose contents
121 should be initialized automatically but should not be cleared
122 in the future or when restoring read data (unlike padding).
123
124 Parameters:
125 n (Integer): Number of bytes (default: 1)
126 '''
127 return Default(Bytes(n), __RFU_VALUE)
Robert Falkenbergb07a3e92021-05-07 15:23:20 +0200128
129def GsmString(n):
130 '''
131 GSM 03.38 encoded byte string of fixed length n.
132 Encoder appends padding bytes (b'\\xff') to maintain
133 length. Decoder removes those trailing bytes.
134
135 Exceptions are raised for invalid characters
136 and length excess.
137
138 Parameters:
139 n (Integer): Fixed length of the encoded byte string
140 '''
141 return GsmStringAdapter(Rpad(Bytes(n), pattern=b'\xff'), codec='gsm03.38')