/src/wireshark/epan/tvbuff_real.c
Line | Count | Source |
1 | | /* tvbuff_real.c |
2 | | * |
3 | | * Copyright (c) 2000 by Gilbert Ramirez <gram@alumni.rice.edu> |
4 | | * |
5 | | * Wireshark - Network traffic analyzer |
6 | | * By Gerald Combs <gerald@wireshark.org> |
7 | | * Copyright 1998 Gerald Combs |
8 | | * |
9 | | * SPDX-License-Identifier: GPL-2.0-or-later |
10 | | */ |
11 | | |
12 | | #include "config.h" |
13 | | |
14 | | #include "tvbuff.h" |
15 | | #include "tvbuff-int.h" |
16 | | #include "proto.h" /* XXX - only used for DISSECTOR_ASSERT, probably a new header file? */ |
17 | | #include "exceptions.h" |
18 | | |
19 | | struct tvb_real { |
20 | | struct tvbuff tvb; |
21 | | |
22 | | /** Func to call when actually freed */ |
23 | | tvbuff_free_cb_t free_cb; |
24 | | }; |
25 | | |
26 | | static void |
27 | | real_free(tvbuff_t *tvb) |
28 | 883k | { |
29 | 883k | struct tvb_real *real_tvb = (struct tvb_real *) tvb; |
30 | | |
31 | 883k | if (real_tvb->free_cb) { |
32 | | /* |
33 | | * XXX - do this with a union? |
34 | | */ |
35 | 392k | real_tvb->free_cb((void *)tvb->real_data); |
36 | 392k | } |
37 | 883k | } |
38 | | |
39 | | static unsigned |
40 | | real_offset(const tvbuff_t *tvb _U_, const unsigned counter) |
41 | 723k | { |
42 | 723k | return counter; |
43 | 723k | } |
44 | | |
45 | | static const struct tvb_ops tvb_real_ops = { |
46 | | sizeof(struct tvb_real), /* size */ |
47 | | |
48 | | real_free, /* free */ |
49 | | real_offset, /* offset */ |
50 | | NULL, /* get_ptr */ |
51 | | NULL, /* memcpy */ |
52 | | NULL, /* find_uint8 */ |
53 | | NULL, /* pbrk_uint8 */ |
54 | | NULL, /* clone */ |
55 | | }; |
56 | | |
57 | | tvbuff_t * |
58 | | tvb_new_real_data(const uint8_t* data, const unsigned length, const unsigned reported_length) |
59 | 904k | { |
60 | 904k | tvbuff_t *tvb; |
61 | 904k | struct tvb_real *real_tvb; |
62 | | |
63 | 904k | tvb = tvb_new(&tvb_real_ops); |
64 | | |
65 | 904k | tvb->real_data = data; |
66 | 904k | tvb->length = length; |
67 | 904k | tvb->reported_length = reported_length; |
68 | 904k | tvb->contained_length = reported_length; |
69 | 904k | tvb->initialized = true; |
70 | | |
71 | | /* |
72 | | * This is the top-level real tvbuff for this data source, |
73 | | * so its data source tvbuff is itself. |
74 | | */ |
75 | 904k | tvb->ds_tvb = tvb; |
76 | | |
77 | 904k | real_tvb = (struct tvb_real *) tvb; |
78 | 904k | real_tvb->free_cb = NULL; |
79 | | |
80 | 904k | return tvb; |
81 | 904k | } |
82 | | |
83 | | void |
84 | | tvb_set_free_cb(tvbuff_t *tvb, const tvbuff_free_cb_t func) |
85 | 417k | { |
86 | 417k | struct tvb_real *real_tvb = (struct tvb_real *) tvb; |
87 | | |
88 | 417k | DISSECTOR_ASSERT(tvb); |
89 | 417k | DISSECTOR_ASSERT(tvb->ops == &tvb_real_ops); |
90 | 417k | real_tvb->free_cb = func; |
91 | 417k | } |
92 | | |
93 | | void |
94 | | tvb_set_child_real_data_tvbuff(tvbuff_t *parent, tvbuff_t *child) |
95 | 637k | { |
96 | 637k | DISSECTOR_ASSERT(parent && child); |
97 | 637k | DISSECTOR_ASSERT(parent->initialized); |
98 | 637k | DISSECTOR_ASSERT(child->initialized); |
99 | 637k | DISSECTOR_ASSERT(child->ops == &tvb_real_ops); |
100 | 637k | tvb_add_to_chain(parent, child); |
101 | 637k | } |
102 | | |
103 | | tvbuff_t * |
104 | | tvb_new_child_real_data(tvbuff_t *parent, const uint8_t* data, const unsigned length, const unsigned reported_length) |
105 | 636k | { |
106 | 636k | tvbuff_t *tvb = tvb_new_real_data(data, length, reported_length); |
107 | | |
108 | 636k | tvb_set_child_real_data_tvbuff(parent, tvb); |
109 | | |
110 | 636k | return tvb; |
111 | 636k | } |
112 | | |
113 | | /* |
114 | | * Editor modelines - https://www.wireshark.org/tools/modelines.html |
115 | | * |
116 | | * Local variables: |
117 | | * c-basic-offset: 8 |
118 | | * tab-width: 8 |
119 | | * indent-tabs-mode: t |
120 | | * End: |
121 | | * |
122 | | * vi: set shiftwidth=8 tabstop=8 noexpandtab: |
123 | | * :indentSize=8:tabSize=8:noTabs=false: |
124 | | */ |