xref: /plan9/sys/src/libventi/root.c (revision 368c31ab13393dea083228fdd1c3445076f83a4b)
1 #include <u.h>
2 #include <libc.h>
3 #include <venti.h>
4 #include "cvt.h"
5 
6 static int
checksize(int n)7 checksize(int n)
8 {
9 	if(n < 256 || n > VtMaxLumpSize) {
10 		werrstr("bad block size");
11 		return -1;
12 	}
13 	return 0;
14 }
15 
16 void
vtrootpack(VtRoot * r,uchar * p)17 vtrootpack(VtRoot *r, uchar *p)
18 {
19 	uchar *op = p;
20 
21 	U16PUT(p, VtRootVersion);
22 	p += 2;
23 	memmove(p, r->name, sizeof(r->name));
24 	p += sizeof(r->name);
25 	memmove(p, r->type, sizeof(r->type));
26 	p += sizeof(r->type);
27 	memmove(p, r->score, VtScoreSize);
28 	p +=  VtScoreSize;
29 	U16PUT(p, r->blocksize);
30 	p += 2;
31 	memmove(p, r->prev, VtScoreSize);
32 	p += VtScoreSize;
33 
34 	assert(p-op == VtRootSize);
35 }
36 
37 int
vtrootunpack(VtRoot * r,uchar * p)38 vtrootunpack(VtRoot *r, uchar *p)
39 {
40 	uchar *op = p;
41 	uint vers;
42 	memset(r, 0, sizeof(*r));
43 
44 	vers = U16GET(p);
45 	if(vers != VtRootVersion) {
46 		werrstr("unknown root version");
47 		return -1;
48 	}
49 	p += 2;
50 	memmove(r->name, p, sizeof(r->name));
51 	r->name[sizeof(r->name)-1] = 0;
52 	p += sizeof(r->name);
53 	memmove(r->type, p, sizeof(r->type));
54 	r->type[sizeof(r->type)-1] = 0;
55 	p += sizeof(r->type);
56 	memmove(r->score, p, VtScoreSize);
57 	p +=  VtScoreSize;
58 	r->blocksize = U16GET(p);
59 	if(checksize(r->blocksize) < 0)
60 		return -1;
61 	p += 2;
62 	memmove(r->prev, p, VtScoreSize);
63 	p += VtScoreSize;
64 
65 	assert(p-op == VtRootSize);
66 	return 0;
67 }
68