2 * Definitions for value_string structures and routines
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
11 #ifndef __VALUE_STRING_H__
12 #define __VALUE_STRING_H__
16 #include "ws_symbol_export.h"
17 #include "wmem/wmem.h"
21 #endif /* __cplusplus */
23 /* VALUE TO STRING MATCHING */
25 typedef struct _value_string
{
31 /* ----- VALUE_STRING "Helper" macros ----- */
33 /* Essentially: Provide the capability to define a list of value_strings once and
34 then to expand the list as an enum and/or as a value_string array. */
38 /*- define list of value strings -*/
39 #define foo_VALUE_STRING_LIST(XXX) \
40 XXX( FOO_A, 1, "aaa" ) \
41 XXX( FOO_B, 3, "bbb" )
44 VALUE_STRING_ENUM(foo
); /* gen's 'enum {FOO_A=1, FOO_B=3};' */
46 /*- gen value_string array -*/
48 VALUE_STRING_ARRAY(foo
); /* gen's 'static const value_string foo[] = {{1,"aaa"}, {3,"bbb"}}; */
51 VALUE_STRING_ARRAY_GLOBAL_DEF(foo
); /* gen's 'const value_string foo[] = {{1,"aaa"}, {3,"bbb"}}; */
52 VALUE_STRING_ARRAY_GLOBAL_DCL(foo
); /* gen's 'const value_string foo[]; */
55 #define bar_VALUE_STRING_LIST(XXX) \
59 VALUE_STRING_ENUM2(bar
); /* gen's 'enum {BAR_A=1, BAR_B=3};' */
60 VALUE_STRING_ARRAY2(bar
); /* gen's 'static const value_string bar[] = {{1,"BAR_A"}, {3,"BAR_B"}}; */
65 #define VALUE_STRING_ENUM( array_name) _VS_ENUM_XXX( array_name, _VS_ENUM_ENTRY)
66 #define VALUE_STRING_ARRAY( array_name) _VS_ARRAY_SC_XXX(array_name, _VS_ARRAY_ENTRY, static)
67 #define VALUE_STRING_ARRAY_GLOBAL_DEF( array_name) _VS_ARRAY_XXX(array_name, _VS_ARRAY_ENTRY)
68 #define VALUE_STRING_ARRAY_GLOBAL_DCL( array_name) _VS_ARRAY_SC_TYPE_NAME(array_name, extern)
70 #define VALUE_STRING_ENUM2( array_name) _VS_ENUM_XXX( array_name, _VS_ENUM_ENTRY2)
71 #define VALUE_STRING_ARRAY2( array_name) _VS_ARRAY_SC_XXX(array_name, _VS_ARRAY_ENTRY2, static)
72 #define VALUE_STRING_ARRAY2_GLOBAL_DEF( array_name) _VS_ARRAY_XXX(array_name, _VS_ARRAY_ENTRY2)
73 #define VALUE_STRING_ARRAY2_GLOBAL_DCL( array_name) _VS_ARRAY_SC_TYPE_NAME(array_name, extern)
76 #define _VS_ENUM_XXX(array_name, macro) \
78 array_name##_VALUE_STRING_LIST(macro) \
79 _##array_name##_ENUM_DUMMY = 0 \
82 #define _VS_ARRAY_SC_XXX(array_name, macro, sc) \
83 _VS_ARRAY_SC_TYPE_NAME(array_name, sc) = { \
84 array_name##_VALUE_STRING_LIST(macro) \
88 #define _VS_ARRAY_XXX(array_name, macro) \
89 _VS_ARRAY_TYPE_NAME(array_name) = { \
90 array_name##_VALUE_STRING_LIST(macro) \
94 #define _VS_ARRAY_SC_TYPE_NAME(array_name, sc) sc const value_string array_name[]
95 #define _VS_ARRAY_TYPE_NAME(array_name) const value_string array_name[]
97 #define _VS_ENUM_ENTRY( name, value, string) name = value,
98 #define _VS_ARRAY_ENTRY(name, value, string) { value, string },
100 #define _VS_ENUM_ENTRY2( name, value) name = value,
101 #define _VS_ARRAY_ENTRY2(name, value) { value, #name },
106 val_to_str(const guint32 val
, const value_string
*vs
, const char *fmt
)
111 val_to_str_wmem(wmem_allocator_t
*scope
, const guint32 val
, const value_string
*vs
, const char *fmt
)
116 val_to_str_const(const guint32 val
, const value_string
*vs
, const char *unknown_str
);
120 try_val_to_str(const guint32 val
, const value_string
*vs
);
124 try_val_to_str_idx(const guint32 val
, const value_string
*vs
, gint
*idx
);
126 /* 64-BIT VALUE TO STRING MATCHING */
128 typedef struct _val64_string
{
135 val64_to_str(const guint64 val
, const val64_string
*vs
, const char *fmt
)
140 val64_to_str_const(const guint64 val
, const val64_string
*vs
, const char *unknown_str
);
144 try_val64_to_str(const guint64 val
, const val64_string
*vs
);
148 try_val64_to_str_idx(const guint64 val
, const val64_string
*vs
, gint
*idx
);
150 /* STRING TO VALUE MATCHING */
154 str_to_val(const gchar
*val
, const value_string
*vs
, const guint32 err_val
);
158 str_to_val_idx(const gchar
*val
, const value_string
*vs
);
160 /* EXTENDED VALUE TO STRING MATCHING */
162 typedef struct _value_string_ext value_string_ext
;
163 typedef const value_string
*(*_value_string_match2_t
)(const guint32
, value_string_ext
*);
165 struct _value_string_ext
{
166 _value_string_match2_t _vs_match2
;
167 guint32 _vs_first_value
; /* first value of the value_string array */
168 guint _vs_num_entries
; /* number of entries in the value_string array */
169 /* (excluding final {0, NULL}) */
170 const value_string
*_vs_p
; /* the value string array address */
171 const gchar
*_vs_name
; /* vse "Name" (for error messages) */
174 #define VALUE_STRING_EXT_VS_P(x) (x)->_vs_p
175 #define VALUE_STRING_EXT_VS_NUM_ENTRIES(x) (x)->_vs_num_entries
176 #define VALUE_STRING_EXT_VS_NAME(x) (x)->_vs_name
180 _try_val_to_str_ext_init(const guint32 val
, value_string_ext
*vse
);
181 #define VALUE_STRING_EXT_INIT(x) { _try_val_to_str_ext_init, 0, G_N_ELEMENTS(x)-1, x, #x }
185 value_string_ext_new(const value_string
*vs
, guint vs_tot_num_entries
, const gchar
*vs_name
);
189 value_string_ext_free(value_string_ext
*vse
);
193 val_to_str_ext(const guint32 val
, value_string_ext
*vse
, const char *fmt
)
198 val_to_str_ext_wmem(wmem_allocator_t
*scope
, const guint32 val
, value_string_ext
*vse
, const char *fmt
)
203 val_to_str_ext_const(const guint32 val
, value_string_ext
*vs
, const char *unknown_str
);
207 try_val_to_str_ext(const guint32 val
, value_string_ext
*vse
);
211 try_val_to_str_idx_ext(const guint32 val
, value_string_ext
*vse
, gint
*idx
);
213 /* EXTENDED 64-BIT VALUE TO STRING MATCHING */
215 typedef struct _val64_string_ext val64_string_ext
;
216 typedef const val64_string
*(*_val64_string_match2_t
)(const guint64
, val64_string_ext
*);
218 struct _val64_string_ext
{
219 _val64_string_match2_t _vs_match2
;
220 guint64 _vs_first_value
; /* first value of the val64_string array */
221 guint _vs_num_entries
; /* number of entries in the val64_string array */
222 /* (excluding final {0, NULL}) */
223 const val64_string
*_vs_p
; /* the value string array address */
224 const gchar
*_vs_name
; /* vse "Name" (for error messages) */
227 #define VAL64_STRING_EXT_VS_P(x) (x)->_vs_p
228 #define VAL64_STRING_EXT_VS_NUM_ENTRIES(x) (x)->_vs_num_entries
229 #define VAL64_STRING_EXT_VS_NAME(x) (x)->_vs_name
233 _try_val64_to_str_ext_init(const guint64 val
, val64_string_ext
*vse
);
234 #define VAL64_STRING_EXT_INIT(x) { _try_val64_to_str_ext_init, 0, G_N_ELEMENTS(x)-1, x, #x }
238 val64_string_ext_new(const val64_string
*vs
, guint vs_tot_num_entries
, const gchar
*vs_name
);
242 val64_string_ext_free(val64_string_ext
*vse
);
246 val64_to_str_ext(const guint64 val
, val64_string_ext
*vse
, const char *fmt
)
251 val64_to_str_ext_wmem(wmem_allocator_t
*scope
, const guint64 val
, val64_string_ext
*vse
, const char *fmt
)
256 val64_to_str_ext_const(const guint64 val
, val64_string_ext
*vs
, const char *unknown_str
);
260 try_val64_to_str_ext(const guint64 val
, val64_string_ext
*vse
);
264 try_val64_to_str_idx_ext(const guint64 val
, val64_string_ext
*vse
, gint
*idx
);
266 /* STRING TO STRING MATCHING */
268 typedef struct _string_string
{
275 str_to_str(const gchar
*val
, const string_string
*vs
, const char *fmt
)
280 try_str_to_str(const gchar
*val
, const string_string
*vs
);
284 try_str_to_str_idx(const gchar
*val
, const string_string
*vs
, gint
*idx
);
286 /* RANGE TO STRING MATCHING */
288 typedef struct _range_string
{
296 rval_to_str(const guint32 val
, const range_string
*rs
, const char *fmt
)
301 rval_to_str_const(const guint32 val
, const range_string
*rs
, const char *unknown_str
);
305 try_rval_to_str(const guint32 val
, const range_string
*rs
);
309 try_rval_to_str_idx(const guint32 val
, const range_string
*rs
, gint
*idx
);
313 try_rval64_to_str(const guint64 val
, const range_string
*rs
);
317 try_rval64_to_str_idx(const guint64 val
, const range_string
*rs
, gint
*idx
);
319 /* BYTES TO STRING MATCHING */
321 typedef struct _bytes_string
{
323 const size_t value_length
;
329 bytesval_to_str(const guint8
*val
, const size_t val_len
, const bytes_string
*bs
, const char *fmt
)
334 try_bytesval_to_str(const guint8
*val
, const size_t val_len
, const bytes_string
*bs
);
338 bytesprefix_to_str(const guint8
*haystack
, const size_t haystack_len
, const bytes_string
*bs
, const char *fmt
)
343 try_bytesprefix_to_str(const guint8
*haystack
, const size_t haystack_len
, const bytes_string
*bs
);
345 /* MISC (generally do not use) */
349 value_string_ext_validate(const value_string_ext
*vse
);
353 value_string_ext_match_type_str(const value_string_ext
*vse
);
357 val64_string_ext_validate(const val64_string_ext
*vse
);
361 val64_string_ext_match_type_str(const val64_string_ext
*vse
);
365 #endif /* __cplusplus */
367 #endif /* __VALUE_STRING_H__ */
370 * Editor modelines - https://www.wireshark.org/tools/modelines.html
375 * indent-tabs-mode: nil
378 * vi: set shiftwidth=4 tabstop=8 expandtab:
379 * :indentSize=4:tabSize=8:noTabs=true: