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.
41 #include <epan/emem.h>
42 #include <epan/report_err.h>
43 #include <epan/filesystem.h>
44 #include <epan/packet.h>
45 #include <epan/range.h>
46 #include <wsutil/file_util.h>
50 static GPtrArray* all_uats = NULL;
53 all_uats = g_ptr_array_new();
56 uat_t* uat_new(const char* name,
59 gboolean from_profile,
64 uat_copy_cb_t copy_cb,
65 uat_update_cb_t update_cb,
66 uat_free_cb_t free_cb,
67 uat_field_t* flds_array) {
68 uat_t* uat = g_malloc(sizeof(uat_t));
72 all_uats = g_ptr_array_new();
74 g_ptr_array_add(all_uats,uat);
76 g_assert(name && size && filename && data_ptr && numitems_ptr);
78 uat->name = g_strdup(name);
79 uat->record_size = size;
80 uat->filename = g_strdup(filename);
81 uat->from_profile = from_profile;
82 uat->user_ptr = data_ptr;
83 uat->nrows_p = numitems_ptr;
84 uat->copy_cb = copy_cb;
85 uat->update_cb = update_cb;
86 uat->free_cb = free_cb;
87 uat->fields = flds_array;
88 uat->user_data = g_array_new(FALSE,FALSE,uat->record_size);
94 uat->category = category;
96 for (i=0;flds_array[i].name;i++) {
97 fld_data_t* f = g_malloc(sizeof(fld_data_t));
103 flds_array[i].priv = f;
115 void* uat_add_record(uat_t* uat, const void* data) {
118 g_array_append_vals (uat->user_data, data, 1);
120 rec = uat->user_data->data + (uat->record_size * (uat->user_data->len-1));
123 uat->copy_cb(rec, data, uat->record_size);
131 void uat_swap(uat_t* uat, guint a, guint b) {
132 guint s = uat->record_size;
133 void* tmp = ep_alloc(s);
135 g_assert( a < uat->user_data->len && b < uat->user_data->len );
139 memcpy(tmp, UAT_INDEX_PTR(uat,a), s);
140 memcpy(UAT_INDEX_PTR(uat,a), UAT_INDEX_PTR(uat,b), s);
141 memcpy(UAT_INDEX_PTR(uat,b), tmp, s);
145 void uat_remove_record_idx(uat_t* uat, guint idx) {
147 g_assert( idx < uat->user_data->len );
150 uat->free_cb(UAT_INDEX_PTR(uat,idx));
153 g_array_remove_index(uat->user_data, idx);
159 /* The returned filename was g_malloc()'d so the caller must free it */
160 gchar* uat_get_actual_filename(uat_t* uat, gboolean for_writing) {
162 gchar* pers_fname = get_persconffile_path(uat->filename, uat->from_profile, for_writing);
164 if (! for_writing ) {
165 gchar* data_fname = get_datafile_path(uat->filename);
167 if ((! file_exists(pers_fname) ) && file_exists(data_fname)) {
175 if ((! file_exists(pers_fname) ) && (! for_writing ) ) {
183 uat_t* uat_get_table_by_name(const char* name) {
186 for (i=0; i < all_uats->len; i++) {
187 uat_t* u = g_ptr_array_index(all_uats,i);
188 if ( g_str_equal(u->name,name) ) {
196 static void putfld(FILE* fp, void* rec, uat_field_t* f) {
200 f->cb.tostr(rec,&fld_ptr,&fld_len,f->cbdata.tostr,f->fld_data);
204 case PT_TXTMOD_STRING: {
209 for(i=0;i<fld_len;i++) {
212 if (c == '"' || c == '\\' || ! isprint((guchar)c) ) {
213 fprintf(fp,"\\x%.2x",c);
222 case PT_TXTMOD_HEXBYTES: {
225 for(i=0;i<fld_len;i++) {
226 fprintf(fp,"%.2x",((guint8*)fld_ptr)[i]);
232 g_assert_not_reached();
236 gboolean uat_save(uat_t* uat, char** error) {
238 gchar* fname = uat_get_actual_filename(uat,TRUE);
241 if (! fname ) return FALSE;
243 fp = ws_fopen(fname,"w");
246 *error = ep_strdup_printf("uat_save: error opening '%s': %s",fname,strerror(errno));
252 fprintf(fp,"# This file is automatically generated, DO NOT MODIFY.\n");
254 for ( i = 0 ; i < uat->user_data->len ; i++ ) {
255 void* rec = uat->user_data->data + (uat->record_size * i);
262 for( j=0 ; j < uat->ncols ; j++ ) {
263 putfld(fp, rec, &(f[j]));
264 fputs((j == uat->ncols - 1) ? "\n" : "," ,fp);
271 uat->changed = FALSE;
276 void uat_destroy(uat_t* uat) {
277 /* XXX still missing a destructor */
278 g_ptr_array_remove(all_uats,uat);
282 uat_t *uat_find(gchar *name) {
285 for (i=0; i < all_uats->len; i++) {
286 uat_t* u = g_ptr_array_index(all_uats,i);
288 if (strcmp(u->name, name) == 0 || strcmp(u->filename, name) == 0) {
295 void uat_clear(uat_t* uat) {
298 for ( i = 0 ; i < uat->user_data->len ; i++ ) {
300 uat->free_cb(UAT_INDEX_PTR(uat,i));
304 g_array_set_size(uat->user_data,0);
306 *((uat)->user_ptr) = NULL;
307 *((uat)->nrows_p) = 0;
310 void* uat_dup(uat_t* uat, guint* len_p) {
311 guint size = (uat->record_size * uat->user_data->len);
313 return size ? g_memdup(uat->user_data->data,size) : NULL ;
316 void* uat_se_dup(uat_t* uat, guint* len_p) {
317 guint size = (uat->record_size * uat->user_data->len);
319 return size ? se_memdup(uat->user_data->data,size) : NULL ;
322 void uat_unload_all(void) {
325 for (i=0; i < all_uats->len; i++) {
326 uat_t* u = g_ptr_array_index(all_uats,i);
327 /* Do not unload if not in profile */
328 if (u->from_profile) {
335 void uat_cleanup(void) {
336 while( all_uats->len ) {
337 uat_destroy((uat_t*)all_uats->pdata);
340 g_ptr_array_free(all_uats,TRUE);
344 void uat_foreach_table(uat_cb_t cb,void* user_data) {
347 for (i=0; i < all_uats->len; i++)
348 cb(g_ptr_array_index(all_uats,i), user_data);
353 void uat_load_all(void) {
357 for (i=0; i < all_uats->len; i++) {
358 uat_t* u = g_ptr_array_index(all_uats,i);
365 report_failure("Error loading table '%s': %s",u->name,err);
371 gboolean uat_fld_chk_str(void* u1 _U_, const char* strptr, unsigned len _U_, void* u2 _U_, void* u3 _U_, const char** err) {
372 if (strptr == NULL) {
373 *err = "NULL pointer";
381 gboolean uat_fld_chk_proto(void* u1 _U_, const char* strptr, unsigned len, void* u2 _U_, void* u3 _U_, const char** err) {
383 char* name = ep_strndup(strptr,len);
387 if (find_dissector(name)) {
391 *err = "dissector not found";
400 gboolean uat_fld_chk_num_dec(void* u1 _U_, const char* strptr, unsigned len, void* u2 _U_, void* u3 _U_, const char** err) {
401 char* str = ep_strndup(strptr,len);
402 long i = strtol(str,&str,10);
404 if ( ( i == 0) && (errno == ERANGE || errno == EINVAL) ) {
405 *err = strerror(errno);
413 gboolean uat_fld_chk_num_hex(void* u1 _U_, const char* strptr, unsigned len, void* u2 _U_, void* u3 _U_, const char** err) {
414 char* str = ep_strndup(strptr,len);
415 long i = strtol(str,&str,16);
417 if ( ( i == 0) && (errno == ERANGE || errno == EINVAL) ) {
418 *err = strerror(errno);
426 gboolean uat_fld_chk_enum(void* u1 _U_, const char* strptr, unsigned len, void* v, void* u3 _U_, const char** err) {
427 char* str = ep_strndup(strptr,len);
429 value_string* vs = v;
431 for(i=0;vs[i].strptr;i++) {
432 if (g_str_equal(vs[i].strptr,str)) {
438 *err = ep_strdup_printf("invalid value: %s",str);
442 gboolean uat_fld_chk_range(void* u1 _U_, const char* strptr, unsigned len, void* v _U_, void* u3, const char** err) {
443 char* str = ep_strndup(strptr,len);
445 convert_ret_t ret = range_convert_str(&r, str,GPOINTER_TO_UINT(u3));
451 case CVT_SYNTAX_ERROR:
452 *err = ep_strdup_printf("syntax error in range: %s",str);
454 case CVT_NUMBER_TOO_BIG:
455 *err = ep_strdup_printf("value too large in range: '%s' (max = %u)",str,GPOINTER_TO_UINT(u3));
458 *err = "This should not happen, it is a bug in wireshark! please report to wireshark-dev@wireshark.org";
463 static int xton(char d) {
475 case 'a': case 'A': return 10;
476 case 'b': case 'B': return 11;
477 case 'c': case 'C': return 12;
478 case 'd': case 'D': return 13;
479 case 'e': case 'E': return 14;
480 case 'f': case 'F': return 15;
485 char* uat_unbinstring(const char* si, guint in_len, guint* len_p) {
487 guint len = in_len/2;
495 buf= g_malloc0(len+1);
496 if (len_p) *len_p = len;
502 buf[i++] = (d1 * 16) + d0;
510 char* uat_unesc(const char* si, guint in_len, guint* len_p) {
511 char* buf = g_malloc0(in_len+1);
515 const char* in_end = si+in_len;
517 for (s = (void*)si; s < in_end; s++) {
521 case 'a': *(p++) = '\a'; len++; break;
522 case 'b': *(p++) = '\b'; len++; break;
523 case 'e': *(p++) = '\033' /* '\e' is non ANSI-C */; len++; printf("."); break;
524 case 'f': *(p++) = '\f'; len++; break;
525 case 'n': *(p++) = '\n'; len++; break;
526 case 'r': *(p++) = '\r'; len++; break;
527 case 't': *(p++) = '\t'; len++; break;
528 case 'v': *(p++) = '\v'; len++; break;
546 if ( s[1] >= '0' && s[1] <= '7' ) {
550 if ( s[1] >= '0' && s[1] <= '7' ) {
556 c = (64 * c2) + (8 * c1) + c0;
557 *(p++) = (char) (c > 255 ? 255 : c);
567 if (isxdigit((guchar)c1) && isxdigit((guchar)c0)) {
568 *(p++) = (xton(c1) * 0x10) + xton(c0);
588 if (len_p) *len_p = len;
592 char* uat_undquote(const char* si, guint in_len, guint* len_p) {
593 return uat_unesc(si+1,in_len-2,len_p);
597 char* uat_esc(const char* buf, guint len) {
598 const guint8* end = ((guint8*)buf)+len;
599 char* out = ep_alloc0((4*len)+1);
603 for (b = (void*)buf; b < end; b++) {
607 g_snprintf(s,5,"\\x%.2x",((guint)*b));
616 CHK_STR_IS_DEF(isprint)
617 CHK_STR_IS_DEF(isalpha)
618 CHK_STR_IS_DEF(isalnum)
619 CHK_STR_IS_DEF(isdigit)
620 CHK_STR_IS_DEF(isxdigit)
628 * indent-tabs-mode: nil
631 * ex: set shiftwidth=4 tabstop=4 expandtab
632 * :indentSize=4:tabSize=4:noTabs=true: