diff --git a/scapy/fields.py b/scapy/fields.py index 914d320d3e5f5c71e057fb15f6d8fccd151002a7..0bd480ac660866d00ea161e8fd9111538d0417fd 100644 --- a/scapy/fields.py +++ b/scapy/fields.py @@ -158,14 +158,17 @@ class PadField: self._align = align self._padwith = padwith or "" + def padlen(self, flen): + return -flen%self._align + def getfield(self, pkt, s): - x = self._fld.getfield(pkt,s) - padlen = ((self._align - (len(x[1]) % self._align)) % self._align) - return x[0][padlen:], x[1] + remain,val = self._fld.getfield(pkt,s) + padlen = self.padlen(len(s)-len(remain)) + return remain[padlen:], val def addfield(self, pkt, s, val): sval = self._fld.addfield(pkt, "", val) - return s+sval+struct.pack("%is" % (-len(sval)%self._align), self._padwith) + return s+sval+struct.pack("%is" % (self.padlen(len(sval))), self._padwith) def __getattr__(self, attr): return getattr(self._fld,attr) diff --git a/test/regression.uts b/test/regression.uts index 069c617ddb4520d604289e7e1527f80cb0d75e18..ee7e71409ab93edda2117e67e0ad8f75469bfd37 100644 --- a/test/regression.uts +++ b/test/regression.uts @@ -135,6 +135,13 @@ assert( _.len == 23 and len(_) == 26 ) IP(str(IP()/Raw("ABC")/Padding("abc")/Padding("def"))) assert( _.len == 23 and len(_) == 29 ) += PadField test +~ PadField padding + +class TestPad(Packet): + fields_desc = [ PadField(StrNullField("st", ""),4), StrField("id", "")] + +TestPad() == TestPad(str(TestPad())) ############