xref: /inferno-os/lib9/convM2D.c (revision 7ef44d652ae9e5e1f5b3465d73684e4a54de73c0)
1 #include	"lib9.h"
2 #include	"fcall.h"
3 
4 int
5 statcheck(uchar *buf, uint nbuf)
6 {
7 	uchar *ebuf;
8 	int i;
9 
10 	ebuf = buf + nbuf;
11 
12 	if(nbuf < STATFIXLEN || nbuf != BIT16SZ + GBIT16(buf))
13 		return -1;
14 
15 	buf += STATFIXLEN - 4 * BIT16SZ;
16 
17 	for(i = 0; i < 4; i++){
18 		if(buf + BIT16SZ > ebuf)
19 			return -1;
20 		buf += BIT16SZ + GBIT16(buf);
21 	}
22 
23 	if(buf != ebuf)
24 		return -1;
25 
26 	return 0;
27 }
28 
29 static char nullstring[] = "";
30 
31 uint
32 convM2D(uchar *buf, uint nbuf, Dir *d, char *strs)
33 {
34 	uchar *p, *ebuf;
35 	char *sv[4];
36 	int i, ns;
37 
38 	if(nbuf < STATFIXLEN)
39 		return 0;
40 
41 	p = buf;
42 	ebuf = buf + nbuf;
43 
44 	p += BIT16SZ;	/* ignore size */
45 	d->type = GBIT16(p);
46 	p += BIT16SZ;
47 	d->dev = GBIT32(p);
48 	p += BIT32SZ;
49 	d->qid.type = GBIT8(p);
50 	p += BIT8SZ;
51 	d->qid.vers = GBIT32(p);
52 	p += BIT32SZ;
53 	d->qid.path = GBIT64(p);
54 	p += BIT64SZ;
55 	d->mode = GBIT32(p);
56 	p += BIT32SZ;
57 	d->atime = GBIT32(p);
58 	p += BIT32SZ;
59 	d->mtime = GBIT32(p);
60 	p += BIT32SZ;
61 	d->length = GBIT64(p);
62 	p += BIT64SZ;
63 
64 	for(i = 0; i < 4; i++){
65 		if(p + BIT16SZ > ebuf)
66 			return 0;
67 		ns = GBIT16(p);
68 		p += BIT16SZ;
69 		if(p + ns > ebuf)
70 			return 0;
71 		if(strs){
72 			sv[i] = strs;
73 			memmove(strs, p, ns);
74 			strs += ns;
75 			*strs++ = '\0';
76 		}
77 		p += ns;
78 	}
79 
80 	if(strs){
81 		d->name = sv[0];
82 		d->uid = sv[1];
83 		d->gid = sv[2];
84 		d->muid = sv[3];
85 	}else{
86 		d->name = nullstring;
87 		d->uid = nullstring;
88 		d->gid = nullstring;
89 		d->muid = nullstring;
90 	}
91 
92 	return p - buf;
93 }
94