blob: d0101e3e98372c7eae05e2c5d92a8a931da18fdf [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."""
Harald Welte7fca85b2021-05-29 21:27:46 +020075 if not isinstance(d, dict):
76 return d
Harald Weltee0f9ef12021-04-10 17:22:35 +020077 res = {}
78 for (key, value) in d.items():
79 if key.startswith(exclude_prefix):
80 continue
81 if type(value) is dict:
82 res[key] = filter_dict(value)
83 else:
84 res[key] = value
85 return res
86
87# here we collect some shared / common definitions of data types
88LV = Prefixed(Int8ub, HexAdapter(GreedyBytes))
Robert Falkenberg9d16fbc2021-04-12 11:43:22 +020089
90# Default value for Reserved for Future Use (RFU) bits/bytes
91# See TS 31.101 Sec. "3.4 Coding Conventions"
92__RFU_VALUE = 0
93
94# Field that packs Reserved for Future Use (RFU) bit
95FlagRFU = Default(Flag, __RFU_VALUE)
96
97# Field that packs Reserved for Future Use (RFU) byte
98ByteRFU = Default(Byte, __RFU_VALUE)
99
100# Field that packs all remaining Reserved for Future Use (RFU) bytes
101GreedyBytesRFU = Default(GreedyBytes, b'')
102
103def BitsRFU(n=1):
104 '''
105 Field that packs Reserved for Future Use (RFU) bit(s)
106 as defined in TS 31.101 Sec. "3.4 Coding Conventions"
107
108 Use this for (currently) unused/reserved bits whose contents
109 should be initialized automatically but should not be cleared
110 in the future or when restoring read data (unlike padding).
111
112 Parameters:
113 n (Integer): Number of bits (default: 1)
114 '''
115 return Default(BitsInteger(n), __RFU_VALUE)
116
117def BytesRFU(n=1):
118 '''
119 Field that packs Reserved for Future Use (RFU) byte(s)
120 as defined in TS 31.101 Sec. "3.4 Coding Conventions"
121
122 Use this for (currently) unused/reserved bytes whose contents
123 should be initialized automatically but should not be cleared
124 in the future or when restoring read data (unlike padding).
125
126 Parameters:
127 n (Integer): Number of bytes (default: 1)
128 '''
129 return Default(Bytes(n), __RFU_VALUE)
Robert Falkenbergb07a3e92021-05-07 15:23:20 +0200130
131def GsmString(n):
132 '''
133 GSM 03.38 encoded byte string of fixed length n.
134 Encoder appends padding bytes (b'\\xff') to maintain
135 length. Decoder removes those trailing bytes.
136
137 Exceptions are raised for invalid characters
138 and length excess.
139
140 Parameters:
141 n (Integer): Fixed length of the encoded byte string
142 '''
143 return GsmStringAdapter(Rpad(Bytes(n), pattern=b'\xff'), codec='gsm03.38')