| 2176 | |
| 2177 | |
| 2178 | class FieldLenField(Field[int, int]): |
| 2179 | __slots__ = ["length_of", "count_of", "adjust"] |
| 2180 | |
| 2181 | def __init__( |
| 2182 | self, |
| 2183 | name, # type: str |
| 2184 | default, # type: Optional[Any] |
| 2185 | length_of=None, # type: Optional[str] |
| 2186 | fmt="H", # type: str |
| 2187 | count_of=None, # type: Optional[str] |
| 2188 | adjust=lambda pkt, x: x, # type: Callable[[Packet, int], int] |
| 2189 | ): |
| 2190 | # type: (...) -> None |
| 2191 | Field.__init__(self, name, default, fmt) |
| 2192 | self.length_of = length_of |
| 2193 | self.count_of = count_of |
| 2194 | self.adjust = adjust |
| 2195 | |
| 2196 | def i2m(self, pkt, x): |
| 2197 | # type: (Optional[Packet], Optional[int]) -> int |
| 2198 | if x is None and pkt is not None: |
| 2199 | if self.length_of is not None: |
| 2200 | fld, fval = pkt.getfield_and_val(self.length_of) |
| 2201 | f = fld.i2len(pkt, fval) |
| 2202 | elif self.count_of is not None: |
| 2203 | fld, fval = pkt.getfield_and_val(self.count_of) |
| 2204 | f = fld.i2count(pkt, fval) |
| 2205 | else: |
| 2206 | raise ValueError( |
| 2207 | "Field should have either length_of or count_of" |
| 2208 | ) |
| 2209 | x = self.adjust(pkt, f) |
| 2210 | elif x is None: |
| 2211 | x = 0 |
| 2212 | return x |
| 2213 | |
| 2214 | |
| 2215 | class StrNullField(StrField): |
no outgoing calls
no test coverage detected