9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

convS2M.c (5756B)


      1 #include	<u.h>
      2 #include	<libc.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 sizeS2Mu(Fcall *f, int dotu)
     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 		if(dotu)
     78 			n += BIT32SZ;
     79 		break;
     80 
     81 	case Tattach:
     82 		n += BIT32SZ;
     83 		n += BIT32SZ;
     84 		n += stringsz(f->uname);
     85 		n += stringsz(f->aname);
     86 		if(dotu)
     87 			n += BIT32SZ;
     88 		break;
     89 
     90 	case Twalk:
     91 		n += BIT32SZ;
     92 		n += BIT32SZ;
     93 		n += BIT16SZ;
     94 		for(i=0; i<f->nwname; i++)
     95 			n += stringsz(f->wname[i]);
     96 		break;
     97 
     98 	case Topen:
     99 	case Topenfd:
    100 		n += BIT32SZ;
    101 		n += BIT8SZ;
    102 		break;
    103 
    104 	case Tcreate:
    105 		n += BIT32SZ;
    106 		n += stringsz(f->name);
    107 		n += BIT32SZ;
    108 		n += BIT8SZ;
    109 		if(dotu)
    110 			n += stringsz(f->extension);
    111 		break;
    112 
    113 	case Tread:
    114 		n += BIT32SZ;
    115 		n += BIT64SZ;
    116 		n += BIT32SZ;
    117 		break;
    118 
    119 	case Twrite:
    120 		n += BIT32SZ;
    121 		n += BIT64SZ;
    122 		n += BIT32SZ;
    123 		n += f->count;
    124 		break;
    125 
    126 	case Tclunk:
    127 	case Tremove:
    128 		n += BIT32SZ;
    129 		break;
    130 
    131 	case Tstat:
    132 		n += BIT32SZ;
    133 		break;
    134 
    135 	case Twstat:
    136 		n += BIT32SZ;
    137 		n += BIT16SZ;
    138 		n += f->nstat;
    139 		break;
    140 /*
    141  */
    142 
    143 	case Rversion:
    144 		n += BIT32SZ;
    145 		n += stringsz(f->version);
    146 		break;
    147 
    148 	case Rerror:
    149 		n += stringsz(f->ename);
    150 		if(dotu)
    151 			n += BIT32SZ;
    152 		break;
    153 
    154 	case Rflush:
    155 		break;
    156 
    157 	case Rauth:
    158 		n += QIDSZ;
    159 		break;
    160 
    161 	case Rattach:
    162 		n += QIDSZ;
    163 		break;
    164 
    165 	case Rwalk:
    166 		n += BIT16SZ;
    167 		n += f->nwqid*QIDSZ;
    168 		break;
    169 
    170 	case Ropen:
    171 	case Rcreate:
    172 		n += QIDSZ;
    173 		n += BIT32SZ;
    174 		break;
    175 
    176 	case Ropenfd:
    177 		n += QIDSZ;
    178 		n += BIT32SZ;
    179 		n += BIT32SZ;
    180 		break;
    181 
    182 	case Rread:
    183 		n += BIT32SZ;
    184 		n += f->count;
    185 		break;
    186 
    187 	case Rwrite:
    188 		n += BIT32SZ;
    189 		break;
    190 
    191 	case Rclunk:
    192 		break;
    193 
    194 	case Rremove:
    195 		break;
    196 
    197 	case Rstat:
    198 		n += BIT16SZ;
    199 		n += f->nstat;
    200 		break;
    201 
    202 	case Rwstat:
    203 		break;
    204 	}
    205 	return n;
    206 }
    207 
    208 uint
    209 sizeS2M(Fcall *f)
    210 {
    211 	return sizeS2Mu(f, 0);
    212 }
    213 
    214 uint
    215 convS2Mu(Fcall *f, uchar *ap, uint nap, int dotu)
    216 {
    217 	uchar *p;
    218 	uint i, size;
    219 
    220 	size = sizeS2Mu(f, dotu);
    221 	if(size == 0)
    222 		return 0;
    223 	if(size > nap)
    224 		return 0;
    225 
    226 	p = (uchar*)ap;
    227 
    228 	PBIT32(p, size);
    229 	p += BIT32SZ;
    230 	PBIT8(p, f->type);
    231 	p += BIT8SZ;
    232 	PBIT16(p, f->tag);
    233 	p += BIT16SZ;
    234 
    235 	switch(f->type)
    236 	{
    237 	default:
    238 		return 0;
    239 
    240 	case Tversion:
    241 		PBIT32(p, f->msize);
    242 		p += BIT32SZ;
    243 		p = pstring(p, f->version);
    244 		break;
    245 
    246 	case Tflush:
    247 		PBIT16(p, f->oldtag);
    248 		p += BIT16SZ;
    249 		break;
    250 
    251 	case Tauth:
    252 		PBIT32(p, f->afid);
    253 		p += BIT32SZ;
    254 		p  = pstring(p, f->uname);
    255 		p  = pstring(p, f->aname);
    256 		if(dotu){
    257 			f->uidnum = NOUID;
    258 			PBIT32(p, f->uidnum);
    259 			p += BIT32SZ;
    260 		}
    261 		break;
    262 
    263 	case Tattach:
    264 		PBIT32(p, f->fid);
    265 		p += BIT32SZ;
    266 		PBIT32(p, f->afid);
    267 		p += BIT32SZ;
    268 		p  = pstring(p, f->uname);
    269 		p  = pstring(p, f->aname);
    270 		if(dotu){
    271 			f->uidnum = NOUID;
    272 			PBIT32(p, f->uidnum);
    273 			p += BIT32SZ;
    274 		}
    275 		break;
    276 
    277 	case Twalk:
    278 		PBIT32(p, f->fid);
    279 		p += BIT32SZ;
    280 		PBIT32(p, f->newfid);
    281 		p += BIT32SZ;
    282 		PBIT16(p, f->nwname);
    283 		p += BIT16SZ;
    284 		if(f->nwname > MAXWELEM)
    285 			return 0;
    286 		for(i=0; i<f->nwname; i++)
    287 			p = pstring(p, f->wname[i]);
    288 		break;
    289 
    290 	case Topen:
    291 	case Topenfd:
    292 		PBIT32(p, f->fid);
    293 		p += BIT32SZ;
    294 		PBIT8(p, f->mode);
    295 		p += BIT8SZ;
    296 		break;
    297 
    298 	case Tcreate:
    299 		PBIT32(p, f->fid);
    300 		p += BIT32SZ;
    301 		p = pstring(p, f->name);
    302 		PBIT32(p, f->perm);
    303 		p += BIT32SZ;
    304 		PBIT8(p, f->mode);
    305 		p += BIT8SZ;
    306 		if(dotu)
    307 			p = pstring(p, f->extension);
    308 		break;
    309 
    310 	case Tread:
    311 		PBIT32(p, f->fid);
    312 		p += BIT32SZ;
    313 		PBIT64(p, f->offset);
    314 		p += BIT64SZ;
    315 		PBIT32(p, f->count);
    316 		p += BIT32SZ;
    317 		break;
    318 
    319 	case Twrite:
    320 		PBIT32(p, f->fid);
    321 		p += BIT32SZ;
    322 		PBIT64(p, f->offset);
    323 		p += BIT64SZ;
    324 		PBIT32(p, f->count);
    325 		p += BIT32SZ;
    326 		memmove(p, f->data, f->count);
    327 		p += f->count;
    328 		break;
    329 
    330 	case Tclunk:
    331 	case Tremove:
    332 		PBIT32(p, f->fid);
    333 		p += BIT32SZ;
    334 		break;
    335 
    336 	case Tstat:
    337 		PBIT32(p, f->fid);
    338 		p += BIT32SZ;
    339 		break;
    340 
    341 	case Twstat:
    342 		PBIT32(p, f->fid);
    343 		p += BIT32SZ;
    344 		PBIT16(p, f->nstat);
    345 		p += BIT16SZ;
    346 		memmove(p, f->stat, f->nstat);
    347 		p += f->nstat;
    348 		break;
    349 /*
    350  */
    351 
    352 	case Rversion:
    353 		PBIT32(p, f->msize);
    354 		p += BIT32SZ;
    355 		p = pstring(p, f->version);
    356 		break;
    357 
    358 	case Rerror:
    359 		p = pstring(p, f->ename);
    360 		if(dotu){
    361 			PBIT32(p, f->errornum);
    362 			p += BIT32SZ;
    363 		}
    364 		break;
    365 
    366 	case Rflush:
    367 		break;
    368 
    369 	case Rauth:
    370 		p = pqid(p, &f->aqid);
    371 		break;
    372 
    373 	case Rattach:
    374 		p = pqid(p, &f->qid);
    375 		break;
    376 
    377 	case Rwalk:
    378 		PBIT16(p, f->nwqid);
    379 		p += BIT16SZ;
    380 		if(f->nwqid > MAXWELEM)
    381 			return 0;
    382 		for(i=0; i<f->nwqid; i++)
    383 			p = pqid(p, &f->wqid[i]);
    384 		break;
    385 
    386 	case Ropen:
    387 	case Rcreate:
    388 	case Ropenfd:
    389 		p = pqid(p, &f->qid);
    390 		PBIT32(p, f->iounit);
    391 		p += BIT32SZ;
    392 		if(f->type == Ropenfd){
    393 			PBIT32(p, f->unixfd);
    394 			p += BIT32SZ;
    395 		}
    396 		break;
    397 
    398 	case Rread:
    399 		PBIT32(p, f->count);
    400 		p += BIT32SZ;
    401 		memmove(p, f->data, f->count);
    402 		p += f->count;
    403 		break;
    404 
    405 	case Rwrite:
    406 		PBIT32(p, f->count);
    407 		p += BIT32SZ;
    408 		break;
    409 
    410 	case Rclunk:
    411 		break;
    412 
    413 	case Rremove:
    414 		break;
    415 
    416 	case Rstat:
    417 		PBIT16(p, f->nstat);
    418 		p += BIT16SZ;
    419 		memmove(p, f->stat, f->nstat);
    420 		p += f->nstat;
    421 		break;
    422 
    423 	case Rwstat:
    424 		break;
    425 	}
    426 	if(size != p-ap)
    427 		return 0;
    428 	return size;
    429 }
    430 
    431 uint
    432 convS2M(Fcall *f, uchar *ap, uint nap)
    433 {
    434 	return convS2Mu(f, ap, nap, 0);
    435 }