diff --git a/scapy/fields.py b/scapy/fields.py index 0843adc42c616e55dd31bed416858de6b47efa6e..4f2fcc60fff31567f580189abbe3283e8a5843ce 100644 --- a/scapy/fields.py +++ b/scapy/fields.py @@ -783,6 +783,8 @@ class BitField(Field): return s,b def randval(self): return RandNum(0,2**self.size-1) + def i2len(self, pkt, x): + return float(self.size)/8 class BitFieldLenField(BitField): diff --git a/test/regression.uts b/test/regression.uts index 639865389b59c406ebc62c712678ea0383494633..0decf2dc4ea1b755631609bcb8991a5f6acbb41c 100644 --- a/test/regression.uts +++ b/test/regression.uts @@ -8043,6 +8043,7 @@ r1 == b'@\x04\x08\x08\x08\x08' r2 = b.dec(r1)[0] r2.val == '8.8.8.8' + ############ ############ + inet.py @@ -8193,3 +8194,22 @@ def test_IPID_count(): assert(lines[0].endswith("Probably 3 classes: [4613, 53881, 58437]")) test_IPID_count() + + +############ +############ ++ Fields + += FieldLenField with BitField +class Test(Packet): + name = "Test" + fields_desc = [ + FieldLenField("BitCount", None, fmt="H", count_of="Values"), + FieldLenField("ByteCount", None, fmt="B", length_of="Values"), + FieldListField("Values", [], BitField("data", 0x0, size=1), + count_from=lambda pkt: pkt.BitCount), + ] + +pkt = Test(str(Test(Values=[0, 0, 0, 0, 1, 1, 1, 1]))) +assert(pkt.BitCount == 8) +assert(pkt.ByteCount == 1)