Use <wiretap/file_util.h> to include "file_util.h"; otherwise, the
[obnox/wireshark/wip.git] / plugins / mate / mate_util.c
index 7a61a20552756efe47420b5d45c8044a5ce36b80..1856f34d4d1364fbd7ec0c629b87ef5489b3ae80 100644 (file)
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
 
-#include "config.h"
-#include <glib.h>
-#include <stdio.h>
-#include <string.h>
 #include "mate.h"
 #include "mate_util.h"
-
-/* TODO:
-+ fix debug_print levels
-+ chunks for scs_strings (do I realy need em??)
-       + checking bounds in (almost) every operator
-- operators
-       + rethink '&' operator
-       - bounds check on op_match()
-       ? change &{} into []
-       ? add {xxx} to get avps named xxxx from the src avpl
-       ? add (yyy) to do aaa+31 or and similar stuff
-       - add perlre operator?
- - transform
-       + map (N->M hash)
-*/
-
+#include <wiretap/file_util.h>
 
 /***************************************************************************
 *  dbg_print
@@ -61,8 +42,8 @@
  * fmt, ...: what to print
  */
 
-void dbg_print(const guint* which, guint how, FILE* where, guint8* fmt, ... ) {
-       static guint8 debug_buffer[DEBUG_BUFFER_SIZE];
+void dbg_print(const gint* which, gint how, FILE* where, const gchar* fmt, ... ) {
+       static gchar debug_buffer[DEBUG_BUFFER_SIZE];
        va_list list;
 
        if ( ! which || *which < how ) return;
@@ -75,6 +56,7 @@ void dbg_print(const guint* which, guint how, FILE* where, guint8* fmt, ... ) {
                g_message(debug_buffer);
        } else {
                fputs(debug_buffer,where);
+               fputs("\n",where);
        }
 
 }
@@ -83,11 +65,10 @@ void dbg_print(const guint* which, guint how, FILE* where, guint8* fmt, ... ) {
 /***************************************************************************
  *  single copy strings
  ***************************************************************************
- * In order to save memory and since strings repeat more often than don't,
+ * Strings repeat more often than don't. In order to save memory
  * we'll keep only one copy of each as key to a hash with a count of
  * subscribers as value.
  ***************************************************************************/
-/* FIXME: use hash fom glib 1.X not 2.X */
 
 /**
  * scs_init:
@@ -151,13 +132,13 @@ extern SCS_collection* scs_init(void) {
  *
  * Return value: a pointer to the subscribed string.
  **/
-guint8* scs_subscribe(SCS_collection* c, guint8* s) {
-       guint8* orig = NULL;
+gchar* scs_subscribe(SCS_collection* c, const gchar* s) {
+       gchar* orig = NULL;
        guint* ip = NULL;
        size_t len = 0;
        GMemChunk* chunk = NULL;
        
-       g_hash_table_lookup_extended(c->hash,s,(gpointer*)&orig,(gpointer*)&ip);
+       g_hash_table_lookup_extended(c->hash,(gconstpointer)s,(gpointer*)&orig,(gpointer*)&ip);
 
        if (ip) {
                (*ip)++;
@@ -202,13 +183,13 @@ guint8* scs_subscribe(SCS_collection* c, guint8* s) {
  * decreases the count of subscribers, if zero frees the internal copy of
  * the string.
  **/
-void scs_unsubscribe(SCS_collection* c, guint8* s) {
-       guint8* orig = NULL;
+void scs_unsubscribe(SCS_collection* c, gchar* s) {
+       gchar* orig = NULL;
        guint* ip = NULL;
        size_t len = 0xffff;
        GMemChunk* chunk = NULL;
        
-       g_hash_table_lookup_extended(c->hash,s,(gpointer*)&orig,(gpointer*)&ip);
+       g_hash_table_lookup_extended(c->hash,(gconstpointer)s,(gpointer*)&orig,(gpointer*)&ip);
 
        if (ip) {
                if (*ip == 0) {
@@ -233,7 +214,7 @@ void scs_unsubscribe(SCS_collection* c, guint8* s) {
                        (*ip)--;
                }
        } else {
-               g_warning("unsusbcribe: already deleted: '%s'?",s);
+               g_warning("unsusbcribe: not subscribed");
        }
 }
 
@@ -246,9 +227,9 @@ void scs_unsubscribe(SCS_collection* c, guint8* s) {
  * Return value: the stored copy of the formated string.
  *
  **/
-extern guint8* scs_subscribe_printf(SCS_collection* c, guint8* fmt, ...) {
+gchar* scs_subscribe_printf(SCS_collection* c, gchar* fmt, ...) {
        va_list list;
-       static guint8 buf[SCS_HUGE_SIZE];
+       static gchar buf[SCS_HUGE_SIZE];
        
        va_start( list, fmt );
        g_vsnprintf(buf, SCS_HUGE_SIZE-1 ,fmt, list);
@@ -257,22 +238,6 @@ extern guint8* scs_subscribe_printf(SCS_collection* c, guint8* fmt, ...) {
        return scs_subscribe(c,buf);
 }
 
-extern guint8* scs_subscribe_int(SCS_collection* c, int i) {
-       static guint8 buf[SCS_SMALL_SIZE];
-       
-       g_snprintf(buf, SCS_SMALL_SIZE-1 ,"%i", i);
-       
-       return scs_subscribe(c,buf);
-}
-
-extern guint8* scs_subscribe_float(SCS_collection* c, float f) {
-       static guint8 buf[SCS_SMALL_SIZE];
-       
-       g_snprintf(buf, SCS_SMALL_SIZE-1 ,"%f", f);
-       
-       return scs_subscribe(c,buf);
-}
-
 /***************************************************************************
 *  AVPs & Co.
 ***************************************************************************
@@ -324,7 +289,7 @@ static int* dbg_avpl_op = &dbg_avpl_op_level;
  * @avpl: a pointer to the level of debugging of facility "avpl"
  * @avpl_op: a pointer to the level of debugging of facility "avpl_op"
  *
- * (If enabled set's up the debug facilities for the avp library.
+ * If enabled set's up the debug facilities for the avp library.
  *
  **/
 extern void setup_avp_debug(FILE* fp, int* general, int* avp, int* avp_op, int* avpl, int* avpl_op) {
@@ -347,13 +312,10 @@ extern void setup_avp_debug(FILE* fp, int* general, int* avp, int* avp_op, int*
  **/
 extern void avp_init(void) {
 
+       if (avp_strings) destroy_scs_collection(avp_strings);
        avp_strings = scs_init();
 
-
-       if ( avp_chunk ) {
-               g_mem_chunk_destroy(avp_chunk);
-       }
-
+       if ( avp_chunk ) g_mem_chunk_destroy(avp_chunk);
        avp_chunk = g_mem_chunk_new("avp_chunk", sizeof(any_avp_type),
                                                                AVP_CHUNK_SIZE, G_ALLOC_AND_FREE);
 
@@ -370,26 +332,16 @@ extern void avp_init(void) {
  * Return value: a pointer to the newly created avp.
  *
  **/
-extern AVP* new_avp_from_finfo(guint8* name, field_info* finfo) {
+extern AVP* new_avp_from_finfo(const gchar* name, field_info* finfo) {
        AVP* new = g_mem_chunk_alloc(avp_chunk);
-       guint8* value;
+       gchar* value;
        
        new->n = scs_subscribe(avp_strings, name);
 
-       if (finfo->value.ftype->get_value_integer) {
-               value = scs_subscribe_int(avp_strings, fvalue_get_integer(&finfo->value));
-#ifdef _AVP_DEBUGGING
-               dbg_print (dbg_avp,2,dbg_fp,"new_avp_from_finfo: from integer: %s",value);
-#endif
-       } else if (finfo->value.ftype->val_to_string_repr) {
+       if (finfo->value.ftype->val_to_string_repr) {
                value = scs_subscribe(avp_strings, fvalue_to_string_repr(&finfo->value,FTREPR_DISPLAY,NULL));
 #ifdef _AVP_DEBUGGING
                dbg_print (dbg_avp,2,dbg_fp,"new_avp_from_finfo: from string: %s",value);
-#endif
-       } else if (finfo->value.ftype->get_value_floating) {
-               value = scs_subscribe_float(avp_strings, (float) fvalue_get_floating(&finfo->value));
-#ifdef _AVP_DEBUGGING
-               dbg_print (dbg_avp,2,dbg_fp,"new_avp_from_finfo: from float: %s",value);
 #endif
        } else {
 #ifdef _AVP_DEBUGGING
@@ -421,7 +373,7 @@ extern AVP* new_avp_from_finfo(guint8* name, field_info* finfo) {
  * Return value: a pointer to the newly created avp.
  *
  **/
-extern AVP* new_avp(guint8* name, guint8* value, guint8 o) {
+extern AVP* new_avp(const gchar* name, const gchar* value, gchar o) {
        AVP* new = g_mem_chunk_alloc(avp_chunk);
 
        new->n = scs_subscribe(avp_strings, name);
@@ -477,8 +429,8 @@ extern AVP* avp_copy(AVP* from) {
 }
 
 
-extern void rename_avp(AVP* avp, guint8* name) {
-       guint8* s = avp->n;
+static void rename_avp(AVP* avp, gchar* name) {
+       gchar* s = avp->n;
        avp->n = scs_subscribe(avp_strings,name);
        scs_unsubscribe(avp_strings,s);
 }
@@ -492,14 +444,14 @@ extern void rename_avp(AVP* avp, guint8* name) {
  * Return value: a pointer to the newly created avpl.
  *
  **/
-extern AVPL* new_avpl(guint8* name) {
+extern AVPL* new_avpl(const gchar* name) {
        AVPL* new_avpl = g_mem_chunk_alloc(avp_chunk);
 
 #ifdef _AVP_DEBUGGING
        dbg_print(dbg_avpl_op,7,dbg_fp,"new_avpl: %X name=%s",new_avpl,name);
 #endif
 
-       new_avpl->name = scs_subscribe(avp_strings, name);
+       new_avpl->name = name ? scs_subscribe(avp_strings, name) : scs_subscribe(avp_strings, "");
        new_avpl->len = 0;
        new_avpl->null.avp = NULL;
        new_avpl->null.next = &new_avpl->null;
@@ -509,7 +461,7 @@ extern AVPL* new_avpl(guint8* name) {
        return new_avpl;
 }
 
-extern void rename_avpl(AVPL* avpl, guint8* name) {
+extern void rename_avpl(AVPL* avpl, gchar* name) {
        scs_unsubscribe(avp_strings,avpl->name);
        avpl->name = scs_subscribe(avp_strings,name);
 }
@@ -592,7 +544,7 @@ extern gboolean insert_avp(AVPL* avpl, AVP* avp) {
  * Return value: a pointer to the next matching avp if there's one, else NULL.
  *
  **/
-extern AVP* get_avp_by_name(AVPL* avpl, guint8* name, void** cookie) {
+extern AVP* get_avp_by_name(AVPL* avpl, gchar* name, void** cookie) {
        AVPN* curr;
        AVPN* start = (AVPN*) *cookie;
 
@@ -631,7 +583,7 @@ extern AVP* get_avp_by_name(AVPL* avpl, guint8* name, void** cookie) {
  * Return value: a pointer to extracted avp if there's one, else NULL.
  *
  **/
-extern AVP* extract_avp_by_name(AVPL* avpl, guint8* name) {
+extern AVP* extract_avp_by_name(AVPL* avpl, gchar* name) {
        AVPN* curr;
        AVP* avp = NULL;
 
@@ -818,11 +770,11 @@ extern AVP* get_next_avp(AVPL* avpl, void** cookie) {
  * Return value: a pointer to the newly allocated string.
  *
  **/
-guint8* avpl_to_str(AVPL* avpl) {
+gchar* avpl_to_str(AVPL* avpl) {
        AVPN* c;
        GString* s = g_string_new("");
-       guint8* avp_s;
-       guint8* r;
+       gchar* avp_s;
+       gchar* r;
 
        for(c=avpl->null.next; c->avp; c = c->next) {
                avp_s = avp_to_str(c->avp);
@@ -837,11 +789,11 @@ guint8* avpl_to_str(AVPL* avpl) {
        return r;
 }
 
-extern guint8* avpl_to_dotstr(AVPL* avpl) {
+extern gchar* avpl_to_dotstr(AVPL* avpl) {
        AVPN* c;
        GString* s = g_string_new("");
-       guint8* avp_s;
-       guint8* r;
+       gchar* avp_s;
+       gchar* r;
 
        for(c=avpl->null.next; c->avp; c = c->next) {
                avp_s = avp_to_str(c->avp);
@@ -937,7 +889,7 @@ extern void merge_avpl(AVPL* dst, AVPL* src, gboolean copy_avps) {
  * Return value: a pointer to the newly allocated string.
  *
  **/
-extern AVPL* new_avpl_from_avpl(guint8* name, AVPL* avpl, gboolean copy_avps) {
+extern AVPL* new_avpl_from_avpl(const gchar* name, AVPL* avpl, gboolean copy_avps) {
        AVPL* newavpl = new_avpl(name);
        void* cookie = NULL;
        AVP* avp;
@@ -965,106 +917,6 @@ extern AVPL* new_avpl_from_avpl(guint8* name, AVPL* avpl, gboolean copy_avps) {
        return newavpl;
 }
 
-
-#define TRANS_NUM   '-': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '0'
-
-/* BROKEN, makes no sense right now */
-/* FIXME: Use subscribe/unsubscribe */
-#if 0
-static AVP* avp_transform(AVP* src, AVP* op) {
-       unsigned int i;
-       guint8 c;
-
-       GString* replace_str = NULL;
-       GString* num_str = NULL;
-       GString* return_str = NULL;
-       guint8* v;
-       long num = 0;
-
-       enum _tranf_state {
-               START,
-               IN_NUM,
-               IN_REPLACE,
-               END
-       } state;
-
-       state = START;
-
-       for(i = 0; i < strlen(op->v); i++) {
-               c= op->v[i];
-
-               if (state == END) break;
-
-               switch(state) {
-                       case START:
-                               switch(c) {
-                                       case '{':
-                                               num_str = g_string_new("");
-                                               state = IN_NUM;
-                                               continue;
-                                       default:
-                                               continue;
-                               };
-                       case IN_NUM:
-                               switch(c) {
-                                       case TRANS_NUM:
-                                               g_string_append_c(num_str,c);
-                                               continue;
-                                       case ':':
-                                               num = strtol(num_str->str,NULL,10);
-                                               g_string_free(num_str,TRUE);
-                                               replace_str = g_string_new("");
-                                               state = IN_REPLACE;
-                                               continue;
-                                       default:
-                                               /* will ignore any char that is not a number */
-                                               continue;
-                               };
-                       case IN_REPLACE:
-                               switch(c) {
-                                       case '\\':
-                                               continue;
-                                       case '}':
-                                               state = END;
-                                               continue;
-                                       default :
-                                               g_string_append_c(replace_str,c);
-                                               continue;
-                               }
-                       case END:
-                               /* it will never reach */
-                               continue;
-               }
-
-       }
-
-       v = src->v;
-
-       if (num > 0) {
-               return_str = g_string_new(v);
-               g_string_erase(return_str,0,num);
-               g_string_prepend(return_str,replace_str->str);
-       } else if (num < 0) {
-               return_str = g_string_new(v);
-               g_string_truncate(return_str,return_str->len+num);
-               g_string_append(return_str,replace_str->str);
-
-       } else {
-               return_str = g_string_new(replace_str->str);
-       }
-
-       g_mem_chunk_free(avp_chunk,v);
-       g_string_free(replace_str,TRUE);
-
-       src->o = '=';
-       src->v = return_str->str;
-       g_string_free(return_str,FALSE);
-
-       return src;
-}
-
-#endif
-
 /**
 * match_avp:
  * @src: an src to be compared agains an "op" avp
@@ -1167,7 +1019,7 @@ extern AVP* match_avp(AVP* src, AVP* op) {
  * Return value: a pointer to the newly created avpl containing the
  *                              matching avps.
  **/
-extern AVPL* new_avpl_loose_match(guint8* name,
+extern AVPL* new_avpl_loose_match(const gchar* name,
                                                                  AVPL* src,
                                                                  AVPL* op,
                                                                  gboolean copy_avps) {
@@ -1245,19 +1097,26 @@ extern AVPL* new_avpl_loose_match(guint8* name,
  * Return value: a pointer to the newly created avpl containing the
  *                              matching avps.
  **/
-extern AVPL* new_avpl_every_match(guint8* name, AVPL* src, AVPL* op, gboolean copy_avps) {
-       AVPL* newavpl = new_avpl(scs_subscribe(avp_strings, name));
+extern AVPL* new_avpl_every_match(const gchar* name, AVPL* src, AVPL* op, gboolean copy_avps) {
+       AVPL* newavpl;
        AVPN* co = NULL;
        AVPN* cs = NULL;
        gint c;
        AVP* m;
        AVP* copy;
-
+       gboolean matches;
+       
 #ifdef _AVP_DEBUGGING
        dbg_print(dbg_avpl_op,3,dbg_fp,"new_avpl_every_match: %X src=%X op=%X name='%s'",newavpl,src,op,name);
 #endif
-
-       gboolean matches = TRUE;
+       if (src->len == 0) return NULL;
+       
+       newavpl = new_avpl(scs_subscribe(avp_strings, name));
+       
+       if (op->len == 0)
+               return newavpl;
+       
+       matches = TRUE;
 
        cs = src->null.next;
        co = op->null.next;
@@ -1329,7 +1188,7 @@ extern AVPL* new_avpl_every_match(guint8* name, AVPL* src, AVPL* op, gboolean co
  * Return value: a pointer to the newly created avpl containing the
  *                              matching avps.
  **/
-extern AVPL* new_avpl_exact_match(guint8* name,AVPL* src, AVPL* op, gboolean copy_avps) {
+extern AVPL* new_avpl_exact_match(const gchar* name,AVPL* src, AVPL* op, gboolean copy_avps) {
        AVPL* newavpl = new_avpl(name);
        AVPN* co = NULL;
        AVPN* cs = NULL;
@@ -1341,6 +1200,14 @@ extern AVPL* new_avpl_exact_match(guint8* name,AVPL* src, AVPL* op, gboolean cop
        dbg_print(dbg_avpl_op,3,dbg_fp,"new_avpl_every_match: %X src=%X op=%X name='%s'",newavpl,src,op,name);
 #endif
 
+       if (op->len == 0)
+               return newavpl;
+       
+       if (src->len == 0) {
+               delete_avpl(newavpl,FALSE);
+               return NULL;
+       }
+
        cs = src->null.next;
        co = op->null.next;
        while(1) {
@@ -1392,7 +1259,7 @@ extern AVPL* new_avpl_exact_match(guint8* name,AVPL* src, AVPL* op, gboolean cop
        return NULL;
 }
 
-extern AVPL* new_avpl_from_match(avpl_match_mode mode, guint8* name,AVPL* src, AVPL* op, gboolean copy_avps) {
+extern AVPL* new_avpl_from_match(avpl_match_mode mode, const gchar* name,AVPL* src, AVPL* op, gboolean copy_avps) {
        AVPL* avpl = NULL;
        
        switch (mode) {
@@ -1421,7 +1288,7 @@ extern AVPL* new_avpl_from_match(avpl_match_mode mode, guint8* name,AVPL* src, A
  *
  * Return value: a pointer to the newly created avpl transformation
  **/
-extern AVPL_Transf* new_avpl_transform(guint8* name, AVPL* mixed, avpl_match_mode match_mode, avpl_replace_mode replace_mode) {
+static AVPL_Transf* new_avpl_transform(gchar* name, AVPL* mixed, avpl_match_mode match_mode, avpl_replace_mode replace_mode) {
        AVPL_Transf* t = g_malloc(sizeof(AVPL_Transf));
        AVP* avp;
 
@@ -1544,7 +1411,7 @@ extern void avpl_transform(AVPL* src, AVPL_Transf* op) {
  *
  * Return value: a pointer to the newly created loal.
  **/
-extern LoAL* new_loal(guint8* name) {
+extern LoAL* new_loal(const gchar* name) {
        LoAL* new_loal = g_mem_chunk_alloc(avp_chunk);
 
        if (! name) {
@@ -1723,11 +1590,11 @@ extern void delete_loal(LoAL* loal, gboolean avpls_too, gboolean avps_too) {
  * load_loal_error:
  * Used by loal_from_file to handle errors while loading.
  **/
-LoAL* load_loal_error(FILE* fp, LoAL* loal, AVPL* curr, int linenum, guint8* fmt, ...) {
+static LoAL* load_loal_error(FILE* fp, LoAL* loal, AVPL* curr, int linenum, const gchar* fmt, ...) {
        va_list list;
-       guint8* desc;
+       gchar* desc;
        LoAL* ret = NULL;
-       guint8* err;
+       gchar* err;
        
        va_start( list, fmt );
        desc = g_strdup_vprintf(fmt, list);
@@ -1748,7 +1615,7 @@ LoAL* load_loal_error(FILE* fp, LoAL* loal, AVPL* curr, int linenum, guint8* fmt
 }
 
 
-/*  the maximum lenght allowed for a line */
+/*  the maximum length allowed for a line */
 #define MAX_ITEM_LEN   8192
 
 /* this two ugly things are used for tokenizing */
@@ -1773,15 +1640,15 @@ case '7': case '8': case '9': case '.'
  * Return value: if successful a pointer to the new populated loal, else NULL.
  *
  **/
-extern LoAL* loal_from_file(guint8* filename) {
-       FILE *fp;
-       guint8 c;
+extern LoAL* loal_from_file(gchar* filename) {
+       FILE *fp = NULL;
+       gchar c;
        int i = 0;
        guint32 linenum = 1;
-       guint8 linenum_buf[MAX_ITEM_LEN];
-       guint8 name[MAX_ITEM_LEN];
-       guint8 value[MAX_ITEM_LEN];
-       guint8 op = '?';
+       gchar linenum_buf[MAX_ITEM_LEN];
+       gchar name[MAX_ITEM_LEN];
+       gchar value[MAX_ITEM_LEN];
+       gchar op = '?';
        LoAL *loal = new_loal(filename);
        AVPL* curr = NULL;
        AVP* avp;
@@ -1796,18 +1663,18 @@ extern LoAL* loal_from_file(guint8* filename) {
 
 #ifndef _WIN32
        if (! getuid()) {
-               g_warning( "MATE Will not run as root");
-               return NULL;
+               return load_loal_error(fp,loal,curr,linenum,"MATE Will not run as root");
        }
 #endif
 
        state = START;
 
-       if (( fp = fopen(filename,"r") )) {
-               while(( c = (guint8) fgetc(fp) )){
+       if (( fp = eth_fopen(filename,"r") )) {
+               while(( c = (gchar) fgetc(fp) )){
 
                        if ( feof(fp) ) {
                                if ( ferror(fp) ) {
+                                       report_read_failure(filename,errno);
                                        return load_loal_error(fp,loal,curr,linenum,"Error while reading '%f'",filename);
                                }
                                break;
@@ -1818,7 +1685,7 @@ extern LoAL* loal_from_file(guint8* filename) {
                        }
 
                        if ( i >= MAX_ITEM_LEN - 1  ) {
-                               return load_loal_error(fp,loal,curr,linenum,"Maximum item lenght exceeded");
+                               return load_loal_error(fp,loal,curr,linenum,"Maximum item length exceeded");
                        }
 
                        switch(state) {
@@ -1940,6 +1807,7 @@ extern LoAL* loal_from_file(guint8* filename) {
                return loal;
 
        } else {
+               report_open_failure(filename,errno,FALSE);
                return load_loal_error(NULL,loal,NULL,0,"Cannot Open file '%s'",filename);
        }
 }