Explain a bit more what this does.
[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 guchar *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 guchar jpeg_jfif_magic[] = { 0xFF, 0xD8, /* SOF */
73                                           0xFF        /* start of the next marker */
74                                         };
75
76 static const mime_files_t magic_files[] = {
77         { jpeg_jfif_magic, sizeof(jpeg_jfif_magic) }
78 };
79
80 #define N_MAGIC_TYPES   (sizeof(magic_files) / sizeof(magic_files[0]))
81
82 static gboolean
83 mime_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
84 {
85         mime_file_private_t *priv = (mime_file_private_t *) wth->priv;
86
87         char _buf[WTAP_MAX_PACKET_SIZE];
88         guint8 *buf;
89         int packet_size;
90
91         if (priv->last_packet) {
92                 *err = file_error(wth->fh, err_info);
93                 return FALSE;
94         }
95
96         wth->phdr.ts.secs = (time_t) wth->data_offset;
97         wth->phdr.ts.nsecs = 0;
98
99         *data_offset = wth->data_offset;
100
101         /* try to read max WTAP_MAX_PACKET_SIZE bytes */
102         packet_size = file_read(_buf, sizeof(_buf), wth->fh);
103
104         if (packet_size <= 0) {
105                 priv->last_packet = TRUE;
106                 /* signal error for packet-mime-encap */
107                 if (packet_size < 0)
108                         wth->phdr.ts.nsecs = 1000000000;
109
110                 wth->phdr.caplen = 0;
111                 wth->phdr.len = 0;
112                 return TRUE;
113         }
114
115         /* copy to wth frame buffer */
116         buffer_assure_space(wth->frame_buffer, packet_size);
117         buf = buffer_start_ptr(wth->frame_buffer);
118         memcpy(buf, _buf, packet_size);
119
120         wth->data_offset += packet_size;
121         wth->phdr.caplen = packet_size;
122         wth->phdr.len = packet_size;
123         return TRUE;
124 }
125
126 static gboolean
127 mime_seek_read(wtap *wth, gint64 seek_off, union wtap_pseudo_header *pseudo_header _U_, guchar *pd, int length, int *err, gchar **err_info)
128 {
129         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) {
130                 *err_info = NULL;
131                 return FALSE;
132         }
133
134         wtap_file_read_expected_bytes(pd, length, wth->random_fh, err, err_info);
135
136         *err = 0;
137         *err_info = NULL;
138         return TRUE;
139 }
140
141 int
142 mime_file_open(wtap *wth, int *err, gchar **err_info)
143 {
144         char magic_buf[128]; /* increase buffer size when needed */
145         int bytes_read;
146         int ret;
147         guint i;
148
149         guint read_bytes = 0;
150
151         for (i = 0; i < N_MAGIC_TYPES; i++)
152                 read_bytes = MAX(read_bytes, magic_files[i].magic_len);
153
154         read_bytes = MIN(read_bytes, sizeof(magic_buf));
155         bytes_read = file_read(magic_buf, read_bytes, wth->fh);
156
157         if (bytes_read > 0) {
158                 gboolean found_file = FALSE;
159                 /* guint file_ok; */
160
161                 for (i = 0; i < N_MAGIC_TYPES; i++) {
162                         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))) {
163                                 if (!found_file) {
164                                         found_file = TRUE;
165                                         /* file_ok = i; */
166                                 } else
167                                         return 0;       /* many files matched, bad file */
168                         }
169                 }
170
171                 if (!found_file)
172                         return 0;
173
174                 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
175                         return -1;
176
177                 wth->file_type = WTAP_FILE_MIME;
178                 wth->file_encap = WTAP_ENCAP_MIME;
179                 wth->tsprecision = WTAP_FILE_TSPREC_SEC;
180                 wth->subtype_read = mime_read;
181                 wth->subtype_seek_read = mime_seek_read;
182                 wth->snapshot_length = 0;
183                 ret = 1;
184
185                 wth->priv = g_malloc0(sizeof(mime_file_private_t));
186
187         } else {
188                 *err = file_error(wth->fh, err_info);
189                 ret = -1;
190         }
191         return ret;
192 }
193