Add HP Switch Protocol SAP value
[obnox/wireshark/wip.git] / epan / value_string.c
1 /* value_string.c
2  * Routines for value_strings
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <stdio.h>
30
31 #include "to_str.h"
32 #include "emem.h"
33 #include "value_string.h"
34
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. */
38 const gchar*
39 val_to_str(guint32 val, const value_string *vs, const char *fmt) {
40   const gchar *ret;
41
42   g_assert(fmt != NULL);
43
44   ret = match_strval(val, vs);
45   if (ret != NULL)
46     return ret;
47
48   return ep_strdup_printf(fmt, val);
49 }
50
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,
54    on failure. */
55 const gchar*
56 match_strval_idx(guint32 val, const value_string *vs, gint *idx) {
57   gint i = 0;
58
59   while (vs[i].strptr) {
60     if (vs[i].value == val) {
61       *idx = i;
62       return(vs[i].strptr);
63     }
64     i++;
65   }
66
67   *idx = -1;
68   return(NULL);
69 }
70
71 /* Like match_strval_idx(), but doesn't return the index. */
72 const gchar*
73 match_strval(guint32 val, const value_string *vs) {
74     gint ignore_me;
75     return match_strval_idx(val, vs, &ignore_me);
76 }
77
78 /* Generate a string describing an enumerated bitfield (an N-bit field
79    with various specific values having particular names). */
80 const char *
81 decode_enumerated_bitfield(guint32 val, guint32 mask, int width,
82     const value_string *tab, const char *fmt)
83 {
84   static char buf[1025];
85   char *p;
86
87   p = decode_bitfield_value(buf, val, mask, width);
88   g_snprintf(p, 1024-(p-buf), fmt, val_to_str(val & mask, tab, "Unknown"));
89   return buf;
90 }
91
92
93 /* Generate a string describing an enumerated bitfield (an N-bit field
94    with various specific values having particular names). */
95 const char *
96 decode_enumerated_bitfield_shifted(guint32 val, guint32 mask, int width,
97     const value_string *tab, const char *fmt)
98 {
99   static char buf[1025];
100   char *p;
101   int shift = 0;
102
103   /* Compute the number of bits we have to shift the bitfield right
104      to extract its value. */
105   while ((mask & (1<<shift)) == 0)
106     shift++;
107
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"));
110   return buf;
111 }