Don't do fcn calls in arg of g_?to??(); Macro may very well eval args multiple times.
[obnox/wireshark/wip.git] / epan / dissectors / packet-rmt-common.c
1 /* packet-rmt-common.c
2  * Reliable Multicast Transport (RMT)
3  * Common RMT functions
4  * Copyright 2005, Stefano Pettini <spettini@users.sourceforge.net>
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include <glib.h>
35
36 #include <epan/packet.h>
37 #include <epan/strutil.h>
38
39 #include "packet-rmt-common.h"
40
41 /* Common RMT exported functions */
42 /* ============================= */
43
44 /* Scan the tvb and put extensions found in an array */
45 void rmt_ext_parse(GArray *a, tvbuff_t *tvb, guint *offset, guint offset_max)
46 {
47         struct _ext e;
48
49         while (*offset < offset_max)
50         {
51                 /* Clear the temporary extension */
52                 memset(&e, 0, sizeof(struct _ext));
53
54                 /* Dissect the extension */
55                 e.offset = *offset;
56                 e.het = tvb_get_guint8(tvb, *offset);
57
58                 if (e.het <= 127) {
59                         /* If HET <= 127, we have a variable-size extension */
60                         e.hel = tvb_get_guint8(tvb, *offset+1);
61                         e.hec_offset = *offset + 2;
62                         e.hec_size = e.hel * 4 - 2;
63                         e.length = e.hel * 4;
64                 } else {
65                         /* If HET > 127, we have a short 32-bit extension */
66                         e.hel = 1;      /* even if HEL field is not defined for HET > 127 */
67                         e.hec_offset = *offset + 1;
68                         e.hec_size = 3;
69                         e.length = 4;
70                 }
71
72                 /* Prevents infinite loops */
73                 if (e.length == 0)
74                         break;
75
76                 g_array_append_val(a, e);
77                 *offset += e.length;
78         }
79 }
80
81 /* Add default items to a subtree */
82 void rmt_ext_decode_default_header(struct _ext *e, tvbuff_t *tvb, proto_tree *tree)
83 {
84         if (tree)
85         {
86                 proto_tree_add_text(tree, tvb, e->offset, 1, "Header Extension Type (HET): %u", e->het);
87                 if (e->het <= 127)
88                         proto_tree_add_text(tree, tvb, e->offset+1, 1, "Header Extension Length (HEL): %u", e->hel);
89         }
90 }
91
92 /* Add a default subtree to a tree item */
93 void rmt_ext_decode_default_subtree(struct _ext *e, tvbuff_t *tvb, proto_item *ti, gint ett)
94 {
95         proto_tree *ext_tree;
96
97         ext_tree = proto_item_add_subtree(ti, ett);
98         rmt_ext_decode_default_header(e, tvb, ext_tree);
99
100         if (ext_tree)
101                 proto_tree_add_text(ext_tree, tvb, e->hec_offset, e->hec_size,
102                         "Header Extension Content (HEC): %s", tvb_bytes_to_str(tvb, e->hec_offset, e->hec_size));
103 }
104
105 /* Add a default subtree for unknown extensions */
106 void rmt_ext_decode_default(struct _ext *e, tvbuff_t *tvb, proto_tree *tree, gint ett)
107 {
108         proto_item *ti;
109
110         if (tree)
111         {
112                 ti = proto_tree_add_text(tree, tvb, e->offset, e->length,
113                         "Unknown extension (%u)", e->het);
114
115                 rmt_ext_decode_default_subtree(e, tvb, ti, ett);
116         }
117 }