3 * MIME file format decoder for the Wiretap library.
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.
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.
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.
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.
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.
32 #ifdef HAVE_SYS_TYPES_H
33 #include <sys/types.h>
46 #include "file_wrappers.h"
47 #include <wsutil/buffer.h>
48 #include "mime_file.h"
56 * Written by Marton Nemeth <nm127@freemail.hu>
57 * Copyright 2009 Marton Nemeth
58 * The JPEG and JFIF specification can be found at:
60 * http://www.jpeg.org/public/jfif.pdf
61 * http://www.w3.org/Graphics/JPEG/itu-t81.pdf
63 static const guint8 jpeg_jfif_magic[] = { 0xFF, 0xD8, /* SOF */
64 0xFF /* start of the next marker */
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 static const guint8 btsnoop_magic[] = { 'b', 't', 's', 'n', 'o', 'o', 'p', 0};
74 static const guint8 pcap_magic[] = { 0xA1, 0xB2, 0xC3, 0xD4 };
75 static const guint8 pcap_swapped_magic[] = { 0xD4, 0xC3, 0xB2, 0xA1 };
76 static const guint8 pcapng_premagic[] = { 0x0A, 0x0D, 0x0D, 0x0A };
78 /* File does not start with it */
79 static const guint8 pcapng_xmagic[] = { 0x1A, 0x2B, 0x3C, 0x4D };
80 static const guint8 pcapng_swapped_xmagic[] = { 0x4D, 0x3C, 0x2B, 0x1A };
82 static const mime_files_t magic_files[] = {
83 { jpeg_jfif_magic, sizeof(jpeg_jfif_magic) },
84 { xml_magic, sizeof(xml_magic) },
85 { png_magic, sizeof(png_magic) },
86 { gif87a_magic, sizeof(gif87a_magic) },
87 { gif89a_magic, sizeof(gif89a_magic) },
88 { elf_magic, sizeof(elf_magic) },
89 { btsnoop_magic, sizeof(btsnoop_magic) },
90 { pcap_magic, sizeof(pcap_magic) },
91 { pcap_swapped_magic, sizeof(pcap_swapped_magic) },
92 { pcapng_premagic, sizeof(pcapng_premagic) }
95 #define N_MAGIC_TYPES (sizeof(magic_files) / sizeof(magic_files[0]))
98 * Impose a not-too-large limit on the maximum file size, to avoid eating
99 * up 99% of the (address space, swap partition, disk space for swap/page
100 * files); if we were to return smaller chunks and let the dissector do
101 * reassembly, it would *still* have to allocate a buffer the size of
102 * the file, so it's not as if we'd neve try to allocate a buffer the
105 #define MAX_FILE_SIZE G_MAXINT
108 mime_read_file(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
109 Buffer *buf, int *err, gchar **err_info)
114 if ((file_size = wtap_file_size(wth, err)) == -1)
117 if (file_size > MAX_FILE_SIZE) {
119 * Don't blow up trying to allocate space for an
120 * immensely-large file.
122 *err = WTAP_ERR_BAD_FILE;
123 *err_info = g_strdup_printf("mime_file: File has %" G_GINT64_MODIFIER "d-byte packet, bigger than maximum of %u",
124 file_size, MAX_FILE_SIZE);
127 packet_size = (int)file_size;
129 phdr->rec_type = REC_TYPE_PACKET;
130 phdr->presence_flags = 0; /* yes, we have no bananas^Wtime stamp */
132 phdr->caplen = packet_size;
133 phdr->len = packet_size;
138 return wtap_read_packet_bytes(fh, buf, packet_size, err, err_info);
142 mime_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
148 offset = file_tell(wth->fh);
150 /* there is only ever one packet */
154 *data_offset = offset;
156 return mime_read_file(wth, wth->fh, &wth->phdr, wth->frame_buffer, err, err_info);
160 mime_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
162 /* there is only one packet */
168 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
171 return mime_read_file(wth, wth->random_fh, phdr, buf, err, err_info);
175 mime_file_open(wtap *wth, int *err, gchar **err_info)
177 char magic_buf[128]; /* increase buffer size when needed */
183 guint read_bytes = 12;
185 for (i = 0; i < N_MAGIC_TYPES; i++)
186 read_bytes = MAX(read_bytes, magic_files[i].magic_len);
188 read_bytes = (guint)MIN(read_bytes, sizeof(magic_buf));
189 bytes_read = file_read(magic_buf, read_bytes, wth->fh);
191 if (bytes_read < 0) {
192 *err = file_error(wth->fh, err_info);
193 return WTAP_OPEN_ERROR;
196 return WTAP_OPEN_NOT_MINE;
199 for (i = 0; i < N_MAGIC_TYPES; i++) {
200 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))) {
202 if (magic_files[i].magic == pcapng_premagic) {
203 if (memcmp(magic_buf + 8, pcapng_xmagic, sizeof(pcapng_xmagic)) &&
204 memcmp(magic_buf + 8, pcapng_swapped_xmagic, sizeof(pcapng_swapped_xmagic)))
209 return WTAP_OPEN_NOT_MINE; /* many files matched, bad file */
214 return WTAP_OPEN_NOT_MINE;
216 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
217 return WTAP_OPEN_ERROR;
219 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_MIME;
220 wth->file_encap = WTAP_ENCAP_MIME;
221 wth->file_tsprec = WTAP_TSPREC_SEC;
222 wth->subtype_read = mime_read;
223 wth->subtype_seek_read = mime_seek_read;
224 wth->snapshot_length = 0;
226 return WTAP_OPEN_MINE;
230 * Editor modelines - http://www.wireshark.org/tools/modelines.html
235 * indent-tabs-mode: t
238 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
239 * :indentSize=8:tabSize=8:noTabs=false: