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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include "file_wrappers.h"
39 #define DUMP_HDR_SIZE (sizeof(struct dump_hdr))
41 static gboolean hcidump_read(wtap *wth, int *err, gchar **err_info,
46 int bytes_read, packet_size;
48 *data_offset = wth->data_offset;
50 bytes_read = file_read(&dh, 1, DUMP_HDR_SIZE, wth->fh);
51 if (bytes_read != DUMP_HDR_SIZE) {
52 *err = file_error(wth->fh);
53 if (*err == 0 && bytes_read != 0)
54 *err = WTAP_ERR_SHORT_READ;
57 wth->data_offset += DUMP_HDR_SIZE;
59 packet_size = GUINT16_FROM_LE(dh.len);
60 if (packet_size > WTAP_MAX_PACKET_SIZE) {
62 * Probably a corrupt capture file; don't blow up trying
63 * to allocate space for an immensely-large packet.
65 *err = WTAP_ERR_BAD_RECORD;
66 *err_info = g_strdup_printf("hcidump: File has %u-byte packet, bigger than maximum of %u",
67 packet_size, WTAP_MAX_PACKET_SIZE);
71 buffer_assure_space(wth->frame_buffer, packet_size);
72 buf = buffer_start_ptr(wth->frame_buffer);
74 bytes_read = file_read(buf, 1, packet_size, wth->fh);
75 if (bytes_read != packet_size) {
76 *err = file_error(wth->fh);
78 *err = WTAP_ERR_SHORT_READ;
81 wth->data_offset += packet_size;
83 wth->phdr.ts.secs = GUINT32_FROM_LE(dh.ts_sec);
84 wth->phdr.ts.nsecs = GUINT32_FROM_LE(dh.ts_usec) * 1000;
85 wth->phdr.caplen = packet_size;
86 wth->phdr.len = packet_size;
88 wth->pseudo_header.p2p.sent = (dh.in ? FALSE : TRUE);
93 static gboolean hcidump_seek_read(wtap *wth, gint64 seek_off,
94 union wtap_pseudo_header *pseudo_header, guint8 *pd, int length,
95 int *err, gchar **err_info _U_)
100 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
103 bytes_read = file_read(&dh, 1, DUMP_HDR_SIZE, wth->random_fh);
104 if (bytes_read != DUMP_HDR_SIZE) {
105 *err = file_error(wth->random_fh);
106 if (*err == 0 && bytes_read != 0)
107 *err = WTAP_ERR_SHORT_READ;
111 bytes_read = file_read(pd, 1, length, wth->random_fh);
112 if (bytes_read != length) {
113 *err = file_error(wth->random_fh);
115 *err = WTAP_ERR_SHORT_READ;
119 pseudo_header->p2p.sent = (dh.in ? FALSE : TRUE);
124 int hcidump_open(wtap *wth, int *err, gchar **err_info _U_)
130 bytes_read = file_read(&dh, 1, DUMP_HDR_SIZE, wth->fh);
131 if (bytes_read != DUMP_HDR_SIZE) {
132 *err = file_error(wth->fh);
133 return (*err != 0) ? -1 : 0;
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, 1, wth->fh);
141 if (bytes_read != 1) {
142 *err = file_error(wth->fh);
143 return (*err != 0) ? -1 : 0;
146 if (type < 1 || type > 4)
149 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
152 wth->file_type = WTAP_FILE_HCIDUMP;
153 wth->file_encap = WTAP_ENCAP_BLUETOOTH_H4;
154 wth->snapshot_length = 0;
156 wth->subtype_read = hcidump_read;
157 wth->subtype_seek_read = hcidump_seek_read;
158 wth->tsprecision = WTAP_FILE_TSPREC_USEC;