Debian Codegraph @

pamqp/decode.py

raw
1 """
2 Functions for decoding data of various types including field tables and arrays
3
4 """
5
6 import collections.abc
7 import datetime
8 import decimal as _decimal
9
10 from pamqp import common
11
12
13 def by_type(
14 value: bytes, data_type: str, offset: int = 0
15 ) -> tuple[int, common.FieldValue]:
16 """Decodes values using the specified type
17
18 :param value: The binary value to decode
19 :param data_type: The data type name of the value
20 :param offset: The starting position of the data in the byte stream
21 :rtype: :class:`tuple` (:class:`int`, :const:`pamqp.common.FieldValue`)
22 :raises ValueError: when the data type is unknown
23
24 """
25 if data_type == 'bit':
26 return bit(value, offset)
27 decoder = METHODS.get(data_type)
28 if decoder is None:
29 raise ValueError(f'Unknown type: {data_type}')
30 return decoder(value)
31
32
33 def bit(value: bytes, position: int) -> tuple[int, bool]:
34 """Decode a bit value, returning bytes consumed and the value.
35
36 :param value: The binary value to decode
37 :param position: The position in the byte of the bit value
38 :rtype: :class:`tuple` (:class:`int`, :class:`bool`)
39 :raises ValueError: when the binary data can not be unpacked
40
41 """
42 bit_buffer = common.Struct.byte.unpack_from(value)[0]
43 try:
44 return 0, (bit_buffer & (1 << position)) != 0
45 except TypeError as err:
46 raise ValueError('Could not unpack bit value') from err
47
48
49 def boolean(value: bytes) -> tuple[int, bool]:
50 """Decode a boolean value, returning bytes consumed and the value.
51
52 :param value: The binary value to decode
53 :rtype: :class:`tuple` (:class:`int`, :class:`bool`)
54 :raises ValueError: when the binary data can not be unpacked
55
56 """
57 try:
58 return 1, bool(common.Struct.byte.unpack_from(value[0:1])[0])
59 except TypeError as err:
60 raise ValueError('Could not unpack boolean value') from err
61
62
63 def byte_array(value: bytes) -> tuple[int, bytearray]:
64 """Decode a byte_array value, returning bytes consumed and the value.
65
66 :param value: The binary value to decode
67 :rtype: :class:`tuple` (:class:`int`, :class:`bytearray`)
68 :raises ValueError: when the binary data can not be unpacked
69
70 """
71 try:
72 length = common.Struct.integer.unpack(value[0:4])[0]
73 return length + 4, bytearray(value[4 : length + 4])
74 except TypeError as err:
75 raise ValueError('Could not unpack byte array value') from err
76
77
78 def decimal(value: bytes) -> tuple[int, _decimal.Decimal]:
79 """Decode a decimal value, returning bytes consumed and the value.
80
81 :param value: The binary value to decode
82 :rtype: :class:`tuple` (:class:`int`, :class:`decimal.Decimal`)
83 :raises ValueError: when the binary data can not be unpacked
84
85 """
86 try:
87 decimals = common.Struct.byte.unpack(value[0:1])[0]
88 raw = common.Struct.integer.unpack(value[1:5])[0]
89 return 5, _decimal.Decimal(raw) * (_decimal.Decimal(10) ** -decimals)
90 except TypeError as err:
91 raise ValueError('Could not unpack decimal value') from err
92
93
94 def double(value: bytes) -> tuple[int, float]:
95 """Decode a double value, returning bytes consumed and the value.
96
97 :param value: The binary value to decode
98 :rtype: :class:`tuple` (:class:`int`, :class:`float`)
99 :raises ValueError: when the binary data can not be unpacked
100
101 """
102 try:
103 return 8, common.Struct.double.unpack_from(value)[0]
104 except TypeError as err:
105 raise ValueError('Could not unpack double value') from err
106
107
108 def floating_point(value: bytes) -> tuple[int, float]:
109 """Decode a floating point value, returning bytes consumed and the value.
110
111 :param value: The binary value to decode
112 :rtype: :class:`tuple` (:class:`int`, :class:`float`)
113 :raises ValueError: when the binary data can not be unpacked
114
115 """
116 try:
117 return 4, common.Struct.float.unpack_from(value)[0]
118 except TypeError as err:
119 raise ValueError('Could not unpack floating point value') from err
120
121
122 def long_int(value: bytes) -> tuple[int, int]:
123 """Decode a long integer value, returning bytes consumed and the value.
124
125 :param value: The binary value to decode
126 :rtype: :class:`tuple` (:class:`int`, :class:`int`)
127 :raises ValueError: when the binary data can not be unpacked
128
129 """
130 try:
131 return 4, common.Struct.long.unpack(value[0:4])[0]
132 except TypeError as err:
133 raise ValueError('Could not unpack long integer value') from err
134
135
136 def long_uint(value: bytes) -> tuple[int, int]:
137 """Decode an unsigned long integer value, returning bytes consumed and
138 the value.
139
140 :param value: The binary value to decode
141 :rtype: :class:`tuple` (:class:`int`, :class:`int`)
142 :raises ValueError: when the binary data can not be unpacked
143
144 """
145 try:
146 return 4, common.Struct.ulong.unpack(value[0:4])[0]
147 except TypeError as err:
148 raise ValueError(
149 'Could not unpack unsigned long integer value'
150 ) from err
151
152
153 def long_long_int(value: bytes) -> tuple[int, int]:
154 """Decode a long-long integer value, returning bytes consumed and the
155 value.
156
157 :param value: The binary value to decode
158 :rtype: :class:`tuple` (:class:`int`, :class:`int`)
159 :raises ValueError: when the binary data can not be unpacked
160
161 """
162 try:
163 return 8, common.Struct.long_long_int.unpack(value[0:8])[0]
164 except TypeError as err:
165 raise ValueError('Could not unpack long-long integer value') from err
166
167
168 def long_str(value: bytes) -> tuple[int, str | bytes]:
169 """Decode a string value, returning bytes consumed and the value.
170
171 :param value: The binary value to decode
172 :rtype: :class:`tuple` (:class:`int`, :class:`str`)
173 :raises ValueError: when the binary data can not be unpacked
174
175 """
176 try:
177 length = common.Struct.integer.unpack(value[0:4])[0]
178 except TypeError as err:
179 raise ValueError('Could not unpack long string value') from err
180 try:
181 return length + 4, value[4 : length + 4].decode('utf-8')
182 except UnicodeDecodeError:
183 return length + 4, value[4 : length + 4]
184
185
186 def octet(value: bytes) -> tuple[int, int]:
187 """Decode an octet value, returning bytes consumed and the value.
188
189 :param value: The binary value to decode
190 :rtype: :class:`tuple` (:class:`int`, :class:`int`)
191 :raises ValueError: when the binary data can not be unpacked
192
193 """
194 try:
195 return 1, common.Struct.byte.unpack(value[0:1])[0]
196 except TypeError as err:
197 raise ValueError('Could not unpack octet value') from err
198
199
200 def short_int(value: bytes) -> tuple[int, int]:
201 """Decode a short integer value, returning bytes consumed and the value.
202
203 :param value: The binary value to decode
204 :rtype: :class:`tuple` (:class:`int`, :class:`int`)
205 :raises ValueError: when the binary data can not be unpacked
206
207 """
208 try:
209 return 2, common.Struct.short.unpack_from(value[0:2])[0]
210 except TypeError as err:
211 raise ValueError('Could not unpack short integer value') from err
212
213
214 def short_uint(value: bytes) -> tuple[int, int]:
215 """Decode an unsigned short integer value, returning bytes consumed and
216 the value.
217
218 :param value: The binary value to decode
219 :rtype: :class:`tuple` (:class:`int`, :class:`int`)
220 :raises ValueError: when the binary data can not be unpacked
221
222 """
223 try:
224 return 2, common.Struct.ushort.unpack_from(value[0:2])[0]
225 except TypeError as err:
226 raise ValueError(
227 'Could not unpack unsigned short integer value'
228 ) from err
229
230
231 def short_short_int(value: bytes) -> tuple[int, int]:
232 """Decode a short-short integer value, returning bytes consumed and the
233 value.
234
235 :param value: The binary value to decode
236 :rtype: :class:`tuple` (:class:`int`, :class:`int`)
237 :raises ValueError: when the binary data can not be unpacked
238
239 """
240 try:
241 return 1, common.Struct.short_short_int.unpack_from(value[0:1])[0]
242 except TypeError as err:
243 raise ValueError('Could not unpack short-short integer value') from err
244
245
246 def short_short_uint(value: bytes) -> tuple[int, int]:
247 """Decode a unsigned short-short integer value, returning bytes consumed
248 and the value.
249
250 :param value: The binary value to decode
251 :rtype: :class:`tuple` (:class:`int`, :class:`int`)
252 :raises ValueError: when the binary data can not be unpacked
253
254 """
255 try:
256 return 1, common.Struct.short_short_uint.unpack_from(value[0:1])[0]
257 except TypeError as err:
258 raise ValueError(
259 'Could not unpack unsigned short-short integer value'
260 ) from err
261
262
263 def short_str(value: bytes) -> tuple[int, str]:
264 """Decode a string value, returning bytes consumed and the value.
265
266 :param value: The binary value to decode
267 :rtype: :class:`tuple` (:class:`int`, :class:`str`)
268 :raises ValueError: when the binary data can not be unpacked
269
270 """
271 try:
272 length = common.Struct.byte.unpack(value[0:1])[0]
273 return length + 1, value[1 : length + 1].decode('utf-8')
274 except TypeError as err:
275 raise ValueError('Could not unpack short string value') from err
276
277
278 def timestamp(value: bytes) -> tuple[int, datetime.datetime]:
279 """Decode a timestamp value, returning bytes consumed and the value.
280
281 :param value: The binary value to decode
282 :rtype: :class:`tuple` (:class:`int`, :class:`datetime.datetime`)
283 :raises ValueError: when the binary data can not be unpacked
284
285 """
286 try:
287 temp = common.Struct.timestamp.unpack(value[0:8])
288 ts_value = temp[0]
289
290 # Anything above the year 2106 is likely milliseconds
291 if ts_value > 0xFFFFFFFF:
292 ts_value /= 1000.0
293
294 return 8, datetime.datetime.fromtimestamp(ts_value, tz=datetime.UTC)
295 except TypeError as err:
296 raise ValueError('Could not unpack timestamp value') from err
297
298
299 def embedded_value(value: bytes) -> tuple[int, common.FieldValue]:
300 """Dynamically decode a value based upon the starting byte
301
302 :param value: The binary value to decode
303 :rtype: :class:`tuple` (:class:`int`, :const:`pamqp.common.FieldValue`)
304 :raises ValueError: when the binary data can not be unpacked
305
306 """
307 if not value:
308 return 0, None
309 try:
310 bytes_consumed, temp = TABLE_MAPPING[value[0:1]](value[1:])
311 except KeyError as err:
312 raise ValueError(f'Unknown type: {value[:1]!r}') from err
313 return bytes_consumed + 1, temp
314
315
316 def field_array(value: bytes) -> tuple[int, common.FieldArray]:
317 """Decode a field array value, returning bytes consumed and the value.
318
319 :param value: The binary value to decode
320 :rtype: :class:`tuple` (:class:`int`, :const:`pamqp.common.FieldArray`)
321 :raises ValueError: when the binary data can not be unpacked
322
323 """
324 try:
325 length = common.Struct.integer.unpack(value[0:4])[0]
326 offset = 4
327 data = []
328 field_array_end = offset + length
329 while offset < field_array_end:
330 consumed, result = embedded_value(value[offset:])
331 offset += consumed
332 data.append(result)
333 return offset, data
334 except TypeError as err:
335 raise ValueError('Could not unpack data') from err
336
337
338 def field_table(value: bytes) -> tuple[int, common.FieldTable]:
339 """Decode a field array value, returning bytes consumed and the value.
340
341 :param value: The binary value to decode
342 :rtype: :class:`tuple` (:class:`int`, :const:`pamqp.common.FieldTable`)
343 :raises ValueError: when the binary data can not be unpacked
344
345 """
346 try:
347 length = common.Struct.integer.unpack(value[0:4])[0]
348 offset = 4
349 data = {}
350 field_table_end = offset + length
351 while offset < field_table_end:
352 key_length = common.Struct.byte.unpack_from(value, offset)[0]
353 offset += 1
354 key = value[offset : offset + key_length].decode('utf-8')
355 offset += key_length
356 consumed, result = embedded_value(value[offset:])
357 offset += consumed
358 data[key] = result
359 return field_table_end, data
360 except TypeError as err:
361 raise ValueError('Could not unpack data') from err
362
363
364 def void(_: bytes) -> tuple[int, None]:
365 """Return a void, no data to decode
366
367 :param _: The empty bytes object to ignore
368 :rtype: :class:`tuple` (:class:`int`, :const:`None`)
369
370 """
371 return 0, None
372
373
374 METHODS: dict[
375 str, collections.abc.Callable[..., tuple[int, common.FieldValue]]
376 ] = {
377 'array': field_array,
378 'bit': bit,
379 'boolean': boolean,
380 'byte_array': byte_array,
381 'decimal': decimal,
382 'double': double,
383 'float': floating_point,
384 'long': long_uint,
385 'longlong': long_long_int,
386 'longstr': long_str,
387 'octet': octet,
388 'short': short_uint,
389 'shortstr': short_str,
390 'table': field_table,
391 'timestamp': timestamp,
392 'void': void,
393 } # Define a data type mapping to methods for by_type()
394
395 # See https://www.rabbitmq.com/amqp-0-9-1-errata.html
396 TABLE_MAPPING: dict[
397 bytes, collections.abc.Callable[..., tuple[int, common.FieldValue]]
398 ] = {
399 b't': boolean,
400 b'b': short_short_int,
401 b'B': short_short_uint,
402 b's': short_int,
403 b'u': short_uint,
404 b'I': long_int,
405 b'i': long_uint,
406 b'l': long_long_int,
407 b'L': long_long_int,
408 b'f': floating_point,
409 b'd': double,
410 b'D': decimal,
411 b'S': long_str,
412 b'A': field_array,
413 b'T': timestamp,
414 b'F': field_table,
415 b'V': void,
416 b'\x00': void, # While not documented, have seen this in the wild
417 b'x': byte_array,
418 } # Define a mapping for use in `field_array()` and `field_table()`