]> git.notmuchmail.org Git - notmuch/blob - message.c
message.c: Free leaked memory in notmuch_message object
[notmuch] / message.c
1 /* message.c - Utility functions for parsing an email message for notmuch.
2  *
3  * Copyright © 2009 Carl Worth
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see http://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include <stdarg.h>
22
23 #include "notmuch-private.h"
24
25 #include <glib.h> /* GHashTable */
26
27 typedef struct {
28     char *str;
29     size_t size;
30     size_t len;
31 } header_value_closure_t;
32
33 struct _notmuch_message {
34     /* File object */
35     FILE *file;
36
37     /* Header storage */
38     int restrict_headers;
39     GHashTable *headers;
40     int broken_headers;
41     int good_headers;
42
43     /* Parsing state */
44     char *line;
45     size_t line_size;
46     header_value_closure_t value;
47
48     int parsing_started;
49     int parsing_finished;
50 };
51
52 static int
53 strcase_equal (const void *a, const void *b)
54 {
55     return strcasecmp (a, b) == 0;
56 }
57
58 static unsigned int
59 strcase_hash (const void *ptr)
60 {
61     const char *s = ptr;
62
63     /* This is the djb2 hash. */
64     unsigned int hash = 5381;
65     while (s && *s) {
66         hash = ((hash << 5) + hash) + tolower (*s);
67         s++;
68     }
69
70     return hash;
71 }
72
73 notmuch_message_t *
74 notmuch_message_open (const char *filename)
75 {
76     notmuch_message_t *message;
77
78     message = xcalloc (1, sizeof (notmuch_message_t));
79
80     message->file = fopen (filename, "r");
81     if (message->file == NULL)
82         goto FAIL;
83
84     message->headers = g_hash_table_new_full (strcase_hash,
85                                               strcase_equal,
86                                               free,
87                                               free);
88
89     message->parsing_started = 0;
90     message->parsing_finished = 0;
91
92     return message;
93
94   FAIL:
95     fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));
96     notmuch_message_close (message);
97
98     return NULL;
99 }
100
101 void
102 notmuch_message_close (notmuch_message_t *message)
103 {
104     if (message == NULL)
105         return;
106
107     if (message->line)
108         free (message->line);
109
110     if (message->value.size)
111         free (message->value.str);
112
113     if (message->headers)
114         g_hash_table_unref (message->headers);
115
116     if (message->file)
117         fclose (message->file);
118
119     free (message);
120 }
121
122 void
123 notmuch_message_restrict_headersv (notmuch_message_t *message,
124                                    va_list va_headers)
125 {
126     char *header;
127
128     if (message->parsing_started ) {
129         fprintf (stderr, "Error: notmuch_message_restrict_headers called after parsing has started\n");
130         exit (1);
131     }
132
133     while (1) {
134         header = va_arg (va_headers, char*);
135         if (header == NULL)
136             break;
137         g_hash_table_insert (message->headers,
138                              xstrdup (header), NULL);
139     }
140
141     message->restrict_headers = 1;
142 }
143
144 void
145 notmuch_message_restrict_headers (notmuch_message_t *message, ...)
146 {
147     va_list va_headers;
148
149     va_start (va_headers, message);
150
151     notmuch_message_restrict_headersv (message, va_headers);
152 }
153
154 void
155 copy_header_unfolding (header_value_closure_t *value,
156                        const char *chunk)
157 {
158     char *last;
159
160     if (chunk == NULL)
161         return;
162
163     while (*chunk == ' ' || *chunk == '\t')
164         chunk++;
165
166     if (value->len + 1 + strlen (chunk) + 1 > value->size) {
167         int new_size = value->size;
168         if (value->size == 0)
169             new_size = strlen (chunk) + 1;
170         else
171             while (value->len + 1 + strlen (chunk) + 1 > new_size)
172                 new_size *= 2;
173         value->str = xrealloc (value->str, new_size);
174         value->size = new_size;
175     }
176
177     last = value->str + value->len;
178     if (value->len) {
179         *last = ' ';
180         last++;
181         value->len++;
182     }
183
184     strcpy (last, chunk);
185     value->len += strlen (chunk);
186
187     last = value->str + value->len - 1;
188     if (*last == '\n') {
189         *last = '\0';
190         value->len--;
191     }
192 }
193
194 const char *
195 notmuch_message_get_header (notmuch_message_t *message,
196                             const char *header_desired)
197 {
198     int contains;
199     char *header, *value;
200     const char *s, *colon;
201     int match;
202
203     message->parsing_started = 1;
204
205     contains = g_hash_table_lookup_extended (message->headers,
206                                              header_desired, NULL,
207                                              (gpointer *) &value);
208     if (contains && value)
209         return value;
210
211     if (message->parsing_finished)
212         return NULL;
213
214 #define NEXT_HEADER_LINE(closure)                               \
215     do {                                                        \
216         ssize_t bytes_read = getline (&message->line,           \
217                                       &message->line_size,      \
218                                       message->file);           \
219         if (bytes_read == -1) {                                 \
220             message->parsing_finished = 1;                      \
221             break;                                              \
222         }                                                       \
223         if (*message->line == '\n') {                           \
224             message->parsing_finished = 1;                      \
225             break;                                              \
226         }                                                       \
227         if (closure &&                                          \
228             (*message->line == ' ' || *message->line == '\t'))  \
229         {                                                       \
230             copy_header_unfolding ((closure), message->line);   \
231         }                                                       \
232     } while (*message->line == ' ' || *message->line == '\t');
233
234     if (message->line == NULL)
235         NEXT_HEADER_LINE (NULL);
236
237     while (1) {
238
239         if (message->parsing_finished)
240             break;
241
242         colon = strchr (message->line, ':');
243
244         if (colon == NULL) {
245             message->broken_headers++;
246             /* A simple heuristic for giving up on things that just
247              * don't look like mail messages. */
248             if (message->broken_headers >= 10 &&
249                 message->good_headers < 5)
250             {
251                 message->parsing_finished = 1;
252                 continue;
253             }
254             NEXT_HEADER_LINE (NULL);
255             continue;
256         }
257
258         message->good_headers++;
259
260         header = xstrndup (message->line, colon - message->line);
261
262         if (message->restrict_headers &&
263             ! g_hash_table_lookup_extended (message->headers,
264                                             header, NULL, NULL))
265         {
266             free (header);
267             NEXT_HEADER_LINE (NULL);
268             continue;
269         }
270
271         s = colon + 1;
272         while (*s == ' ' || *s == '\t')
273             s++;
274
275         message->value.len = 0;
276         copy_header_unfolding (&message->value, s);
277
278         NEXT_HEADER_LINE (&message->value);
279
280         match = (strcasecmp (header, header_desired) == 0);
281
282         value = xstrdup (message->value.str);
283
284         g_hash_table_insert (message->headers, header, value);
285
286         if (match)
287             return value;
288     }
289
290     if (message->line)
291         free (message->line);
292     message->line = NULL;
293
294     if (message->value.size) {
295         free (message->value.str);
296         message->value.str = NULL;
297         message->value.size = 0;
298         message->value.len = 0;
299     }
300
301     return NULL;
302 }