3 * MIME file format decoder for the Wiretap library.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #ifdef HAVE_SYS_TYPES_H
28 #include <sys/types.h>
41 #include "file_wrappers.h"
43 #include "mime_file.h"
48 } mime_file_private_t;
56 * Written by Marton Nemeth <nm127@freemail.hu>
57 * Copyright 2009 Marton Nemeth
58 * The JPEG and JFIF specification can be found at:
60 * http://www.jpeg.org/public/jfif.pdf
61 * http://www.w3.org/Graphics/JPEG/itu-t81.pdf
63 static const guchar jpeg_jfif_magic[] = { 0xFF, 0xD8, /* SOF */
64 0xFF /* start of the next marker */
67 static const mime_files_t magic_files[] = {
68 { jpeg_jfif_magic, sizeof(jpeg_jfif_magic) }
71 #define N_MAGIC_TYPES (sizeof(magic_files) / sizeof(magic_files[0]))
74 mime_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
76 mime_file_private_t *priv = (mime_file_private_t *) wth->priv;
78 char _buf[WTAP_MAX_PACKET_SIZE];
82 if (priv->last_packet) {
83 *err = file_error(wth->fh, err_info);
87 wth->phdr.ts.secs = (time_t) wth->data_offset;
88 wth->phdr.ts.nsecs = 0;
90 *data_offset = wth->data_offset;
92 /* try to read max WTAP_MAX_PACKET_SIZE bytes */
93 packet_size = file_read(_buf, sizeof(_buf), wth->fh);
95 if (packet_size <= 0) {
96 priv->last_packet = TRUE;
97 /* signal error for packet-mime-encap */
99 wth->phdr.ts.nsecs = 1000000000;
101 wth->phdr.caplen = 0;
106 /* copy to wth frame buffer */
107 buffer_assure_space(wth->frame_buffer, packet_size);
108 buf = buffer_start_ptr(wth->frame_buffer);
109 memcpy(buf, _buf, packet_size);
111 wth->data_offset += packet_size;
112 wth->phdr.caplen = packet_size;
113 wth->phdr.len = packet_size;
118 mime_seek_read(wtap *wth, gint64 seek_off, union wtap_pseudo_header *pseudo_header _U_, guchar *pd, int length, int *err, gchar **err_info)
120 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) {
125 wtap_file_read_expected_bytes(pd, length, wth->random_fh, err, err_info);
133 mime_file_open(wtap *wth, int *err, gchar **err_info)
135 char magic_buf[128]; /* increase buffer size when needed */
140 guint read_bytes = 0;
142 for (i = 0; i < N_MAGIC_TYPES; i++)
143 read_bytes = MAX(read_bytes, magic_files[i].magic_len);
145 read_bytes = MIN(read_bytes, sizeof(magic_buf));
146 bytes_read = file_read(magic_buf, read_bytes, wth->fh);
148 if (bytes_read > 0) {
149 gboolean found_file = FALSE;
152 for (i = 0; i < N_MAGIC_TYPES; i++) {
153 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))) {
158 return 0; /* many files matched, bad file */
165 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
168 wth->file_type = WTAP_FILE_MIME;
169 wth->file_encap = WTAP_ENCAP_MIME;
170 wth->tsprecision = WTAP_FILE_TSPREC_SEC;
171 wth->subtype_read = mime_read;
172 wth->subtype_seek_read = mime_seek_read;
173 wth->snapshot_length = 0;
176 wth->priv = g_malloc0(sizeof(mime_file_private_t));
179 *err = file_error(wth->fh, err_info);