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