| 553 | |
| 554 | |
| 555 | class FieldUVarLenField(AbstractUVarIntField): |
| 556 | __slots__ = ['_length_of', '_adjust'] |
| 557 | |
| 558 | def __init__(self, name, default, size, length_of, adjust=lambda x: x): |
| 559 | # type: (str, Optional[int], int, str, Callable[[int], int]) -> None |
| 560 | """ Initializes a FieldUVarLenField |
| 561 | |
| 562 | :param str name: The name of this field instance. |
| 563 | :param int|None default: the default value of this field instance. |
| 564 | :param int size: the number of bits that are occupied by this field in the first byte of a binary string. # noqa: E501 |
| 565 | size must be in the range ]0;8]. |
| 566 | :param str length_of: The name of the field this field value is measuring/representing. # noqa: E501 |
| 567 | :param callable adjust: A function that modifies the value computed from the "length_of" field. # noqa: E501 |
| 568 | |
| 569 | adjust can be used for instance to add a constant to the length_of field # noqa: E501 |
| 570 | length. For instance, let's say that i2len of the length_of field |
| 571 | returns 2. If adjust is lambda x: x+1 In that case, this field will |
| 572 | value 3 at build time. |
| 573 | :return: None |
| 574 | :raises: AssertionError |
| 575 | """ |
| 576 | assert default is None or default >= 0 |
| 577 | assert 0 < size <= 8 |
| 578 | |
| 579 | super(FieldUVarLenField, self).__init__(name, default, size) |
| 580 | self._length_of = length_of |
| 581 | self._adjust = adjust |
| 582 | |
| 583 | def addfield(self, pkt, s, val): |
| 584 | # type: (Optional[packet.Packet], Union[str, Tuple[str, int, int]], Optional[int]) -> str # noqa: E501 |
| 585 | """ |
| 586 | :param packet.Packet|None pkt: the packet instance containing this field instance. This parameter must not be # noqa: E501 |
| 587 | None if the val parameter is. |
| 588 | :param str|(str, int, long) s: the string to append this field to. A tuple indicates that some bits were already # noqa: E501 |
| 589 | generated by another bitfield-compatible field. This MUST be the case if "size" is not 8. The int is the # noqa: E501 |
| 590 | number of bits already generated in the first byte of the str. The long is the value that was generated by the # noqa: E501 |
| 591 | previous bitfield-compatible fields. |
| 592 | :param int|None val: the positive or null value to be added. If None, the value is computed from pkt. # noqa: E501 |
| 593 | :return: str: s concatenated with the machine representation of this field. # noqa: E501 |
| 594 | :raises: AssertionError |
| 595 | """ |
| 596 | if val is None: |
| 597 | assert isinstance(pkt, packet.Packet), \ |
| 598 | 'EINVAL: pkt: Packet expected when val is None; received {}'.format(type(pkt)) # noqa: E501 |
| 599 | val = self._compute_value(pkt) |
| 600 | return super(FieldUVarLenField, self).addfield(pkt, s, val) |
| 601 | |
| 602 | def i2m(self, pkt, x): |
| 603 | # type: (Optional[packet.Packet], Optional[int]) -> str |
| 604 | """ |
| 605 | :param packet.Packet|None pkt: the packet instance containing this field instance. This parameter must not be # noqa: E501 |
| 606 | None if the x parameter is. |
| 607 | :param int|None x: the positive or null value to be added. If None, the value is computed from pkt. # noqa: E501 |
| 608 | :return: str |
| 609 | :raises: AssertionError |
| 610 | """ |
| 611 | if x is None: |
| 612 | assert isinstance(pkt, packet.Packet), \ |
no outgoing calls
no test coverage detected
searching dependent graphs…