Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 1 | # Implement (a subset of) Sun XDR -- RFC1014. |
| 2 | |
| 3 | |
Guido van Rossum | 7271bab | 1992-12-17 17:32:24 +0000 | [diff] [blame] | 4 | try: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 5 | import struct |
Guido van Rossum | 7271bab | 1992-12-17 17:32:24 +0000 | [diff] [blame] | 6 | except ImportError: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 7 | struct = None |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 8 | |
| 9 | |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 10 | Long = type(0) |
Guido van Rossum | 3862535 | 1992-12-15 21:43:59 +0000 | [diff] [blame] | 11 | |
| 12 | |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 13 | class Packer: |
| 14 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 15 | def __init__(self): |
| 16 | self.reset() |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 17 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 18 | def reset(self): |
| 19 | self.buf = '' |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 20 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 21 | def get_buf(self): |
| 22 | return self.buf |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 23 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 24 | def pack_uint(self, x): |
| 25 | self.buf = self.buf + \ |
| 26 | (chr(int(x>>24 & 0xff)) + chr(int(x>>16 & 0xff)) + \ |
| 27 | chr(int(x>>8 & 0xff)) + chr(int(x & 0xff))) |
| 28 | if struct and struct.pack('l', 1) == '\0\0\0\1': |
| 29 | def pack_uint(self, x): |
| 30 | if type(x) == Long: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 31 | x = int((x + 0x80000000) % 0x100000000 \ |
| 32 | - 0x80000000) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 33 | self.buf = self.buf + struct.pack('l', x) |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 34 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 35 | pack_int = pack_uint |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 36 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 37 | pack_enum = pack_int |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 38 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 39 | def pack_bool(self, x): |
| 40 | if x: self.buf = self.buf + '\0\0\0\1' |
| 41 | else: self.buf = self.buf + '\0\0\0\0' |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 42 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 43 | def pack_uhyper(self, x): |
| 44 | self.pack_uint(int(x>>32 & 0xffffffff)) |
| 45 | self.pack_uint(int(x & 0xffffffff)) |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 46 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 47 | pack_hyper = pack_uhyper |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 48 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 49 | def pack_float(self, x): |
| 50 | # XXX |
| 51 | self.buf = self.buf + struct.pack('f', x) |
Guido van Rossum | 9e80d6f | 1993-12-17 14:32:26 +0000 | [diff] [blame] | 52 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 53 | def pack_double(self, x): |
| 54 | # XXX |
| 55 | self.buf = self.buf + struct.pack('d', x) |
Guido van Rossum | 9e80d6f | 1993-12-17 14:32:26 +0000 | [diff] [blame] | 56 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 57 | def pack_fstring(self, n, s): |
| 58 | if n < 0: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 59 | raise ValueError('fstring size must be nonnegative') |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 60 | n = ((n+3)/4)*4 |
| 61 | data = s[:n] |
| 62 | data = data + (n - len(data)) * '\0' |
| 63 | self.buf = self.buf + data |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 64 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 65 | pack_fopaque = pack_fstring |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 66 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 67 | def pack_string(self, s): |
| 68 | n = len(s) |
| 69 | self.pack_uint(n) |
| 70 | self.pack_fstring(n, s) |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 71 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 72 | pack_opaque = pack_string |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 73 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 74 | def pack_list(self, list, pack_item): |
| 75 | for item in list: |
| 76 | self.pack_uint(1) |
| 77 | pack_item(item) |
| 78 | self.pack_uint(0) |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 79 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 80 | def pack_farray(self, n, list, pack_item): |
Neal Norwitz | 3bd844e | 2006-08-29 04:39:12 +0000 | [diff] [blame] | 81 | if len(list) != n: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 82 | raise ValueError('wrong array size') |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 83 | for item in list: |
| 84 | pack_item(item) |
Guido van Rossum | a3b986e | 1992-12-21 14:33:18 +0000 | [diff] [blame] | 85 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 86 | def pack_array(self, list, pack_item): |
| 87 | n = len(list) |
| 88 | self.pack_uint(n) |
| 89 | self.pack_farray(n, list, pack_item) |
Guido van Rossum | a3b986e | 1992-12-21 14:33:18 +0000 | [diff] [blame] | 90 | |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 91 | |
| 92 | class Unpacker: |
| 93 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 94 | def __init__(self, data): |
| 95 | self.reset(data) |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 96 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 97 | def reset(self, data): |
| 98 | self.buf = data |
| 99 | self.pos = 0 |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 100 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 101 | def done(self): |
| 102 | if self.pos < len(self.buf): |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 103 | raise RuntimeError('unextracted data remains') |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 104 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 105 | def unpack_uint(self): |
| 106 | i = self.pos |
| 107 | self.pos = j = i+4 |
| 108 | data = self.buf[i:j] |
| 109 | if len(data) < 4: |
| 110 | raise EOFError |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 111 | x = int(ord(data[0]))<<24 | ord(data[1])<<16 | \ |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 112 | ord(data[2])<<8 | ord(data[3]) |
| 113 | # Return a Python long only if the value is not representable |
| 114 | # as a nonnegative Python int |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 115 | if x < 0x80000000: x = int(x) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 116 | return x |
| 117 | if struct and struct.unpack('l', '\0\0\0\1') == 1: |
| 118 | def unpack_uint(self): |
| 119 | i = self.pos |
| 120 | self.pos = j = i+4 |
| 121 | data = self.buf[i:j] |
| 122 | if len(data) < 4: |
| 123 | raise EOFError |
| 124 | return struct.unpack('l', data) |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 125 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 126 | def unpack_int(self): |
| 127 | x = self.unpack_uint() |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 128 | if x >= 0x80000000: x = x - 0x100000000 |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 129 | return int(x) |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 130 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 131 | unpack_enum = unpack_int |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 132 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 133 | unpack_bool = unpack_int |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 134 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 135 | def unpack_uhyper(self): |
| 136 | hi = self.unpack_uint() |
| 137 | lo = self.unpack_uint() |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 138 | return int(hi)<<32 | lo |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 139 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 140 | def unpack_hyper(self): |
| 141 | x = self.unpack_uhyper() |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 142 | if x >= 0x8000000000000000: x = x - 0x10000000000000000 |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 143 | return x |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 144 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 145 | def unpack_float(self): |
| 146 | # XXX |
| 147 | i = self.pos |
| 148 | self.pos = j = i+4 |
| 149 | data = self.buf[i:j] |
| 150 | if len(data) < 4: |
| 151 | raise EOFError |
| 152 | return struct.unpack('f', data)[0] |
Guido van Rossum | 9e80d6f | 1993-12-17 14:32:26 +0000 | [diff] [blame] | 153 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 154 | def unpack_double(self): |
| 155 | # XXX |
| 156 | i = self.pos |
| 157 | self.pos = j = i+8 |
| 158 | data = self.buf[i:j] |
| 159 | if len(data) < 8: |
| 160 | raise EOFError |
| 161 | return struct.unpack('d', data)[0] |
Guido van Rossum | 9e80d6f | 1993-12-17 14:32:26 +0000 | [diff] [blame] | 162 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 163 | def unpack_fstring(self, n): |
| 164 | if n < 0: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 165 | raise ValueError('fstring size must be nonnegative') |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 166 | i = self.pos |
| 167 | j = i + (n+3)/4*4 |
| 168 | if j > len(self.buf): |
| 169 | raise EOFError |
| 170 | self.pos = j |
| 171 | return self.buf[i:i+n] |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 172 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 173 | unpack_fopaque = unpack_fstring |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 174 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 175 | def unpack_string(self): |
| 176 | n = self.unpack_uint() |
| 177 | return self.unpack_fstring(n) |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 178 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 179 | unpack_opaque = unpack_string |
Guido van Rossum | e3cafbe | 1992-12-14 23:25:04 +0000 | [diff] [blame] | 180 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 181 | def unpack_list(self, unpack_item): |
| 182 | list = [] |
| 183 | while 1: |
| 184 | x = self.unpack_uint() |
| 185 | if x == 0: break |
Neal Norwitz | 3bd844e | 2006-08-29 04:39:12 +0000 | [diff] [blame] | 186 | if x != 1: |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 187 | raise RuntimeError('0 or 1 expected, got %r' % (x, )) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 188 | item = unpack_item() |
| 189 | list.append(item) |
| 190 | return list |
Guido van Rossum | a3b986e | 1992-12-21 14:33:18 +0000 | [diff] [blame] | 191 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 192 | def unpack_farray(self, n, unpack_item): |
| 193 | list = [] |
| 194 | for i in range(n): |
| 195 | list.append(unpack_item()) |
| 196 | return list |
Guido van Rossum | a3b986e | 1992-12-21 14:33:18 +0000 | [diff] [blame] | 197 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 198 | def unpack_array(self, unpack_item): |
| 199 | n = self.unpack_uint() |
| 200 | return self.unpack_farray(n, unpack_item) |