3 * STANAG 4607 file reading
5 * http://www.nato.int/structur/AC/224/standard/4607/4607e_JAS_ED3.pdf
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.
27 #include "file_wrappers.h"
28 #include <wsutil/buffer.h>
29 #include "stanag4607.h"
35 static gboolean is_valid_id(guint16 version_id)
37 #define VERSION_21 0x3231
38 #define VERSION_30 0x3330
39 if ((version_id != VERSION_21) &&
40 (version_id != VERSION_30))
41 /* Not a stanag4607 file */
46 static gboolean stanag4607_read_file(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
47 Buffer *buf, int *err, gchar **err_info)
49 stanag4607_t *stanag4607 = (stanag4607_t *)wth->priv;
50 guint32 millisecs, secs, nsecs;
52 guint8 stanag_pkt_hdr[37];
57 /* Combined packet header and segment header */
58 if (!wtap_read_bytes_or_eof(fh, stanag_pkt_hdr, sizeof stanag_pkt_hdr, err, err_info))
60 offset += sizeof stanag_pkt_hdr;
62 if (!is_valid_id(pntoh16(&stanag_pkt_hdr[0]))) {
63 *err = WTAP_ERR_BAD_FILE;
64 *err_info = g_strdup("Bad version number");
68 phdr->rec_type = REC_TYPE_PACKET;
70 /* The next 4 bytes are the packet length */
71 packet_size = pntoh32(&stanag_pkt_hdr[2]);
72 if (packet_size > WTAP_MAX_PACKET_SIZE) {
74 * Probably a corrupt capture file; don't blow up trying
75 * to allocate space for an immensely-large packet.
77 *err = WTAP_ERR_BAD_FILE;
78 *err_info = g_strdup_printf("stanag4607: File has %" G_GUINT32_FORMAT "d-byte packet, "
79 "bigger than maximum of %u", packet_size, WTAP_MAX_PACKET_SIZE);
82 phdr->caplen = packet_size;
83 phdr->len = packet_size;
85 /* Sadly, the header doesn't contain times; but some segments do */
86 /* So, get the segment header, which is just past the 32-byte header. */
87 phdr->presence_flags = WTAP_HAS_TS;
89 /* If no time specified, it's the last baseline time */
90 phdr->ts.secs = stanag4607->base_secs;
94 #define MISSION_SEGMENT 1
95 #define DWELL_SEGMENT 2
96 #define JOB_DEFINITION_SEGMENT 5
97 #define PLATFORM_LOCATION_SEGMENT 13
98 if (MISSION_SEGMENT == stanag_pkt_hdr[32]) {
102 if (!wtap_read_bytes(fh, &mseg, sizeof mseg, err, err_info))
104 offset += sizeof mseg;
106 tm.tm_year = pntoh16(&mseg[35]) - 1900;
107 tm.tm_mon = mseg[37] - 1;
108 tm.tm_mday = mseg[38];
113 stanag4607->base_secs = mktime(&tm);
114 phdr->ts.secs = stanag4607->base_secs;
116 else if (PLATFORM_LOCATION_SEGMENT == stanag_pkt_hdr[32]) {
117 if (!wtap_read_bytes(fh, &millisecs, sizeof millisecs, err, err_info))
119 offset += sizeof millisecs;
120 millisecs = g_ntohl(millisecs);
122 else if (DWELL_SEGMENT == stanag_pkt_hdr[32]) {
124 if (!wtap_read_bytes(fh, &dseg, sizeof dseg, err, err_info))
126 offset += sizeof dseg;
127 millisecs = pntoh32(&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)
140 return wtap_read_packet_bytes(fh, buf, packet_size, err, err_info);
143 static gboolean stanag4607_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
149 offset = file_tell(wth->fh);
151 *data_offset = offset;
153 return stanag4607_read_file(wth, wth->fh, &wth->phdr, wth->frame_buffer, err, err_info);
156 static gboolean stanag4607_seek_read(wtap *wth, gint64 seek_off,
157 struct wtap_pkthdr *phdr,
158 Buffer *buf, int *err, gchar **err_info)
160 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
163 return stanag4607_read_file(wth, wth->random_fh, phdr, buf, err, err_info);
166 wtap_open_return_val stanag4607_open(wtap *wth, int *err, gchar **err_info)
169 stanag4607_t *stanag4607;
171 if (!wtap_read_bytes(wth->fh, &version_id, sizeof version_id, err, err_info))
172 return (*err != WTAP_ERR_SHORT_READ) ? WTAP_OPEN_ERROR : WTAP_OPEN_NOT_MINE;
174 if (!is_valid_id(GUINT16_TO_BE(version_id)))
175 /* Not a stanag4607 file */
176 return WTAP_OPEN_NOT_MINE;
178 /* seek back to the start of the file */
179 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
180 return WTAP_OPEN_ERROR;
182 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_STANAG_4607;
183 wth->file_encap = WTAP_ENCAP_STANAG_4607;
184 wth->snapshot_length = 0; /* not known */
186 stanag4607 = (stanag4607_t *)g_malloc(sizeof(stanag4607_t));
187 wth->priv = (void *)stanag4607;
188 stanag4607->base_secs = 0; /* unknown as of yet */
190 wth->subtype_read = stanag4607_read;
191 wth->subtype_seek_read = stanag4607_seek_read;
192 wth->file_tsprec = WTAP_TSPREC_MSEC;
194 return WTAP_OPEN_MINE;
198 * Editor modelines - http://www.wireshark.org/tools/modelines.html
203 * indent-tabs-mode: nil
206 * vi: set shiftwidth=2 tabstop=8 expandtab:
207 * :indentSize=2:tabSize=8:noTabs=true: