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 |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 9 | |
Skip Montanaro | 40fc160 | 2001-03-01 04:27:19 +0000 | [diff] [blame] | 10 | __all__ = ["Error", "Packer", "Unpacker", "ConversionError"] |
| 11 | |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 12 | # exceptions |
Neal Norwitz | f74e46c | 2002-03-31 13:59:18 +0000 | [diff] [blame] | 13 | class Error(Exception): |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 14 | """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 Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 24 | self.msg = msg |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 25 | def __repr__(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 26 | return repr(self.msg) |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 27 | def __str__(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 28 | return str(self.msg) |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 29 | |
| 30 | |
| 31 | class ConversionError(Error): |
| 32 | pass |
| 33 | |
| 34 | |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 36 | class Packer: |
| 37 | """Pack various data representations into a buffer.""" |
| 38 | |
| 39 | def __init__(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 40 | self.reset() |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 41 | |
| 42 | def reset(self): |
Brett Cannon | 1eb79cf | 2007-07-26 03:19:46 +0000 | [diff] [blame] | 43 | self.__buf = BytesIO() |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 44 | |
| 45 | def get_buffer(self): |
Martin v. Löwis | c47016e | 2001-08-16 17:06:44 +0000 | [diff] [blame] | 46 | return self.__buf.getvalue() |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 47 | # backwards compatibility |
| 48 | get_buf = get_buffer |
| 49 | |
| 50 | def pack_uint(self, x): |
Martin v. Löwis | c47016e | 2001-08-16 17:06:44 +0000 | [diff] [blame] | 51 | self.__buf.write(struct.pack('>L', x)) |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 52 | |
| 53 | pack_int = pack_uint |
| 54 | pack_enum = pack_int |
| 55 | |
| 56 | def pack_bool(self, x): |
Brett Cannon | 1eb79cf | 2007-07-26 03:19:46 +0000 | [diff] [blame] | 57 | if x: self.__buf.write(b'\0\0\0\1') |
| 58 | else: self.__buf.write(b'\0\0\0\0') |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 59 | |
| 60 | def pack_uhyper(self, x): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 61 | self.pack_uint(x>>32 & 0xffffffff) |
| 62 | self.pack_uint(x & 0xffffffff) |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 63 | |
| 64 | pack_hyper = pack_uhyper |
| 65 | |
| 66 | def pack_float(self, x): |
Martin v. Löwis | c47016e | 2001-08-16 17:06:44 +0000 | [diff] [blame] | 67 | try: self.__buf.write(struct.pack('>f', x)) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 68 | except struct.error as msg: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 69 | raise ConversionError(msg) |
Guido van Rossum | 6083f0e | 1997-01-02 22:52:15 +0000 | [diff] [blame] | 70 | |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 71 | def pack_double(self, x): |
Martin v. Löwis | c47016e | 2001-08-16 17:06:44 +0000 | [diff] [blame] | 72 | try: self.__buf.write(struct.pack('>d', x)) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 73 | except struct.error as msg: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 74 | raise ConversionError(msg) |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 75 | |
| 76 | def pack_fstring(self, n, s): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 77 | if n < 0: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 78 | raise ValueError('fstring size must be nonnegative') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 79 | data = s[:n] |
Thomas Wouters | 4f564bd | 2006-04-20 22:36:57 +0000 | [diff] [blame] | 80 | n = ((n+3)//4)*4 |
Brett Cannon | 1eb79cf | 2007-07-26 03:19:46 +0000 | [diff] [blame] | 81 | data = data + (n - len(data)) * b'\0' |
Martin v. Löwis | c47016e | 2001-08-16 17:06:44 +0000 | [diff] [blame] | 82 | self.__buf.write(data) |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 83 | |
| 84 | pack_fopaque = pack_fstring |
| 85 | |
| 86 | def pack_string(self, s): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 87 | n = len(s) |
| 88 | self.pack_uint(n) |
| 89 | self.pack_fstring(n, s) |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 90 | |
| 91 | pack_opaque = pack_string |
| 92 | pack_bytes = pack_string |
| 93 | |
| 94 | def pack_list(self, list, pack_item): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 95 | for item in list: |
| 96 | self.pack_uint(1) |
| 97 | pack_item(item) |
| 98 | self.pack_uint(0) |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 99 | |
| 100 | def pack_farray(self, n, list, pack_item): |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 101 | if len(list) != n: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 102 | raise ValueError('wrong array size') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 103 | for item in list: |
| 104 | pack_item(item) |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 105 | |
| 106 | def pack_array(self, list, pack_item): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 107 | n = len(list) |
| 108 | self.pack_uint(n) |
| 109 | self.pack_farray(n, list, pack_item) |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 110 | |
| 111 | |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 112 | |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 113 | class Unpacker: |
| 114 | """Unpacks various data representations from the given buffer.""" |
| 115 | |
| 116 | def __init__(self, data): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 117 | self.reset(data) |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 118 | |
| 119 | def reset(self, data): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 120 | self.__buf = data |
| 121 | self.__pos = 0 |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 122 | |
| 123 | def get_position(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 124 | return self.__pos |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 125 | |
| 126 | def set_position(self, position): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 127 | self.__pos = position |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 128 | |
Barry Warsaw | 75eccc5 | 1996-12-04 22:04:39 +0000 | [diff] [blame] | 129 | def get_buffer(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 130 | return self.__buf |
Barry Warsaw | 75eccc5 | 1996-12-04 22:04:39 +0000 | [diff] [blame] | 131 | |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 132 | def done(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 133 | if self.__pos < len(self.__buf): |
| 134 | raise Error('unextracted data remains') |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 135 | |
| 136 | def unpack_uint(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 137 | 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 Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 147 | |
| 148 | def unpack_int(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 149 | 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 Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 155 | |
| 156 | unpack_enum = unpack_int |
Martin v. Löwis | c2a0ac2 | 2005-02-24 20:22:10 +0000 | [diff] [blame] | 157 | |
| 158 | def unpack_bool(self): |
| 159 | return bool(self.unpack_int()) |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 160 | |
| 161 | def unpack_uhyper(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 162 | hi = self.unpack_uint() |
| 163 | lo = self.unpack_uint() |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 164 | return int(hi)<<32 | lo |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 165 | |
| 166 | def unpack_hyper(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 167 | x = self.unpack_uhyper() |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 168 | if x >= 0x8000000000000000: |
| 169 | x = x - 0x10000000000000000 |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 170 | return x |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 171 | |
| 172 | def unpack_float(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 173 | 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 Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 179 | |
Guido van Rossum | 6083f0e | 1997-01-02 22:52:15 +0000 | [diff] [blame] | 180 | def unpack_double(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 181 | 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 Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 187 | |
| 188 | def unpack_fstring(self, n): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 189 | if n < 0: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 190 | raise ValueError('fstring size must be nonnegative') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 191 | i = self.__pos |
Thomas Wouters | 4f564bd | 2006-04-20 22:36:57 +0000 | [diff] [blame] | 192 | j = i + (n+3)//4*4 |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 193 | if j > len(self.__buf): |
| 194 | raise EOFError |
| 195 | self.__pos = j |
| 196 | return self.__buf[i:i+n] |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 197 | |
| 198 | unpack_fopaque = unpack_fstring |
| 199 | |
| 200 | def unpack_string(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 201 | n = self.unpack_uint() |
| 202 | return self.unpack_fstring(n) |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 203 | |
| 204 | unpack_opaque = unpack_string |
| 205 | unpack_bytes = unpack_string |
| 206 | |
| 207 | def unpack_list(self, unpack_item): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 208 | list = [] |
| 209 | while 1: |
| 210 | x = self.unpack_uint() |
| 211 | if x == 0: break |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 212 | if x != 1: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 213 | raise ConversionError('0 or 1 expected, got %r' % (x,)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 214 | item = unpack_item() |
| 215 | list.append(item) |
| 216 | return list |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 217 | |
| 218 | def unpack_farray(self, n, unpack_item): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 219 | list = [] |
| 220 | for i in range(n): |
| 221 | list.append(unpack_item()) |
| 222 | return list |
Guido van Rossum | 731630b | 1996-08-19 22:26:43 +0000 | [diff] [blame] | 223 | |
| 224 | def unpack_array(self, unpack_item): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 225 | n = self.unpack_uint() |
| 226 | return self.unpack_farray(n, unpack_item) |