1996-08-19 19:26:43 -03:00
|
|
|
"""Implements (a subset of) Sun XDR -- eXternal Data Representation.
|
|
|
|
|
|
|
|
See: RFC 1014
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import struct
|
2007-07-26 00:19:46 -03:00
|
|
|
from io import BytesIO
|
2014-10-10 15:21:52 -03:00
|
|
|
from functools import wraps
|
2022-04-29 22:22:10 -03:00
|
|
|
import warnings
|
|
|
|
|
|
|
|
warnings._deprecated(__name__, remove=(3, 13))
|
1996-08-19 19:26:43 -03:00
|
|
|
|
2001-03-01 00:27:19 -04:00
|
|
|
__all__ = ["Error", "Packer", "Unpacker", "ConversionError"]
|
|
|
|
|
1996-08-19 19:26:43 -03:00
|
|
|
# exceptions
|
2002-03-31 09:59:18 -04:00
|
|
|
class Error(Exception):
|
1996-08-19 19:26:43 -03:00
|
|
|
"""Exception class for this module. Use:
|
|
|
|
|
2012-11-01 08:43:06 -03:00
|
|
|
except xdrlib.Error as var:
|
1996-08-19 19:26:43 -03:00
|
|
|
# var has the Error instance for the exception
|
|
|
|
|
|
|
|
Public ivars:
|
|
|
|
msg -- contains the message
|
|
|
|
|
|
|
|
"""
|
|
|
|
def __init__(self, msg):
|
1998-03-26 17:13:24 -04:00
|
|
|
self.msg = msg
|
1996-08-19 19:26:43 -03:00
|
|
|
def __repr__(self):
|
1998-03-26 17:13:24 -04:00
|
|
|
return repr(self.msg)
|
1996-08-19 19:26:43 -03:00
|
|
|
def __str__(self):
|
1998-03-26 17:13:24 -04:00
|
|
|
return str(self.msg)
|
1996-08-19 19:26:43 -03:00
|
|
|
|
|
|
|
|
|
|
|
class ConversionError(Error):
|
|
|
|
pass
|
|
|
|
|
2014-10-10 15:21:52 -03:00
|
|
|
def raise_conversion_error(function):
|
|
|
|
""" Wrap any raised struct.errors in a ConversionError. """
|
|
|
|
|
|
|
|
@wraps(function)
|
|
|
|
def result(self, value):
|
|
|
|
try:
|
|
|
|
return function(self, value)
|
|
|
|
except struct.error as e:
|
|
|
|
raise ConversionError(e.args[0]) from None
|
|
|
|
return result
|
1996-08-19 19:26:43 -03:00
|
|
|
|
2001-01-14 23:34:38 -04:00
|
|
|
|
1996-08-19 19:26:43 -03:00
|
|
|
class Packer:
|
|
|
|
"""Pack various data representations into a buffer."""
|
|
|
|
|
|
|
|
def __init__(self):
|
1998-03-26 17:13:24 -04:00
|
|
|
self.reset()
|
1996-08-19 19:26:43 -03:00
|
|
|
|
|
|
|
def reset(self):
|
2007-07-26 00:19:46 -03:00
|
|
|
self.__buf = BytesIO()
|
1996-08-19 19:26:43 -03:00
|
|
|
|
|
|
|
def get_buffer(self):
|
2001-08-16 14:06:44 -03:00
|
|
|
return self.__buf.getvalue()
|
1996-08-19 19:26:43 -03:00
|
|
|
# backwards compatibility
|
|
|
|
get_buf = get_buffer
|
|
|
|
|
2014-10-10 15:21:52 -03:00
|
|
|
@raise_conversion_error
|
1996-08-19 19:26:43 -03:00
|
|
|
def pack_uint(self, x):
|
2001-08-16 14:06:44 -03:00
|
|
|
self.__buf.write(struct.pack('>L', x))
|
1996-08-19 19:26:43 -03:00
|
|
|
|
2014-10-10 15:21:52 -03:00
|
|
|
@raise_conversion_error
|
2011-03-27 12:25:40 -03:00
|
|
|
def pack_int(self, x):
|
|
|
|
self.__buf.write(struct.pack('>l', x))
|
|
|
|
|
1996-08-19 19:26:43 -03:00
|
|
|
pack_enum = pack_int
|
|
|
|
|
|
|
|
def pack_bool(self, x):
|
2007-07-26 00:19:46 -03:00
|
|
|
if x: self.__buf.write(b'\0\0\0\1')
|
|
|
|
else: self.__buf.write(b'\0\0\0\0')
|
1996-08-19 19:26:43 -03:00
|
|
|
|
|
|
|
def pack_uhyper(self, x):
|
2014-10-10 15:21:52 -03:00
|
|
|
try:
|
|
|
|
self.pack_uint(x>>32 & 0xffffffff)
|
|
|
|
except (TypeError, struct.error) as e:
|
|
|
|
raise ConversionError(e.args[0]) from None
|
|
|
|
try:
|
|
|
|
self.pack_uint(x & 0xffffffff)
|
|
|
|
except (TypeError, struct.error) as e:
|
|
|
|
raise ConversionError(e.args[0]) from None
|
1996-08-19 19:26:43 -03:00
|
|
|
|
|
|
|
pack_hyper = pack_uhyper
|
|
|
|
|
2014-10-10 15:21:52 -03:00
|
|
|
@raise_conversion_error
|
1996-08-19 19:26:43 -03:00
|
|
|
def pack_float(self, x):
|
2014-10-10 15:21:52 -03:00
|
|
|
self.__buf.write(struct.pack('>f', x))
|
1997-01-02 18:52:15 -04:00
|
|
|
|
2014-10-10 15:21:52 -03:00
|
|
|
@raise_conversion_error
|
1996-08-19 19:26:43 -03:00
|
|
|
def pack_double(self, x):
|
2014-10-10 15:21:52 -03:00
|
|
|
self.__buf.write(struct.pack('>d', x))
|
1996-08-19 19:26:43 -03:00
|
|
|
|
|
|
|
def pack_fstring(self, n, s):
|
1998-03-26 17:13:24 -04:00
|
|
|
if n < 0:
|
2007-08-29 22:19:48 -03:00
|
|
|
raise ValueError('fstring size must be nonnegative')
|
1998-03-26 17:13:24 -04:00
|
|
|
data = s[:n]
|
2006-04-20 19:36:57 -03:00
|
|
|
n = ((n+3)//4)*4
|
2007-07-26 00:19:46 -03:00
|
|
|
data = data + (n - len(data)) * b'\0'
|
2001-08-16 14:06:44 -03:00
|
|
|
self.__buf.write(data)
|
1996-08-19 19:26:43 -03:00
|
|
|
|
|
|
|
pack_fopaque = pack_fstring
|
|
|
|
|
|
|
|
def pack_string(self, s):
|
1998-03-26 17:13:24 -04:00
|
|
|
n = len(s)
|
|
|
|
self.pack_uint(n)
|
|
|
|
self.pack_fstring(n, s)
|
1996-08-19 19:26:43 -03:00
|
|
|
|
|
|
|
pack_opaque = pack_string
|
|
|
|
pack_bytes = pack_string
|
|
|
|
|
|
|
|
def pack_list(self, list, pack_item):
|
1998-03-26 17:13:24 -04:00
|
|
|
for item in list:
|
|
|
|
self.pack_uint(1)
|
|
|
|
pack_item(item)
|
|
|
|
self.pack_uint(0)
|
1996-08-19 19:26:43 -03:00
|
|
|
|
|
|
|
def pack_farray(self, n, list, pack_item):
|
2000-12-12 19:20:45 -04:00
|
|
|
if len(list) != n:
|
2007-08-29 22:19:48 -03:00
|
|
|
raise ValueError('wrong array size')
|
1998-03-26 17:13:24 -04:00
|
|
|
for item in list:
|
|
|
|
pack_item(item)
|
1996-08-19 19:26:43 -03:00
|
|
|
|
|
|
|
def pack_array(self, list, pack_item):
|
1998-03-26 17:13:24 -04:00
|
|
|
n = len(list)
|
|
|
|
self.pack_uint(n)
|
|
|
|
self.pack_farray(n, list, pack_item)
|
1996-08-19 19:26:43 -03:00
|
|
|
|
|
|
|
|
2001-01-14 23:34:38 -04:00
|
|
|
|
1996-08-19 19:26:43 -03:00
|
|
|
class Unpacker:
|
|
|
|
"""Unpacks various data representations from the given buffer."""
|
|
|
|
|
|
|
|
def __init__(self, data):
|
1998-03-26 17:13:24 -04:00
|
|
|
self.reset(data)
|
1996-08-19 19:26:43 -03:00
|
|
|
|
|
|
|
def reset(self, data):
|
1998-03-26 17:13:24 -04:00
|
|
|
self.__buf = data
|
|
|
|
self.__pos = 0
|
1996-08-19 19:26:43 -03:00
|
|
|
|
|
|
|
def get_position(self):
|
1998-03-26 17:13:24 -04:00
|
|
|
return self.__pos
|
1996-08-19 19:26:43 -03:00
|
|
|
|
|
|
|
def set_position(self, position):
|
1998-03-26 17:13:24 -04:00
|
|
|
self.__pos = position
|
1996-08-19 19:26:43 -03:00
|
|
|
|
1996-12-04 18:04:39 -04:00
|
|
|
def get_buffer(self):
|
1998-03-26 17:13:24 -04:00
|
|
|
return self.__buf
|
1996-12-04 18:04:39 -04:00
|
|
|
|
1996-08-19 19:26:43 -03:00
|
|
|
def done(self):
|
1998-03-26 17:13:24 -04:00
|
|
|
if self.__pos < len(self.__buf):
|
|
|
|
raise Error('unextracted data remains')
|
1996-08-19 19:26:43 -03:00
|
|
|
|
|
|
|
def unpack_uint(self):
|
1998-03-26 17:13:24 -04:00
|
|
|
i = self.__pos
|
|
|
|
self.__pos = j = i+4
|
|
|
|
data = self.__buf[i:j]
|
|
|
|
if len(data) < 4:
|
|
|
|
raise EOFError
|
2011-10-23 17:11:00 -03:00
|
|
|
return struct.unpack('>L', data)[0]
|
1996-08-19 19:26:43 -03:00
|
|
|
|
|
|
|
def unpack_int(self):
|
1998-03-26 17:13:24 -04:00
|
|
|
i = self.__pos
|
|
|
|
self.__pos = j = i+4
|
|
|
|
data = self.__buf[i:j]
|
|
|
|
if len(data) < 4:
|
|
|
|
raise EOFError
|
|
|
|
return struct.unpack('>l', data)[0]
|
1996-08-19 19:26:43 -03:00
|
|
|
|
|
|
|
unpack_enum = unpack_int
|
2005-02-24 16:22:10 -04:00
|
|
|
|
|
|
|
def unpack_bool(self):
|
|
|
|
return bool(self.unpack_int())
|
1996-08-19 19:26:43 -03:00
|
|
|
|
|
|
|
def unpack_uhyper(self):
|
1998-03-26 17:13:24 -04:00
|
|
|
hi = self.unpack_uint()
|
|
|
|
lo = self.unpack_uint()
|
2007-01-15 12:59:06 -04:00
|
|
|
return int(hi)<<32 | lo
|
1996-08-19 19:26:43 -03:00
|
|
|
|
|
|
|
def unpack_hyper(self):
|
1998-03-26 17:13:24 -04:00
|
|
|
x = self.unpack_uhyper()
|
2007-01-15 12:59:06 -04:00
|
|
|
if x >= 0x8000000000000000:
|
|
|
|
x = x - 0x10000000000000000
|
1998-03-26 17:13:24 -04:00
|
|
|
return x
|
1996-08-19 19:26:43 -03:00
|
|
|
|
|
|
|
def unpack_float(self):
|
1998-03-26 17:13:24 -04:00
|
|
|
i = self.__pos
|
|
|
|
self.__pos = j = i+4
|
|
|
|
data = self.__buf[i:j]
|
|
|
|
if len(data) < 4:
|
|
|
|
raise EOFError
|
|
|
|
return struct.unpack('>f', data)[0]
|
1997-01-02 18:52:15 -04:00
|
|
|
|
1996-08-19 19:26:43 -03:00
|
|
|
def unpack_double(self):
|
1998-03-26 17:13:24 -04:00
|
|
|
i = self.__pos
|
|
|
|
self.__pos = j = i+8
|
|
|
|
data = self.__buf[i:j]
|
|
|
|
if len(data) < 8:
|
|
|
|
raise EOFError
|
|
|
|
return struct.unpack('>d', data)[0]
|
1996-08-19 19:26:43 -03:00
|
|
|
|
|
|
|
def unpack_fstring(self, n):
|
1998-03-26 17:13:24 -04:00
|
|
|
if n < 0:
|
2007-08-29 22:19:48 -03:00
|
|
|
raise ValueError('fstring size must be nonnegative')
|
1998-03-26 17:13:24 -04:00
|
|
|
i = self.__pos
|
2006-04-20 19:36:57 -03:00
|
|
|
j = i + (n+3)//4*4
|
1998-03-26 17:13:24 -04:00
|
|
|
if j > len(self.__buf):
|
|
|
|
raise EOFError
|
|
|
|
self.__pos = j
|
|
|
|
return self.__buf[i:i+n]
|
1996-08-19 19:26:43 -03:00
|
|
|
|
|
|
|
unpack_fopaque = unpack_fstring
|
|
|
|
|
|
|
|
def unpack_string(self):
|
1998-03-26 17:13:24 -04:00
|
|
|
n = self.unpack_uint()
|
|
|
|
return self.unpack_fstring(n)
|
1996-08-19 19:26:43 -03:00
|
|
|
|
|
|
|
unpack_opaque = unpack_string
|
|
|
|
unpack_bytes = unpack_string
|
|
|
|
|
|
|
|
def unpack_list(self, unpack_item):
|
1998-03-26 17:13:24 -04:00
|
|
|
list = []
|
2022-11-26 18:33:25 -04:00
|
|
|
while (x := self.unpack_uint()) != 0:
|
2000-12-12 19:20:45 -04:00
|
|
|
if x != 1:
|
2007-08-29 22:19:48 -03:00
|
|
|
raise ConversionError('0 or 1 expected, got %r' % (x,))
|
1998-03-26 17:13:24 -04:00
|
|
|
item = unpack_item()
|
|
|
|
list.append(item)
|
|
|
|
return list
|
1996-08-19 19:26:43 -03:00
|
|
|
|
|
|
|
def unpack_farray(self, n, unpack_item):
|
1998-03-26 17:13:24 -04:00
|
|
|
list = []
|
|
|
|
for i in range(n):
|
|
|
|
list.append(unpack_item())
|
|
|
|
return list
|
1996-08-19 19:26:43 -03:00
|
|
|
|
|
|
|
def unpack_array(self, unpack_item):
|
1998-03-26 17:13:24 -04:00
|
|
|
n = self.unpack_uint()
|
|
|
|
return self.unpack_farray(n, unpack_item)
|