Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 1 | """Implements (a subset of) Sun XDR -- eXternal Data Representation. |
| 2 | |
| 3 | See: RFC 1014 |
| 4 | |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 5 | """ |
| 6 | |
| 7 | import struct |
Brett Cannon | 1eb79cf | 2007-07-26 03:19:46 +0000 | [diff] [blame] | 8 | from io import BytesIO |
Petri Lehtinen | 3894b2a | 2014-10-10 21:21:52 +0300 | [diff] [blame] | 9 | from functools import wraps |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 10 | |
Skip Montanaro | 40fc160 | 2001-03-01 04:27:19 +0000 | [diff] [blame] | 11 | __all__ = ["Error", "Packer", "Unpacker", "ConversionError"] |
| 12 | |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 13 | # exceptions |
Neal Norwitz | f74e46c | 2002-03-31 13:59:18 +0000 | [diff] [blame] | 14 | class Error(Exception): |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 15 | """Exception class for this module. Use: |
| 16 | |
Andrew Svetlov | 42d5c41 | 2012-11-01 13:43:06 +0200 | [diff] [blame] | 17 | except xdrlib.Error as var: |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 18 | # var has the Error instance for the exception |
| 19 | |
| 20 | Public ivars: |
| 21 | msg -- contains the message |
| 22 | |
| 23 | """ |
| 24 | def __init__(self, msg): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 25 | self.msg = msg |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 26 | def __repr__(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 27 | return repr(self.msg) |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 28 | def __str__(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 29 | return str(self.msg) |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 30 | |
| 31 | |
| 32 | class ConversionError(Error): |
| 33 | pass |
| 34 | |
Petri Lehtinen | 3894b2a | 2014-10-10 21:21:52 +0300 | [diff] [blame] | 35 | def raise_conversion_error(function): |
| 36 | """ Wrap any raised struct.errors in a ConversionError. """ |
| 37 | |
| 38 | @wraps(function) |
| 39 | def result(self, value): |
| 40 | try: |
| 41 | return function(self, value) |
| 42 | except struct.error as e: |
| 43 | raise ConversionError(e.args[0]) from None |
| 44 | return result |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 45 | |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 47 | class Packer: |
| 48 | """Pack various data representations into a buffer.""" |
| 49 | |
| 50 | def __init__(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 51 | self.reset() |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 52 | |
| 53 | def reset(self): |
Brett Cannon | 1eb79cf | 2007-07-26 03:19:46 +0000 | [diff] [blame] | 54 | self.__buf = BytesIO() |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 55 | |
| 56 | def get_buffer(self): |
Martin v. Löwis | c47016e | 2001-08-16 17:06:44 +0000 | [diff] [blame] | 57 | return self.__buf.getvalue() |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 58 | # backwards compatibility |
| 59 | get_buf = get_buffer |
| 60 | |
Petri Lehtinen | 3894b2a | 2014-10-10 21:21:52 +0300 | [diff] [blame] | 61 | @raise_conversion_error |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 62 | def pack_uint(self, x): |
Martin v. Löwis | c47016e | 2001-08-16 17:06:44 +0000 | [diff] [blame] | 63 | self.__buf.write(struct.pack('>L', x)) |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 64 | |
Petri Lehtinen | 3894b2a | 2014-10-10 21:21:52 +0300 | [diff] [blame] | 65 | @raise_conversion_error |
Mark Dickinson | 92b60d5 | 2011-03-27 16:25:40 +0100 | [diff] [blame] | 66 | def pack_int(self, x): |
| 67 | self.__buf.write(struct.pack('>l', x)) |
| 68 | |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 69 | pack_enum = pack_int |
| 70 | |
| 71 | def pack_bool(self, x): |
Brett Cannon | 1eb79cf | 2007-07-26 03:19:46 +0000 | [diff] [blame] | 72 | if x: self.__buf.write(b'\0\0\0\1') |
| 73 | else: self.__buf.write(b'\0\0\0\0') |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 74 | |
| 75 | def pack_uhyper(self, x): |
Petri Lehtinen | 3894b2a | 2014-10-10 21:21:52 +0300 | [diff] [blame] | 76 | try: |
| 77 | self.pack_uint(x>>32 & 0xffffffff) |
| 78 | except (TypeError, struct.error) as e: |
| 79 | raise ConversionError(e.args[0]) from None |
| 80 | try: |
| 81 | self.pack_uint(x & 0xffffffff) |
| 82 | except (TypeError, struct.error) as e: |
| 83 | raise ConversionError(e.args[0]) from None |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 84 | |
| 85 | pack_hyper = pack_uhyper |
| 86 | |
Petri Lehtinen | 3894b2a | 2014-10-10 21:21:52 +0300 | [diff] [blame] | 87 | @raise_conversion_error |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 88 | def pack_float(self, x): |
Petri Lehtinen | 3894b2a | 2014-10-10 21:21:52 +0300 | [diff] [blame] | 89 | self.__buf.write(struct.pack('>f', x)) |
Guido van Rossum | 6083f0e | 1997-01-02 22:52:15 +0000 | [diff] [blame] | 90 | |
Petri Lehtinen | 3894b2a | 2014-10-10 21:21:52 +0300 | [diff] [blame] | 91 | @raise_conversion_error |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 92 | def pack_double(self, x): |
Petri Lehtinen | 3894b2a | 2014-10-10 21:21:52 +0300 | [diff] [blame] | 93 | self.__buf.write(struct.pack('>d', x)) |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 94 | |
| 95 | def pack_fstring(self, n, s): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 96 | if n < 0: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 97 | raise ValueError('fstring size must be nonnegative') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 98 | data = s[:n] |
Thomas Wouters | 4f564bd | 2006-04-20 22:36:57 +0000 | [diff] [blame] | 99 | n = ((n+3)//4)*4 |
Brett Cannon | 1eb79cf | 2007-07-26 03:19:46 +0000 | [diff] [blame] | 100 | data = data + (n - len(data)) * b'\0' |
Martin v. Löwis | c47016e | 2001-08-16 17:06:44 +0000 | [diff] [blame] | 101 | self.__buf.write(data) |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 102 | |
| 103 | pack_fopaque = pack_fstring |
| 104 | |
| 105 | def pack_string(self, s): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 106 | n = len(s) |
| 107 | self.pack_uint(n) |
| 108 | self.pack_fstring(n, s) |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 109 | |
| 110 | pack_opaque = pack_string |
| 111 | pack_bytes = pack_string |
| 112 | |
| 113 | def pack_list(self, list, pack_item): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 114 | for item in list: |
| 115 | self.pack_uint(1) |
| 116 | pack_item(item) |
| 117 | self.pack_uint(0) |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 118 | |
| 119 | def pack_farray(self, n, list, pack_item): |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 120 | if len(list) != n: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 121 | raise ValueError('wrong array size') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 122 | for item in list: |
| 123 | pack_item(item) |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 124 | |
| 125 | def pack_array(self, list, pack_item): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 126 | n = len(list) |
| 127 | self.pack_uint(n) |
| 128 | self.pack_farray(n, list, pack_item) |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 129 | |
| 130 | |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 131 | |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 132 | class Unpacker: |
| 133 | """Unpacks various data representations from the given buffer.""" |
| 134 | |
| 135 | def __init__(self, data): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 136 | self.reset(data) |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 137 | |
| 138 | def reset(self, data): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 139 | self.__buf = data |
| 140 | self.__pos = 0 |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 141 | |
| 142 | def get_position(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 143 | return self.__pos |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 144 | |
| 145 | def set_position(self, position): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 146 | self.__pos = position |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 147 | |
Barry Warsaw | 75eccc5 | 1996-12-04 22:04:39 +0000 | [diff] [blame] | 148 | def get_buffer(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 149 | return self.__buf |
Barry Warsaw | 75eccc5 | 1996-12-04 22:04:39 +0000 | [diff] [blame] | 150 | |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 151 | def done(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 152 | if self.__pos < len(self.__buf): |
| 153 | raise Error('unextracted data remains') |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 154 | |
| 155 | def unpack_uint(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 156 | i = self.__pos |
| 157 | self.__pos = j = i+4 |
| 158 | data = self.__buf[i:j] |
| 159 | if len(data) < 4: |
| 160 | raise EOFError |
Florent Xicluna | 2bb96f5 | 2011-10-23 22:11:00 +0200 | [diff] [blame] | 161 | return struct.unpack('>L', data)[0] |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 162 | |
| 163 | def unpack_int(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 164 | i = self.__pos |
| 165 | self.__pos = j = i+4 |
| 166 | data = self.__buf[i:j] |
| 167 | if len(data) < 4: |
| 168 | raise EOFError |
| 169 | return struct.unpack('>l', data)[0] |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 170 | |
| 171 | unpack_enum = unpack_int |
Martin v. Löwis | c2a0ac2 | 2005-02-24 20:22:10 +0000 | [diff] [blame] | 172 | |
| 173 | def unpack_bool(self): |
| 174 | return bool(self.unpack_int()) |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 175 | |
| 176 | def unpack_uhyper(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 177 | hi = self.unpack_uint() |
| 178 | lo = self.unpack_uint() |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 179 | return int(hi)<<32 | lo |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 180 | |
| 181 | def unpack_hyper(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 182 | x = self.unpack_uhyper() |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 183 | if x >= 0x8000000000000000: |
| 184 | x = x - 0x10000000000000000 |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 185 | return x |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 186 | |
| 187 | def unpack_float(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 188 | i = self.__pos |
| 189 | self.__pos = j = i+4 |
| 190 | data = self.__buf[i:j] |
| 191 | if len(data) < 4: |
| 192 | raise EOFError |
| 193 | return struct.unpack('>f', data)[0] |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 194 | |
Guido van Rossum | 6083f0e | 1997-01-02 22:52:15 +0000 | [diff] [blame] | 195 | def unpack_double(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 196 | i = self.__pos |
| 197 | self.__pos = j = i+8 |
| 198 | data = self.__buf[i:j] |
| 199 | if len(data) < 8: |
| 200 | raise EOFError |
| 201 | return struct.unpack('>d', data)[0] |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 202 | |
| 203 | def unpack_fstring(self, n): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 204 | if n < 0: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 205 | raise ValueError('fstring size must be nonnegative') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 206 | i = self.__pos |
Thomas Wouters | 4f564bd | 2006-04-20 22:36:57 +0000 | [diff] [blame] | 207 | j = i + (n+3)//4*4 |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 208 | if j > len(self.__buf): |
| 209 | raise EOFError |
| 210 | self.__pos = j |
| 211 | return self.__buf[i:i+n] |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 212 | |
| 213 | unpack_fopaque = unpack_fstring |
| 214 | |
| 215 | def unpack_string(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 216 | n = self.unpack_uint() |
| 217 | return self.unpack_fstring(n) |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 218 | |
| 219 | unpack_opaque = unpack_string |
| 220 | unpack_bytes = unpack_string |
| 221 | |
| 222 | def unpack_list(self, unpack_item): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 223 | list = [] |
| 224 | while 1: |
| 225 | x = self.unpack_uint() |
| 226 | if x == 0: break |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 227 | if x != 1: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 228 | raise ConversionError('0 or 1 expected, got %r' % (x,)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 229 | item = unpack_item() |
| 230 | list.append(item) |
| 231 | return list |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 232 | |
| 233 | def unpack_farray(self, n, unpack_item): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 234 | list = [] |
| 235 | for i in range(n): |
| 236 | list.append(unpack_item()) |
| 237 | return list |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 238 | |
| 239 | def unpack_array(self, unpack_item): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 240 | n = self.unpack_uint() |
| 241 | return self.unpack_farray(n, unpack_item) |