Fix possible compilation errors.
[obnox/wireshark/wip.git] / wiretap / mime_file.c
1 /* mime_file.c
2  *
3  * MIME file format decoder for the Wiretap library.
4  *
5  * $Id$
6  *
7  * 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.
12  *
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.
17  *
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #ifdef HAVE_SYS_TYPES_H
28 #include <sys/types.h>
29 #endif
30
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34
35 #include <errno.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <time.h>
39
40 #include "wtap-int.h"
41 #include "file_wrappers.h"
42 #include "buffer.h"
43 #include "mime_file.h"
44
45 typedef struct {
46         gboolean last_packet;
47
48 } mime_file_private_t;
49
50 typedef struct {
51         const guchar *magic;
52         guint magic_len;
53 } mime_files_t;
54
55 /*
56  * Written by Marton Nemeth <nm127@freemail.hu>
57  * Copyright 2009 Marton Nemeth
58  * The JPEG and JFIF specification can be found at:
59  *
60  * http://www.jpeg.org/public/jfif.pdf
61  * http://www.w3.org/Graphics/JPEG/itu-t81.pdf
62  */
63 static const guchar jpeg_jfif_magic[] = { 0xFF, 0xD8, /* SOF */
64                                           0xFF        /* start of the next marker */
65                                         };
66
67 static const mime_files_t magic_files[] = {
68         { jpeg_jfif_magic, sizeof(jpeg_jfif_magic) }
69 };
70
71 #define N_MAGIC_TYPES   (sizeof(magic_files) / sizeof(magic_files[0]))
72
73 static gboolean
74 mime_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
75 {
76         mime_file_private_t *priv = (mime_file_private_t *) wth->priv;
77
78         char _buf[WTAP_MAX_PACKET_SIZE];
79         guint8 *buf;
80         int packet_size;
81
82         if (priv->last_packet) {
83                 *err = file_error(wth->fh, err_info);
84                 return FALSE;
85         }
86
87         wth->phdr.ts.secs = (time_t) wth->data_offset;
88         wth->phdr.ts.nsecs = 0;
89
90         *data_offset = wth->data_offset;
91
92         /* try to read max WTAP_MAX_PACKET_SIZE bytes */
93         packet_size = file_read(_buf, sizeof(_buf), wth->fh);
94
95         if (packet_size <= 0) {
96                 priv->last_packet = TRUE;
97                 /* signal error for packet-mime-encap */
98                 if (packet_size < 0)
99                         wth->phdr.ts.nsecs = 1000000000;
100
101                 wth->phdr.caplen = 0;
102                 wth->phdr.len = 0;
103                 return TRUE;
104         }
105
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);
110
111         wth->data_offset += packet_size;
112         wth->phdr.caplen = packet_size;
113         wth->phdr.len = packet_size;
114         return TRUE;
115 }
116
117 static gboolean
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)
119 {
120         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) {
121                 *err_info = NULL;
122                 return FALSE;
123         }
124
125         wtap_file_read_expected_bytes(pd, length, wth->random_fh, err, err_info);
126
127         *err = 0;
128         *err_info = NULL;
129         return TRUE;
130 }
131
132 int
133 mime_file_open(wtap *wth, int *err, gchar **err_info)
134 {
135         char magic_buf[128]; /* increase buffer size when needed */
136         int bytes_read;
137         int ret;
138         guint i;
139
140         guint read_bytes = 0;
141
142         for (i = 0; i < N_MAGIC_TYPES; i++)
143                 read_bytes = MAX(read_bytes, magic_files[i].magic_len);
144
145         read_bytes = MIN(read_bytes, sizeof(magic_buf));
146         bytes_read = file_read(magic_buf, read_bytes, wth->fh);
147
148         if (bytes_read > 0) {
149                 gboolean found_file = FALSE;
150                 /* guint file_ok; */
151
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))) {
154                                 if (!found_file) {
155                                         found_file = TRUE;
156                                         /* file_ok = i; */
157                                 } else
158                                         return 0;       /* many files matched, bad file */
159                         }
160                 }
161
162                 if (!found_file)
163                         return 0;
164
165                 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
166                         return -1;
167
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;
174                 ret = 1;
175
176                 wth->priv = g_malloc0(sizeof(mime_file_private_t));
177
178         } else {
179                 *err = file_error(wth->fh, err_info);
180                 ret = -1;
181         }
182         return ret;
183 }
184