MCPcopy Index your code
hub / github.com/secdev/scapy / getfield

Method getfield

scapy/fields.py:1785–1846  ·  view source on GitHub ↗
(self, pkt, s)

Source from the content-addressed store, hash-verified

1783 return sum(len(self.i2m(pkt, p)) for p in val)
1784
1785 def getfield(self, pkt, s):
1786 # type: (Packet, bytes) -> Tuple[bytes, List[BasePacket]]
1787 c = len_pkt = cls = None
1788 if self.length_from is not None:
1789 len_pkt = self.length_from(pkt)
1790 elif self.count_from is not None:
1791 c = self.count_from(pkt)
1792 if self.next_cls_cb is not None:
1793 cls = self.next_cls_cb(pkt, [], None, s)
1794 c = 1
1795 if cls is None:
1796 c = 0
1797
1798 lst = [] # type: List[BasePacket]
1799 ret = b""
1800 remain = s
1801 if len_pkt is not None:
1802 remain, ret = s[:len_pkt], s[len_pkt:]
1803 while remain:
1804 if c is not None:
1805 if c <= 0:
1806 break
1807 c -= 1
1808 try:
1809 if cls is not None:
1810 try:
1811 # we want to set parent wherever possible
1812 p = cls(remain, _parent=pkt)
1813 except TypeError:
1814 p = cls(remain)
1815 else:
1816 p = self.m2i(pkt, remain)
1817 except Exception:
1818 if conf.debug_dissector:
1819 raise
1820 p = conf.raw_layer(load=remain)
1821 remain = b""
1822 else:
1823 if conf.padding_layer in p:
1824 pad = p[conf.padding_layer]
1825 remain = pad.load
1826 del pad.underlayer.payload
1827 if self.next_cls_cb is not None:
1828 cls = self.next_cls_cb(pkt, lst, p, remain)
1829 if cls is not None:
1830 c = 0 if c is None else c
1831 c += 1
1832 else:
1833 remain = b""
1834 lst.append(p)
1835 if len(lst) > (self.max_count or conf.max_list_count):
1836 raise MaximumItemsCount(
1837 "Maximum amount of items reached in PacketListField: %s "
1838 "(defaults to conf.max_list_count)"
1839 % (self.max_count or conf.max_list_count)
1840 )
1841
1842 if isinstance(remain, tuple):

Callers 1

_ParseStringArrayFunction · 0.95

Calls 5

MaximumItemsCountClass · 0.85
next_cls_cbMethod · 0.80
length_fromMethod · 0.45
m2iMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected