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