Add wtap_pseudo_header union to wtap_pkthdr structure.
[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         gboolean last_packet;
54
55 } mime_file_private_t;
56
57 typedef struct {
58         const guint8 *magic;
59         guint magic_len;
60 } mime_files_t;
61
62 /*
63  * Written by Marton Nemeth <nm127@freemail.hu>
64  * Copyright 2009 Marton Nemeth
65  * The JPEG and JFIF specification can be found at:
66  *
67  * http://www.jpeg.org/public/jfif.pdf
68  * http://www.w3.org/Graphics/JPEG/itu-t81.pdf
69  */
70 static const guint8 jpeg_jfif_magic[] = { 0xFF, 0xD8, /* SOF */
71                                           0xFF        /* start of the next marker */
72                                         };
73
74 /* <?xml */
75 static const guint8 xml_magic[] = { '<', '?', 'x', 'm', 'l' };
76
77 static const mime_files_t magic_files[] = {
78         { jpeg_jfif_magic, sizeof(jpeg_jfif_magic) },
79         { xml_magic, sizeof(xml_magic) }
80 };
81
82 #define N_MAGIC_TYPES   (sizeof(magic_files) / sizeof(magic_files[0]))
83
84 static gboolean
85 mime_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
86 {
87         mime_file_private_t *priv = (mime_file_private_t *) wth->priv;
88
89         char _buf[WTAP_MAX_PACKET_SIZE];
90         guint8 *buf;
91         int packet_size;
92
93         if (priv->last_packet) {
94                 *err = file_error(wth->fh, err_info);
95                 return FALSE;
96         }
97
98         wth->phdr.presence_flags = 0;
99
100         wth->phdr.ts.secs = 0;
101         wth->phdr.ts.nsecs = 0;
102
103         *data_offset = file_tell(wth->fh);
104
105         /* try to read max WTAP_MAX_PACKET_SIZE bytes */
106         packet_size = file_read(_buf, sizeof(_buf), wth->fh);
107
108         if (packet_size <= 0) {
109                 priv->last_packet = TRUE;
110                 /* signal error for packet-mime-encap */
111                 if (packet_size < 0)
112                         wth->phdr.ts.nsecs = 1000000000;
113
114                 wth->phdr.caplen = 0;
115                 wth->phdr.len = 0;
116                 return TRUE;
117         }
118
119         /* copy to wth frame buffer */
120         buffer_assure_space(wth->frame_buffer, packet_size);
121         buf = buffer_start_ptr(wth->frame_buffer);
122         memcpy(buf, _buf, packet_size);
123
124         wth->phdr.caplen = packet_size;
125         wth->phdr.len = packet_size;
126         return TRUE;
127 }
128
129 static gboolean
130 mime_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr _U_, guint8 *pd, int length, int *err, gchar **err_info)
131 {
132         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) {
133                 *err_info = NULL;
134                 return FALSE;
135         }
136
137         wtap_file_read_expected_bytes(pd, length, wth->random_fh, err, err_info);
138
139         *err = 0;
140         *err_info = NULL;
141         return TRUE;
142 }
143
144 int
145 mime_file_open(wtap *wth, int *err, gchar **err_info)
146 {
147         char magic_buf[128]; /* increase buffer size when needed */
148         int bytes_read;
149         int ret;
150         guint i;
151
152         guint read_bytes = 0;
153
154         for (i = 0; i < N_MAGIC_TYPES; i++)
155                 read_bytes = MAX(read_bytes, magic_files[i].magic_len);
156
157         read_bytes = MIN(read_bytes, sizeof(magic_buf));
158         bytes_read = file_read(magic_buf, read_bytes, wth->fh);
159
160         if (bytes_read > 0) {
161                 gboolean found_file = FALSE;
162                 /* guint file_ok; */
163
164                 for (i = 0; i < N_MAGIC_TYPES; i++) {
165                         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))) {
166                                 if (!found_file) {
167                                         found_file = TRUE;
168                                         /* file_ok = i; */
169                                 } else
170                                         return 0;       /* many files matched, bad file */
171                         }
172                 }
173
174                 if (!found_file)
175                         return 0;
176
177                 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
178                         return -1;
179
180                 wth->file_type = WTAP_FILE_MIME;
181                 wth->file_encap = WTAP_ENCAP_MIME;
182                 wth->tsprecision = WTAP_FILE_TSPREC_SEC;
183                 wth->subtype_read = mime_read;
184                 wth->subtype_seek_read = mime_seek_read;
185                 wth->snapshot_length = 0;
186                 ret = 1;
187
188                 wth->priv = g_malloc0(sizeof(mime_file_private_t));
189
190         } else {
191                 *err = file_error(wth->fh, err_info);
192                 ret = -1;
193         }
194         return ret;
195 }
196