2 * Routines for value_strings
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
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) {
43 g_assert(fmt != NULL);
45 ret = match_strval(val, vs);
50 g_snprintf(cur, 64, fmt, val);
54 /* Tries to match val against each element in the value_string array vs.
55 Returns the associated string ptr, and sets "*idx" to the index in
56 that table, on a match, and returns NULL, and sets "*idx" to -1,
59 match_strval_idx(guint32 val, const value_string *vs, gint *idx) {
62 while (vs[i].strptr) {
63 if (vs[i].value == val) {
74 /* Like match_strval_idx(), but doesn't return the index. */
76 match_strval(guint32 val, const value_string *vs) {
78 return match_strval_idx(val, vs, &ignore_me);
81 /* Generate a string describing an enumerated bitfield (an N-bit field
82 with various specific values having particular names). */
84 decode_enumerated_bitfield(guint32 val, guint32 mask, int width,
85 const value_string *tab, const char *fmt)
87 static char buf[1025];
90 p = decode_bitfield_value(buf, val, mask, width);
91 g_snprintf(p, 1024-(p-buf), fmt, val_to_str(val & mask, tab, "Unknown"));
96 /* Generate a string describing an enumerated bitfield (an N-bit field
97 with various specific values having particular names). */
99 decode_enumerated_bitfield_shifted(guint32 val, guint32 mask, int width,
100 const value_string *tab, const char *fmt)
102 static char buf[1025];
106 /* Compute the number of bits we have to shift the bitfield right
107 to extract its value. */
108 while ((mask & (1<<shift)) == 0)
111 p = decode_bitfield_value(buf, val, mask, width);
112 g_snprintf(p, 1024-(p-buf), fmt, val_to_str((val & mask) >> shift, tab, "Unknown"));