aboutsummaryrefslogtreecommitdiff
path: root/src/libventi/root.c
blob: 2627ee81b240ddd5eb24a2c4bb7fb2e10cec2e7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <u.h>
#include <libc.h>
#include <venti.h>
#include "cvt.h"

static int
checksize(int n)
{
	if(n < 256) {
		werrstr("bad block size");
		return -1;
	}
	return 0;
}

extern int vttobig(ulong);

void
vtrootpack(VtRoot *r, uchar *p)
{
	uchar *op = p;
	int vers, bsize;

	vers = VtRootVersion;
	bsize = r->blocksize;
	if(bsize >= (1<<16)) {
		vers |= _VtRootVersionBig;
		bsize = vttobig(bsize);
		if(bsize < 0)
			sysfatal("invalid root blocksize: %#lx", r->blocksize);
	}
	U16PUT(p, vers);
	p += 2;
	memmove(p, r->name, sizeof(r->name));
	p += sizeof(r->name);
	memmove(p, r->type, sizeof(r->type));
	p += sizeof(r->type);
	memmove(p, r->score, VtScoreSize);
	p +=  VtScoreSize;
	U16PUT(p, bsize);
	p += 2;
	memmove(p, r->prev, VtScoreSize);
	p += VtScoreSize;

	assert(p-op == VtRootSize);
}

int
vtrootunpack(VtRoot *r, uchar *p)
{
	uchar *op = p;
	uint vers;
	memset(r, 0, sizeof(*r));

	vers = U16GET(p);
	if((vers&~_VtRootVersionBig) != VtRootVersion) {
		werrstr("unknown root version");
		return -1;
	}
	p += 2;
	memmove(r->name, p, sizeof(r->name));
	r->name[sizeof(r->name)-1] = 0;
	p += sizeof(r->name);
	memmove(r->type, p, sizeof(r->type));
	r->type[sizeof(r->type)-1] = 0;
	p += sizeof(r->type);
	memmove(r->score, p, VtScoreSize);
	p +=  VtScoreSize;
	r->blocksize = U16GET(p);
	if(vers & _VtRootVersionBig)
		r->blocksize = (r->blocksize >> 5) << (r->blocksize & 31);
	if(checksize(r->blocksize) < 0)
		return -1;
	p += 2;
	memmove(r->prev, p, VtScoreSize);
	p += VtScoreSize;

	assert(p-op == VtRootSize);
	return 0;
}