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