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 #endif /* __cplusplus */
19 #include "ws_symbol_export.h"
20 #include "wmem/wmem.h"
22 /* VALUE TO STRING MATCHING */
24 typedef struct _value_string {
30 /* ----- VALUE_STRING "Helper" macros ----- */
32 /* Essentially: Provide the capability to define a list of value_strings once and
33 then to expand the list as an enum and/or as a value_string array. */
37 /*- define list of value strings -*/
38 #define foo_VALUE_STRING_LIST(XXX) \
39 XXX( FOO_A, 1, "aaa" ) \
40 XXX( FOO_B, 3, "bbb" )
43 VALUE_STRING_ENUM(foo); /* gen's 'enum {FOO_A=1, FOO_B=3};' */
45 /*- gen value_string array -*/
47 VALUE_STRING_ARRAY(foo); /* gen's 'static const value_string foo[] = {{1,"aaa"}, {3,"bbb"}}; */
50 VALUE_STRING_ARRAY_GLOBAL_DEF(foo); /* gen's 'const value_string foo[] = {{1,"aaa"}, {3,"bbb"}}; */
51 VALUE_STRING_ARRAY_GLOBAL_DCL(foo); /* gen's 'const value_string foo[]; */
54 #define bar_VALUE_STRING_LIST(XXX) \
58 VALUE_STRING_ENUM2(bar); /* gen's 'enum {BAR_A=1, BAR_B=3};' */
59 VALUE_STRING_ARRAY2(bar); /* gen's 'static const value_string bar[] = {{1,"BAR_A"}, {3,"BAR_B"}}; */
64 #define VALUE_STRING_ENUM( array_name) _VS_ENUM_XXX( array_name, _VS_ENUM_ENTRY)
65 #define VALUE_STRING_ARRAY( array_name) _VS_ARRAY_SC_XXX(array_name, _VS_ARRAY_ENTRY, static)
66 #define VALUE_STRING_ARRAY_GLOBAL_DEF( array_name) _VS_ARRAY_XXX(array_name, _VS_ARRAY_ENTRY)
67 #define VALUE_STRING_ARRAY_GLOBAL_DCL( array_name) _VS_ARRAY_SC_TYPE_NAME(array_name, extern)
69 #define VALUE_STRING_ENUM2( array_name) _VS_ENUM_XXX( array_name, _VS_ENUM_ENTRY2)
70 #define VALUE_STRING_ARRAY2( array_name) _VS_ARRAY_SC_XXX(array_name, _VS_ARRAY_ENTRY2, static)
71 #define VALUE_STRING_ARRAY2_GLOBAL_DEF( array_name) _VS_ARRAY_XXX(array_name, _VS_ARRAY_ENTRY2)
72 #define VALUE_STRING_ARRAY2_GLOBAL_DCL( array_name) _VS_ARRAY_SC_TYPE_NAME(array_name, extern)
75 #define _VS_ENUM_XXX(array_name, macro) \
77 array_name##_VALUE_STRING_LIST(macro) \
78 _##array_name##_ENUM_DUMMY = 0 \
81 #define _VS_ARRAY_SC_XXX(array_name, macro, sc) \
82 _VS_ARRAY_SC_TYPE_NAME(array_name, sc) = { \
83 array_name##_VALUE_STRING_LIST(macro) \
87 #define _VS_ARRAY_XXX(array_name, macro) \
88 _VS_ARRAY_TYPE_NAME(array_name) = { \
89 array_name##_VALUE_STRING_LIST(macro) \
93 #define _VS_ARRAY_SC_TYPE_NAME(array_name, sc) sc const value_string array_name[]
94 #define _VS_ARRAY_TYPE_NAME(array_name) const value_string array_name[]
96 #define _VS_ENUM_ENTRY( name, value, string) name = value,
97 #define _VS_ARRAY_ENTRY(name, value, string) { value, string },
99 #define _VS_ENUM_ENTRY2( name, value) name = value,
100 #define _VS_ARRAY_ENTRY2(name, value) { value, #name },
105 val_to_str(const guint32 val, const value_string *vs, const char *fmt)
110 val_to_str_wmem(wmem_allocator_t *scope, const guint32 val, const value_string *vs, const char *fmt)
115 val_to_str_const(const guint32 val, const value_string *vs, const char *unknown_str);
119 try_val_to_str(const guint32 val, const value_string *vs);
123 try_val_to_str_idx(const guint32 val, const value_string *vs, gint *idx);
125 /* 64-BIT VALUE TO STRING MATCHING */
127 typedef struct _val64_string {
134 val64_to_str(const guint64 val, const val64_string *vs, const char *fmt)
139 val64_to_str_const(const guint64 val, const val64_string *vs, const char *unknown_str);
143 try_val64_to_str(const guint64 val, const val64_string *vs);
147 try_val64_to_str_idx(const guint64 val, const val64_string *vs, gint *idx);
149 /* STRING TO VALUE MATCHING */
153 str_to_val(const gchar *val, const value_string *vs, const guint32 err_val);
157 str_to_val_idx(const gchar *val, const value_string *vs);
159 /* EXTENDED VALUE TO STRING MATCHING */
161 typedef struct _value_string_ext value_string_ext;
162 typedef const value_string *(*_value_string_match2_t)(const guint32, value_string_ext*);
164 struct _value_string_ext {
165 _value_string_match2_t _vs_match2;
166 guint32 _vs_first_value; /* first value of the value_string array */
167 guint _vs_num_entries; /* number of entries in the value_string array */
168 /* (excluding final {0, NULL}) */
169 const value_string *_vs_p; /* the value string array address */
170 const gchar *_vs_name; /* vse "Name" (for error messages) */
173 #define VALUE_STRING_EXT_VS_P(x) (x)->_vs_p
174 #define VALUE_STRING_EXT_VS_NUM_ENTRIES(x) (x)->_vs_num_entries
175 #define VALUE_STRING_EXT_VS_NAME(x) (x)->_vs_name
179 _try_val_to_str_ext_init(const guint32 val, value_string_ext *vse);
180 #define VALUE_STRING_EXT_INIT(x) { _try_val_to_str_ext_init, 0, G_N_ELEMENTS(x)-1, x, #x }
184 value_string_ext_new(const value_string *vs, guint vs_tot_num_entries, const gchar *vs_name);
188 value_string_ext_free(value_string_ext *vse);
192 val_to_str_ext(const guint32 val, value_string_ext *vse, const char *fmt)
197 val_to_str_ext_wmem(wmem_allocator_t *scope, const guint32 val, value_string_ext *vse, const char *fmt)
202 val_to_str_ext_const(const guint32 val, value_string_ext *vs, const char *unknown_str);
206 try_val_to_str_ext(const guint32 val, value_string_ext *vse);
210 try_val_to_str_idx_ext(const guint32 val, value_string_ext *vse, gint *idx);
212 /* EXTENDED 64-BIT VALUE TO STRING MATCHING */
214 typedef struct _val64_string_ext val64_string_ext;
215 typedef const val64_string *(*_val64_string_match2_t)(const guint64, val64_string_ext*);
217 struct _val64_string_ext {
218 _val64_string_match2_t _vs_match2;
219 guint64 _vs_first_value; /* first value of the val64_string array */
220 guint _vs_num_entries; /* number of entries in the val64_string array */
221 /* (excluding final {0, NULL}) */
222 const val64_string *_vs_p; /* the value string array address */
223 const gchar *_vs_name; /* vse "Name" (for error messages) */
226 #define VAL64_STRING_EXT_VS_P(x) (x)->_vs_p
227 #define VAL64_STRING_EXT_VS_NUM_ENTRIES(x) (x)->_vs_num_entries
228 #define VAL64_STRING_EXT_VS_NAME(x) (x)->_vs_name
232 _try_val64_to_str_ext_init(const guint64 val, val64_string_ext *vse);
233 #define VAL64_STRING_EXT_INIT(x) { _try_val64_to_str_ext_init, 0, G_N_ELEMENTS(x)-1, x, #x }
237 val64_string_ext_new(const val64_string *vs, guint vs_tot_num_entries, const gchar *vs_name);
241 val64_string_ext_free(val64_string_ext *vse);
245 val64_to_str_ext(const guint64 val, val64_string_ext *vse, const char *fmt)
250 val64_to_str_ext_wmem(wmem_allocator_t *scope, const guint64 val, val64_string_ext *vse, const char *fmt)
255 val64_to_str_ext_const(const guint64 val, val64_string_ext *vs, const char *unknown_str);
259 try_val64_to_str_ext(const guint64 val, val64_string_ext *vse);
263 try_val64_to_str_idx_ext(const guint64 val, val64_string_ext *vse, gint *idx);
265 /* STRING TO STRING MATCHING */
267 typedef struct _string_string {
274 str_to_str(const gchar *val, const string_string *vs, const char *fmt)
279 try_str_to_str(const gchar *val, const string_string *vs);
283 try_str_to_str_idx(const gchar *val, const string_string *vs, gint *idx);
285 /* RANGE TO STRING MATCHING */
287 typedef struct _range_string {
295 rval_to_str(const guint32 val, const range_string *rs, const char *fmt)
300 rval_to_str_const(const guint32 val, const range_string *rs, const char *unknown_str);
304 try_rval_to_str(const guint32 val, const range_string *rs);
308 try_rval_to_str_idx(const guint32 val, const range_string *rs, gint *idx);
312 try_rval64_to_str(const guint64 val, const range_string *rs);
316 try_rval64_to_str_idx(const guint64 val, const range_string *rs, gint *idx);
318 /* BYTES TO STRING MATCHING */
320 typedef struct _bytes_string {
322 const size_t value_length;
328 bytesval_to_str(const guint8 *val, const size_t val_len, const bytes_string *bs, const char *fmt)
333 try_bytesval_to_str(const guint8 *val, const size_t val_len, const bytes_string *bs);
337 bytesprefix_to_str(const guint8 *haystack, const size_t haystack_len, const bytes_string *bs, const char *fmt)
342 try_bytesprefix_to_str(const guint8 *haystack, const size_t haystack_len, const bytes_string *bs);
344 /* MISC (generally do not use) */
348 value_string_ext_validate(const value_string_ext *vse);
352 value_string_ext_match_type_str(const value_string_ext *vse);
356 val64_string_ext_validate(const val64_string_ext *vse);
360 val64_string_ext_match_type_str(const val64_string_ext *vse);
364 #endif /* __cplusplus */
366 #endif /* __VALUE_STRING_H__ */
369 * Editor modelines - http://www.wireshark.org/tools/modelines.html
374 * indent-tabs-mode: nil
377 * vi: set shiftwidth=4 tabstop=8 expandtab:
378 * :indentSize=4:tabSize=8:noTabs=true: