2 * Routines for value_strings
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 #include "value_string.h"
35 /* Tries to match val against each element in the value_string array vs.
36 Returns the associated string ptr on a match.
37 Formats val with fmt, and returns the resulting string, on failure. */
39 val_to_str(guint32 val, const value_string *vs, const char *fmt) {
42 g_assert(fmt != NULL);
44 ret = match_strval(val, vs);
48 return ep_strdup_printf(fmt, val);
51 /* Tries to match val against each element in the value_string array vs.
52 Returns the associated string ptr, and sets "*idx" to the index in
53 that table, on a match, and returns NULL, and sets "*idx" to -1,
56 match_strval_idx(guint32 val, const value_string *vs, gint *idx) {
59 while (vs[i].strptr) {
60 if (vs[i].value == val) {
71 /* Like match_strval_idx(), but doesn't return the index. */
73 match_strval(guint32 val, const value_string *vs) {
75 return match_strval_idx(val, vs, &ignore_me);
78 /* Generate a string describing an enumerated bitfield (an N-bit field
79 with various specific values having particular names). */
81 decode_enumerated_bitfield(guint32 val, guint32 mask, int width,
82 const value_string *tab, const char *fmt)
84 static char buf[1025];
87 p = decode_bitfield_value(buf, val, mask, width);
88 g_snprintf(p, 1024-(p-buf), fmt, val_to_str(val & mask, tab, "Unknown"));
93 /* Generate a string describing an enumerated bitfield (an N-bit field
94 with various specific values having particular names). */
96 decode_enumerated_bitfield_shifted(guint32 val, guint32 mask, int width,
97 const value_string *tab, const char *fmt)
99 static char buf[1025];
103 /* Compute the number of bits we have to shift the bitfield right
104 to extract its value. */
105 while ((mask & (1<<shift)) == 0)
108 p = decode_bitfield_value(buf, val, mask, width);
109 g_snprintf(p, 1024-(p-buf), fmt, val_to_str((val & mask) >> shift, tab, "Unknown"));
114 /* FF: ranges aware versions */
116 /* Tries to match val against each range in the range_string array rs.
117 Returns the associated string ptr on a match.
118 Formats val with fmt, and returns the resulting string, on failure. */
119 const gchar *rval_to_str(guint32 val, const range_string *rs, const char *fmt)
121 const gchar *ret = NULL;
123 g_assert(fmt != NULL);
125 ret = match_strrval(val, rs);
129 return ep_strdup_printf(fmt, val);
132 /* Tries to match val against each range in the range_string array rs.
133 Returns the associated string ptr, and sets "*idx" to the index in
134 that table, on a match, and returns NULL, and sets "*idx" to -1,
136 const gchar *match_strrval_idx(guint32 val, const range_string *rs, gint *idx)
140 while(rs[i].strptr) {
141 if( (val >= rs[i].value_min) && (val <= rs[i].value_max) ) {
143 return (rs[i].strptr);
152 /* Like match_strrval_idx(), but doesn't return the index. */
153 const gchar *match_strrval(guint32 val, const range_string *rs)
156 return match_strrval_idx(val, rs, &ignore_me);