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
128WS_DLL_PUBLIC
129const char *
130char_val_to_str(char val, const value_string *vs, const char *msg);
131
132/* 64-BIT VALUE TO STRING MATCHING */
133
134typedef struct _val64_string {
135 uint64_t value;
136 const char *strptr;
138
139WS_DLL_PUBLIC
140const char *
141val64_to_str(const uint64_t val, const val64_string *vs, const char *fmt)
142G_GNUC_PRINTF(3, 0);
143
144WS_DLL_PUBLIC
145const char *
146val64_to_str_const(const uint64_t val, const val64_string *vs, const char *unknown_str);
147
148WS_DLL_PUBLIC
149const char *
150try_val64_to_str(const uint64_t val, const val64_string *vs);
151
152WS_DLL_PUBLIC
153const char *
154try_val64_to_str_idx(const uint64_t val, const val64_string *vs, int *idx);
155
156/* STRING TO VALUE MATCHING */
157
158WS_DLL_PUBLIC
159uint32_t
160str_to_val(const char *val, const value_string *vs, const uint32_t err_val);
161
162WS_DLL_PUBLIC
163int
164str_to_val_idx(const char *val, const value_string *vs);
165
166/* EXTENDED VALUE TO STRING MATCHING */
167
169typedef const value_string *(*_value_string_match2_t)(const uint32_t, value_string_ext*);
170
172 _value_string_match2_t _vs_match2;
173 uint32_t _vs_first_value; /* first value of the value_string array */
174 unsigned _vs_num_entries; /* number of entries in the value_string array */
175 /* (excluding final {0, NULL}) */
176 const value_string *_vs_p; /* the value string array address */
177 const char *_vs_name; /* vse "Name" (for error messages) */
178};
179
180#define VALUE_STRING_EXT_VS_P(x) (x)->_vs_p
181#define VALUE_STRING_EXT_VS_NUM_ENTRIES(x) (x)->_vs_num_entries
182#define VALUE_STRING_EXT_VS_NAME(x) (x)->_vs_name
183
184WS_DLL_PUBLIC
185const value_string *
186_try_val_to_str_ext_init(const uint32_t val, value_string_ext *vse);
187#define VALUE_STRING_EXT_INIT(x) { _try_val_to_str_ext_init, 0, G_N_ELEMENTS(x)-1, x, #x }
188
189WS_DLL_PUBLIC
191value_string_ext_new(const value_string *vs, unsigned vs_tot_num_entries, const char *vs_name);
192
193WS_DLL_PUBLIC
194void
195value_string_ext_free(value_string_ext *vse);
196
197WS_DLL_PUBLIC
198const char *
199val_to_str_ext(const uint32_t val, value_string_ext *vse, const char *fmt)
200G_GNUC_PRINTF(3, 0);
201
202WS_DLL_PUBLIC
203char *
204val_to_str_ext_wmem(wmem_allocator_t *scope, const uint32_t val, value_string_ext *vse, const char *fmt)
205G_GNUC_PRINTF(4, 0);
206
207WS_DLL_PUBLIC
208const char *
209val_to_str_ext_const(const uint32_t val, value_string_ext *vs, const char *unknown_str);
210
211WS_DLL_PUBLIC
212const char *
213try_val_to_str_ext(const uint32_t val, value_string_ext *vse);
214
215WS_DLL_PUBLIC
216const char *
217try_val_to_str_idx_ext(const uint32_t val, value_string_ext *vse, int *idx);
218
219/* EXTENDED 64-BIT VALUE TO STRING MATCHING */
220
222typedef const val64_string *(*_val64_string_match2_t)(const uint64_t, val64_string_ext*);
223
225 _val64_string_match2_t _vs_match2;
226 uint64_t _vs_first_value; /* first value of the val64_string array */
227 unsigned _vs_num_entries; /* number of entries in the val64_string array */
228 /* (excluding final {0, NULL}) */
229 const val64_string *_vs_p; /* the value string array address */
230 const char *_vs_name; /* vse "Name" (for error messages) */
231};
232
233#define VAL64_STRING_EXT_VS_P(x) (x)->_vs_p
234#define VAL64_STRING_EXT_VS_NUM_ENTRIES(x) (x)->_vs_num_entries
235#define VAL64_STRING_EXT_VS_NAME(x) (x)->_vs_name
236
237WS_DLL_PUBLIC
238int
239value_str_value_compare(const void* a, const void* b);
240
241WS_DLL_PUBLIC
242const val64_string *
243_try_val64_to_str_ext_init(const uint64_t val, val64_string_ext *vse);
244#define VAL64_STRING_EXT_INIT(x) { _try_val64_to_str_ext_init, 0, G_N_ELEMENTS(x)-1, x, #x }
245
246WS_DLL_PUBLIC
248val64_string_ext_new(const val64_string *vs, unsigned vs_tot_num_entries, const char *vs_name);
249
250WS_DLL_PUBLIC
251void
252val64_string_ext_free(val64_string_ext *vse);
253
254WS_DLL_PUBLIC
255const char *
256val64_to_str_ext(const uint64_t val, val64_string_ext *vse, const char *fmt)
257G_GNUC_PRINTF(3, 0);
258
259WS_DLL_PUBLIC
260char *
261val64_to_str_ext_wmem(wmem_allocator_t *scope, const uint64_t val, val64_string_ext *vse, const char *fmt)
262G_GNUC_PRINTF(4, 0);
263
264WS_DLL_PUBLIC
265const char *
266val64_to_str_ext_const(const uint64_t val, val64_string_ext *vs, const char *unknown_str);
267
268WS_DLL_PUBLIC
269const char *
270try_val64_to_str_ext(const uint64_t val, val64_string_ext *vse);
271
272WS_DLL_PUBLIC
273const char *
274try_val64_to_str_idx_ext(const uint64_t val, val64_string_ext *vse, int *idx);
275
276/* STRING TO STRING MATCHING */
277
278typedef struct _string_string {
279 const char *value;
280 const char *strptr;
282
283WS_DLL_PUBLIC
284const char *
285str_to_str(const char *val, const string_string *vs, const char *fmt)
286G_GNUC_PRINTF(3, 0);
287
288WS_DLL_PUBLIC
289const char *
290try_str_to_str(const char *val, const string_string *vs);
291
292WS_DLL_PUBLIC
293const char *
294try_str_to_str_idx(const char *val, const string_string *vs, int *idx);
295
296/* RANGE TO STRING MATCHING */
297
298typedef struct _range_string {
299 uint64_t value_min;
300 uint64_t value_max;
301 const char *strptr;
303
304WS_DLL_PUBLIC
305const char *
306rval_to_str(const uint32_t val, const range_string *rs, const char *fmt)
307G_GNUC_PRINTF(3, 0);
308
309WS_DLL_PUBLIC
310const char *
311rval_to_str_const(const uint32_t val, const range_string *rs, const char *unknown_str);
312
313WS_DLL_PUBLIC
314const char *
315try_rval_to_str(const uint32_t val, const range_string *rs);
316
317WS_DLL_PUBLIC
318const char *
319try_rval_to_str_idx(const uint32_t val, const range_string *rs, int *idx);
320
321WS_DLL_PUBLIC
322const char *
323try_rval64_to_str(const uint64_t val, const range_string *rs);
324
325WS_DLL_PUBLIC
326const char *
327try_rval64_to_str_idx(const uint64_t val, const range_string *rs, int *idx);
328
329/* TIME TO STRING MATCHING */
330
331typedef struct _time_value_string {
332 nstime_t value;
333 const char *strptr;
335
336WS_DLL_PUBLIC
337const char *
338try_time_val_to_str(const nstime_t *val, const time_value_string *vs);
339
340/* BYTES TO STRING MATCHING */
341
342typedef struct _bytes_string {
343 const uint8_t *value;
344 const size_t value_length;
345 const char *strptr;
347
348WS_DLL_PUBLIC
349const char *
350bytesval_to_str(const uint8_t *val, const size_t val_len, const bytes_string *bs, const char *fmt)
351G_GNUC_PRINTF(4, 0);
352
353WS_DLL_PUBLIC
354const char *
355try_bytesval_to_str(const uint8_t *val, const size_t val_len, const bytes_string *bs);
356
357WS_DLL_PUBLIC
358const char *
359bytesprefix_to_str(const uint8_t *haystack, const size_t haystack_len, const bytes_string *bs, const char *fmt)
360G_GNUC_PRINTF(4, 0);
361
362WS_DLL_PUBLIC
363const char *
364try_bytesprefix_to_str(const uint8_t *haystack, const size_t haystack_len, const bytes_string *bs);
365
366/* MISC (generally do not use) */
367
368WS_DLL_LOCAL
369bool
370value_string_ext_validate(const value_string_ext *vse);
371
372WS_DLL_LOCAL
373const char *
374value_string_ext_match_type_str(const value_string_ext *vse);
375
376WS_DLL_LOCAL
377bool
378val64_string_ext_validate(const val64_string_ext *vse);
379
380WS_DLL_LOCAL
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:342
Definition value_string.h:298
Definition value_string.h:278
Definition value_string.h:331
Definition value_string.h:224
Definition value_string.h:134
Definition value_string.h:171
Definition value_string.h:27
Definition wmem_allocator.h:27
Definition nstime.h:26