opa: Add dissectors for Intel’s Omni-Path Architecture (OPA)
[metze/wireshark/wip.git] / wiretap / tnef.c
1 /* tnef.c
2  *
3  * Transport-Neutral Encapsulation Format (TNEF) file reading
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #include "config.h"
21
22 #include <errno.h>
23
24 #include "wtap-int.h"
25 #include "file_wrappers.h"
26 #include <wsutil/buffer.h>
27 #include "tnef.h"
28
29 static gboolean tnef_read_file(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
30                                Buffer *buf, int *err, gchar **err_info)
31 {
32   gint64 file_size;
33   int packet_size;
34
35   if ((file_size = wtap_file_size(wth, err)) == -1)
36     return FALSE;
37
38   if (file_size > WTAP_MAX_PACKET_SIZE) {
39     /*
40      * Probably a corrupt capture file; don't blow up trying
41      * to allocate space for an immensely-large packet.
42      */
43     *err = WTAP_ERR_BAD_FILE;
44     *err_info = g_strdup_printf("tnef: File has %" G_GINT64_MODIFIER "d-byte packet, bigger than maximum of %u",
45                                 file_size, WTAP_MAX_PACKET_SIZE);
46     return FALSE;
47   }
48   packet_size = (int)file_size;
49
50   phdr->rec_type = REC_TYPE_PACKET;
51   phdr->presence_flags = 0; /* yes, we have no bananas^Wtime stamp */
52
53   phdr->caplen = packet_size;
54   phdr->len = packet_size;
55
56   phdr->ts.secs = 0;
57   phdr->ts.nsecs = 0;
58
59   return wtap_read_packet_bytes(fh, buf, packet_size, err, err_info);
60 }
61
62 static gboolean tnef_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
63 {
64   gint64 offset;
65
66   *err = 0;
67
68   offset = file_tell(wth->fh);
69
70   /* there is only ever one packet */
71   if (offset)
72     return FALSE;
73
74   *data_offset = offset;
75
76   return tnef_read_file(wth, wth->fh, &wth->phdr, wth->frame_buffer, err, err_info);
77 }
78
79 static gboolean tnef_seek_read(wtap *wth, gint64 seek_off,
80                                struct wtap_pkthdr *phdr,
81                                Buffer *buf, int *err, gchar **err_info)
82 {
83   /* there is only one packet */
84   if(seek_off > 0) {
85     *err = 0;
86     return FALSE;
87   }
88
89   if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
90     return FALSE;
91
92   return tnef_read_file(wth, wth->random_fh, phdr, buf, err, err_info);
93 }
94
95 wtap_open_return_val tnef_open(wtap *wth, int *err, gchar **err_info)
96 {
97   guint32 magic;
98
99   if (!wtap_read_bytes(wth->fh, &magic, sizeof magic, err, err_info))
100     return (*err != WTAP_ERR_SHORT_READ) ? WTAP_OPEN_ERROR : WTAP_OPEN_NOT_MINE;
101
102   if (GUINT32_TO_LE(magic) != TNEF_SIGNATURE)
103      /* Not a tnef file */
104      return WTAP_OPEN_NOT_MINE;
105
106   /* seek back to the start of the file  */
107   if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
108     return WTAP_OPEN_ERROR;
109
110   wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_TNEF;
111   wth->file_encap = WTAP_ENCAP_TNEF;
112   wth->snapshot_length = 0;
113
114   wth->subtype_read = tnef_read;
115   wth->subtype_seek_read = tnef_seek_read;
116   wth->file_tsprec = WTAP_TSPREC_SEC;
117
118   return WTAP_OPEN_MINE;
119 }
120
121 /*
122  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
123  *
124  * Local Variables:
125  * c-basic-offset: 2
126  * tab-width: 8
127  * indent-tabs-mode: nil
128  * End:
129  *
130  * vi: set shiftwidth=2 tabstop=8 expandtab:
131  * :indentSize=2:tabSize=8:noTabs=true:
132  */