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
236int
237value_str_value_compare(const void* a, const void* b);
238
239WS_DLL_PUBLIC
240const val64_string *
241_try_val64_to_str_ext_init(const uint64_t val, val64_string_ext *vse);
242#define VAL64_STRING_EXT_INIT(x) { _try_val64_to_str_ext_init, 0, G_N_ELEMENTS(x)-1, x, #x }
243
244WS_DLL_PUBLIC
246val64_string_ext_new(const val64_string *vs, unsigned vs_tot_num_entries, const char *vs_name);
247
248WS_DLL_PUBLIC
249void
250val64_string_ext_free(val64_string_ext *vse);
251
252WS_DLL_PUBLIC
253const char *
254val64_to_str_ext(const uint64_t val, val64_string_ext *vse, const char *fmt)
255G_GNUC_PRINTF(3, 0);
256
257WS_DLL_PUBLIC
258char *
259val64_to_str_ext_wmem(wmem_allocator_t *scope, const uint64_t val, val64_string_ext *vse, const char *fmt)
260G_GNUC_PRINTF(4, 0);
261
262WS_DLL_PUBLIC
263const char *
264val64_to_str_ext_const(const uint64_t val, val64_string_ext *vs, const char *unknown_str);
265
266WS_DLL_PUBLIC
267const char *
268try_val64_to_str_ext(const uint64_t val, val64_string_ext *vse);
269
270WS_DLL_PUBLIC
271const char *
272try_val64_to_str_idx_ext(const uint64_t val, val64_string_ext *vse, int *idx);
273
274/* STRING TO STRING MATCHING */
275
276typedef struct _string_string {
277 const char *value;
278 const char *strptr;
280
281WS_DLL_PUBLIC
282const char *
283str_to_str(const char *val, const string_string *vs, const char *fmt)
284G_GNUC_PRINTF(3, 0);
285
286WS_DLL_PUBLIC
287const char *
288try_str_to_str(const char *val, const string_string *vs);
289
290WS_DLL_PUBLIC
291const char *
292try_str_to_str_idx(const char *val, const string_string *vs, int *idx);
293
294/* RANGE TO STRING MATCHING */
295
296typedef struct _range_string {
297 uint64_t value_min;
298 uint64_t value_max;
299 const char *strptr;
301
302WS_DLL_PUBLIC
303const char *
304rval_to_str(const uint32_t val, const range_string *rs, const char *fmt)
305G_GNUC_PRINTF(3, 0);
306
307WS_DLL_PUBLIC
308const char *
309rval_to_str_const(const uint32_t val, const range_string *rs, const char *unknown_str);
310
311WS_DLL_PUBLIC
312const char *
313try_rval_to_str(const uint32_t val, const range_string *rs);
314
315WS_DLL_PUBLIC
316const char *
317try_rval_to_str_idx(const uint32_t val, const range_string *rs, int *idx);
318
319WS_DLL_PUBLIC
320const char *
321try_rval64_to_str(const uint64_t val, const range_string *rs);
322
323WS_DLL_PUBLIC
324const char *
325try_rval64_to_str_idx(const uint64_t val, const range_string *rs, int *idx);
326
327/* BYTES TO STRING MATCHING */
328
329typedef struct _bytes_string {
330 const uint8_t *value;
331 const size_t value_length;
332 const char *strptr;
334
335WS_DLL_PUBLIC
336const char *
337bytesval_to_str(const uint8_t *val, const size_t val_len, const bytes_string *bs, const char *fmt)
338G_GNUC_PRINTF(4, 0);
339
340WS_DLL_PUBLIC
341const char *
342try_bytesval_to_str(const uint8_t *val, const size_t val_len, const bytes_string *bs);
343
344WS_DLL_PUBLIC
345const char *
346bytesprefix_to_str(const uint8_t *haystack, const size_t haystack_len, const bytes_string *bs, const char *fmt)
347G_GNUC_PRINTF(4, 0);
348
349WS_DLL_PUBLIC
350const char *
351try_bytesprefix_to_str(const uint8_t *haystack, const size_t haystack_len, const bytes_string *bs);
352
353/* MISC (generally do not use) */
354
355WS_DLL_LOCAL
356bool
357value_string_ext_validate(const value_string_ext *vse);
358
359WS_DLL_LOCAL
360const char *
361value_string_ext_match_type_str(const value_string_ext *vse);
362
363WS_DLL_LOCAL
364bool
365val64_string_ext_validate(const val64_string_ext *vse);
366
367WS_DLL_LOCAL
368const char *
369val64_string_ext_match_type_str(const val64_string_ext *vse);
370
371#ifdef __cplusplus
372}
373#endif /* __cplusplus */
374
375#endif /* __VALUE_STRING_H__ */
376
377/*
378 * Editor modelines - https://www.wireshark.org/tools/modelines.html
379 *
380 * Local variables:
381 * c-basic-offset: 4
382 * tab-width: 8
383 * indent-tabs-mode: nil
384 * End:
385 *
386 * vi: set shiftwidth=4 tabstop=8 expandtab:
387 * :indentSize=4:tabSize=8:noTabs=true:
388 */
Definition value_string.h:329
Definition value_string.h:296
Definition value_string.h:276
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