conv* functions
[akaros.git] / kern / src / ns / convS2M.c
1 // INFERNO
2 #include <vfs.h>
3 #include <kfs.h>
4 #include <slab.h>
5 #include <kmalloc.h>
6 #include <kref.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <assert.h>
10 #include <error.h>
11 #include <cpio.h>
12 #include <pmap.h>
13 #include <smp.h>
14 #include <ip.h>
15
16 static
17 uint8_t*
18 pstring(uint8_t *p, char *s)
19 {
20         unsigned int n;
21
22         if(s == NULL){
23                 PBIT16(p, 0);
24                 p += BIT16SZ;
25                 return p;
26         }
27
28         n = strlen(s);
29         PBIT16(p, n);
30         p += BIT16SZ;
31         memmove(p, s, n);
32         p += n;
33         return p;
34 }
35
36 static
37 uint8_t*
38 pqid(uint8_t *p, struct qid *q)
39 {
40         PBIT8(p, q->type);
41         p += BIT8SZ;
42         PBIT32(p, q->vers);
43         p += BIT32SZ;
44         PBIT64(p, q->path);
45         p += BIT64SZ;
46         return p;
47 }
48
49 static
50 unsigned int
51 stringsz(char *s)
52 {
53         if(s == NULL)
54                 return BIT16SZ;
55
56         return BIT16SZ+strlen(s);
57 }
58
59 unsigned int
60 sizeS2M(struct fcall *f)
61 {
62         unsigned int n;
63         int i;
64
65         n = 0;
66         n += BIT32SZ;   /* size */
67         n += BIT8SZ;    /* type */
68         n += BIT16SZ;   /* tag */
69
70         switch(f->type)
71         {
72         default:
73                 return 0;
74
75         case Tversion:
76                 n += BIT32SZ;
77                 n += stringsz(f->version);
78                 break;
79
80         case Tflush:
81                 n += BIT16SZ;
82                 break;
83
84         case Tauth:
85                 n += BIT32SZ;
86                 n += stringsz(f->uname);
87                 n += stringsz(f->aname);
88                 break;
89
90         case Tattach:
91                 n += BIT32SZ;
92                 n += BIT32SZ;
93                 n += stringsz(f->uname);
94                 n += stringsz(f->aname);
95                 break;
96
97         case Twalk:
98                 n += BIT32SZ;
99                 n += BIT32SZ;
100                 n += BIT16SZ;
101                 for(i=0; i<f->nwname; i++)
102                         n += stringsz(f->wname[i]);
103                 break;
104
105         case Topen:
106                 n += BIT32SZ;
107                 n += BIT8SZ;
108                 break;
109
110         case Tcreate:
111                 n += BIT32SZ;
112                 n += stringsz(f->name);
113                 n += BIT32SZ;
114                 n += BIT8SZ;
115                 break;
116
117         case Tread:
118                 n += BIT32SZ;
119                 n += BIT64SZ;
120                 n += BIT32SZ;
121                 break;
122
123         case Twrite:
124                 n += BIT32SZ;
125                 n += BIT64SZ;
126                 n += BIT32SZ;
127                 n += f->count;
128                 break;
129
130         case Tclunk:
131         case Tremove:
132                 n += BIT32SZ;
133                 break;
134
135         case Tstat:
136                 n += BIT32SZ;
137                 break;
138
139         case Twstat:
140                 n += BIT32SZ;
141                 n += BIT16SZ;
142                 n += f->nstat;
143                 break;
144 /*
145  */
146
147         case Rversion:
148                 n += BIT32SZ;
149                 n += stringsz(f->version);
150                 break;
151
152         case Rerror:
153                 n += stringsz(f->ename);
154                 break;
155
156         case Rflush:
157                 break;
158
159         case Rauth:
160                 n += QIDSZ;
161                 break;
162
163         case Rattach:
164                 n += QIDSZ;
165                 break;
166
167         case Rwalk:
168                 n += BIT16SZ;
169                 n += f->nwqid*QIDSZ;
170                 break;
171
172         case Ropen:
173         case Rcreate:
174                 n += QIDSZ;
175                 n += BIT32SZ;
176                 break;
177
178         case Rread:
179                 n += BIT32SZ;
180                 n += f->count;
181                 break;
182
183         case Rwrite:
184                 n += BIT32SZ;
185                 break;
186
187         case Rclunk:
188                 break;
189
190         case Rremove:
191                 break;
192
193         case Rstat:
194                 n += BIT16SZ;
195                 n += f->nstat;
196                 break;
197
198         case Rwstat:
199                 break;
200         }
201         return n;
202 }
203
204 unsigned int
205 convS2M(struct fcall *f, uint8_t *ap, unsigned int nap)
206 {
207         uint8_t *p;
208         unsigned int i, size;
209
210         size = sizeS2M(f);
211         if(size == 0)
212                 return 0;
213         if(size > nap)
214                 return 0;
215
216         p = ( uint8_t *)ap;
217
218         PBIT32(p, size);
219         p += BIT32SZ;
220         PBIT8(p, f->type);
221         p += BIT8SZ;
222         PBIT16(p, f->tag);
223         p += BIT16SZ;
224
225         switch(f->type)
226         {
227         default:
228                 return 0;
229
230         case Tversion:
231                 PBIT32(p, f->msize);
232                 p += BIT32SZ;
233                 p = pstring(p, f->version);
234                 break;
235
236         case Tflush:
237                 PBIT16(p, f->oldtag);
238                 p += BIT16SZ;
239                 break;
240
241         case Tauth:
242                 PBIT32(p, f->afid);
243                 p += BIT32SZ;
244                 p  = pstring(p, f->uname);
245                 p  = pstring(p, f->aname);
246                 break;
247
248         case Tattach:
249                 PBIT32(p, f->fid);
250                 p += BIT32SZ;
251                 PBIT32(p, f->afid);
252                 p += BIT32SZ;
253                 p  = pstring(p, f->uname);
254                 p  = pstring(p, f->aname);
255                 break;
256
257         case Twalk:
258                 PBIT32(p, f->fid);
259                 p += BIT32SZ;
260                 PBIT32(p, f->newfid);
261                 p += BIT32SZ;
262                 PBIT16(p, f->nwname);
263                 p += BIT16SZ;
264                 if(f->nwname > MAXWELEM)
265                         return 0;
266                 for(i=0; i<f->nwname; i++)
267                         p = pstring(p, f->wname[i]);
268                 break;
269
270         case Topen:
271                 PBIT32(p, f->fid);
272                 p += BIT32SZ;
273                 PBIT8(p, f->mode);
274                 p += BIT8SZ;
275                 break;
276
277         case Tcreate:
278                 PBIT32(p, f->fid);
279                 p += BIT32SZ;
280                 p = pstring(p, f->name);
281                 PBIT32(p, f->perm);
282                 p += BIT32SZ;
283                 PBIT8(p, f->mode);
284                 p += BIT8SZ;
285                 break;
286
287         case Tread:
288                 PBIT32(p, f->fid);
289                 p += BIT32SZ;
290                 PBIT64(p, f->offset);
291                 p += BIT64SZ;
292                 PBIT32(p, f->count);
293                 p += BIT32SZ;
294                 break;
295
296         case Twrite:
297                 PBIT32(p, f->fid);
298                 p += BIT32SZ;
299                 PBIT64(p, f->offset);
300                 p += BIT64SZ;
301                 PBIT32(p, f->count);
302                 p += BIT32SZ;
303                 memmove(p, f->data, f->count);
304                 p += f->count;
305                 break;
306
307         case Tclunk:
308         case Tremove:
309                 PBIT32(p, f->fid);
310                 p += BIT32SZ;
311                 break;
312
313         case Tstat:
314                 PBIT32(p, f->fid);
315                 p += BIT32SZ;
316                 break;
317
318         case Twstat:
319                 PBIT32(p, f->fid);
320                 p += BIT32SZ;
321                 PBIT16(p, f->nstat);
322                 p += BIT16SZ;
323                 memmove(p, f->stat, f->nstat);
324                 p += f->nstat;
325                 break;
326 /*
327  */
328
329         case Rversion:
330                 PBIT32(p, f->msize);
331                 p += BIT32SZ;
332                 p = pstring(p, f->version);
333                 break;
334
335         case Rerror:
336                 p = pstring(p, f->ename);
337                 break;
338
339         case Rflush:
340                 break;
341
342         case Rauth:
343                 p = pqid(p, &f->aqid);
344                 break;
345
346         case Rattach:
347                 p = pqid(p, &f->qid);
348                 break;
349
350         case Rwalk:
351                 PBIT16(p, f->nwqid);
352                 p += BIT16SZ;
353                 if(f->nwqid > MAXWELEM)
354                         return 0;
355                 for(i=0; i<f->nwqid; i++)
356                         p = pqid(p, &f->wqid[i]);
357                 break;
358
359         case Ropen:
360         case Rcreate:
361                 p = pqid(p, &f->qid);
362                 PBIT32(p, f->iounit);
363                 p += BIT32SZ;
364                 break;
365
366         case Rread:
367                 PBIT32(p, f->count);
368                 p += BIT32SZ;
369                 memmove(p, f->data, f->count);
370                 p += f->count;
371                 break;
372
373         case Rwrite:
374                 PBIT32(p, f->count);
375                 p += BIT32SZ;
376                 break;
377
378         case Rclunk:
379                 break;
380
381         case Rremove:
382                 break;
383
384         case Rstat:
385                 PBIT16(p, f->nstat);
386                 p += BIT16SZ;
387                 memmove(p, f->stat, f->nstat);
388                 p += f->nstat;
389                 break;
390
391         case Rwstat:
392                 break;
393         }
394         if(size != p-ap)
395                 return 0;
396         return size;
397 }