aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/vac/vtread.c
blob: 8550279f4c88e7541015e2c553fc87d0db7e1f84 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "stdinc.h"
#include <bio.h>

typedef struct Source Source;

struct Source
{
	ulong gen;
	int psize;
	int dsize;
	int dir;
	int active;
	int depth;
	uvlong size;
	uchar score[VtScoreSize];
	int reserved;
};

int bsize;
Biobuf *bout;
VtRootLump root;
int ver;
int cmp;
int all;
int find;
uchar fscore[VtScoreSize];
int dirSize;
void (*parse)(Source*, uchar*);
VtSession *z;

int vtGetUint16(uchar *p);
ulong vtGetUint32(uchar *p);
uvlong vtGetUint48(uchar *p);
void usage(void);
int parseScore(uchar *score, char *buf, int n);
void readRoot(VtRootLump*, uchar *score, char *file);
void parse1(Source*, uchar*);
void parse2(Source*, uchar*);
int dumpDir(Source*, int indent);

void
main(int argc, char *argv[])
{
	char *host = nil;
	uchar score[VtScoreSize];
	uchar buf[VtMaxLumpSize];
	int type;
	int n;
	
	type = VtDataType;

	ARGBEGIN{
	case 't':
		type = atoi(ARGF());
		break;
	}ARGEND

	vtAttach();

	bout = vtMemAllocZ(sizeof(Biobuf));
	Binit(bout, 1, OWRITE);

	if(argc != 1)
		usage();

	vtAttach();

	fmtinstall('V', vtScoreFmt);
	fmtinstall('R', vtErrFmt);

	z = vtDial(host);
	if(z == nil)
		vtFatal("could not connect to server: %s", vtGetError());

	if(!vtConnect(z, 0))
		sysfatal("vtConnect: %r");

	if(!parseScore(score, argv[0], strlen(argv[0])))
		vtFatal("could not parse score: %s", vtGetError());

	n = vtRead(z, score, type, buf, VtMaxLumpSize);
	if(n < 0)
		vtFatal("could not read block: %s", vtGetError());
	Bwrite(bout, buf, n);

	Bterm(bout);

	vtClose(z);
	vtDetach();
	exits(0);
}

void
usage(void)
{
	fprint(2, "%s: -t type score\n", argv0);
	exits("usage");
}

int
parseScore(uchar *score, char *buf, int n)
{
	int i, c;

	memset(score, 0, VtScoreSize);

	if(n < VtScoreSize*2)
		return 0;
	for(i=0; i<VtScoreSize*2; i++) {
		if(buf[i] >= '0' && buf[i] <= '9')
			c = buf[i] - '0';
		else if(buf[i] >= 'a' && buf[i] <= 'f')
			c = buf[i] - 'a' + 10;
		else if(buf[i] >= 'A' && buf[i] <= 'F')
			c = buf[i] - 'A' + 10;
		else {
			return 0;
		}

		if((i & 1) == 0)
			c <<= 4;
	
		score[i>>1] |= c;
	}
	return 1;
}