PA-PK-AS-REP-Win2k ::= PaPkAsRep
[metze/wireshark/wip.git] / epan / uat.h
1 /*
2  *  uat.h
3  *
4  *  User Accessible Tables
5  *  Maintain an array of user accessible data structures
6  *
7  * (c) 2007, Luis E. Garcia Ontanon <luis@ontanon.org>
8  *
9  * Wireshark - Network traffic analyzer
10  * By Gerald Combs <gerald@wireshark.org>
11  * Copyright 2001 Gerald Combs
12  *
13  * SPDX-License-Identifier: GPL-2.0-or-later
14  */
15
16 #ifndef __UAT_H__
17 #define __UAT_H__
18
19 #include <stdlib.h>
20
21 #include "ws_symbol_export.h"
22 #include <wsutil/strtoi.h>
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif /* __cplusplus */
27
28 /*
29  * UAT maintains a dynamically allocated table accessible to the user
30  * via a file and/or via GUI preference dialogs.
31  *
32  * The file is read from and written in the personal configuration directory. If
33  * there is no such file, defaults will be loaded from the global data
34  * directory.
35  *
36  * The behaviour of the table is controlled by a series of callbacks which
37  * the caller (e.g. a dissector) must provide.
38  *
39  * BEWARE that the user can change an UAT at (almost) any time (via the GUI).
40  * That is, pointers to records in an UAT are valid only during the call
41  * to the function that obtains them (do not store pointers to these records).
42  * The records contents are only guaranteed to be valid in the post_update_cb
43  * function. (Implementation detail: currently a race condition is possible
44  * where the UAT consumer (dissector code) tries to use the UAT while the GUI
45  * user frees a record resulting in use-after-free. This is not ideal and might
46  * be fixed later.)
47  *
48  * UATs are meant for short tables of user data (passwords and such), there is
49  * no quick access, you must iterate through them each time to fetch the record
50  * you are looking for.
51  *
52  * Only users via GUI or editing the file can add/remove records, your
53  * (dissector) code cannot.
54  */
55
56 /* obscure data type to handle an uat */
57 typedef struct epan_uat uat_t;
58 /********************************************
59  * Callbacks:
60  * these instruct uat on how to deal with user info and data in records
61  ********************************************/
62
63 /********
64  * Callbacks dealing with the entire table
65  ********/
66
67 /*
68  * Post-Update CB
69  *
70  * To be called by the GUI code after to the table has being edited.
71  * Will be called once the user clicks the Apply or OK button
72  * optional
73  */
74 typedef void (*uat_post_update_cb_t)(void);
75
76
77 /********
78  * Callbacks dealing with records (these deal with entire records)
79  ********/
80
81 /**
82  * Copy CB
83  * copy(dest, source, len)
84  *
85  * Used to duplicate the contents of one record to another.
86  * Optional, memcpy will be used if not given.
87  */
88 typedef void* (*uat_copy_cb_t)(void *dest, const void *source, size_t len);
89
90 /**
91  * Free CB
92  * free(record)
93  *
94  * Destroy the contents of a record, possibly freeing some fields.
95  * Do not free the container itself, this memory is owned by the UAT core.
96  * Optional if the record contains no pointers that need to be freed.
97  */
98 typedef void (*uat_free_cb_t)(void *record);
99
100 /**
101  * Reset DB
102  *
103  * Used to free resources associated with a UAT loaded from file (e.g. post_update_cb)
104  * Optional.
105  */
106 typedef void (*uat_reset_cb_t)(void);
107
108 /**
109  * Update CB
110  * update(record,&error)
111  *
112  * Validates the contents of the record contents, to be called after any record
113  * fields had been updated (either from file or after modifications in the GUI).
114  *
115  * Optional, the record will be considered valid if the callback is omitted.
116  * It must return TRUE if the contents are considered valid and FALSE otherwise
117  * in which case the failure reason is set in 'error'. The error string will be
118  * freed by g_free.
119  */
120 typedef gboolean (*uat_update_cb_t)(void *record, char **error);
121
122
123 /*******
124  * Callbacks for single fields (these deal with single values)
125  * the caller should provide one of these for every field!
126  ********/
127
128 /*
129  * Check CB
130  * chk(record, ptr, len, chk_data, fld_data, &error)
131  *
132  * given an input string (ptr, len) checks if the value is OK for a field in the record.
133  * it will return TRUE if OK or else
134  * it will return FALSE and set *error to inform the user on what's
135  * wrong with the given input
136  * The error string must be allocated with g_malloc() or
137  * a routine that calls it.
138  * optional, if not given any input is considered OK and the set cb will be called
139  */
140 typedef gboolean (*uat_fld_chk_cb_t)(void *record, const char *ptr, unsigned len, const void *chk_data, const void *fld_data, char **error);
141
142 /*
143  * Set Field CB
144  * set(record, ptr, len, set_data, fld_data)
145  *
146  * given an input string (ptr, len) sets the value of a field in the record,
147  * it is mandatory
148  */
149 typedef void (*uat_fld_set_cb_t)(void *record, const char *ptr, unsigned len, const void *set_data, const void *fld_data);
150
151 /*
152  * Convert-to-string CB
153  * tostr(record, &out_ptr, &out_len, tostr_data, fld_data)
154  *
155  * given a record returns a string representation of the field
156  * mandatory
157  */
158 typedef void (*uat_fld_tostr_cb_t)(void *record, char **out_ptr, unsigned *out_len, const void *tostr_data, const void *fld_data);
159
160 /***********
161  * Text Mode
162  *
163  * used for file and dialog representation of fields in columns,
164  * when the file is read it modifies the way the value is passed back to the fld_set_cb
165  * (see definition bellow for description)
166  ***********/
167
168 typedef enum _uat_text_mode_t {
169         PT_TXTMOD_NONE,
170         /* not used */
171
172         PT_TXTMOD_STRING,
173         /*
174          file:
175                  reads:
176                          ,"\x20\x00\x30", as " \00",3 ("space nil zero" of length 3)
177                          ,"", as "",0
178                          ,, as NULL,0
179                  writes:
180                          ,"\x20\x30\x00\x20", for " 0\0 ",4
181                          ,"", for *, 0
182                          ,, for NULL, *
183          dialog:
184                  accepts \x?? and other escapes
185                  gets "",0 on empty string
186          */
187         PT_TXTMOD_HEXBYTES,
188         /*
189          file:
190                  reads:
191                          ,A1b2C3d4, as "\xa1\xb2\xc3\xd4",4
192                          ,, as NULL,0
193                  writes:
194                          ,, on NULL, *
195                          ,a1b2c3d4, on "\xa1\xb2\xc3\xd4",4
196          dialog:
197                  interprets the following input ... as ...:
198                  "a1b2c3d4" as "\xa1\xb2\xc3\xd4",4
199                  "a1 b2:c3d4" as "\xa1\xb2\xc3\xd4",4
200                  "" as NULL,0
201                  "invalid" as NULL,3
202                  "a1b" as NULL, 1
203          */
204         PT_TXTMOD_ENUM,
205         /* Read/Writes/displays the string value (not number!) */
206
207         PT_TXTMOD_COLOR,
208         /* Reads/Writes/display color in #RRGGBB format */
209
210         PT_TXTMOD_FILENAME,
211         /* processed like a PT_TXTMOD_STRING, but shows a filename dialog */
212         PT_TXTMOD_DIRECTORYNAME,
213         /* processed like a PT_TXTMOD_STRING, but shows a directory dialog */
214         PT_TXTMOD_DISPLAY_FILTER,
215         /* processed like a PT_TXTMOD_STRING, but verifies display filter */
216         PT_TXTMOD_PROTO_FIELD,
217         /* processed like a PT_TXTMOD_STRING, but verifies protocol field name (e.g tcp.flags.syn) */
218         PT_TXTMOD_BOOL
219         /* Displays a checkbox for value */
220 } uat_text_mode_t;
221
222 /*
223  * Fields
224  *
225  *
226  */
227 typedef struct _uat_field_t {
228         const char* name;
229         const char* title;
230         uat_text_mode_t mode;
231
232         struct {
233                 uat_fld_chk_cb_t chk;
234                 uat_fld_set_cb_t set;
235                 uat_fld_tostr_cb_t tostr;
236         } cb;
237
238         struct {
239                 const void* chk;
240                 const void* set;
241                 const void* tostr;
242         } cbdata;
243
244         const void* fld_data;
245
246         const char* desc;
247         struct _fld_data_t* priv;
248 } uat_field_t;
249
250 #define FLDFILL NULL
251 #define UAT_END_FIELDS {NULL,NULL,PT_TXTMOD_NONE,{0,0,0},{0,0,0},0,0,FLDFILL}
252
253 /*
254  * Flags to indicate what the settings in this UAT affect.
255  * This is used when UATs are changed interactively, to indicate what needs
256  * to be redone when the UAT is changed.
257  */
258 #define UAT_AFFECTS_DISSECTION  0x00000001      /* affects packet dissection */
259 #define UAT_AFFECTS_FIELDS      0x00000002      /* affects what named fields exist */
260
261 /** Create a new uat
262  *
263  * @param name The name of the table
264  * @param size The size of the structure
265  * @param filename The filename to be used (either in userdir or datadir)
266  * @param from_profile TRUE if profile directory to be used
267  * @param data_ptr Although a void*, this is really a pointer to a null terminated array of pointers to the data
268  * @param num_items_ptr A pointer with number of items
269  * @param flags flags indicating what this UAT affects
270  * @param help A pointer to help text
271  * @param copy_cb A function that copies the data in the struct
272  * @param update_cb Will be called when a record is updated
273  * @param free_cb Will be called to destroy a struct in the dataset
274  * @param post_update_cb Will be called once the user clicks the Apply or OK button
275  * @param reset_cb Will be called to destroy internal data
276  * @param flds_array A pointer to an array of uat_field_t structs
277  *
278  * @return A freshly-allocated and populated uat_t struct.
279  */
280 WS_DLL_PUBLIC
281 uat_t* uat_new(const char* name,
282                            size_t size,
283                            const char* filename,
284                            gboolean from_profile,
285                            void* data_ptr,
286                            guint* num_items_ptr,
287                            guint flags,
288                            const char* help,
289                            uat_copy_cb_t copy_cb,
290                            uat_update_cb_t update_cb,
291                            uat_free_cb_t free_cb,
292                            uat_post_update_cb_t post_update_cb,
293                            uat_reset_cb_t reset_cb,
294                            uat_field_t* flds_array);
295
296 /** Cleanup all Uats
297  *
298  */
299 void uat_cleanup(void);
300
301 /** Populate a uat using its file.
302  *
303  * @param uat_in Pointer to a uat. Must not be NULL.
304  * @param filename Filename to load, NULL to fetch from current profile.
305  * @param err Upon failure, points to an error string.
306  *
307  * @return TRUE on success, FALSE on failure.
308  */
309 WS_DLL_PUBLIC
310 gboolean uat_load(uat_t* uat_in, const gchar *filename, char** err);
311
312 /** Create or update a single uat entry using a string.
313  *
314  * @param uat_in Pointer to a uat. Must not be NULL.
315  * @param entry The string representation of the entry. Format must match
316  * what's written to the uat's output file.
317  * @param err Upon failure, points to an error string.
318  *
319  * @return TRUE on success, FALSE on failure.
320  */
321 gboolean uat_load_str(uat_t* uat_in, char* entry, char** err);
322
323 /** Given a uat name or filename, find its pointer.
324  *
325  * @param name The name or filename of the uat
326  *
327  * @return A pointer to the uat on success, NULL on failure.
328  */
329 uat_t *uat_find(gchar *name);
330
331 WS_DLL_PUBLIC
332 uat_t* uat_get_table_by_name(const char* name);
333
334 /*
335  * Some common uat_fld_chk_cbs
336  */
337 WS_DLL_PUBLIC
338 gboolean uat_fld_chk_str(void*, const char*, unsigned, const void*, const void*, char** err);
339 gboolean uat_fld_chk_oid(void*, const char*, unsigned, const void*, const void*, char** err);
340 WS_DLL_PUBLIC
341 gboolean uat_fld_chk_proto(void*, const char*, unsigned, const void*, const void*, char** err);
342 WS_DLL_PUBLIC
343 gboolean uat_fld_chk_num_dec(void*, const char*, unsigned, const void*, const void*, char** err);
344 WS_DLL_PUBLIC
345 gboolean uat_fld_chk_num_hex(void*, const char*, unsigned, const void*, const void*, char** err);
346 WS_DLL_PUBLIC
347 gboolean uat_fld_chk_bool(void*, const char*, unsigned, const void*, const void*, char** err);
348 WS_DLL_PUBLIC
349 gboolean uat_fld_chk_enum(void*, const char*, unsigned, const void*, const void*, char**);
350 WS_DLL_PUBLIC
351 gboolean uat_fld_chk_range(void*, const char*, unsigned, const void*, const void*, char**);
352 WS_DLL_PUBLIC
353 gboolean uat_fld_chk_color(void*, const char*, unsigned, const void*, const void*, char**);
354
355 typedef void (*uat_cb_t)(void* uat,void* user_data);
356 WS_DLL_PUBLIC
357 void uat_foreach_table(uat_cb_t cb,void* user_data);
358 void uat_unload_all(void);
359
360 char* uat_undquote(const char* si, guint in_len, guint* len_p);
361 char* uat_unbinstring(const char* si, guint in_len, guint* len_p);
362 char* uat_unesc(const char* si, guint in_len, guint* len_p);
363 char* uat_esc(const char* buf, guint len);
364
365 /* Some strings entirely made of ... already declared */
366
367 WS_DLL_PUBLIC
368 gboolean uat_fld_chk_str_isprint(void*, const char*, unsigned, const void*, const void*, char**);
369
370 WS_DLL_PUBLIC
371 gboolean uat_fld_chk_str_isalpha(void*, const char*, unsigned, const void*, const void*, char**);
372
373 WS_DLL_PUBLIC
374 gboolean uat_fld_chk_str_isalnum(void*, const char*, unsigned, const void*, const void*, char**);
375
376 WS_DLL_PUBLIC
377 gboolean uat_fld_chk_str_isdigit(void*, const char*, unsigned, const void*, const void*, char**);
378
379 WS_DLL_PUBLIC
380 gboolean uat_fld_chk_str_isxdigit(void*, const char*, unsigned, const void*, const void*, char**);
381
382
383 /*
384  * Macros
385  *   to define basic uat_fld_set_cbs, uat_fld_tostr_cbs
386  *   for those elements in uat_field_t array
387  */
388
389 #ifdef __cplusplus
390 #define UNUSED_PARAMETER(n)
391 #else
392 #define UNUSED_PARAMETER(n) n _U_
393 #endif
394
395 /*
396  * CSTRING macros,
397  *    a simple c-string contained in (((rec_t*)rec)->(field_name))
398  */
399 #define UAT_CSTRING_CB_DEF(basename,field_name,rec_t) \
400 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
401     char* new_buf = g_strndup(buf,len); \
402         g_free((((rec_t*)rec)->field_name)); \
403         (((rec_t*)rec)->field_name) = new_buf; } \
404 static void basename ## _ ## field_name ## _tostr_cb(void* rec, char** out_ptr, unsigned* out_len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
405                 if (((rec_t*)rec)->field_name ) { \
406                         *out_ptr = g_strdup((((rec_t*)rec)->field_name)); \
407                         *out_len = (unsigned)strlen((((rec_t*)rec)->field_name)); \
408                 } else { \
409                         *out_ptr = g_strdup(""); *out_len = 0; } }
410
411 #define UAT_FLD_CSTRING(basename,field_name,title,desc) \
412         {#field_name, title, PT_TXTMOD_STRING,{uat_fld_chk_str,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
413
414 #define UAT_FLD_CSTRING_ISPRINT(basename,field_name,title,desc) \
415         {#field_name, title, PT_TXTMOD_STRING,{uat_fld_chk_str_isprint,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
416
417 #define UAT_FLD_CSTRING_OTHER(basename,field_name,title,chk,desc) \
418         {#field_name, title, PT_TXTMOD_STRING,{ chk ,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
419
420 /*
421  * FILENAME and DIRECTORYNAME,
422  *    a simple c-string contained in (((rec_t*)rec)->(field_name))
423  */
424 #define UAT_FILENAME_CB_DEF(basename,field_name,rec_t) UAT_CSTRING_CB_DEF(basename,field_name,rec_t)
425
426 /* XXX UAT_FLD_FILENAME is currently unused. */
427 #define UAT_FLD_FILENAME(basename,field_name,title,desc) \
428         {#field_name, title, PT_TXTMOD_FILENAME,{uat_fld_chk_str,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
429
430 /*
431  * Both the Qt and GTK+ UIs assume that we're opening a preexisting
432  * file. We might want to split the ..._FILENAME defines into
433  * ..._FILE_OPEN and ..._FILE_SAVE if we ever need to specify a
434  * file that we're creating.
435  */
436 #define UAT_FLD_FILENAME_OTHER(basename,field_name,title,chk,desc) \
437         {#field_name, title, PT_TXTMOD_FILENAME,{chk,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
438
439 #define UAT_DIRECTORYNAME_CB_DEF(basename,field_name,rec_t) UAT_CSTRING_CB_DEF(basename,field_name,rec_t)
440
441 #define UAT_FLD_DIRECTORYNAME(basename,field_name,title,desc) \
442         {#field_name, title, PT_TXTMOD_DIRECTORYNAME,{uat_fld_chk_str,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
443
444 /*
445  * DISPLAY_FILTER,
446  *    a simple c-string contained in (((rec_t*)rec)->(field_name))
447  */
448 #define UAT_DISPLAY_FILTER_CB_DEF(basename,field_name,rec_t) UAT_CSTRING_CB_DEF(basename,field_name,rec_t)
449
450 #define UAT_FLD_DISPLAY_FILTER(basename,field_name,title,desc) \
451         {#field_name, title, PT_TXTMOD_DISPLAY_FILTER, {uat_fld_chk_str,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
452
453 /*
454  * PROTO_FIELD,
455  *    a simple c-string contained in (((rec_t*)rec)->(field_name))
456  */
457 #define UAT_PROTO_FIELD_CB_DEF(basename,field_name,rec_t) UAT_CSTRING_CB_DEF(basename,field_name,rec_t)
458
459 #define UAT_FLD_PROTO_FIELD(basename,field_name,title,desc) \
460         {#field_name, title, PT_TXTMOD_PROTO_FIELD, {uat_fld_chk_str,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
461
462 /*
463  * OID - just a CSTRING with a specific check routine
464  */
465 #define UAT_FLD_OID(basename,field_name,title,desc) \
466         {#field_name, title, PT_TXTMOD_STRING,{uat_fld_chk_oid,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
467
468
469 /*
470  * LSTRING MACROS
471  */
472 #define UAT_LSTRING_CB_DEF(basename,field_name,rec_t,ptr_element,len_element) \
473 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
474         char* new_val = uat_unesc(buf,len,&(((rec_t*)rec)->len_element)); \
475         g_free((((rec_t*)rec)->ptr_element)); \
476         (((rec_t*)rec)->ptr_element) = new_val; }\
477 static void basename ## _ ## field_name ## _tostr_cb(void* rec, char** out_ptr, unsigned* out_len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
478         if (((rec_t*)rec)->ptr_element ) { \
479                 *out_ptr = uat_esc(((rec_t*)rec)->ptr_element, (((rec_t*)rec)->len_element)); \
480                 *out_len = (unsigned)strlen(*out_ptr); \
481         } else { \
482                 *out_ptr = g_strdup(""); \
483                 *out_len = 0; \
484         } \
485 }
486
487 #define UAT_FLD_LSTRING(basename,field_name,title, desc) \
488 {#field_name, title, PT_TXTMOD_STRING,{0,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
489
490
491 /*
492  * BUFFER macros,
493  *    a buffer_ptr contained in (((rec_t*)rec)->(field_name))
494  *    and its len in (((rec_t*)rec)->(len_name))
495  */
496 #define UAT_BUFFER_CB_DEF(basename,field_name,rec_t,ptr_element,len_element) \
497 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
498         char* new_buf = len ? (char *)g_memdup(buf,len) : NULL; \
499         g_free((((rec_t*)rec)->ptr_element)); \
500         (((rec_t*)rec)->ptr_element) = new_buf; \
501         (((rec_t*)rec)->len_element) = len; } \
502 static void basename ## _ ## field_name ## _tostr_cb(void* rec, char** out_ptr, unsigned* out_len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
503         *out_ptr = ((rec_t*)rec)->ptr_element ? (char*)g_memdup(((rec_t*)rec)->ptr_element,((rec_t*)rec)->len_element) : g_strdup(""); \
504         *out_len = ((rec_t*)rec)->len_element; }
505
506 #define UAT_FLD_BUFFER(basename,field_name,title,desc) \
507         {#field_name, title, PT_TXTMOD_HEXBYTES,{0,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
508
509
510 /*
511  * DEC Macros,
512  *   a decimal number contained in
513  */
514 #define UAT_DEC_CB_DEF(basename,field_name,rec_t) \
515 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
516         char* tmp_str = g_strndup(buf,len); \
517         ((rec_t*)rec)->field_name = (guint)strtol(tmp_str,NULL,10); \
518         g_free(tmp_str); } \
519 static void basename ## _ ## field_name ## _tostr_cb(void* rec, char** out_ptr, unsigned* out_len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
520         *out_ptr = g_strdup_printf("%d",((rec_t*)rec)->field_name); \
521         *out_len = (unsigned)strlen(*out_ptr); }
522
523 #define UAT_FLD_DEC(basename,field_name,title,desc) \
524         {#field_name, title, PT_TXTMOD_STRING,{uat_fld_chk_num_dec,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
525
526 #define UAT_FLD_NONE(basename,field_name,title,desc) \
527         {#field_name, title, PT_TXTMOD_NONE,{uat_fld_chk_num_dec,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
528
529
530 /*
531  * HEX Macros,
532  *   an hexadecimal number contained in
533  */
534 #define UAT_HEX_CB_DEF(basename,field_name,rec_t) \
535 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
536         char* tmp_str = g_strndup(buf,len); \
537         ws_hexstrtou32(tmp_str, NULL, &((rec_t*)rec)->field_name); \
538         g_free(tmp_str); } \
539 static void basename ## _ ## field_name ## _tostr_cb(void* rec, char** out_ptr, unsigned* out_len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
540         *out_ptr = g_strdup_printf("%x",((rec_t*)rec)->field_name); \
541         *out_len = (unsigned)strlen(*out_ptr); }
542
543 #define UAT_FLD_HEX(basename,field_name,title,desc) \
544 {#field_name, title, PT_TXTMOD_STRING,{uat_fld_chk_num_hex,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
545
546 /*
547  * BOOL Macros,
548  *   an boolean value contained in
549  */
550 #define UAT_BOOL_CB_DEF(basename,field_name,rec_t) \
551 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
552         char* tmp_str = g_strndup(buf,len); \
553         if (g_strcmp0(tmp_str, "TRUE") == 0) \
554                 ((rec_t*)rec)->field_name = 1; \
555         else \
556                 ((rec_t*)rec)->field_name = 0; \
557         g_free(tmp_str); } \
558 static void basename ## _ ## field_name ## _tostr_cb(void* rec, char** out_ptr, unsigned* out_len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
559         *out_ptr = g_strdup_printf("%s",((rec_t*)rec)->field_name ? "TRUE" : "FALSE"); \
560         *out_len = (unsigned)strlen(*out_ptr); }
561
562 #define UAT_FLD_BOOL(basename,field_name,title,desc) \
563 {#field_name, title, PT_TXTMOD_BOOL,{uat_fld_chk_bool,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
564
565 /*
566  * ENUM macros
567  *  enum_t: name = ((enum_t*)ptr)->strptr
568  *          value = ((enum_t*)ptr)->value
569  *  rec_t:
570  *        value
571  */
572 #define UAT_VS_DEF(basename,field_name,rec_t,default_t,default_val,default_str) \
573 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* vs, const void* UNUSED_PARAMETER(u2)) {\
574         guint i; \
575         char* str = g_strndup(buf,len); \
576         const char* cstr; \
577         ((rec_t*)rec)->field_name = default_val; \
578         for(i=0; ( cstr = ((const value_string*)vs)[i].strptr ) ;i++) { \
579                 if (g_str_equal(cstr,str)) { \
580                         ((rec_t*)rec)->field_name = (default_t)((const value_string*)vs)[i].value; \
581                         g_free(str); \
582                         return; \
583                 } \
584         } \
585         g_free(str); } \
586 static void basename ## _ ## field_name ## _tostr_cb(void* rec, char** out_ptr, unsigned* out_len, const void* vs, const void* UNUSED_PARAMETER(u2)) {\
587         guint i; \
588         for(i=0;((const value_string*)vs)[i].strptr;i++) { \
589                 if ( ((const value_string*)vs)[i].value == ((rec_t*)rec)->field_name ) { \
590                         *out_ptr = g_strdup(((const value_string*)vs)[i].strptr); \
591                         *out_len = (unsigned)strlen(*out_ptr); \
592                         return; \
593                 } \
594         } \
595         *out_ptr = g_strdup(default_str); \
596         *out_len = (unsigned)strlen(default_str); }
597
598 #define UAT_VS_CSTRING_DEF(basename,field_name,rec_t,default_val,default_str) \
599 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* vs, const void* UNUSED_PARAMETER(u2)) {\
600         guint i; \
601         char* str = g_strndup(buf,len); \
602         const char* cstr; \
603         ((rec_t*)rec)->field_name = default_val; \
604         for(i=0; ( cstr = ((const value_string*)vs)[i].strptr ) ;i++) { \
605                 if (g_str_equal(cstr,str)) { \
606                   ((rec_t*)rec)->field_name = g_strdup(((const value_string*)vs)[i].strptr); \
607                   g_free(str); \
608                   return; \
609                 } \
610         } \
611         g_free(str);} \
612 static void basename ## _ ## field_name ## _tostr_cb(void* rec, char** out_ptr, unsigned* out_len, const void* UNUSED_PARAMETER(vs), const void* UNUSED_PARAMETER(u2)) {\
613                 if (((rec_t*)rec)->field_name ) { \
614                         *out_ptr = g_strdup((((rec_t*)rec)->field_name)); \
615                         *out_len = (unsigned)strlen((((rec_t*)rec)->field_name)); \
616                 } else { \
617                         *out_ptr = g_strdup(""); *out_len = 0; } }
618
619 #define UAT_FLD_VS(basename,field_name,title,enum,desc) \
620         {#field_name, title, PT_TXTMOD_ENUM,{uat_fld_chk_enum,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{&(enum),&(enum),&(enum)},&(enum),desc,FLDFILL}
621
622
623 /*
624  * Color Macros,
625  *   an #RRGGBB color value contained in
626  */
627 #define UAT_COLOR_CB_DEF(basename,field_name,rec_t) \
628 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
629         if (len < 1) { \
630                 ((rec_t*)rec)->field_name = 0; \
631                 return; \
632         } \
633         char* tmp_str = g_strndup(buf+1,len-1); \
634         ((rec_t*)rec)->field_name = (guint)strtol(tmp_str,NULL,16); \
635         g_free(tmp_str); } \
636 static void basename ## _ ## field_name ## _tostr_cb(void* rec, char** out_ptr, unsigned* out_len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
637         *out_ptr = g_strdup_printf("#%06X",((rec_t*)rec)->field_name); \
638         *out_len = (unsigned)strlen(*out_ptr); }
639
640 #define UAT_FLD_COLOR(basename,field_name,title,desc) \
641 {#field_name, title, PT_TXTMOD_COLOR,{uat_fld_chk_color,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
642
643
644 /*
645  * PROTO macros
646  */
647
648 #define UAT_PROTO_DEF(basename, field_name, dissector_field, name_field, rec_t) \
649 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
650         if (len) { \
651                 gchar *tmp = g_strndup(buf,len); \
652                 ((rec_t*)rec)->name_field = g_ascii_strdown(tmp, -1); \
653                 g_free(tmp); \
654                 g_strchug(((rec_t*)rec)->name_field); \
655                 ((rec_t*)rec)->dissector_field = find_dissector(((rec_t*)rec)->name_field); \
656         } else { \
657                 ((rec_t*)rec)->dissector_field = find_dissector("data"); \
658                 ((rec_t*)rec)->name_field = NULL; \
659                 } } \
660 static void basename ## _ ## field_name ## _tostr_cb(void* rec, char** out_ptr, unsigned* out_len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
661         if ( ((rec_t*)rec)->name_field ) { \
662                 *out_ptr = g_strdup((((rec_t*)rec)->name_field)); \
663                 *out_len = (unsigned)strlen(*out_ptr); \
664         } else { \
665                 *out_ptr = g_strdup(""); *out_len = 0; } }
666
667
668 #define UAT_FLD_PROTO(basename,field_name,title,desc) \
669         {#field_name, title, PT_TXTMOD_STRING,{uat_fld_chk_proto,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
670
671 /*
672  * RANGE macros
673  */
674
675 #define UAT_RANGE_CB_DEF(basename,field_name,rec_t) \
676 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* UNUSED_PARAMETER(u1), const void* u2) {\
677         char* rng = g_strndup(buf,len);\
678                 range_convert_str(NULL, &(((rec_t*)rec)->field_name), rng,GPOINTER_TO_UINT(u2)); \
679                 g_free(rng); \
680         } \
681 static void basename ## _ ## field_name ## _tostr_cb(void* rec, char** out_ptr, unsigned* out_len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
682         if ( ((rec_t*)rec)->field_name ) { \
683                 *out_ptr = range_convert_range(NULL, ((rec_t*)rec)->field_name); \
684                 *out_len = (unsigned)strlen(*out_ptr); \
685         } else { \
686                 *out_ptr = g_strdup(""); *out_len = 0; } }
687
688
689 #define UAT_FLD_RANGE(basename,field_name,title,max,desc) \
690         {#field_name, title, PT_TXTMOD_STRING,{uat_fld_chk_range,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},\
691           {0,0,0},GUINT_TO_POINTER(max),desc,FLDFILL}
692
693 #ifdef __cplusplus
694 }
695 #endif /* __cplusplus */
696
697 #endif /* __UAT_H__ */