6 * User Accessible Tables
7 * Mantain an array of user accessible data strucures
9 * (c) 2007, Luis E. Garcia Ontanon <luis.ontanon@gmail.com>
11 * Wireshark - Network traffic analyzer
12 * By Gerald Combs <gerald@wireshark.org>
13 * Copyright 2001 Gerald Combs
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 * uat mantains a dynamically allocated table accessible to the user
35 * via a file and/or gui tables.
37 * the file is located either in userdir(when first read or when writen) or
38 * in datadir for defaults (read only , it will be always written to userdir).
40 * the behaviour of the table is controlled by a series of callbacks
41 * the caller must provide.
43 * BEWARE that the user can change an uat at (almost) any time,
44 * That is pointers to records in an uat are valid only during the call
45 * to the function that obtains them (do not store them).
47 * UATs are meant for short tables of user data (passwords and such) there's
48 * no quick access, you must iterate through them each time to fetch the record
49 * you are looking for. Use uat_dup() or uat_se_dup() if necessary.
51 * Only users via gui or editing the file can add/remove records your code cannot.
54 /* obscure data type to handle an uat */
55 typedef struct _uat_t uat_t;
56 /********************************************
58 * these instruct uat on how to deal with user info and data in records
59 ********************************************/
62 * Callbacks for the entire table (these deal with entire records)
67 * used to copy a record
68 * optional, memcpy will be used if not given
71 typedef void* (*uat_copy_cb_t)(void*, const void*, unsigned);
77 * destroy a record's child data
78 * (do not free the container, it will be handled by uat)
79 * it is optional, no child data will be freed if no present
82 typedef void (*uat_free_cb_t)(void*);
87 * to be called after all record fields has been updated
88 * optional, record will be updated always if not given
89 * update(record,&error)
91 typedef void (*uat_update_cb_t)(void* , char** );
95 * Callbacks for single fields (these deal with single values)
96 * the caller should provide one of these for every field!
100 * given an input string (ptr, len) checks if the value is OK for a field in the record.
101 * it will return TRUE if OK or else
102 * it will return FALSE and may set *error to inform the user on what's
103 * wrong with the given input
104 * optional, if not given any input is considered OK and the set cb will be called
105 * chk(record, ptr, len, chk_data, fld_data, &error)
107 typedef gboolean (*uat_fld_chk_cb_t)(void*, const char*, unsigned, void*, void*, char**);
112 * given an input string (ptr, len) sets the value of a field in the record,
113 * it will return TRUE if OK or else
114 * it will return FALSE and may set *error to inform the user on what's
115 * wrong with the given input
117 * set(record, ptr, len, set_data, fld_data)
119 typedef void (*uat_fld_set_cb_t)(void*, const char*, unsigned, void*, void*);
122 * given a record returns a string representation of the field
124 * tostr(record, &out_ptr, &out_len, tostr_data, fld_data)
126 typedef void (*uat_fld_tostr_cb_t)(void*, char**, unsigned*, void*, void*);
131 * used for file and dialog representation of fileds in columns,
132 * when the file is read it modifies the way the value is passed back to the fld_set_cb
133 * (see definition bellow for description)
136 typedef enum _uat_text_mode_t {
144 ,"\x20\x00\x30", as " \00",3
148 ,"\x20\x30\x00\x20", for " 0\0 ",4
152 accepts \x?? and other escapes
153 gets "",0 on empty string
159 ,A1b2C3d4, as "\001\002\003\004",4
163 ,a1b2c3d4, on "\001\002\003\004",4
165 "a1b2c3d4" as "\001\002\003\004",4
166 "a1 b2:c3d4" as "\001\002\003\004",4
179 typedef struct _uat_field_t {
181 uat_text_mode_t mode;
184 uat_fld_chk_cb_t chk;
185 uat_fld_set_cb_t set;
186 uat_fld_tostr_cb_t tostr;
197 struct _fld_data_t* priv;
201 #define UAT_END_FIELDS {0,PT_TXTMOD_NONE,{0,0,0},{0,0,0},0,FLDFILL}
210 * name: the name of the table
212 * data_ptr: a pointer to a null terminated array of pointers to the data
214 * default_data: a pinter to a struct containing default values
216 * size: the size of the structure
218 * filename: the filename to be used (either in userdir or datadir)
220 * copy_cb: a function that copies the data in the struct
222 * update_cb: will be called when a record is updated
224 * free_cb: will be called to destroy a struct in the dataset
228 uat_t* uat_new(const char* name,
234 uat_copy_cb_t copy_cb,
235 uat_update_cb_t update_cb,
236 uat_free_cb_t free_cb,
237 uat_field_t* flds_array);
243 * make a reliable copy of an uat for internal use,
244 * so that pointers to records can be kept through calls.
245 * return NULL on zero len.
247 void* uat_dup(uat_t*, guint* len_p); /* to be freed */
248 void* uat_se_dup(uat_t*, guint* len_p);
251 * Some common uat_fld_chk_cbs
253 gboolean uat_fld_chk_str(void*, const char*, unsigned, void*,void*, char** err);
254 gboolean uat_fld_chk_proto(void*, const char*, unsigned, void*,void*, char** err);
255 gboolean uat_fld_chk_num_dec(void*, const char*, unsigned, void*, void*, char** err);
256 gboolean uat_fld_chk_num_hex(void*, const char*, unsigned, void*, void*, char** err);
257 gboolean uat_fld_chk_enum(void*, const char*, unsigned, void*, void*, char**);
259 #define CHK_STR_IS_DECL(what) \
260 gboolean uat_fld_chk_str_ ## what (void*, const char*, unsigned, void*, void*, char**)
262 /* Some strings entirely made of ... already declared */
263 CHK_STR_IS_DECL(isprint);
264 CHK_STR_IS_DECL(isalpha);
265 CHK_STR_IS_DECL(isalnum);
266 CHK_STR_IS_DECL(isdigit);
267 CHK_STR_IS_DECL(isxdigit);
269 #define CHK_STR_IS_DEF(what) \
270 gboolean uat_fld_chk_str_ ## what (void* u1 _U_, const char* strptr, unsigned len, void* u2 _U_, void* u3 _U_, char** err) { \
271 guint i; for (i=0;i<len;i++) { \
272 char c = strptr[i]; \
274 *err = ep_strdup_printf("invalid char pos=%d value=%.2x",i,c); return FALSE; } } \
275 *err = NULL; return TRUE; }
280 * to define basic uat_fld_set_cbs, uat_fld_tostr_cbs
281 * for those elements in uat_field_t array
286 * a simple c-string contained in (((rec_t*)rec)->(field_name))
288 #define UAT_CSTRING_CB_DEF(basename,field_name,rec_t) \
289 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, unsigned len, void* u1 _U_, void* u2 _U_) {\
290 if ((((rec_t*)rec)->field_name)) g_free((((rec_t*)rec)->field_name)); \
291 (((rec_t*)rec)->field_name) = g_strndup(buf,len); } \
292 static void basename ## _ ## field_name ## _tostr_cb(void* rec, char** out_ptr, unsigned* out_len, void* u1 _U_, void* u2 _U_) {\
293 if (((rec_t*)rec)->field_name ) { \
294 *out_ptr = (((rec_t*)rec)->field_name); *out_len = strlen((((rec_t*)rec)->field_name)); \
296 *out_ptr = ""; *out_len = 0; } }
298 #define UAT_FLD_CSTRING(basename,field_name) \
299 {#field_name, PT_TXTMOD_STRING,{uat_fld_chk_str,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{NULL,NULL,NULL},NULL,FLDFILL}
301 #define UAT_FLD_CSTRING_ISPRINT(basename,field_name) \
302 {#field_name, PT_TXTMOD_STRING,{uat_fld_chk_str_isprint,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{NULL,NULL,NULL},NULL,FLDFILL}
304 #define UAT_FLD_CSTRING_OTHER(basename,field_name,chk) \
305 {#field_name, PT_TXTMOD_STRING,{ chk ,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{NULL,NULL,NULL},NULL,FLDFILL}
310 #define UAT_LSTRING_CB_DEF(basename,field_name,rec_t,ptr_element,len_element) \
311 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, unsigned len, void* u1 _U_, void* u2 _U_) {\
312 if ((((rec_t*)rec)->ptr_element)) g_free((((rec_t*)rec)->ptr_element)); \
313 (((rec_t*)rec)->ptr_element) = g_strndup(buf,len); \
314 (((rec_t*)rec)->len_element) = len; } \
315 static void basename ## _ ## field_name ## _tostr_cb(void* rec, char** out_ptr, unsigned* out_len, void* u1 _U_, void* u2 _U_) {\
316 if (((rec_t*)rec)->ptr_element ) { \
317 *out_ptr = (((rec_t*)rec)->ptr_element); *out_len = (((rec_t*)rec)->len_element); \
319 *out_ptr = ""; *out_len = 0; } }
321 #define UAT_FLD_LSTRING(basename,field_name) \
322 {#field_name, PT_TXTMOD_STRING,{NULL,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{NULL,NULL,NULL},NULL,FLDFILL}
327 * a buffer_ptr contained in (((rec_t*)rec)->(field_name))
328 * and its len in (((rec_t*)rec)->(len_name))
331 #define UAT_BUFFER_CB_DEF(basename,field_name,rec_t,ptr_element,len_element) \
332 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, unsigned len, void* u1 _U_, void* u2 _U_) {\
333 if ((((rec_t*)rec)->ptr_element) ) g_free((((rec_t*)rec)->ptr_element)); \
334 (((rec_t*)rec)->ptr_element) = len ? g_memdup(buf,len) : NULL; \
335 (((rec_t*)rec)->len_element) = len; } \
336 static void basename ## _ ## field_name ## _tostr_cb(void* rec, char** out_ptr, unsigned* out_len, void* u1 _U_, void* u2 _U_) {\
337 *out_ptr = ((rec_t*)rec)->ptr_element ? ep_memdup(((rec_t*)rec)->ptr_element,((rec_t*)rec)->len_element) : ""; \
338 *out_len = ((rec_t*)rec)->len_element; }
340 #define UAT_FLD_BUFFER(basename,field_name) \
341 {#field_name, PT_TXTMOD_HEXBYTES,{NULL,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{NULL,NULL,NULL},NULL,FLDFILL}
346 * a decimal number contained in
348 #define UAT_DEC_CB_DEF(basename,field_name,rec_t) \
349 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, unsigned len, void* u1 _U_, void* u2 _U_) {\
350 ((rec_t*)rec)->(field_name) = strtol(buf,end,10); } \
351 static void basename ## _ ## field_name ## _tostr_cb(void* rec, char** out_ptr, unsigned* out_len, void* u1 _U_, void* u2 _U_) {\
352 *out_ptr = ep_strdup_printf("%d",((rec_t*)rec)->(field_name)); \
353 *out_len = strlen(*out_ptr); }
355 #define UAT_FLD_DEC(basename,field_name) \
356 {#field_name, PT_TXTMOD_STRING,{uat_fld_chk_num_dec,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{NULL,NULL,NULL},NULL,FLDFILL}
361 * an hexadecimal number contained in
363 #define UAT_HEX_CB_DEF(basename,field_name,rec_t) \
364 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, unsigned len, void* u1 _U_, void* u2 _U_) {\
365 ((rec_t*)rec)->(field_name) = strtol(buf,end,16); } \
366 static void basename ## _ ## field_name ## _tostr_cb(void* rec, char** out_ptr, unsigned* out_len, void* u1 _U_, void* u2 _U_) {\
367 *out_ptr = ep_strdup_printf("%x",((rec_t*)rec)->(field_name)); \
368 *out_len = strlen(*out_ptr); }
370 #define UAT_FLD_HEX(basename,field_name) \
371 {#field_name, PT_TXTMOD_STRING,{uat_fld_chk_num_hex,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{NULL,NULL,NULL},NULL,FLDFILL}
376 * enum_t: name = ((enum_t*)ptr)->strptr
377 * value = ((enum_t*)ptr)->value
381 #define UAT_VS_DEF(basename,field_name,rec_t,default_val,default_str) \
382 static void basename ## _ ## field_name ## _set_cb(void* rec, const char* buf, unsigned len, void* vs, void* u2 _U_) {\
383 guint i; ((rec_t*)rec)->field_name = default_val; \
384 char* str = ep_strndup(buf,len); \
386 for(i=0; ( cstr = ((value_string*)vs)[i].strptr ) ;i++) { \
387 if (g_str_equal(cstr,str)) { \
388 ((rec_t*)rec)->field_name = ((value_string*)vs)[i].value; return; } } } \
389 static void basename ## _ ## field_name ## _tostr_cb(void* rec, char** out_ptr, unsigned* out_len, void* vs, void* u2 _U_) {\
391 *out_ptr = default_str; *out_len = strlen(default_str);\
392 for(i=0;((value_string*)vs)[i].strptr;i++) { \
393 if ( ((value_string*)vs)[i].value == ((rec_t*)rec)->field_name ) { \
394 *out_ptr = ep_strdup(((value_string*)vs)[i].strptr); \
395 *out_len = strlen(*out_ptr); return; } } }
398 #define UAT_FLD_VS(basename,field_name,enum) \
399 {#field_name, PT_TXTMOD_ENUM,{uat_fld_chk_enum,basename ## _ ## field_name ## _set_cb,basename ## _ ## field_name ## _tostr_cb},{&(enum),&(enum),&(enum)},&(enum),FLDFILL}