DCERPC: factor out proto_tree_add_dcerpc_drep()
[wireshark-wip.git] / wiretap / stanag4607.c
blobdf521ebacb88351761ce779c3f7c44e671c65e8e
1 /* stanag4607.c
3 * STANAG 4607 file reading
5 * http://www.nato.int/structur/AC/224/standard/4607/4607e_JAS_ED3.pdf
7 * $Id$
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "config.h"
26 #include <glib.h>
27 #include <errno.h>
29 #ifdef HAVE_SYS_STAT_H
30 #include <sys/stat.h>
31 #endif
33 #include "wtap-int.h"
34 #include "file_wrappers.h"
35 #include "buffer.h"
36 #include "stanag4607.h"
38 typedef struct {
39 time_t base_secs;
40 } stanag4607_t;
42 static gboolean is_valid_id(guint16 version_id)
44 #define VERSION_21 0x3231
45 #define VERSION_30 0x3330
46 if ((version_id != VERSION_21) &&
47 (version_id != VERSION_30))
48 /* Not a stanag4607 file */
49 return FALSE;
50 return TRUE;
53 static gboolean stanag4607_read_file(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
54 Buffer *buf, int *err, gchar **err_info)
56 stanag4607_t *stanag4607 = (stanag4607_t *)wth->priv;
57 guint32 millisecs, secs, nsecs;
58 gint64 offset = 0;
59 guint8 stanag_pkt_hdr[37];
60 int bytes_read;
61 guint32 packet_size;
63 *err = 0;
65 /* Combined packet header and segment header */
66 bytes_read = file_read(stanag_pkt_hdr, sizeof stanag_pkt_hdr, fh);
67 if (bytes_read != sizeof stanag_pkt_hdr)
68 goto fail;
69 offset += bytes_read;
71 if (!is_valid_id(pntohs(&stanag_pkt_hdr[0]))) {
72 *err = WTAP_ERR_BAD_FILE;
73 *err_info = g_strdup("Bad version number");
74 return FALSE;
77 /* The next 4 bytes are the packet length */
78 packet_size = pntohl(&stanag_pkt_hdr[2]);
79 phdr->caplen = packet_size;
80 phdr->len = packet_size;
82 /* Sadly, the header doesn't contain times; but some segments do */
83 /* So, get the segment header, which is just past the 32-byte header. */
84 phdr->presence_flags = WTAP_HAS_TS;
86 /* If no time specified, it's the last baseline time */
87 phdr->ts.secs = stanag4607->base_secs;
88 phdr->ts.nsecs = 0;
89 millisecs = 0;
91 #define MISSION_SEGMENT 1
92 #define DWELL_SEGMENT 2
93 #define JOB_DEFINITION_SEGMENT 5
94 #define PLATFORM_LOCATION_SEGMENT 13
95 if (MISSION_SEGMENT == stanag_pkt_hdr[32]) {
96 guint8 mseg[39];
97 struct tm tm;
99 bytes_read = file_read(&mseg, sizeof mseg, fh);
100 if (bytes_read != sizeof mseg)
101 goto fail;
102 offset += bytes_read;
104 tm.tm_year = pntohs(&mseg[35]) - 1900;
105 tm.tm_mon = mseg[37] - 1;
106 tm.tm_mday = mseg[38];
107 tm.tm_hour = 0;
108 tm.tm_min = 0;
109 tm.tm_sec = 0;
110 tm.tm_isdst = -1;
111 stanag4607->base_secs = mktime(&tm);
112 phdr->ts.secs = stanag4607->base_secs;
114 else if (PLATFORM_LOCATION_SEGMENT == stanag_pkt_hdr[32]) {
115 bytes_read = file_read(&millisecs, sizeof millisecs, fh);
116 if (bytes_read != sizeof millisecs)
117 goto fail;
118 offset += bytes_read;
119 millisecs = g_ntohl(millisecs);
121 else if (DWELL_SEGMENT == stanag_pkt_hdr[32]) {
122 guint8 dseg[19];
123 bytes_read = file_read(&dseg, sizeof dseg, fh);
124 if (bytes_read != sizeof dseg)
125 goto fail;
126 offset += bytes_read;
127 millisecs = pntohl(&dseg[15]);
129 if (0 != millisecs) {
130 secs = millisecs/1000;
131 nsecs = (millisecs - 1000 * secs) * 1000000;
132 phdr->ts.secs = stanag4607->base_secs + secs;
133 phdr->ts.nsecs = nsecs;
136 /* wind back to the start of the packet ... */
137 if (file_seek(fh, - offset, SEEK_CUR, err) == -1)
138 goto fail;
140 return wtap_read_packet_bytes(fh, buf, packet_size, err, err_info);
142 fail:
143 *err = file_error(wth->fh, err_info);
144 return FALSE;
147 static gboolean stanag4607_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
149 gint64 offset;
151 *err = 0;
153 offset = file_tell(wth->fh);
155 *data_offset = offset;
157 return stanag4607_read_file(wth, wth->fh, &wth->phdr, wth->frame_buffer, err, err_info);
160 static gboolean stanag4607_seek_read(wtap *wth, gint64 seek_off,
161 struct wtap_pkthdr *phdr,
162 Buffer *buf, int length _U_, int *err, gchar **err_info)
164 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
165 return FALSE;
167 return stanag4607_read_file(wth, wth->random_fh, phdr, buf, err, err_info);
170 int stanag4607_open(wtap *wth, int *err, gchar **err_info)
172 int bytes_read;
173 guint16 version_id;
174 stanag4607_t *stanag4607;
176 bytes_read = file_read(&version_id, sizeof version_id, wth->fh);
177 if (bytes_read != sizeof version_id) {
178 *err = file_error(wth->fh, err_info);
179 return (*err != 0) ? -1 : 0;
182 if (!is_valid_id(GUINT16_TO_BE(version_id)))
183 /* Not a stanag4607 file */
184 return 0;
186 /* seek back to the start of the file */
187 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
188 return -1;
190 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_STANAG_4607;
191 wth->file_encap = WTAP_ENCAP_STANAG_4607;
192 wth->snapshot_length = 0; /* not known */
194 stanag4607 = (stanag4607_t *)g_malloc(sizeof(stanag4607_t));
195 wth->priv = (void *)stanag4607;
196 stanag4607->base_secs = 0; /* unknown as of yet */
198 wth->subtype_read = stanag4607_read;
199 wth->subtype_seek_read = stanag4607_seek_read;
200 wth->tsprecision = WTAP_FILE_TSPREC_MSEC;
202 return 1;