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