convS2M.c (5013B)
1 #include "u.h" 2 #include "lib.h" 3 #include "fcall.h" 4 5 static 6 uchar* 7 pstring(uchar *p, char *s) 8 { 9 uint n; 10 11 if(s == nil){ 12 PBIT16(p, 0); 13 p += BIT16SZ; 14 return p; 15 } 16 17 n = strlen(s); 18 PBIT16(p, n); 19 p += BIT16SZ; 20 memmove(p, s, n); 21 p += n; 22 return p; 23 } 24 25 static 26 uchar* 27 pqid(uchar *p, Qid *q) 28 { 29 PBIT8(p, q->type); 30 p += BIT8SZ; 31 PBIT32(p, q->vers); 32 p += BIT32SZ; 33 PBIT64(p, q->path); 34 p += BIT64SZ; 35 return p; 36 } 37 38 static 39 uint 40 stringsz(char *s) 41 { 42 if(s == nil) 43 return BIT16SZ; 44 45 return BIT16SZ+strlen(s); 46 } 47 48 uint 49 sizeS2M(Fcall *f) 50 { 51 uint n; 52 int i; 53 54 n = 0; 55 n += BIT32SZ; /* size */ 56 n += BIT8SZ; /* type */ 57 n += BIT16SZ; /* tag */ 58 59 switch(f->type) 60 { 61 default: 62 return 0; 63 64 case Tversion: 65 n += BIT32SZ; 66 n += stringsz(f->version); 67 break; 68 69 case Tflush: 70 n += BIT16SZ; 71 break; 72 73 case Tauth: 74 n += BIT32SZ; 75 n += stringsz(f->uname); 76 n += stringsz(f->aname); 77 break; 78 79 case Tattach: 80 n += BIT32SZ; 81 n += BIT32SZ; 82 n += stringsz(f->uname); 83 n += stringsz(f->aname); 84 break; 85 86 case Twalk: 87 n += BIT32SZ; 88 n += BIT32SZ; 89 n += BIT16SZ; 90 for(i=0; i<f->nwname; i++) 91 n += stringsz(f->wname[i]); 92 break; 93 94 case Topen: 95 n += BIT32SZ; 96 n += BIT8SZ; 97 break; 98 99 case Tcreate: 100 n += BIT32SZ; 101 n += stringsz(f->name); 102 n += BIT32SZ; 103 n += BIT8SZ; 104 break; 105 106 case Tread: 107 n += BIT32SZ; 108 n += BIT64SZ; 109 n += BIT32SZ; 110 break; 111 112 case Twrite: 113 n += BIT32SZ; 114 n += BIT64SZ; 115 n += BIT32SZ; 116 n += f->count; 117 break; 118 119 case Tclunk: 120 case Tremove: 121 n += BIT32SZ; 122 break; 123 124 case Tstat: 125 n += BIT32SZ; 126 break; 127 128 case Twstat: 129 n += BIT32SZ; 130 n += BIT16SZ; 131 n += f->nstat; 132 break; 133 /* 134 */ 135 136 case Rversion: 137 n += BIT32SZ; 138 n += stringsz(f->version); 139 break; 140 141 case Rerror: 142 n += stringsz(f->ename); 143 break; 144 145 case Rflush: 146 break; 147 148 case Rauth: 149 n += QIDSZ; 150 break; 151 152 case Rattach: 153 n += QIDSZ; 154 break; 155 156 case Rwalk: 157 n += BIT16SZ; 158 n += f->nwqid*QIDSZ; 159 break; 160 161 case Ropen: 162 case Rcreate: 163 n += QIDSZ; 164 n += BIT32SZ; 165 break; 166 167 case Rread: 168 n += BIT32SZ; 169 n += f->count; 170 break; 171 172 case Rwrite: 173 n += BIT32SZ; 174 break; 175 176 case Rclunk: 177 break; 178 179 case Rremove: 180 break; 181 182 case Rstat: 183 n += BIT16SZ; 184 n += f->nstat; 185 break; 186 187 case Rwstat: 188 break; 189 } 190 return n; 191 } 192 193 uint 194 convS2M(Fcall *f, uchar *ap, uint nap) 195 { 196 uchar *p; 197 uint i, size; 198 199 size = sizeS2M(f); 200 if(size == 0) 201 return 0; 202 if(size > nap) 203 return 0; 204 205 p = (uchar*)ap; 206 207 PBIT32(p, size); 208 p += BIT32SZ; 209 PBIT8(p, f->type); 210 p += BIT8SZ; 211 PBIT16(p, f->tag); 212 p += BIT16SZ; 213 214 switch(f->type) 215 { 216 default: 217 return 0; 218 219 case Tversion: 220 PBIT32(p, f->msize); 221 p += BIT32SZ; 222 p = pstring(p, f->version); 223 break; 224 225 case Tflush: 226 PBIT16(p, f->oldtag); 227 p += BIT16SZ; 228 break; 229 230 case Tauth: 231 PBIT32(p, f->afid); 232 p += BIT32SZ; 233 p = pstring(p, f->uname); 234 p = pstring(p, f->aname); 235 break; 236 237 case Tattach: 238 PBIT32(p, f->fid); 239 p += BIT32SZ; 240 PBIT32(p, f->afid); 241 p += BIT32SZ; 242 p = pstring(p, f->uname); 243 p = pstring(p, f->aname); 244 break; 245 246 case Twalk: 247 PBIT32(p, f->fid); 248 p += BIT32SZ; 249 PBIT32(p, f->newfid); 250 p += BIT32SZ; 251 PBIT16(p, f->nwname); 252 p += BIT16SZ; 253 if(f->nwname > MAXWELEM) 254 return 0; 255 for(i=0; i<f->nwname; i++) 256 p = pstring(p, f->wname[i]); 257 break; 258 259 case Topen: 260 PBIT32(p, f->fid); 261 p += BIT32SZ; 262 PBIT8(p, f->mode); 263 p += BIT8SZ; 264 break; 265 266 case Tcreate: 267 PBIT32(p, f->fid); 268 p += BIT32SZ; 269 p = pstring(p, f->name); 270 PBIT32(p, f->perm); 271 p += BIT32SZ; 272 PBIT8(p, f->mode); 273 p += BIT8SZ; 274 break; 275 276 case Tread: 277 PBIT32(p, f->fid); 278 p += BIT32SZ; 279 PBIT64(p, f->offset); 280 p += BIT64SZ; 281 PBIT32(p, f->count); 282 p += BIT32SZ; 283 break; 284 285 case Twrite: 286 PBIT32(p, f->fid); 287 p += BIT32SZ; 288 PBIT64(p, f->offset); 289 p += BIT64SZ; 290 PBIT32(p, f->count); 291 p += BIT32SZ; 292 memmove(p, f->data, f->count); 293 p += f->count; 294 break; 295 296 case Tclunk: 297 case Tremove: 298 PBIT32(p, f->fid); 299 p += BIT32SZ; 300 break; 301 302 case Tstat: 303 PBIT32(p, f->fid); 304 p += BIT32SZ; 305 break; 306 307 case Twstat: 308 PBIT32(p, f->fid); 309 p += BIT32SZ; 310 PBIT16(p, f->nstat); 311 p += BIT16SZ; 312 memmove(p, f->stat, f->nstat); 313 p += f->nstat; 314 break; 315 /* 316 */ 317 318 case Rversion: 319 PBIT32(p, f->msize); 320 p += BIT32SZ; 321 p = pstring(p, f->version); 322 break; 323 324 case Rerror: 325 p = pstring(p, f->ename); 326 break; 327 328 case Rflush: 329 break; 330 331 case Rauth: 332 p = pqid(p, &f->aqid); 333 break; 334 335 case Rattach: 336 p = pqid(p, &f->qid); 337 break; 338 339 case Rwalk: 340 PBIT16(p, f->nwqid); 341 p += BIT16SZ; 342 if(f->nwqid > MAXWELEM) 343 return 0; 344 for(i=0; i<f->nwqid; i++) 345 p = pqid(p, &f->wqid[i]); 346 break; 347 348 case Ropen: 349 case Rcreate: 350 p = pqid(p, &f->qid); 351 PBIT32(p, f->iounit); 352 p += BIT32SZ; 353 break; 354 355 case Rread: 356 PBIT32(p, f->count); 357 p += BIT32SZ; 358 memmove(p, f->data, f->count); 359 p += f->count; 360 break; 361 362 case Rwrite: 363 PBIT32(p, f->count); 364 p += BIT32SZ; 365 break; 366 367 case Rclunk: 368 break; 369 370 case Rremove: 371 break; 372 373 case Rstat: 374 PBIT16(p, f->nstat); 375 p += BIT16SZ; 376 memmove(p, f->stat, f->nstat); 377 p += f->nstat; 378 break; 379 380 case Rwstat: 381 break; 382 } 383 if(size != p-ap) 384 return 0; 385 return size; 386 }