blob: bee88b439c1738cef9416278fa14aee364464616 [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
73
74class Unpacker:
75
76 def init(self, data):
77 self.reset(data)
78 return self
79
80 def reset(self, data):
81 self.buf = data
82 self.pos = 0
83
84 def done(self):
85 if self.pos < len(self.buf):
86 raise RuntimeError, 'unextracted data remains'
87
88 def unpack_uint(self):
89 i = self.pos
90 self.pos = j = i+4
91 data = self.buf[i:j]
92 x = long(ord(data[0]))<<24 | ord(data[1])<<16 | \
93 ord(data[2])<<8 | ord(data[3])
94 # Return a Python long only if the value is not representable
95 # as a nonnegative Python int
96 if x < 0x80000000L: x = int(x)
97 return x
Guido van Rossum7271bab1992-12-17 17:32:24 +000098 if struct and struct.unpack('l', '\0\0\0\1') == 1:
Guido van Rossume3cafbe1992-12-14 23:25:04 +000099 def unpack_uint(self):
100 i = self.pos
101 self.pos = j = i+4
Guido van Rossum3346b6a1992-12-17 17:12:48 +0000102 return struct.unpack('l', self.buf[i:j])
Guido van Rossume3cafbe1992-12-14 23:25:04 +0000103
104 def unpack_int(self):
105 x = self.unpack_uint()
106 if x >= 0x80000000L: x = x - 0x100000000L
107 return int(x)
108
109 unpack_enum = unpack_int
110
111 unpack_bool = unpack_int
112
113 def unpack_uhyper(self):
114 hi = self.unpack_uint()
115 lo = self.unpack_uint()
116 return long(hi)<<32 | lo
117
118 def unpack_hyper(self):
119 x = self.unpack_uhyper()
120 if x >= 0x8000000000000000L: x = x - 0x10000000000000000L
121 return x
122
123 def unpack_fstring(self, n):
124 if n < 0:
125 raise ValueError, 'fstring size must be nonnegative'
126 i = self.pos
127 j = i + (n+3)/4*4
128 if j > len(self.buf):
129 raise RuntimeError, 'buffer overrun'
130 self.pos = j
131 return self.buf[i:i+n]
132
133 unpack_fopaque = unpack_fstring
134
135 def unpack_string(self):
136 n = self.unpack_uint()
137 return self.unpack_fstring(n)
138
139 unpack_opaque = unpack_string
140
141 def unpack_list(self, unpack_item):
142 list = []
143 while 1:
144 x = self.unpack_uint()
145 if not x: break
146 if x <> 1:
147 raise RuntimeError, \
148 '0 or 1 expected, got ' + `x`
149 list.append(unpack_item())
150 return list