forked from vandry/mairix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
reader.c
306 lines (276 loc) · 8.83 KB
/
reader.c
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
mairix - message index builder and finder for maildir folders.
**********************************************************************
* Copyright (C) Richard P. Curnow 2002,2003,2004,2005
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
**********************************************************************
*/
/* Database reader */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <sys/mman.h>
#include "reader.h"
#include "memmac.h"
#include "mairix.h"
void read_db_int_list_reader_init(
struct int_list_reader *ilr,
const struct read_db *db,
unsigned int start_offset
) {
ilr->accumulator = 0;
ilr->pos = ((unsigned char *)db->data) + start_offset;
ilr->end = ((unsigned char *)db->data) + db->len;
}
void matches_int_list_reader_init(
struct int_list_reader *ilr, const struct matches *m
) {
ilr->accumulator = 0;
ilr->pos = m->msginfo;
ilr->end = m->msginfo + m->n;
}
int int_list_reader_read(struct int_list_reader *ilr, int *result) {
unsigned char x0, x1, x2, x3;
unsigned int incr;
if (ilr->pos >= ilr->end) return 0;
x0 = *ilr->pos;
if (x0 == 0xff) {
return 0;
} else if ((x0 & 0xc0) == 0xc0) {
/* 4 byte encoding */
if ((ilr->pos + 4) > ilr->end) return 0;
x1 = ilr->pos[1];
x2 = ilr->pos[2];
x3 = ilr->pos[3];
incr = ((x0 & 0x3f) << 24) + (x1 << 16) + (x2 << 8) + x3;
ilr->pos += 4;
} else if (x0 & 0x80) {
/* 2 byte encoding */
if ((ilr->pos + 2) > ilr->end) return 0;
x1 = ilr->pos[1];
incr = ((x0 & 0x7f) << 8) + x1;
ilr->pos += 2;
} else {
/* Single byte encoding */
incr = x0;
ilr->pos++;
}
ilr->accumulator += incr;
*result = ilr->accumulator;
return 1;
}
void int_list_reader_copy(struct int_list_reader *ilr, struct matches *out) {
const unsigned char *base = ilr->pos;
int size, index;
while (int_list_reader_read(ilr, &index));
size = ilr->pos - base;
out->n = size;
out->highest = index;
/* Allow a bit of headroom for adding more entries later */
size += size / 2;
if (size < 16) size = 16;
out->max = size;
out->msginfo = new_array(unsigned char, size);
memcpy(out->msginfo, base, out->n);
}
const char *get_db_token(const struct read_db *db, unsigned int token_offset) {
const char *token = db->data + token_offset;
const char *db_end = db->data + db->len;
const char *p = token;
while (p < db_end) {
if ((*(p++)) == 0) return token;
}
return NULL;
}
static int read_toktable_db(char *data, int len, struct toktable_db *toktable, int start, unsigned int *uidata)/*{{{*/
{
char *file_end = data + len;
toktable->n = uidata[start];
toktable->tok_offsets = uidata + uidata[start+1];
if (((char *)(&(toktable->tok_offsets[toktable->n]))) > file_end) {
return 0;
}
toktable->enc_offsets = uidata + uidata[start+2];
if (((char *)(&(toktable->enc_offsets[toktable->n]))) > file_end) {
return 0;
}
return 1;
}
/*}}}*/
static int read_toktable2_db(char *data, int len, struct toktable2_db *toktable, int start, unsigned int *uidata)/*{{{*/
{
char *file_end = data + len;
toktable->n = uidata[start];
toktable->tok_offsets = uidata + uidata[start+1];
if (((char *)(&(toktable->tok_offsets[toktable->n]))) > file_end) {
return 0;
}
toktable->enc0_offsets = uidata + uidata[start+2];
if (((char *)(&(toktable->enc0_offsets[toktable->n]))) > file_end) {
return 0;
}
toktable->enc1_offsets = uidata + uidata[start+3];
if (((char *)(&(toktable->enc1_offsets[toktable->n]))) > file_end) {
return 0;
}
return 1;
}
/*}}}*/
struct read_db *open_db(char *filename)/*{{{*/
{
int fd, len;
char *data;
struct stat sb;
struct read_db *result;
unsigned int *uidata;
unsigned char *ucdata;
size_t uioffset_max; /* ui offset that lies beyond bounds */
fd = open(filename, O_RDONLY);
if (fd < 0) {
report_error("open", filename);
unlock_and_exit (2);
}
if (fstat(fd, &sb) < 0) {
report_error("stat", filename);
unlock_and_exit(2);
}
len = sb.st_size;
if (len < UC_HEADER_LEN) {
fprintf(stderr, "Database file %s is too short, possibly corrupted (actual size %d, minimum size %d). Please rebuild.\n", filename, len, UC_HEADER_LEN);
unlock_and_exit(2);
}
data = (char *) mmap(0, len, PROT_READ, MAP_SHARED, fd, 0);
if (data == MAP_FAILED) {
report_error("reader:mmap", filename);
unlock_and_exit(2);
}
if (!data) {
/* Empty file opened => database corrupt for sure */
if (close(fd) < 0) {
report_error("close", filename);
unlock_and_exit(2);
}
return NULL;
}
if (close(fd) < 0) {
report_error("close", filename);
unlock_and_exit(2);
}
result = new(struct read_db);
uidata = (unsigned int *) data; /* alignment is assured */
ucdata = (unsigned char *) data;
uioffset_max = len / sizeof(unsigned int);
result->len = len;
result->data = data;
/*{{{ Magic number check */
if (ucdata[0] == HEADER_MAGIC0 ||
ucdata[1] == HEADER_MAGIC1 ||
ucdata[2] == HEADER_MAGIC2) {
if (ucdata[3] != HEADER_MAGIC3) {
fprintf(stderr, "Another version of this program produced the existing database! Please rebuild.\n");
unlock_and_exit(2);
}
} else {
fprintf(stderr, "The existing database wasn't produced by this program! Please rebuild.\n");
unlock_and_exit(2);
}
/*}}}*/
/* {{{ Endianness check */
if (uidata[UI_ENDIAN] == 0x11223344) {
fprintf(stderr, "The endianness of the database is reversed for this machine\n");
unlock_and_exit(2);
} else if (uidata[UI_ENDIAN] != 0x44332211) {
fprintf(stderr, "The endianness of this machine is strange (or database is corrupt)\n");
unlock_and_exit(2);
}
/* }}} */
#define GET_TABLE(dest, start_index, table_size) {\
unsigned int table_offset = uidata[(start_index)];\
if ((table_offset + (table_size)) > uioffset_max) goto corrupt;\
(dest) = uidata + table_offset;\
}
#define GET_MSG_TABLE(dest, start_index) GET_TABLE((dest), (start_index), result->n_msgs)
#define GET_MBOX_TABLE(dest, start_index) GET_TABLE((dest), (start_index), result->n_mboxen)
/* Now build tables of where things are in the file */
result->n_msgs = uidata[UI_N_MSGS];
result->msg_type_and_flags = ucdata + uidata[UI_MSG_TYPE_AND_FLAGS];
if (0 && (
(result->msg_type_and_flags + result->n_msgs) >
(ucdata + len)
)) {
corrupt:
fprintf(stderr, "Database is corrupt. Please rebuild.\n");
unlock_and_exit(2);
}
GET_MSG_TABLE(result->path_offsets, UI_MSG_CDATA);
GET_MSG_TABLE(result->mtime_table, UI_MSG_MTIME);
GET_MSG_TABLE(result->size_table, UI_MSG_SIZE);
GET_MSG_TABLE(result->date_table, UI_MSG_DATE);
GET_MSG_TABLE(result->tid_table, UI_MSG_TID);
result->n_mboxen = uidata[UI_MBOX_N];
GET_MBOX_TABLE(result->mbox_paths_table, UI_MBOX_PATHS);
GET_MBOX_TABLE(result->mbox_entries_table, UI_MBOX_ENTRIES);
GET_MBOX_TABLE(result->mbox_mtime_table, UI_MBOX_MTIME);
GET_MBOX_TABLE(result->mbox_size_table, UI_MBOX_SIZE);
GET_MBOX_TABLE(result->mbox_checksum_table, UI_MBOX_CKSUM);
result->hash_key = uidata[UI_HASH_KEY];
if (!(
read_toktable_db(data, len, &result->to, UI_TO_BASE, uidata) &&
read_toktable_db(data, len, &result->cc, UI_CC_BASE, uidata) &&
read_toktable_db(data, len, &result->from, UI_FROM_BASE, uidata) &&
read_toktable_db(data, len, &result->subject, UI_SUBJECT_BASE, uidata) &&
read_toktable_db(data, len, &result->body, UI_BODY_BASE, uidata) &&
read_toktable_db(data, len, &result->attachment_name, UI_ATTACHMENT_NAME_BASE, uidata) &&
read_toktable2_db(data, len, &result->msg_ids, UI_MSGID_BASE, uidata)
)) {
goto corrupt;
}
return result;
}
/*}}}*/
static void free_toktable_db(struct toktable_db *x)/*{{{*/
{
/* Nothing to do */
}
/*}}}*/
static void free_toktable2_db(struct toktable2_db *x)/*{{{*/
{
/* Nothing to do */
}
/*}}}*/
void close_db(struct read_db *x)/*{{{*/
{
free_toktable_db(&x->to);
free_toktable_db(&x->cc);
free_toktable_db(&x->from);
free_toktable_db(&x->subject);
free_toktable_db(&x->body);
free_toktable_db(&x->attachment_name);
free_toktable2_db(&x->msg_ids);
if (munmap(x->data, x->len) < 0) {
perror("munmap");
unlock_and_exit(2);
}
free(x);
return;
}
/*}}}*/