Skip to content

Add align_from attribute to PadField #4553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions scapy/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,17 +625,19 @@ def fld(self):
class PadField(_FieldContainer):
"""Add bytes after the proxified field so that it ends at the specified
alignment from its beginning"""
__slots__ = ["fld", "_align", "_padwith"]
__slots__ = ["fld", "_align", "_align_from", "_padwith"]

def __init__(self, fld, align, padwith=None):
# type: (AnyField, int, Optional[bytes]) -> None
def __init__(self, fld, align=None, align_from=None, padwith=None):
# type: (AnyField, Optional[int], Optional[Callable[[Packet], int]], Optional[bytes]) -> None # noqa: E501
self.fld = fld
self._align = align
self._align_from = align_from or (lambda x: 0)
if align is not None:
self._align_from = lambda x, align=align: align # type: ignore
self._padwith = padwith or b"\x00"

def padlen(self, flen, pkt):
# type: (int, Packet) -> int
return -flen % self._align
return -flen % self._align_from(pkt)

def getfield(self,
pkt, # type: Packet
Expand Down