ia64/xen-unstable

view tools/xfrd/xdr.c @ 3498:1d24a5b0b338

bitkeeper revision 1.1159.223.25 (41f2cb9aEKMZkZbvqBE0eXhpljlV4Q)

Description: fix path to python
There is no python2 in debian. Instead, use python.

From: Adam Heath <doogie@brainfood.com>
Signed-off-by: ian.pratt@cl.cam.ac.uk
author iap10@labyrinth.cl.cam.ac.uk
date Sat Jan 22 21:54:34 2005 +0000 (2005-01-22)
parents 0e23f01219c6
children 59a924789d34 a01199a95070
line source
1 #include <errno.h>
2 #include "xdr.h"
4 #define MODULE_NAME "XDR"
5 //#define DEBUG 1
6 #undef DEBUG
7 #include "debug.h"
9 /** @file
10 * XDR packer/unpacker for elements.
11 *
12 * string -> [T_STRING] [len:u16] <len bytes>
13 * atom -> [T_ATOM] [len:u16] <len bytes>
14 * uint -> [T_UINT] [value]
15 * cons -> [T_LIST] {1 elt}* 0
16 * null -> [T_NULL]
17 * none -> [T_NONE]
18 * bool -> [T_BOOL] { 0:u8 | 1:u8 }
19 *
20 * types packed as u16.
21 *
22 * So (a b c) -> [T_CONS] a [T_CONS] b [T_CONS] c [T_NULL]
23 * () -> [T_NULL]
24 */
26 int pack_bool(IOStream *io, int x){
27 int err=0;
28 //dprintf("> x=%d\n", x);
29 err = IOStream_print(io, "%c", 0xff & x);
30 if(err > 0) err = 0;
31 //dprintf("< err=%d\n", err);
32 return err;
33 }
35 int unpack_bool(IOStream *io, int *x){
36 int err = 0;
37 int c;
38 //dprintf(">\n");
39 c = IOStream_getc(io);
40 *x = (c < 0 ? 0 : c);
41 err = IOStream_error(io);
42 if(c < 0 && !err) err = -EIO;
43 //dprintf("< err=%d x=%d\n", err, *x);
44 return err;
45 }
47 int pack_ushort(IOStream *io, unsigned short x){
48 int err=0;
49 //dprintf("> x=%u\n", x);
50 err = IOStream_print(io, "%c%c",
51 0xff & (x >> 8),
52 0xff & (x ));
53 if(err > 0) err = 0;
54 //dprintf("< err=%d\n", err);
55 return err;
56 }
58 int unpack_ushort(IOStream *io, unsigned short *x){
59 int err = 0;
60 int i, c = 0;
61 //dprintf(">\n");
62 *x = 0;
63 for(i = 0; i< 2; i++){
64 c = IOStream_getc(io);
65 if(c < 0) break;
66 *x <<= 8;
67 *x |= (0xff & c);
68 }
69 err = IOStream_error(io);
71 if(c < 0 && !err) err = -EIO;
72 //dprintf("< err=%d x=%u\n", err, *x);
73 return err;
74 }
76 int pack_type(IOStream *io, unsigned short x){
77 return pack_ushort(io, x);
78 }
80 int unpack_type(IOStream *io, unsigned short *x){
81 return unpack_ushort(io, x);
82 }
84 int pack_uint(IOStream *io, unsigned int x){
85 int err=0;
86 //dprintf("> x=%u\n", x);
87 err = IOStream_print(io, "%c%c%c%c",
88 0xff & (x >> 24),
89 0xff & (x >> 16),
90 0xff & (x >> 8),
91 0xff & (x ));
92 if(err > 0) err = 0;
93 //dprintf("< err=%d\n", err);
94 return err;
95 }
97 int unpack_uint(IOStream *io, unsigned int *x){
98 int err = 0;
99 int i, c = 0;
100 //dprintf(">\n");
101 *x = 0;
102 for(i = 0; i< 4; i++){
103 c = IOStream_getc(io);
104 if(c < 0) break;
105 *x <<= 8;
106 *x |= (0xff & c);
107 }
108 err = IOStream_error(io);
109 if(c < 0 && !err) err = -EIO;
110 //dprintf("< err=%d x=%u\n", err, *x);
111 return err;
112 }
114 int pack_string(IOStream *io, Sxpr x){
115 int err = 0;
116 unsigned short n = 0xffff & string_length(x);
117 char *s = string_string(x);
118 int i;
119 //dprintf("> n=%d s=%s\n", n, s);
120 err = pack_ushort(io, n);
121 if(err) goto exit;
122 for(i = 0; i < n; i++){
123 err = IOStream_print(io, "%c", s[i]);
124 if(err < 0) break;
125 }
126 if(err > 0) err = 0;
127 exit:
128 //dprintf("< err=%d\n", err);
129 return err;
130 }
132 int unpack_string(IOStream *io, Sxpr *x){
133 int err;
134 unsigned short n;
135 int i, c = 0;
136 char *s;
137 Sxpr val = ONONE;
139 //dprintf(">\n");
140 err = unpack_ushort(io, &n);
141 if(err) goto exit;
142 val = halloc(n+1, T_STRING);
143 if(NOMEMP(val)){
144 err = -ENOMEM;
145 goto exit;
146 }
147 s = string_string(val);
148 for(i=0; i<n; i++){
149 c = IOStream_getc(io);
150 if(c < 0) break;
151 s[i] = (char)c;
152 }
153 s[n] = '\0';
154 exit:
155 err = IOStream_error(io);
156 if(c < 0 && !err) err = -EIO;
157 if(err){
158 objfree(val);
159 val = ONONE;
160 }
161 *x = val;
162 //IOStream_print(iostdout, "n=%d str=", n);
163 //objprint(iostdout, *x, 0);
164 //IOStream_print(iostdout, "\n");
165 //dprintf("< err=%d\n", err);
166 return err;
167 }
169 int pack_cons(IOStream *io, Sxpr x){
170 int err = 0;
171 Sxpr l;
172 //dprintf(">\n");
173 for(l = x; CONSP(l); l = CDR(l)){
174 err = pack_bool(io, 1);
175 if(err) goto exit;
176 err = pack_sxpr(io, CAR(l));
177 if(err) goto exit;
178 }
179 err = pack_bool(io, 0);
180 exit:
181 //dprintf("< err=%d\n", err);
182 return err;
183 }
185 int unpack_cons(IOStream *io, Sxpr *x){
186 int err = 0;
187 int more = 0;
188 Sxpr u = ONONE, v = ONONE, val = ONULL;
190 dprintf(">\n");
191 while(1){
192 err = unpack_bool(io, &more);
193 if(err) goto exit;
194 if(!more){
195 //IOStream_print(iostdout, "unpack_cons 1 val=");
196 ////objprint(iostdout, val, 0);
197 IOStream_print(iostdout, "\n");
199 val = nrev(val);
201 //IOStream_print(iostdout, "unpack_cons 2 val=");
202 //objprint(iostdout, val, 0);
203 //IOStream_print(iostdout, "\n");
205 break;
206 }
207 err = unpack_sxpr(io, &u);
208 if(err) goto exit;
209 v = cons_new(u, val);
210 if(NOMEMP(v)){
211 err = -ENOMEM;
212 objfree(u);
213 goto exit;
214 }
215 val = v;
216 }
217 exit:
218 if(err){
219 objfree(val);
220 val = ONONE;
221 }
222 *x = val;
223 dprintf("< err=%d\n", err);
224 return err;
225 }
227 int pack_sxpr(IOStream *io, Sxpr x){
228 int err = 0;
229 unsigned short type = get_type(x);
230 //dprintf(">\n");
231 //objprint(iostdout, x, 0);
232 //IOStream_print(iostdout, "\n");
234 err = pack_type(io, type);
235 if(err) goto exit;
236 switch(type){
237 case T_NULL:
238 break;
239 case T_NONE:
240 break;
241 case T_BOOL:
242 err = pack_bool(io, get_ul(x));
243 break;
244 case T_CONS:
245 err = pack_cons(io, x);
246 break;
247 case T_ATOM:
248 err = pack_string(io, OBJ_ATOM(x)->name);
249 break;
250 case T_STRING:
251 err = pack_string(io, x);
252 break;
253 case T_UINT:
254 err = pack_uint(io, get_ul(x));
255 break;
256 default:
257 err = -EINVAL;
258 IOStream_print(iostderr, "%s> invalid type %d\n", __FUNCTION__, type);
259 break;
260 }
261 exit:
262 //dprintf("< err=%d\n", err);
263 return err;
264 }
266 int unpack_sxpr(IOStream *io, Sxpr *x){
267 int err = 0;
268 unsigned short type;
269 unsigned int u;
270 Sxpr val = ONONE, y;
272 //dprintf(">\n");
273 err = unpack_type(io, &type);
274 if(err) goto exit;
275 switch(type){
276 case T_NULL:
277 val = ONULL;
278 break;
279 case T_NONE:
280 val = ONONE;
281 break;
282 case T_CONS:
283 err = unpack_cons(io, &val);
284 break;
285 case T_BOOL:
286 err = unpack_bool(io, &u);
287 if(err) goto exit;
288 val = (u ? OTRUE : OFALSE);
289 break;
290 case T_ATOM:
291 err = unpack_string(io, &y);
292 if(err) goto exit;
293 val = intern(string_string(y));
294 objfree(y);
295 break;
296 case T_STRING:
297 err = unpack_string(io, &val);
298 break;
299 case T_UINT:
300 err = unpack_uint(io, &u);
301 if(err) goto exit;
302 val = OBJI(type, u);
303 break;
304 default:
305 err = -EINVAL;
306 IOStream_print(iostderr, "%s> invalid type %d\n", __FUNCTION__, type);
307 break;
308 }
309 exit:
310 *x = (err ? ONONE : val);
311 //IOStream_print(iostdout, "sxpr=");
312 //objprint(iostdout, *x, 0);
313 //IOStream_print(iostdout, "\n");
314 //dprintf("< err=%d\n", err);
315 return err;
316 }