| 1115 | |
| 1116 | |
| 1117 | class NBytesField(Field[int, List[int]]): |
| 1118 | def __init__(self, name, default, sz): |
| 1119 | # type: (str, Optional[int], int) -> None |
| 1120 | Field.__init__(self, name, default, "<" + "B" * sz) |
| 1121 | |
| 1122 | def i2m(self, pkt, x): |
| 1123 | # type: (Optional[Packet], Optional[int]) -> List[int] |
| 1124 | if x is None: |
| 1125 | return [0] * self.sz |
| 1126 | x2m = list() |
| 1127 | for _ in range(self.sz): |
| 1128 | x2m.append(x % 256) |
| 1129 | x //= 256 |
| 1130 | return x2m[::-1] |
| 1131 | |
| 1132 | def m2i(self, pkt, x): |
| 1133 | # type: (Optional[Packet], Union[List[int], int]) -> int |
| 1134 | if isinstance(x, int): |
| 1135 | return x |
| 1136 | # x can be a tuple when coming from struct.unpack (from getfield) |
| 1137 | if isinstance(x, (list, tuple)): |
| 1138 | return sum(d * (256 ** i) for i, d in enumerate(reversed(x))) |
| 1139 | return 0 |
| 1140 | |
| 1141 | def i2repr(self, pkt, x): |
| 1142 | # type: (Optional[Packet], int) -> str |
| 1143 | if isinstance(x, int): |
| 1144 | return '%i' % x |
| 1145 | return super(NBytesField, self).i2repr(pkt, x) |
| 1146 | |
| 1147 | def addfield(self, pkt, s, val): |
| 1148 | # type: (Optional[Packet], bytes, Optional[int]) -> bytes |
| 1149 | return s + self.struct.pack(*self.i2m(pkt, val)) |
| 1150 | |
| 1151 | def getfield(self, pkt, s): |
| 1152 | # type: (Optional[Packet], bytes) -> Tuple[bytes, int] |
| 1153 | return (s[self.sz:], |
| 1154 | self.m2i(pkt, self.struct.unpack(s[:self.sz]))) # type: ignore |
| 1155 | |
| 1156 | def randval(self): |
| 1157 | # type: () -> RandNum |
| 1158 | return RandNum(0, 2 ** (self.sz * 8) - 1) |
| 1159 | |
| 1160 | |
| 1161 | class XNBytesField(NBytesField): |
no outgoing calls
no test coverage detected
searching dependent graphs…