blob: 4e4867704057247ea6a237eef1906f3a668b72d1 [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
Mark Dickinson92b60d52011-03-27 16:25:40 +010053 def pack_int(self, x):
54 self.__buf.write(struct.pack('>l', x))
55
Guido van Rossum731630b1996-08-19 22:26:43 +000056 pack_enum = pack_int
57
58 def pack_bool(self, x):
Brett Cannon1eb79cf2007-07-26 03:19:46 +000059 if x: self.__buf.write(b'\0\0\0\1')
60 else: self.__buf.write(b'\0\0\0\0')
Guido van Rossum731630b1996-08-19 22:26:43 +000061
62 def pack_uhyper(self, x):
Guido van Rossume2a383d2007-01-15 16:59:06 +000063 self.pack_uint(x>>32 & 0xffffffff)
64 self.pack_uint(x & 0xffffffff)
Guido van Rossum731630b1996-08-19 22:26:43 +000065
66 pack_hyper = pack_uhyper
67
68 def pack_float(self, x):
Martin v. Löwisc47016e2001-08-16 17:06:44 +000069 try: self.__buf.write(struct.pack('>f', x))
Guido van Rossumb940e112007-01-10 16:19:56 +000070 except struct.error as msg:
Collin Winterce36ad82007-08-30 01:19:48 +000071 raise ConversionError(msg)
Guido van Rossum6083f0e1997-01-02 22:52:15 +000072
Guido van Rossum731630b1996-08-19 22:26:43 +000073 def pack_double(self, x):
Martin v. Löwisc47016e2001-08-16 17:06:44 +000074 try: self.__buf.write(struct.pack('>d', x))
Guido van Rossumb940e112007-01-10 16:19:56 +000075 except struct.error as msg:
Collin Winterce36ad82007-08-30 01:19:48 +000076 raise ConversionError(msg)
Guido van Rossum731630b1996-08-19 22:26:43 +000077
78 def pack_fstring(self, n, s):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000079 if n < 0:
Collin Winterce36ad82007-08-30 01:19:48 +000080 raise ValueError('fstring size must be nonnegative')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000081 data = s[:n]
Thomas Wouters4f564bd2006-04-20 22:36:57 +000082 n = ((n+3)//4)*4
Brett Cannon1eb79cf2007-07-26 03:19:46 +000083 data = data + (n - len(data)) * b'\0'
Martin v. Löwisc47016e2001-08-16 17:06:44 +000084 self.__buf.write(data)
Guido van Rossum731630b1996-08-19 22:26:43 +000085
86 pack_fopaque = pack_fstring
87
88 def pack_string(self, s):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000089 n = len(s)
90 self.pack_uint(n)
91 self.pack_fstring(n, s)
Guido van Rossum731630b1996-08-19 22:26:43 +000092
93 pack_opaque = pack_string
94 pack_bytes = pack_string
95
96 def pack_list(self, list, pack_item):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000097 for item in list:
98 self.pack_uint(1)
99 pack_item(item)
100 self.pack_uint(0)
Guido van Rossum731630b1996-08-19 22:26:43 +0000101
102 def pack_farray(self, n, list, pack_item):
Fred Drake8152d322000-12-12 23:20:45 +0000103 if len(list) != n:
Collin Winterce36ad82007-08-30 01:19:48 +0000104 raise ValueError('wrong array size')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000105 for item in list:
106 pack_item(item)
Guido van Rossum731630b1996-08-19 22:26:43 +0000107
108 def pack_array(self, list, pack_item):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000109 n = len(list)
110 self.pack_uint(n)
111 self.pack_farray(n, list, pack_item)
Guido van Rossum731630b1996-08-19 22:26:43 +0000112
113
Tim Peterse1190062001-01-15 03:34:38 +0000114
Guido van Rossum731630b1996-08-19 22:26:43 +0000115class Unpacker:
116 """Unpacks various data representations from the given buffer."""
117
118 def __init__(self, data):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000119 self.reset(data)
Guido van Rossum731630b1996-08-19 22:26:43 +0000120
121 def reset(self, data):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000122 self.__buf = data
123 self.__pos = 0
Guido van Rossum731630b1996-08-19 22:26:43 +0000124
125 def get_position(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000126 return self.__pos
Guido van Rossum731630b1996-08-19 22:26:43 +0000127
128 def set_position(self, position):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000129 self.__pos = position
Guido van Rossum731630b1996-08-19 22:26:43 +0000130
Barry Warsaw75eccc51996-12-04 22:04:39 +0000131 def get_buffer(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000132 return self.__buf
Barry Warsaw75eccc51996-12-04 22:04:39 +0000133
Guido van Rossum731630b1996-08-19 22:26:43 +0000134 def done(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000135 if self.__pos < len(self.__buf):
136 raise Error('unextracted data remains')
Guido van Rossum731630b1996-08-19 22:26:43 +0000137
138 def unpack_uint(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000139 i = self.__pos
140 self.__pos = j = i+4
141 data = self.__buf[i:j]
142 if len(data) < 4:
143 raise EOFError
144 x = struct.unpack('>L', data)[0]
145 try:
146 return int(x)
147 except OverflowError:
148 return x
Guido van Rossum731630b1996-08-19 22:26:43 +0000149
150 def unpack_int(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000151 i = self.__pos
152 self.__pos = j = i+4
153 data = self.__buf[i:j]
154 if len(data) < 4:
155 raise EOFError
156 return struct.unpack('>l', data)[0]
Guido van Rossum731630b1996-08-19 22:26:43 +0000157
158 unpack_enum = unpack_int
Martin v. Löwisc2a0ac22005-02-24 20:22:10 +0000159
160 def unpack_bool(self):
161 return bool(self.unpack_int())
Guido van Rossum731630b1996-08-19 22:26:43 +0000162
163 def unpack_uhyper(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000164 hi = self.unpack_uint()
165 lo = self.unpack_uint()
Guido van Rossume2a383d2007-01-15 16:59:06 +0000166 return int(hi)<<32 | lo
Guido van Rossum731630b1996-08-19 22:26:43 +0000167
168 def unpack_hyper(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000169 x = self.unpack_uhyper()
Guido van Rossume2a383d2007-01-15 16:59:06 +0000170 if x >= 0x8000000000000000:
171 x = x - 0x10000000000000000
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000172 return x
Guido van Rossum731630b1996-08-19 22:26:43 +0000173
174 def unpack_float(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000175 i = self.__pos
176 self.__pos = j = i+4
177 data = self.__buf[i:j]
178 if len(data) < 4:
179 raise EOFError
180 return struct.unpack('>f', data)[0]
Guido van Rossum731630b1996-08-19 22:26:43 +0000181
Guido van Rossum6083f0e1997-01-02 22:52:15 +0000182 def unpack_double(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000183 i = self.__pos
184 self.__pos = j = i+8
185 data = self.__buf[i:j]
186 if len(data) < 8:
187 raise EOFError
188 return struct.unpack('>d', data)[0]
Guido van Rossum731630b1996-08-19 22:26:43 +0000189
190 def unpack_fstring(self, n):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000191 if n < 0:
Collin Winterce36ad82007-08-30 01:19:48 +0000192 raise ValueError('fstring size must be nonnegative')
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000193 i = self.__pos
Thomas Wouters4f564bd2006-04-20 22:36:57 +0000194 j = i + (n+3)//4*4
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000195 if j > len(self.__buf):
196 raise EOFError
197 self.__pos = j
198 return self.__buf[i:i+n]
Guido van Rossum731630b1996-08-19 22:26:43 +0000199
200 unpack_fopaque = unpack_fstring
201
202 def unpack_string(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000203 n = self.unpack_uint()
204 return self.unpack_fstring(n)
Guido van Rossum731630b1996-08-19 22:26:43 +0000205
206 unpack_opaque = unpack_string
207 unpack_bytes = unpack_string
208
209 def unpack_list(self, unpack_item):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000210 list = []
211 while 1:
212 x = self.unpack_uint()
213 if x == 0: break
Fred Drake8152d322000-12-12 23:20:45 +0000214 if x != 1:
Collin Winterce36ad82007-08-30 01:19:48 +0000215 raise ConversionError('0 or 1 expected, got %r' % (x,))
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000216 item = unpack_item()
217 list.append(item)
218 return list
Guido van Rossum731630b1996-08-19 22:26:43 +0000219
220 def unpack_farray(self, n, unpack_item):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000221 list = []
222 for i in range(n):
223 list.append(unpack_item())
224 return list
Guido van Rossum731630b1996-08-19 22:26:43 +0000225
226 def unpack_array(self, unpack_item):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000227 n = self.unpack_uint()
228 return self.unpack_farray(n, unpack_item)