blob: d9d2120a60c1156e47f99e8b4cdb21c603f7d801 [file] [log] [blame]
Guido van Rossum731630b1996-08-19 22:26:43 +00001"""Implements (a subset of) Sun XDR -- eXternal Data Representation.
2
3See: RFC 1014
4
Guido van Rossum731630b1996-08-19 22:26:43 +00005"""
6
7import struct
Martin v. Löwisc47016e2001-08-16 17:06:44 +00008try:
9 from cStringIO import StringIO as _StringIO
10except ImportError:
11 from StringIO import StringIO as _StringIO
Guido van Rossum731630b1996-08-19 22:26:43 +000012
Skip Montanaro40fc1602001-03-01 04:27:19 +000013__all__ = ["Error", "Packer", "Unpacker", "ConversionError"]
14
Guido van Rossum731630b1996-08-19 22:26:43 +000015# exceptions
Neal Norwitzf74e46c2002-03-31 13:59:18 +000016class Error(Exception):
Guido van Rossum731630b1996-08-19 22:26:43 +000017 """Exception class for this module. Use:
18
19 except xdrlib.Error, var:
20 # var has the Error instance for the exception
21
22 Public ivars:
23 msg -- contains the message
24
25 """
26 def __init__(self, msg):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000027 self.msg = msg
Guido van Rossum731630b1996-08-19 22:26:43 +000028 def __repr__(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000029 return repr(self.msg)
Guido van Rossum731630b1996-08-19 22:26:43 +000030 def __str__(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000031 return str(self.msg)
Guido van Rossum731630b1996-08-19 22:26:43 +000032
33
34class ConversionError(Error):
35 pass
36
37
Tim Peterse1190062001-01-15 03:34:38 +000038
Guido van Rossum731630b1996-08-19 22:26:43 +000039class Packer:
40 """Pack various data representations into a buffer."""
41
42 def __init__(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000043 self.reset()
Guido van Rossum731630b1996-08-19 22:26:43 +000044
45 def reset(self):
Martin v. Löwisc47016e2001-08-16 17:06:44 +000046 self.__buf = _StringIO()
Guido van Rossum731630b1996-08-19 22:26:43 +000047
48 def get_buffer(self):
Martin v. Löwisc47016e2001-08-16 17:06:44 +000049 return self.__buf.getvalue()
Guido van Rossum731630b1996-08-19 22:26:43 +000050 # backwards compatibility
51 get_buf = get_buffer
52
53 def pack_uint(self, x):
Martin v. Löwisc47016e2001-08-16 17:06:44 +000054 self.__buf.write(struct.pack('>L', x))
Guido van Rossum731630b1996-08-19 22:26:43 +000055
56 pack_int = pack_uint
57 pack_enum = pack_int
58
59 def pack_bool(self, x):
Martin v. Löwisc47016e2001-08-16 17:06:44 +000060 if x: self.__buf.write('\0\0\0\1')
61 else: self.__buf.write('\0\0\0\0')
Guido van Rossum731630b1996-08-19 22:26:43 +000062
63 def pack_uhyper(self, x):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000064 self.pack_uint(x>>32 & 0xffffffffL)
65 self.pack_uint(x & 0xffffffffL)
Guido van Rossum731630b1996-08-19 22:26:43 +000066
67 pack_hyper = pack_uhyper
68
69 def pack_float(self, x):
Martin v. Löwisc47016e2001-08-16 17:06:44 +000070 try: self.__buf.write(struct.pack('>f', x))
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000071 except struct.error, msg:
72 raise ConversionError, msg
Guido van Rossum6083f0e1997-01-02 22:52:15 +000073
Guido van Rossum731630b1996-08-19 22:26:43 +000074 def pack_double(self, x):
Martin v. Löwisc47016e2001-08-16 17:06:44 +000075 try: self.__buf.write(struct.pack('>d', x))
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000076 except struct.error, msg:
77 raise ConversionError, msg
Guido van Rossum731630b1996-08-19 22:26:43 +000078
79 def pack_fstring(self, n, s):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000080 if n < 0:
81 raise ValueError, 'fstring size must be nonnegative'
82 n = ((n+3)/4)*4
83 data = s[:n]
84 data = data + (n - len(data)) * '\0'
Martin v. Löwisc47016e2001-08-16 17:06:44 +000085 self.__buf.write(data)
Guido van Rossum731630b1996-08-19 22:26:43 +000086
87 pack_fopaque = pack_fstring
88
89 def pack_string(self, s):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000090 n = len(s)
91 self.pack_uint(n)
92 self.pack_fstring(n, s)
Guido van Rossum731630b1996-08-19 22:26:43 +000093
94 pack_opaque = pack_string
95 pack_bytes = pack_string
96
97 def pack_list(self, list, pack_item):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000098 for item in list:
99 self.pack_uint(1)
100 pack_item(item)
101 self.pack_uint(0)
Guido van Rossum731630b1996-08-19 22:26:43 +0000102
103 def pack_farray(self, n, list, pack_item):
Fred Drake8152d322000-12-12 23:20:45 +0000104 if len(list) != n:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000105 raise ValueError, 'wrong array size'
106 for item in list:
107 pack_item(item)
Guido van Rossum731630b1996-08-19 22:26:43 +0000108
109 def pack_array(self, list, pack_item):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000110 n = len(list)
111 self.pack_uint(n)
112 self.pack_farray(n, list, pack_item)
Guido van Rossum731630b1996-08-19 22:26:43 +0000113
114
Tim Peterse1190062001-01-15 03:34:38 +0000115
Guido van Rossum731630b1996-08-19 22:26:43 +0000116class Unpacker:
117 """Unpacks various data representations from the given buffer."""
118
119 def __init__(self, data):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000120 self.reset(data)
Guido van Rossum731630b1996-08-19 22:26:43 +0000121
122 def reset(self, data):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000123 self.__buf = data
124 self.__pos = 0
Guido van Rossum731630b1996-08-19 22:26:43 +0000125
126 def get_position(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000127 return self.__pos
Guido van Rossum731630b1996-08-19 22:26:43 +0000128
129 def set_position(self, position):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000130 self.__pos = position
Guido van Rossum731630b1996-08-19 22:26:43 +0000131
Barry Warsaw75eccc51996-12-04 22:04:39 +0000132 def get_buffer(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000133 return self.__buf
Barry Warsaw75eccc51996-12-04 22:04:39 +0000134
Guido van Rossum731630b1996-08-19 22:26:43 +0000135 def done(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000136 if self.__pos < len(self.__buf):
137 raise Error('unextracted data remains')
Guido van Rossum731630b1996-08-19 22:26:43 +0000138
139 def unpack_uint(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000140 i = self.__pos
141 self.__pos = j = i+4
142 data = self.__buf[i:j]
143 if len(data) < 4:
144 raise EOFError
145 x = struct.unpack('>L', data)[0]
146 try:
147 return int(x)
148 except OverflowError:
149 return x
Guido van Rossum731630b1996-08-19 22:26:43 +0000150
151 def unpack_int(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000152 i = self.__pos
153 self.__pos = j = i+4
154 data = self.__buf[i:j]
155 if len(data) < 4:
156 raise EOFError
157 return struct.unpack('>l', data)[0]
Guido van Rossum731630b1996-08-19 22:26:43 +0000158
159 unpack_enum = unpack_int
Martin v. Löwisc2a0ac22005-02-24 20:22:10 +0000160
161 def unpack_bool(self):
162 return bool(self.unpack_int())
Guido van Rossum731630b1996-08-19 22:26:43 +0000163
164 def unpack_uhyper(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000165 hi = self.unpack_uint()
166 lo = self.unpack_uint()
167 return long(hi)<<32 | lo
Guido van Rossum731630b1996-08-19 22:26:43 +0000168
169 def unpack_hyper(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000170 x = self.unpack_uhyper()
171 if x >= 0x8000000000000000L:
172 x = x - 0x10000000000000000L
173 return x
Guido van Rossum731630b1996-08-19 22:26:43 +0000174
175 def unpack_float(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000176 i = self.__pos
177 self.__pos = j = i+4
178 data = self.__buf[i:j]
179 if len(data) < 4:
180 raise EOFError
181 return struct.unpack('>f', data)[0]
Guido van Rossum731630b1996-08-19 22:26:43 +0000182
Guido van Rossum6083f0e1997-01-02 22:52:15 +0000183 def unpack_double(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000184 i = self.__pos
185 self.__pos = j = i+8
186 data = self.__buf[i:j]
187 if len(data) < 8:
188 raise EOFError
189 return struct.unpack('>d', data)[0]
Guido van Rossum731630b1996-08-19 22:26:43 +0000190
191 def unpack_fstring(self, n):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000192 if n < 0:
193 raise ValueError, 'fstring size must be nonnegative'
194 i = self.__pos
195 j = i + (n+3)/4*4
196 if j > len(self.__buf):
197 raise EOFError
198 self.__pos = j
199 return self.__buf[i:i+n]
Guido van Rossum731630b1996-08-19 22:26:43 +0000200
201 unpack_fopaque = unpack_fstring
202
203 def unpack_string(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000204 n = self.unpack_uint()
205 return self.unpack_fstring(n)
Guido van Rossum731630b1996-08-19 22:26:43 +0000206
207 unpack_opaque = unpack_string
208 unpack_bytes = unpack_string
209
210 def unpack_list(self, unpack_item):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000211 list = []
212 while 1:
213 x = self.unpack_uint()
214 if x == 0: break
Fred Drake8152d322000-12-12 23:20:45 +0000215 if x != 1:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000216 raise ConversionError, '0 or 1 expected, got %r' % (x,)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000217 item = unpack_item()
218 list.append(item)
219 return list
Guido van Rossum731630b1996-08-19 22:26:43 +0000220
221 def unpack_farray(self, n, unpack_item):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000222 list = []
223 for i in range(n):
224 list.append(unpack_item())
225 return list
Guido van Rossum731630b1996-08-19 22:26:43 +0000226
227 def unpack_array(self, unpack_item):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000228 n = self.unpack_uint()
229 return self.unpack_farray(n, unpack_item)
Guido van Rossum731630b1996-08-19 22:26:43 +0000230
Tim Peterse1190062001-01-15 03:34:38 +0000231
Guido van Rossum731630b1996-08-19 22:26:43 +0000232# test suite
Guido van Rossum72fba791996-08-19 22:49:35 +0000233def _test():
Guido van Rossum731630b1996-08-19 22:26:43 +0000234 p = Packer()
235 packtest = [
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000236 (p.pack_uint, (9,)),
Martin v. Löwisc2a0ac22005-02-24 20:22:10 +0000237 (p.pack_bool, (True,)),
238 (p.pack_bool, (False,)),
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000239 (p.pack_uhyper, (45L,)),
240 (p.pack_float, (1.9,)),
241 (p.pack_double, (1.9,)),
242 (p.pack_string, ('hello world',)),
243 (p.pack_list, (range(5), p.pack_uint)),
244 (p.pack_array, (['what', 'is', 'hapnin', 'doctor'], p.pack_string)),
245 ]
Guido van Rossum731630b1996-08-19 22:26:43 +0000246 succeedlist = [1] * len(packtest)
247 count = 0
248 for method, args in packtest:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000249 print 'pack test', count,
250 try:
Guido van Rossum68468eb2003-02-27 20:14:51 +0000251 method(*args)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000252 print 'succeeded'
253 except ConversionError, var:
254 print 'ConversionError:', var.msg
255 succeedlist[count] = 0
256 count = count + 1
Guido van Rossum731630b1996-08-19 22:26:43 +0000257 data = p.get_buffer()
258 # now verify
259 up = Unpacker(data)
260 unpacktest = [
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000261 (up.unpack_uint, (), lambda x: x == 9),
Martin v. Löwisc2a0ac22005-02-24 20:22:10 +0000262 (up.unpack_bool, (), lambda x: x is True),
263 (up.unpack_bool, (), lambda x: x is False),
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000264 (up.unpack_uhyper, (), lambda x: x == 45L),
265 (up.unpack_float, (), lambda x: 1.89 < x < 1.91),
266 (up.unpack_double, (), lambda x: 1.89 < x < 1.91),
267 (up.unpack_string, (), lambda x: x == 'hello world'),
268 (up.unpack_list, (up.unpack_uint,), lambda x: x == range(5)),
269 (up.unpack_array, (up.unpack_string,),
270 lambda x: x == ['what', 'is', 'hapnin', 'doctor']),
271 ]
Guido van Rossum731630b1996-08-19 22:26:43 +0000272 count = 0
273 for method, args, pred in unpacktest:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000274 print 'unpack test', count,
275 try:
276 if succeedlist[count]:
Guido van Rossum68468eb2003-02-27 20:14:51 +0000277 x = method(*args)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000278 print pred(x) and 'succeeded' or 'failed', ':', x
279 else:
280 print 'skipping'
281 except ConversionError, var:
282 print 'ConversionError:', var.msg
283 count = count + 1
Guido van Rossum731630b1996-08-19 22:26:43 +0000284
Tim Peterse1190062001-01-15 03:34:38 +0000285
Guido van Rossum731630b1996-08-19 22:26:43 +0000286if __name__ == '__main__':
Guido van Rossum72fba791996-08-19 22:49:35 +0000287 _test()