xref: /plan9/sys/src/liboventi/zero.c (revision 368c31ab13393dea083228fdd1c3445076f83a4b)
1 #include <u.h>
2 #include <libc.h>
3 #include <oventi.h>
4 
5 /* score of a zero length block */
6 uchar	vtZeroScore[VtScoreSize] = {
7 	0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55,
8 	0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09
9 };
10 
11 
12 int
vtZeroExtend(int type,uchar * buf,int n,int nn)13 vtZeroExtend(int type, uchar *buf, int n, int nn)
14 {
15 	uchar *p, *ep;
16 
17 	switch(type) {
18 	default:
19 		memset(buf+n, 0, nn-n);
20 		break;
21 	case VtPointerType0:
22 	case VtPointerType1:
23 	case VtPointerType2:
24 	case VtPointerType3:
25 	case VtPointerType4:
26 	case VtPointerType5:
27 	case VtPointerType6:
28 	case VtPointerType7:
29 	case VtPointerType8:
30 	case VtPointerType9:
31 		p = buf + (n/VtScoreSize)*VtScoreSize;
32 		ep = buf + (nn/VtScoreSize)*VtScoreSize;
33 		while(p < ep) {
34 			memmove(p, vtZeroScore, VtScoreSize);
35 			p += VtScoreSize;
36 		}
37 		memset(p, 0, buf+nn-p);
38 		break;
39 	}
40 	return 1;
41 }
42 
43 int
vtZeroTruncate(int type,uchar * buf,int n)44 vtZeroTruncate(int type, uchar *buf, int n)
45 {
46 	uchar *p;
47 
48 	switch(type) {
49 	default:
50 		for(p = buf + n; p > buf; p--) {
51 			if(p[-1] != 0)
52 				break;
53 		}
54 		return p - buf;
55 	case VtRootType:
56 		if(n < VtRootSize)
57 			return n;
58 		return VtRootSize;
59 	case VtPointerType0:
60 	case VtPointerType1:
61 	case VtPointerType2:
62 	case VtPointerType3:
63 	case VtPointerType4:
64 	case VtPointerType5:
65 	case VtPointerType6:
66 	case VtPointerType7:
67 	case VtPointerType8:
68 	case VtPointerType9:
69 		/* ignore slop at end of block */
70 		p = buf + (n/VtScoreSize)*VtScoreSize;
71 
72 		while(p > buf) {
73 			if(memcmp(p - VtScoreSize, vtZeroScore, VtScoreSize) != 0)
74 				break;
75 			p -= VtScoreSize;
76 		}
77 		return p - buf;
78 	}
79 }
80