plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

root.c (1542B)


      1 #include <u.h>
      2 #include <libc.h>
      3 #include <venti.h>
      4 #include "cvt.h"
      5 
      6 static int
      7 checksize(int n)
      8 {
      9 	if(n < 256) {
     10 		werrstr("bad block size");
     11 		return -1;
     12 	}
     13 	return 0;
     14 }
     15 
     16 extern int vttobig(ulong);
     17 
     18 void
     19 vtrootpack(VtRoot *r, uchar *p)
     20 {
     21 	uchar *op = p;
     22 	int vers, bsize;
     23 
     24 	vers = VtRootVersion;
     25 	bsize = r->blocksize;
     26 	if(bsize >= (1<<16)) {
     27 		vers |= _VtRootVersionBig;
     28 		bsize = vttobig(bsize);
     29 		if(bsize < 0)
     30 			sysfatal("invalid root blocksize: %#lx", r->blocksize);
     31 	}
     32 	U16PUT(p, vers);
     33 	p += 2;
     34 	memmove(p, r->name, sizeof(r->name));
     35 	p += sizeof(r->name);
     36 	memmove(p, r->type, sizeof(r->type));
     37 	p += sizeof(r->type);
     38 	memmove(p, r->score, VtScoreSize);
     39 	p +=  VtScoreSize;
     40 	U16PUT(p, bsize);
     41 	p += 2;
     42 	memmove(p, r->prev, VtScoreSize);
     43 	p += VtScoreSize;
     44 
     45 	assert(p-op == VtRootSize);
     46 }
     47 
     48 int
     49 vtrootunpack(VtRoot *r, uchar *p)
     50 {
     51 	uchar *op = p;
     52 	uint vers;
     53 	memset(r, 0, sizeof(*r));
     54 
     55 	vers = U16GET(p);
     56 	if((vers&~_VtRootVersionBig) != VtRootVersion) {
     57 		werrstr("unknown root version");
     58 		return -1;
     59 	}
     60 	p += 2;
     61 	memmove(r->name, p, sizeof(r->name));
     62 	r->name[sizeof(r->name)-1] = 0;
     63 	p += sizeof(r->name);
     64 	memmove(r->type, p, sizeof(r->type));
     65 	r->type[sizeof(r->type)-1] = 0;
     66 	p += sizeof(r->type);
     67 	memmove(r->score, p, VtScoreSize);
     68 	p +=  VtScoreSize;
     69 	r->blocksize = U16GET(p);
     70 	if(vers & _VtRootVersionBig)
     71 		r->blocksize = (r->blocksize >> 5) << (r->blocksize & 31);
     72 	if(checksize(r->blocksize) < 0)
     73 		return -1;
     74 	p += 2;
     75 	memmove(r->prev, p, VtScoreSize);
     76 	p += VtScoreSize;
     77 
     78 	assert(p-op == VtRootSize);
     79 	return 0;
     80 }