ia64/xen-unstable

view tools/xfrd/marshal.c @ 1820:3d4f8eb89670

bitkeeper revision 1.1106.1.2 (40faa780dekT3E5arFwcbQDu1MbX6g)

Cleaned up Xen's instruction emulator.
author kaf24@scramble.cl.cam.ac.uk
date Sun Jul 18 16:38:24 2004 +0000 (2004-07-18)
parents 0e23f01219c6
children 59a924789d34 a01199a95070
line source
1 #include <errno.h>
2 #include "sys_net.h"
3 #include "allocate.h"
4 #include "marshal.h"
6 #define dprintf(fmt, args...) IOStream_print(iostdout, "[DEBUG] %s" fmt, __FUNCTION__, ##args)
7 #define wprintf(fmt, args...) IOStream_print(iostderr, "[WARN] %s" fmt, __FUNCTION__, ##args)
8 #define iprintf(fmt, args...) IOStream_print(iostdout, "[INFO] %s" fmt, __FUNCTION__, ##args)
9 #define eprintf(fmt, args...) IOStream_print(iostderr, "[ERROR] %s" fmt, __FUNCTION__, ##args)
12 #define ARRAY_SIZE(ary) (sizeof(ary)/sizeof((ary)[0]))
14 /* Messages are coded as msgid followed by message fields.
15 * Initial message on any channel is hello - so can check version
16 * compatibility.
17 *
18 * char* -> uint16_t:n <n bytes>
19 * ints/uints go as suitable number of bytes (e.g. uint16_t is 2 bytes).
20 * optional fields go as '1' <val> or '0' (the 0/1 is 1 byte).
21 * lists go as ('1' <elt>)* '0'
22 */
24 int marshal_flush(IOStream *io){
25 int err = 0;
26 err = IOStream_flush(io);
27 return err;
28 }
30 int marshal_bytes(IOStream *io, void *s, uint32_t s_n){
31 int err = 0;
32 int n;
33 n = IOStream_write(io, s, s_n);
34 if(n < 0){
35 err = n;
36 } else if (n < s_n){
37 wprintf("> Wanted %d, got %d\n", s_n, n);
38 err = -EIO;
39 }
40 return err;
41 }
43 int unmarshal_bytes(IOStream *io, void *s, uint32_t s_n){
44 int err = 0;
45 int n;
46 //dprintf("> s_n=%d\n", s_n);
47 n = IOStream_read(io, s, s_n);
48 //dprintf("> n=%d\n", n);
49 if(n < 0){
50 err = n;
51 } else if(n < s_n){
52 wprintf("> Wanted %d, got %d\n", s_n, n);
53 err = -EIO;
54 }
55 //dprintf("< err=%d\n", err);
56 return err;
57 }
59 int marshal_uint8(IOStream *io, uint8_t x){
60 return marshal_bytes(io, &x, sizeof(x));
61 }
63 int unmarshal_uint8(IOStream *io, uint8_t *x){
64 return unmarshal_bytes(io, x, sizeof(*x));
65 }
67 int marshal_uint16(IOStream *io, uint16_t x){
68 x = htons(x);
69 return marshal_bytes(io, &x, sizeof(x));
70 }
72 int unmarshal_uint16(IOStream *io, uint16_t *x){
73 int err = 0;
74 err = unmarshal_bytes(io, x, sizeof(*x));
75 *x = ntohs(*x);
76 return err;
77 }
79 int marshal_int32(IOStream *io, int32_t x){
80 int err = 0;
81 //dprintf("> x=%d\n", x);
82 x = htonl(x);
83 err = marshal_bytes(io, &x, sizeof(x));
84 //dprintf("< err=%d\n", err);
85 return err;
86 }
88 int unmarshal_int32(IOStream *io, int32_t *x){
89 int err = 0;
90 //dprintf(">\n");
91 err = unmarshal_bytes(io, x, sizeof(*x));
92 *x = ntohl(*x);
93 //dprintf("< err=%d x=%d\n", err, *x);
94 return err;
95 }
97 int marshal_uint32(IOStream *io, uint32_t x){
98 int err = 0;
99 //dprintf("> x=%u\n", x);
100 x = htonl(x);
101 err = marshal_bytes(io, &x, sizeof(x));
102 //dprintf("< err=%d\n", err);
103 return err;
104 }
106 int unmarshal_uint32(IOStream *io, uint32_t *x){
107 int err = 0;
108 //dprintf(">\n");
109 err = unmarshal_bytes(io, x, sizeof(*x));
110 *x = ntohl(*x);
111 //dprintf("< err=%d x=%u\n", err, *x);
112 return err;
113 }
115 int marshal_uint64(IOStream *io, uint64_t x){
116 int err;
117 err = marshal_uint32(io, (uint32_t) ((x >> 32) & 0xffffffff));
118 if(err) goto exit;
119 err = marshal_uint32(io, (uint32_t) ( x & 0xffffffff));
120 exit:
121 return err;
122 }
124 int unmarshal_uint64(IOStream *io, uint64_t *x){
125 int err = 0;
126 uint32_t hi, lo;
127 err = unmarshal_uint32(io, &hi);
128 if(err) goto exit;
129 err = unmarshal_uint32(io, &lo);
130 *x = (((uint64_t) hi) << 32) | lo;
131 exit:
132 return err;
133 }
135 int marshal_net16(IOStream *io, net16_t x){
136 return marshal_bytes(io, &x, sizeof(x));
137 }
139 int unmarshal_net16(IOStream *io, net16_t *x){
140 int err = 0;
141 err = unmarshal_bytes(io, x, sizeof(*x));
142 return err;
143 }
145 int marshal_net32(IOStream *io, net32_t x){
146 return marshal_bytes(io, &x, sizeof(x));
147 }
149 int unmarshal_net32(IOStream *io, net32_t *x){
150 int err = 0;
151 err = unmarshal_bytes(io, x, sizeof(*x));
152 return err;
153 }
155 int marshal_string(IOStream *io, char *s, uint32_t s_n){
156 int err;
157 //dprintf("> s=%s\n", s);
158 err = marshal_uint32(io, s_n);
159 if(err) goto exit;
160 err = marshal_bytes(io, s, s_n);
161 exit:
162 //dprintf("< err=%d\n", err);
163 return err;
164 }
166 int unmarshal_string(IOStream *io, char *s, uint32_t s_n){
167 int err = 0, val_n = 0;
168 //dprintf(">\n");
169 err = unmarshal_uint32(io, &val_n);
170 if(err) goto exit;
171 if(val_n >= s_n){
172 err = -EINVAL;
173 goto exit;
174 }
175 err = unmarshal_bytes(io, s, val_n);
176 if(err) goto exit;
177 s[val_n] = '\0';
178 exit:
179 //dprintf("< err=%d s=%s\n", err, s);
180 return err;
181 }
183 int unmarshal_new_string(IOStream *io, char **s, uint32_t *s_n){
184 int err = 0, val_n = 0;
185 char *val = NULL;
186 //dprintf(">\n");
187 err = unmarshal_uint32(io, &val_n);
188 if(err) goto exit;
189 val = allocate(val_n + 1);
190 if(!val){
191 err = -ENOMEM;
192 goto exit;
193 }
194 err = unmarshal_bytes(io, val, val_n);
195 if(err) goto exit;
196 val[val_n] = '\0';
197 exit:
198 if(err){
199 if(val) deallocate(val);
200 val = NULL;
201 val_n = 0;
202 }
203 *s = val;
204 if(s_n) *s_n = val_n;
205 //dprintf("< err=%d s=%s\n", err, *s);
206 return err;
207 }