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_read(wtap *wth, int *err, gchar **err_info,
44 int bytes_read, packet_size;
46 *data_offset = file_tell(wth->fh);
48 bytes_read = file_read(&dh, DUMP_HDR_SIZE, wth->fh);
49 if (bytes_read != DUMP_HDR_SIZE) {
50 *err = file_error(wth->fh, err_info);
51 if (*err == 0 && bytes_read != 0)
52 *err = WTAP_ERR_SHORT_READ;
56 packet_size = GUINT16_FROM_LE(dh.len);
57 if (packet_size > WTAP_MAX_PACKET_SIZE) {
59 * Probably a corrupt capture file; don't blow up trying
60 * to allocate space for an immensely-large packet.
62 *err = WTAP_ERR_BAD_FILE;
63 *err_info = g_strdup_printf("hcidump: File has %u-byte packet, bigger than maximum of %u",
64 packet_size, WTAP_MAX_PACKET_SIZE);
68 buffer_assure_space(wth->frame_buffer, packet_size);
69 buf = buffer_start_ptr(wth->frame_buffer);
71 bytes_read = file_read(buf, packet_size, wth->fh);
72 if (bytes_read != packet_size) {
73 *err = file_error(wth->fh, err_info);
75 *err = WTAP_ERR_SHORT_READ;
79 wth->phdr.presence_flags = WTAP_HAS_TS;
80 wth->phdr.ts.secs = GUINT32_FROM_LE(dh.ts_sec);
81 wth->phdr.ts.nsecs = GUINT32_FROM_LE(dh.ts_usec) * 1000;
82 wth->phdr.caplen = packet_size;
83 wth->phdr.len = packet_size;
85 wth->phdr.pseudo_header.p2p.sent = (dh.in ? FALSE : TRUE);
90 static gboolean hcidump_seek_read(wtap *wth, gint64 seek_off,
91 struct wtap_pkthdr *phdr, guint8 *pd, int length,
92 int *err, gchar **err_info)
94 union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
98 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
101 bytes_read = file_read(&dh, DUMP_HDR_SIZE, wth->random_fh);
102 if (bytes_read != DUMP_HDR_SIZE) {
103 *err = file_error(wth->random_fh, err_info);
104 if (*err == 0 && bytes_read != 0)
105 *err = WTAP_ERR_SHORT_READ;
109 bytes_read = file_read(pd, length, wth->random_fh);
110 if (bytes_read != length) {
111 *err = file_error(wth->random_fh, err_info);
113 *err = WTAP_ERR_SHORT_READ;
117 pseudo_header->p2p.sent = (dh.in ? FALSE : TRUE);
122 int hcidump_open(wtap *wth, int *err, gchar **err_info)
128 bytes_read = file_read(&dh, DUMP_HDR_SIZE, wth->fh);
129 if (bytes_read != DUMP_HDR_SIZE) {
130 *err = file_error(wth->fh, err_info);
131 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
136 if ((dh.in != 0 && dh.in != 1) || dh.pad != 0
137 || GUINT16_FROM_LE(dh.len) < 1)
140 bytes_read = file_read(&type, 1, wth->fh);
141 if (bytes_read != 1) {
142 *err = file_error(wth->fh, err_info);
143 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
148 if (type < 1 || type > 4)
151 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
154 wth->file_type = WTAP_FILE_HCIDUMP;
155 wth->file_encap = WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR;
156 wth->snapshot_length = 0;
158 wth->subtype_read = hcidump_read;
159 wth->subtype_seek_read = hcidump_seek_read;
160 wth->tsprecision = WTAP_FILE_TSPREC_USEC;