blob: b293e06a1084db3bf98ff58bea50347c6c8ec84b [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
Brett Cannon1eb79cf2007-07-26 03:19:46 +00008from io import BytesIO
Guido van Rossum731630b1996-08-19 22:26:43 +00009
Skip Montanaro40fc1602001-03-01 04:27:19 +000010__all__ = ["Error", "Packer", "Unpacker", "ConversionError"]
11
Guido van Rossum731630b1996-08-19 22:26:43 +000012# exceptions
Neal Norwitzf74e46c2002-03-31 13:59:18 +000013class Error(Exception):
Guido van Rossum731630b1996-08-19 22:26:43 +000014 """Exception class for this module. Use:
15
16 except xdrlib.Error, var:
17 # var has the Error instance for the exception
18
19 Public ivars:
20 msg -- contains the message
21
22 """
23 def __init__(self, msg):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000024 self.msg = msg
Guido van Rossum731630b1996-08-19 22:26:43 +000025 def __repr__(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000026 return repr(self.msg)
Guido van Rossum731630b1996-08-19 22:26:43 +000027 def __str__(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000028 return str(self.msg)
Guido van Rossum731630b1996-08-19 22:26:43 +000029
30
31class ConversionError(Error):
32 pass
33
34
Tim Peterse1190062001-01-15 03:34:38 +000035
Guido van Rossum731630b1996-08-19 22:26:43 +000036class Packer:
37 """Pack various data representations into a buffer."""
38
39 def __init__(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000040 self.reset()
Guido van Rossum731630b1996-08-19 22:26:43 +000041
42 def reset(self):
Brett Cannon1eb79cf2007-07-26 03:19:46 +000043 self.__buf = BytesIO()
Guido van Rossum731630b1996-08-19 22:26:43 +000044
45 def get_buffer(self):
Martin v. Löwisc47016e2001-08-16 17:06:44 +000046 return self.__buf.getvalue()
Guido van Rossum731630b1996-08-19 22:26:43 +000047 # backwards compatibility
48 get_buf = get_buffer
49
50 def pack_uint(self, x):
Martin v. Löwisc47016e2001-08-16 17:06:44 +000051 self.__buf.write(struct.pack('>L', x))
Guido van Rossum731630b1996-08-19 22:26:43 +000052
53 pack_int = pack_uint
54 pack_enum = pack_int
55
56 def pack_bool(self, x):
Brett Cannon1eb79cf2007-07-26 03:19:46 +000057 if x: self.__buf.write(b'\0\0\0\1')
58 else: self.__buf.write(b'\0\0\0\0')
Guido van Rossum731630b1996-08-19 22:26:43 +000059
60 def pack_uhyper(self, x):
Guido van Rossume2a383d2007-01-15 16:59:06 +000061 self.pack_uint(x>>32 & 0xffffffff)
62 self.pack_uint(x & 0xffffffff)
Guido van Rossum731630b1996-08-19 22:26:43 +000063
64 pack_hyper = pack_uhyper
65
66 def pack_float(self, x):
Martin v. Löwisc47016e2001-08-16 17:06:44 +000067 try: self.__buf.write(struct.pack('>f', x))
Guido van Rossumb940e112007-01-10 16:19:56 +000068 except struct.error as msg:
Collin Winterce36ad82007-08-30 01:19:48 +000069 raise ConversionError(msg)
Guido van Rossum6083f0e1997-01-02 22:52:15 +000070
Guido van Rossum731630b1996-08-19 22:26:43 +000071 def pack_double(self, x):
Martin v. Löwisc47016e2001-08-16 17:06:44 +000072 try: self.__buf.write(struct.pack('>d', x))
Guido van Rossumb940e112007-01-10 16:19:56 +000073 except struct.error as msg:
Collin Winterce36ad82007-08-30 01:19:48 +000074 raise ConversionError(msg)
Guido van Rossum731630b1996-08-19 22:26:43 +000075
76 def pack_fstring(self, n, s):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000077 if n < 0:
Collin Winterce36ad82007-08-30 01:19:48 +000078 raise ValueError('fstring size must be nonnegative')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000079 data = s[:n]
Thomas Wouters4f564bd2006-04-20 22:36:57 +000080 n = ((n+3)//4)*4
Brett Cannon1eb79cf2007-07-26 03:19:46 +000081 data = data + (n - len(data)) * b'\0'
Martin v. Löwisc47016e2001-08-16 17:06:44 +000082 self.__buf.write(data)
Guido van Rossum731630b1996-08-19 22:26:43 +000083
84 pack_fopaque = pack_fstring
85
86 def pack_string(self, s):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000087 n = len(s)
88 self.pack_uint(n)
89 self.pack_fstring(n, s)
Guido van Rossum731630b1996-08-19 22:26:43 +000090
91 pack_opaque = pack_string
92 pack_bytes = pack_string
93
94 def pack_list(self, list, pack_item):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000095 for item in list:
96 self.pack_uint(1)
97 pack_item(item)
98 self.pack_uint(0)
Guido van Rossum731630b1996-08-19 22:26:43 +000099
100 def pack_farray(self, n, list, pack_item):
Fred Drake8152d322000-12-12 23:20:45 +0000101 if len(list) != n:
Collin Winterce36ad82007-08-30 01:19:48 +0000102 raise ValueError('wrong array size')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000103 for item in list:
104 pack_item(item)
Guido van Rossum731630b1996-08-19 22:26:43 +0000105
106 def pack_array(self, list, pack_item):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000107 n = len(list)
108 self.pack_uint(n)
109 self.pack_farray(n, list, pack_item)
Guido van Rossum731630b1996-08-19 22:26:43 +0000110
111
Tim Peterse1190062001-01-15 03:34:38 +0000112
Guido van Rossum731630b1996-08-19 22:26:43 +0000113class Unpacker:
114 """Unpacks various data representations from the given buffer."""
115
116 def __init__(self, data):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000117 self.reset(data)
Guido van Rossum731630b1996-08-19 22:26:43 +0000118
119 def reset(self, data):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000120 self.__buf = data
121 self.__pos = 0
Guido van Rossum731630b1996-08-19 22:26:43 +0000122
123 def get_position(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000124 return self.__pos
Guido van Rossum731630b1996-08-19 22:26:43 +0000125
126 def set_position(self, position):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000127 self.__pos = position
Guido van Rossum731630b1996-08-19 22:26:43 +0000128
Barry Warsaw75eccc51996-12-04 22:04:39 +0000129 def get_buffer(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000130 return self.__buf
Barry Warsaw75eccc51996-12-04 22:04:39 +0000131
Guido van Rossum731630b1996-08-19 22:26:43 +0000132 def done(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000133 if self.__pos < len(self.__buf):
134 raise Error('unextracted data remains')
Guido van Rossum731630b1996-08-19 22:26:43 +0000135
136 def unpack_uint(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000137 i = self.__pos
138 self.__pos = j = i+4
139 data = self.__buf[i:j]
140 if len(data) < 4:
141 raise EOFError
142 x = struct.unpack('>L', data)[0]
143 try:
144 return int(x)
145 except OverflowError:
146 return x
Guido van Rossum731630b1996-08-19 22:26:43 +0000147
148 def unpack_int(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000149 i = self.__pos
150 self.__pos = j = i+4
151 data = self.__buf[i:j]
152 if len(data) < 4:
153 raise EOFError
154 return struct.unpack('>l', data)[0]
Guido van Rossum731630b1996-08-19 22:26:43 +0000155
156 unpack_enum = unpack_int
Martin v. Löwisc2a0ac22005-02-24 20:22:10 +0000157
158 def unpack_bool(self):
159 return bool(self.unpack_int())
Guido van Rossum731630b1996-08-19 22:26:43 +0000160
161 def unpack_uhyper(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000162 hi = self.unpack_uint()
163 lo = self.unpack_uint()
Guido van Rossume2a383d2007-01-15 16:59:06 +0000164 return int(hi)<<32 | lo
Guido van Rossum731630b1996-08-19 22:26:43 +0000165
166 def unpack_hyper(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000167 x = self.unpack_uhyper()
Guido van Rossume2a383d2007-01-15 16:59:06 +0000168 if x >= 0x8000000000000000:
169 x = x - 0x10000000000000000
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000170 return x
Guido van Rossum731630b1996-08-19 22:26:43 +0000171
172 def unpack_float(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000173 i = self.__pos
174 self.__pos = j = i+4
175 data = self.__buf[i:j]
176 if len(data) < 4:
177 raise EOFError
178 return struct.unpack('>f', data)[0]
Guido van Rossum731630b1996-08-19 22:26:43 +0000179
Guido van Rossum6083f0e1997-01-02 22:52:15 +0000180 def unpack_double(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000181 i = self.__pos
182 self.__pos = j = i+8
183 data = self.__buf[i:j]
184 if len(data) < 8:
185 raise EOFError
186 return struct.unpack('>d', data)[0]
Guido van Rossum731630b1996-08-19 22:26:43 +0000187
188 def unpack_fstring(self, n):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000189 if n < 0:
Collin Winterce36ad82007-08-30 01:19:48 +0000190 raise ValueError('fstring size must be nonnegative')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000191 i = self.__pos
Thomas Wouters4f564bd2006-04-20 22:36:57 +0000192 j = i + (n+3)//4*4
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000193 if j > len(self.__buf):
194 raise EOFError
195 self.__pos = j
196 return self.__buf[i:i+n]
Guido van Rossum731630b1996-08-19 22:26:43 +0000197
198 unpack_fopaque = unpack_fstring
199
200 def unpack_string(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000201 n = self.unpack_uint()
202 return self.unpack_fstring(n)
Guido van Rossum731630b1996-08-19 22:26:43 +0000203
204 unpack_opaque = unpack_string
205 unpack_bytes = unpack_string
206
207 def unpack_list(self, unpack_item):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000208 list = []
209 while 1:
210 x = self.unpack_uint()
211 if x == 0: break
Fred Drake8152d322000-12-12 23:20:45 +0000212 if x != 1:
Collin Winterce36ad82007-08-30 01:19:48 +0000213 raise ConversionError('0 or 1 expected, got %r' % (x,))
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000214 item = unpack_item()
215 list.append(item)
216 return list
Guido van Rossum731630b1996-08-19 22:26:43 +0000217
218 def unpack_farray(self, n, unpack_item):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000219 list = []
220 for i in range(n):
221 list.append(unpack_item())
222 return list
Guido van Rossum731630b1996-08-19 22:26:43 +0000223
224 def unpack_array(self, unpack_item):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000225 n = self.unpack_uint()
226 return self.unpack_farray(n, unpack_item)