Blob
1 #include <u.h>2 #include <libc.h>3 #include <fcall.h>5 uint6 sizeD2M(Dir *d)7 {8 char *sv[5];9 int i, ns, nstr, fixlen;11 sv[0] = d->name;12 sv[1] = d->uid;13 sv[2] = d->gid;14 sv[3] = d->muid;16 fixlen = STATFIXLEN;17 nstr = 4;19 ns = 0;20 for(i = 0; i < nstr; i++)21 if(sv[i])22 ns += strlen(sv[i]);24 return fixlen + ns;25 }27 uint28 convD2M(Dir *d, uchar *buf, uint nbuf)29 {30 uchar *p, *ebuf;31 char *sv[5];32 int i, ns, nsv[5], ss, nstr, fixlen;34 if(nbuf < BIT16SZ)35 return 0;37 p = buf;38 ebuf = buf + nbuf;40 sv[0] = d->name;41 sv[1] = d->uid;42 sv[2] = d->gid;43 sv[3] = d->muid;45 fixlen = STATFIXLEN;46 nstr = 4;48 ns = 0;49 for(i = 0; i < nstr; i++){50 if(sv[i])51 nsv[i] = strlen(sv[i]);52 else53 nsv[i] = 0;54 ns += nsv[i];55 }57 ss = fixlen + ns;59 /* set size befor erroring, so user can know how much is needed */60 /* note that length excludes count field itself */61 PBIT16(p, ss-BIT16SZ);62 p += BIT16SZ;64 if(ss > nbuf)65 return BIT16SZ;67 PBIT16(p, d->type);68 p += BIT16SZ;69 PBIT32(p, d->dev);70 p += BIT32SZ;71 PBIT8(p, d->qid.type);72 p += BIT8SZ;73 PBIT32(p, d->qid.vers);74 p += BIT32SZ;75 PBIT64(p, d->qid.path);76 p += BIT64SZ;77 PBIT32(p, d->mode);78 p += BIT32SZ;79 PBIT32(p, d->atime);80 p += BIT32SZ;81 PBIT32(p, d->mtime);82 p += BIT32SZ;83 PBIT64(p, d->length);84 p += BIT64SZ;86 for(i = 0; i < nstr; i++){87 ns = nsv[i];88 if(p + ns + BIT16SZ > ebuf)89 return 0;90 PBIT16(p, ns);91 p += BIT16SZ;92 if(ns)93 memmove(p, sv[i], ns);94 p += ns;95 }97 if(ss != p - buf)98 return 0;100 return p - buf;101 }