Fix Win64 compilation problems in the plugins directory.
[obnox/wireshark/wip.git] / plugins / mate / mate_util.c
index 1c66cea3df3b4e9489112ac6b41c2ad757125eff..3a6ce69878ce72d915b8dae5144a3c93dbc1d16e 100644 (file)
@@ -2,7 +2,7 @@
 * MATE -- Meta Analysis Tracing Engine
 * Utility Library: Single Copy Strings and Attribute Value Pairs
 *
-* Copyright 2004, Luis E. Garcia Ontanon <luis.ontanon@gmail.com>
+* Copyright 2004, Luis E. Garcia Ontanon <luis@ontanon.org>
 *
 * $Id$
 *
 
 #include "mate.h"
 #include "mate_util.h"
-#include <wiretap/file_util.h>
-#include <epan/ws_strsplit.h>
+#include <wsutil/file_util.h>
+
+/***************************************************************************
+*  ADDRDIFF
+***************************************************************************
+* This is a macro that computes the difference between the raw address
+* values of two pointers (rather than the difference between the pointers)
+* as a ptrdiff_t.
+***************************************************************************/
+#define ADDRDIFF(p,q)  (((char *)(void *)(p)) - ((char *)(void *)(q)))
+
 
 /***************************************************************************
 *  dbg_print
@@ -54,7 +63,7 @@ void dbg_print(const gint* which, gint how, FILE* where, const gchar* fmt, ... )
        va_end( list );
 
        if (! where) {
-               g_message(debug_buffer);
+               g_message("%s", debug_buffer);
        } else {
                fputs(debug_buffer,where);
                fputs("\n",where);
@@ -82,7 +91,7 @@ void dbg_print(const gint* which, gint how, FILE* where, const gchar* fmt, ... )
 struct _scs_collection {
        GHashTable* hash;       /* key: a string value: guint number of subscribers */
        GMemChunk* ctrs;
-       GMemChunk* mate_small;  
+       GMemChunk* mate_small;
        GMemChunk* mate_medium;
        GMemChunk* mate_large;
        GMemChunk* mate_huge;
@@ -94,7 +103,7 @@ extern void destroy_scs_collection(SCS_collection* c) {
        if ( c->mate_medium ) g_mem_chunk_destroy(c->mate_medium);
        if ( c->mate_large ) g_mem_chunk_destroy(c->mate_large);
        if ( c->mate_huge ) g_mem_chunk_destroy(c->mate_huge);
-       
+
        if (c->hash) g_hash_table_destroy(c->hash);
 }
 
