GSM SMS: follow-up of gd65b7d5
[metze/wireshark/wip.git] / epan / uat.h
1 /*
2  *  uat.h
3  *
4  *  User Accessible Tables
5  *  Mantain an array of user accessible data strucures
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  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26  */
27
28 #ifndef __UAT_H__
29 #define __UAT_H__
30
31 #include <stdlib.h>
32
33 #include <epan/emem.h>
34
35 #include "ws_symbol_export.h"
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif /* __cplusplus */
40
41 /*
42  * uat mantains a dynamically allocated table accessible to the user
43  * via a file and/or gui tables.
44  *
45  * the file is located either in userdir(when first read or when writen) or
46  * in datadir for defaults (read only , it will be always written to userdir).
47  *
48  * the behaviour of the table is controlled by a series of callbacks
49  * the caller must provide.
50  *
51  * BEWARE that the user can change an uat at (almost) any time,
52  * That is pointers to records in an uat are valid only during the call
53  * to the function that obtains them (do not store them).
54  *
55  * UATs are meant for short tables of user data (passwords and such) there's
56  * no quick access, you must iterate through them each time to fetch the record
57  * you are looking for.
58  *
59  * Only users via gui or editing the file can add/remove records your code cannot.
60  */
61
62 /* obscure data type to handle an uat */
63 typedef struct epan_uat uat_t;
64 /********************************************
65  * Callbacks:
66  * these instruct uat on how to deal with user info and data in records
67  ********************************************/
68
69 /********
70  * Callbacks dealing with the entire table
71  ********/
72
73 /*
74  * Post-Update CB
75  *
76  * to be called after to the table has being edited
77  * Will be called once the user clicks the Apply or OK button
78  * optional
79  */
80 typedef void (*uat_post_update_cb_t)(void);
81
82
83 /********
84  * Callbacks dealing with records (these deal with entire records)
85  ********/
86
87 /*
88  * Copy CB
89  * used to copy a record
90  * optional, memcpy will be used if not given
91  * copy(dest,orig,len)
92  */
93 typedef void* (*uat_copy_cb_t)(void*, const void*, size_t);
94
95 /*
96  *
97  * Free CB
98  *
99  * destroy a record's child data
100  * (do not free the container, it will be handled by uat)
101  * it is optional, no child data will be freed if no present
102  * free(record)
103  */
104 typedef void (*uat_free_cb_t)(void*);
105
106 /*
107  * Update CB
108  *
109  * to be called after any record fields had been updated
110  * optional, record will be updated always if not given
111  * update(record,&error)
112  */
113 typedef void (*uat_update_cb_t)(void* , const char** );
114
115
116 /*******
117  * Callbacks for single fields (these deal with single values)
118  * the caller should provide one of these for every field!
119  ********/
120
121 /*
122  * given an input string (ptr, len) checks if the value is OK for a field in the record.
123  * it will return TRUE if OK or else
124  * it will return FALSE and may set *error to inform the user on what's
125  * wrong with the given input
126  * optional, if not given any input is considered OK and the set cb will be called
127  * chk(record, ptr, len, chk_data, fld_data, &error)
128  */
129 typedef gboolean (*uat_fld_chk_cb_t)(void*, const char*, unsigned, const void*, const void*, const char**);
130
131 /*
132  * Set Field CB
133  *
134  * given an input string (ptr, len) sets the value of a field in the record,
135  * it will return TRUE if OK or else
136  * it will return FALSE and may set *error to inform the user on what's
137  * wrong with the given input
138  * it is mandatory
139  * set(record, ptr, len, set_data, fld_data)
140  */
141 typedef void (*uat_fld_set_cb_t)(void*, const char*, unsigned, const void*, const void*);
142
143 /*
144  * given a record returns a string representation of the field
145  * mandatory
146  * tostr(record, &out_ptr, &out_len, tostr_data, fld_data)
147  */
148 typedef void (*uat_fld_tostr_cb_t)(void*, const char**, unsigned*, const void*, const void*);
149
150 /***********
151  * Text Mode
152  *
153  * used for file and dialog representation of fields in columns,
154  * when the file is read it modifies the way the value is passed back to the fld_set_cb
155  * (see definition bellow for description)
156  ***********/
157
158 typedef enum _uat_text_mode_t {
159         PT_TXTMOD_NONE,
160         /* not used */
161
162         PT_TXTMOD_STRING,
163         /*
164          file:
165                  reads:
166                          ,"\x20\x00\x30", as " \00",3
167                          ,"", as "",0
168                          ,, as NULL,0
169                  writes:
170                          ,"\x20\x30\x00\x20", for " 0\0 ",4
171                          ,"", for *, 0
172                          ,, for NULL, *
173          dialog:
174                  accepts \x?? and other escapes
175                  gets "",0 on empty string
176          */
177         PT_TXTMOD_HEXBYTES,
178         /*
179          file:
180                  reads:
181                          ,A1b2C3d4, as "\001\002\003\004",4
182                          ,, as NULL,0
183                  writes:
184                          ,, on NULL, *
185                          ,a1b2c3d4, on "\001\002\003\004",4
186          dialog:
187                  "a1b2c3d4" as "\001\002\003\004",4
188                  "a1 b2:c3d4" as "\001\002\003\004",4
189                  "" as NULL,0
190                  "invalid" as NULL,3
191                  "a1b" as NULL, 1
192          */
193         PT_TXTMOD_ENUM,
194
195         PT_TXTMOD_FILENAME,
196         /* processed like a PT_TXTMOD_STRING, but shows a filename dialog */
197         PT_TXTMOD_DIRECTORYNAME
198         /* processed like a PT_TXTMOD_STRING, but shows a directory dialog */
199 } uat_text_mode_t;
200
201 /*
202  * Fields
203  *
204  *
205  */
206 typedef struct _uat_field_t {
207         const char* name;
208         const char* title;
209         uat_text_mode_t mode;
210
211         struct {
212                 uat_fld_chk_cb_t chk;
213                 uat_fld_set_cb_t set;
214                 uat_fld_tostr_cb_t tostr;
215         } cb;
216
217         struct {
218                 const void* chk;
219                 const void* set;
220                 const void* tostr;
221         } cbdata;
222
223         const void* fld_data;
224
225         const char* desc;
226         struct _fld_data_t* priv;
227 } uat_field_t;
228
229 #define FLDFILL NULL
230 #define UAT_END_FIELDS {NULL,NULL,PT_TXTMOD_NONE,{0,0,0},{0,0,0},0,0,FLDFILL}
231
232 /*
233  * Flags to indicate what the settings in this UAT affect.
234  * This is used when UATs are changed interactively, to indicate what needs
235  * to be redone when the UAT is changed.
236  */
237 #define UAT_AFFECTS_DISSECTION  0x00000001      /* affects packet dissection */
238 #define UAT_AFFECTS_FIELDS      0x00000002      /* affects what named fields exist */
239
240 /** Create a new uat
241  *
242  * @param name The name of the table
243  * @param size The size of the structure
244  * @param filename The filename to be used (either in userdir or datadir)
245  * @param from_profile TRUE if profile directory to be used
246  * @param data_ptr Although a void*, this is really a pointer to a null terminated array of pointers to the data
247  * @param num_items_ptr A pointer with number of items
248  * @param flags flags indicating what this UAT affects
249  * @param help A pointer to help text
250  * @param copy_cb A function that copies the data in the struct
251  * @param update_cb Will be called when a record is updated
252  * @param free_cb Will be called to destroy a struct in the dataset
253  * @param post_update_cb Will be called once the user clicks the Apply or OK button
254  * @param flds_array A pointer to an array of uat_field_t structs
255  *
256  * @return A freshly-allocated and populated uat_t struct.
257  */
258 WS_DLL_PUBLIC
259 uat_t* uat_new(const char* name,
260                            size_t size,
261                            const char* filename,
262                            gboolean from_profile,
263                            void* data_ptr,
264                            guint* num_items_ptr,
265                            guint flags,
266                            const char* help,
267                            uat_copy_cb_t copy_cb,
268                            uat_update_cb_t update_cb,
269                            uat_free_cb_t free_cb,
270                            uat_post_update_cb_t post_update_cb,
271                            uat_field_t* flds_array);
272
273 /** Populate a uat using its file.
274  *
275  * @param uat_in Pointer to a uat. Must not be NULL.
276  * @param err Upon failure, points to an error string.
277  *
278  * @return TRUE on success, FALSE on failure.
279  */
280 WS_DLL_PUBLIC
281 gboolean uat_load(uat_t* uat_in, const char** err);
282
283 /** Create or update a single uat entry using a string.
284  *
285  * @param uat_in Pointer to a uat. Must not be NULL.
286  * @param entry The string representation of the entry. Format must match
287  * what's written to the uat's output file.
288  * @param err Upon failure, points to an error string.
289  *
290  * @return TRUE on success, FALSE on failure.
291  */
292 gboolean uat_load_str(uat_t* uat_in, char* entry, char** err);
293
294 /** Given a uat name or filename, find its pointer.
295  *
296  * @param name The name or filename of the uat
297  *
298  * @return A pointer to the uat on success, NULL on failure.
299  */
300 uat_t *uat_find(gchar *name);
301
302 WS_DLL_PUBLIC
303 uat_t* uat_get_table_by_name(const char* name);
304
305 /*
306  * Some common uat_fld_chk_cbs
307  */
308 WS_DLL_PUBLIC
309 gboolean uat_fld_chk_str(void*, const char*, unsigned, const void*, const void*, const char** err);
310 gboolean uat_fld_chk_oid(void*, const char*, unsigned, const void*, const void*, const char** err);
311 WS_DLL_PUBLIC
312 gboolean uat_fld_chk_proto(void*, const char*, unsigned, const void*, const void*, const char** err);
313 WS_DLL_PUBLIC
314 gboolean uat_fld_chk_num_dec(void*, const char*, unsigned, const void*, const void*, const char** err);
315 WS_DLL_PUBLIC
316 gboolean uat_fld_chk_num_hex(void*, const char*, unsigned, const void*, const void*, const char** err);
317 WS_DLL_PUBLIC
318 gboolean uat_fld_chk_enum(void*, const char*, unsigned, const void*, const void*, const char**);
319 WS_DLL_PUBLIC
320 gboolean uat_fld_chk_range(void*, const char*, unsigned, const void*, const void*, const char**);
321
322 #define CHK_STR_IS_DECL(what) \
323 gboolean uat_fld_chk_str_ ## what (void*, const char*, unsigned, const void*, const void*, const char**)
324
325 typedef void (*uat_cb_t)(void* uat,void* user_data);
326 WS_DLL_PUBLIC
327 void uat_foreach_table(uat_cb_t cb,void* user_data);
328 void uat_unload_all(void);
329
330 char* uat_undquote(const char* si, guint in_len, guint* len_p);
331 char* uat_unbinstring(const char* si, guint in_len, guint* len_p);
332 char* uat_unesc(const char* si, guint in_len, guint* len_p);
333 char* uat_esc(const char* buf, guint len);
334
335 #ifdef __cplusplus
336 #define UNUSED_PARAMETER(n)
337 #else
338 #define UNUSED_PARAMETER(n) n _U_
339 #endif
340
341 /* Some strings entirely made of ... already declared */
342 WS_DLL_PUBLIC
343 CHK_STR_IS_DECL(isprint);
344 WS_DLL_PUBLIC
345 CHK_STR_IS_DECL(isalpha);
346 WS_DLL_PUBLIC
347 CHK_STR_IS_DECL(isalnum);
348 WS_DLL_PUBLIC
349 CHK_STR_IS_DECL(isdigit);
350 WS_DLL_PUBLIC
351 CHK_STR_IS_DECL(isxdigit);
352
353 #define CHK_STR_IS_DEF(what) \
354 gboolean uat_fld_chk_str_ ## what (void* UNUSED_PARAMETER(u1), const char* strptr, guint len, const void* UNUSED_PARAMETER(u2), const void* UNUSED_PARAMETER(u3), const char** err) { \
355         guint i; for (i=0;i<len;i++) { \
356                 char c = strptr[i]; \
357                         if (! what((int)c)) { \
358                                 *err = ep_strdup_printf("invalid char pos=%d value=%.2x",i,c); return FALSE;  } } \
359                 *err = NULL; return TRUE; }
360
361
362 /*
363  * Macros
364  *   to define basic uat_fld_set_cbs, uat_fld_tostr_cbs
365  *   for those elements in uat_field_t array
366  */
367
368 /*
369  * CSTRING macros,
370  *    a simple c-string contained in (((rec_t*)rec)->(field_name))
371  */
372 #define UAT_CSTRING_CB_DEF(basename,field_name,rec_t) \
373 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
374     char* new_buf = g_strndup(buf,len); \
375         g_free((((rec_t*)rec)->field_name)); \
376         (((rec_t*)rec)->field_name) = new_buf; } \
377 static void basename ## _ ## field_name ## _tostr_cb(void* rec, const char** out_ptr, unsigned* out_len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
378                 if (((rec_t*)rec)->field_name ) { \
379                         *out_ptr = (((rec_t*)rec)->field_name); \
380                         *out_len = (unsigned)strlen((((rec_t*)rec)->field_name)); \
381                 } else { \
382                         *out_ptr = ""; *out_len = 0; } }
383
384 #define UAT_FLD_CSTRING(basename,field_name,title,desc) \
385         {#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}
386
387 #define UAT_FLD_CSTRING_ISPRINT(basename,field_name,title,desc) \
388         {#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}
389
390 #define UAT_FLD_CSTRING_OTHER(basename,field_name,title,chk,desc) \
391         {#field_name, title, PT_TXTMOD_STRING,{ chk ,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
392
393 /*
394  * FILENAME and DIRECTORYNAME,
395  *    a simple c-string contained in (((rec_t*)rec)->(field_name))
396  */
397 #define UAT_FILENAME_CB_DEF(basename,field_name,rec_t) UAT_CSTRING_CB_DEF(basename,field_name,rec_t)
398
399 #define UAT_FLD_FILENAME(basename,field_name,title,desc) \
400         {#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}
401
402 #define UAT_FLD_FILENAME_OTHER(basename,field_name,title,chk,desc) \
403         {#field_name, title, PT_TXTMOD_FILENAME,{chk,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
404
405 #define UAT_DIRECTORYNAME_CB_DEF(basename,field_name,rec_t) UAT_CSTRING_CB_DEF(basename,field_name,rec_t)
406
407 #define UAT_FLD_DIRECTORYNAME(basename,field_name,title,desc) \
408         {#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}
409
410 /*
411  * OID - just a CSTRING with a specific check routine
412  */
413 #define UAT_FLD_OID(basename,field_name,title,desc) \
414         {#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}
415
416
417 /*
418  * LSTRING MACROS
419  */
420 #define UAT_LSTRING_CB_DEF(basename,field_name,rec_t,ptr_element,len_element) \
421 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
422         char* new_val = uat_unesc(buf,len,&(((rec_t*)rec)->len_element)); \
423         g_free((((rec_t*)rec)->ptr_element)); \
424         (((rec_t*)rec)->ptr_element) = new_val; }\
425 static void basename ## _ ## field_name ## _tostr_cb(void* rec, const char** out_ptr, unsigned* out_len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
426         if (((rec_t*)rec)->ptr_element ) { \
427                 *out_ptr = uat_esc(((rec_t*)rec)->ptr_element, (((rec_t*)rec)->len_element)); \
428                 *out_len = (unsigned)strlen(*out_ptr); \
429         } else { \
430                 *out_ptr = ""; *out_len = 0; } }
431
432 #define UAT_FLD_LSTRING(basename,field_name,title, desc) \
433 {#field_name, title, PT_TXTMOD_STRING,{0,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
434
435
436 /*
437  * BUFFER macros,
438  *    a buffer_ptr contained in (((rec_t*)rec)->(field_name))
439  *    and its len in (((rec_t*)rec)->(len_name))
440  *  XXX: UNTESTED and probably BROKEN
441  */
442 #define UAT_BUFFER_CB_DEF(basename,field_name,rec_t,ptr_element,len_element) \
443 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
444         char* new_buf = len ? (char *)g_memdup(buf,len) : NULL; \
445         g_free((((rec_t*)rec)->ptr_element)); \
446         (((rec_t*)rec)->ptr_element) = new_buf; \
447         (((rec_t*)rec)->len_element) = len; } \
448 static void basename ## _ ## field_name ## _tostr_cb(void* rec, const char** out_ptr, unsigned* out_len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
449         *out_ptr = ((rec_t*)rec)->ptr_element ? (const char*)ep_memdup(((rec_t*)rec)->ptr_element,((rec_t*)rec)->len_element) : ""; \
450         *out_len = ((rec_t*)rec)->len_element; }
451
452 #define UAT_FLD_BUFFER(basename,field_name,title,desc) \
453         {#field_name, title, PT_TXTMOD_HEXBYTES,{0,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{0,0,0},0,desc,FLDFILL}
454
455
456 /*
457  * DEC Macros,
458  *   a decimal number contained in
459  */
460 #define UAT_DEC_CB_DEF(basename,field_name,rec_t) \
461 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
462         ((rec_t*)rec)->field_name = (guint)strtol(ep_strndup(buf,len),NULL,10); } \
463 static void basename ## _ ## field_name ## _tostr_cb(void* rec, const char** out_ptr, unsigned* out_len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
464         *out_ptr = ep_strdup_printf("%d",((rec_t*)rec)->field_name); \
465         *out_len = (unsigned)strlen(*out_ptr); }
466
467 #define UAT_FLD_DEC(basename,field_name,title,desc) \
468         {#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}
469
470 #define UAT_FLD_NONE(basename,field_name,title,desc) \
471         {#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}
472
473
474 /*
475  * HEX Macros,
476  *   an hexadecimal number contained in
477  */
478 #define UAT_HEX_CB_DEF(basename,field_name,rec_t) \
479 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
480         ((rec_t*)rec)->field_name = (guint)strtol(ep_strndup(buf,len),NULL,16); } \
481 static void basename ## _ ## field_name ## _tostr_cb(void* rec, const char** out_ptr, unsigned* out_len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
482         *out_ptr = ep_strdup_printf("%x",((rec_t*)rec)->field_name); \
483         *out_len = (unsigned)strlen(*out_ptr); }
484
485 #define UAT_FLD_HEX(basename,field_name,title,desc) \
486 {#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}
487
488
489 /*
490  * ENUM macros
491  *  enum_t: name = ((enum_t*)ptr)->strptr
492  *          value = ((enum_t*)ptr)->value
493  *  rec_t:
494  *        value
495  */
496 #define UAT_VS_DEF(basename,field_name,rec_t,default_t,default_val,default_str) \
497 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* vs, const void* UNUSED_PARAMETER(u2)) {\
498         guint i; \
499         char* str = ep_strndup(buf,len); \
500         const char* cstr; ((rec_t*)rec)->field_name = default_val; \
501         for(i=0; ( cstr = ((const value_string*)vs)[i].strptr ) ;i++) { \
502                 if (g_str_equal(cstr,str)) { \
503                         ((rec_t*)rec)->field_name = (default_t)((const value_string*)vs)[i].value; return; } } } \
504 static void basename ## _ ## field_name ## _tostr_cb(void* rec, const char** out_ptr, unsigned* out_len, const void* vs, const void* UNUSED_PARAMETER(u2)) {\
505         guint i; \
506         *out_ptr = ep_strdup(default_str); \
507         *out_len = (unsigned)strlen(default_str);\
508         for(i=0;((const value_string*)vs)[i].strptr;i++) { \
509                 if ( ((const value_string*)vs)[i].value == ((rec_t*)rec)->field_name ) { \
510                         *out_ptr = ep_strdup(((const value_string*)vs)[i].strptr); \
511                         *out_len = (unsigned)strlen(*out_ptr); return; } } }
512
513 #define UAT_VS_CSTRING_DEF(basename,field_name,rec_t,default_val,default_str) \
514 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* vs, const void* UNUSED_PARAMETER(u2)) {\
515         guint i; \
516         char* str = ep_strndup(buf,len); \
517         const char* cstr; ((rec_t*)rec)->field_name = default_val; \
518         for(i=0; ( cstr = ((const value_string*)vs)[i].strptr ) ;i++) { \
519                 if (g_str_equal(cstr,str)) { \
520                   ((rec_t*)rec)->field_name = g_strdup(((const value_string*)vs)[i].strptr); return; } } } \
521 static void basename ## _ ## field_name ## _tostr_cb(void* rec, const char** out_ptr, unsigned* out_len, const void* UNUSED_PARAMETER(vs), const void* UNUSED_PARAMETER(u2)) {\
522                 if (((rec_t*)rec)->field_name ) { \
523                         *out_ptr = (((rec_t*)rec)->field_name); \
524                         *out_len = (unsigned)strlen((((rec_t*)rec)->field_name)); \
525                 } else { \
526                         *out_ptr = ""; *out_len = 0; } }
527
528 #define UAT_FLD_VS(basename,field_name,title,enum,desc) \
529         {#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}
530
531
532 /*
533  * PROTO macros
534  */
535
536 #define UAT_PROTO_DEF(basename, field_name, dissector_field, name_field, rec_t) \
537 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
538         if (len) { \
539                 gchar *tmp = g_strndup(buf,len); \
540                 ((rec_t*)rec)->name_field = g_ascii_strdown(tmp, -1); \
541                 g_free(tmp); \
542                 g_strchug(((rec_t*)rec)->name_field); \
543                 ((rec_t*)rec)->dissector_field = find_dissector(((rec_t*)rec)->name_field); \
544         } else { \
545                 ((rec_t*)rec)->dissector_field = find_dissector("data"); \
546                 ((rec_t*)rec)->name_field = NULL; \
547                 } } \
548 static void basename ## _ ## field_name ## _tostr_cb(void* rec, const char** out_ptr, unsigned* out_len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
549         if ( ((rec_t*)rec)->name_field ) { \
550                 *out_ptr = (((rec_t*)rec)->name_field); \
551                 *out_len = (unsigned)strlen(*out_ptr); \
552         } else { \
553                 *out_ptr = ""; *out_len = 0; } }
554
555
556 #define UAT_FLD_PROTO(basename,field_name,title,desc) \
557         {#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}
558
559 /*
560  * RANGE macros
561  */
562
563 #define UAT_RANGE_CB_DEF(basename,field_name,rec_t) \
564 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, guint len, const void* UNUSED_PARAMETER(u1), const void* u2) {\
565         char* rng = ep_strndup(buf,len);\
566                 range_convert_str(&(((rec_t*)rec)->field_name), rng,GPOINTER_TO_UINT(u2)); \
567         } \
568 static void basename ## _ ## field_name ## _tostr_cb(void* rec, const char** out_ptr, unsigned* out_len, const void* UNUSED_PARAMETER(u1), const void* UNUSED_PARAMETER(u2)) {\
569         if ( ((rec_t*)rec)->field_name ) { \
570                 *out_ptr = range_convert_range(((rec_t*)rec)->field_name); \
571                 *out_len = (unsigned)strlen(*out_ptr); \
572         } else { \
573                 *out_ptr = ""; *out_len = 0; } }
574
575
576 #define UAT_FLD_RANGE(basename,field_name,title,max,desc) \
577         {#field_name, title, PT_TXTMOD_STRING,{uat_fld_chk_range,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},\
578           {0,0,0},GUINT_TO_POINTER(max),desc,FLDFILL}
579
580 #ifdef __cplusplus
581 }
582 #endif /* __cplusplus */
583
584 #endif /* __UAT_H__ */