5 * Copyright (c) 2003 by Marcel Holtmann <marcel@holtmann.org>
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.
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.
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.
25 #include "file_wrappers.h"
37 #define DUMP_HDR_SIZE (sizeof(struct dump_hdr))
39 static gboolean hcidump_process_header(FILE_T fh, struct wtap_pkthdr *phdr,
40 int *err, gchar **err_info)
43 int bytes_read, packet_size;
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;
53 packet_size = GUINT16_FROM_LE(dh.len);
54 if (packet_size > WTAP_MAX_PACKET_SIZE) {
56 * Probably a corrupt capture file; don't blow up trying
57 * to allocate space for an immensely-large packet.
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);
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;
71 phdr->pseudo_header.p2p.sent = (dh.in ? FALSE : TRUE);
76 static gboolean hcidump_read(wtap *wth, int *err, gchar **err_info,
79 *data_offset = file_tell(wth->fh);
81 if (!hcidump_process_header(wth->fh, &wth->phdr, err, err_info))
84 return wtap_read_packet_bytes(wth->fh, wth->frame_buffer,
85 wth->phdr.caplen, err, err_info);
88 static gboolean hcidump_seek_read(wtap *wth, gint64 seek_off,
89 struct wtap_pkthdr *phdr, Buffer *buf, int length,
90 int *err, gchar **err_info)
92 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
95 if (!hcidump_process_header(wth->random_fh, phdr, err, err_info))
98 return wtap_read_packet_bytes(wth->random_fh, buf, length,
102 int hcidump_open(wtap *wth, int *err, gchar **err_info)
108 bytes_read = file_read(&dh, DUMP_HDR_SIZE, wth->fh);
109 if (bytes_read != DUMP_HDR_SIZE) {
110 *err = file_error(wth->fh, err_info);
111 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
116 if ((dh.in != 0 && dh.in != 1) || dh.pad != 0
117 || GUINT16_FROM_LE(dh.len) < 1)
120 bytes_read = file_read(&type, 1, wth->fh);
121 if (bytes_read != 1) {
122 *err = file_error(wth->fh, err_info);
123 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
128 if (type < 1 || type > 4)
131 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
134 wth->file_type = WTAP_FILE_HCIDUMP;
135 wth->file_encap = WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR;
136 wth->snapshot_length = 0;
138 wth->subtype_read = hcidump_read;
139 wth->subtype_seek_read = hcidump_seek_read;
140 wth->tsprecision = WTAP_FILE_TSPREC_USEC;