Have the seek-read routines take a Buffer rather than a guint8 pointer
[metze/wireshark/wip.git] / wiretap / hcidump.c
1 /* hcidump.c
2  *
3  * $Id$
4  *
5  * Copyright (c) 2003 by Marcel Holtmann <marcel@holtmann.org>
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include "config.h"
23
24 #include "wtap-int.h"
25 #include "file_wrappers.h"
26 #include "buffer.h"
27 #include "hcidump.h"
28
29 struct dump_hdr {
30         guint16 len;
31         guint8  in;
32         guint8  pad;
33         guint32 ts_sec;
34         guint32 ts_usec;
35 };
36
37 #define DUMP_HDR_SIZE (sizeof(struct dump_hdr))
38
39 static gboolean hcidump_process_header(FILE_T fh, struct wtap_pkthdr *phdr,
40     int *err, gchar **err_info)
41 {
42         struct dump_hdr dh;
43         int bytes_read, packet_size;
44
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;
50                 return FALSE;
51         }
52
53         packet_size = GUINT16_FROM_LE(dh.len);
54         if (packet_size > WTAP_MAX_PACKET_SIZE) {
55                 /*
56                  * Probably a corrupt capture file; don't blow up trying
57                  * to allocate space for an immensely-large packet.
58                  */
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);
62                 return FALSE;
63         }
64
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;
70
71         phdr->pseudo_header.p2p.sent = (dh.in ? FALSE : TRUE);
72
73         return TRUE;
74 }
75
76 static gboolean hcidump_read(wtap *wth, int *err, gchar **err_info,
77     gint64 *data_offset)
78 {
79         *data_offset = file_tell(wth->fh);
80
81         if (!hcidump_process_header(wth->fh, &wth->phdr, err, err_info))
82                 return FALSE;
83
84         return wtap_read_packet_bytes(wth->fh, wth->frame_buffer,
85             wth->phdr.caplen, err, err_info);
86 }
87
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)
91 {
92         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
93                 return FALSE;
94
95         if (!hcidump_process_header(wth->random_fh, phdr, err, err_info))
96                 return FALSE;
97
98         return wtap_read_packet_bytes(wth->random_fh, buf, length,
99             err, err_info);
100 }
101
102 int hcidump_open(wtap *wth, int *err, gchar **err_info)
103 {
104         struct dump_hdr dh;
105         guint8 type;
106         int bytes_read;
107
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)
112                         return -1;
113                 return 0;
114         }
115
116         if ((dh.in != 0 && dh.in != 1) || dh.pad != 0
117             || GUINT16_FROM_LE(dh.len) < 1)
118                 return 0;
119
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)
124                         return -1;
125                 return 0;
126         }
127
128         if (type < 1 || type > 4)
129                 return 0;
130
131         if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
132                 return -1;
133
134         wth->file_type = WTAP_FILE_HCIDUMP;
135         wth->file_encap = WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR;
136         wth->snapshot_length = 0;
137
138         wth->subtype_read = hcidump_read;
139         wth->subtype_seek_read = hcidump_seek_read;
140         wth->tsprecision = WTAP_FILE_TSPREC_USEC;
141
142         return 1;
143 }