add WERR_INVALID_STATE
[wireshark-wip.git] / wiretap / tnef.c
blobc726381fcc8994e5e84a4e500235d33ed4342323
1 /* tnef.c
3 * Transport-Neutral Encapsulation Format (TNEF) file reading
5 * $Id$
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "config.h"
24 #include <errno.h>
26 #ifdef HAVE_SYS_STAT_H
27 #include <sys/stat.h>
28 #endif
30 #include "wtap-int.h"
31 #include "file_wrappers.h"
32 #include "buffer.h"
33 #include "tnef.h"
35 static gboolean tnef_read_file(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
36 Buffer *buf, int *err, gchar **err_info)
38 gint64 file_size;
39 int packet_size;
41 if ((file_size = wtap_file_size(wth, err)) == -1)
42 return FALSE;
44 if (file_size > WTAP_MAX_PACKET_SIZE) {
46 * Probably a corrupt capture file; don't blow up trying
47 * to allocate space for an immensely-large packet.
49 *err = WTAP_ERR_BAD_FILE;
50 *err_info = g_strdup_printf("tnef: File has %" G_GINT64_MODIFIER "d-byte packet, bigger than maximum of %u",
51 file_size, WTAP_MAX_PACKET_SIZE);
52 return FALSE;
54 packet_size = (int)file_size;
56 phdr->presence_flags = 0; /* yes, we have no bananas^Wtime stamp */
58 phdr->caplen = packet_size;
59 phdr->len = packet_size;
61 phdr->ts.secs = 0;
62 phdr->ts.nsecs = 0;
64 return wtap_read_packet_bytes(fh, buf, packet_size, err, err_info);
67 static gboolean tnef_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
69 gint64 offset;
71 *err = 0;
73 offset = file_tell(wth->fh);
75 /* there is only ever one packet */
76 if (offset)
77 return FALSE;
79 *data_offset = offset;
81 return tnef_read_file(wth, wth->fh, &wth->phdr, wth->frame_buffer, err, err_info);
84 static gboolean tnef_seek_read(wtap *wth, gint64 seek_off,
85 struct wtap_pkthdr *phdr,
86 Buffer *buf, int length _U_, int *err, gchar **err_info)
88 /* there is only one packet */
89 if(seek_off > 0) {
90 *err = 0;
91 return FALSE;
94 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
95 return FALSE;
97 return tnef_read_file(wth, wth->random_fh, phdr, buf, err, err_info);
100 int tnef_open(wtap *wth, int *err, gchar **err_info)
102 int bytes_read;
103 guint32 magic;
105 bytes_read = file_read(&magic, sizeof magic, wth->fh);
106 if (bytes_read != sizeof magic) {
107 *err = file_error(wth->fh, err_info);
108 return (*err != 0) ? -1 : 0;
111 if (htolel(magic) != TNEF_SIGNATURE)
112 /* Not a tnef file */
113 return 0;
115 /* seek back to the start of the file */
116 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
117 return -1;
119 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_TNEF;
120 wth->file_encap = WTAP_ENCAP_TNEF;
121 wth->snapshot_length = 0;
123 wth->subtype_read = tnef_read;
124 wth->subtype_seek_read = tnef_seek_read;
125 wth->tsprecision = WTAP_FILE_TSPREC_SEC;
127 return 1;