Wireshark 4.5.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#include <epan/wmem_scopes.h>
18
19#ifdef __cplusplus
20extern "C" {
21#endif /* __cplusplus */
22
23/* VALUE TO STRING MATCHING */
24
25typedef struct _value_string {
26 uint32_t value;
27 const char *strptr;
29
30#if 0
31 /* ----- VALUE_STRING "Helper" macros ----- */
32
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. */
35
36 /* Usage: */
37
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" )
42
43 /*- gen enum -*/
44 VALUE_STRING_ENUM(foo); /* gen's 'enum {FOO_A=1, FOO_B=3};' */
45
46 /*- gen value_string array -*/
47 /* local */
48 VALUE_STRING_ARRAY(foo); /* gen's 'static const value_string foo[] = {{1,"aaa"}, {3,"bbb"}}; */
49
50 /* global */
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[]; */
53
54 /* Alternatively: */
55 #define bar_VALUE_STRING_LIST(XXX) \
56 XXX( BAR_A, 1) \
57 XXX( BAR_B, 3)
58
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"}}; */
61 ...
62#endif
63
64/* -- Public -- */
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)
69
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)
74
75/* -- Private -- */
76#define _VS_ENUM_XXX(array_name, macro) \
77enum { \
78 array_name##_VALUE_STRING_LIST(macro) \
79 _##array_name##_ENUM_DUMMY = 0 \
80}
81
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) \
85 { 0, NULL } \
86}
87
88#define _VS_ARRAY_XXX(array_name, macro) \
89 _VS_ARRAY_TYPE_NAME(array_name) = { \
90 array_name##_VALUE_STRING_LIST(macro) \
91 { 0, NULL } \
92}
93
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[]
96
97#define _VS_ENUM_ENTRY( name, value, string) name = value,
98#define _VS_ARRAY_ENTRY(name, value, string) { value, string },
99
100#define _VS_ENUM_ENTRY2( name, value) name = value,
101#define _VS_ARRAY_ENTRY2(name, value) { value, #name },
102/* ----- ----- */
103
104WS_DLL_PUBLIC
105const char *
106val_to_str(const uint32_t val, const value_string *vs, const char *fmt)
107G_GNUC_PRINTF(3, 0);
108
109WS_DLL_PUBLIC
110char *
111val_to_str_wmem(wmem_allocator_t *scope, const uint32_t val, const value_string *vs, const char *fmt)
112G_GNUC_PRINTF(4, 0);
113
114WS_DLL_PUBLIC
115const char *
116val_to_str_const(const uint32_t val, const value_string *vs, const char *unknown_str);
117
118WS_DLL_PUBLIC
119const char *
120try_val_to_str(const uint32_t val, const value_string *vs);
121
122WS_DLL_PUBLIC
123const char *
124try_val_to_str_idx(const uint32_t val, const value_string *vs, int *idx);
125
126WS_DLL_PUBLIC
127const char *
128char_val_to_str(char val, const value_string *vs, const char *msg);
129
130/* 64-BIT VALUE TO STRING MATCHING */
131
132typedef struct _val64_string {
133 uint64_t value;
134 const char *strptr;
136
137WS_DLL_PUBLIC
138const char *
139val64_to_str(const uint64_t val, const val64_string *vs, const char *fmt)
140G_GNUC_PRINTF(3, 0);
141
142WS_DLL_PUBLIC
143const char *
144val64_to_str_const(const uint64_t val, const val64_string *vs, const char *unknown_str);
145
146WS_DLL_PUBLIC
147const char *
148try_val64_to_str(const uint64_t val, const val64_string *vs);
149
150WS_DLL_PUBLIC
151const char *
152try_val64_to_str_idx(const uint64_t val, const val64_string *vs, int *idx);
153
154/* STRING TO VALUE MATCHING */
155
156WS_DLL_PUBLIC
157uint32_t
158str_to_val(const char *val, const value_string *vs, const uint32_t err_val);
159
160WS_DLL_PUBLIC
161int
162str_to_val_idx(const char *val, const value_string *vs);
163
164/* EXTENDED VALUE TO STRING MATCHING */
165
167typedef const value_string *(*_value_string_match2_t)(const uint32_t, value_string_ext*);
168
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) */
176};
177
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
181
182WS_DLL_PUBLIC
183const value_string *
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 }
186
187WS_DLL_PUBLIC
189value_string_ext_new(const value_string *vs, unsigned vs_tot_num_entries, const char *vs_name);
190
191WS_DLL_PUBLIC
192void
193value_string_ext_free(value_string_ext *vse);
194
195WS_DLL_PUBLIC
196const char *
197val_to_str_ext(const uint32_t val, value_string_ext *vse, const char *fmt)
198G_GNUC_PRINTF(3, 0);
199
200WS_DLL_PUBLIC
201char *
202val_to_str_ext_wmem(wmem_allocator_t *scope, const uint32_t val, value_string_ext *vse, const char *fmt)
203G_GNUC_PRINTF(4, 0);
204
205WS_DLL_PUBLIC
206const char *
207val_to_str_ext_const(const uint32_t val, value_string_ext *vs, const char *unknown_str);
208
209WS_DLL_PUBLIC
210const char *
211try_val_to_str_ext(const uint32_t val, value_string_ext *vse);
212
213WS_DLL_PUBLIC
214const char *
215try_val_to_str_idx_ext(const uint32_t val, value_string_ext *vse, int *idx);
216
217/* EXTENDED 64-BIT VALUE TO STRING MATCHING */
218
220typedef const val64_string *(*_val64_string_match2_t)(const uint64_t, val64_string_ext*);
221
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) */
229};
230
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
234
235WS_DLL_PUBLIC
236const val64_string *
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 }
239
240WS_DLL_PUBLIC
242val64_string_ext_new(const val64_string *vs, unsigned vs_tot_num_entries, const char *vs_name);
243
244WS_DLL_PUBLIC
245void
246val64_string_ext_free(val64_string_ext *vse);
247
248WS_DLL_PUBLIC
249const char *
250val64_to_str_ext(const uint64_t val, val64_string_ext *vse, const char *fmt)
251G_GNUC_PRINTF(3, 0);
252
253WS_DLL_PUBLIC
254char *
255val64_to_str_ext_wmem(wmem_allocator_t *scope, const uint64_t val, val64_string_ext *vse, const char *fmt)
256G_GNUC_PRINTF(4, 0);
257
258WS_DLL_PUBLIC
259const char *
260val64_to_str_ext_const(const uint64_t val, val64_string_ext *vs, const char *unknown_str);
261
262WS_DLL_PUBLIC
263const char *
264try_val64_to_str_ext(const uint64_t val, val64_string_ext *vse);
265
266WS_DLL_PUBLIC
267const char *
268try_val64_to_str_idx_ext(const uint64_t val, val64_string_ext *vse, int *idx);
269
270/* STRING TO STRING MATCHING */
271
272typedef struct _string_string {
273 const char *value;
274 const char *strptr;
276
277WS_DLL_PUBLIC
278const char *
279str_to_str(const char *val, const string_string *vs, const char *fmt)
280G_GNUC_PRINTF(3, 0);
281
282WS_DLL_PUBLIC
283const char *
284try_str_to_str(const char *val, const string_string *vs);
285
286WS_DLL_PUBLIC
287const char *
288try_str_to_str_idx(const char *val, const string_string *vs, int *idx);
289
290/* RANGE TO STRING MATCHING */
291
292typedef struct _range_string {
293 uint64_t value_min;
294 uint64_t value_max;
295 const char *strptr;
297
298WS_DLL_PUBLIC
299const char *
300rval_to_str(const uint32_t val, const range_string *rs, const char *fmt)
301G_GNUC_PRINTF(3, 0);
302
303WS_DLL_PUBLIC
304const char *
305rval_to_str_const(const uint32_t val, const range_string *rs, const char *unknown_str);
306
307WS_DLL_PUBLIC
308const char *
309try_rval_to_str(const uint32_t val, const range_string *rs);
310
311WS_DLL_PUBLIC
312const char *
313try_rval_to_str_idx(const uint32_t val, const range_string *rs, int *idx);
314
315WS_DLL_PUBLIC
316const char *
317try_rval64_to_str(const uint64_t val, const range_string *rs);
318
319WS_DLL_PUBLIC
320const char *
321try_rval64_to_str_idx(const uint64_t val, const range_string *rs, int *idx);
322
323/* BYTES TO STRING MATCHING */
324
325typedef struct _bytes_string {
326 const uint8_t *value;
327 const size_t value_length;
328 const char *strptr;
330
331WS_DLL_PUBLIC
332const char *
333bytesval_to_str(const uint8_t *val, const size_t val_len, const bytes_string *bs, const char *fmt)
334G_GNUC_PRINTF(4, 0);
335
336WS_DLL_PUBLIC
337const char *
338try_bytesval_to_str(const uint8_t *val, const size_t val_len, const bytes_string *bs);
339
340WS_DLL_PUBLIC
341const char *
342bytesprefix_to_str(const uint8_t *haystack, const size_t haystack_len, const bytes_string *bs, const char *fmt)
343G_GNUC_PRINTF(4, 0);
344
345WS_DLL_PUBLIC
346const char *
347try_bytesprefix_to_str(const uint8_t *haystack, const size_t haystack_len, const bytes_string *bs);
348
349/* MISC (generally do not use) */
350
351WS_DLL_LOCAL
352bool
353value_string_ext_validate(const value_string_ext *vse);
354
355WS_DLL_LOCAL
356const char *
357value_string_ext_match_type_str(const value_string_ext *vse);
358
359WS_DLL_LOCAL
360bool
361val64_string_ext_validate(const val64_string_ext *vse);
362
363WS_DLL_LOCAL
364const char *
365val64_string_ext_match_type_str(const val64_string_ext *vse);
366
367#ifdef __cplusplus
368}
369#endif /* __cplusplus */
370
371#endif /* __VALUE_STRING_H__ */
372
373/*
374 * Editor modelines - https://www.wireshark.org/tools/modelines.html
375 *
376 * Local variables:
377 * c-basic-offset: 4
378 * tab-width: 8
379 * indent-tabs-mode: nil
380 * End:
381 *
382 * vi: set shiftwidth=4 tabstop=8 expandtab:
383 * :indentSize=4:tabSize=8:noTabs=true:
384 */
Definition value_string.h:325
Definition value_string.h:292
Definition value_string.h:272
Definition value_string.h:222
Definition value_string.h:132
Definition value_string.h:169
Definition value_string.h:25
Definition wmem_allocator.h:27