conv* functions
[akaros.git] / kern / src / ns / convM2D.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 int
17 statcheck(uint8_t *buf, unsigned int nbuf)
18 {
19         uint8_t *ebuf;
20         int i;
21
22         ebuf = buf + nbuf;
23
24         if(nbuf < STATFIXLEN || nbuf != BIT16SZ + GBIT16(buf))
25                 return -1;
26
27         buf += STATFIXLEN - 4 * BIT16SZ;
28
29         for(i = 0; i < 4; i++){
30                 if(buf + BIT16SZ > ebuf)
31                         return -1;
32                 buf += BIT16SZ + GBIT16(buf);
33         }
34
35         if(buf != ebuf)
36                 return -1;
37
38         return 0;
39 }
40
41 static char nullstring[] = "";
42
43 unsigned int
44 convM2D(uint8_t *buf, unsigned int nbuf, struct dir *d, char *strs)
45 {
46         uint8_t *p, *ebuf;
47         char *sv[4];
48         int i, ns;
49
50         if(nbuf < STATFIXLEN)
51                 return 0; 
52
53         p = buf;
54         ebuf = buf + nbuf;
55
56         p += BIT16SZ;   /* ignore size */
57         d->type = GBIT16(p);
58         p += BIT16SZ;
59         d->dev = GBIT32(p);
60         p += BIT32SZ;
61         d->qid.type = GBIT8(p);
62         p += BIT8SZ;
63         d->qid.vers = GBIT32(p);
64         p += BIT32SZ;
65         d->qid.path = GBIT64(p);
66         p += BIT64SZ;
67         d->mode = GBIT32(p);
68         p += BIT32SZ;
69         d->atime = GBIT32(p);
70         p += BIT32SZ;
71         d->mtime = GBIT32(p);
72         p += BIT32SZ;
73         d->length = GBIT64(p);
74         p += BIT64SZ;
75
76         for(i = 0; i < 4; i++){
77                 if(p + BIT16SZ > ebuf)
78                         return 0;
79                 ns = GBIT16(p);
80                 p += BIT16SZ;
81                 if(p + ns > ebuf)
82                         return 0;
83                 if(strs){
84                         sv[i] = strs;
85                         memmove(strs, p, ns);
86                         strs += ns;
87                         *strs++ = '\0';
88                 }
89                 p += ns;
90         }
91
92         if(strs){
93                 d->name = sv[0];
94                 d->uid = sv[1];
95                 d->gid = sv[2];
96                 d->muid = sv[3];
97         }else{
98                 d->name = nullstring;
99                 d->uid = nullstring;
100                 d->gid = nullstring;
101                 d->muid = nullstring;
102         }
103         
104         return p - buf;
105 }