MCPcopy Index your code
hub / github.com/secdev/scapy / StrFixedLenField

Class StrFixedLenField

scapy/fields.py:1860–1905  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1858
1859
1860class StrFixedLenField(StrField):
1861 __slots__ = ["length_from"]
1862
1863 def __init__(
1864 self,
1865 name, # type: str
1866 default, # type: Optional[bytes]
1867 length=None, # type: Optional[int]
1868 length_from=None, # type: Optional[Callable[[Packet], int]]
1869 ):
1870 # type: (...) -> None
1871 super(StrFixedLenField, self).__init__(name, default)
1872 self.length_from = length_from or (lambda x: 0)
1873 if length is not None:
1874 self.sz = length
1875 self.length_from = lambda x, length=length: length # type: ignore
1876
1877 def i2repr(self,
1878 pkt, # type: Optional[Packet]
1879 v, # type: bytes
1880 ):
1881 # type: (...) -> str
1882 if isinstance(v, bytes) and not conf.debug_strfixedlenfield:
1883 v = v.rstrip(b"\0")
1884 return super(StrFixedLenField, self).i2repr(pkt, v)
1885
1886 def getfield(self, pkt, s):
1887 # type: (Packet, bytes) -> Tuple[bytes, bytes]
1888 len_pkt = self.length_from(pkt)
1889 if len_pkt == 0:
1890 return s, b""
1891 return s[len_pkt:], self.m2i(pkt, s[:len_pkt])
1892
1893 def addfield(self, pkt, s, val):
1894 # type: (Packet, bytes, Optional[bytes]) -> bytes
1895 len_pkt = self.length_from(pkt)
1896 if len_pkt is None:
1897 return s + self.i2m(pkt, val)
1898 return s + struct.pack("%is" % len_pkt, self.i2m(pkt, val))
1899
1900 def randval(self):
1901 # type: () -> RandBin
1902 try:
1903 return RandBin(self.length_from(None)) # type: ignore
1904 except Exception:
1905 return RandBin(RandNum(0, 200))
1906
1907
1908class StrFixedLenFieldUtf16(StrFixedLenField, StrFieldUtf16):

Callers 15

sockaddrClass · 0.90
if_dataClass · 0.90
rt_metricsClass · 0.90
MQTTSNSubscribeClass · 0.90
MQTTSNUnsubscribeClass · 0.90
VS_UART_CMD_CNFClass · 0.90
CM_SLAC_PARM_REQClass · 0.90
CM_SLAC_PARM_CNFClass · 0.90
CM_ATTEN_CHAR_INDClass · 0.90
CM_ATTEN_CHAR_RSPClass · 0.90
SLAC_varfieldClass · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected