Bugfixes of ASTERIX I034
[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_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.
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  *
16  * SPDX-License-Identifier: GPL-2.0-or-later
17  */
18
19 #include "config.h"
20
21 #include <sys/types.h>
22
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26
27 #include <errno.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <time.h>
31
32 #include "wtap-int.h"
33 #include "file_wrappers.h"
34 #include <wsutil/buffer.h>
35 #include "mime_file.h"
36
37 typedef struct {
38         const guint8 *magic;
39         guint magic_len;
40 } mime_files_t;
41
42 /*
43  * Written by Marton Nemeth <nm127@freemail.hu>
44  * Copyright 2009 Marton Nemeth
45  * The JPEG and JFIF specification can be found at:
46  *
47  * http://www.jpeg.org/public/jfif.pdf
48  * http://www.w3.org/Graphics/JPEG/itu-t81.pdf
49  */
50 static const guint8 jpeg_jfif_magic[] = { 0xFF, 0xD8, /* SOF */
51                                           0xFF        /* start of the next marker */
52                                         };
53
54 /* <?xml */
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 };
64
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 };
68
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) }
80 };
81
82 #define N_MAGIC_TYPES   (sizeof(magic_files) / sizeof(magic_files[0]))
83
84 wtap_open_return_val
85 mime_file_open(wtap *wth, int *err, gchar **err_info)
86 {
87         char magic_buf[128]; /* increase buffer size when needed */
88         int bytes_read;
89         gboolean found_file;
90         /* guint file_ok; */
91         guint i;
92
93         guint read_bytes = 12;
94
95         for (i = 0; i < N_MAGIC_TYPES; i++)
96                 read_bytes = MAX(read_bytes, magic_files[i].magic_len);
97
98         read_bytes = (guint)MIN(read_bytes, sizeof(magic_buf));
99         bytes_read = file_read(magic_buf, read_bytes, wth->fh);
100
101         if (bytes_read < 0) {
102                 *err = file_error(wth->fh, err_info);
103                 return WTAP_OPEN_ERROR;
104         }
105         if (bytes_read == 0)
106                 return WTAP_OPEN_NOT_MINE;
107
108         found_file = FALSE;
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))) {
111                         if (!found_file) {
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)))
115                                                 continue;
116                                 }
117                                 found_file = TRUE;
118                         } else
119                                 return WTAP_OPEN_NOT_MINE;      /* many files matched, bad file */
120                 }
121         }
122
123         if (!found_file)
124                 return WTAP_OPEN_NOT_MINE;
125
126         if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
127                 return WTAP_OPEN_ERROR;
128
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;
135
136         return WTAP_OPEN_MINE;
137 }
138
139 /*
140  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
141  *
142  * Local variables:
143  * c-basic-offset: 8
144  * tab-width: 8
145  * indent-tabs-mode: t
146  * End:
147  *
148  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
149  * :indentSize=8:tabSize=8:noTabs=false:
150  */