1d77389b8234595cd8ee4ab4e9ef272e9529b2c4
[metze/wireshark/wip.git] / wiretap / hcidump.c
1 /* hcidump.c
2  *
3  * $Id$
4  *
5  * Copyright (c) 2003 by Marcel Holtmann <marcel@holtmann.org>
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include "config.h"
23
24 #include "wtap-int.h"
25 #include "file_wrappers.h"
26 #include "buffer.h"
27 #include "hcidump.h"
28
29 struct dump_hdr {
30         guint16 len;
31         guint8  in;
32         guint8  pad;
33         guint32 ts_sec;
34         guint32 ts_usec;
35 };
36
37 #define DUMP_HDR_SIZE (sizeof(struct dump_hdr))
38
39 static gboolean hcidump_process_packet(FILE_T fh, struct wtap_pkthdr *phdr,
40     Buffer *buf, int *err, gchar **err_info)
41 {
42         struct dump_hdr dh;
43         int bytes_read, packet_size;
44
45         bytes_read = file_read(&dh, DUMP_HDR_SIZE, fh);
46         if (bytes_read != DUMP_HDR_SIZE) {
47                 *err = file_error(fh, err_info);
48                 if (*err == 0 && bytes_read != 0)
49                         *err = WTAP_ERR_SHORT_READ;
50                 return FALSE;
51         }
52
53         packet_size = GUINT16_FROM_LE(dh.len);
54         if (packet_size > WTAP_MAX_PACKET_SIZE) {
55                 /*
56                  * Probably a corrupt capture file; don't blow up trying
57                  * to allocate space for an immensely-large packet.
58                  */
59                 *err = WTAP_ERR_BAD_FILE;
60                 *err_info = g_strdup_printf("hcidump: File has %u-byte packet, bigger than maximum of %u",
61                         packet_size, WTAP_MAX_PACKET_SIZE);
62                 return FALSE;
63         }
64
65         phdr->presence_flags = WTAP_HAS_TS;
66         phdr->ts.secs = GUINT32_FROM_LE(dh.ts_sec);
67         phdr->ts.nsecs = GUINT32_FROM_LE(dh.ts_usec) * 1000;
68         phdr->caplen = packet_size;
69         phdr->len = packet_size;
70
71         phdr->pseudo_header.p2p.sent = (dh.in ? FALSE : TRUE);
72
73         return wtap_read_packet_bytes(fh, buf, packet_size, err, err_info);
74 }
75
76 static gboolean hcidump_read(wtap *wth, int *err, gchar **err_info,
77     gint64 *data_offset)
78 {
79         *data_offset = file_tell(wth->fh);
80
81         return hcidump_process_packet(wth->fh, &wth->phdr, wth->frame_buffer,
82             err, err_info);
83 }
84
85 static gboolean hcidump_seek_read(wtap *wth, gint64 seek_off,
86     struct wtap_pkthdr *phdr, Buffer *buf, int length _U_,
87     int *err, gchar **err_info)
88 {
89         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
90                 return FALSE;
91
92         return hcidump_process_packet(wth->random_fh, phdr, buf, err, err_info);
93 }
94
95 int hcidump_open(wtap *wth, int *err, gchar **err_info)
96 {
97         struct dump_hdr dh;
98         guint8 type;
99         int bytes_read;
100
101         bytes_read = file_read(&dh, DUMP_HDR_SIZE, wth->fh);
102         if (bytes_read != DUMP_HDR_SIZE) {
103                 *err = file_error(wth->fh, err_info);
104                 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
105                         return -1;
106                 return 0;
107         }
108
109         if ((dh.in != 0 && dh.in != 1) || dh.pad != 0
110             || GUINT16_FROM_LE(dh.len) < 1)
111                 return 0;
112
113         bytes_read = file_read(&type, 1, wth->fh);
114         if (bytes_read != 1) {
115                 *err = file_error(wth->fh, err_info);
116                 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
117                         return -1;
118                 return 0;
119         }
120
121         if (type < 1 || type > 4)
122                 return 0;
123
124         if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
125                 return -1;
126
127         wth->file_type = WTAP_FILE_HCIDUMP;
128         wth->file_encap = WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR;
129         wth->snapshot_length = 0;
130
131         wth->subtype_read = hcidump_read;
132         wth->subtype_seek_read = hcidump_seek_read;
133         wth->tsprecision = WTAP_FILE_TSPREC_USEC;
134
135         return 1;
136 }