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