MCPcopy Create free account
hub / github.com/secdev/scapy / HPackMagicBitField

Class HPackMagicBitField

scapy/contrib/http2.py:53–167  ·  view source on GitHub ↗

HPackMagicBitField is a BitField variant that cannot be assigned another value than the default one. This field must not be used where there is potential for fuzzing. OTOH, this field makes sense (for instance, if the magic bits are used by a dispatcher to select the payload class)

Source from the content-addressed store, hash-verified

51
52
53class HPackMagicBitField(fields.BitField):
54 """ HPackMagicBitField is a BitField variant that cannot be assigned another
55 value than the default one. This field must not be used where there is
56 potential for fuzzing. OTOH, this field makes sense (for instance, if the
57 magic bits are used by a dispatcher to select the payload class)
58 """
59
60 __slots__ = ['_magic']
61
62 def __init__(self, name, default, size):
63 # type: (str, int, int) -> None
64 """
65 :param str name: this field instance name.
66 :param int default: this field only valid value.
67 :param int size: this bitfield bitlength.
68 :return: None
69 :raises: AssertionError
70 """
71 assert default >= 0
72 # size can be negative if encoding is little-endian (see rev property of bitfields) # noqa: E501
73 assert size != 0
74 self._magic = default
75 super(HPackMagicBitField, self).__init__(name, default, size)
76
77 def addfield(self, pkt, s, val):
78 # type: (Optional[packet.Packet], Union[str, Tuple[str, int, int]], int) -> Union[str, Tuple[str, int, int]] # noqa: E501
79 """
80 :param packet.Packet|None pkt: the packet instance containing this field instance; probably unused. # noqa: E501
81 :param str|(str, int, long) s: either a str if 0 == size%8 or a tuple with the string to add this field to, the # noqa: E501
82 number of bits already generated and the generated value so far.
83 :param int val: unused; must be equal to default value
84 :return: str|(str, int, long): the s string extended with this field machine representation # noqa: E501
85 :raises: AssertionError
86 """
87 assert val == self._magic, 'val parameter must value {}; received: {}'.format(self._magic, val) # noqa: E501
88 return super(HPackMagicBitField, self).addfield(pkt, s, self._magic)
89
90 def getfield(self, pkt, s):
91 # type: (Optional[packet.Packet], Union[str, Tuple[str, int]]) -> Tuple[Union[Tuple[str, int], str], int] # noqa: E501
92 """
93 :param packet.Packet|None pkt: the packet instance containing this field instance; probably unused. # noqa: E501
94 :param str|(str, int) s: either a str if size%8==0 or a tuple with the string to parse from and the number of # noqa: E501
95 bits already consumed by previous bitfield-compatible fields.
96 :return: (str|(str, int), int): Returns the remaining string and the parsed value. May return a tuple if there # noqa: E501
97 are remaining bits to parse in the first byte. Returned value is equal to default value # noqa: E501
98 :raises: AssertionError
99 """
100 r = super(HPackMagicBitField, self).getfield(pkt, s)
101 assert (
102 isinstance(r, tuple) and
103 len(r) == 2 and
104 isinstance(r[1], int)
105 ), 'Second element of BitField.getfield return value expected to be an int or a long; API change detected' # noqa: E501
106 assert r[1] == self._magic, 'Invalid value parsed from s; error in class guessing detected!' # noqa: E501
107 return r
108
109 def h2i(self, pkt, x):
110 # type: (Optional[packet.Packet], int) -> int

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…