summaryrefslogtreecommitdiff
path: root/navit/data/mg/tree.c
blob: 077649e9689f6bd77cccba4593983e5d10b69d2c (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <stdio.h>
#include <string.h>
#include "debug.h"
#include "mg.h"

struct tree_hdr {
	unsigned int addr;
	unsigned int size;
	unsigned int low;
};

struct tree_hdr_h {
	unsigned int addr;
	unsigned int size;
};

struct tree_leaf_h {
	unsigned int lower;
	unsigned int higher;
	unsigned int match;
	unsigned int value;
};


struct tree_hdr_v {
	unsigned int count;
	unsigned int next;
	unsigned int unknown;
};

struct tree_leaf_v {
	unsigned char key;
	int value;
} __attribute__((packed));

static int
tree_search_h(struct file *file, unsigned int search)
{
	unsigned char *p=file->begin,*end;
	int last,i=0,value,lower;
	struct tree_hdr_h *thdr;
	struct tree_leaf_h *tleaf;

	dbg(1,"enter\n");
	while (i++ < 1000) {
		thdr=(struct tree_hdr_h *)p;
		p+=sizeof(*thdr);
		end=p+thdr->size;
		dbg(1,"@0x%x\n", p-file->begin);
		last=0;
		while (p < end) {
			tleaf=(struct tree_leaf_h *)p;
			p+=sizeof(*tleaf);
			dbg(1,"low:0x%x high:0x%x match:0x%x val:0x%x search:0x%x\n", tleaf->lower, tleaf->higher, tleaf->match, tleaf->value, search);
			value=tleaf->value;
			if (value == search)
				return tleaf->match;
			if (value > search) {
				dbg(1,"lower\n");
				lower=tleaf->lower;
				if (lower)
					last=lower;
				break;
			}
			last=tleaf->higher;
		}
		if (! last || last == -1)
			return 0;
		p=file->begin+last;
	}
	return 0;
}

static int
tree_search_v(struct file *file, int offset, int search)
{
	unsigned char *p=file->begin+offset;
	int i=0,count,next;
	struct tree_hdr_v *thdr;
	struct tree_leaf_v *tleaf;
	while (i++ < 1000) {
		thdr=(struct tree_hdr_v *)p;
		p+=sizeof(*thdr);
		count=L(thdr->count);
		dbg(1,"offset=0x%x count=0x%x\n", p-file->begin, count);
		while (count--) {
			tleaf=(struct tree_leaf_v *)p;
			p+=sizeof(*tleaf);
			dbg(1,"0x%x 0x%x\n", tleaf->key, search);
			if (tleaf->key == search)
				return L(tleaf->value);
		}
		next=L(thdr->next);
		if (! next)
			break;
		p=file->begin+next;
	}
	return 0;
}

int
tree_search_hv(char *dirname, char *filename, unsigned int search_h, unsigned int search_v, int *result)
{
	struct file *f_idx_h, *f_idx_v;
	char buffer[4096];
	int h,v;

	dbg(1,"enter(%s, %s, 0x%x, 0x%x, %p)\n",dirname, filename, search_h, search_v, result);
	sprintf(buffer, "%s/%s.h1", dirname, filename);
	f_idx_h=file_create_caseinsensitive(buffer);
	if (! f_idx_h)
		return 0;
	file_mmap(f_idx_h);	
	sprintf(buffer, "%s/%s.v1", dirname, filename);
	f_idx_v=file_create_caseinsensitive(buffer);
	dbg(1,"%p %p\n", f_idx_h, f_idx_v);
	if (! f_idx_v) {
		file_destroy(f_idx_h);
		return 0;
	}
	file_mmap(f_idx_v);
	if ((h=tree_search_h(f_idx_h, search_h))) {
		dbg(1,"h=0x%x\n", h);
		if ((v=tree_search_v(f_idx_v, h, search_v))) {
			dbg(1,"v=0x%x\n", v);
			*result=v;
			file_destroy(f_idx_v);
			file_destroy(f_idx_h);
			dbg(1,"return 1\n");
			return 1;
		}
	}
	file_destroy(f_idx_v);
	file_destroy(f_idx_h);
	dbg(1,"return 0\n");
	return 0;
}

static struct tree_search_node *
tree_search_enter(struct tree_search *ts, int offset)
{
	struct tree_search_node *tsn=&ts->nodes[++ts->curr_node];
	unsigned char *p;
	p=ts->f->begin+offset;
	tsn->hdr=(struct tree_hdr *)p;
	tsn->p=p+sizeof(struct tree_hdr);
	tsn->last=tsn->p;
	tsn->end=p+tsn->hdr->size;
	tsn->low=tsn->hdr->low;
	tsn->high=tsn->hdr->low;
	dbg(1,"pos 0x%x addr 0x%x size 0x%x low 0x%x end 0x%x\n", p-ts->f->begin, tsn->hdr->addr, tsn->hdr->size, tsn->hdr->low, tsn->end-ts->f->begin);
	return tsn;
}

int tree_search_next(struct tree_search *ts, unsigned char **p, int dir)
{
	struct tree_search_node *tsn=&ts->nodes[ts->curr_node];

	if (! *p) 
		*p=tsn->p;
	dbg(1,"next *p=%p dir=%d\n", *p, dir);
	dbg(1,"low1=0x%x high1=0x%x\n", tsn->low, tsn->high);
	if (dir <= 0) {
		dbg(1,"down 0x%x\n", tsn->low);
		if (tsn->low != 0xffffffff) {
			tsn=tree_search_enter(ts, tsn->low);
			*p=tsn->p;
			tsn->high=get_u32(p);
			ts->last_node=ts->curr_node;
			dbg(1,"saving last2 %d 0x%x\n", ts->curr_node, tsn->last-ts->f->begin);
			dbg(1,"high2=0x%x\n", tsn->high);
			return 0;
		}
		return -1;
	}
	tsn->low=tsn->high;
	tsn->last=*p;
	tsn->high=get_u32_unal(p);
	dbg(1,"saving last3 %d %p\n", ts->curr_node, tsn->last);
	if (*p < tsn->end)
		return (tsn->low == 0xffffffff ? 1 : 0);
	dbg(1,"end reached high=0x%x\n",tsn->high);
	if (tsn->low != 0xffffffff) {
		dbg(1,"low 0x%x\n", tsn->low);
		tsn=tree_search_enter(ts, tsn->low);
		*p=tsn->p;
		tsn->high=get_u32_unal(p);
		ts->last_node=ts->curr_node;
		dbg(1,"saving last4 %d 0x%x\n", ts->curr_node, tsn->last-ts->f->begin);
		dbg(1,"high4=0x%x\n", tsn->high);
		return 0;
	}
	return -1;
}

int tree_search_next_lin(struct tree_search *ts, unsigned char **p)
{
	struct tree_search_node *tsn=&ts->nodes[ts->curr_node];
	int high;
	
	dbg(1,"pos=%d 0x%x\n", ts->curr_node, *p-ts->f->begin);
	if (*p)
		ts->nodes[ts->last_node].last=*p;
	*p=tsn->last;
	for (;;) {
		high=get_u32_unal(p);
		if (*p < tsn->end) {
			ts->last_node=ts->curr_node;
			while (high != 0xffffffff) {
				tsn=tree_search_enter(ts, high);
				dbg(1,"reload %d\n",ts->curr_node);
				high=tsn->low;
			}
			return 1;
		}
		dbg(1,"eon %d 0x%x 0x%x\n", ts->curr_node, *p-ts->f->begin, tsn->end-ts->f->begin);
		if (! ts->curr_node)
			break;
		ts->curr_node--;
		tsn=&ts->nodes[ts->curr_node];
		*p=tsn->last;
	}

	return 0;
}

void
tree_search_init(char *dirname, char *filename, struct tree_search *ts, int offset)
{
	char buffer[4096];
	sprintf(buffer, "%s/%s", dirname, filename);
	ts->f=file_create_caseinsensitive(buffer);
	ts->curr_node=-1;
	if (ts->f) {
		file_mmap(ts->f);
		tree_search_enter(ts, offset);
	}
}

void
tree_search_free(struct tree_search *ts)
{
	file_destroy(ts->f);
}