1 # Implement (a subset of) Sun XDR -- RFC1014.
24 def pack_uint(self
, x
):
25 self
.buf
= self
.buf
+ \
26 (chr(int(x
>>24 & 0xff)) + chr(int(x
>>16 & 0xff)) + \
27 chr(int(x
>>8 & 0xff)) + chr(int(x
& 0xff)))
28 if struct
and struct
.pack('l', 1) == '\0\0\0\1':
29 def pack_uint(self
, x
):
31 x
= int((x
+ 0x80000000L
) % 0x100000000L \
33 self
.buf
= self
.buf
+ struct
.pack('l', x
)
39 def pack_bool(self
, x
):
40 if x
: self
.buf
= self
.buf
+ '\0\0\0\1'
41 else: self
.buf
= self
.buf
+ '\0\0\0\0'
43 def pack_uhyper(self
, x
):
44 self
.pack_uint(int(x
>>32 & 0xffffffff))
45 self
.pack_uint(int(x
& 0xffffffff))
47 pack_hyper
= pack_uhyper
49 def pack_float(self
, x
):
51 self
.buf
= self
.buf
+ struct
.pack('f', x
)
53 def pack_double(self
, x
):
55 self
.buf
= self
.buf
+ struct
.pack('d', x
)
57 def pack_fstring(self
, n
, s
):
59 raise ValueError, 'fstring size must be nonnegative'
62 data
= data
+ (n
- len(data
)) * '\0'
63 self
.buf
= self
.buf
+ data
65 pack_fopaque
= pack_fstring
67 def pack_string(self
, s
):
70 self
.pack_fstring(n
, s
)
72 pack_opaque
= pack_string
74 def pack_list(self
, list, pack_item
):
80 def pack_farray(self
, n
, list, pack_item
):
82 raise ValueError, 'wrong array size'
86 def pack_array(self
, list, pack_item
):
89 self
.pack_farray(n
, list, pack_item
)
94 def __init__(self
, data
):
97 def reset(self
, data
):
102 if self
.pos
< len(self
.buf
):
103 raise RuntimeError, 'unextracted data remains'
105 def unpack_uint(self
):
111 x
= long(ord(data
[0]))<<24 |
ord(data
[1])<<16 | \
112 ord(data
[2])<<8 |
ord(data
[3])
113 # Return a Python long only if the value is not representable
114 # as a nonnegative Python int
115 if x
< 0x80000000L
: x
= int(x
)
117 if struct
and struct
.unpack('l', '\0\0\0\1') == 1:
118 def unpack_uint(self
):
124 return struct
.unpack('l', data
)
126 def unpack_int(self
):
127 x
= self
.unpack_uint()
128 if x
>= 0x80000000L
: x
= x
- 0x100000000L
131 unpack_enum
= unpack_int
133 unpack_bool
= unpack_int
135 def unpack_uhyper(self
):
136 hi
= self
.unpack_uint()
137 lo
= self
.unpack_uint()
138 return long(hi
)<<32 | lo
140 def unpack_hyper(self
):
141 x
= self
.unpack_uhyper()
142 if x
>= 0x8000000000000000L
: x
= x
- 0x10000000000000000L
145 def unpack_float(self
):
152 return struct
.unpack('f', data
)[0]
154 def unpack_double(self
):
161 return struct
.unpack('d', data
)[0]
163 def unpack_fstring(self
, n
):
165 raise ValueError, 'fstring size must be nonnegative'
168 if j
> len(self
.buf
):
171 return self
.buf
[i
:i
+n
]
173 unpack_fopaque
= unpack_fstring
175 def unpack_string(self
):
176 n
= self
.unpack_uint()
177 return self
.unpack_fstring(n
)
179 unpack_opaque
= unpack_string
181 def unpack_list(self
, unpack_item
):
184 x
= self
.unpack_uint()
187 raise RuntimeError, '0 or 1 expected, got %r' % (x
, )
192 def unpack_farray(self
, n
, unpack_item
):
195 list.append(unpack_item())
198 def unpack_array(self
, unpack_item
):
199 n
= self
.unpack_uint()
200 return self
.unpack_farray(n
, unpack_item
)