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 <epan/wmem_scopes.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 uint32_t val
, const value_string
*vs
, const char *fmt
)
111 val_to_str_wmem(wmem_allocator_t
*scope
, const uint32_t val
, const value_string
*vs
, const char *fmt
)
116 val_to_str_const(const uint32_t val
, const value_string
*vs
, const char *unknown_str
);
120 try_val_to_str(const uint32_t val
, const value_string
*vs
);
124 try_val_to_str_idx(const uint32_t val
, const value_string
*vs
, int *idx
);
128 char_val_to_str(char val
, const value_string
*vs
, const char *msg
);
130 /* 64-BIT VALUE TO STRING MATCHING */
132 typedef struct _val64_string
{
139 val64_to_str(const uint64_t val
, const val64_string
*vs
, const char *fmt
)
144 val64_to_str_const(const uint64_t val
, const val64_string
*vs
, const char *unknown_str
);
148 try_val64_to_str(const uint64_t val
, const val64_string
*vs
);
152 try_val64_to_str_idx(const uint64_t val
, const val64_string
*vs
, int *idx
);
154 /* STRING TO VALUE MATCHING */
158 str_to_val(const char *val
, const value_string
*vs
, const uint32_t err_val
);
162 str_to_val_idx(const char *val
, const value_string
*vs
);
164 /* EXTENDED VALUE TO STRING MATCHING */
166 typedef struct _value_string_ext value_string_ext
;
167 typedef const value_string
*(*_value_string_match2_t
)(const uint32_t, value_string_ext
*);
169 struct _value_string_ext
{
170 _value_string_match2_t _vs_match2
;
171 uint32_t _vs_first_value
; /* first value of the value_string array */
172 unsigned _vs_num_entries
; /* number of entries in the value_string array */
173 /* (excluding final {0, NULL}) */
174 const value_string
*_vs_p
; /* the value string array address */
175 const char *_vs_name
; /* vse "Name" (for error messages) */
178 #define VALUE_STRING_EXT_VS_P(x) (x)->_vs_p
179 #define VALUE_STRING_EXT_VS_NUM_ENTRIES(x) (x)->_vs_num_entries
180 #define VALUE_STRING_EXT_VS_NAME(x) (x)->_vs_name
184 _try_val_to_str_ext_init(const uint32_t val
, value_string_ext
*vse
);
185 #define VALUE_STRING_EXT_INIT(x) { _try_val_to_str_ext_init, 0, G_N_ELEMENTS(x)-1, x, #x }
189 value_string_ext_new(const value_string
*vs
, unsigned vs_tot_num_entries
, const char *vs_name
);
193 value_string_ext_free(value_string_ext
*vse
);
197 val_to_str_ext(const uint32_t val
, value_string_ext
*vse
, const char *fmt
)
202 val_to_str_ext_wmem(wmem_allocator_t
*scope
, const uint32_t val
, value_string_ext
*vse
, const char *fmt
)
207 val_to_str_ext_const(const uint32_t val
, value_string_ext
*vs
, const char *unknown_str
);
211 try_val_to_str_ext(const uint32_t val
, value_string_ext
*vse
);
215 try_val_to_str_idx_ext(const uint32_t val
, value_string_ext
*vse
, int *idx
);
217 /* EXTENDED 64-BIT VALUE TO STRING MATCHING */
219 typedef struct _val64_string_ext val64_string_ext
;
220 typedef const val64_string
*(*_val64_string_match2_t
)(const uint64_t, val64_string_ext
*);
222 struct _val64_string_ext
{
223 _val64_string_match2_t _vs_match2
;
224 uint64_t _vs_first_value
; /* first value of the val64_string array */
225 unsigned _vs_num_entries
; /* number of entries in the val64_string array */
226 /* (excluding final {0, NULL}) */
227 const val64_string
*_vs_p
; /* the value string array address */
228 const char *_vs_name
; /* vse "Name" (for error messages) */
231 #define VAL64_STRING_EXT_VS_P(x) (x)->_vs_p
232 #define VAL64_STRING_EXT_VS_NUM_ENTRIES(x) (x)->_vs_num_entries
233 #define VAL64_STRING_EXT_VS_NAME(x) (x)->_vs_name
237 _try_val64_to_str_ext_init(const uint64_t val
, val64_string_ext
*vse
);
238 #define VAL64_STRING_EXT_INIT(x) { _try_val64_to_str_ext_init, 0, G_N_ELEMENTS(x)-1, x, #x }
242 val64_string_ext_new(const val64_string
*vs
, unsigned vs_tot_num_entries
, const char *vs_name
);
246 val64_string_ext_free(val64_string_ext
*vse
);
250 val64_to_str_ext(const uint64_t val
, val64_string_ext
*vse
, const char *fmt
)
255 val64_to_str_ext_wmem(wmem_allocator_t
*scope
, const uint64_t val
, val64_string_ext
*vse
, const char *fmt
)
260 val64_to_str_ext_const(const uint64_t val
, val64_string_ext
*vs
, const char *unknown_str
);
264 try_val64_to_str_ext(const uint64_t val
, val64_string_ext
*vse
);
268 try_val64_to_str_idx_ext(const uint64_t val
, val64_string_ext
*vse
, int *idx
);
270 /* STRING TO STRING MATCHING */
272 typedef struct _string_string
{
279 str_to_str(const char *val
, const string_string
*vs
, const char *fmt
)
284 try_str_to_str(const char *val
, const string_string
*vs
);
288 try_str_to_str_idx(const char *val
, const string_string
*vs
, int *idx
);
290 /* RANGE TO STRING MATCHING */
292 typedef struct _range_string
{
300 rval_to_str(const uint32_t val
, const range_string
*rs
, const char *fmt
)
305 rval_to_str_const(const uint32_t val
, const range_string
*rs
, const char *unknown_str
);
309 try_rval_to_str(const uint32_t val
, const range_string
*rs
);
313 try_rval_to_str_idx(const uint32_t val
, const range_string
*rs
, int *idx
);
317 try_rval64_to_str(const uint64_t val
, const range_string
*rs
);
321 try_rval64_to_str_idx(const uint64_t val
, const range_string
*rs
, int *idx
);
323 /* BYTES TO STRING MATCHING */
325 typedef struct _bytes_string
{
326 const uint8_t *value
;
327 const size_t value_length
;
333 bytesval_to_str(const uint8_t *val
, const size_t val_len
, const bytes_string
*bs
, const char *fmt
)
338 try_bytesval_to_str(const uint8_t *val
, const size_t val_len
, const bytes_string
*bs
);
342 bytesprefix_to_str(const uint8_t *haystack
, const size_t haystack_len
, const bytes_string
*bs
, const char *fmt
)
347 try_bytesprefix_to_str(const uint8_t *haystack
, const size_t haystack_len
, const bytes_string
*bs
);
349 /* MISC (generally do not use) */
353 value_string_ext_validate(const value_string_ext
*vse
);
357 value_string_ext_match_type_str(const value_string_ext
*vse
);
361 val64_string_ext_validate(const val64_string_ext
*vse
);
365 val64_string_ext_match_type_str(const val64_string_ext
*vse
);
369 #endif /* __cplusplus */
371 #endif /* __VALUE_STRING_H__ */
374 * Editor modelines - https://www.wireshark.org/tools/modelines.html
379 * indent-tabs-mode: nil
382 * vi: set shiftwidth=4 tabstop=8 expandtab:
383 * :indentSize=4:tabSize=8:noTabs=true: