dcerpc-nt: add UNION_ALIGN_TO... helpers
[wireshark-sm.git] / epan / value_string.h
blobefa4310c0643c9aefc339193fa95b868f4f6ded3
1 /** @file
2 * Definitions for value_string structures and routines
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
11 #ifndef __VALUE_STRING_H__
12 #define __VALUE_STRING_H__
14 #include <stdint.h>
16 #include "ws_symbol_export.h"
17 #include <epan/wmem_scopes.h>
19 #ifdef __cplusplus
20 extern "C" {
21 #endif /* __cplusplus */
23 /* VALUE TO STRING MATCHING */
25 typedef struct _value_string {
26 uint32_t value;
27 const char *strptr;
28 } value_string;
30 #if 0
31 /* ----- VALUE_STRING "Helper" macros ----- */
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. */
36 /* Usage: */
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" )
43 /*- gen enum -*/
44 VALUE_STRING_ENUM(foo); /* gen's 'enum {FOO_A=1, FOO_B=3};' */
46 /*- gen value_string array -*/
47 /* local */
48 VALUE_STRING_ARRAY(foo); /* gen's 'static const value_string foo[] = {{1,"aaa"}, {3,"bbb"}}; */
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[]; */
54 /* Alternatively: */
55 #define bar_VALUE_STRING_LIST(XXX) \
56 XXX( BAR_A, 1) \
57 XXX( BAR_B, 3)
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
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)
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)
75 /* -- Private -- */
76 #define _VS_ENUM_XXX(array_name, macro) \
77 enum { \
78 array_name##_VALUE_STRING_LIST(macro) \
79 _##array_name##_ENUM_DUMMY = 0 \
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 } \
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 } \
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[]
97 #define _VS_ENUM_ENTRY( name, value, string) name = value,
98 #define _VS_ARRAY_ENTRY(name, value, string) { value, string },
100 #define _VS_ENUM_ENTRY2( name, value) name = value,
101 #define _VS_ARRAY_ENTRY2(name, value) { value, #name },
102 /* ----- ----- */
104 WS_DLL_PUBLIC
105 const char *
106 val_to_str(const uint32_t val, const value_string *vs, const char *fmt)
107 G_GNUC_PRINTF(3, 0);
109 WS_DLL_PUBLIC
110 char *
111 val_to_str_wmem(wmem_allocator_t *scope, const uint32_t val, const value_string *vs, const char *fmt)
112 G_GNUC_PRINTF(4, 0);
114 WS_DLL_PUBLIC
115 const char *
116 val_to_str_const(const uint32_t val, const value_string *vs, const char *unknown_str);
118 WS_DLL_PUBLIC
119 const char *
120 try_val_to_str(const uint32_t val, const value_string *vs);
122 WS_DLL_PUBLIC
123 const char *
124 try_val_to_str_idx(const uint32_t val, const value_string *vs, int *idx);
126 WS_DLL_PUBLIC
127 const char *
128 char_val_to_str(char val, const value_string *vs, const char *msg);
130 /* 64-BIT VALUE TO STRING MATCHING */
132 typedef struct _val64_string {
133 uint64_t value;
134 const char *strptr;
135 } val64_string;
137 WS_DLL_PUBLIC
138 const char *
139 val64_to_str(const uint64_t val, const val64_string *vs, const char *fmt)
140 G_GNUC_PRINTF(3, 0);
142 WS_DLL_PUBLIC
143 const char *
144 val64_to_str_const(const uint64_t val, const val64_string *vs, const char *unknown_str);
146 WS_DLL_PUBLIC
147 const char *
148 try_val64_to_str(const uint64_t val, const val64_string *vs);
150 WS_DLL_PUBLIC
151 const char *
152 try_val64_to_str_idx(const uint64_t val, const val64_string *vs, int *idx);
154 /* STRING TO VALUE MATCHING */
156 WS_DLL_PUBLIC
157 uint32_t
158 str_to_val(const char *val, const value_string *vs, const uint32_t err_val);
160 WS_DLL_PUBLIC
162 str_to_val_idx(const char *val, const value_string *vs);
164 /* EXTENDED VALUE TO STRING MATCHING */
166 typedef struct _value_string_ext value_string_ext;
167 typedef const value_string *(*_value_string_match2_t)(const uint32_t, value_string_ext*);
169 struct _value_string_ext {
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) */
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
182 WS_DLL_PUBLIC
183 const 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 }
187 WS_DLL_PUBLIC
188 value_string_ext *
189 value_string_ext_new(const value_string *vs, unsigned vs_tot_num_entries, const char *vs_name);
191 WS_DLL_PUBLIC
192 void
193 value_string_ext_free(value_string_ext *vse);
195 WS_DLL_PUBLIC
196 const char *
197 val_to_str_ext(const uint32_t val, value_string_ext *vse, const char *fmt)
198 G_GNUC_PRINTF(3, 0);
200 WS_DLL_PUBLIC
201 char *
202 val_to_str_ext_wmem(wmem_allocator_t *scope, const uint32_t val, value_string_ext *vse, const char *fmt)
203 G_GNUC_PRINTF(4, 0);
205 WS_DLL_PUBLIC
206 const char *
207 val_to_str_ext_const(const uint32_t val, value_string_ext *vs, const char *unknown_str);
209 WS_DLL_PUBLIC
210 const char *
211 try_val_to_str_ext(const uint32_t val, value_string_ext *vse);
213 WS_DLL_PUBLIC
214 const char *
215 try_val_to_str_idx_ext(const uint32_t val, value_string_ext *vse, int *idx);
217 /* EXTENDED 64-BIT VALUE TO STRING MATCHING */
219 typedef struct _val64_string_ext val64_string_ext;
220 typedef const val64_string *(*_val64_string_match2_t)(const uint64_t, val64_string_ext*);
222 struct _val64_string_ext {
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) */
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
235 WS_DLL_PUBLIC
236 const 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 }
240 WS_DLL_PUBLIC
241 val64_string_ext *
242 val64_string_ext_new(const val64_string *vs, unsigned vs_tot_num_entries, const char *vs_name);
244 WS_DLL_PUBLIC
245 void
246 val64_string_ext_free(val64_string_ext *vse);
248 WS_DLL_PUBLIC
249 const char *
250 val64_to_str_ext(const uint64_t val, val64_string_ext *vse, const char *fmt)
251 G_GNUC_PRINTF(3, 0);
253 WS_DLL_PUBLIC
254 char *
255 val64_to_str_ext_wmem(wmem_allocator_t *scope, const uint64_t val, val64_string_ext *vse, const char *fmt)
256 G_GNUC_PRINTF(4, 0);
258 WS_DLL_PUBLIC
259 const char *
260 val64_to_str_ext_const(const uint64_t val, val64_string_ext *vs, const char *unknown_str);
262 WS_DLL_PUBLIC
263 const char *
264 try_val64_to_str_ext(const uint64_t val, val64_string_ext *vse);
266 WS_DLL_PUBLIC
267 const char *
268 try_val64_to_str_idx_ext(const uint64_t val, val64_string_ext *vse, int *idx);
270 /* STRING TO STRING MATCHING */
272 typedef struct _string_string {
273 const char *value;
274 const char *strptr;
275 } string_string;
277 WS_DLL_PUBLIC
278 const char *
279 str_to_str(const char *val, const string_string *vs, const char *fmt)
280 G_GNUC_PRINTF(3, 0);
282 WS_DLL_PUBLIC
283 const char *
284 try_str_to_str(const char *val, const string_string *vs);
286 WS_DLL_PUBLIC
287 const char *
288 try_str_to_str_idx(const char *val, const string_string *vs, int *idx);
290 /* RANGE TO STRING MATCHING */
292 typedef struct _range_string {
293 uint64_t value_min;
294 uint64_t value_max;
295 const char *strptr;
296 } range_string;
298 WS_DLL_PUBLIC
299 const char *
300 rval_to_str(const uint32_t val, const range_string *rs, const char *fmt)
301 G_GNUC_PRINTF(3, 0);
303 WS_DLL_PUBLIC
304 const char *
305 rval_to_str_const(const uint32_t val, const range_string *rs, const char *unknown_str);
307 WS_DLL_PUBLIC
308 const char *
309 try_rval_to_str(const uint32_t val, const range_string *rs);
311 WS_DLL_PUBLIC
312 const char *
313 try_rval_to_str_idx(const uint32_t val, const range_string *rs, int *idx);
315 WS_DLL_PUBLIC
316 const char *
317 try_rval64_to_str(const uint64_t val, const range_string *rs);
319 WS_DLL_PUBLIC
320 const char *
321 try_rval64_to_str_idx(const uint64_t val, const range_string *rs, int *idx);
323 /* BYTES TO STRING MATCHING */
325 typedef struct _bytes_string {
326 const uint8_t *value;
327 const size_t value_length;
328 const char *strptr;
329 } bytes_string;
331 WS_DLL_PUBLIC
332 const char *
333 bytesval_to_str(const uint8_t *val, const size_t val_len, const bytes_string *bs, const char *fmt)
334 G_GNUC_PRINTF(4, 0);
336 WS_DLL_PUBLIC
337 const char *
338 try_bytesval_to_str(const uint8_t *val, const size_t val_len, const bytes_string *bs);
340 WS_DLL_PUBLIC
341 const char *
342 bytesprefix_to_str(const uint8_t *haystack, const size_t haystack_len, const bytes_string *bs, const char *fmt)
343 G_GNUC_PRINTF(4, 0);
345 WS_DLL_PUBLIC
346 const char *
347 try_bytesprefix_to_str(const uint8_t *haystack, const size_t haystack_len, const bytes_string *bs);
349 /* MISC (generally do not use) */
351 WS_DLL_LOCAL
352 bool
353 value_string_ext_validate(const value_string_ext *vse);
355 WS_DLL_LOCAL
356 const char *
357 value_string_ext_match_type_str(const value_string_ext *vse);
359 WS_DLL_LOCAL
360 bool
361 val64_string_ext_validate(const val64_string_ext *vse);
363 WS_DLL_LOCAL
364 const char *
365 val64_string_ext_match_type_str(const val64_string_ext *vse);
367 #ifdef __cplusplus
369 #endif /* __cplusplus */
371 #endif /* __VALUE_STRING_H__ */
374 * Editor modelines - https://www.wireshark.org/tools/modelines.html
376 * Local variables:
377 * c-basic-offset: 4
378 * tab-width: 8
379 * indent-tabs-mode: nil
380 * End:
382 * vi: set shiftwidth=4 tabstop=8 expandtab:
383 * :indentSize=4:tabSize=8:noTabs=true: