Bugfixes of ASTERIX I034
[metze/wireshark/wip.git] / wiretap / capsa.c
1 /* capsa.c
2  *
3  * Wiretap Library
4  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8
9 #include "config.h"
10 #include <errno.h>
11 #include <string.h>
12 #include "wtap-int.h"
13 #include "file_wrappers.h"
14 #include "capsa.h"
15
16 /*
17  * A file begins with a header containing:
18  *
19  *   a 4-byte magic number, with 'c', 'p', 's', 'e';
20  *
21  *   either a 2-byte little-endian "format indicator" (version number?),
22  *   or a 1-byte major version number followed by a 1-byte minor version
23  *   number, or a 1-byte "format indicator" followed by something else
24  *   that's always been 0;
25  *
26  *   a 2-byte 0xe8 0x03 (1000 - a data rate?  megabits/second?)
27  *
28  *   4 bytes of 0x01 0x00 0x01 0x00;
29  *
30  *   either a 4-byte little-endian file size followed by 0x00 0x00 0x00 0x00
31  *   or an 8-byte little-endian file size;
32  *
33  *   a 4-byte little-endian packet count (in dns_error_of_udp, it exceeds?)
34  *
35  *   a 4-byte little-endian number?
36  *
37  *   hex 2c 01 c8 00 00 00 da 36 00 00 00 00 00 00;
38  *
39  *   the same 4-byte little-endian number as above (yes, misaligned);
40  *
41  *   0x01 or 0x03;
42  *
43  *   a bunch of 0s, up to an offset of 0x36d6;
44  *
45  *   more stuff.
46  *
47  * Following that is a sequence of { record offset block, up to 200 records }
48  * pairs.
49  *
50  * A record offset block has 1 byte with the value 0xfe, a sequence of
51  * up to 200 4-byte little-endian record offsets, and 4 or more bytes
52  * of unknown data, making the block 805 bytes long.
53  *
54  * The record offsets are offsets, from the beginning of the record offset
55  * block (i.e., from the 0xfe byte), of the records following the block.
56  */
57
58 /* Magic number in Capsa files. */
59 static const char capsa_magic[] = {
60         'c', 'p', 's', 'e'
61 };
62
63 /*
64  * Before each group of 200 or fewer records there's a block of frame
65  * offsets, giving the offsets, from the beginning of that block minus
66  * one(1), of the next N records.
67  */
68 #define N_RECORDS_PER_GROUP     200
69
70 /* Capsa (format indicator 1) record header. */
71 struct capsarec_hdr {
72         guint32 unknown1;       /* low-order 32 bits of a number? */
73         guint32 unknown2;       /* 0x00 0x00 0x00 0x00 */
74         guint32 timestamplo;    /* low-order 32 bits of the time stamp, in microseconds since January 1, 1970, 00:00:00 UTC */
75         guint32 timestamphi;    /* high-order 32 bits of the time stamp, in microseconds since January 1, 1970, 00:00:00 UTC */
76         guint16 rec_len;        /* length of record */
77         guint16 incl_len;       /* number of octets captured in file */
78         guint16 orig_len;       /* actual length of packet */
79         guint16 unknown5;       /* 0x00 0x00 */
80         guint8 count1;          /* count1*4 bytes after unknown8 */
81         guint8 count2;          /* count2*4 bytes after that */
82         guint16 unknown7;       /* 0x01 0x10 */
83         guint32 unknown8;       /* 0x00 0x00 0x00 0x00 or random numbers */
84 };
85
86 /* Packet Builder (format indicator 2) record header. */
87 struct pbrec_hdr {
88         guint16 rec_len;        /* length of record */
89         guint16 incl_len;       /* number of octets captured in file */
90         guint16 orig_len;       /* actual length of packet */
91         guint16 unknown1;
92         guint16 unknown2;
93         guint16 unknown3;
94         guint32 unknown4;
95         guint32 timestamplo;    /* low-order 32 bits of the time stamp, in microseconds since January 1, 1970, 00:00:00 UTC */
96         guint32 timestamphi;    /* high-order 32 bits of the time stamp, in microseconds since January 1, 1970, 00:00:00 UTC */
97         guint32 unknown5;
98         guint32 unknown6;
99 };
100
101 typedef struct {
102         guint16 format_indicator;
103         guint32 number_of_frames;
104         guint32 frame_count;
105         gint64 base_offset;
106         guint32 record_offsets[N_RECORDS_PER_GROUP];
107 } capsa_t;
108
109 static gboolean capsa_read(wtap *wth, int *err, gchar **err_info,
110     gint64 *data_offset);
111 static gboolean capsa_seek_read(wtap *wth, gint64 seek_off,
112     wtap_rec *rec, Buffer *buf, int *err, gchar **err_info);
113 static int capsa_read_packet(wtap *wth, FILE_T fh, wtap_rec *rec,
114     Buffer *buf, int *err, gchar **err_info);
115
116 wtap_open_return_val capsa_open(wtap *wth, int *err, gchar **err_info)
117 {
118         char magic[sizeof capsa_magic];
119         guint16 format_indicator;
120         int file_type_subtype;
121         guint32 number_of_frames;
122         capsa_t *capsa;
123
124         /* Read in the string that should be at the start of a Capsa file */
125         if (!wtap_read_bytes(wth->fh, magic, sizeof magic, err, err_info)) {
126                 if (*err != WTAP_ERR_SHORT_READ)
127                         return WTAP_OPEN_ERROR;
128                 return WTAP_OPEN_NOT_MINE;
129         }
130
131         if (memcmp(magic, capsa_magic, sizeof capsa_magic) != 0) {
132                 return WTAP_OPEN_NOT_MINE;
133         }
134
135         /* Read the mysterious "format indicator" */
136         if (!wtap_read_bytes(wth->fh, &format_indicator, sizeof format_indicator,
137             err, err_info))
138                 return WTAP_OPEN_ERROR;
139         format_indicator = GUINT16_FROM_LE(format_indicator);
140
141         /*
142          * Make sure it's a format we support.
143          */
144         switch (format_indicator) {
145
146         case 1:         /* Capsa */
147                 file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_COLASOFT_CAPSA;
148                 break;
149
150         case 2:         /* Packet Builder */
151                 file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_COLASOFT_PACKET_BUILDER;
152                 break;
153
154         default:
155                 *err = WTAP_ERR_UNSUPPORTED;
156                 *err_info = g_strdup_printf("capsa: format indicator %u unsupported",
157                     format_indicator);
158                 return WTAP_OPEN_ERROR;
159         }
160
161         /*
162          * Link speed, in megabytes/second?
163          */
164         if (!wtap_read_bytes(wth->fh, NULL, 2, err, err_info))
165                 return WTAP_OPEN_ERROR;
166
167         /*
168          * Flags of some sort?  Four 1-byte numbers, two of which are 1
169          * and two of which are zero?  Two 2-byte numbers or flag fields,
170          * both of which are 1?
171          */
172         if (!wtap_read_bytes(wth->fh, NULL, 4, err, err_info))
173                 return WTAP_OPEN_ERROR;
174
175         /*
176          * File size, in bytes.
177          */
178         if (!wtap_read_bytes(wth->fh, NULL, 4, err, err_info))
179                 return WTAP_OPEN_ERROR;
180
181         /*
182          * Zeroes?  Or upper 4 bytes of file size?
183          */
184         if (!wtap_read_bytes(wth->fh, NULL, 4, err, err_info))
185                 return WTAP_OPEN_ERROR;
186
187         /*
188          * Count of packets.
189          */
190         if (!wtap_read_bytes(wth->fh, &number_of_frames, sizeof number_of_frames,
191             err, err_info))
192                 return WTAP_OPEN_ERROR;
193         number_of_frames = GUINT32_FROM_LE(number_of_frames);
194
195         /*
196          * Skip past what we think is file header.
197          */
198         if (!file_seek(wth->fh, 0x44ef, SEEK_SET, err))
199                 return WTAP_OPEN_ERROR;
200
201         wth->file_type_subtype = file_type_subtype;
202         capsa = (capsa_t *)g_malloc(sizeof(capsa_t));
203         capsa->format_indicator = format_indicator;
204         capsa->number_of_frames = number_of_frames;
205         capsa->frame_count = 0;
206         wth->priv = (void *)capsa;
207         wth->subtype_read = capsa_read;
208         wth->subtype_seek_read = capsa_seek_read;
209         /*
210          * XXX - we've never seen a Wi-Fi Capsa capture, so we don't
211          * yet know how to handle them.
212          */
213         wth->file_encap = WTAP_ENCAP_ETHERNET;
214         wth->snapshot_length = 0;       /* not available in header */
215         wth->file_tsprec = WTAP_TSPREC_USEC;
216         return WTAP_OPEN_MINE;
217 }
218
219 /* Read the next packet */
220 static gboolean capsa_read(wtap *wth, int *err, gchar **err_info,
221     gint64 *data_offset)
222 {
223         capsa_t *capsa = (capsa_t *)wth->priv;
224         guint32 frame_within_block;
225         int     padbytes;
226
227         if (capsa->frame_count == capsa->number_of_frames) {
228                 /*
229                  * No more frames left.  Return an EOF.
230                  */
231                 *err = 0;
232                 return FALSE;
233         }
234         frame_within_block = capsa->frame_count % N_RECORDS_PER_GROUP;
235         if (frame_within_block == 0) {
236                 /*
237                  * Here's a record offset block.
238                  * Get the offset of the block, and then skip the
239                  * first byte.
240                  */
241                 capsa->base_offset = file_tell(wth->fh);
242                 if (!wtap_read_bytes(wth->fh, NULL, 1, err, err_info))
243                         return FALSE;
244
245                 /*
246                  * Now read the record offsets.
247                  */
248                 if (!wtap_read_bytes(wth->fh, &capsa->record_offsets,
249                     sizeof capsa->record_offsets, err, err_info))
250                         return FALSE;
251
252                 /*
253                  * And finish processing all 805 bytes by skipping
254                  * the last 4 bytes.
255                  */
256                 if (!wtap_read_bytes(wth->fh, NULL, 4, err, err_info))
257                         return FALSE;
258         }
259
260         *data_offset = capsa->base_offset +
261             GUINT32_FROM_LE(capsa->record_offsets[frame_within_block]);
262         if (!file_seek(wth->fh, *data_offset, SEEK_SET, err))
263                 return FALSE;
264
265         padbytes = capsa_read_packet(wth, wth->fh, &wth->rec,
266             wth->rec_data, err, err_info);
267         if (padbytes == -1)
268                 return FALSE;
269
270         /*
271          * Skip over the padding, if any.
272          */
273         if (padbytes != 0) {
274                 if (!wtap_read_bytes(wth->fh, NULL, padbytes, err, err_info))
275                         return FALSE;
276         }
277
278         capsa->frame_count++;
279
280         return TRUE;
281 }
282
283 static gboolean
284 capsa_seek_read(wtap *wth, gint64 seek_off,
285     wtap_rec *rec, Buffer *buf, int *err, gchar **err_info)
286 {
287         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
288                 return FALSE;
289
290         if (capsa_read_packet(wth, wth->random_fh, rec, buf, err, err_info) == -1) {
291                 if (*err == 0)
292                         *err = WTAP_ERR_SHORT_READ;
293                 return FALSE;
294         }
295         return TRUE;
296 }
297
298 static int
299 capsa_read_packet(wtap *wth, FILE_T fh, wtap_rec *rec,
300     Buffer *buf, int *err, gchar **err_info)
301 {
302         capsa_t *capsa = (capsa_t *)wth->priv;
303         struct capsarec_hdr capsarec_hdr;
304         struct pbrec_hdr pbrec_hdr;
305         guint32 rec_size;
306         guint32 packet_size;
307         guint32 orig_size;
308         guint32 header_size;
309         guint64 timestamp;
310
311         /* Read record header. */
312         switch (capsa->format_indicator) {
313
314         case 1:
315                 if (!wtap_read_bytes_or_eof(fh, &capsarec_hdr,
316                     sizeof capsarec_hdr, err, err_info))
317                         return -1;
318                 rec_size = GUINT16_FROM_LE(capsarec_hdr.rec_len);
319                 orig_size = GUINT16_FROM_LE(capsarec_hdr.orig_len);
320                 packet_size = GUINT16_FROM_LE(capsarec_hdr.incl_len);
321                 header_size = sizeof capsarec_hdr;
322                 timestamp = (((guint64)GUINT32_FROM_LE(capsarec_hdr.timestamphi))<<32) + GUINT32_FROM_LE(capsarec_hdr.timestamplo);
323
324                 /*
325                  * OK, the rest of this is variable-length.
326                  * We skip: (count1+count2)*4 bytes.
327                  * XXX - what is that?  Measured statistics?
328                  * Calculated statistics?
329                  */
330                 if (!wtap_read_bytes(fh, NULL,
331                     (capsarec_hdr.count1 + capsarec_hdr.count2)*4,
332                     err, err_info))
333                         return -1;
334                 header_size += (capsarec_hdr.count1 + capsarec_hdr.count2)*4;
335                 break;
336
337         case 2:
338                 if (!wtap_read_bytes_or_eof(fh, &pbrec_hdr,
339                     sizeof pbrec_hdr, err, err_info))
340                         return -1;
341                 rec_size = GUINT16_FROM_LE(pbrec_hdr.rec_len);
342                 orig_size = GUINT16_FROM_LE(pbrec_hdr.orig_len);
343                 packet_size = GUINT16_FROM_LE(pbrec_hdr.incl_len);
344                 header_size = sizeof pbrec_hdr;
345                 timestamp = (((guint64)GUINT32_FROM_LE(pbrec_hdr.timestamphi))<<32) + GUINT32_FROM_LE(pbrec_hdr.timestamplo);
346                 /*
347                  * XXX - from the results of some conversions between
348                  * Capsa format and pcap by Colasoft Packet Builder,
349                  * I do not trust its conversion of time stamps (at
350                  * least one of Colasoft's sample files, when
351                  * converted to pcap format, has, as its time stamps,
352                  * time stamps on the day after the conversion was
353                  * done, which seems like more than just coincidence).
354                  */
355                 break;
356
357         default:
358                 g_assert_not_reached();
359                 *err = WTAP_ERR_INTERNAL;
360                 return -1;
361         }
362         if (orig_size > WTAP_MAX_PACKET_SIZE_STANDARD) {
363                 /*
364                  * Probably a corrupt capture file; don't blow up trying
365                  * to allocate space for an immensely-large packet.
366                  */
367                 *err = WTAP_ERR_BAD_FILE;
368                 *err_info = g_strdup_printf("capsa: File has %u-byte original length, bigger than maximum of %u",
369                     orig_size, WTAP_MAX_PACKET_SIZE_STANDARD);
370                 return -1;
371         }
372         if (packet_size > WTAP_MAX_PACKET_SIZE_STANDARD) {
373                 /*
374                  * Probably a corrupt capture file; don't blow up trying
375                  * to allocate space for an immensely-large packet.
376                  */
377                 *err = WTAP_ERR_BAD_FILE;
378                 *err_info = g_strdup_printf("capsa: File has %u-byte packet, bigger than maximum of %u",
379                     packet_size, WTAP_MAX_PACKET_SIZE_STANDARD);
380                 return -1;
381         }
382         if (header_size + packet_size > rec_size) {
383                 /*
384                  * Probably a corrupt capture file.
385                  */
386                 *err = WTAP_ERR_BAD_FILE;
387                 *err_info = g_strdup_printf("capsa: File has %u-byte packet with %u-byte record header, bigger than record size %u",
388                     packet_size, header_size, rec_size);
389                 return -1;
390         }
391
392         /*
393          * The "on the wire" record size always includes the CRC.
394          * If it's greater than the "captured" size by 4, then
395          * we subtract 4 from it, to reflect the way the "on the wire"
396          * record size works for other file formats.
397          */
398         if (orig_size == packet_size + 4)
399                 orig_size = packet_size;
400
401         /*
402          * We assume there's no FCS in this frame.
403          * XXX - is there ever one?
404          */
405         rec->rec_header.packet_header.pseudo_header.eth.fcs_len = 0;
406
407         rec->rec_type = REC_TYPE_PACKET;
408         rec->rec_header.packet_header.caplen = packet_size;
409         rec->rec_header.packet_header.len = orig_size;
410         rec->presence_flags = WTAP_HAS_CAP_LEN|WTAP_HAS_TS;
411         rec->ts.secs = (time_t)(timestamp / 1000000);
412         rec->ts.nsecs = ((int)(timestamp % 1000000))*1000;
413
414         /*
415          * Read the packet data.
416          */
417         if (!wtap_read_packet_bytes(fh, buf, packet_size, err, err_info))
418                 return -1;      /* failed */
419
420         return rec_size - (header_size + packet_size);
421 }
422
423 /*
424  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
425  *
426  * Local variables:
427  * c-basic-offset: 8
428  * tab-width: 8
429  * indent-tabs-mode: t
430  * End:
431  *
432  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
433  * :indentSize=8:tabSize=8:noTabs=false:
434  */