1 /*
2   mairix - message index builder and finder for maildir folders.
3 
4  **********************************************************************
5  * Copyright (C) Richard P. Curnow  2002-2004,2006
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of version 2 of the GNU General Public License as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  **********************************************************************
21  */
22 
23 #ifndef READER_H
24 #define READER_H
25 
26 /* MX, then a high byte, then the version no. */
27 #define HEADER_MAGIC0 'M'
28 #define HEADER_MAGIC1 'X'
29 #define HEADER_MAGIC2 0xA5
30 #define HEADER_MAGIC3 0x03
31 
32 /*{{{ Constants for file data positions */
33 #define UI_ENDIAN          1
34 #define UI_N_MSGS          2
35 
36 /* Offset to byte-per-message table encoding message types */
37 #define UI_MSG_TYPE_AND_FLAGS 3
38 
39 /* Header positions containing offsets to the per-message tables. */
40 /* Character data:
41  * for maildir/MH : the path of the box.
42  * for mbox : index of mbox containing the message */
43 
44 #define UI_MSG_CDATA       4
45 /* For maildir/MH : mtime of file containing message */
46 #define UI_MSG_MTIME       5
47 /* For mbox msgs : the offset into the file */
48 #define UI_MSG_OFFSET      5
49 /* For all formats : message size */
50 #define UI_MSG_SIZE        6
51 /* For mbox msgs : offset into file */
52 #define UI_MSG_START       6
53 /* These are common to Maildir,MH,mbox messages */
54 #define UI_MSG_DATE        7
55 #define UI_MSG_TID         8
56 
57 /* Header positions for mbox (file-level) information */
58 /* Number of mboxes */
59 #define UI_MBOX_N          9
60 #define UI_MBOX_PATHS     10
61 #define UI_MBOX_ENTRIES   11
62 /* mtime of mboxes */
63 #define UI_MBOX_MTIME     12
64 /* Size in bytes */
65 #define UI_MBOX_SIZE      13
66 /* Base of checksums for messages in each mbox */
67 #define UI_MBOX_CKSUM     14
68 
69 #define UI_HASH_KEY       15
70 
71 /* Header positions for token tables */
72 #define UI_TO_BASE        16
73 #define UI_CC_BASE        19
74 #define UI_FROM_BASE      22
75 #define UI_SUBJECT_BASE   25
76 #define UI_BODY_BASE      28
77 #define UI_ATTACHMENT_NAME_BASE 31
78 #define UI_MSGID_BASE     34
79 
80 /* Larger than the last table offset. */
81 #define UI_HEADER_LEN     40
82 #define UC_HEADER_LEN     ((UI_HEADER_LEN) << 2)
83 
84 #define UI_N_OFFSET        0
85 #define UI_TOK_OFFSET      1
86 #define UI_ENC_OFFSET      2
87 
88 #define UI_TO_N           (UI_TO_BASE + UI_N_OFFSET)
89 #define UI_TO_TOK         (UI_TO_BASE + UI_TOK_OFFSET)
90 #define UI_TO_ENC         (UI_TO_BASE + UI_ENC_OFFSET)
91 #define UI_CC_N           (UI_CC_BASE + UI_N_OFFSET)
92 #define UI_CC_TOK         (UI_CC_BASE + UI_TOK_OFFSET)
93 #define UI_CC_ENC         (UI_CC_BASE + UI_ENC_OFFSET)
94 #define UI_FROM_N         (UI_FROM_BASE + UI_N_OFFSET)
95 #define UI_FROM_TOK       (UI_FROM_BASE + UI_TOK_OFFSET)
96 #define UI_FROM_ENC       (UI_FROM_BASE + UI_ENC_OFFSET)
97 #define UI_SUBJECT_N      (UI_SUBJECT_BASE + UI_N_OFFSET)
98 #define UI_SUBJECT_TOK    (UI_SUBJECT_BASE + UI_TOK_OFFSET)
99 #define UI_SUBJECT_ENC    (UI_SUBJECT_BASE + UI_ENC_OFFSET)
100 #define UI_BODY_N         (UI_BODY_BASE + UI_N_OFFSET)
101 #define UI_BODY_TOK       (UI_BODY_BASE + UI_TOK_OFFSET)
102 #define UI_BODY_ENC       (UI_BODY_BASE + UI_ENC_OFFSET)
103 #define UI_ATTACHMENT_NAME_N    (UI_ATTACHMENT_NAME_BASE + UI_N_OFFSET)
104 #define UI_ATTACHMENT_NAME_TOK  (UI_ATTACHMENT_NAME_BASE + UI_TOK_OFFSET)
105 #define UI_ATTACHMENT_NAME_ENC  (UI_ATTACHMENT_NAME_BASE + UI_ENC_OFFSET)
106 #define UI_MSGID_N        (UI_MSGID_BASE + UI_N_OFFSET)
107 #define UI_MSGID_TOK      (UI_MSGID_BASE + UI_TOK_OFFSET)
108 #define UI_MSGID_ENC0     (UI_MSGID_BASE + UI_ENC_OFFSET)
109 #define UI_MSGID_ENC1     (UI_MSGID_ENC0 + 1)
110 
111 /*}}}*/
112 
113 /*{{{ Literals used for encoding messages types in database file */
114 #define DB_MSG_DEAD 0
115 /* maildir/MH : one file per message */
116 #define DB_MSG_FILE 1
117 /* mbox : multiple files per message */
118 #define DB_MSG_MBOX 2
119 /* message comes from IMAP server */
120 #define DB_MSG_IMAP 3
121 /*}}}*/
122 
123 #define FLAG_SEEN    (1<<3)
124 #define FLAG_REPLIED (1<<4)
125 #define FLAG_FLAGGED (1<<5)
126 
127 struct toktable_db {/*{{{*/
128   unsigned int n; /* number of entries in this table */
129   unsigned int *tok_offsets; /* offset to table of token offsets */
130   unsigned int *enc_offsets; /* offset to table of encoding offsets */
131 };
132 /*}}}*/
133 struct toktable2_db {/*{{{*/
134   unsigned int n; /* number of entries in this table */
135   unsigned int *tok_offsets; /* offset to table of token offsets */
136   unsigned int *enc0_offsets; /* offset to table of encoding offsets */
137   unsigned int *enc1_offsets; /* offset to table of encoding offsets */
138 };
139 /*}}}*/
140 struct read_db {/*{{{*/
141   /* Raw file parameters, needed later for munmap */
142   char *data;
143   int len;
144 
145   /* Pathname information */
146   int n_msgs;
147   unsigned char *msg_type_and_flags;
148   unsigned int *path_offsets; /* or (mbox index, msg index) */
149   unsigned int *mtime_table; /* or offset into mbox */
150   unsigned int *size_table;  /* either file size or span inside mbox */
151   unsigned int *date_table;
152   unsigned int *tid_table;
153 
154   int n_mboxen;
155   unsigned int *mbox_paths_table;
156   unsigned int *mbox_entries_table; /* table of number of messages per mbox */
157   unsigned int *mbox_mtime_table;
158   unsigned int *mbox_size_table;
159   unsigned int *mbox_checksum_table;
160 
161   unsigned int hash_key;
162 
163   struct toktable_db to;
164   struct toktable_db cc;
165   struct toktable_db from;
166   struct toktable_db subject;
167   struct toktable_db body;
168   struct toktable_db attachment_name;
169   struct toktable2_db msg_ids;
170 
171 };
172 /*}}}*/
173 
174 struct read_db *open_db(char *filename);
175 void close_db(struct read_db *x);
176 
rd_msg_type(struct read_db * db,int i)177 static inline int rd_msg_type(struct read_db *db, int i) {
178   return db->msg_type_and_flags[i] & 0x7;
179 }
180 
181 /* Common to search and db reader. */
182 int read_increment(unsigned char **encpos);
183 
184 #endif /* READER_H */
185