xref: /plan9/sys/src/ape/lib/ap/plan9/convM2D.c (revision 9a747e4fd48b9f4522c70c07e8f882a15030f964)
1 #include "lib.h"
2 #include <string.h>
3 #include "sys9.h"
4 #include "dir.h"
5 #define nil ((void*)0)
6 
7 static char nullstring[] = "";
8 
9 uint
_convM2D(uchar * buf,uint nbuf,Dir * d,char * strs)10 _convM2D(uchar *buf, uint nbuf, Dir *d, char *strs)
11 {
12 	uchar *p, *ebuf;
13 	char *sv[4];
14 	int i, ns, nsv[4];
15 
16 	p = buf;
17 	ebuf = buf + nbuf;
18 
19 	p += BIT16SZ;	/* ignore size */
20 	d->type = GBIT16(p);
21 	p += BIT16SZ;
22 	d->dev = GBIT32(p);
23 	p += BIT32SZ;
24 	d->qid.type = GBIT8(p);
25 	p += BIT8SZ;
26 	d->qid.vers = GBIT32(p);
27 	p += BIT32SZ;
28 	d->qid.path = GBIT64(p);
29 	p += BIT64SZ;
30 	d->mode = GBIT32(p);
31 	p += BIT32SZ;
32 	d->atime = GBIT32(p);
33 	p += BIT32SZ;
34 	d->mtime = GBIT32(p);
35 	p += BIT32SZ;
36 	d->length = GBIT64(p);
37 	p += BIT64SZ;
38 
39 	d->name = nil;
40 	d->uid = nil;
41 	d->gid = nil;
42 	d->muid = nil;
43 
44 	for(i = 0; i < 4; i++){
45 		if(p + BIT16SZ > ebuf)
46 			return 0;
47 		ns = GBIT16(p);
48 		p += BIT16SZ;
49 		if(p + ns > ebuf)
50 			return 0;
51 		if(strs){
52 			nsv[i] = ns;
53 			sv[i] = strs;
54 			memmove(strs, p, ns);
55 			strs += ns;
56 			*strs++ = '\0';
57 		}
58 		p += ns;
59 	}
60 
61 	if(strs){
62 		d->name = sv[0];
63 		d->uid = sv[1];
64 		d->gid = sv[2];
65 		d->muid = sv[3];
66 	}else{
67 		d->name = nullstring;
68 		d->uid = nullstring;
69 		d->gid = nullstring;
70 		d->muid = nullstring;
71 	}
72 
73 	return p - buf;
74 }
75