"""Implements (a subset of) Sun XDR -- eXternal Data Representation.See: RFC 1014"""import structfrom io import BytesIOfrom functools import wraps__all__ = ["Error", "Packer", "Unpacker", "ConversionError"]# exceptionsclass Error(Exception):"""Exception class for this module. Use:except xdrlib.Error as var:# var has the Error instance for the exceptionPublic ivars:msg -- contains the message"""def __init__(self, msg):self.msg = msgdef __repr__(self):return repr(self.msg)def __str__(self):return str(self.msg)class ConversionError(Error):passdef 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 Nonereturn resultclass Packer:"""Pack various data representations into a buffer."""def __init__(self):self.reset()def reset(self):self.__buf = BytesIO()def get_buffer(self):return self.__buf.getvalue()# backwards compatibilityget_buf = get_buffer@raise_conversion_errordef pack_uint(self, x):self.__buf.write(struct.pack('>L', x))@raise_conversion_errordef pack_int(self, x):self.__buf.write(struct.pack('>l', x))pack_enum = pack_intdef pack_bool(self, x):if x: self.__buf.write(b'0円0円0円1円')else: self.__buf.write(b'0円0円0円0円')def pack_uhyper(self, x):try:self.pack_uint(x>>32 & 0xffffffff)except (TypeError, struct.error) as e:raise ConversionError(e.args[0]) from Nonetry:self.pack_uint(x & 0xffffffff)except (TypeError, struct.error) as e:raise ConversionError(e.args[0]) from Nonepack_hyper = pack_uhyper@raise_conversion_errordef pack_float(self, x):self.__buf.write(struct.pack('>f', x))@raise_conversion_errordef pack_double(self, x):self.__buf.write(struct.pack('>d', x))def pack_fstring(self, n, s):if n < 0:raise ValueError('fstring size must be nonnegative')data = s[:n]n = ((n+3)//4)*4data = data + (n - len(data)) * b'0円'self.__buf.write(data)pack_fopaque = pack_fstringdef pack_string(self, s):n = len(s)self.pack_uint(n)self.pack_fstring(n, s)pack_opaque = pack_stringpack_bytes = pack_stringdef pack_list(self, list, pack_item):for item in list:self.pack_uint(1)pack_item(item)self.pack_uint(0)def pack_farray(self, n, list, pack_item):if len(list) != n:raise ValueError('wrong array size')for item in list:pack_item(item)def pack_array(self, list, pack_item):n = len(list)self.pack_uint(n)self.pack_farray(n, list, pack_item)class Unpacker:"""Unpacks various data representations from the given buffer."""def __init__(self, data):self.reset(data)def reset(self, data):self.__buf = dataself.__pos = 0def get_position(self):return self.__posdef set_position(self, position):self.__pos = positiondef get_buffer(self):return self.__bufdef done(self):if self.__pos < len(self.__buf):raise Error('unextracted data remains')def unpack_uint(self):i = self.__posself.__pos = j = i+4data = self.__buf[i:j]if len(data) < 4:raise EOFErrorreturn struct.unpack('>L', data)[0]def unpack_int(self):i = self.__posself.__pos = j = i+4data = self.__buf[i:j]if len(data) < 4:raise EOFErrorreturn struct.unpack('>l', data)[0]unpack_enum = unpack_intdef unpack_bool(self):return bool(self.unpack_int())def unpack_uhyper(self):hi = self.unpack_uint()lo = self.unpack_uint()return int(hi)<<32 | lodef unpack_hyper(self):x = self.unpack_uhyper()if x >= 0x8000000000000000:x = x - 0x10000000000000000return xdef unpack_float(self):i = self.__posself.__pos = j = i+4data = self.__buf[i:j]if len(data) < 4:raise EOFErrorreturn struct.unpack('>f', data)[0]def unpack_double(self):i = self.__posself.__pos = j = i+8data = self.__buf[i:j]if len(data) < 8:raise EOFErrorreturn struct.unpack('>d', data)[0]def unpack_fstring(self, n):if n < 0:raise ValueError('fstring size must be nonnegative')i = self.__posj = i + (n+3)//4*4if j > len(self.__buf):raise EOFErrorself.__pos = jreturn self.__buf[i:i+n]unpack_fopaque = unpack_fstringdef unpack_string(self):n = self.unpack_uint()return self.unpack_fstring(n)unpack_opaque = unpack_stringunpack_bytes = unpack_stringdef unpack_list(self, unpack_item):list = []while 1:x = self.unpack_uint()if x == 0: breakif x != 1:raise ConversionError('0 or 1 expected, got %r' % (x,))item = unpack_item()list.append(item)return listdef unpack_farray(self, n, unpack_item):list = []for i in range(n):list.append(unpack_item())return listdef unpack_array(self, unpack_item):n = self.unpack_uint()return self.unpack_farray(n, unpack_item)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。