Skip to content

Commit

Permalink
one more refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
obormot committed Dec 27, 2020
1 parent 6ce6a3a commit 9dbe8fa
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 9 deletions.
9 changes: 4 additions & 5 deletions dpkt/compat.py
@@ -1,6 +1,6 @@
from __future__ import absolute_import

import struct
from struct import pack, unpack
import sys

if sys.version_info < (3,):
Expand Down Expand Up @@ -40,9 +40,8 @@ def iteritems(d, **kw):
intround = round


def ntole(fmt, v):
"""convert a 2-byte word (fmt='H') or a 4-byte integer (fmt='I')
from the network byte order (big endian) to little endian;
def ntole(v):
"""convert a 2-byte word from the network byte order (big endian) to little endian;
replaces socket.ntohs() to work on both little and big endian architectures
"""
return struct.unpack('<' + fmt, struct.pack('!' + fmt, v))[0]
return unpack('<H', pack('!H', v))[0]
2 changes: 1 addition & 1 deletion dpkt/dpkt.py
Expand Up @@ -208,7 +208,7 @@ def in_cksum_add(s, buf):
def in_cksum_done(s):
s = (s >> 16) + (s & 0xffff)
s += (s >> 16)
return ntole('H', ~s & 0xffff)
return ntole(~s & 0xffff)


def in_cksum(buf):
Expand Down
4 changes: 2 additions & 2 deletions dpkt/ieee80211.py
Expand Up @@ -363,7 +363,7 @@ def unpack(self, buf):
if self.type == MGMT_TYPE:
self.unpack_ies(field.data)
if self.subtype in FRAMES_WITH_CAPABILITY:
self.capability = self.Capability(ntole('H', field.capability))
self.capability = self.Capability(ntole(field.capability))

if self.type == DATA_TYPE and self.subtype == D_QOS_DATA:
self.qos_data = self.QoS_Data(field.data)
Expand Down Expand Up @@ -422,7 +422,7 @@ def tid(self, val):
def unpack(self, buf):
dpkt.Packet.unpack(self, buf)
self.data = buf[self.__hdr_len__:]
self.ctl = ntole('H', self.ctl)
self.ctl = ntole(self.ctl)

if self.compressed:
self.bmp = struct.unpack('8s', self.data[0:_COMPRESSED_BMP_LENGTH])[0]
Expand Down
2 changes: 1 addition & 1 deletion dpkt/radiotap.py
Expand Up @@ -233,7 +233,7 @@ def ext_present(self, val):

def unpack(self, buf):
dpkt.Packet.unpack(self, buf)
self.data = buf[ntole('H', self.length):]
self.data = buf[ntole(self.length):]

self.fields = []
buf = buf[self.__hdr_len__:]
Expand Down

0 comments on commit 9dbe8fa

Please sign in to comment.