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