blob: 17a5f92cc3be634b6dc040a5fe80ed2fcf49c51e [file] [log] [blame]
Guido van Rossume3cafbe1992-12-14 23:25:04 +00001# Implement (a subset of) Sun XDR -- RFC1014.
2
3
Guido van Rossum7271bab1992-12-17 17:32:24 +00004try:
5 import struct
6except ImportError:
7 struct = None
Guido van Rossume3cafbe1992-12-14 23:25:04 +00008
9
Guido van Rossum38625351992-12-15 21:43:59 +000010Long = type(0L)
11
12
Guido van Rossume3cafbe1992-12-14 23:25:04 +000013class Packer:
14
15 def init(self):
16 self.reset()
17 return self
18
19 def reset(self):
20 self.buf = ''
21
22 def get_buf(self):
23 return self.buf
24
25 def pack_uint(self, x):
26 self.buf = self.buf + \
27 (chr(int(x>>24 & 0xff)) + chr(int(x>>16 & 0xff)) + \
28 chr(int(x>>8 & 0xff)) + chr(int(x & 0xff)))
Guido van Rossum7271bab1992-12-17 17:32:24 +000029 if struct and struct.pack('l', 1) == '\0\0\0\1':
Guido van Rossume3cafbe1992-12-14 23:25:04 +000030 def pack_uint(self, x):
Guido van Rossum38625351992-12-15 21:43:59 +000031 if type(x) == Long:
32 x = int((x + 0x80000000L) % 0x100000000L \
33 - 0x80000000L)
34 self.buf = self.buf + struct.pack('l', x)
Guido van Rossume3cafbe1992-12-14 23:25:04 +000035
36 pack_int = pack_uint
37
38 pack_enum = pack_int
39
40 def pack_bool(self, x):
41 if x: self.buf = self.buf + '\0\0\0\1'
42 else: self.buf = self.buf + '\0\0\0\0'
43
44 def pack_uhyper(self, x):
Guido van Rossum38625351992-12-15 21:43:59 +000045 self.pack_uint(int(x>>32 & 0xffffffff))
46 self.pack_uint(int(x & 0xffffffff))
Guido van Rossume3cafbe1992-12-14 23:25:04 +000047
48 pack_hyper = pack_uhyper
49
50 def pack_fstring(self, n, s):
51 if n < 0:
52 raise ValueError, 'fstring size must be nonnegative'
53 n = ((n+3)/4)*4
54 data = s[:n]
55 data = data + (n - len(data)) * '\0'
56 self.buf = self.buf + data
57
58 pack_fopaque = pack_fstring
59
60 def pack_string(self, s):
61 n = len(s)
62 self.pack_uint(n)
63 self.pack_fstring(n, s)
64
65 pack_opaque = pack_string
66
67 def pack_list(self, list, pack_item):
68 for item in list:
69 self.pack_uint(1)
70 pack_item(list)
71 self.pack_uint(0)
72
Guido van Rossuma3b986e1992-12-21 14:33:18 +000073 def pack_farray(self, n, list, pack_item):
74 if len(list) <> n:
75 raise ValueError, 'wrong array size'
76 for item in list:
77 pack_item(item)
78
79 def pack_array(self, list, pack_item):
80 n = len(list)
81 self.pack_uint(n)
82 self.pack_farray(n, list)
83
Guido van Rossume3cafbe1992-12-14 23:25:04 +000084
85class Unpacker:
86
87 def init(self, data):
88 self.reset(data)
89 return self
90
91 def reset(self, data):
92 self.buf = data
93 self.pos = 0
94
95 def done(self):
96 if self.pos < len(self.buf):
97 raise RuntimeError, 'unextracted data remains'
98
99 def unpack_uint(self):
100 i = self.pos
101 self.pos = j = i+4
102 data = self.buf[i:j]
Guido van Rossumc91d60a1992-12-19 00:06:17 +0000103 if len(data) < 4:
104 raise EOFError
Guido van Rossume3cafbe1992-12-14 23:25:04 +0000105 x = long(ord(data[0]))<<24 | ord(data[1])<<16 | \
106 ord(data[2])<<8 | ord(data[3])
107 # Return a Python long only if the value is not representable
108 # as a nonnegative Python int
109 if x < 0x80000000L: x = int(x)
110 return x
Guido van Rossum7271bab1992-12-17 17:32:24 +0000111 if struct and struct.unpack('l', '\0\0\0\1') == 1:
Guido van Rossume3cafbe1992-12-14 23:25:04 +0000112 def unpack_uint(self):
113 i = self.pos
114 self.pos = j = i+4
Guido van Rossumc91d60a1992-12-19 00:06:17 +0000115 data = self.buf[i:j]
116 if len(data) < 4:
117 raise EOFError
118 return struct.unpack('l', data)
Guido van Rossume3cafbe1992-12-14 23:25:04 +0000119
120 def unpack_int(self):
121 x = self.unpack_uint()
122 if x >= 0x80000000L: x = x - 0x100000000L
123 return int(x)
124
125 unpack_enum = unpack_int
126
127 unpack_bool = unpack_int
128
129 def unpack_uhyper(self):
130 hi = self.unpack_uint()
131 lo = self.unpack_uint()
132 return long(hi)<<32 | lo
133
134 def unpack_hyper(self):
135 x = self.unpack_uhyper()
136 if x >= 0x8000000000000000L: x = x - 0x10000000000000000L
137 return x
138
139 def unpack_fstring(self, n):
140 if n < 0:
141 raise ValueError, 'fstring size must be nonnegative'
142 i = self.pos
143 j = i + (n+3)/4*4
144 if j > len(self.buf):
Guido van Rossumc91d60a1992-12-19 00:06:17 +0000145 raise EOFError
Guido van Rossume3cafbe1992-12-14 23:25:04 +0000146 self.pos = j
147 return self.buf[i:i+n]
148
149 unpack_fopaque = unpack_fstring
150
151 def unpack_string(self):
152 n = self.unpack_uint()
153 return self.unpack_fstring(n)
154
155 unpack_opaque = unpack_string
156
157 def unpack_list(self, unpack_item):
158 list = []
159 while 1:
160 x = self.unpack_uint()
161 if not x: break
162 if x <> 1:
163 raise RuntimeError, \
164 '0 or 1 expected, got ' + `x`
165 list.append(unpack_item())
166 return list
Guido van Rossuma3b986e1992-12-21 14:33:18 +0000167
168 def unpack_farray(self, n, unpack_item):
169 list = []
170 for i in range(n):
171 list.append(unpack_item())
172 return list
173
174 def unpack_array(self, unpack_item):
175 n = self.unpack_uint()
176 return self.unpack_farray(n, unpack_item)