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