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