# Implement (a subset of) Sun XDR -- RFC1014.
(chr(int(x>>24 & 0xff)) + chr(int(x>>16 & 0xff)) + \
chr(int(x>>8 & 0xff)) + chr(int(x & 0xff)))
if struct and struct.pack('l', 1) == '\0\0\0\1':
x = int((x + 0x80000000L) % 0x100000000L \
self.buf = self.buf + struct.pack('l', x)
if x: self.buf = self.buf + '\0\0\0\1'
else: self.buf = self.buf + '\0\0\0\0'
def pack_uhyper(self, x):
self.pack_uint(int(x>>32 & 0xffffffff))
self.pack_uint(int(x & 0xffffffff))
self.buf = self.buf + struct.pack('f', x)
def pack_double(self, x):
self.buf = self.buf + struct.pack('d', x)
def pack_fstring(self, n, s):
raise ValueError, 'fstring size must be nonnegative'
data = data + (n - len(data)) * '\0'
self.buf = self.buf + data
pack_fopaque = pack_fstring
def pack_string(self, s):
pack_opaque = pack_string
def pack_list(self, list, pack_item):
def pack_farray(self, n, list, pack_item):
raise ValueError, 'wrong array size'
def pack_array(self, list, pack_item):
self.pack_farray(n, list, pack_item)
def __init__(self, data):
if self.pos < len(self.buf):
raise RuntimeError, 'unextracted data remains'
x = long(ord(data[0]))<<24 | ord(data[1])<<16 | \
ord(data[2])<<8 | ord(data[3])
# Return a Python long only if the value is not representable
# as a nonnegative Python int
if x < 0x80000000L: x = int(x)
if struct and struct.unpack('l', '\0\0\0\1') == 1:
return struct.unpack('l', data)
if x >= 0x80000000L: x = x - 0x100000000L
if x >= 0x8000000000000000L: x = x - 0x10000000000000000L
return struct.unpack('f', data)[0]
return struct.unpack('d', data)[0]
def unpack_fstring(self, n):
raise ValueError, 'fstring size must be nonnegative'
unpack_fopaque = unpack_fstring
return self.unpack_fstring(n)
unpack_opaque = unpack_string
def unpack_list(self, unpack_item):
raise RuntimeError, '0 or 1 expected, got %r' % (x, )
def unpack_farray(self, n, unpack_item):
list.append(unpack_item())
def unpack_array(self, unpack_item):
return self.unpack_farray(n, unpack_item)