MAX_MCS_INDEX is a valid array index.
[metze/wireshark/wip.git] / epan / decode_as.c
1 /* decode_as.c
2  * Routines for dissector Decode As handlers
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #include "config.h"
24
25 #include <glib.h>
26
27 #include "decode_as.h"
28 #include "packet.h"
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 GList *decode_as_list = NULL;
33
34 void register_decode_as(decode_as_t* reg)
35 {
36     dissector_table_t decode_table;
37
38     /* Ensure valid functions */
39     DISSECTOR_ASSERT(reg->populate_list);
40     DISSECTOR_ASSERT(reg->reset_value);
41     DISSECTOR_ASSERT(reg->change_value);
42
43     /* Ensure the dissector table can't have duplicate protocols
44        that could confuse users */
45     decode_table = find_dissector_table(reg->table_name);
46     /* XXX - This should really be a DISSECTOR_ASSERT but a Bluetooth
47      * dissector is registering for "media_type" dissector table before it
48      * can be created
49      * There is also the "fake" DCE/RPC dissector table that needs to be fixed
50      */
51     if (decode_table != NULL)
52     {
53         /* FT_STRING can at least show the string value in the dialog, so don't penalize them */
54         if ((dissector_table_get_type(decode_table) != FT_STRING) &&
55             (dissector_table_get_proto_allowed(decode_table) != DISSECTOR_TABLE_NOT_ALLOW_DUPLICATE))
56         {
57             fprintf(stderr, "%s allows duplicates, which can lead to confuse in the Decode As dialog\n", reg->table_name);
58             if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
59                 abort();
60         }
61     }
62
63     decode_as_list = g_list_append(decode_as_list, reg);
64 }
65
66
67 struct decode_as_default_populate
68 {
69     decode_as_add_to_list_func add_to_list;
70     gpointer ui_element;
71 };
72
73 static void
74 decode_proto_add_to_list (const gchar *table_name, gpointer value, gpointer user_data)
75 {
76     struct decode_as_default_populate* populate = (struct decode_as_default_populate*)user_data;
77     const gchar     *proto_name;
78     gint       i;
79     dissector_handle_t handle;
80
81
82     handle = (dissector_handle_t)value;
83     proto_name = dissector_handle_get_short_name(handle);
84
85     i = dissector_handle_get_protocol_index(handle);
86     if (i >= 0 && !proto_is_protocol_enabled(find_protocol_by_id(i)))
87         return;
88
89     populate->add_to_list(table_name, proto_name, value, populate->ui_element);
90 }
91
92 void decode_as_default_populate_list(const gchar *table_name, decode_as_add_to_list_func add_to_list, gpointer ui_element)
93 {
94     struct decode_as_default_populate populate;
95
96     populate.add_to_list = add_to_list;
97     populate.ui_element = ui_element;
98
99     dissector_table_foreach_handle(table_name, decode_proto_add_to_list, &populate);
100 }
101
102 gboolean decode_as_default_reset(const char *name, const gpointer pattern)
103 {
104     switch (get_dissector_table_selector_type(name)) {
105     case FT_UINT8:
106     case FT_UINT16:
107     case FT_UINT24:
108     case FT_UINT32:
109         dissector_reset_uint(name, GPOINTER_TO_UINT(pattern));
110         return TRUE;
111     case FT_STRING:
112     case FT_STRINGZ:
113     case FT_UINT_STRING:
114     case FT_STRINGZPAD:
115         dissector_reset_string(name, (!pattern)?"":(gchar *) pattern);
116         return TRUE;
117     default:
118         return FALSE;
119     };
120
121     return TRUE;
122 }
123
124 gboolean decode_as_default_change(const char *name, const gpointer pattern, gpointer handle, gchar* list_name _U_)
125 {
126     dissector_handle_t* dissector = (dissector_handle_t*)handle;
127     if (dissector != NULL) {
128         switch (get_dissector_table_selector_type(name)) {
129         case FT_UINT8:
130         case FT_UINT16:
131         case FT_UINT24:
132         case FT_UINT32:
133             dissector_change_uint(name, GPOINTER_TO_UINT(pattern), *dissector);
134             return TRUE;
135         case FT_STRING:
136         case FT_STRINGZ:
137         case FT_UINT_STRING:
138         case FT_STRINGZPAD:
139             dissector_change_string(name, (!pattern)?"":(gchar *) pattern, *dissector);
140             return TRUE;
141         default:
142             return FALSE;
143         };
144
145         return FALSE;
146     }
147
148     return TRUE;
149 }
150
151 /*
152  * Editor modelines
153  *
154  * Local Variables:
155  * c-basic-offset: 4
156  * tab-width: 8
157  * indent-tabs-mode: nil
158  * End:
159  *
160  * ex: set shiftwidth=4 tabstop=8 expandtab:
161  * :indentSize=4:tabSize=8:noTabs=true:
162  */