本文整理汇总了Python中scapy.fields.BitField方法的典型用法代码示例。如果您正苦于以下问题:Python fields.BitField方法的具体用法?Python fields.BitField怎么用?Python fields.BitField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scapy.fields
的用法示例。
在下文中一共展示了fields.BitField方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getfield
# 需要导入模块: from scapy import fields [as 别名]
# 或者: from scapy.fields import BitField [as 别名]
def getfield(self, pkt, s):
# type: (Optional[packet.Packet], Union[str, Tuple[str, int]]) -> Tuple[Union[Tuple[str, int], str], int] # noqa: E501
"""
:param packet.Packet|None pkt: the packet instance containing this field instance; probably unused. # noqa: E501
: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
bits already consumed by previous bitfield-compatible fields.
:return: (str|(str, int), int): Returns the remaining string and the parsed value. May return a tuple if there # noqa: E501
are remaining bits to parse in the first byte. Returned value is equal to default value # noqa: E501
:raises: AssertionError
"""
r = super(HPackMagicBitField, self).getfield(pkt, s)
assert (
isinstance(r, tuple) and
len(r) == 2 and
isinstance(r[1], six.integer_types)
), 'Second element of BitField.getfield return value expected to be an int or a long; API change detected' # noqa: E501
assert r[1] == self._magic, 'Invalid value parsed from s; error in class guessing detected!' # noqa: E501
return r
示例2: addfield
# 需要导入模块: from scapy import fields [as 别名]
# 或者: from scapy.fields import BitField [as 别名]
def addfield(self, pkt, s, val):
# type: (Optional[packet.Packet], Union[str, Tuple[str, int, int]], int) -> str # noqa: E501
"""
An AbstractUVarIntField prefix always consumes the remaining bits
of a BitField;if no current BitField is in use (no tuple in
entry) then the prefix length is 8 bits and the whole byte is to
be consumed
:param packet.Packet|None pkt: the packet containing this field.
Probably unused.
:param str|(str, int, long) s: the string to append this field to.
A tuple indicates that some bits were already generated by another
bitfield-compatible field. This MUST be the case if "size" is not 8.
The int is the number of bits already generated in the first byte of
the str. The long is the value that was generated by the previous
bitfield-compatible fields.
:param int val: the positive or null value to be added.
:return: str: s concatenated with the machine representation of this
field.
:raises: AssertionError
"""
assert(val >= 0)
if isinstance(s, bytes):
assert self.size == 8, 'EINVAL: s: tuple expected when prefix_len is not a full byte' # noqa: E501
return s + self.i2m(pkt, val)
# s is a tuple
# assert(s[1] >= 0)
# assert(s[2] >= 0)
# assert (8 - s[1]) == self.size, 'EINVAL: s: not enough bits remaining in current byte to read the prefix' # noqa: E501
if val >= self._max_value:
return s[0] + chb((s[2] << self.size) + self._max_value) + self.i2m(pkt, val)[1:] # noqa: E501
# This AbstractUVarIntField is only one byte long; setting the prefix value # noqa: E501
# and appending the resulting byte to the string
return s[0] + chb((s[2] << self.size) + orb(self.i2m(pkt, val)))