TODO ... pkinit win2k
[metze/wireshark/wip.git] / wiretap / dpa400.c
1 /* dpa400.c
2  *
3  * Unigraf DisplayPort AUX channel monitor output parser
4  * Copyright 2018, Dirk Eibach, Guntermann & Drunck GmbH <dirk.eibach@gdsys.cc>
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8
9 #include "config.h"
10
11 #include <string.h>
12
13 #include "wtap-int.h"
14 #include "file_wrappers.h"
15 #include "dpa400.h"
16
17 enum {
18         DPA400_DATA = 0x00,
19         DPA400_DATA_END = 0x01,
20         DPA400_EVENT = 0x02,
21         DPA400_START = 0x03,
22         DPA400_STOP = 0x04,
23         DPA400_TS_OVERFLOW = 0x84,
24 };
25
26 struct dpa400_header {
27         guint8 t0;
28         guint8 sb0;
29         guint8 t1;
30         guint8 sb1;
31         guint8 t2;
32         guint8 sb2;
33 };
34
35 static gboolean dpa400_read_header(FILE_T fh, struct dpa400_header *hdr, int *err, gchar **err_info)
36 {
37         if (!wtap_read_bytes_or_eof(fh, hdr, sizeof(struct dpa400_header), err, err_info))
38                 return FALSE;
39
40         if (hdr->sb0 || hdr->sb1 || hdr->sb2) {
41                 *err = WTAP_ERR_BAD_FILE;
42                 *err_info = g_strdup("dpa400: malformed packet header");
43                 return FALSE;
44         }
45
46         return TRUE;
47 }
48
49 static void get_ts(struct dpa400_header *hdr, nstime_t *ts)
50 {
51         guint32 val;
52
53         val = (hdr->t0 | (hdr->t1 << 8) | ((hdr->t2 & 0x7f) << 16)) << 5;
54
55         ts->secs = val / 1000000;
56         ts->nsecs = (val % 1000000) * 1000;
57 }
58
59 static void get_ts_overflow(nstime_t *ts)
60 {
61         guint32 val = 0x7fffff << 5;
62
63         ts->secs = val / 1000000;
64         ts->nsecs = (val % 1000000) * 1000;
65 }
66
67 static guint8 get_from(struct dpa400_header *hdr)
68 {
69         return hdr->t2 & 0x80;
70 }
71
72 static gboolean dpa400_read_packet(wtap *wth, FILE_T fh, wtap_rec *rec,
73     Buffer *buf, int *err, gchar **err_info)
74 {
75         guint8 chunk[2];
76         guint32 ctr = 0;
77
78         if (!wth || !rec || !buf)
79                 return FALSE;
80
81         if (!wtap_read_bytes_or_eof(fh, chunk, sizeof(chunk), err, err_info))
82                 return FALSE;
83
84         if (chunk[1] != 1) {
85                 *err = WTAP_ERR_BAD_FILE;
86                 *err_info = g_strdup("dpa400: malformed packet framing");
87                 return FALSE;
88         }
89
90         ws_buffer_clean(buf);
91
92         ws_buffer_append(buf, &chunk[0], 1);
93         ctr++;
94
95         switch (chunk[0]) {
96         case DPA400_STOP: {
97                 struct dpa400_header hdr;
98
99                 if (!dpa400_read_header(fh, &hdr, err, err_info))
100                         return FALSE;
101
102                 get_ts(&hdr, &rec->ts);
103
104                 rec->rec_type = REC_TYPE_PACKET;
105                 rec->presence_flags = WTAP_HAS_TS;
106                 rec->rec_header.packet_header.caplen = rec->rec_header.packet_header.len = 0;
107
108                 break;
109         }
110
111         case DPA400_START:
112         case DPA400_EVENT: {
113                 struct dpa400_header hdr;
114
115                 if (!dpa400_read_header(fh, &hdr, err, err_info))
116                         return FALSE;
117
118                 get_ts(&hdr, &rec->ts);
119
120                 if (!wtap_read_bytes_or_eof(fh, chunk, sizeof(chunk), err, err_info))
121                         return FALSE;
122
123                 if (chunk[1]) {
124                         *err = WTAP_ERR_BAD_FILE;
125                         *err_info = g_strdup("dpa400: malformed packet");
126                         return FALSE;
127                 }
128
129                 ws_buffer_append(buf, &chunk[0], 1);
130                 ctr++;
131
132                 rec->rec_type = REC_TYPE_PACKET;
133                 rec->presence_flags = WTAP_HAS_TS;
134                 rec->rec_header.packet_header.caplen = rec->rec_header.packet_header.len = ctr;
135
136                 break;
137         }
138
139         case DPA400_DATA: {
140                 struct dpa400_header hdr;
141                 guint8 from_source;
142
143                 if (!dpa400_read_header(fh, &hdr, err, err_info))
144                         return FALSE;
145
146                 get_ts(&hdr, &rec->ts);
147
148                 from_source = !get_from(&hdr);
149                 ws_buffer_append(buf, &from_source, 1);
150                 ctr++;
151
152                 while (1) {
153                         if (!wtap_read_bytes_or_eof(fh, chunk, sizeof(chunk), err, err_info))
154                                 return FALSE;
155
156                         if (chunk[1])
157                                 break;
158
159                         if (++ctr > WTAP_MAX_PACKET_SIZE_STANDARD) {
160                                 *err = WTAP_ERR_BAD_FILE;
161                                 *err_info = g_strdup_printf("dpa400: File has data record bigger than maximum of %u",
162                                         WTAP_MAX_PACKET_SIZE_STANDARD);
163                                 return FALSE;
164                         }
165
166                         ws_buffer_append(buf, &chunk[0], 1);
167                 }
168
169                 rec->rec_type = REC_TYPE_PACKET;
170                 rec->presence_flags = WTAP_HAS_TS;
171                 rec->rec_header.packet_header.caplen = rec->rec_header.packet_header.len = ctr;
172
173                 break;
174         }
175
176         case DPA400_TS_OVERFLOW: {
177                 get_ts_overflow(&rec->ts);
178
179                 rec->rec_type = REC_TYPE_PACKET;
180                 rec->presence_flags = WTAP_HAS_TS;
181                 rec->rec_header.packet_header.caplen = rec->rec_header.packet_header.len = ctr;
182
183                 break;
184         }
185
186         default:
187                 *err = WTAP_ERR_BAD_FILE;
188                 *err_info = g_strdup_printf("dpa400: unknown packet type %02x", chunk[0]);
189                 return FALSE;
190         }
191
192         return TRUE;
193 }
194
195 static gboolean dpa400_seek_read(wtap *wth, gint64 seek_off, wtap_rec *rec, Buffer *buf,
196     int *err, gchar **err_info)
197 {
198         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
199                 return FALSE;
200
201         return dpa400_read_packet(wth, wth->random_fh, rec, buf, err, err_info);
202 }
203
204 static gboolean dpa400_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
205 {
206         *data_offset = file_tell(wth->fh);
207
208         return dpa400_read_packet(wth, wth->fh, &wth->rec, wth->rec_data, err, err_info);
209 }
210
211 wtap_open_return_val dpa400_open(wtap *wth, int *err, gchar **err_info)
212 {
213         char magic[4];
214         const char dpa_magic[] = { 'D', 'B', 'F', 'R' };
215
216         /* Read in the number that should be at the start of a "dpa-400" file */
217         if (!wtap_read_bytes(wth->fh, &magic, sizeof magic, err, err_info)) {
218                 if (*err != WTAP_ERR_SHORT_READ)
219                         return WTAP_OPEN_ERROR;
220                 return WTAP_OPEN_NOT_MINE;
221         }
222
223         if (memcmp(magic, dpa_magic, sizeof(dpa_magic)))
224                 return WTAP_OPEN_NOT_MINE;
225
226         wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_DPA400;
227         wth->file_encap = WTAP_ENCAP_DPAUXMON;
228         wth->file_tsprec = WTAP_TSPREC_USEC;
229         wth->subtype_read = dpa400_read;
230         wth->subtype_seek_read = dpa400_seek_read;
231         wth->snapshot_length = 0;
232
233         return WTAP_OPEN_MINE;
234 }