aboutsummaryrefslogtreecommitdiff
path: root/src/libventi/zero.c
blob: 33353c7865eac7a21c7fb54e2be872f2c3a71724 (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
#include <u.h>
#include <libc.h>
#include <venti.h>

void
vtzeroextend(int type, uchar *buf, uint n, uint nn)
{
	uchar *p, *ep;

	switch(type&7) {
	case 0:
		memset(buf+n, 0, nn-n);
		break;
	default:
		p = buf + (n/VtScoreSize)*VtScoreSize;
		ep = buf + (nn/VtScoreSize)*VtScoreSize;
		while(p < ep) {
			memmove(p, vtzeroscore, VtScoreSize);
			p += VtScoreSize;
		}
		memset(p, 0, buf+nn-p);
		break;
	}
}

uint
vtzerotruncate(int type, uchar *buf, uint n)
{
	uchar *p;

	if(type == VtRootType){
		if(n < VtRootSize)
			return n;
		return VtRootSize;
	}

	switch(type&7){
	case 0:
		for(p = buf + n; p > buf; p--) {
			if(p[-1] != 0)
				break;
		}
		return p - buf;
	default:
		/* ignore slop at end of block */
		p = buf + (n/VtScoreSize)*VtScoreSize;

		while(p > buf) {
			if(memcmp(p - VtScoreSize, vtzeroscore, VtScoreSize) != 0)
				break;
			p -= VtScoreSize;
		}
		return p - buf;
	}
}