6 * User Accessible Tables
7 * Mantain an array of user accessible data strucures
9 * (c) 2007, Luis E. Garcia Ontanon <luis@ontanon.org>
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.
42 #include <wsutil/file_util.h>
43 #include <wsutil/str_util.h>
45 #include <epan/emem.h>
46 #include <epan/report_err.h>
47 #include <epan/filesystem.h>
48 #include <epan/packet.h>
49 #include <epan/range.h>
53 static GPtrArray* all_uats = NULL;
56 all_uats = g_ptr_array_new();
59 uat_t* uat_new(const char* name,
62 gboolean from_profile,
67 uat_copy_cb_t copy_cb,
68 uat_update_cb_t update_cb,
69 uat_free_cb_t free_cb,
70 uat_post_update_cb_t post_update_cb,
71 uat_field_t* flds_array) {
73 uat_t* uat = g_malloc(sizeof(uat_t));
76 /* Add to global array of uats */
78 all_uats = g_ptr_array_new();
80 g_ptr_array_add(all_uats,uat);
83 g_assert(name && size && filename && data_ptr && numitems_ptr);
85 /* Set uat values from inputs */
86 uat->name = g_strdup(name);
87 uat->record_size = size;
88 uat->filename = g_strdup(filename);
89 uat->from_profile = from_profile;
90 uat->user_ptr = data_ptr;
91 uat->nrows_p = numitems_ptr;
92 uat->copy_cb = copy_cb;
93 uat->update_cb = update_cb;
94 uat->free_cb = free_cb;
95 uat->post_update_cb = post_update_cb;
96 uat->fields = flds_array;
97 uat->user_data = g_array_new(FALSE,FALSE,(guint)uat->record_size);
100 uat->from_global = FALSE;
102 uat->free_rep = NULL;
104 uat->category = category;
106 for (i=0;flds_array[i].title;i++) {
107 fld_data_t* f = g_malloc(sizeof(fld_data_t));
113 flds_array[i].priv = f;
124 void* uat_add_record(uat_t* uat, const void* data) {
127 g_array_append_vals (uat->user_data, data, 1);
129 rec = uat->user_data->data + (uat->record_size * (uat->user_data->len-1));
132 uat->copy_cb(rec, data, (unsigned int) uat->record_size);
140 void uat_swap(uat_t* uat, guint a, guint b) {
141 size_t s = uat->record_size;
142 void* tmp = ep_alloc(s);
144 g_assert( a < uat->user_data->len && b < uat->user_data->len );
148 memcpy(tmp, UAT_INDEX_PTR(uat,a), s);
149 memcpy(UAT_INDEX_PTR(uat,a), UAT_INDEX_PTR(uat,b), s);
150 memcpy(UAT_INDEX_PTR(uat,b), tmp, s);
154 void uat_remove_record_idx(uat_t* uat, guint idx) {
156 g_assert( idx < uat->user_data->len );
159 uat->free_cb(UAT_INDEX_PTR(uat,idx));
162 g_array_remove_index(uat->user_data, idx);
168 /* The returned filename was g_malloc()'d so the caller must free it */
169 gchar* uat_get_actual_filename(uat_t* uat, gboolean for_writing) {
170 gchar *pers_fname = NULL;
172 if (! uat->from_global) {
173 pers_fname = get_persconffile_path(uat->filename, uat->from_profile, for_writing);
176 if ((! for_writing ) && (! file_exists(pers_fname) )) {
177 gchar* data_fname = get_datafile_path(uat->filename);
179 if (file_exists(data_fname)) {
192 uat_t* uat_get_table_by_name(const char* name) {
195 for (i=0; i < all_uats->len; i++) {
196 uat_t* u = g_ptr_array_index(all_uats,i);
197 if ( g_str_equal(u->name,name) ) {
205 static void putfld(FILE* fp, void* rec, uat_field_t* f) {
209 f->cb.tostr(rec,&fld_ptr,&fld_len,f->cbdata.tostr,f->fld_data);
213 case PT_TXTMOD_FILENAME:
214 case PT_TXTMOD_DIRECTORYNAME:
215 case PT_TXTMOD_STRING: {
220 for(i=0;i<fld_len;i++) {
223 if (c == '"' || c == '\\' || ! isprint((guchar)c) ) {
224 fprintf(fp,"\\x%.2x",c);
233 case PT_TXTMOD_HEXBYTES: {
236 for(i=0;i<fld_len;i++) {
237 fprintf(fp,"%.2x",((guint8*)fld_ptr)[i]);
243 g_assert_not_reached();
247 gboolean uat_save(uat_t* uat, char** error) {
249 gchar* fname = uat_get_actual_filename(uat,TRUE);
252 if (! fname ) return FALSE;
254 fp = ws_fopen(fname,"w");
256 if (!fp && errno == ENOENT) {
257 /* Parent directory does not exist, try creating first */
258 gchar *pf_dir_path = NULL;
259 if (create_persconffile_dir(&pf_dir_path) != 0) {
260 *error = ep_strdup_printf("uat_save: error creating '%s'", pf_dir_path);
261 g_free (pf_dir_path);
264 fp = ws_fopen(fname,"w");
268 *error = ep_strdup_printf("uat_save: error opening '%s': %s",fname,g_strerror(errno));
275 fprintf(fp,"# This file is automatically generated, DO NOT MODIFY.\n");
277 for ( i = 0 ; i < uat->user_data->len ; i++ ) {
278 void* rec = uat->user_data->data + (uat->record_size * i);
285 for( j=0 ; j < uat->ncols ; j++ ) {
286 putfld(fp, rec, &(f[j]));
287 fputs((j == uat->ncols - 1) ? "\n" : "," ,fp);
294 uat->changed = FALSE;
299 void uat_destroy(uat_t* uat) {
300 /* XXX still missing a destructor */
301 g_ptr_array_remove(all_uats,uat);
305 uat_t *uat_find(gchar *name) {
308 for (i=0; i < all_uats->len; i++) {
309 uat_t* u = g_ptr_array_index(all_uats,i);
311 if (strcmp(u->name, name) == 0 || strcmp(u->filename, name) == 0) {
318 void uat_clear(uat_t* uat) {
321 for ( i = 0 ; i < uat->user_data->len ; i++ ) {
323 uat->free_cb(UAT_INDEX_PTR(uat,i));
327 g_array_set_size(uat->user_data,0);
329 *((uat)->user_ptr) = NULL;
330 *((uat)->nrows_p) = 0;
333 void* uat_dup(uat_t* uat, guint* len_p) {
334 guint size = (guint) (uat->record_size * uat->user_data->len);
336 return size ? g_memdup(uat->user_data->data,size) : NULL ;
339 void* uat_se_dup(uat_t* uat, guint* len_p) {
340 guint size = (guint) (uat->record_size * uat->user_data->len);
341 *len_p = (guint) size;
342 return size ? se_memdup(uat->user_data->data,size) : NULL ;
345 void uat_unload_all(void) {
348 for (i=0; i < all_uats->len; i++) {
349 uat_t* u = g_ptr_array_index(all_uats,i);
350 /* Do not unload if not in profile */
351 if (u->from_profile) {
358 void uat_cleanup(void) {
359 while( all_uats->len ) {
360 uat_destroy((uat_t*)all_uats->pdata);
363 g_ptr_array_free(all_uats,TRUE);
367 void uat_foreach_table(uat_cb_t cb,void* user_data) {
370 for (i=0; i < all_uats->len; i++)
371 cb(g_ptr_array_index(all_uats,i), user_data);
376 void uat_load_all(void) {
380 for (i=0; i < all_uats->len; i++) {
381 uat_t* u = g_ptr_array_index(all_uats,i);
388 report_failure("Error loading table '%s': %s",u->name,err);
394 gboolean uat_fld_chk_str(void* u1 _U_, const char* strptr, unsigned len _U_, const void* u2 _U_, const void* u3 _U_, const char** err) {
395 if (strptr == NULL) {
396 *err = "NULL pointer";
404 gboolean uat_fld_chk_oid(void* u1 _U_, const char* strptr, unsigned len, const void* u2 _U_, const void* u3 _U_, const char** err) {
408 if (strptr == NULL) {
409 *err = "NULL pointer";
413 for(i = 0; i < len; i++)
414 if(!(isdigit(strptr[i]) || strptr[i] == '.')) {
415 *err = "Only digits [0-9] and \".\" allowed in an OID";
419 if(strptr[len-1] == '.')
420 *err = "OIDs must not be terminated with a \".\"";
422 if(!((*strptr == '0' || *strptr == '1' || *strptr =='2') && (len > 1 && strptr[1] == '.')))
423 *err = "OIDs must start with \"0.\" (ITU-T assigned), \"1.\" (ISO assigned) or \"2.\" (joint ISO/ITU-T assigned)";
425 /* should also check that the second arc is in the range 0-39 */
430 gboolean uat_fld_chk_proto(void* u1 _U_, const char* strptr, unsigned len, const void* u2 _U_, const void* u3 _U_, const char** err) {
432 char* name = ep_strndup(strptr,len);
433 ascii_strdown_inplace(name);
436 if (find_dissector(name)) {
440 *err = "dissector not found";
449 gboolean uat_fld_chk_num_dec(void* u1 _U_, const char* strptr, unsigned len, const void* u2 _U_, const void* u3 _U_, const char** err) {
451 char* str = ep_strndup(strptr,len);
452 long i = strtol(str,&str,10);
454 if ( ( i == 0) && (errno == ERANGE || errno == EINVAL) ) {
455 *err = g_strerror(errno);
464 gboolean uat_fld_chk_num_hex(void* u1 _U_, const char* strptr, unsigned len, const void* u2 _U_, const void* u3 _U_, const char** err) {
466 char* str = ep_strndup(strptr,len);
467 long i = strtol(str,&str,16);
469 if ( ( i == 0) && (errno == ERANGE || errno == EINVAL) ) {
470 *err = g_strerror(errno);
479 gboolean uat_fld_chk_enum(void* u1 _U_, const char* strptr, unsigned len, const void* v, const void* u3 _U_, const char** err) {
480 char* str = ep_strndup(strptr,len);
482 const value_string* vs = v;
484 for(i=0;vs[i].strptr;i++) {
485 if (g_str_equal(vs[i].strptr,str)) {
491 *err = ep_strdup_printf("invalid value: %s",str);
495 gboolean uat_fld_chk_range(void* u1 _U_, const char* strptr, unsigned len, const void* v _U_, const void* u3, const char** err) {
496 char* str = ep_strndup(strptr,len);
498 convert_ret_t ret = range_convert_str(&r, str,GPOINTER_TO_UINT(u3));
504 case CVT_SYNTAX_ERROR:
505 *err = ep_strdup_printf("syntax error in range: %s",str);
507 case CVT_NUMBER_TOO_BIG:
508 *err = ep_strdup_printf("value too large in range: '%s' (max = %u)",str,GPOINTER_TO_UINT(u3));
511 *err = "This should not happen, it is a bug in wireshark! please report to wireshark-dev@wireshark.org";
516 static int xton(char d) {
528 case 'a': case 'A': return 10;
529 case 'b': case 'B': return 11;
530 case 'c': case 'C': return 12;
531 case 'd': case 'D': return 13;
532 case 'e': case 'E': return 14;
533 case 'f': case 'F': return 15;
538 char* uat_unbinstring(const char* si, guint in_len, guint* len_p) {
540 guint len = in_len/2;
548 buf= g_malloc0(len+1);
549 if (len_p) *len_p = len;
555 buf[i++] = (d1 * 16) + d0;
563 char* uat_unesc(const char* si, guint in_len, guint* len_p) {
564 char* buf = g_malloc0(in_len+1);
568 const char* in_end = si+in_len;
570 for (s = (void*)si; s < in_end; s++) {
574 case 'a': *(p++) = '\a'; len++; break;
575 case 'b': *(p++) = '\b'; len++; break;
576 case 'e': *(p++) = '\033' /* '\e' is non ANSI-C */; len++; break;
577 case 'f': *(p++) = '\f'; len++; break;
578 case 'n': *(p++) = '\n'; len++; break;
579 case 'r': *(p++) = '\r'; len++; break;
580 case 't': *(p++) = '\t'; len++; break;
581 case 'v': *(p++) = '\v'; len++; break;
599 if ( s[1] >= '0' && s[1] <= '7' ) {
603 if ( s[1] >= '0' && s[1] <= '7' ) {
609 c = (64 * c2) + (8 * c1) + c0;
610 *(p++) = (char) (c > 255 ? 255 : c);
620 if (isxdigit((guchar)c1) && isxdigit((guchar)c0)) {
621 *(p++) = (xton(c1) * 0x10) + xton(c0);
641 if (len_p) *len_p = len;
645 char* uat_undquote(const char* si, guint in_len, guint* len_p) {
646 return uat_unesc(si+1,in_len-2,len_p);
650 char* uat_esc(const char* buf, guint len) {
651 const guint8* end = ((guint8*)buf)+len;
652 char* out = ep_alloc0((4*len)+1);
656 for (b = (void*)buf; b < end; b++) {
660 g_snprintf(s,5,"\\x%.2x",((guint)*b));
669 CHK_STR_IS_DEF(isprint)
670 CHK_STR_IS_DEF(isalpha)
671 CHK_STR_IS_DEF(isalnum)
672 CHK_STR_IS_DEF(isdigit)
673 CHK_STR_IS_DEF(isxdigit)
681 * indent-tabs-mode: nil
684 * ex: set shiftwidth=4 tabstop=8 expandtab:
685 * :indentSize=4:tabSize=8:noTabs=true: