Fix encoding issues with python 3
In python 3, the default encoding was switched from ascii character sets to unicode character sets in order to support internationalization by default. Some interfaces, like ioctls and packets, however, specify data in terms of non-unicode encodings formats, either in host endian (`fcntl.ioctl`) or network endian (`dpkt`) byte order/format. This change alters assumptions made by previous code where it was all data objects were assumed to be basestrings, when they should have been treated as byte arrays. In order to achieve this the following are done: * str objects with encodings needing to be encoded as ascii byte arrays are done so via `.encode("ascii")`. In order for this to work on python 3 in a type agnostic way (as it anecdotally varied depending on the caller), call `.encode("ascii")` only on str objects with python 3 to cast them to ascii byte arrays in a helper function name `str_to_ascii(..)`. * `dpkt.Packet` objects needing to be passed in to `fcntl.ioctl(..)` are done so by casting them to byte arrays via `bytes()`, which calls `dpkt.Packet__str__` under the covers and does the necessary str to byte array conversion needed for the `dpkt` APIs and `struct` module. In order to accomodate this change, apply the necessary typecasting for the byte array literal in order to search `fop.name` for nul bytes. This resolves all remaining python 2.x and python 3.x compatibility issues on amd64. More work needs to be done for the tests to function with i386, in general (this is a legacy issue). PR: 237403 MFC after: 1 week Tested with: python 2.7.16 (amd64), python 3.6.8 (amd64)
This commit is contained in:
parent
4d6fa83c73
commit
ffbc8cc0f2
@ -136,16 +136,22 @@ def _getdev():
|
||||
|
||||
_cryptodev = _getdev()
|
||||
|
||||
def str_to_ascii(val):
|
||||
if sys.version_info[0] >= 3:
|
||||
if isinstance(val, str):
|
||||
return val.encode("ascii")
|
||||
return val
|
||||
|
||||
def _findop(crid, name):
|
||||
fop = FindOp()
|
||||
fop.crid = crid
|
||||
fop.name = name
|
||||
fop.name = str_to_ascii(name)
|
||||
s = array.array('B', fop.pack_hdr())
|
||||
ioctl(_cryptodev, CIOCFINDDEV, s, 1)
|
||||
fop.unpack(s)
|
||||
|
||||
try:
|
||||
idx = fop.name.index('\x00')
|
||||
idx = fop.name.index(b'\x00')
|
||||
name = fop.name[:idx]
|
||||
except ValueError:
|
||||
name = fop.name
|
||||
@ -218,11 +224,11 @@ def _doop(self, op, src, iv):
|
||||
if self._maclen is not None:
|
||||
m = array.array('B', [0] * self._maclen)
|
||||
cop.mac = m.buffer_info()[0]
|
||||
ivbuf = array.array('B', iv)
|
||||
ivbuf = array.array('B', str_to_ascii(iv))
|
||||
cop.iv = ivbuf.buffer_info()[0]
|
||||
|
||||
#print('cop:', cop)
|
||||
ioctl(_cryptodev, CIOCCRYPT, str(cop))
|
||||
ioctl(_cryptodev, CIOCCRYPT, bytes(cop))
|
||||
|
||||
s = array_tobytes(s)
|
||||
if self._maclen is not None:
|
||||
@ -236,6 +242,7 @@ def _doaead(self, op, src, aad, iv, tag=None):
|
||||
caead.op = op
|
||||
caead.flags = CRD_F_IV_EXPLICIT
|
||||
caead.flags = 0
|
||||
src = str_to_ascii(src)
|
||||
caead.len = len(src)
|
||||
s = array.array('B', src)
|
||||
caead.src = caead.dst = s.buffer_info()[0]
|
||||
@ -246,6 +253,7 @@ def _doaead(self, op, src, aad, iv, tag=None):
|
||||
if self._maclen is None:
|
||||
raise ValueError('must have a tag length')
|
||||
|
||||
tag = str_to_ascii(tag)
|
||||
if tag is None:
|
||||
tag = array.array('B', [0] * self._maclen)
|
||||
else:
|
||||
@ -259,7 +267,7 @@ def _doaead(self, op, src, aad, iv, tag=None):
|
||||
caead.ivlen = len(iv)
|
||||
caead.iv = ivbuf.buffer_info()[0]
|
||||
|
||||
ioctl(_cryptodev, CIOCCRYPTAEAD, str(caead))
|
||||
ioctl(_cryptodev, CIOCCRYPTAEAD, bytes(caead))
|
||||
|
||||
s = array_tobytes(s)
|
||||
|
||||
@ -267,6 +275,7 @@ def _doaead(self, op, src, aad, iv, tag=None):
|
||||
|
||||
def perftest(self, op, size, timeo=3):
|
||||
inp = array.array('B', (random.randint(0, 255) for x in range(size)))
|
||||
inp = str_to_ascii(inp)
|
||||
out = array.array('B', inp)
|
||||
|
||||
# prep ioctl
|
||||
@ -293,8 +302,9 @@ def alarmhandle(a, b, exit=exit):
|
||||
|
||||
start = time.time()
|
||||
reps = 0
|
||||
cop = bytes(cop)
|
||||
while not exit[0]:
|
||||
ioctl(_cryptodev, CIOCCRYPT, str(cop))
|
||||
ioctl(_cryptodev, CIOCCRYPT, cop)
|
||||
reps += 1
|
||||
|
||||
end = time.time()
|
||||
|
Loading…
Reference in New Issue
Block a user