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_packet(FILE_T fh
, struct wtap_pkthdr
*phdr
,
40 Buffer
*buf
, 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
);
73 return wtap_read_packet_bytes(fh
, buf
, packet_size
, err
, err_info
);
76 static gboolean
hcidump_read(wtap
*wth
, int *err
, gchar
**err_info
,
79 *data_offset
= file_tell(wth
->fh
);
81 return hcidump_process_packet(wth
->fh
, &wth
->phdr
, wth
->frame_buffer
,
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
)
89 if (file_seek(wth
->random_fh
, seek_off
, SEEK_SET
, err
) == -1)
92 return hcidump_process_packet(wth
->random_fh
, phdr
, buf
, err
, err_info
);
95 int hcidump_open(wtap
*wth
, int *err
, gchar
**err_info
)
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
)
109 if ((dh
.in
!= 0 && dh
.in
!= 1) || dh
.pad
!= 0
110 || GUINT16_FROM_LE(dh
.len
) < 1)
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
)
121 if (type
< 1 || type
> 4)
124 if (file_seek(wth
->fh
, 0, SEEK_SET
, err
) == -1)
127 wth
->file_type_subtype
= WTAP_FILE_TYPE_SUBTYPE_HCIDUMP
;
128 wth
->file_encap
= WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR
;
129 wth
->snapshot_length
= 0;
131 wth
->subtype_read
= hcidump_read
;
132 wth
->subtype_seek_read
= hcidump_seek_read
;
133 wth
->tsprecision
= WTAP_FILE_TSPREC_USEC
;