kerberos dissect KERBEROS_AD_GSS_API_ETYPE_NEGOTIATION content
[metze/wireshark/wip.git] / wiretap / hcidump.c
1 /* hcidump.c
2  *
3  * Copyright (c) 2003 by Marcel Holtmann <marcel@holtmann.org>
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  */
7
8 #include "config.h"
9
10 #include "wtap-int.h"
11 #include "file_wrappers.h"
12 #include "hcidump.h"
13
14 struct dump_hdr {
15         guint16 len;
16         guint8  in;
17         guint8  pad;
18         guint32 ts_sec;
19         guint32 ts_usec;
20 };
21
22 #define DUMP_HDR_SIZE (sizeof(struct dump_hdr))
23
24 static gboolean hcidump_read_packet(FILE_T fh, wtap_rec *rec,
25     Buffer *buf, int *err, gchar **err_info)
26 {
27         struct dump_hdr dh;
28         int packet_size;
29
30         if (!wtap_read_bytes_or_eof(fh, &dh, DUMP_HDR_SIZE, err, err_info))
31                 return FALSE;
32
33         packet_size = GUINT16_FROM_LE(dh.len);
34         if (packet_size > WTAP_MAX_PACKET_SIZE_STANDARD) {
35                 /*
36                  * Probably a corrupt capture file; don't blow up trying
37                  * to allocate space for an immensely-large packet.
38                  */
39                 *err = WTAP_ERR_BAD_FILE;
40                 *err_info = g_strdup_printf("hcidump: File has %u-byte packet, bigger than maximum of %u",
41                         packet_size, WTAP_MAX_PACKET_SIZE_STANDARD);
42                 return FALSE;
43         }
44
45         rec->rec_type = REC_TYPE_PACKET;
46         rec->presence_flags = WTAP_HAS_TS;
47         rec->ts.secs = GUINT32_FROM_LE(dh.ts_sec);
48         rec->ts.nsecs = GUINT32_FROM_LE(dh.ts_usec) * 1000;
49         rec->rec_header.packet_header.caplen = packet_size;
50         rec->rec_header.packet_header.len = packet_size;
51
52         rec->rec_header.packet_header.pseudo_header.p2p.sent = (dh.in ? FALSE : TRUE);
53
54         return wtap_read_packet_bytes(fh, buf, packet_size, err, err_info);
55 }
56
57 static gboolean hcidump_read(wtap *wth, int *err, gchar **err_info,
58     gint64 *data_offset)
59 {
60         *data_offset = file_tell(wth->fh);
61
62         return hcidump_read_packet(wth->fh, &wth->rec, wth->rec_data,
63             err, err_info);
64 }
65
66 static gboolean hcidump_seek_read(wtap *wth, gint64 seek_off,
67     wtap_rec *rec, Buffer *buf, int *err, gchar **err_info)
68 {
69         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
70                 return FALSE;
71
72         return hcidump_read_packet(wth->random_fh, rec, buf, err, err_info);
73 }
74
75 wtap_open_return_val hcidump_open(wtap *wth, int *err, gchar **err_info)
76 {
77         struct dump_hdr dh;
78         guint8 type;
79
80         if (!wtap_read_bytes(wth->fh, &dh, DUMP_HDR_SIZE, err, err_info)) {
81                 if (*err != WTAP_ERR_SHORT_READ)
82                         return WTAP_OPEN_ERROR;
83                 return WTAP_OPEN_NOT_MINE;
84         }
85
86         if ((dh.in != 0 && dh.in != 1) || dh.pad != 0
87             || GUINT16_FROM_LE(dh.len) < 1)
88                 return WTAP_OPEN_NOT_MINE;
89
90         if (!wtap_read_bytes(wth->fh, &type, 1, err, err_info)) {
91                 if (*err != WTAP_ERR_SHORT_READ)
92                         return WTAP_OPEN_ERROR;
93                 return WTAP_OPEN_NOT_MINE;
94         }
95
96         if (type < 1 || type > 4)
97                 return WTAP_OPEN_NOT_MINE;
98
99         if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
100                 return WTAP_OPEN_ERROR;
101
102         wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_HCIDUMP;
103         wth->file_encap = WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR;
104         wth->snapshot_length = 0;
105
106         wth->subtype_read = hcidump_read;
107         wth->subtype_seek_read = hcidump_seek_read;
108         wth->file_tsprec = WTAP_TSPREC_USEC;
109
110         return WTAP_OPEN_MINE;
111 }
112
113 /*
114  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
115  *
116  * Local variables:
117  * c-basic-offset: 8
118  * tab-width: 8
119  * indent-tabs-mode: t
120  * End:
121  *
122  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
123  * :indentSize=8:tabSize=8:noTabs=false:
124  */