Skip to content
Snippets Groups Projects
Commit 50915ed7 authored by Phil's avatar Phil
Browse files

Fix PadField.getfield() pad length calculation (ticket #358)

parent 0505db94
No related branches found
No related tags found
No related merge requests found
...@@ -158,14 +158,17 @@ class PadField: ...@@ -158,14 +158,17 @@ class PadField:
self._align = align self._align = align
self._padwith = padwith or "" self._padwith = padwith or ""
def padlen(self, flen):
return -flen%self._align
def getfield(self, pkt, s): def getfield(self, pkt, s):
x = self._fld.getfield(pkt,s) remain,val = self._fld.getfield(pkt,s)
padlen = ((self._align - (len(x[1]) % self._align)) % self._align) padlen = self.padlen(len(s)-len(remain))
return x[0][padlen:], x[1] return remain[padlen:], val
def addfield(self, pkt, s, val): def addfield(self, pkt, s, val):
sval = self._fld.addfield(pkt, "", 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): def __getattr__(self, attr):
return getattr(self._fld,attr) return getattr(self._fld,attr)
......
...@@ -135,6 +135,13 @@ assert( _.len == 23 and len(_) == 26 ) ...@@ -135,6 +135,13 @@ assert( _.len == 23 and len(_) == 26 )
IP(str(IP()/Raw("ABC")/Padding("abc")/Padding("def"))) IP(str(IP()/Raw("ABC")/Padding("abc")/Padding("def")))
assert( _.len == 23 and len(_) == 29 ) 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()))
############ ############
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment