Skip to content

Commit d85cfac

Browse files
committed
bugfix in fields (added length property and revised __call__)
1 parent 88c0a2c commit d85cfac

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

pcapkit/corekit/fields/collections.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ class ListField(_Field[List[_TL]]):
4242
4343
"""
4444

45+
@property
46+
def length(self) -> 'int':
47+
"""Field size."""
48+
return self._length
49+
4550
@property
4651
def optional(self) -> 'bool':
4752
"""Field is optional."""
@@ -121,7 +126,7 @@ def unpack(self, buffer: 'bytes | IO[bytes]', packet: 'dict[str, Any]') -> 'byte
121126
Unpacked field value.
122127
123128
"""
124-
length = self.length
129+
length = self._length
125130
if isinstance(buffer, bytes):
126131
file = io.BytesIO(buffer) # type: IO[bytes]
127132
else:
@@ -134,7 +139,7 @@ def unpack(self, buffer: 'bytes | IO[bytes]', packet: 'dict[str, Any]') -> 'byte
134139
is_schema = isinstance(self._item_type, SchemaField)
135140

136141
temp = [] # type: list[_TL]
137-
while length:
142+
while length > 0:
138143
field = self._item_type(packet)
139144

140145
if is_schema:
@@ -195,6 +200,11 @@ def eool(self) -> 'int | StdlibEnum | AenumEnum':
195200
"""EOOL option."""
196201
return self._eool
197202

203+
@property
204+
def option_padding(self) -> 'int':
205+
"""Length option padding data."""
206+
return self._option_padding
207+
198208
def __init__(self, length: 'int | Callable[[dict[str, Any]], int]' = lambda _: -1,
199209
base_schema: 'Optional[Type[Schema]]' = None,
200210
type_name: 'str' = 'type',
@@ -204,6 +214,7 @@ def __init__(self, length: 'int | Callable[[dict[str, Any]], int]' = lambda _: -
204214
super().__init__(length, None, callback)
205215
self._name = '<option>'
206216
self._eool = eool
217+
self._option_padding = 0
207218

208219
if base_schema is None:
209220
raise FieldValueError('Field <option> has no base schema.')
@@ -229,12 +240,12 @@ def unpack(self, buffer: 'bytes | IO[bytes]', packet: 'dict[str, Any]') -> 'list
229240
230241
Important:
231242
If the option list ended before the specified size limit,
232-
inject ``__option_padding__`` as the remaining length to
243+
set :attr:`self.option_padding <OptionField.option_padding>` as the remaining length to
233244
the ``packet`` argument such that the next fields can be
234245
aware of such informations.
235246
236247
"""
237-
length = self.length
248+
length = self._length
238249
if isinstance(buffer, bytes):
239250
file = io.BytesIO(buffer) # type: IO[bytes]
240251
else:
@@ -246,7 +257,7 @@ def unpack(self, buffer: 'bytes | IO[bytes]', packet: 'dict[str, Any]') -> 'list
246257
new_packet[self.name] = OrderedMultiDict()
247258

248259
temp = [] # type: list[Schema]
249-
while length:
260+
while length > 0:
250261
# unpack option type using base schema
251262
meta = self._base_schema.unpack(file, length, packet) # type: ignore[call-arg,misc,var-annotated]
252263
code = cast('int', meta[self._type_name])
@@ -265,6 +276,7 @@ def unpack(self, buffer: 'bytes | IO[bytes]', packet: 'dict[str, Any]') -> 'list
265276

266277
# check for EOOL
267278
if code == self._eool:
268-
packet['__option_padding__'] = length
269279
break
280+
281+
self._option_padding = length
270282
return temp

pcapkit/corekit/fields/misc.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
class NoValueField(_Field[_TN]):
3333
"""Schema field for no value type (or :obj:`None`)."""
3434

35+
_default = NoValue
36+
3537
@property
3638
def template(self) -> 'str':
3739
"""Field template."""
@@ -426,7 +428,8 @@ def __call__(self, packet: 'dict[str, Any]') -> 'SwitchField[_TC]':
426428
427429
"""
428430
new_self = copy.copy(self)
429-
new_self._field = self._selector(packet)(packet)
431+
new_self._field = new_self._selector(packet)(packet)
432+
new_self._field.name = self.name
430433
return new_self
431434

432435
def pre_process(self, value: '_TC', packet: 'dict[str, Any]') -> 'Any': # pylint: disable=unused-argument
@@ -504,6 +507,11 @@ class SchemaField(_Field[_TS]):
504507
505508
"""
506509

510+
@property
511+
def length(self) -> 'int':
512+
"""Field size."""
513+
return self._length # type: ignore[has-type]
514+
507515
@property
508516
def optional(self) -> 'bool':
509517
"""Field is optional."""

pcapkit/corekit/fields/numbers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ def __call__(self, packet: 'dict[str, Any]') -> 'Self':
9999
"""
100100
new_self = super().__call__(packet)
101101

102-
if self._bit_length < 0:
103-
self._bit_length = self._length * 8
104-
self._bit_mask = (1 << self._bit_length) - 1
102+
if new_self._bit_length < 0:
103+
new_self._bit_length = new_self._length * 8
104+
new_self._bit_mask = (1 << new_self._bit_length) - 1
105105

106106
endian = '>' if new_self._byteorder == 'big' else '<'
107107
struct_fmt = new_self.build_template(new_self._length, new_self._signed)

0 commit comments

Comments
 (0)