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_STANDARD,
8 * each of which is reported as a packet, so that files larger than
9 * WTAP_MAX_PACKET_SIZE_STANDARD 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.
16 * SPDX-License-Identifier: GPL-2.0-or-later
21 #include <sys/types.h>
33 #include "file_wrappers.h"
34 #include <wsutil/buffer.h>
35 #include "mime_file.h"
43 * Written by Marton Nemeth <nm127@freemail.hu>
44 * Copyright 2009 Marton Nemeth
45 * The JPEG and JFIF specification can be found at:
47 * http://www.jpeg.org/public/jfif.pdf
48 * http://www.w3.org/Graphics/JPEG/itu-t81.pdf
50 static const guint8 jpeg_jfif_magic[] = { 0xFF, 0xD8, /* SOF */
51 0xFF /* start of the next marker */
55 static const guint8 xml_magic[] = { '<', '?', 'x', 'm', 'l' };
56 static const guint8 png_magic[] = { 0x89, 'P', 'N', 'G', '\r', '\n', 0x1A, '\n' };
57 static const guint8 gif87a_magic[] = { 'G', 'I', 'F', '8', '7', 'a'};
58 static const guint8 gif89a_magic[] = { 'G', 'I', 'F', '8', '9', 'a'};
59 static const guint8 elf_magic[] = { 0x7F, 'E', 'L', 'F'};
60 static const guint8 btsnoop_magic[] = { 'b', 't', 's', 'n', 'o', 'o', 'p', 0};
61 static const guint8 pcap_magic[] = { 0xA1, 0xB2, 0xC3, 0xD4 };
62 static const guint8 pcap_swapped_magic[] = { 0xD4, 0xC3, 0xB2, 0xA1 };
63 static const guint8 pcapng_premagic[] = { 0x0A, 0x0D, 0x0D, 0x0A };
65 /* File does not start with it */
66 static const guint8 pcapng_xmagic[] = { 0x1A, 0x2B, 0x3C, 0x4D };
67 static const guint8 pcapng_swapped_xmagic[] = { 0x4D, 0x3C, 0x2B, 0x1A };
69 static const mime_files_t magic_files[] = {
70 { jpeg_jfif_magic, sizeof(jpeg_jfif_magic) },
71 { xml_magic, sizeof(xml_magic) },
72 { png_magic, sizeof(png_magic) },
73 { gif87a_magic, sizeof(gif87a_magic) },
74 { gif89a_magic, sizeof(gif89a_magic) },
75 { elf_magic, sizeof(elf_magic) },
76 { btsnoop_magic, sizeof(btsnoop_magic) },
77 { pcap_magic, sizeof(pcap_magic) },
78 { pcap_swapped_magic, sizeof(pcap_swapped_magic) },
79 { pcapng_premagic, sizeof(pcapng_premagic) }
82 #define N_MAGIC_TYPES (sizeof(magic_files) / sizeof(magic_files[0]))
85 mime_file_open(wtap *wth, int *err, gchar **err_info)
87 char magic_buf[128]; /* increase buffer size when needed */
93 guint read_bytes = 12;
95 for (i = 0; i < N_MAGIC_TYPES; i++)
96 read_bytes = MAX(read_bytes, magic_files[i].magic_len);
98 read_bytes = (guint)MIN(read_bytes, sizeof(magic_buf));
99 bytes_read = file_read(magic_buf, read_bytes, wth->fh);
101 if (bytes_read < 0) {
102 *err = file_error(wth->fh, err_info);
103 return WTAP_OPEN_ERROR;
106 return WTAP_OPEN_NOT_MINE;
109 for (i = 0; i < N_MAGIC_TYPES; i++) {
110 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))) {
112 if (magic_files[i].magic == pcapng_premagic) {
113 if (memcmp(magic_buf + 8, pcapng_xmagic, sizeof(pcapng_xmagic)) &&
114 memcmp(magic_buf + 8, pcapng_swapped_xmagic, sizeof(pcapng_swapped_xmagic)))
119 return WTAP_OPEN_NOT_MINE; /* many files matched, bad file */
124 return WTAP_OPEN_NOT_MINE;
126 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
127 return WTAP_OPEN_ERROR;
129 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_MIME;
130 wth->file_encap = WTAP_ENCAP_MIME;
131 wth->file_tsprec = WTAP_TSPREC_SEC;
132 wth->subtype_read = wtap_full_file_read;
133 wth->subtype_seek_read = wtap_full_file_seek_read;
134 wth->snapshot_length = 0;
136 return WTAP_OPEN_MINE;
140 * Editor modelines - http://www.wireshark.org/tools/modelines.html
145 * indent-tabs-mode: t
148 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
149 * :indentSize=8:tabSize=8:noTabs=false: