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