Repository navigation
Expand file tree
/
Copy pathstrings.py
More file actions
409 lines (324 loc) · 16.5 KB
/
Copy pathstrings.py
File metadata and controls
409 lines (324 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# -*- coding: utf-8 -*-
"""text field class"""
import urllib.parse as urllib_parse
from typing import TYPE_CHECKING, Any, Generic, TypeVar
from pcapkit.corekit.fields.field import Field
from pcapkit.corekit.sentinels import NO_VALUE
from pcapkit.utilities.chardet import detect
from pcapkit.utilities.compat import Dict, unquote
from pcapkit.utilities.exceptions import FieldValueError
__all__ = [
'_TextField',
'BytesField',
'StringField',
'DecodedString',
'BitField',
'PaddingField',
]
if TYPE_CHECKING:
from typing import Callable, Optional, Tuple
from typing_extensions import Literal, Self
from pcapkit.corekit.sentinels import NoValueType
NamespaceEntry = Tuple[int, int]
_T = TypeVar('_T', 'str', 'bytes', 'dict[str, Any]')
class _TextField(Field[_T], Generic[_T]):
"""Internal text value for protocol fields.
Args:
length: Field size (in bytes); if a callable is given, it should return
an integer value and accept the current packet as its only argument.
default: Field default value, if any.
callback: Callback function to be called upon
:meth:`self.__call__ <pcapkit.corekit.fields.field.FieldBase.__call__>`.
"""
def __init__(self, length: 'int | Callable[[dict[str, Any]], int]',
default: '_T | NoValueType' = NO_VALUE,
callback: 'Callable[[Self, dict[str, Any]], None]' = lambda *_: None) -> 'None':
super().__init__(length, default, callback) # type: ignore[arg-type]
self._template = f'{self._length}s' if self._length >= 0 else '1024s' # reasonable default
def __call__(self, packet: 'dict[str, Any]') -> 'Self':
"""Update field attributes.
Args:
packet: Packet data.
Returns:
New instance of :class:`_TextField`.
This method will return a new instance of :class:`_TextField` instead of
updating the current instance.
"""
new_self = super().__call__(packet)
new_self._template = f'{new_self._length}s'
return new_self
class BytesField(_TextField[bytes]):
"""Bytes value for protocol fields.
Args:
length: Field size (in bytes); if a callable is given, it should return
an integer value and accept the current packet as its only argument.
default: Field default value, if any.
callback: Callback function to be called upon
:meth:`self.__call__ <pcapkit.corekit.fields.field.FieldBase.__call__>`.
"""
def pre_process(self, value: 'bytes', packet: 'dict[str, Any]') -> 'bytes': # pylint: disable=unused-argument
"""Process field value before construction (packing).
Arguments:
value: Field value.
packet: Packet data.
Returns:
Processed field value.
"""
if self._length < 0:
self._length = len(value)
self._template = f'{self._length}s'
return value
class StringField(_TextField[str]):
r"""String value for protocol fields.
Args:
length: Field size (in bytes); if a callable is given, it should return
an integer value and accept the current packet as its only argument.
default: Field default value, if any.
encoding: The encoding with which to decode the :obj:`bytes`, and with which
a :obj:`str` is encoded when packing. If not provided, :mod:`pcapkit`
will first try detecting its encoding using |chardet|_. The fallback
encoding is **UTF-8**.
errors: The error handling scheme to use for the handling of decoding errors.
The default is ``'strict'``. Should decoding fail under it, the field
retries with ``'replace'`` rather than raising, so an octet the charset
cannot decode reads as U+FFFD -- the octets themselves are not lost,
see below. A codec that raises under ``'replace'`` as well, such as
``idna``, which supports ``'strict'`` alone, decodes as UTF-8 with
``'replace'`` instead, and its octets are not lost either.
Other possible values are ``'ignore'`` and ``'replace'`` as well as any
other name registered with :func:`codecs.register_error` that can handle
:exc:`UnicodeDecodeError`.
unquote: Whether to unquote the decoded string as a URL. Should decoding fail,
the method will try again replacing ``'%'`` with ``'\x'`` then decoding the
``url`` as ``'utf-8'`` with ``'replace'`` for error handling.
callback: Callback function to be called upon
:meth:`self.__call__ <pcapkit.corekit.fields.field.FieldBase.__call__>`.
Unpacking is lossless. Where re-encoding the decoded :obj:`str` would not
reproduce the octets it came from -- a detected charset other than UTF-8, an
octet the charset cannot decode, a lossy ``errors`` scheme, or a URL quoting
that differs from the original -- the field returns a :class:`DecodedString`,
which remembers those octets and packs back as exactly them. The decoded
*text* is the plain decoding, U+FFFD and all: a consumer that only reads
the text, such as a dumper writing it back out as UTF-8, sees an ordinary
:obj:`str`. A length of ``-1`` is resolved to the number of *encoded* octets.
"""
def __init__(self, length: 'int | Callable[[dict[str, Any]], int]',
default: 'str | NoValueType' = NO_VALUE, encoding: 'Optional[str]' = None,
errors: 'Literal["strict", "ignore", "replace"]' = 'strict',
unquote: 'bool' = False, # pylint: disable=redefined-outer-name
callback: 'Callable[[Self, dict[str, Any]], None]' = lambda *_: None) -> 'None':
super().__init__(length, default, callback)
self._encoding = encoding
self._errors = errors
self._unquote = unquote
def _encode(self, value: 'str') -> 'bytes':
"""Encode a :obj:`str` value the way :meth:`pre_process` packs it.
Arguments:
value: Field value.
Returns:
Encoded field value.
"""
if self._unquote:
value = urllib_parse.quote(value, encoding=self._encoding or 'utf-8', errors=self._errors)
return value.encode(self._encoding or 'utf-8', self._errors)
def pre_process(self, value: 'str', packet: 'dict[str, Any]') -> 'bytes': # pylint: disable=unused-argument
"""Process field value before construction (packing).
Arguments:
value: Field value.
packet: Packet data.
Returns:
Processed field value.
"""
# NOTE: The octets a DecodedString was decoded from are used verbatim,
# since re-encoding its text is exactly what would not reproduce them.
buffer = value.raw if isinstance(value, DecodedString) else self._encode(value)
# NOTE: A negative length is resolved from the *encoded* octets, not from
# the character count -- the ``Ns`` template counts octets, so counting
# characters truncated any text that does not encode one octet per
# character.
if self._length < 0:
self._length = len(buffer)
self._template = f'{self._length}s'
return buffer
def post_process(self, value: 'bytes', packet: 'dict[str, Any]') -> 'str': # pylint: disable=unused-argument
"""Process field value after parsing (unpacked).
Arguments:
value: Field value.
packet: Packet data.
Returns:
Processed field value; a :class:`DecodedString` carrying ``value``
where :meth:`pre_process` would not otherwise reproduce it.
"""
if self._unquote:
try:
ret = unquote(value, encoding=self._encoding or 'utf-8', errors=self._errors)
except UnicodeError:
ret = unquote(value.replace(b'%', rb'\x'), encoding='utf-8', errors='replace')
else:
charset = self._encoding or detect(value)
try:
ret = value.decode(charset, self._errors)
except UnicodeError:
# NOTE: A codec may raise under ``'replace'`` too -- ``idna``
# supports ``'strict'`` alone, and ``punycode`` before Python 3.13
# decodes some non-ASCII octets strictly whatever the handler -- so
# the last resort is the fallback encoding, UTF-8, which cannot.
# The check below keeps the octets, as for any lossy decoding.
try:
ret = value.decode(charset, 'replace')
except UnicodeError:
ret = value.decode('utf-8', 'replace')
# NOTE: Encoding may itself fail -- ``errors='strict'`` over text that
# decoded under a lone-surrogate scheme, for one -- which is as much a
# failure to reproduce the octets as a mismatch is.
try:
lossless = self._encode(ret) == value
except UnicodeError:
lossless = False
if lossless:
return ret
return DecodedString(ret, value)
class DecodedString(str):
"""Decoded :class:`StringField` value that remembers its octets.
:class:`StringField` returns one where encoding the decoded text would not
reproduce the octets it was decoded from, so that the field packs it back as
exactly those octets. It is otherwise an ordinary :obj:`str`, and deliberately
so: it compares and hashes as its text, since the text is the value and
:attr:`raw` is provenance. Two of them with the same text and different octets
therefore compare equal -- the alternative, mixing :attr:`raw` into
:meth:`~object.__eq__`, would make ``info.comment == 'café'`` false against a
plain :obj:`str` literal, which is the comparison callers actually write.
Any string operation on one returns a plain :obj:`str`, which a field then
packs with its own encoding; that is intended, as an edited value is no longer
the text those octets held.
Args:
value: Decoded text.
raw: Octets ``value`` was decoded from.
"""
#: Octets the string was decoded from.
raw: 'bytes'
def __new__(cls, value: 'str', raw: 'bytes') -> 'Self':
self = super().__new__(cls, value)
self.raw = raw
return self
def __getnewargs__(self) -> 'tuple[str, bytes]': # type: ignore[override]
return str(self), self.raw
class BitField(_TextField[Dict[str, Any]]):
"""Bit value for protocol fields.
Args:
length: Field size (in bytes).
default: Field default value, if any.
namespace: Field namespace (a dict mapping field name to a tuple of start index,
and length of the subfield).
callback: Callback function to be called upon
:meth:`self.__call__ <pcapkit.corekit.fields.field.FieldBase.__call__>`.
Bits that no subfield names are kept as captured: where any of them is
set, the unpacked value carries them under an ``__unnamed__`` key, as an
integer over the whole field with every named bit cleared, and packing
writes them back (:issue:`1487`). The key is absent when they are all
zero, so a value built without it packs them as zeros.
"""
#: Key of the unpacked value that carries the unnamed bits.
UNNAMED = '__unnamed__'
def __init__(self, length: 'int',
default: 'dict[str, Any] | NoValueType' = NO_VALUE,
namespace: 'Optional[dict[str, NamespaceEntry]]' = None,
callback: 'Callable[[Self, dict[str, Any]], None]' = lambda *_: None) -> 'None':
super().__init__(length, default, callback)
self._namespace = namespace or {}
# NOTE: A subfield reaching past the end of the field is a mistake in the
# schema that declares it, not something a packet can cause, so it is
# rejected here rather than at packing time -- where it would instead grow
# the buffer, and surface much later as an opaque ``OverflowError`` from
# :meth:`int.to_bytes`.
width = self.length * 8
for name, (start, size) in self._namespace.items():
if start < 0 or size < 1 or start + size > width:
raise FieldValueError(
f'{type(self).__name__}: subfield {name!r} spans bits '
f'{start}-{start + size - 1} of a {width}-bit field'
)
#: Mask of the bits no subfield names, most significant bit first.
self._unnamed = (1 << width) - 1
for start, size in self._namespace.values():
self._unnamed &= ~(((1 << size) - 1) << (width - start - size))
def pre_process(self, value: 'dict[str, Any]', packet: 'dict[str, Any]') -> 'bytes': # pylint: disable=unused-argument
"""Process field value before construction (packing).
Arguments:
value: Field value.
packet: Packet data.
Returns:
Processed field value.
Raises:
FieldValueError: If a subfield value does not fit the bits it was
declared to occupy, or the ``__unnamed__`` value is not an
integer.
"""
# NOTE: The buffer holds one ASCII digit per bit, so it must be seeded with
# ``b'0'`` rather than with NUL bytes -- a zero bit written by the loop below
# is the *character* ``b'0'``, which is itself non-zero, so a truthiness test
# over the buffer cannot tell a cleared bit from a set one and would report
# every named bit as set.
#
# The unnamed bits seed it, so that they come back as captured; masking
# them keeps a stray named bit in the record from overriding a subfield.
unnamed = value.get(self.UNNAMED, 0)
if isinstance(unnamed, bool) or not isinstance(unnamed, int):
raise FieldValueError(
f'{type(self).__name__}: {self.UNNAMED} must be an int, not {unnamed!r}'
)
buffer = bytearray(f'{unnamed & self._unnamed:0{self.length * 8}b}'.encode())
for name, (start, size) in self._namespace.items():
bits = f'{value[name]:0{size}b}'
if len(bits) > size:
raise FieldValueError(
f'{type(self).__name__}: subfield {name!r} value {value[name]!r} '
f'needs {len(bits)} bits, but was declared with {size}'
)
buffer[start:start + size] = bits.encode()
return int(buffer, 2).to_bytes(self.length, 'big')
def post_process(self, value: 'bytes', packet: 'dict[str, Any]') -> 'dict[str, Any]': # pylint: disable=unused-argument
"""Process field value after parsing (unpacked).
Arguments:
value: Field value.
packet: Packet data.
Returns:
Processed field value.
"""
buffer = {}
binary = ''.join(f'{byte:08b}' for byte in value)
for name, (start, len) in self._namespace.items():
end = start + len
buffer[name] = int(binary[start:end], 2)
unnamed = int.from_bytes(value, 'big') & self._unnamed
if unnamed:
buffer[self.UNNAMED] = unnamed
return buffer
class PaddingField(BytesField):
"""Bytes value for protocol fields.
Args:
length: Field size (in bytes); if a callable is given, it should return
an integer value and accept the current packet as its only argument.
default: Field default value, if any.
callback: Callback function to be called upon
:meth:`self.__call__ <pcapkit.corekit.fields.field.FieldBase.__call__>`.
Padding packs back as the octets it was given, so a schema unpacked from
non-zero padding rebuilds byte for byte. An absent or empty value packs as
zeros -- the default for a freshly built packet -- and a value of another
width is truncated or zero-filled to the field's, as :mod:`struct`'s ``s``
format does.
"""
def pack(self, value: 'Optional[bytes]', packet: 'dict[str, Any]') -> 'bytes': # pylint: disable=unused-argument
"""Pack field value into :obj:`bytes`.
Args:
value: Field value, i.e. the padding octets as captured.
packet: Packet data.
Returns:
Packed field value: ``value`` truncated or zero-filled to the
field's width, or that many zero octets if ``value`` is absent or
empty.
"""
length = self.length
if isinstance(value, (bytes, bytearray)) and value:
return bytes(value[:length]).ljust(length, b'\x00')
return bytes(length)