StrField with a length :param length_from: a function that returns the size of the string :param max_length: max size to use as randval
| 1968 | |
| 1969 | |
| 1970 | class StrLenField(StrField): |
| 1971 | """ |
| 1972 | StrField with a length |
| 1973 | |
| 1974 | :param length_from: a function that returns the size of the string |
| 1975 | :param max_length: max size to use as randval |
| 1976 | """ |
| 1977 | __slots__ = ["length_from", "max_length"] |
| 1978 | ON_WIRE_SIZE_UTF16 = True |
| 1979 | |
| 1980 | def __init__( |
| 1981 | self, |
| 1982 | name, # type: str |
| 1983 | default, # type: bytes |
| 1984 | length_from=None, # type: Optional[Callable[[Packet], int]] |
| 1985 | max_length=None, # type: Optional[Any] |
| 1986 | ): |
| 1987 | # type: (...) -> None |
| 1988 | super(StrLenField, self).__init__(name, default) |
| 1989 | self.length_from = length_from |
| 1990 | self.max_length = max_length |
| 1991 | |
| 1992 | def getfield(self, pkt, s): |
| 1993 | # type: (Any, bytes) -> Tuple[bytes, bytes] |
| 1994 | len_pkt = (self.length_from or (lambda x: 0))(pkt) |
| 1995 | if not self.ON_WIRE_SIZE_UTF16: |
| 1996 | len_pkt *= 2 |
| 1997 | if len_pkt == 0: |
| 1998 | return s, b"" |
| 1999 | return s[len_pkt:], self.m2i(pkt, s[:len_pkt]) |
| 2000 | |
| 2001 | def randval(self): |
| 2002 | # type: () -> RandBin |
| 2003 | return RandBin(RandNum(0, self.max_length or 1200)) |
| 2004 | |
| 2005 | |
| 2006 | class _XStrField(Field[bytes, bytes]): |
no outgoing calls
no test coverage detected