emem -> wmem
[metze/wireshark/wip.git] / epan / tvbuff_real.c
1 /* tvbuff_real.c
2  *
3  * $Id$
4  *
5  * Copyright (c) 2000 by Gilbert Ramirez <gram@alumni.rice.edu>
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
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.
15  *
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.
20  *
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.
24  */
25
26 #include "config.h"
27
28 #include "tvbuff.h"
29 #include "tvbuff-int.h"
30 #include "proto.h"      /* XXX - only used for DISSECTOR_ASSERT, probably a new header file? */
31
32 struct tvb_real {
33         struct tvbuff tvb;
34
35         /** Func to call when actually freed */
36         tvbuff_free_cb_t        free_cb;
37 };
38
39 static void
40 real_free(tvbuff_t *tvb)
41 {
42         struct tvb_real *real_tvb = (struct tvb_real *) tvb;
43
44         if (real_tvb->free_cb) {
45                 /*
46                  * XXX - do this with a union?
47                  */
48                 real_tvb->free_cb((gpointer)tvb->real_data);
49         }
50 }
51
52 static guint
53 real_offset(const tvbuff_t *tvb _U_, const guint counter)
54 {
55         return counter;
56 }
57
58 static const struct tvb_ops tvb_real_ops = {
59         sizeof(struct tvb_real), /* size */
60
61         real_free,            /* free */
62         real_offset,          /* offset */
63         NULL,                 /* get_ptr */
64         NULL,                 /* memcpy */
65         NULL,                 /* find_guint8 */
66         NULL,                 /* pbrk_guint8 */
67         NULL,                 /* clone */
68 };
69
70 tvbuff_t *
71 tvb_new_real_data(const guint8* data, const guint length, const gint reported_length)
72 {
73         tvbuff_t *tvb;
74         struct tvb_real *real_tvb;
75
76         THROW_ON(reported_length < -1, ReportedBoundsError);
77
78         tvb = tvb_new(&tvb_real_ops);
79
80         tvb->real_data       = data;
81         tvb->length          = length;
82         tvb->reported_length = reported_length;
83         tvb->initialized     = TRUE;
84
85         /*
86          * This is the top-level real tvbuff for this data source,
87          * so its data source tvbuff is itself.
88          */
89         tvb->ds_tvb = tvb;
90
91         real_tvb = (struct tvb_real *) tvb;
92         real_tvb->free_cb = NULL;
93
94         return tvb;
95 }
96
97 void
98 tvb_set_free_cb(tvbuff_t *tvb, const tvbuff_free_cb_t func)
99 {
100         struct tvb_real *real_tvb = (struct tvb_real *) tvb;
101
102         DISSECTOR_ASSERT(tvb);
103         DISSECTOR_ASSERT(tvb->ops == &tvb_real_ops);
104         real_tvb->free_cb = func;
105 }
106
107 void
108 tvb_set_child_real_data_tvbuff(tvbuff_t *parent, tvbuff_t *child)
109 {
110         DISSECTOR_ASSERT(parent && child);
111         DISSECTOR_ASSERT(parent->initialized);
112         DISSECTOR_ASSERT(child->initialized);
113         DISSECTOR_ASSERT(child->ops == &tvb_real_ops);
114         tvb_add_to_chain(parent, child);
115 }
116
117 tvbuff_t *
118 tvb_new_child_real_data(tvbuff_t *parent, const guint8* data, const guint length, const gint reported_length)
119 {
120         tvbuff_t *tvb = tvb_new_real_data(data, length, reported_length);
121
122         tvb_set_child_real_data_tvbuff(parent, tvb);
123
124         return tvb;
125 }