@@ -102,19 +111,19 @@ extern SCS_collection* scs_init(void) {
        SCS_collection* c = g_malloc(sizeof(SCS_collection));
 
        c->hash =  g_hash_table_new(g_str_hash,g_str_equal);
-       
+
        c->ctrs = g_mem_chunk_new("ints_scs_chunk", sizeof(guint),
                                                           sizeof(guint) * SCS_SMALL_CHUNK_SIZE, G_ALLOC_AND_FREE);
-       
+
        c->mate_small = g_mem_chunk_new("small_scs_chunk", SCS_SMALL_SIZE,
                                                           SCS_SMALL_SIZE * SCS_SMALL_CHUNK_SIZE, G_ALLOC_AND_FREE);
-       
+
        c->mate_medium = g_mem_chunk_new("medium_scs_chunk", SCS_MEDIUM_SIZE,
                                                           SCS_MEDIUM_SIZE * SCS_MEDIUM_CHUNK_SIZE, G_ALLOC_AND_FREE);
-       
+
        c->mate_large = g_mem_chunk_new("large_scs_chunk", SCS_LARGE_SIZE,
                                                           SCS_LARGE_SIZE * SCS_LARGE_CHUNK_SIZE, G_ALLOC_AND_FREE);
-       
+
        c->mate_huge = g_mem_chunk_new("huge_scs_chunk", SCS_HUGE_SIZE,
                                                           SCS_HUGE_SIZE * SCS_HUGE_CHUNK_SIZE, G_ALLOC_AND_FREE);
        return c;
@@ -138,17 +147,17 @@ gchar* scs_subscribe(SCS_collection* c, const gchar* s) {
        guint* ip = NULL;
        size_t len = 0;
        GMemChunk* chunk = NULL;
-       
-       g_hash_table_lookup_extended(c->hash,(gconstpointer)s,(gpointer*)&orig,(gpointer*)&ip);
+
+       g_hash_table_lookup_extended(c->hash,(gconstpointer)s,(gpointer)&orig,(gpointer)&ip);
 
        if (ip) {
                (*ip)++;
        } else {
                ip = g_mem_chunk_alloc(c->ctrs);
                *ip = 0;
-               
+
                len = strlen(s) + 1;
-               
+
                if (len <= SCS_SMALL_SIZE) {
                        chunk = c->mate_small;
                        len = SCS_SMALL_SIZE;
@@ -166,10 +175,10 @@ gchar* scs_subscribe(SCS_collection* c, const gchar* s) {
                        len = SCS_HUGE_SIZE;
                        g_warning("mate SCS: string truncated to huge size");
                }
-               
+
                orig = g_mem_chunk_alloc(chunk);
                strncpy(orig,s,len);
-               
+
                g_hash_table_insert(c->hash,orig,ip);
        }
 
@@ -189,15 +198,15 @@ void scs_unsubscribe(SCS_collection* c, gchar* s) {
        guint* ip = NULL;
        size_t len = 0xffff;
        GMemChunk* chunk = NULL;
-       
-       g_hash_table_lookup_extended(c->hash,(gconstpointer)s,(gpointer*)&orig,(gpointer*)&ip);
+
+       g_hash_table_lookup_extended(c->hash,(gconstpointer)s,(gpointer)&orig,(gpointer)&ip);
 
        if (ip) {
                if (*ip == 0) {
                        g_hash_table_remove(c->hash,orig);
-                       
+
                        len = strlen(orig);
-                       
+
                        if (len < SCS_SMALL_SIZE) {
                                chunk = c->mate_small;
                        } else if (len < SCS_MEDIUM_SIZE) {
@@ -206,8 +215,8 @@ void scs_unsubscribe(SCS_collection* c, gchar* s) {
                                chunk = c->mate_large;
                        } else {
                                chunk = c->mate_huge;
-                       } 
-                       
+                       }
+
                        g_mem_chunk_free(chunk,orig);
                        g_mem_chunk_free(c->ctrs,ip);
                }
@@ -215,7 +224,7 @@ void scs_unsubscribe(SCS_collection* c, gchar* s) {
                        (*ip)--;
                }
        } else {
-               g_warning("unsusbcribe: not subscribed");
+               g_warning("unsubscribe: not subscribed");
        }
 }
 
@@ -231,9 +240,9 @@ void scs_unsubscribe(SCS_collection* c, gchar* s) {
 gchar* scs_subscribe_printf(SCS_collection* c, gchar* fmt, ...) {
        va_list list;
        static gchar buf[SCS_HUGE_SIZE];
-       
+
        va_start( list, fmt );
-       g_vsnprintf(buf, SCS_HUGE_SIZE-1 ,fmt, list);
+       g_vsnprintf(buf, SCS_HUGE_SIZEfmt, list);
        va_end( list );
 
        return scs_subscribe(c,buf);
@@ -336,7 +345,7 @@ extern void avp_init(void) {
 extern AVP* new_avp_from_finfo(const gchar* name, field_info* finfo) {
        AVP* new = g_mem_chunk_alloc(avp_chunk);
        gchar* value;
-       
+
        new->n = scs_subscribe(avp_strings, name);
 
        if (finfo->value.ftype->val_to_string_repr) {
@@ -772,7 +781,7 @@ gchar* avpl_to_str(AVPL* avpl) {
 
        for(c=avpl->null.next; c->avp; c = c->next) {
                avp_s = avp_to_str(c->avp);
-               g_string_sprintfa(s," %s;",avp_s);
+               g_string_append_printf(s," %s;",avp_s);
                g_free(avp_s);
        }
 
@@ -791,7 +800,7 @@ extern gchar* avpl_to_dotstr(AVPL* avpl) {
 
        for(c=avpl->null.next; c->avp; c = c->next) {
                avp_s = avp_to_str(c->avp);
-               g_string_sprintfa(s," .%s;",avp_s);
+               g_string_append_printf(s," .%s;",avp_s);
                g_free(avp_s);
        }
 
@@ -816,7 +825,7 @@ extern gchar* avpl_to_dotstr(AVPL* avpl) {
 extern void merge_avpl(AVPL* dst, AVPL* src, gboolean copy_avps) {
        AVPN* cd = NULL;
        AVPN* cs = NULL;
-       gint c;
+       ptrdiff_t c;
        AVP* copy;
 
 #ifdef _AVP_DEBUGGING
@@ -829,7 +838,7 @@ extern void merge_avpl(AVPL* dst, AVPL* src, gboolean copy_avps) {
        while(cs->avp) {
 
                if(cd->avp) {
-                       c = (guint) cd->avp->n - (guint) cs->avp->n;
+                       c = ADDRDIFF(cd->avp->n,cs->avp->n);
                } else {
                        c = -1;
                }
@@ -977,8 +986,8 @@ extern AVP* match_avp(AVP* src, AVP* op) {
                        }
                case AVP_OP_ENDS:
                        /* does this work? */
-                       ls = strlen(src->v);
-                       lo = strlen(op->v);
+                       ls = (guint) strlen(src->v);
+                       lo = (guint) strlen(op->v);
 
                        if ( ls < lo ) {
                                return NULL;
@@ -1021,7 +1030,7 @@ extern AVPL* new_avpl_loose_match(const gchar* name,
        AVPL* newavpl = new_avpl(scs_subscribe(avp_strings, name));
        AVPN* co = NULL;
        AVPN* cs = NULL;
-       gint  c;
+       ptrdiff_t c;
        AVP* m;
        AVP* copy;
 
@@ -1043,7 +1052,7 @@ extern AVPL* new_avpl_loose_match(const gchar* name,
                }
 
 
-               c = (guint) co->avp->n - (guint) cs->avp->n;
+               c = ADDRDIFF(co->avp->n, cs->avp->n);
 
                if ( c > 0 ) {
                        if (co->avp) co = co->next;
@@ -1095,21 +1104,21 @@ extern AVPL* new_avpl_every_match(const gchar* name, AVPL* src, AVPL* op, gboole
        AVPL* newavpl;
        AVPN* co = NULL;
        AVPN* cs = NULL;
-       gint c;
+       ptrdiff_t 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
        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;
@@ -1124,7 +1133,7 @@ extern AVPL* new_avpl_every_match(const gchar* name, AVPL* src, AVPL* op, gboole
                        break;
                }
 
-               c = (guint) co->avp->n - (guint) cs->avp->n;
+               c = ADDRDIFF(co->avp->n,cs->avp->n);
 
                if ( c > 0 ) {
                        delete_avpl(newavpl,TRUE);
@@ -1186,9 +1195,9 @@ extern AVPL* new_avpl_exact_match(const gchar* name,AVPL* src, AVPL* op, gboolea
        AVPL* newavpl = new_avpl(name);
        AVPN* co = NULL;
        AVPN* cs = NULL;
-       gint c;
+       ptrdiff_t c;
        AVP* m;
-    AVP* copy;
+       AVP* copy;
 
 #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);
@@ -1196,7 +1205,7 @@ extern AVPL* new_avpl_exact_match(const gchar* name,AVPL* src, AVPL* op, gboolea
 
        if (op->len == 0)
                return newavpl;
-       
+
        if (src->len == 0) {
                delete_avpl(newavpl,FALSE);
                return NULL;
@@ -1206,7 +1215,7 @@ extern AVPL* new_avpl_exact_match(const gchar* name,AVPL* src, AVPL* op, gboolea
        co = op->null.next;
        while(1) {
 
-               c = (guint) co->avp->n - (guint) cs->avp->n;
+               c = ADDRDIFF(co->avp->n,cs->avp->n);
 
                if ( c > 0 ) {
                        delete_avpl(newavpl,TRUE);
@@ -1255,7 +1264,7 @@ extern AVPL* new_avpl_exact_match(const gchar* name,AVPL* src, AVPL* op, gboolea
 
 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) {
                case AVPL_STRICT:
                        avpl = new_avpl_exact_match(name,src,op,copy_avps);
@@ -1271,7 +1280,7 @@ extern AVPL* new_avpl_from_match(avpl_match_mode mode, const gchar* name,AVPL* s
                        merge_avpl(avpl, op, copy_avps);
                        break;
        }
-       
+
        return avpl;
 }
 
@@ -1326,7 +1335,7 @@ extern void avpl_transform(AVPL* src, AVPL_Transf* op) {
 #endif
 
        for ( ; op ; op = op->next) {
-               
+
                avpl = new_avpl_from_match(op->match_mode, src->name,src, op->match, TRUE);
 
                if (avpl) {
@@ -1557,7 +1566,7 @@ static LoAL* load_loal_error(FILE* fp, LoAL* loal, AVPL* curr, int linenum, cons
        gchar* desc;
        LoAL* ret = NULL;
        gchar* err;
-       
+
        va_start( list, fmt );
        desc = g_strdup_vprintf(fmt, list);
        va_end( list );
@@ -1631,7 +1640,7 @@ extern LoAL* loal_from_file(gchar* filename) {
 
        state = START;
 
-       if (( fp = eth_fopen(filename,"r") )) {
+       if (( fp = ws_fopen(filename,"r") )) {
                while(( c = (gchar) fgetc(fp) )){
 
                        if ( feof(fp) ) {