2 * MATE -- Meta Analysis Tracing Engine
3 * Utility Library: Single Copy Strings and Attribute Value Pairs
5 * Copyright 2004, Luis E. Garcia Ontanon <luis@ontanon.org>
9 * Wireshark - Network traffic analyzer
10 * By Gerald Combs <gerald@wireshark.org>
11 * Copyright 1998 Gerald Combs
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #include "mate_util.h"
30 #include <wsutil/file_util.h>
32 /***************************************************************************
34 ***************************************************************************
35 * This is a macro that computes the difference between the raw address
36 * values of two pointers (rather than the difference between the pointers)
38 ***************************************************************************/
39 #define ADDRDIFF(p,q) (((char *)(void *)(p)) - ((char *)(void *)(q)))
42 /***************************************************************************
44 ***************************************************************************
45 * This is the debug facility of the thing.
46 ***************************************************************************/
49 * which: a pointer to the current level of debugging for a feature
50 * how: the level over which this message should be printed out
51 * where: the file on which to print (g_message if null)
52 * fmt, ...: what to print
55 void dbg_print(const gint* which, gint how, FILE* where, const gchar* fmt, ... ) {
56 static gchar debug_buffer[DEBUG_BUFFER_SIZE];
59 if ( ! which || *which < how ) return;
61 va_start( list, fmt );
62 g_vsnprintf(debug_buffer,DEBUG_BUFFER_SIZE,fmt,list);
66 g_message("%s", debug_buffer);
68 fputs(debug_buffer,where);
75 /***************************************************************************
77 ***************************************************************************
78 * Strings repeat more often than don't. In order to save memory
79 * we'll keep only one copy of each as key to a hash with a count of
80 * subscribers as value.
81 ***************************************************************************/
86 * Initializes the scs hash.
89 struct _scs_collection {
90 GHashTable* hash; /* key: a string value: guint number of subscribers */
93 /* ToDo? free any string,ctr entries pointed to by the hash table ??
94 * XXX: AFAIKT destroy_scs_collection() might be called only when reading a
95 * mate config file. Since reading a new config file can apparently currently
96 * only be done once after starting Wireshark, in theory this fcn
97 * currently should never be called since there will never be an existing
98 * scs_collection to be destroyed.
100 static void destroy_scs_collection(SCS_collection* c) {
101 if (c->hash) g_hash_table_destroy(c->hash);
104 static SCS_collection* scs_init(void) {
105 SCS_collection* c = g_malloc(sizeof(SCS_collection));
107 c->hash = g_hash_table_new(g_str_hash,g_str_equal);
115 * @collection: the scs hash
118 * Checks if the given string exists already and if so it increases the count of
119 * subsscribers and returns a pointer to the stored string. If not It will copy
120 * the given string store it in the hash and return the pointer to the copy.
121 * Remember, containment is handled internally, take care of your own strings.
123 * Return value: a pointer to the subscribed string.
125 gchar* scs_subscribe(SCS_collection* c, const gchar* s) {
130 g_hash_table_lookup_extended(c->hash,(gconstpointer)s,(gpointer)&orig,(gpointer)&ip);
135 ip = g_slice_new(guint);
140 if (len <= SCS_SMALL_SIZE) {
141 len = SCS_SMALL_SIZE;
142 } else if (len <= SCS_MEDIUM_SIZE) {
143 len = SCS_MEDIUM_SIZE;
144 } else if (len <= SCS_LARGE_SIZE) {
145 len = SCS_LARGE_SIZE;
146 } else if (len < SCS_HUGE_SIZE) {
150 g_warning("mate SCS: string truncated due to huge size");
153 orig = g_slice_alloc(len);
154 g_strlcpy(orig,s,len);
156 g_hash_table_insert(c->hash,orig,ip);
164 * @collection: the scs hash
167 * decreases the count of subscribers, if zero frees the internal copy of
170 void scs_unsubscribe(SCS_collection* c, gchar* s) {
175 g_hash_table_lookup_extended(c->hash,(gconstpointer)s,(gpointer)&orig,(gpointer)&ip);
179 g_hash_table_remove(c->hash,orig);
183 if (len < SCS_SMALL_SIZE) {
184 len = SCS_SMALL_SIZE;
185 } else if (len < SCS_MEDIUM_SIZE) {
186 len = SCS_MEDIUM_SIZE;
187 } else if (len < SCS_LARGE_SIZE) {
188 len = SCS_LARGE_SIZE;
193 g_slice_free1(len, orig);
194 g_slice_free(guint,ip);
200 g_warning("unsubscribe: not subscribed");
205 * scs_subscribe_printf:
206 * @fmt: a format string ...
208 * Formats the input and subscribes it.
210 * Return value: the stored copy of the formated string.
213 gchar* scs_subscribe_printf(SCS_collection* c, gchar* fmt, ...) {
215 static gchar buf[SCS_HUGE_SIZE];
217 va_start( list, fmt );
218 g_vsnprintf(buf, SCS_HUGE_SIZE, fmt, list);
221 return scs_subscribe(c,buf);
224 /***************************************************************************
226 ***************************************************************************
227 * The Thing operates mainly on avps, avpls and loals
228 * - attribute value pairs (two strings: the name and the value and an operator)
229 * - avp lists a somehow sorted list of avps
230 * - loal (list of avp lists) an arbitrarily sorted list of avpls
233 ***************************************************************************/
236 typedef union _any_avp_type {
245 static SCS_collection* avp_strings = NULL;
247 #ifdef _AVP_DEBUGGING
248 static FILE* dbg_fp = NULL;
250 static int dbg_level = 0;
251 static int* dbg = &dbg_level;
253 static int dbg_avp_level = 0;
254 static int* dbg_avp = &dbg_avp_level;
256 static int dbg_avp_op_level = 0;
257 static int* dbg_avp_op = &dbg_avp_op_level;
259 static int dbg_avpl_level = 0;
260 static int* dbg_avpl = &dbg_avpl_level;
262 static int dbg_avpl_op_level = 0;
263 static int* dbg_avpl_op = &dbg_avpl_op_level;
267 * @fp: the file in which to send debugging output.
268 * @general: a pointer to the level of debugging of facility "general"
269 * @avp: a pointer to the level of debugging of facility "avp"
270 * @avp_op: a pointer to the level of debugging of facility "avp_op"
271 * @avpl: a pointer to the level of debugging of facility "avpl"
272 * @avpl_op: a pointer to the level of debugging of facility "avpl_op"
274 * If enabled sets up the debug facilities for the avp library.
277 extern void setup_avp_debug(FILE* fp, int* general, int* avp, int* avp_op, int* avpl, int* avpl_op) {
283 dbg_avpl_op = avpl_op;
286 #endif /* _AVP_DEBUGGING */
291 * (Re)Initializes the avp library.
294 extern void avp_init(void) {
296 if (avp_strings) destroy_scs_collection(avp_strings);
297 avp_strings = scs_init();
302 * new_avp_from_finfo:
303 * @name: the name the avp will have.
304 * @finfo: the field_info from which to fetch the data.
306 * Creates an avp from a field_info record.
308 * Return value: a pointer to the newly created avp.
311 extern AVP* new_avp_from_finfo(const gchar* name, field_info* finfo) {
312 AVP* new = (AVP*)g_slice_new(any_avp_type);
315 new->n = scs_subscribe(avp_strings, name);
317 if (finfo->value.ftype->val_to_string_repr) {
318 value = scs_subscribe(avp_strings, fvalue_to_string_repr(&finfo->value,FTREPR_DISPLAY,NULL));
319 #ifdef _AVP_DEBUGGING
320 dbg_print (dbg_avp,2,dbg_fp,"new_avp_from_finfo: from string: %s",value);
323 #ifdef _AVP_DEBUGGING
324 dbg_print (dbg_avp,2,dbg_fp,"new_avp_from_finfo: a proto: %s",finfo->hfinfo->abbrev);
326 value = scs_subscribe(avp_strings, "");
333 #ifdef _AVP_DEBUGGING
334 dbg_print (dbg_avp,1,dbg_fp,"new_avp_from_finfo: %X %s%c%s;",(guint32) new,new->n,new->o,new->v);
343 * @name: the name the avp will have.
344 * @value: the value the avp will have.
345 * @o: the operator of this avp.
347 * Creates an avp given every parameter.
349 * Return value: a pointer to the newly created avp.
352 extern AVP* new_avp(const gchar* name, const gchar* value, gchar o) {
353 AVP* new = (AVP*)g_slice_new(any_avp_type);
355 new->n = scs_subscribe(avp_strings, name);
356 new->v = scs_subscribe(avp_strings, value);
359 #ifdef _AVP_DEBUGGING
360 dbg_print(dbg_avp,1,dbg_fp,"new_avp: %X %s%c%s;",(guint32) new,new->n,new->o,new->v);
368 * @avp: the avp to delete.
370 * Destroys an avp and releases the resources it uses.
373 extern void delete_avp(AVP* avp) {
374 #ifdef _AVP_DEBUGGING
375 dbg_print(dbg_avp,1,dbg_fp,"delete_avp: %X %s%c%s;",(guint32) avp,avp->n,avp->o,avp->v);
378 scs_unsubscribe(avp_strings, avp->n);
379 scs_unsubscribe(avp_strings, avp->v);
380 g_slice_free(any_avp_type,(any_avp_type*)avp);
386 * @from: the avp to be copied.
388 * Creates an avp whose name op and value are copies of the given one.
390 * Return value: a pointer to the newly created avp.
393 extern AVP* avp_copy(AVP* from) {
394 AVP* new = (AVP*)g_slice_new(any_avp_type);
396 new->n = scs_subscribe(avp_strings, from->n);
397 new->v = scs_subscribe(avp_strings, from->v);
400 #ifdef _AVP_DEBUGGING
401 dbg_print(dbg_avp,1,dbg_fp,"copy_avp: %X %s%c%s;",(guint32) new,new->n,new->o,new->v);
409 * @name: the name the avpl will have.
411 * Creates an empty avpl.
413 * Return value: a pointer to the newly created avpl.
416 extern AVPL* new_avpl(const gchar* name) {
417 AVPL* new_avpl_p = (AVPL*)g_slice_new(any_avp_type);
419 #ifdef _AVP_DEBUGGING
420 dbg_print(dbg_avpl_op,7,dbg_fp,"new_avpl_p: %X name=%s",new_avpl_p,name);
423 new_avpl_p->name = name ? scs_subscribe(avp_strings, name) : scs_subscribe(avp_strings, "");
425 new_avpl_p->null.avp = NULL;
426 new_avpl_p->null.next = &new_avpl_p->null;
427 new_avpl_p->null.prev = &new_avpl_p->null;
433 extern void rename_avpl(AVPL* avpl, gchar* name) {
434 scs_unsubscribe(avp_strings,avpl->name);
435 avpl->name = scs_subscribe(avp_strings,name);
440 * @avpl: the avpl in which to insert.
441 * @avp: the avp to be inserted.
443 * Inserts the given AVP into the given AVPL if an identical one isn't yet there.
445 * Return value: whether it was inserted or not.
447 * BEWARE: Check the return value, you might need to delete the avp if
448 * it is not inserted.
450 extern gboolean insert_avp(AVPL* avpl, AVP* avp) {
451 AVPN* new = (AVPN*)g_slice_new(any_avp_type);
456 #ifdef _AVP_DEBUGGING
457 dbg_print(dbg_avpl_op,7,dbg_fp,"new_avpn: %X",new);
458 dbg_print(dbg_avpl_op,4,dbg_fp,"insert_avp: %X %X %s%c%s;",avpl,avp,avp->n,avp->o,avp->v);
461 /* get to the insertion point */
462 for(c=avpl->null.next; c->avp; c = c->next) {
464 if ( avp->n == c->avp->n ) {
466 if (avp->v > c->avp->v) {
470 if (avp->v == c->avp->v) {
471 if (avp->o == AVP_OP_EQUAL) {
472 #ifdef _AVP_DEBUGGING
473 dbg_print(dbg_avpl_op,7,dbg_fp,"delete_avpn: %X",new);
475 g_slice_free(any_avp_type,(any_avp_type*)new);
481 if (avp->n > c->avp->n) {
486 #ifdef _AVP_DEBUGGING
487 dbg_print(dbg_avpl,5,dbg_fp,"insert_avp: inserting %X in %X before %X;",avp,avpl,c);
497 #ifdef _AVP_DEBUGGING
498 dbg_print(dbg_avpl,4,dbg_fp,"avpl: %X new len: %i",avpl,avpl->len);
506 * @avpl: the avpl from which to try to get the avp.
507 * @name: the name of the avp we are looking for.
508 * @cookie: variable in which to store the state between calls.
510 * Gets pointer to the next avp whose name is given; uses cookie to store its
511 * state between calls.
513 * Return value: a pointer to the next matching avp if there's one, else NULL.
516 extern AVP* get_avp_by_name(AVPL* avpl, gchar* name, void** cookie) {
518 AVPN* start = (AVPN*) *cookie;
520 #ifdef _AVP_DEBUGGING
521 dbg_print(dbg_avpl_op,7,dbg_fp,"get_avp_by_name: entering: %X %s %X",avpl,name,*cookie);
524 name = scs_subscribe(avp_strings, name);
526 if (!start) start = avpl->null.next;
528 for ( curr = start; curr->avp; curr = curr->next ) {
529 if ( curr->avp->n == name ) {
536 #ifdef _AVP_DEBUGGING
537 dbg_print(dbg_avpl_op,5,dbg_fp,"get_avp_by_name: got avp: %X",curr);
540 scs_unsubscribe(avp_strings, name);
546 * extract_avp_by_name:
547 * @avpl: the avpl from which to try to extract the avp.
548 * @name: the name of the avp we are looking for.
550 * Extracts from the avpl the next avp whose name is given;
552 * Return value: a pointer to extracted avp if there's one, else NULL.
555 extern AVP* extract_avp_by_name(AVPL* avpl, gchar* name) {
559 #ifdef _AVP_DEBUGGING
560 dbg_print(dbg_avpl_op,7,dbg_fp,"extract_avp_by_name: entering: %X %s",avpl,name);
563 name = scs_subscribe(avp_strings, name);
565 for ( curr = avpl->null.next; curr->avp; curr = curr->next ) {
566 if ( curr->avp->n == name ) {
571 scs_unsubscribe(avp_strings, name);
573 if( ! curr->avp ) return NULL;
575 curr->next->prev = curr->prev;
576 curr->prev->next = curr->next;
580 g_slice_free(any_avp_type,(any_avp_type*)curr);
584 #ifdef _AVP_DEBUGGING
585 dbg_print(dbg_avpl,4,dbg_fp,"avpl: %X new len: %i",avpl,avpl->len);
588 #ifdef _AVP_DEBUGGING
589 dbg_print(dbg_avpl_op,5,dbg_fp,"extract_avp_by_name: got avp: %X",avp);
598 * @avpl: the avpl from which to try to extract the avp.
600 * Extracts the fisrt avp from the avpl.
602 * Return value: a pointer to extracted avp if there's one, else NULL.
605 extern AVP* extract_first_avp(AVPL* avpl) {
609 #ifdef _AVP_DEBUGGING
610 dbg_print(dbg_avpl_op,7,dbg_fp,"extract_first_avp: %X",avpl);
613 node = avpl->null.next;
615 avpl->null.next->prev = &avpl->null;
616 avpl->null.next = node->next;
621 g_slice_free(any_avp_type,(any_avp_type*)node);
623 #ifdef _AVP_DEBUGGING
624 dbg_print(dbg_avpl,4,dbg_fp,"avpl: %X new len: %i",avpl,avpl->len);
628 #ifdef _AVP_DEBUGGING
629 dbg_print(dbg_avpl_op,5,dbg_fp,"extract_first_avp: got avp: %X",avp);
639 * @avpl: the avpl from which to try to extract the avp.
641 * Extracts the last avp from the avpl.
643 * Return value: a pointer to extracted avp if there's one, else NULL.
646 extern AVP* extract_last_avp(AVPL* avpl) {
650 node = avpl->null.prev;
652 avpl->null.prev->next = &avpl->null;
653 avpl->null.prev = node->prev;
658 g_slice_free(any_avp_type,(any_avp_type*)node);
660 #ifdef _AVP_DEBUGGING
661 dbg_print(dbg_avpl,4,dbg_fp,"avpl: %X new len: %i",avpl,avpl->len);
665 #ifdef _AVP_DEBUGGING
666 dbg_print(dbg_avpl_op,5,dbg_fp,"extract_last_avp: got avp: %X",avp);
676 * @avpl: the avpl from which to try to extract the avp.
677 * @avps_too: whether or not it should delete the avps as well.
679 * Destroys an avpl and releases the resources it uses. If told to do
680 * so releases the avps as well.
683 extern void delete_avpl(AVPL* avpl, gboolean avps_too) {
685 #ifdef _AVP_DEBUGGING
686 dbg_print(dbg_avpl,3,dbg_fp,"delete_avpl: %X",avpl);
689 while(( avp = extract_last_avp(avpl))) {
695 scs_unsubscribe(avp_strings,avpl->name);
696 g_slice_free(any_avp_type,(any_avp_type*)avpl);
703 * @avpl: the avpl from which to try to get the avps.
704 * @cookie: variable in which to store the state between calls.
706 * Iterates on an avpl to get its avps.
708 * Return value: a pointer to the next avp if there's one, else NULL.
711 extern AVP* get_next_avp(AVPL* avpl, void** cookie) {
714 #ifdef _AVP_DEBUGGING
715 dbg_print(dbg_avpl_op,5,dbg_fp,"get_next_avp: avpl: %X avpn: %X",avpl,*cookie);
719 node = (AVPN*) *cookie;
721 node = avpl->null.next;
724 *cookie = node->next;
726 #ifdef _AVP_DEBUGGING
727 dbg_print(dbg_avpl_op,5,dbg_fp,"extract_last_avp: got avp: %X",node->avp);
735 * @avpl: the avpl to represent.
737 * Creates a newly allocated string containing a representation of an avpl.
739 * Return value: a pointer to the newly allocated string.
742 gchar* avpl_to_str(AVPL* avpl) {
744 GString* s = g_string_new("");
748 for(c=avpl->null.next; c->avp; c = c->next) {
749 avp_s = avp_to_str(c->avp);
750 g_string_append_printf(s," %s;",avp_s);
755 g_string_free(s,FALSE);
757 /* g_strchug(r); ? */
761 extern gchar* avpl_to_dotstr(AVPL* avpl) {
763 GString* s = g_string_new("");
767 for(c=avpl->null.next; c->avp; c = c->next) {
768 avp_s = avp_to_str(c->avp);
769 g_string_append_printf(s," .%s;",avp_s);
774 g_string_free(s,FALSE);
776 /* g_strchug(r); ? */
782 * @dst: the avpl in which to merge the avps.
783 * @src: the avpl from which to get the avps.
784 * @copy: whether avps should be copied instead of referenced.
786 * Adds the avps of src that are not existent in dst into dst.
788 * Return value: a pointer to the newly allocated string.
791 extern void merge_avpl(AVPL* dst, AVPL* src, gboolean copy_avps) {
797 #ifdef _AVP_DEBUGGING
798 dbg_print(dbg_avpl_op,3,dbg_fp,"merge_avpl: %X %X",dst,src);
807 c = ADDRDIFF(cd->avp->n,cs->avp->n);
813 if (cd->avp) cd = cd->next;
816 copy = avp_copy(cs->avp);
817 if ( ! insert_avp(dst,copy) ) {
821 insert_avp(dst,cs->avp);
826 if ( ! cd->avp || ! (cd->avp->v == cs->avp->v) ) {
828 copy = avp_copy(cs->avp);
829 if ( ! insert_avp(dst,copy) ) {
833 insert_avp(dst,cs->avp);
837 if (cd->avp) cd = cd->next;
841 #ifdef _AVP_DEBUGGING
842 dbg_print(dbg_avpl_op,8,dbg_fp,"merge_avpl: done");
851 * @name: the name of the new avpl.
852 * @avpl: the avpl from which to get the avps.
853 * @copy_avps: whether avps should be copied instead of referenced.
855 * Creates a new avpl containing the same avps as the given avpl
856 * It will either reference or copie the avps.
858 * Return value: a pointer to the newly allocated string.
861 extern AVPL* new_avpl_from_avpl(const gchar* name, AVPL* avpl, gboolean copy_avps) {
862 AVPL* newavpl = new_avpl(name);
867 #ifdef _AVP_DEBUGGING
868 dbg_print(dbg_avpl_op,3,dbg_fp,"new_avpl_from_avpl: %X from=%X name='%s'",newavpl,avpl,name);
871 while(( avp = get_next_avp(avpl,&cookie) )) {
873 copy = avp_copy(avp);
874 if ( ! insert_avp(newavpl,copy) ) {
878 insert_avp(newavpl,avp);
882 #ifdef _AVP_DEBUGGING
883 dbg_print(dbg_avpl_op,8,dbg_fp,"new_avpl_from_avpl: done");
891 * @src: an src to be compared agains an "op" avp
892 * @op: the "op" avp that will be matched against the src avp
894 * Checks whether or not two avp's match.
896 * Return value: a pointer to the src avp if there's a match.
899 extern AVP* match_avp(AVP* src, AVP* op) {
907 gboolean lower = FALSE;
909 #ifdef _AVP_DEBUGGING
910 dbg_print(dbg_avpl_op,3,dbg_fp,"match_avp: %s%c%s; vs. %s%c%s;",src->n,src->o,src->v,op->n,op->o,op->v);
913 if ( src->n != op->n ) {
921 return src->v == op->v ? src : NULL;
922 case AVP_OP_NOTEQUAL:
923 return !( src->v == op->v) ? src : NULL;
925 return strncmp(src->v,op->v,strlen(op->v)) == 0 ? src : NULL;
927 splited = g_strsplit(op->v,"|",0);
929 for (i=0;splited[i];i++) {
930 if(g_str_equal(splited[i],src->v)) {
944 fs = (float) strtod(src->v, NULL);
945 fo = (float) strtod(src->v, NULL);
948 if (fs<fo) return src;
951 if (fs>fo) return src;
955 /* does this work? */
956 ls = (guint) strlen(src->v);
957 lo = (guint) strlen(op->v);
962 p = src->v + ( ls - lo );
963 return g_str_equal(p,op->v) ? src : NULL;
966 /* case AVP_OP_TRANSF: */
967 /* return do_transform(src,op); */
968 case AVP_OP_CONTAINS:
972 /* will never get here */
978 /* TODO: rename me */
980 * new_avpl_loose_match:
981 * @name: the name of the resulting avpl
982 * @src: avpl to be matched agains an "op" avpl
983 * @op: the "op" avpl that will be matched against the src avpl
984 * @copy_avps: whether the avps in the resulting avpl should be copied
986 * creates an avp list containing any avps in src matching any avps in op
987 * it will eventually create an empty list in none match
989 * Return value: a pointer to the newly created avpl containing the
992 extern AVPL* new_avpl_loose_match(const gchar* name,
995 gboolean copy_avps) {
997 AVPL* newavpl = new_avpl(scs_subscribe(avp_strings, name));
1004 #ifdef _AVP_DEBUGGING
1005 dbg_print(dbg_avpl_op,3,dbg_fp,"new_avpl_loose_match: %X src=%X op=%X name='%s'",newavpl,src,op,name);
1009 cs = src->null.next;
1022 c = ADDRDIFF(co->avp->n, cs->avp->n);
1025 if (co->avp) co = co->next;
1027 if (cs->avp) cs = cs->next;
1029 m = match_avp(cs->avp,co->avp);
1034 if ( ! insert_avp(newavpl,copy) ) {
1038 insert_avp(newavpl,m);
1044 if (cs->avp) cs = cs->next;
1049 #ifdef _AVP_DEBUGGING
1050 dbg_print(dbg_avpl_op,6,dbg_fp,"new_avpl_loose_match: done!");
1056 /* TODO: rename me */
1058 * new_avpl_every_match:
1059 * @name: the name of the resulting avpl
1060 * @src: avpl to be matched agains an "op" avpl
1061 * @op: the "op" avpl that will be matched against the src avpl
1062 * @copy_avps: whether the avps in the resulting avpl should be copied
1064 * creates an avp list containing any avps in src matching every avp in op
1065 * it will not create a list if there is not a match for every attribute in op
1067 * Return value: a pointer to the newly created avpl containing the
1070 extern AVPL* new_avpl_every_match(const gchar* name, AVPL* src, AVPL* op, gboolean copy_avps) {
1079 #ifdef _AVP_DEBUGGING
1080 dbg_print(dbg_avpl_op,3,dbg_fp,"new_avpl_every_match: %X src=%X op=%X name='%s'",newavpl,src,op,name);
1082 if (src->len == 0) return NULL;
1084 newavpl = new_avpl(scs_subscribe(avp_strings, name));
1091 cs = src->null.next;
1103 c = ADDRDIFF(co->avp->n,cs->avp->n);
1106 delete_avpl(newavpl,TRUE);
1114 m = match_avp(cs->avp,co->avp);
1123 if ( ! insert_avp(newavpl,copy) ) {
1127 insert_avp(newavpl,m);
1140 delete_avpl(newavpl,TRUE);
1146 /* TODO: rename me */
1148 * new_avpl_exact_match:
1149 * @name: the name of the resulting avpl
1150 * @src: avpl to be matched agains an "op" avpl
1151 * @op: the "op" avpl that will be matched against the src avpl
1152 * @copy_avps: whether the avps in the resulting avpl should be copied
1154 * creates an avp list containing every avp in src matching every avp in op
1155 * it will not create a list unless every avp in op is matched only once
1156 * to every avp in op.
1158 * Return value: a pointer to the newly created avpl containing the
1161 extern AVPL* new_avpl_exact_match(const gchar* name,AVPL* src, AVPL* op, gboolean copy_avps) {
1162 AVPL* newavpl = new_avpl(name);
1169 #ifdef _AVP_DEBUGGING
1170 dbg_print(dbg_avpl_op,3,dbg_fp,"new_avpl_every_match: %X src=%X op=%X name='%s'",newavpl,src,op,name);
1176 if (src->len == 0) {
1177 delete_avpl(newavpl,FALSE);
1181 cs = src->null.next;
1185 c = ADDRDIFF(co->avp->n,cs->avp->n);
1188 delete_avpl(newavpl,TRUE);
1193 delete_avpl(newavpl,TRUE);
1197 m = match_avp(cs->avp,co->avp);
1205 if ( ! insert_avp(newavpl,copy) ) {
1209 insert_avp(newavpl,m);
1217 delete_avpl(newavpl,TRUE);
1221 delete_avpl(newavpl,TRUE);
1228 /* should never be reached */
1232 extern AVPL* new_avpl_from_match(avpl_match_mode mode, const gchar* name,AVPL* src, AVPL* op, gboolean copy_avps) {
1237 avpl = new_avpl_exact_match(name,src,op,copy_avps);
1240 avpl = new_avpl_loose_match(name,src,op,copy_avps);
1243 avpl = new_avpl_every_match(name,src,op,copy_avps);
1246 avpl = new_avpl_from_avpl(name,src,copy_avps);
1247 merge_avpl(avpl, op, copy_avps);
1255 * delete_avpl_transform:
1256 * @it: a pointer to the avpl transformation object
1258 * Destroys an avpl transformation object and releases all the resources it
1262 extern void delete_avpl_transform(AVPL_Transf* op) {
1265 for (; op ; op = next) {
1271 delete_avpl(op->match,TRUE);
1275 delete_avpl(op->replace,TRUE);
1286 * @src: the source avpl for the transform operation.
1287 * @op: a pointer to the avpl transformation object to apply.
1289 * Applies the "op" transformation to an avpl, matches it and eventually
1290 * replaces or inserts the transformed avps.
1292 * Return value: whether the transformation was performed or not.
1294 extern void avpl_transform(AVPL* src, AVPL_Transf* op) {
1300 #ifdef _AVP_DEBUGGING
1301 dbg_print(dbg_avpl_op,3,dbg_fp,"avpl_transform: src=%X op=%X",src,op);
1304 for ( ; op ; op = op->next) {
1306 avpl = new_avpl_from_match(op->match_mode, src->name,src, op->match, TRUE);
1309 switch (op->replace_mode) {
1310 case AVPL_NO_REPLACE:
1311 delete_avpl(avpl,TRUE);
1314 merge_avpl(src,op->replace,TRUE);
1315 delete_avpl(avpl,TRUE);
1318 cs = src->null.next;
1319 cm = avpl->null.next;
1321 if (cm->avp && cs->avp->n == cm->avp->n && cs->avp->v == cm->avp->v) {
1324 cs->prev->next = cs->next;
1325 cs->next->prev = cs->prev;
1326 g_slice_free(any_avp_type,(any_avp_type*)cs);
1335 merge_avpl(src,op->replace,TRUE);
1336 delete_avpl(avpl,TRUE);
1346 * @name: the name the loal will take.
1348 * Creates an empty list of avp lists.
1350 * Return value: a pointer to the newly created loal.
1352 extern LoAL* new_loal(const gchar* name) {
1353 LoAL* new_loal_p = (LoAL*)g_slice_new(any_avp_type);
1359 #ifdef _AVP_DEBUGGING
1360 dbg_print(dbg_avpl_op,3,dbg_fp,"new_loal_p: %X name=%s",new_loal_p,name);
1363 new_loal_p->name = scs_subscribe(avp_strings,name);
1364 new_loal_p->null.avpl = NULL;
1365 new_loal_p->null.next = &new_loal_p->null;
1366 new_loal_p->null.prev = &new_loal_p->null;
1367 new_loal_p->len = 0;
1373 * @loal: the loal on which to operate.
1374 * @avpl: the avpl to append.
1376 * Appends an avpl to a loal.
1379 extern void loal_append(LoAL* loal, AVPL* avpl) {
1380 LoALnode* node = (LoALnode*)g_slice_new(any_avp_type);
1382 #ifdef _AVP_DEBUGGING
1383 dbg_print(dbg_avpl_op,3,dbg_fp,"new_loal_node: %X",node);
1387 node->next = &loal->null;
1388 node->prev = loal->null.prev;
1390 loal->null.prev->next = node;
1391 loal->null.prev = node;
1397 * extract_first_avpl:
1398 * @loal: the loal on which to operate.
1400 * Extracts the first avpl contained in a loal.
1402 * Return value: a pointer to the extracted avpl.
1405 extern AVPL* extract_first_avpl(LoAL* loal) {
1409 #ifdef _AVP_DEBUGGING
1410 dbg_print(dbg_avpl_op,3,dbg_fp,"extract_first_avpl: from: %s",loal->name);
1413 node = loal->null.next;
1415 loal->null.next->next->prev = &loal->null;
1416 loal->null.next = node->next;
1423 g_slice_free(any_avp_type,(any_avp_type*)node);
1425 #ifdef _AVP_DEBUGGING
1426 dbg_print(dbg_avpl_op,3,dbg_fp,"extract_first_avpl: got %s",avpl->name);
1427 dbg_print(dbg_avpl_op,3,dbg_fp,"delete_loal_node: %X",node);
1435 * extract_first_avpl:
1436 * @loal: the loal on which to operate.
1438 * Extracts the last avpl contained in a loal.
1440 * Return value: a pointer to the extracted avpl.
1443 extern AVPL* extract_last_avpl(LoAL* loal){
1447 node = loal->null.prev;
1449 loal->null.prev->prev->next = &loal->null;
1450 loal->null.prev = node->prev;
1457 g_slice_free(any_avp_type,(any_avp_type*)node);
1458 #ifdef _AVP_DEBUGGING
1459 dbg_print(dbg_avpl_op,3,dbg_fp,"delete_loal_node: %X",node);
1467 * extract_first_avpl:
1468 * @loal: the loal on which to operate.
1469 * @cookie pointer to the pointer variable to contain the state between calls
1471 * At each call will return the following avpl from a loal. The given cookie
1472 * will be used to manatain the state between calls.
1474 * Return value: a pointer to the next avpl.
1477 extern AVPL* get_next_avpl(LoAL* loal,void** cookie) {
1480 #ifdef _AVP_DEBUGGING
1481 dbg_print(dbg_avpl_op,3,dbg_fp,"get_next_avpl: loal=%X node=%X",loal,*cookie);
1485 node = (LoALnode*) *cookie;
1487 node = loal->null.next;
1490 *cookie = node->next;
1497 * @loal: the loal to be deleted.
1498 * @avpls_too: whether avpls contained by the loal should be deleted as well
1499 * @avps_too: whether avps contained by the avpls should be also deleted
1501 * Destroys a loal and eventually desstroys avpls and avps.
1504 extern void delete_loal(LoAL* loal, gboolean avpls_too, gboolean avps_too) {
1507 #ifdef _AVP_DEBUGGING
1508 dbg_print(dbg_avpl_op,3,dbg_fp,"delete_loal: %X",loal);
1511 while(( avpl = extract_last_avpl(loal) )) {
1513 delete_avpl(avpl,avps_too);
1517 scs_unsubscribe(avp_strings,loal->name);
1518 g_slice_free(any_avp_type,(any_avp_type*)loal);
1523 /****************************************************************************
1524 ******************* the following are used in load_loal_from_file
1525 ****************************************************************************/
1529 * Used by loal_from_file to handle errors while loading.
1531 static LoAL* load_loal_error(FILE* fp, LoAL* loal, AVPL* curr, int linenum, const gchar* fmt, ...) {
1537 va_start( list, fmt );
1538 desc = g_strdup_vprintf(fmt, list);
1542 err = g_strdup_printf("Error Loading LoAL from file: in %s at line: %i, %s",loal->name,linenum,desc);
1543 ret = new_loal(err);
1549 if (loal) delete_loal(loal,TRUE,TRUE);
1550 if (curr) delete_avpl(curr,TRUE);
1556 /* the maximum length allowed for a line */
1557 #define MAX_ITEM_LEN 8192
1559 /* this two ugly things are used for tokenizing */
1560 #define AVP_OP_CHAR '=': case '^': case '$': case '~': case '<': case '>': case '?': case '|': case '&' : case '!'
1562 #define AVP_NAME_CHAR 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J':\
1563 case 'K': case 'L': case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T':\
1564 case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z': case 'a': case 'b': case 'c': case 'd':\
1565 case 'e': case 'f': case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':\
1566 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': case 'v': case 'w': case 'x':\
1567 case 'y': case 'z': case '_': case '0': case '1': case '2': case '3': case '4': case '5': case '6':\
1568 case '7': case '8': case '9': case '.'
1573 * @filename: the file containing a loals text representation.
1575 * Given a filename it will attempt to load a loal containing a copy of
1576 * the avpls represented in the file.
1578 * Return value: if successful a pointer to the new populated loal, else NULL.
1581 extern LoAL* loal_from_file(gchar* filename) {
1585 guint32 linenum = 1;
1586 gchar linenum_buf[MAX_ITEM_LEN];
1587 gchar name[MAX_ITEM_LEN];
1588 gchar value[MAX_ITEM_LEN];
1590 LoAL *loal = new_loal(filename);
1594 enum _load_loal_states {
1604 return load_loal_error(fp,loal,curr,linenum,"MATE Will not run as root");
1610 if (( fp = ws_fopen(filename,"r") )) {
1611 while(( c = (gchar) fgetc(fp) )){
1615 report_read_failure(filename,errno);
1616 return load_loal_error(fp,loal,curr,linenum,"Error while reading '%f'",filename);
1625 if ( i >= MAX_ITEM_LEN - 1 ) {
1626 return load_loal_error(fp,loal,curr,linenum,"Maximum item length exceeded");
1641 case ' ': case '\t':
1642 /* ignore whitespace at line start */
1645 /* ignore empty lines */
1653 g_snprintf(linenum_buf,sizeof(linenum_buf),"%s:%u",filename,linenum);
1654 curr = new_avpl(linenum_buf);
1660 return load_loal_error(fp,loal,curr,linenum,"expecting name got: '%c'",c);
1667 c = (gchar) fgetc(fp);
1668 if (c != '\n') ungetc(c,fp);
1680 loal_append(loal,curr);
1684 return load_loal_error(fp,loal,curr,linenum,"expecting name got: '%c'",c);
1689 state = BEFORE_NAME;
1696 avp = new_avp(name,value,op);
1698 if (! insert_avp(curr,avp) ) {
1713 return load_loal_error(fp,loal,curr,linenum,"operator expected found new line");
1715 return load_loal_error(fp,loal,curr,linenum,"name or match operator expected found '%c'",c);
1720 value[i++] = (gchar) fgetc(fp);
1723 state = BEFORE_NAME;
1728 avp = new_avp(name,value,op);
1730 if (! insert_avp(curr,avp) ) {
1735 return load_loal_error(fp,loal,curr,linenum,"';' expected found new line");
1747 report_open_failure(filename,errno,FALSE);
1748 return load_loal_error(NULL,loal,NULL,0,"Cannot Open file '%s'",filename);