2 * Implements a tvbuff for frame
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <epan/packet.h>
28 #include <epan/tvbuff-int.h>
29 #include <epan/tvbuff.h>
31 #include "frame_tvbuff.h"
34 #include "wtap-int.h" /* for ->random_fh */
39 Buffer *buf; /* Packet data */
41 wtap *wth; /**< Wiretap session */
42 gint64 file_off; /**< File offset */
48 frame_read(struct tvb_frame *frame_tvb, struct wtap_pkthdr *phdr, Buffer *buf)
53 /* sanity check, capture file was closed? */
54 if (cfile.wth != frame_tvb->wth)
57 /* XXX, what if phdr->caplen isn't equal to
58 * frame_tvb->tvb.length + frame_tvb->offset?
60 if (!wtap_seek_read(frame_tvb->wth, frame_tvb->file_off, phdr, buf, &err, &err_info)) {
62 case WTAP_ERR_UNSUPPORTED_ENCAP:
63 case WTAP_ERR_BAD_FILE:
73 frame_cache(struct tvb_frame *frame_tvb)
75 struct wtap_pkthdr phdr; /* Packet header */
77 if (frame_tvb->buf == NULL) {
78 frame_tvb->buf = (struct Buffer *) g_malloc(sizeof(struct Buffer));
80 /* XXX, register frame_tvb to some list which frees from time to time not used buffers :] */
81 buffer_init(frame_tvb->buf, frame_tvb->tvb.length + frame_tvb->offset);
83 if (!frame_read(frame_tvb, &phdr, frame_tvb->buf))
84 { /* TODO: THROW(???); */ }
87 frame_tvb->tvb.real_data = buffer_start_ptr(frame_tvb->buf) + frame_tvb->offset;
91 frame_free(tvbuff_t *tvb)
93 struct tvb_frame *frame_tvb = (struct tvb_frame *) tvb;
96 buffer_free(frame_tvb->buf);
98 g_free(frame_tvb->buf);
102 static const guint8 *
103 frame_get_ptr(tvbuff_t *tvb, guint abs_offset, guint abs_length _U_)
105 struct tvb_frame *frame_tvb = (struct tvb_frame *) tvb;
107 frame_cache(frame_tvb);
109 return tvb->real_data + abs_offset;
113 frame_memcpy(tvbuff_t *tvb, void *target, guint abs_offset, guint abs_length)
115 struct tvb_frame *frame_tvb = (struct tvb_frame *) tvb;
117 frame_cache(frame_tvb);
119 return memcpy(target, tvb->real_data + abs_offset, abs_length);
123 frame_find_guint8(tvbuff_t *tvb, guint abs_offset, guint limit, guint8 needle)
125 struct tvb_frame *frame_tvb = (struct tvb_frame *) tvb;
126 const guint8 *result;
128 frame_cache(frame_tvb);
130 result = (const guint8 *)memchr(tvb->real_data + abs_offset, needle, limit);
132 return (gint) (result - tvb->real_data);
138 frame_pbrk_guint8(tvbuff_t *tvb, guint abs_offset, guint limit, const guint8 *needles, guchar *found_needle)
140 struct tvb_frame *frame_tvb = (struct tvb_frame *) tvb;
142 frame_cache(frame_tvb);
144 return tvb_pbrk_guint8(tvb, abs_offset, limit, needles, found_needle);
148 frame_offset(const tvbuff_t *tvb _U_, const guint counter)
150 /* XXX: frame_tvb->offset */
154 static tvbuff_t *frame_clone(tvbuff_t *tvb, guint abs_offset, guint abs_length);
156 static const struct tvb_ops tvb_frame_ops = {
157 sizeof(struct tvb_frame), /* size */
159 frame_free, /* free */
160 frame_offset, /* offset */
161 frame_get_ptr, /* get_ptr */
162 frame_memcpy, /* memcpy */
163 frame_find_guint8, /* find_guint8 */
164 frame_pbrk_guint8, /* pbrk_guint8 */
165 frame_clone, /* clone */
168 /* based on tvb_new_real_data() */
170 frame_tvbuff_new(const frame_data *fd, const guint8 *buf)
172 struct tvb_frame *frame_tvb;
175 tvb = tvb_new(&tvb_frame_ops);
178 * XXX - currently, the length arguments in
179 * tvbuff structure are signed, but the captured
180 * and reported length values are unsigned; this means
181 * that length values > 2^31 - 1 will appear as
184 * Captured length values that large will already
185 * have been filtered out by the Wiretap modules
186 * (the file will be reported as corrupted), to
187 * avoid trying to allocate large chunks of data.
189 * Reported length values will not have been
190 * filtered out, and should not be filtered out,
191 * as those lengths are not necessarily invalid.
193 * For now, we clip the reported length at G_MAXINT
195 * (XXX, is this still a problem?) There was an exception when we call
196 * tvb_new_real_data() now there's no one
199 tvb->real_data = buf;
200 tvb->length = fd->cap_len;
201 tvb->reported_length = fd->pkt_len > G_MAXINT ? G_MAXINT : fd->pkt_len;
202 tvb->initialized = TRUE;
205 * This is the top-level real tvbuff for this data source,
206 * so its data source tvbuff is itself.
210 frame_tvb = (struct tvb_frame *) tvb;
212 /* XXX, wtap_can_seek() */
213 if (cfile.wth && cfile.wth->random_fh
214 #ifdef WANT_PACKET_EDITOR
215 && fd->file_off != -1 /* generic clone for modified packets */
218 frame_tvb->wth = cfile.wth;
219 frame_tvb->file_off = fd->file_off;
220 frame_tvb->offset = 0;
222 frame_tvb->wth = NULL;
224 frame_tvb->buf = NULL;
230 frame_tvbuff_new_buffer(const frame_data *fd, Buffer *buf)
232 return frame_tvbuff_new(fd, buffer_start_ptr(buf));
236 frame_clone(tvbuff_t *tvb, guint abs_offset, guint abs_length)
238 struct tvb_frame *frame_tvb = (struct tvb_frame *) tvb;
240 tvbuff_t *cloned_tvb;
241 struct tvb_frame *cloned_frame_tvb;
243 /* file not seekable */
247 abs_offset += frame_tvb->offset;
249 cloned_tvb = tvb_new(&tvb_frame_ops);
251 /* data will be read when needed */
252 cloned_tvb->real_data = NULL;
253 cloned_tvb->length = abs_length;
254 cloned_tvb->reported_length = abs_length; /* XXX? */
255 cloned_tvb->initialized = TRUE;
258 * This is the top-level real tvbuff for this data source,
259 * so its data source tvbuff is itself.
261 cloned_tvb->ds_tvb = cloned_tvb;
263 cloned_frame_tvb = (struct tvb_frame *) cloned_tvb;
264 cloned_frame_tvb->wth = frame_tvb->wth;
265 cloned_frame_tvb->file_off = frame_tvb->file_off;
266 cloned_frame_tvb->offset = abs_offset;
267 cloned_frame_tvb->buf = NULL;
273 /* based on tvb_new_real_data() */
275 file_tvbuff_new(const frame_data *fd, const guint8 *buf)
277 struct tvb_frame *frame_tvb;
280 tvb = tvb_new(&tvb_frame_ops);
283 * XXX - currently, the length arguments in
284 * tvbuff structure are signed, but the captured
285 * and reported length values are unsigned; this means
286 * that length values > 2^31 - 1 will appear as
289 * Captured length values that large will already
290 * have been filtered out by the Wiretap modules
291 * (the file will be reported as corrupted), to
292 * avoid trying to allocate large chunks of data.
294 * Reported length values will not have been
295 * filtered out, and should not be filtered out,
296 * as those lengths are not necessarily invalid.
298 * For now, we clip the reported length at G_MAXINT
300 * (XXX, is this still a problem?) There was an exception when we call
301 * tvb_new_real_data() now there's no one
304 tvb->real_data = buf;
305 tvb->length = fd->cap_len;
306 tvb->reported_length = fd->pkt_len > G_MAXINT ? G_MAXINT : fd->pkt_len;
307 tvb->initialized = TRUE;
310 * This is the top-level real tvbuff for this data source,
311 * so its data source tvbuff is itself.
315 frame_tvb = (struct tvb_frame *) tvb;
317 /* XXX, wtap_can_seek() */
318 if (cfile.wth && cfile.wth->random_fh
319 #ifdef WANT_PACKET_EDITOR
320 && fd->file_off != -1 /* generic clone for modified packets */
323 frame_tvb->wth = cfile.wth;
324 frame_tvb->file_off = fd->file_off;
325 frame_tvb->offset = 0;
327 frame_tvb->wth = NULL;
329 frame_tvb->buf = NULL;
335 file_tvbuff_new_buffer(const frame_data *fd, Buffer *buf)
337 return frame_tvbuff_new(fd, buffer_start_ptr(buf));