Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python3....
File: xdrlib.py
"""Implements (a subset of) Sun XDR -- eXternal Data Representation.
[0] Fix | Delete
[1] Fix | Delete
See: RFC 1014
[2] Fix | Delete
[3] Fix | Delete
"""
[4] Fix | Delete
[5] Fix | Delete
import struct
[6] Fix | Delete
from io import BytesIO
[7] Fix | Delete
from functools import wraps
[8] Fix | Delete
[9] Fix | Delete
__all__ = ["Error", "Packer", "Unpacker", "ConversionError"]
[10] Fix | Delete
[11] Fix | Delete
# exceptions
[12] Fix | Delete
class Error(Exception):
[13] Fix | Delete
"""Exception class for this module. Use:
[14] Fix | Delete
[15] Fix | Delete
except xdrlib.Error as var:
[16] Fix | Delete
# var has the Error instance for the exception
[17] Fix | Delete
[18] Fix | Delete
Public ivars:
[19] Fix | Delete
msg -- contains the message
[20] Fix | Delete
[21] Fix | Delete
"""
[22] Fix | Delete
def __init__(self, msg):
[23] Fix | Delete
self.msg = msg
[24] Fix | Delete
def __repr__(self):
[25] Fix | Delete
return repr(self.msg)
[26] Fix | Delete
def __str__(self):
[27] Fix | Delete
return str(self.msg)
[28] Fix | Delete
[29] Fix | Delete
[30] Fix | Delete
class ConversionError(Error):
[31] Fix | Delete
pass
[32] Fix | Delete
[33] Fix | Delete
def raise_conversion_error(function):
[34] Fix | Delete
""" Wrap any raised struct.errors in a ConversionError. """
[35] Fix | Delete
[36] Fix | Delete
@wraps(function)
[37] Fix | Delete
def result(self, value):
[38] Fix | Delete
try:
[39] Fix | Delete
return function(self, value)
[40] Fix | Delete
except struct.error as e:
[41] Fix | Delete
raise ConversionError(e.args[0]) from None
[42] Fix | Delete
return result
[43] Fix | Delete
[44] Fix | Delete
[45] Fix | Delete
class Packer:
[46] Fix | Delete
"""Pack various data representations into a buffer."""
[47] Fix | Delete
[48] Fix | Delete
def __init__(self):
[49] Fix | Delete
self.reset()
[50] Fix | Delete
[51] Fix | Delete
def reset(self):
[52] Fix | Delete
self.__buf = BytesIO()
[53] Fix | Delete
[54] Fix | Delete
def get_buffer(self):
[55] Fix | Delete
return self.__buf.getvalue()
[56] Fix | Delete
# backwards compatibility
[57] Fix | Delete
get_buf = get_buffer
[58] Fix | Delete
[59] Fix | Delete
@raise_conversion_error
[60] Fix | Delete
def pack_uint(self, x):
[61] Fix | Delete
self.__buf.write(struct.pack('>L', x))
[62] Fix | Delete
[63] Fix | Delete
@raise_conversion_error
[64] Fix | Delete
def pack_int(self, x):
[65] Fix | Delete
self.__buf.write(struct.pack('>l', x))
[66] Fix | Delete
[67] Fix | Delete
pack_enum = pack_int
[68] Fix | Delete
[69] Fix | Delete
def pack_bool(self, x):
[70] Fix | Delete
if x: self.__buf.write(b'\0\0\0\1')
[71] Fix | Delete
else: self.__buf.write(b'\0\0\0\0')
[72] Fix | Delete
[73] Fix | Delete
def pack_uhyper(self, x):
[74] Fix | Delete
try:
[75] Fix | Delete
self.pack_uint(x>>32 & 0xffffffff)
[76] Fix | Delete
except (TypeError, struct.error) as e:
[77] Fix | Delete
raise ConversionError(e.args[0]) from None
[78] Fix | Delete
try:
[79] Fix | Delete
self.pack_uint(x & 0xffffffff)
[80] Fix | Delete
except (TypeError, struct.error) as e:
[81] Fix | Delete
raise ConversionError(e.args[0]) from None
[82] Fix | Delete
[83] Fix | Delete
pack_hyper = pack_uhyper
[84] Fix | Delete
[85] Fix | Delete
@raise_conversion_error
[86] Fix | Delete
def pack_float(self, x):
[87] Fix | Delete
self.__buf.write(struct.pack('>f', x))
[88] Fix | Delete
[89] Fix | Delete
@raise_conversion_error
[90] Fix | Delete
def pack_double(self, x):
[91] Fix | Delete
self.__buf.write(struct.pack('>d', x))
[92] Fix | Delete
[93] Fix | Delete
def pack_fstring(self, n, s):
[94] Fix | Delete
if n < 0:
[95] Fix | Delete
raise ValueError('fstring size must be nonnegative')
[96] Fix | Delete
data = s[:n]
[97] Fix | Delete
n = ((n+3)//4)*4
[98] Fix | Delete
data = data + (n - len(data)) * b'\0'
[99] Fix | Delete
self.__buf.write(data)
[100] Fix | Delete
[101] Fix | Delete
pack_fopaque = pack_fstring
[102] Fix | Delete
[103] Fix | Delete
def pack_string(self, s):
[104] Fix | Delete
n = len(s)
[105] Fix | Delete
self.pack_uint(n)
[106] Fix | Delete
self.pack_fstring(n, s)
[107] Fix | Delete
[108] Fix | Delete
pack_opaque = pack_string
[109] Fix | Delete
pack_bytes = pack_string
[110] Fix | Delete
[111] Fix | Delete
def pack_list(self, list, pack_item):
[112] Fix | Delete
for item in list:
[113] Fix | Delete
self.pack_uint(1)
[114] Fix | Delete
pack_item(item)
[115] Fix | Delete
self.pack_uint(0)
[116] Fix | Delete
[117] Fix | Delete
def pack_farray(self, n, list, pack_item):
[118] Fix | Delete
if len(list) != n:
[119] Fix | Delete
raise ValueError('wrong array size')
[120] Fix | Delete
for item in list:
[121] Fix | Delete
pack_item(item)
[122] Fix | Delete
[123] Fix | Delete
def pack_array(self, list, pack_item):
[124] Fix | Delete
n = len(list)
[125] Fix | Delete
self.pack_uint(n)
[126] Fix | Delete
self.pack_farray(n, list, pack_item)
[127] Fix | Delete
[128] Fix | Delete
[129] Fix | Delete
[130] Fix | Delete
class Unpacker:
[131] Fix | Delete
"""Unpacks various data representations from the given buffer."""
[132] Fix | Delete
[133] Fix | Delete
def __init__(self, data):
[134] Fix | Delete
self.reset(data)
[135] Fix | Delete
[136] Fix | Delete
def reset(self, data):
[137] Fix | Delete
self.__buf = data
[138] Fix | Delete
self.__pos = 0
[139] Fix | Delete
[140] Fix | Delete
def get_position(self):
[141] Fix | Delete
return self.__pos
[142] Fix | Delete
[143] Fix | Delete
def set_position(self, position):
[144] Fix | Delete
self.__pos = position
[145] Fix | Delete
[146] Fix | Delete
def get_buffer(self):
[147] Fix | Delete
return self.__buf
[148] Fix | Delete
[149] Fix | Delete
def done(self):
[150] Fix | Delete
if self.__pos < len(self.__buf):
[151] Fix | Delete
raise Error('unextracted data remains')
[152] Fix | Delete
[153] Fix | Delete
def unpack_uint(self):
[154] Fix | Delete
i = self.__pos
[155] Fix | Delete
self.__pos = j = i+4
[156] Fix | Delete
data = self.__buf[i:j]
[157] Fix | Delete
if len(data) < 4:
[158] Fix | Delete
raise EOFError
[159] Fix | Delete
return struct.unpack('>L', data)[0]
[160] Fix | Delete
[161] Fix | Delete
def unpack_int(self):
[162] Fix | Delete
i = self.__pos
[163] Fix | Delete
self.__pos = j = i+4
[164] Fix | Delete
data = self.__buf[i:j]
[165] Fix | Delete
if len(data) < 4:
[166] Fix | Delete
raise EOFError
[167] Fix | Delete
return struct.unpack('>l', data)[0]
[168] Fix | Delete
[169] Fix | Delete
unpack_enum = unpack_int
[170] Fix | Delete
[171] Fix | Delete
def unpack_bool(self):
[172] Fix | Delete
return bool(self.unpack_int())
[173] Fix | Delete
[174] Fix | Delete
def unpack_uhyper(self):
[175] Fix | Delete
hi = self.unpack_uint()
[176] Fix | Delete
lo = self.unpack_uint()
[177] Fix | Delete
return int(hi)<<32 | lo
[178] Fix | Delete
[179] Fix | Delete
def unpack_hyper(self):
[180] Fix | Delete
x = self.unpack_uhyper()
[181] Fix | Delete
if x >= 0x8000000000000000:
[182] Fix | Delete
x = x - 0x10000000000000000
[183] Fix | Delete
return x
[184] Fix | Delete
[185] Fix | Delete
def unpack_float(self):
[186] Fix | Delete
i = self.__pos
[187] Fix | Delete
self.__pos = j = i+4
[188] Fix | Delete
data = self.__buf[i:j]
[189] Fix | Delete
if len(data) < 4:
[190] Fix | Delete
raise EOFError
[191] Fix | Delete
return struct.unpack('>f', data)[0]
[192] Fix | Delete
[193] Fix | Delete
def unpack_double(self):
[194] Fix | Delete
i = self.__pos
[195] Fix | Delete
self.__pos = j = i+8
[196] Fix | Delete
data = self.__buf[i:j]
[197] Fix | Delete
if len(data) < 8:
[198] Fix | Delete
raise EOFError
[199] Fix | Delete
return struct.unpack('>d', data)[0]
[200] Fix | Delete
[201] Fix | Delete
def unpack_fstring(self, n):
[202] Fix | Delete
if n < 0:
[203] Fix | Delete
raise ValueError('fstring size must be nonnegative')
[204] Fix | Delete
i = self.__pos
[205] Fix | Delete
j = i + (n+3)//4*4
[206] Fix | Delete
if j > len(self.__buf):
[207] Fix | Delete
raise EOFError
[208] Fix | Delete
self.__pos = j
[209] Fix | Delete
return self.__buf[i:i+n]
[210] Fix | Delete
[211] Fix | Delete
unpack_fopaque = unpack_fstring
[212] Fix | Delete
[213] Fix | Delete
def unpack_string(self):
[214] Fix | Delete
n = self.unpack_uint()
[215] Fix | Delete
return self.unpack_fstring(n)
[216] Fix | Delete
[217] Fix | Delete
unpack_opaque = unpack_string
[218] Fix | Delete
unpack_bytes = unpack_string
[219] Fix | Delete
[220] Fix | Delete
def unpack_list(self, unpack_item):
[221] Fix | Delete
list = []
[222] Fix | Delete
while 1:
[223] Fix | Delete
x = self.unpack_uint()
[224] Fix | Delete
if x == 0: break
[225] Fix | Delete
if x != 1:
[226] Fix | Delete
raise ConversionError('0 or 1 expected, got %r' % (x,))
[227] Fix | Delete
item = unpack_item()
[228] Fix | Delete
list.append(item)
[229] Fix | Delete
return list
[230] Fix | Delete
[231] Fix | Delete
def unpack_farray(self, n, unpack_item):
[232] Fix | Delete
list = []
[233] Fix | Delete
for i in range(n):
[234] Fix | Delete
list.append(unpack_item())
[235] Fix | Delete
return list
[236] Fix | Delete
[237] Fix | Delete
def unpack_array(self, unpack_item):
[238] Fix | Delete
n = self.unpack_uint()
[239] Fix | Delete
return self.unpack_farray(n, unpack_item)
[240] Fix | Delete
[241] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function