Wireshark 4.5.0
The Wireshark network protocol analyzer
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
value_string.h
Go to the documentation of this file.
1
11#ifndef __VALUE_STRING_H__
12#define __VALUE_STRING_H__
13
14#include <stdint.h>
15
16#include "ws_symbol_export.h"
17#include <epan/wmem_scopes.h>
18
19#include <wsutil/nstime.h>
20
21#ifdef __cplusplus
22extern "C" {
23#endif /* __cplusplus */
24
25/* VALUE TO STRING MATCHING */
26
27typedef struct _value_string {
28 uint32_t value;
29 const char *strptr;
31
32#if 0
33 /* ----- VALUE_STRING "Helper" macros ----- */
34
35 /* Essentially: Provide the capability to define a list of value_strings once and
36 then to expand the list as an enum and/or as a value_string array. */
37
38 /* Usage: */
39
40 /*- define list of value strings -*/
41 #define foo_VALUE_STRING_LIST(XXX) \
42 XXX( FOO_A, 1, "aaa" ) \
43 XXX( FOO_B, 3, "bbb" )
44
45 /*- gen enum -*/
46 VALUE_STRING_ENUM(foo); /* gen's 'enum {FOO_A=1, FOO_B=3};' */
47
48 /*- gen value_string array -*/
49 /* local */
50 VALUE_STRING_ARRAY(foo); /* gen's 'static const value_string foo[] = {{1,"aaa"}, {3,"bbb"}}; */
51
52 /* global */
53 VALUE_STRING_ARRAY_GLOBAL_DEF(foo); /* gen's 'const value_string foo[] = {{1,"aaa"}, {3,"bbb"}}; */
54 VALUE_STRING_ARRAY_GLOBAL_DCL(foo); /* gen's 'const value_string foo[]; */
55
56 /* Alternatively: */
57 #define bar_VALUE_STRING_LIST(XXX) \
58 XXX( BAR_A, 1) \
59 XXX( BAR_B, 3)
60
61 VALUE_STRING_ENUM2(bar); /* gen's 'enum {BAR_A=1, BAR_B=3};' */
62 VALUE_STRING_ARRAY2(bar); /* gen's 'static const value_string bar[] = {{1,"BAR_A"}, {3,"BAR_B"}}; */
63 ...
64#endif
65
66/* -- Public -- */
67#define VALUE_STRING_ENUM( array_name) _VS_ENUM_XXX( array_name, _VS_ENUM_ENTRY)
68#define VALUE_STRING_ARRAY( array_name) _VS_ARRAY_SC_XXX(array_name, _VS_ARRAY_ENTRY, static)
69#define VALUE_STRING_ARRAY_GLOBAL_DEF( array_name) _VS_ARRAY_XXX(array_name, _VS_ARRAY_ENTRY)
70#define VALUE_STRING_ARRAY_GLOBAL_DCL( array_name) _VS_ARRAY_SC_TYPE_NAME(array_name, extern)
71
72#define VALUE_STRING_ENUM2( array_name) _VS_ENUM_XXX( array_name, _VS_ENUM_ENTRY2)
73#define VALUE_STRING_ARRAY2( array_name) _VS_ARRAY_SC_XXX(array_name, _VS_ARRAY_ENTRY2, static)
74#define VALUE_STRING_ARRAY2_GLOBAL_DEF( array_name) _VS_ARRAY_XXX(array_name, _VS_ARRAY_ENTRY2)
75#define VALUE_STRING_ARRAY2_GLOBAL_DCL( array_name) _VS_ARRAY_SC_TYPE_NAME(array_name, extern)
76
77/* -- Private -- */
78#define _VS_ENUM_XXX(array_name, macro) \
79enum { \
80 array_name##_VALUE_STRING_LIST(macro) \
81 _##array_name##_ENUM_DUMMY = 0 \
82}
83
84#define _VS_ARRAY_SC_XXX(array_name, macro, sc) \
85 _VS_ARRAY_SC_TYPE_NAME(array_name, sc) = { \
86 array_name##_VALUE_STRING_LIST(macro) \
87 { 0, NULL } \
88}
89
90#define _VS_ARRAY_XXX(array_name, macro) \
91 _VS_ARRAY_TYPE_NAME(array_name) = { \
92 array_name##_VALUE_STRING_LIST(macro) \
93 { 0, NULL } \
94}
95
96#define _VS_ARRAY_SC_TYPE_NAME(array_name, sc) sc const value_string array_name[]
97#define _VS_ARRAY_TYPE_NAME(array_name) const value_string array_name[]
98
99#define _VS_ENUM_ENTRY( name, value, string) name = value,
100#define _VS_ARRAY_ENTRY(name, value, string) { value, string },
101
102#define _VS_ENUM_ENTRY2( name, value) name = value,
103#define _VS_ARRAY_ENTRY2(name, value) { value, #name },
104/* ----- ----- */
105
106WS_DLL_PUBLIC
107const char *
108val_to_str(const uint32_t val, const value_string *vs, const char *fmt)
109G_GNUC_PRINTF(3, 0);
110
111WS_DLL_PUBLIC
112char *
113val_to_str_wmem(wmem_allocator_t *scope, const uint32_t val, const value_string *vs, const char *fmt)
114G_GNUC_PRINTF(4, 0);
115
116WS_DLL_PUBLIC
117const char *
118val_to_str_const(const uint32_t val, const value_string *vs, const char *unknown_str);
119
120WS_DLL_PUBLIC
121const char *
122try_val_to_str(const uint32_t val, const value_string *vs);
123
124WS_DLL_PUBLIC
125const char *
126try_val_to_str_idx(const uint32_t val, const value_string *vs, int *idx);
127
128/* 64-BIT VALUE TO STRING MATCHING */
129
130typedef struct _val64_string {
131 uint64_t value;
132 const char *strptr;
134
135WS_DLL_PUBLIC
136const char *
137val64_to_str_wmem(wmem_allocator_t* scope, const uint64_t val, const val64_string *vs, const char *fmt)
138G_GNUC_PRINTF(4, 0);
139
140WS_DLL_PUBLIC
141const char *
142val64_to_str_const(const uint64_t val, const val64_string *vs, const char *unknown_str);
143
144WS_DLL_PUBLIC
145const char *
146try_val64_to_str(const uint64_t val, const val64_string *vs);
147
148WS_DLL_PUBLIC
149const char *
150try_val64_to_str_idx(const uint64_t val, const val64_string *vs, int *idx);
151
152/* STRING TO VALUE MATCHING */
153
154WS_DLL_PUBLIC
155uint32_t
156str_to_val(const char *val, const value_string *vs, const uint32_t err_val);
157
158WS_DLL_PUBLIC
159int
160str_to_val_idx(const char *val, const value_string *vs);
161
162/* EXTENDED VALUE TO STRING MATCHING */
163
165typedef const value_string *(*_value_string_match2_t)(const uint32_t, value_string_ext*);
166
168 _value_string_match2_t _vs_match2;
169 uint32_t _vs_first_value; /* first value of the value_string array */
170 unsigned _vs_num_entries; /* number of entries in the value_string array */
171 /* (excluding final {0, NULL}) */
172 const value_string *_vs_p; /* the value string array address */
173 const char *_vs_name; /* vse "Name" (for error messages) */
174};
175
176#define VALUE_STRING_EXT_VS_P(x) (x)->_vs_p
177#define VALUE_STRING_EXT_VS_NUM_ENTRIES(x) (x)->_vs_num_entries
178#define VALUE_STRING_EXT_VS_NAME(x) (x)->_vs_name
179
180WS_DLL_PUBLIC
181const value_string *
182_try_val_to_str_ext_init(const uint32_t val, value_string_ext *vse);
183#define VALUE_STRING_EXT_INIT(x) { _try_val_to_str_ext_init, 0, G_N_ELEMENTS(x)-1, x, #x }
184
185WS_DLL_PUBLIC
187value_string_ext_new(const value_string *vs, unsigned vs_tot_num_entries, const char *vs_name);
188
189WS_DLL_PUBLIC
190void
191value_string_ext_free(value_string_ext *vse);
192
193WS_DLL_PUBLIC
194const char *
195val_to_str_ext(const uint32_t val, value_string_ext *vse, const char *fmt)
196G_GNUC_PRINTF(3, 0);
197
198WS_DLL_PUBLIC
199char *
200val_to_str_ext_wmem(wmem_allocator_t *scope, const uint32_t val, value_string_ext *vse, const char *fmt)
201G_GNUC_PRINTF(4, 0);
202
203WS_DLL_PUBLIC
204const char *
205val_to_str_ext_const(const uint32_t val, value_string_ext *vs, const char *unknown_str);
206
207WS_DLL_PUBLIC
208const char *
209try_val_to_str_ext(const uint32_t val, value_string_ext *vse);
210
211WS_DLL_PUBLIC
212const char *
213try_val_to_str_idx_ext(const uint32_t val, value_string_ext *vse, int *idx);
214
215/* EXTENDED 64-BIT VALUE TO STRING MATCHING */
216
218typedef const val64_string *(*_val64_string_match2_t)(const uint64_t, val64_string_ext*);
219
221 _val64_string_match2_t _vs_match2;
222 uint64_t _vs_first_value; /* first value of the val64_string array */
223 unsigned _vs_num_entries; /* number of entries in the val64_string array */
224 /* (excluding final {0, NULL}) */
225 const val64_string *_vs_p; /* the value string array address */
226 const char *_vs_name; /* vse "Name" (for error messages) */
227};
228
229#define VAL64_STRING_EXT_VS_P(x) (x)->_vs_p
230#define VAL64_STRING_EXT_VS_NUM_ENTRIES(x) (x)->_vs_num_entries
231#define VAL64_STRING_EXT_VS_NAME(x) (x)->_vs_name
232
233WS_DLL_PUBLIC
234int
235value_str_value_compare(const void* a, const void* b);
236
237WS_DLL_PUBLIC
238const val64_string *
239_try_val64_to_str_ext_init(const uint64_t val, val64_string_ext *vse);
240#define VAL64_STRING_EXT_INIT(x) { _try_val64_to_str_ext_init, 0, G_N_ELEMENTS(x)-1, x, #x }
241
242WS_DLL_PUBLIC
244val64_string_ext_new(const val64_string *vs, unsigned vs_tot_num_entries, const char *vs_name);
245
246WS_DLL_PUBLIC
247void
248val64_string_ext_free(val64_string_ext *vse);
249
250WS_DLL_PUBLIC
251char *
252val64_to_str_ext_wmem(wmem_allocator_t *scope, const uint64_t val, val64_string_ext *vse, const char *fmt)
253G_GNUC_PRINTF(4, 0);
254
255WS_DLL_PUBLIC
256const char *
257val64_to_str_ext_const(const uint64_t val, val64_string_ext *vs, const char *unknown_str);
258
259WS_DLL_PUBLIC
260const char *
261try_val64_to_str_ext(const uint64_t val, val64_string_ext *vse);
262
263WS_DLL_PUBLIC
264const char *
265try_val64_to_str_idx_ext(const uint64_t val, val64_string_ext *vse, int *idx);
266
267/* STRING TO STRING MATCHING */
268
269typedef struct _string_string {
270 const char *value;
271 const char *strptr;
273
274WS_DLL_PUBLIC
275const char *
276str_to_str_wmem(wmem_allocator_t* scope, const char *val, const string_string *vs, const char *fmt)
277G_GNUC_PRINTF(4, 0);
278
279WS_DLL_PUBLIC
280const char *
281try_str_to_str(const char *val, const string_string *vs);
282
283WS_DLL_PUBLIC
284const char *
285try_str_to_str_idx(const char *val, const string_string *vs, int *idx);
286
287/* RANGE TO STRING MATCHING */
288
289typedef struct _range_string {
290 uint64_t value_min;
291 uint64_t value_max;
292 const char *strptr;
294
295WS_DLL_PUBLIC
296const char *
297rval_to_str_wmem(wmem_allocator_t* scope, const uint32_t val, const range_string *rs, const char *fmt)
298G_GNUC_PRINTF(4, 0);
299
300WS_DLL_PUBLIC
301const char *
302rval_to_str_const(const uint32_t val, const range_string *rs, const char *unknown_str);
303
304WS_DLL_PUBLIC
305const char *
306try_rval_to_str(const uint32_t val, const range_string *rs);
307
308WS_DLL_PUBLIC
309const char *
310try_rval_to_str_idx(const uint32_t val, const range_string *rs, int *idx);
311
312WS_DLL_PUBLIC
313const char *
314try_rval64_to_str(const uint64_t val, const range_string *rs);
315
316WS_DLL_PUBLIC
317const char *
318try_rval64_to_str_idx(const uint64_t val, const range_string *rs, int *idx);
319
320/* TIME TO STRING MATCHING */
321
322typedef struct _time_value_string {
323 nstime_t value;
324 const char *strptr;
326
327WS_DLL_PUBLIC
328const char *
329try_time_val_to_str(const nstime_t *val, const time_value_string *vs);
330
331/* BYTES TO STRING MATCHING */
332
333typedef struct _bytes_string {
334 const uint8_t *value;
335 const size_t value_length;
336 const char *strptr;
338
339WS_DLL_PUBLIC
340const char *
341bytesval_to_str_wmem(wmem_allocator_t* scope, const uint8_t *val, const size_t val_len, const bytes_string *bs, const char *fmt)
342G_GNUC_PRINTF(5, 0);
343
344WS_DLL_PUBLIC
345const char *
346try_bytesval_to_str(const uint8_t *val, const size_t val_len, const bytes_string *bs);
347
348WS_DLL_PUBLIC
349const char *
350bytesprefix_to_str(wmem_allocator_t* scope, const uint8_t *haystack, const size_t haystack_len, const bytes_string *bs, const char *fmt)
351G_GNUC_PRINTF(5, 0);
352
353WS_DLL_PUBLIC
354const char *
355try_bytesprefix_to_str(const uint8_t *haystack, const size_t haystack_len, const bytes_string *bs);
356
357/* MISC (generally do not use) */
358
359WS_DLL_LOCAL
360bool
361value_string_ext_validate(const value_string_ext *vse);
362
363WS_DLL_LOCAL
364const char *
365value_string_ext_match_type_str(const value_string_ext *vse);
366
367WS_DLL_LOCAL
368bool
369val64_string_ext_validate(const val64_string_ext *vse);
370
371WS_DLL_LOCAL
372const char *
373val64_string_ext_match_type_str(const val64_string_ext *vse);
374
375#ifdef __cplusplus
376}
377#endif /* __cplusplus */
378
379#endif /* __VALUE_STRING_H__ */
380
381/*
382 * Editor modelines - https://www.wireshark.org/tools/modelines.html
383 *
384 * Local variables:
385 * c-basic-offset: 4
386 * tab-width: 8
387 * indent-tabs-mode: nil
388 * End:
389 *
390 * vi: set shiftwidth=4 tabstop=8 expandtab:
391 * :indentSize=4:tabSize=8:noTabs=true:
392 */
Definition value_string.h:333
Definition value_string.h:289
Definition value_string.h:269
Definition value_string.h:322
Definition value_string.h:220
Definition value_string.h:130
Definition value_string.h:167
Definition value_string.h:27
Definition wmem_allocator.h:27
Definition nstime.h:26