From 0227a5157c655a17dcee79287ac89a184599d295 Mon Sep 17 00:00:00 2001 From: Pierre LALET <pierre.lalet@cea.fr> Date: Mon, 17 Apr 2017 14:41:43 +0200 Subject: [PATCH] Fields/BitField: implement .i2len() --- scapy/fields.py | 2 ++ test/regression.uts | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/scapy/fields.py b/scapy/fields.py index 0843adc4..4f2fcc60 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 9a614020..daefcafb 100644 --- a/test/regression.uts +++ b/test/regression.uts @@ -7969,6 +7969,7 @@ r1 == b'@\x04\x08\x08\x08\x08' r2 = b.dec(r1)[0] r2.val == '8.8.8.8' + ############ ############ + inet.py @@ -8113,3 +8114,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) -- GitLab