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);
101 uat->free_rep = NULL;
103 uat->category = category;
105 for (i=0;flds_array[i].title;i++) {
106 fld_data_t* f = g_malloc(sizeof(fld_data_t));
112 flds_array[i].priv = f;
123 void* uat_add_record(uat_t* uat, const void* data) {
126 g_array_append_vals (uat->user_data, data, 1);
128 rec = uat->user_data->data + (uat->record_size * (uat->user_data->len-1));
131 uat->copy_cb(rec, data, (unsigned int) uat->record_size);
139 void uat_swap(uat_t* uat, guint a, guint b) {
140 size_t s = uat->record_size;
141 void* tmp = ep_alloc(s);
143 g_assert( a < uat->user_data->len && b < uat->user_data->len );
147 memcpy(tmp, UAT_INDEX_PTR(uat,a), s);
148 memcpy(UAT_INDEX_PTR(uat,a), UAT_INDEX_PTR(uat,b), s);
149 memcpy(UAT_INDEX_PTR(uat,b), tmp, s);
153 void uat_remove_record_idx(uat_t* uat, guint idx) {
155 g_assert( idx < uat->user_data->len );
158 uat->free_cb(UAT_INDEX_PTR(uat,idx));
161 g_array_remove_index(uat->user_data, idx);
167 /* The returned filename was g_malloc()'d so the caller must free it */
168 gchar* uat_get_actual_filename(uat_t* uat, gboolean for_writing) {
170 gchar* pers_fname = get_persconffile_path(uat->filename, uat->from_profile, for_writing);
172 if (! for_writing ) {
173 gchar* data_fname = get_datafile_path(uat->filename);
175 if ((! file_exists(pers_fname) ) && file_exists(data_fname)) {
183 if ((! file_exists(pers_fname) ) && (! for_writing ) ) {
191 uat_t* uat_get_table_by_name(const char* name) {
194 for (i=0; i < all_uats->len; i++) {
195 uat_t* u = g_ptr_array_index(all_uats,i);
196 if ( g_str_equal(u->name,name) ) {
204 static void putfld(FILE* fp, void* rec, uat_field_t* f) {
208 f->cb.tostr(rec,&fld_ptr,&fld_len,f->cbdata.tostr,f->fld_data);
212 case PT_TXTMOD_STRING: {
217 for(i=0;i<fld_len;i++) {
220 if (c == '"' || c == '\\' || ! isprint((guchar)c) ) {
221 fprintf(fp,"\\x%.2x",c);
230 case PT_TXTMOD_HEXBYTES: {
233 for(i=0;i<fld_len;i++) {
234 fprintf(fp,"%.2x",((guint8*)fld_ptr)[i]);
240 g_assert_not_reached();
244 gboolean uat_save(uat_t* uat, char** error) {
246 gchar* fname = uat_get_actual_filename(uat,TRUE);
249 if (! fname ) return FALSE;
251 fp = ws_fopen(fname,"w");
253 if (!fp && errno == ENOENT) {
254 /* Parent directory does not exist, try creating first */
255 gchar *pf_dir_path = NULL;
256 if (create_persconffile_dir(&pf_dir_path) != 0) {
257 *error = ep_strdup_printf("uat_save: error creating '%s'", pf_dir_path);
258 g_free (pf_dir_path);
261 fp = ws_fopen(fname,"w");
265 *error = ep_strdup_printf("uat_save: error opening '%s': %s",fname,g_strerror(errno));
272 fprintf(fp,"# This file is automatically generated, DO NOT MODIFY.\n");
274 for ( i = 0 ; i < uat->user_data->len ; i++ ) {
275 void* rec = uat->user_data->data + (uat->record_size * i);
282 for( j=0 ; j < uat->ncols ; j++ ) {
283 putfld(fp, rec, &(f[j]));
284 fputs((j == uat->ncols - 1) ? "\n" : "," ,fp);
291 uat->changed = FALSE;
296 void uat_destroy(uat_t* uat) {
297 /* XXX still missing a destructor */
298 g_ptr_array_remove(all_uats,uat);
302 uat_t *uat_find(gchar *name) {
305 for (i=0; i < all_uats->len; i++) {
306 uat_t* u = g_ptr_array_index(all_uats,i);
308 if (strcmp(u->name, name) == 0 || strcmp(u->filename, name) == 0) {
315 void uat_clear(uat_t* uat) {
318 for ( i = 0 ; i < uat->user_data->len ; i++ ) {
320 uat->free_cb(UAT_INDEX_PTR(uat,i));
324 g_array_set_size(uat->user_data,0);
326 *((uat)->user_ptr) = NULL;
327 *((uat)->nrows_p) = 0;
330 void* uat_dup(uat_t* uat, guint* len_p) {
331 guint size = (guint) (uat->record_size * uat->user_data->len);
333 return size ? g_memdup(uat->user_data->data,size) : NULL ;
336 void* uat_se_dup(uat_t* uat, guint* len_p) {
337 guint size = (guint) (uat->record_size * uat->user_data->len);
338 *len_p = (guint) size;
339 return size ? se_memdup(uat->user_data->data,size) : NULL ;
342 void uat_unload_all(void) {
345 for (i=0; i < all_uats->len; i++) {
346 uat_t* u = g_ptr_array_index(all_uats,i);
347 /* Do not unload if not in profile */
348 if (u->from_profile) {
355 void uat_cleanup(void) {
356 while( all_uats->len ) {
357 uat_destroy((uat_t*)all_uats->pdata);
360 g_ptr_array_free(all_uats,TRUE);
364 void uat_foreach_table(uat_cb_t cb,void* user_data) {
367 for (i=0; i < all_uats->len; i++)
368 cb(g_ptr_array_index(all_uats,i), user_data);
373 void uat_load_all(void) {
377 for (i=0; i < all_uats->len; i++) {
378 uat_t* u = g_ptr_array_index(all_uats,i);
385 report_failure("Error loading table '%s': %s",u->name,err);
391 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) {
392 if (strptr == NULL) {
393 *err = "NULL pointer";
401 gboolean uat_fld_chk_oid(void* u1 _U_, const char* strptr, unsigned len, const void* u2 _U_, const void* u3 _U_, const char** err) {
405 if (strptr == NULL) {
406 *err = "NULL pointer";
410 for(i = 0; i < len; i++)
411 if(!(isdigit(strptr[i]) || strptr[i] == '.')) {
412 *err = "Only digits [0-9] and \".\" allowed in an OID";
416 if(strptr[len-1] == '.')
417 *err = "OIDs must not be terminated with a \".\"";
419 if(!((*strptr == '0' || *strptr == '1' || *strptr =='2') && (len > 1 && strptr[1] == '.')))
420 *err = "OIDs must start with \"0.\" (ITU-T assigned), \"1.\" (ISO assigned) or \"2.\" (joint ISO/ITU-T assigned)";
422 /* should also check that the second arc is in the range 0-39 */
427 gboolean uat_fld_chk_proto(void* u1 _U_, const char* strptr, unsigned len, const void* u2 _U_, const void* u3 _U_, const char** err) {
429 char* name = ep_strndup(strptr,len);
430 ascii_strdown_inplace(name);
433 if (find_dissector(name)) {
437 *err = "dissector not found";
446 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) {
448 char* str = ep_strndup(strptr,len);
449 long i = strtol(str,&str,10);
451 if ( ( i == 0) && (errno == ERANGE || errno == EINVAL) ) {
452 *err = g_strerror(errno);
461 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) {
463 char* str = ep_strndup(strptr,len);
464 long i = strtol(str,&str,16);
466 if ( ( i == 0) && (errno == ERANGE || errno == EINVAL) ) {
467 *err = g_strerror(errno);
476 gboolean uat_fld_chk_enum(void* u1 _U_, const char* strptr, unsigned len, const void* v, const void* u3 _U_, const char** err) {
477 char* str = ep_strndup(strptr,len);
479 const value_string* vs = v;
481 for(i=0;vs[i].strptr;i++) {
482 if (g_str_equal(vs[i].strptr,str)) {
488 *err = ep_strdup_printf("invalid value: %s",str);
492 gboolean uat_fld_chk_range(void* u1 _U_, const char* strptr, unsigned len, const void* v _U_, const void* u3, const char** err) {
493 char* str = ep_strndup(strptr,len);
495 convert_ret_t ret = range_convert_str(&r, str,GPOINTER_TO_UINT(u3));
501 case CVT_SYNTAX_ERROR:
502 *err = ep_strdup_printf("syntax error in range: %s",str);
504 case CVT_NUMBER_TOO_BIG:
505 *err = ep_strdup_printf("value too large in range: '%s' (max = %u)",str,GPOINTER_TO_UINT(u3));
508 *err = "This should not happen, it is a bug in wireshark! please report to wireshark-dev@wireshark.org";
513 static int xton(char d) {
525 case 'a': case 'A': return 10;
526 case 'b': case 'B': return 11;
527 case 'c': case 'C': return 12;
528 case 'd': case 'D': return 13;
529 case 'e': case 'E': return 14;
530 case 'f': case 'F': return 15;
535 char* uat_unbinstring(const char* si, guint in_len, guint* len_p) {
537 guint len = in_len/2;
545 buf= g_malloc0(len+1);
546 if (len_p) *len_p = len;
552 buf[i++] = (d1 * 16) + d0;
560 char* uat_unesc(const char* si, guint in_len, guint* len_p) {
561 char* buf = g_malloc0(in_len+1);
565 const char* in_end = si+in_len;
567 for (s = (void*)si; s < in_end; s++) {
571 case 'a': *(p++) = '\a'; len++; break;
572 case 'b': *(p++) = '\b'; len++; break;
573 case 'e': *(p++) = '\033' /* '\e' is non ANSI-C */; len++; break;
574 case 'f': *(p++) = '\f'; len++; break;
575 case 'n': *(p++) = '\n'; len++; break;
576 case 'r': *(p++) = '\r'; len++; break;
577 case 't': *(p++) = '\t'; len++; break;
578 case 'v': *(p++) = '\v'; len++; break;
596 if ( s[1] >= '0' && s[1] <= '7' ) {
600 if ( s[1] >= '0' && s[1] <= '7' ) {
606 c = (64 * c2) + (8 * c1) + c0;
607 *(p++) = (char) (c > 255 ? 255 : c);
617 if (isxdigit((guchar)c1) && isxdigit((guchar)c0)) {
618 *(p++) = (xton(c1) * 0x10) + xton(c0);
638 if (len_p) *len_p = len;
642 char* uat_undquote(const char* si, guint in_len, guint* len_p) {
643 return uat_unesc(si+1,in_len-2,len_p);
647 char* uat_esc(const char* buf, guint len) {
648 const guint8* end = ((guint8*)buf)+len;
649 char* out = ep_alloc0((4*len)+1);
653 for (b = (void*)buf; b < end; b++) {
657 g_snprintf(s,5,"\\x%.2x",((guint)*b));
666 CHK_STR_IS_DEF(isprint)
667 CHK_STR_IS_DEF(isalpha)
668 CHK_STR_IS_DEF(isalnum)
669 CHK_STR_IS_DEF(isdigit)
670 CHK_STR_IS_DEF(isxdigit)
678 * indent-tabs-mode: nil
681 * ex: set shiftwidth=4 tabstop=4 expandtab
682 * :indentSize=4:tabSize=4:noTabs=true: