MSWSP: add two more Property Sets
[wireshark-wip.git] / epan / tvbuff_real.c
blobe6b277b927bbdfc3fb6ee886da3c584526413078
1 /* tvbuff_real.c
3 * $Id$
5 * Copyright (c) 2000 by Gilbert Ramirez <gram@alumni.rice.edu>
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "config.h"
28 #include <epan/emem.h>
30 #include "tvbuff.h"
31 #include "tvbuff-int.h"
32 #include "proto.h" /* XXX - only used for DISSECTOR_ASSERT, probably a new header file? */
33 #include "exceptions.h"
35 struct tvb_real {
36 struct tvbuff tvb;
38 /** Func to call when actually freed */
39 tvbuff_free_cb_t free_cb;
42 static void
43 real_free(tvbuff_t *tvb)
45 struct tvb_real *real_tvb = (struct tvb_real *) tvb;
47 if (real_tvb->free_cb) {
49 * XXX - do this with a union?
51 real_tvb->free_cb((gpointer)tvb->real_data);
55 static guint
56 real_offset(const tvbuff_t *tvb _U_, const guint counter)
58 return counter;
61 static const struct tvb_ops tvb_real_ops = {
62 sizeof(struct tvb_real), /* size */
64 real_free, /* free */
65 real_offset, /* offset */
66 NULL, /* get_ptr */
67 NULL, /* memcpy */
68 NULL, /* find_guint8 */
69 NULL, /* pbrk_guint8 */
70 NULL, /* clone */
73 tvbuff_t *
74 tvb_new_real_data(const guint8* data, const guint length, const gint reported_length)
76 tvbuff_t *tvb;
77 struct tvb_real *real_tvb;
79 THROW_ON(reported_length < -1, ReportedBoundsError);
81 tvb = tvb_new(&tvb_real_ops);
83 tvb->real_data = data;
84 tvb->length = length;
85 tvb->reported_length = reported_length;
86 tvb->initialized = TRUE;
89 * This is the top-level real tvbuff for this data source,
90 * so its data source tvbuff is itself.
92 tvb->ds_tvb = tvb;
94 real_tvb = (struct tvb_real *) tvb;
95 real_tvb->free_cb = NULL;
97 return tvb;
100 void
101 tvb_set_free_cb(tvbuff_t *tvb, const tvbuff_free_cb_t func)
103 struct tvb_real *real_tvb = (struct tvb_real *) tvb;
105 DISSECTOR_ASSERT(tvb);
106 DISSECTOR_ASSERT(tvb->ops == &tvb_real_ops);
107 real_tvb->free_cb = func;
110 void
111 tvb_set_child_real_data_tvbuff(tvbuff_t *parent, tvbuff_t *child)
113 DISSECTOR_ASSERT(parent && child);
114 DISSECTOR_ASSERT(parent->initialized);
115 DISSECTOR_ASSERT(child->initialized);
116 DISSECTOR_ASSERT(child->ops == &tvb_real_ops);
117 tvb_add_to_chain(parent, child);
120 tvbuff_t *
121 tvb_new_child_real_data(tvbuff_t *parent, const guint8* data, const guint length, const gint reported_length)
123 tvbuff_t *tvb = tvb_new_real_data(data, length, reported_length);
125 tvb_set_child_real_data_tvbuff(parent, tvb);
127 return tvb;