cc38a621544104ee802c7cb586b6eba8bb19d22f
[metze/wireshark/wip.git] / wiretap / hcidump.c
1 /* hcidump.c
2  *
3  * Copyright (c) 2003 by Marcel Holtmann <marcel@holtmann.org>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #include "config.h"
21
22 #include "wtap-int.h"
23 #include "file_wrappers.h"
24 #include "buffer.h"
25 #include "hcidump.h"
26
27 struct dump_hdr {
28         guint16 len;
29         guint8  in;
30         guint8  pad;
31         guint32 ts_sec;
32         guint32 ts_usec;
33 };
34
35 #define DUMP_HDR_SIZE (sizeof(struct dump_hdr))
36
37 static gboolean hcidump_process_packet(FILE_T fh, struct wtap_pkthdr *phdr,
38     Buffer *buf, int *err, gchar **err_info)
39 {
40         struct dump_hdr dh;
41         int bytes_read, packet_size;
42
43         bytes_read = file_read(&dh, DUMP_HDR_SIZE, fh);
44         if (bytes_read != DUMP_HDR_SIZE) {
45                 *err = file_error(fh, err_info);
46                 if (*err == 0 && bytes_read != 0)
47                         *err = WTAP_ERR_SHORT_READ;
48                 return FALSE;
49         }
50
51         packet_size = GUINT16_FROM_LE(dh.len);
52         if (packet_size > WTAP_MAX_PACKET_SIZE) {
53                 /*
54                  * Probably a corrupt capture file; don't blow up trying
55                  * to allocate space for an immensely-large packet.
56                  */
57                 *err = WTAP_ERR_BAD_FILE;
58                 *err_info = g_strdup_printf("hcidump: File has %u-byte packet, bigger than maximum of %u",
59                         packet_size, WTAP_MAX_PACKET_SIZE);
60                 return FALSE;
61         }
62
63         phdr->presence_flags = WTAP_HAS_TS;
64         phdr->ts.secs = GUINT32_FROM_LE(dh.ts_sec);
65         phdr->ts.nsecs = GUINT32_FROM_LE(dh.ts_usec) * 1000;
66         phdr->caplen = packet_size;
67         phdr->len = packet_size;
68
69         phdr->pseudo_header.p2p.sent = (dh.in ? FALSE : TRUE);
70
71         return wtap_read_packet_bytes(fh, buf, packet_size, err, err_info);
72 }
73
74 static gboolean hcidump_read(wtap *wth, int *err, gchar **err_info,
75     gint64 *data_offset)
76 {
77         *data_offset = file_tell(wth->fh);
78
79         return hcidump_process_packet(wth->fh, &wth->phdr, wth->frame_buffer,
80             err, err_info);
81 }
82
83 static gboolean hcidump_seek_read(wtap *wth, gint64 seek_off,
84     struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
85 {
86         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
87                 return FALSE;
88
89         return hcidump_process_packet(wth->random_fh, phdr, buf, err, err_info);
90 }
91
92 int hcidump_open(wtap *wth, int *err, gchar **err_info)
93 {
94         struct dump_hdr dh;
95         guint8 type;
96         int bytes_read;
97
98         bytes_read = file_read(&dh, DUMP_HDR_SIZE, wth->fh);
99         if (bytes_read != DUMP_HDR_SIZE) {
100                 *err = file_error(wth->fh, err_info);
101                 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
102                         return -1;
103                 return 0;
104         }
105
106         if ((dh.in != 0 && dh.in != 1) || dh.pad != 0
107             || GUINT16_FROM_LE(dh.len) < 1)
108                 return 0;
109
110         bytes_read = file_read(&type, 1, wth->fh);
111         if (bytes_read != 1) {
112                 *err = file_error(wth->fh, err_info);
113                 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
114                         return -1;
115                 return 0;
116         }
117
118         if (type < 1 || type > 4)
119                 return 0;
120
121         if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
122                 return -1;
123
124         wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_HCIDUMP;
125         wth->file_encap = WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR;
126         wth->snapshot_length = 0;
127
128         wth->subtype_read = hcidump_read;
129         wth->subtype_seek_read = hcidump_seek_read;
130         wth->tsprecision = WTAP_FILE_TSPREC_USEC;
131
132         return 1;
133 }