MIME: Allow to dissect big files
[metze/wireshark/wip.git] / wiretap / mime_file.c
1 /* mime_file.c
2  *
3  * MIME file format decoder for the Wiretap library.
4  *
5  * This is for use with Wireshark dissectors that handle file
6  * formats (e.g., because they handle a particular MIME media type).
7  * It breaks the file into chunks of at most WTAP_MAX_PACKET_SIZE,
8  * each of which is reported as a packet, so that files larger than
9  * WTAP_MAX_PACKET_SIZE can be handled by reassembly.
10  *
11  * The "MIME file" dissector does the reassembly, and hands the result
12  * off to heuristic dissectors to try to identify the file's contents.
13  *
14  * Wiretap Library
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28  */
29
30 #include "config.h"
31
32 #ifdef HAVE_SYS_TYPES_H
33 #include <sys/types.h>
34 #endif
35
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39
40 #include <errno.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <time.h>
44
45 #include "wtap-int.h"
46 #include "file_wrappers.h"
47 #include <wsutil/buffer.h>
48 #include "mime_file.h"
49
50 typedef struct {
51         const guint8 *magic;
52         guint magic_len;
53 } mime_files_t;
54
55 /*
56  * Written by Marton Nemeth <nm127@freemail.hu>
57  * Copyright 2009 Marton Nemeth
58  * The JPEG and JFIF specification can be found at:
59  *
60  * http://www.jpeg.org/public/jfif.pdf
61  * http://www.w3.org/Graphics/JPEG/itu-t81.pdf
62  */
63 static const guint8 jpeg_jfif_magic[] = { 0xFF, 0xD8, /* SOF */
64                                           0xFF        /* start of the next marker */
65                                         };
66
67 /* <?xml */
68 static const guint8 xml_magic[]    = { '<', '?', 'x', 'm', 'l' };
69 static const guint8 png_magic[]    = { 0x89, 'P', 'N', 'G', '\r', '\n', 0x1A, '\n' };
70 static const guint8 gif87a_magic[] = { 'G', 'I', 'F', '8', '7', 'a'};
71 static const guint8 gif89a_magic[] = { 'G', 'I', 'F', '8', '9', 'a'};
72 static const guint8 elf_magic[]    = { 0x7F, 'E', 'L', 'F'};
73
74 static const mime_files_t magic_files[] = {
75         { jpeg_jfif_magic, sizeof(jpeg_jfif_magic) },
76         { xml_magic, sizeof(xml_magic) },
77         { png_magic, sizeof(png_magic) },
78         { gif87a_magic, sizeof(gif87a_magic) },
79         { gif89a_magic, sizeof(gif89a_magic) },
80         { elf_magic, sizeof(elf_magic) }
81 };
82
83 #define N_MAGIC_TYPES   (sizeof(magic_files) / sizeof(magic_files[0]))
84
85 /*
86  * Impose a not-too-large limit on the maximum file size, to avoid eating
87  * up 99% of the (address space, swap partition, disk space for swap/page
88  * files); if we were to return smaller chunks and let the dissector do
89  * reassembly, it would *still* have to allocate a buffer the size of
90  * the file, so it's not as if we'd neve try to allocate a buffer the
91  * size of the file.
92  */
93 #define MAX_FILE_SIZE   G_MAXINT
94
95 static gboolean
96 mime_read_file(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
97     Buffer *buf, int *err, gchar **err_info)
98 {
99         gint64 file_size;
100         int packet_size;
101
102         if ((file_size = wtap_file_size(wth, err)) == -1)
103                 return FALSE;
104
105         if (file_size > MAX_FILE_SIZE) {
106                 /*
107                  * Don't blow up trying to allocate space for an
108                  * immensely-large file.
109                  */
110                 *err = WTAP_ERR_BAD_FILE;
111                 *err_info = g_strdup_printf("mime_file: File has %" G_GINT64_MODIFIER "d-byte packet, bigger than maximum of %u",
112                                 file_size, MAX_FILE_SIZE);
113                 return FALSE;
114         }
115         packet_size = (int)file_size;
116
117         phdr->rec_type = REC_TYPE_PACKET;
118         phdr->presence_flags = 0; /* yes, we have no bananas^Wtime stamp */
119
120         phdr->caplen = packet_size;
121         phdr->len = packet_size;
122
123         phdr->ts.secs = 0;
124         phdr->ts.nsecs = 0;
125
126         return wtap_read_packet_bytes(fh, buf, packet_size, err, err_info);
127 }
128
129 static gboolean
130 mime_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
131 {
132         gint64 offset;
133
134         *err = 0;
135
136         offset = file_tell(wth->fh);
137
138         /* there is only ever one packet */
139         if (offset != 0)
140                 return FALSE;
141
142         *data_offset = offset;
143
144         return mime_read_file(wth, wth->fh, &wth->phdr, wth->frame_buffer, err, err_info);
145 }
146
147 static gboolean
148 mime_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
149 {
150         /* there is only one packet */
151         if (seek_off > 0) {
152                 *err = 0;
153                 return FALSE;
154         }
155
156         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
157                 return FALSE;
158
159         return mime_read_file(wth, wth->random_fh, phdr, buf, err, err_info);
160 }
161
162 wtap_open_return_val
163 mime_file_open(wtap *wth, int *err, gchar **err_info)
164 {
165         char magic_buf[128]; /* increase buffer size when needed */
166         int bytes_read;
167         gboolean found_file;
168         /* guint file_ok; */
169         guint i;
170
171         guint read_bytes = 0;
172
173         for (i = 0; i < N_MAGIC_TYPES; i++)
174                 read_bytes = MAX(read_bytes, magic_files[i].magic_len);
175
176         read_bytes = (guint)MIN(read_bytes, sizeof(magic_buf));
177         bytes_read = file_read(magic_buf, read_bytes, wth->fh);
178
179         if (bytes_read < 0) {
180                 *err = file_error(wth->fh, err_info);
181                 return WTAP_OPEN_ERROR;
182         }
183         if (bytes_read == 0)
184                 return WTAP_OPEN_NOT_MINE;
185
186         found_file = FALSE;
187         for (i = 0; i < N_MAGIC_TYPES; i++) {
188                 if ((guint) bytes_read >= magic_files[i].magic_len && !memcmp(magic_buf, magic_files[i].magic, MIN(magic_files[i].magic_len, (guint) bytes_read))) {
189                         if (!found_file) {
190                                 found_file = TRUE;
191                                 /* file_ok = i; */
192                         } else
193                                 return WTAP_OPEN_NOT_MINE;      /* many files matched, bad file */
194                 }
195         }
196
197         if (!found_file)
198                 return WTAP_OPEN_NOT_MINE;
199
200         if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
201                 return WTAP_OPEN_ERROR;
202
203         wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_MIME;
204         wth->file_encap = WTAP_ENCAP_MIME;
205         wth->file_tsprec = WTAP_TSPREC_SEC;
206         wth->subtype_read = mime_read;
207         wth->subtype_seek_read = mime_seek_read;
208         wth->snapshot_length = 0;
209
210         return WTAP_OPEN_MINE;
211 }
212
213 /*
214  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
215  *
216  * Local variables:
217  * c-basic-offset: 8
218  * tab-width: 8
219  * indent-tabs-mode: t
220  * End:
221  *
222  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
223  * :indentSize=8:tabSize=8:noTabs=false:
224  */