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