3 * Copyright (c) 2003 by Marcel Holtmann <marcel@holtmann.org>
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.
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.
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.
23 #include "file_wrappers.h"
35 #define DUMP_HDR_SIZE (sizeof(struct dump_hdr))
37 static gboolean hcidump_process_packet(FILE_T fh, struct wtap_pkthdr *phdr,
38 Buffer *buf, int *err, gchar **err_info)
41 int bytes_read, packet_size;
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;
51 packet_size = GUINT16_FROM_LE(dh.len);
52 if (packet_size > WTAP_MAX_PACKET_SIZE) {
54 * Probably a corrupt capture file; don't blow up trying
55 * to allocate space for an immensely-large packet.
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);
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;
69 phdr->pseudo_header.p2p.sent = (dh.in ? FALSE : TRUE);
71 return wtap_read_packet_bytes(fh, buf, packet_size, err, err_info);
74 static gboolean hcidump_read(wtap *wth, int *err, gchar **err_info,
77 *data_offset = file_tell(wth->fh);
79 return hcidump_process_packet(wth->fh, &wth->phdr, wth->frame_buffer,
83 static gboolean hcidump_seek_read(wtap *wth, gint64 seek_off,
84 struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
86 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
89 return hcidump_process_packet(wth->random_fh, phdr, buf, err, err_info);
92 int hcidump_open(wtap *wth, int *err, gchar **err_info)
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)
106 if ((dh.in != 0 && dh.in != 1) || dh.pad != 0
107 || GUINT16_FROM_LE(dh.len) < 1)
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)
118 if (type < 1 || type > 4)
121 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
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;
128 wth->subtype_read = hcidump_read;
129 wth->subtype_seek_read = hcidump_seek_read;
130 wth->tsprecision = WTAP_FILE_TSPREC_USEC;