From Weston Schmidt via https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=8689:
[metze/wireshark/wip.git] / wiretap / tnef.c
1 /* tnef.c
2  *
3  * Transport-Neutral Encapsulation Format (TNEF) file reading
4  *
5  * $Id$
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include "config.h"
23
24 #include <errno.h>
25
26 #ifdef HAVE_SYS_STAT_H
27 #include <sys/stat.h>
28 #endif
29
30 #include "wtap-int.h"
31 #include "file_wrappers.h"
32 #include "buffer.h"
33 #include "tnef.h"
34
35
36 static gboolean tnef_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
37 {
38   gint64 offset;
39   guint8 *buf;
40   gint64 file_size;
41   int packet_size;
42
43   *err = 0;
44
45   offset = file_tell(wth->fh);
46
47   /* there is only ever one packet */
48   if (offset)
49     return FALSE;
50
51   *data_offset = offset;
52
53   if ((file_size = wtap_file_size(wth, err)) == -1)
54     return FALSE;
55
56   if (file_size > WTAP_MAX_PACKET_SIZE) {
57     /*
58      * Probably a corrupt capture file; don't blow up trying
59      * to allocate space for an immensely-large packet.
60      */
61     *err = WTAP_ERR_BAD_FILE;
62     *err_info = g_strdup_printf("tnef: File has %" G_GINT64_MODIFIER "d-byte packet, bigger than maximum of %u",
63                                 file_size, WTAP_MAX_PACKET_SIZE);
64     return FALSE;
65   }
66   packet_size = (int)file_size;
67
68   buffer_assure_space(wth->frame_buffer, packet_size);
69   buf = buffer_start_ptr(wth->frame_buffer);
70
71   wtap_file_read_expected_bytes(buf, packet_size, wth->fh, err, err_info);
72
73   wth->phdr.presence_flags = 0; /* no time stamp, no "real length" */
74
75   wth->phdr.caplen = packet_size;
76   wth->phdr.len = packet_size;
77
78   wth->phdr.ts.secs = 0;
79   wth->phdr.ts.nsecs = 0;
80
81   return TRUE;
82 }
83
84 static gboolean tnef_seek_read(wtap *wth, gint64 seek_off,
85                                struct wtap_pkthdr *phdr _U_,
86                                guint8 *pd, int length, int *err, gchar **err_info)
87 {
88   int packet_size = length;
89
90   /* there is only one packet */
91   if(seek_off > 0) {
92     *err = 0;
93     return FALSE;
94   }
95
96   if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
97     return FALSE;
98
99   wtap_file_read_expected_bytes(pd, packet_size, wth->random_fh, err, err_info);
100
101   return TRUE;
102 }
103
104 int tnef_open(wtap *wth, int *err, gchar **err_info)
105 {
106   int bytes_read;
107   guint32 magic;
108
109   bytes_read = file_read(&magic, sizeof magic, wth->fh);
110   if (bytes_read != sizeof magic) {
111     *err = file_error(wth->fh, err_info);
112     if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
113       return -1;
114     return 0;
115   }
116
117   if (htolel(magic) != TNEF_SIGNATURE)
118      /* Not a tnef file */
119      return 0;
120
121   /* seek back to the start of the file  */
122   if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
123     return -1;
124
125   wth->file_type = WTAP_FILE_TNEF;
126   wth->file_encap = WTAP_ENCAP_TNEF;
127   wth->snapshot_length = 0;
128
129   wth->subtype_read = tnef_read;
130   wth->subtype_seek_read = tnef_seek_read;
131   wth->tsprecision = WTAP_FILE_TSPREC_SEC;
132
133   return 1;
134 }