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(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 ***************************************************************************/
85 * @collection: the scs hash
87 * Initializes the scs hash.
90 /* Don't call variables "small" or "huge". They are keywords for the MSVC compiler. Rename them to "mate_small" and "mate_huge"*/
91 struct _scs_collection {
92 GHashTable* hash; /* key: a string value: guint number of subscribers */
94 GMemChunk* mate_small;
95 GMemChunk* mate_medium;
96 GMemChunk* mate_large;
100 extern void destroy_scs_collection(SCS_collection* c) {
101 if ( c->ctrs ) g_mem_chunk_destroy(c->ctrs);
102 if ( c->mate_small ) g_mem_chunk_destroy(c->mate_small);
103 if ( c->mate_medium ) g_mem_chunk_destroy(c->mate_medium);
104 if ( c->mate_large ) g_mem_chunk_destroy(c->mate_large);
105 if ( c->mate_huge ) g_mem_chunk_destroy(c->mate_huge);
107 if (c->hash) g_hash_table_destroy(c->hash);
110 extern SCS_collection* scs_init(void) {
111 SCS_collection* c = g_malloc(sizeof(SCS_collection));
113 c->hash = g_hash_table_new(g_str_hash,g_str_equal);
115 c->ctrs = g_mem_chunk_new("ints_scs_chunk", sizeof(guint),
116 sizeof(guint) * SCS_SMALL_CHUNK_SIZE, G_ALLOC_AND_FREE);
118 c->mate_small = g_mem_chunk_new("small_scs_chunk", SCS_SMALL_SIZE,
119 SCS_SMALL_SIZE * SCS_SMALL_CHUNK_SIZE, G_ALLOC_AND_FREE);
121 c->mate_medium = g_mem_chunk_new("medium_scs_chunk", SCS_MEDIUM_SIZE,
122 SCS_MEDIUM_SIZE * SCS_MEDIUM_CHUNK_SIZE, G_ALLOC_AND_FREE);
124 c->mate_large = g_mem_chunk_new("large_scs_chunk", SCS_LARGE_SIZE,
125 SCS_LARGE_SIZE * SCS_LARGE_CHUNK_SIZE, G_ALLOC_AND_FREE);
127 c->mate_huge = g_mem_chunk_new("huge_scs_chunk", SCS_HUGE_SIZE,
128 SCS_HUGE_SIZE * SCS_HUGE_CHUNK_SIZE, G_ALLOC_AND_FREE);
135 * @collection: the scs hash
138 * Checks if the given string exists already and if so it increases the count of
139 * subsscribers and returns a pointer to the stored string. If not It will copy
140 * the given string store it in the hash and return the pointer to the copy.
141 * Remember, containment is handled internally, take care of your own strings.
143 * Return value: a pointer to the subscribed string.
145 gchar* scs_subscribe(SCS_collection* c, const gchar* s) {
149 GMemChunk* chunk = NULL;
151 g_hash_table_lookup_extended(c->hash,(gconstpointer)s,(gpointer)&orig,(gpointer)&ip);
156 ip = g_mem_chunk_alloc(c->ctrs);
161 if (len <= SCS_SMALL_SIZE) {
162 chunk = c->mate_small;
163 len = SCS_SMALL_SIZE;
164 } else if (len <= SCS_MEDIUM_SIZE) {
165 chunk = c->mate_medium;
166 len = SCS_MEDIUM_SIZE;
167 } else if (len <= SCS_LARGE_SIZE) {
168 chunk = c->mate_large;
169 len = SCS_LARGE_SIZE;
170 } else if (len < SCS_HUGE_SIZE) {
171 chunk = c->mate_huge;
174 chunk = c->mate_huge;
176 g_warning("mate SCS: string truncated to huge size");
179 orig = g_mem_chunk_alloc(chunk);
182 g_hash_table_insert(c->hash,orig,ip);
190 * @collection: the scs hash
193 * decreases the count of subscribers, if zero frees the internal copy of
196 void scs_unsubscribe(SCS_collection* c, gchar* s) {
200 GMemChunk* chunk = NULL;
202 g_hash_table_lookup_extended(c->hash,(gconstpointer)s,(gpointer)&orig,(gpointer)&ip);
206 g_hash_table_remove(c->hash,orig);
210 if (len < SCS_SMALL_SIZE) {
211 chunk = c->mate_small;
212 } else if (len < SCS_MEDIUM_SIZE) {
213 chunk = c->mate_medium;
214 } else if (len < SCS_LARGE_SIZE) {
215 chunk = c->mate_large;
217 chunk = c->mate_huge;
220 g_mem_chunk_free(chunk,orig);
221 g_mem_chunk_free(c->ctrs,ip);
227 g_warning("unsusbcribe: not subscribed");
232 * scs_subscribe_printf:
233 * @fmt: a format string ...
235 * Formats the input and subscribes it.
237 * Return value: the stored copy of the formated string.
240 gchar* scs_subscribe_printf(SCS_collection* c, gchar* fmt, ...) {
242 static gchar buf[SCS_HUGE_SIZE];
244 va_start( list, fmt );
245 g_vsnprintf(buf, SCS_HUGE_SIZE-1 ,fmt, list);
248 return scs_subscribe(c,buf);
251 /***************************************************************************
253 ***************************************************************************
254 * The Thing operates mainly on avps, avpls and loals
255 * - attribute value pairs (two strings: the name and the value and an opeartor)
256 * - avp lists a somehow sorted list of avps
257 * - loal (list of avp lists) an arbitrarily sorted list of avpls
260 ***************************************************************************/
263 typedef union _any_avp_type {
272 static GMemChunk* avp_chunk = NULL;
273 static SCS_collection* avp_strings = NULL;
275 #ifdef _AVP_DEBUGGING
276 static FILE* dbg_fp = NULL;
278 static int dbg_level = 0;
279 static int* dbg = &dbg_level;
281 static int dbg_avp_level = 0;
282 static int* dbg_avp = &dbg_avp_level;
284 static int dbg_avp_op_level = 0;
285 static int* dbg_avp_op = &dbg_avp_op_level;
287 static int dbg_avpl_level = 0;
288 static int* dbg_avpl = &dbg_avpl_level;
290 static int dbg_avpl_op_level = 0;
291 static int* dbg_avpl_op = &dbg_avpl_op_level;
295 * @fp: the file in which to send debugging output.
296 * @general: a pointer to the level of debugging of facility "general"
297 * @avp: a pointer to the level of debugging of facility "avp"
298 * @avp_op: a pointer to the level of debugging of facility "avp_op"
299 * @avpl: a pointer to the level of debugging of facility "avpl"
300 * @avpl_op: a pointer to the level of debugging of facility "avpl_op"
302 * If enabled set's up the debug facilities for the avp library.
305 extern void setup_avp_debug(FILE* fp, int* general, int* avp, int* avp_op, int* avpl, int* avpl_op) {
311 dbg_avpl_op = avpl_op;
314 #endif /* _AVP_DEBUGGING */
318 * @chunk_size: the initial chunk's size.
320 * (Re)Initializes the avp library.
323 extern void avp_init(void) {
325 if (avp_strings) destroy_scs_collection(avp_strings);
326 avp_strings = scs_init();
328 if ( avp_chunk ) g_mem_chunk_destroy(avp_chunk);
329 avp_chunk = g_mem_chunk_new("avp_chunk", sizeof(any_avp_type),
330 AVP_CHUNK_SIZE, G_ALLOC_AND_FREE);
336 * new_avp_from_finfo:
337 * @name: the name the avp will have.
338 * @finfo: the field_info from which to fetch the data.
340 * Creates an avp from a field_info record.
342 * Return value: a pointer to the newly created avp.
345 extern AVP* new_avp_from_finfo(const gchar* name, field_info* finfo) {
346 AVP* new = g_mem_chunk_alloc(avp_chunk);
349 new->n = scs_subscribe(avp_strings, name);
351 if (finfo->value.ftype->val_to_string_repr) {
352 value = scs_subscribe(avp_strings, fvalue_to_string_repr(&finfo->value,FTREPR_DISPLAY,NULL));
353 #ifdef _AVP_DEBUGGING
354 dbg_print (dbg_avp,2,dbg_fp,"new_avp_from_finfo: from string: %s",value);
357 #ifdef _AVP_DEBUGGING
358 dbg_print (dbg_avp,2,dbg_fp,"new_avp_from_finfo: a proto: %s",finfo->hfinfo->abbrev);
360 value = scs_subscribe(avp_strings, "");
367 #ifdef _AVP_DEBUGGING
368 dbg_print (dbg_avp,1,dbg_fp,"new_avp_from_finfo: %X %s%c%s;",(guint32) new,new->n,new->o,new->v);
377 * @name: the name the avp will have.
378 * @value: the value the avp will have.
379 * @o: the operator of this avp.
381 * Creates an avp given every parameter.
383 * Return value: a pointer to the newly created avp.
386 extern AVP* new_avp(const gchar* name, const gchar* value, gchar o) {
387 AVP* new = g_mem_chunk_alloc(avp_chunk);
389 new->n = scs_subscribe(avp_strings, name);
390 new->v = scs_subscribe(avp_strings, value);
393 #ifdef _AVP_DEBUGGING
394 dbg_print(dbg_avp,1,dbg_fp,"new_avp: %X %s%c%s;",(guint32) new,new->n,new->o,new->v);
402 * @avp: the avp to delete.
404 * Destroys an avp and releases the resources it uses.
407 extern void delete_avp(AVP* avp) {
408 #ifdef _AVP_DEBUGGING
409 dbg_print(dbg_avp,1,dbg_fp,"delete_avp: %X %s%c%s;",(guint32) avp,avp->n,avp->o,avp->v);
412 scs_unsubscribe(avp_strings, avp->n);
413 scs_unsubscribe(avp_strings, avp->v);
414 g_mem_chunk_free(avp_chunk,avp);
420 * @from: the avp to be copied.
422 * Creates an avp whose name op and value are copyes of the given one.
424 * Return value: a pointer to the newly created avp.
427 extern AVP* avp_copy(AVP* from) {
428 AVP* new = g_mem_chunk_alloc(avp_chunk);
430 new->n = scs_subscribe(avp_strings, from->n);
431 new->v = scs_subscribe(avp_strings, from->v);
434 #ifdef _AVP_DEBUGGING
435 dbg_print(dbg_avp,1,dbg_fp,"copy_avp: %X %s%c%s;",(guint32) new,new->n,new->o,new->v);
443 * @name: the name the avpl will have.
445 * Creates an empty avpl.
447 * Return value: a pointer to the newly created avpl.
450 extern AVPL* new_avpl(const gchar* name) {
451 AVPL* new_avpl = g_mem_chunk_alloc(avp_chunk);
453 #ifdef _AVP_DEBUGGING
454 dbg_print(dbg_avpl_op,7,dbg_fp,"new_avpl: %X name=%s",new_avpl,name);
457 new_avpl->name = name ? scs_subscribe(avp_strings, name) : scs_subscribe(avp_strings, "");
459 new_avpl->null.avp = NULL;
460 new_avpl->null.next = &new_avpl->null;
461 new_avpl->null.prev = &new_avpl->null;
467 extern void rename_avpl(AVPL* avpl, gchar* name) {
468 scs_unsubscribe(avp_strings,avpl->name);
469 avpl->name = scs_subscribe(avp_strings,name);
474 * @avpl: the avpl in which to insert.
475 * @avp: the avp to be inserted.
477 * Inserts the given AVP into the given AVPL if an identical one isn't yet there.
479 * Return value: whether it was inserted or not.
481 * BEWARE: Check the return value, you might need to delete the avp if
482 * it is not inserted.
484 extern gboolean insert_avp(AVPL* avpl, AVP* avp) {
485 AVPN* new = g_mem_chunk_alloc(avp_chunk);
490 #ifdef _AVP_DEBUGGING
491 dbg_print(dbg_avpl_op,7,dbg_fp,"new_avpn: %X",new);
492 dbg_print(dbg_avpl_op,4,dbg_fp,"insert_avp: %X %X %s%c%s;",avpl,avp,avp->n,avp->o,avp->v);
495 /* get to the insertion point */
496 for(c=avpl->null.next; c->avp; c = c->next) {
498 if ( avp->n == c->avp->n ) {
500 if (avp->v > c->avp->v) {
504 if (avp->v == c->avp->v) {
505 if (avp->o == AVP_OP_EQUAL) {
506 #ifdef _AVP_DEBUGGING
507 dbg_print(dbg_avpl_op,7,dbg_fp,"delete_avpn: %X",new);
509 g_mem_chunk_free(avp_chunk,new);
515 if (avp->n > c->avp->n) {
520 #ifdef _AVP_DEBUGGING
521 dbg_print(dbg_avpl,5,dbg_fp,"insert_avp: inserting %X in %X before %X;",avp,avpl,c);
531 #ifdef _AVP_DEBUGGING
532 dbg_print(dbg_avpl,4,dbg_fp,"avpl: %X new len: %i",avpl,avpl->len);
540 * @avpl: the avpl from which to try to get the avp.
541 * @name: the name of the avp we are looking for.
542 * @cookie: variable in which to store the state between calls.
544 * Gets pointer to the next avp whose name is given; uses cookie to store its
545 * state between calls.
547 * Return value: a pointer to the next matching avp if there's one, else NULL.
550 extern AVP* get_avp_by_name(AVPL* avpl, gchar* name, void** cookie) {
552 AVPN* start = (AVPN*) *cookie;
554 #ifdef _AVP_DEBUGGING
555 dbg_print(dbg_avpl_op,7,dbg_fp,"get_avp_by_name: entering: %X %s %X",avpl,name,*cookie);
558 name = scs_subscribe(avp_strings, name);
560 if (!start) start = avpl->null.next;
562 for ( curr = start; curr->avp; curr = curr->next ) {
563 if ( curr->avp->n == name ) {
570 #ifdef _AVP_DEBUGGING
571 dbg_print(dbg_avpl_op,5,dbg_fp,"get_avp_by_name: got avp: %X",curr);
574 scs_unsubscribe(avp_strings, name);
580 * extract_avp_by_name:
581 * @avpl: the avpl from which to try to extract the avp.
582 * @name: the name of the avp we are looking for.
584 * Extracts from the avpl the next avp whose name is given;
586 * Return value: a pointer to extracted avp if there's one, else NULL.
589 extern AVP* extract_avp_by_name(AVPL* avpl, gchar* name) {
593 #ifdef _AVP_DEBUGGING
594 dbg_print(dbg_avpl_op,7,dbg_fp,"extract_avp_by_name: entering: %X %s",avpl,name);
597 name = scs_subscribe(avp_strings, name);
599 for ( curr = avpl->null.next; curr->avp; curr = curr->next ) {
600 if ( curr->avp->n == name ) {
605 scs_unsubscribe(avp_strings, name);
607 if( ! curr->avp ) return NULL;
609 curr->next->prev = curr->prev;
610 curr->prev->next = curr->next;
614 g_mem_chunk_free(avp_chunk,curr);
618 #ifdef _AVP_DEBUGGING
619 dbg_print(dbg_avpl,4,dbg_fp,"avpl: %X new len: %i",avpl,avpl->len);
622 #ifdef _AVP_DEBUGGING
623 dbg_print(dbg_avpl_op,5,dbg_fp,"extract_avp_by_name: got avp: %X",avp);
632 * @avpl: the avpl from which to try to extract the avp.
634 * Extracts the fisrt avp from the avpl.
636 * Return value: a pointer to extracted avp if there's one, else NULL.
639 extern AVP* extract_first_avp(AVPL* avpl) {
643 #ifdef _AVP_DEBUGGING
644 dbg_print(dbg_avpl_op,7,dbg_fp,"extract_first_avp: %X",avpl);
647 node = avpl->null.next;
649 avpl->null.next->prev = &avpl->null;
650 avpl->null.next = node->next;
655 g_mem_chunk_free(avp_chunk,node);
657 #ifdef _AVP_DEBUGGING
658 dbg_print(dbg_avpl,4,dbg_fp,"avpl: %X new len: %i",avpl,avpl->len);
662 #ifdef _AVP_DEBUGGING
663 dbg_print(dbg_avpl_op,5,dbg_fp,"extract_first_avp: got avp: %X",avp);
673 * @avpl: the avpl from which to try to extract the avp.
675 * Extracts the last avp from the avpl.
677 * Return value: a pointer to extracted avp if there's one, else NULL.
680 extern AVP* extract_last_avp(AVPL* avpl) {
684 node = avpl->null.prev;
686 avpl->null.prev->next = &avpl->null;
687 avpl->null.prev = node->prev;
692 g_mem_chunk_free(avp_chunk,node);
694 #ifdef _AVP_DEBUGGING
695 dbg_print(dbg_avpl,4,dbg_fp,"avpl: %X new len: %i",avpl,avpl->len);
699 #ifdef _AVP_DEBUGGING
700 dbg_print(dbg_avpl_op,5,dbg_fp,"extract_last_avp: got avp: %X",avp);
710 * @avpl: the avpl from which to try to extract the avp.
711 * @avps_too: whether or not it should delete the avps as well.
713 * Destroys an avpl and releases the resources it uses. If told to do
714 * so releases the avps as well.
717 extern void delete_avpl(AVPL* avpl, gboolean avps_too) {
719 #ifdef _AVP_DEBUGGING
720 dbg_print(dbg_avpl,3,dbg_fp,"delete_avpl: %X",avpl);
723 while(( avp = extract_last_avp(avpl))) {
729 scs_unsubscribe(avp_strings,avpl->name);
730 g_mem_chunk_free(avp_chunk,avpl);
737 * @avpl: the avpl from which to try to get the avps.
738 * @cookie: variable in which to store the state between calls.
740 * Iterates on an avpl to get its avps.
742 * Return value: a pointer to the next avp if there's one, else NULL.
745 extern AVP* get_next_avp(AVPL* avpl, void** cookie) {
748 #ifdef _AVP_DEBUGGING
749 dbg_print(dbg_avpl_op,5,dbg_fp,"get_next_avp: avpl: %X avpn: %X",avpl,*cookie);
753 node = (AVPN*) *cookie;
755 node = avpl->null.next;
758 *cookie = node->next;
760 #ifdef _AVP_DEBUGGING
761 dbg_print(dbg_avpl_op,5,dbg_fp,"extract_last_avp: got avp: %X",node->avp);
769 * @avpl: the avpl to represent.
771 * Creates a newly allocated string containing a representation of an avpl.
773 * Return value: a pointer to the newly allocated string.
776 gchar* avpl_to_str(AVPL* avpl) {
778 GString* s = g_string_new("");
782 for(c=avpl->null.next; c->avp; c = c->next) {
783 avp_s = avp_to_str(c->avp);
784 g_string_append_printf(s," %s;",avp_s);
789 g_string_free(s,FALSE);
791 /* g_strchug(r); ? */
795 extern gchar* avpl_to_dotstr(AVPL* avpl) {
797 GString* s = g_string_new("");
801 for(c=avpl->null.next; c->avp; c = c->next) {
802 avp_s = avp_to_str(c->avp);
803 g_string_append_printf(s," .%s;",avp_s);
808 g_string_free(s,FALSE);
810 /* g_strchug(r); ? */
816 * @dst: the avpl in which to merge the avps.
817 * @src: the avpl from which to get the avps.
818 * @copy: whether avps should be copied instead of referenced.
820 * Adds the avps of src that are not existent in dst into dst.
822 * Return value: a pointer to the newly allocated string.
825 extern void merge_avpl(AVPL* dst, AVPL* src, gboolean copy_avps) {
831 #ifdef _AVP_DEBUGGING
832 dbg_print(dbg_avpl_op,3,dbg_fp,"merge_avpl: %X %X",dst,src);
841 c = ADDRDIFF(cd->avp->n,cs->avp->n);
847 if (cd->avp) cd = cd->next;
850 copy = avp_copy(cs->avp);
851 if ( ! insert_avp(dst,copy) ) {
855 insert_avp(dst,cs->avp);
860 if ( ! cd->avp || ! (cd->avp->v == cs->avp->v) ) {
862 copy = avp_copy(cs->avp);
863 if ( ! insert_avp(dst,copy) ) {
867 insert_avp(dst,cs->avp);
871 if (cd->avp) cd = cd->next;
875 #ifdef _AVP_DEBUGGING
876 dbg_print(dbg_avpl_op,8,dbg_fp,"merge_avpl: done");
885 * @name: the name of the new avpl.
886 * @avpl: the avpl from which to get the avps.
887 * @copy_avps: whether avps should be copied instead of referenced.
889 * Creates a new avpl containing the same avps as the given avpl
890 * It will either reference or copie the avps.
892 * Return value: a pointer to the newly allocated string.
895 extern AVPL* new_avpl_from_avpl(const gchar* name, AVPL* avpl, gboolean copy_avps) {
896 AVPL* newavpl = new_avpl(name);
901 #ifdef _AVP_DEBUGGING
902 dbg_print(dbg_avpl_op,3,dbg_fp,"new_avpl_from_avpl: %X from=%X name='%s'",newavpl,avpl,name);
905 while(( avp = get_next_avp(avpl,&cookie) )) {
907 copy = avp_copy(avp);
908 if ( ! insert_avp(newavpl,copy) ) {
912 insert_avp(newavpl,avp);
916 #ifdef _AVP_DEBUGGING
917 dbg_print(dbg_avpl_op,8,dbg_fp,"new_avpl_from_avpl: done");
925 * @src: an src to be compared agains an "op" avp
926 * @op: the "op" avp that will be matched against the src avp
928 * Checks whether or not two avp's match.
930 * Return value: a pointer to the src avp if there's a match.
933 extern AVP* match_avp(AVP* src, AVP* op) {
941 gboolean lower = FALSE;
943 #ifdef _AVP_DEBUGGING
944 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);
947 if ( src->n != op->n ) {
955 return src->v == op->v ? src : NULL;
956 case AVP_OP_NOTEQUAL:
957 return !( src->v == op->v) ? src : NULL;
959 return strncmp(src->v,op->v,strlen(op->v)) == 0 ? src : NULL;
961 splited = g_strsplit(op->v,"|",0);
963 for (i=0;splited[i];i++) {
964 if(g_str_equal(splited[i],src->v)) {
977 fs = (float) strtod(src->v, NULL);
978 fo = (float) strtod(src->v, NULL);
981 if (fs<fo) return src;
984 if (fs>fo) return src;
988 /* does this work? */
995 p = src->v + ( ls - lo );
996 return g_str_equal(p,op->v) ? src : NULL;
999 /* case AVP_OP_TRANSF: */
1000 /* return do_transform(src,op); */
1001 case AVP_OP_CONTAINS:
1005 /* will never get here */
1011 /* TODO: rename me */
1013 * new_avpl_loose_match:
1014 * @name: the name of the resulting avpl
1015 * @src: avpl to be matched agains an "op" avpl
1016 * @op: the "op" avpl that will be matched against the src avpl
1017 * @copy_avps: whether the avps in the resulting avpl should be copied
1019 * creates an avp list containing any avps in src matching any avps in op
1020 * it will eventually create an empty list in none match
1022 * Return value: a pointer to the newly created avpl containing the
1025 extern AVPL* new_avpl_loose_match(const gchar* name,
1028 gboolean copy_avps) {
1030 AVPL* newavpl = new_avpl(scs_subscribe(avp_strings, name));
1037 #ifdef _AVP_DEBUGGING
1038 dbg_print(dbg_avpl_op,3,dbg_fp,"new_avpl_loose_match: %X src=%X op=%X name='%s'",newavpl,src,op,name);
1042 cs = src->null.next;
1055 c = (guint) co->avp->n - (guint) cs->avp->n;
1058 if (co->avp) co = co->next;
1060 if (cs->avp) cs = cs->next;
1062 m = match_avp(cs->avp,co->avp);
1067 if ( ! insert_avp(newavpl,copy) ) {
1071 insert_avp(newavpl,m);
1077 if (cs->avp) cs = cs->next;
1082 #ifdef _AVP_DEBUGGING
1083 dbg_print(dbg_avpl_op,6,dbg_fp,"new_avpl_loose_match: done!");
1089 /* TODO: rename me */
1091 * new_avpl_every_match:
1092 * @name: the name of the resulting avpl
1093 * @src: avpl to be matched agains an "op" avpl
1094 * @op: the "op" avpl that will be matched against the src avpl
1095 * @copy_avps: whether the avps in the resulting avpl should be copied
1097 * creates an avp list containing any avps in src matching every avp in op
1098 * it will not create a list if there is not a match for every attribute in op
1100 * Return value: a pointer to the newly created avpl containing the
1103 extern AVPL* new_avpl_every_match(const gchar* name, AVPL* src, AVPL* op, gboolean copy_avps) {
1112 #ifdef _AVP_DEBUGGING
1113 dbg_print(dbg_avpl_op,3,dbg_fp,"new_avpl_every_match: %X src=%X op=%X name='%s'",newavpl,src,op,name);
1115 if (src->len == 0) return NULL;
1117 newavpl = new_avpl(scs_subscribe(avp_strings, name));
1124 cs = src->null.next;
1136 c = ADDRDIFF(co->avp->n,cs->avp->n);
1139 delete_avpl(newavpl,TRUE);
1147 m = match_avp(cs->avp,co->avp);
1156 if ( ! insert_avp(newavpl,copy) ) {
1160 insert_avp(newavpl,m);
1173 delete_avpl(newavpl,TRUE);
1179 /* TODO: rename me */
1181 * new_avpl_exact_match:
1182 * @name: the name of the resulting avpl
1183 * @src: avpl to be matched agains an "op" avpl
1184 * @op: the "op" avpl that will be matched against the src avpl
1185 * @copy_avps: whether the avps in the resulting avpl should be copied
1187 * creates an avp list containing every avp in src matching every avp in op
1188 * it will not create a list unless every avp in op is matched only once
1189 * to every avp in op.
1191 * Return value: a pointer to the newly created avpl containing the
1194 extern AVPL* new_avpl_exact_match(const gchar* name,AVPL* src, AVPL* op, gboolean copy_avps) {
1195 AVPL* newavpl = new_avpl(name);
1202 #ifdef _AVP_DEBUGGING
1203 dbg_print(dbg_avpl_op,3,dbg_fp,"new_avpl_every_match: %X src=%X op=%X name='%s'",newavpl,src,op,name);
1209 if (src->len == 0) {
1210 delete_avpl(newavpl,FALSE);
1214 cs = src->null.next;
1218 c = ADDRDIFF(co->avp->n,cs->avp->n);
1221 delete_avpl(newavpl,TRUE);
1226 delete_avpl(newavpl,TRUE);
1230 m = match_avp(cs->avp,co->avp);
1238 if ( ! insert_avp(newavpl,copy) ) {
1242 insert_avp(newavpl,m);
1250 delete_avpl(newavpl,TRUE);
1254 delete_avpl(newavpl,TRUE);
1261 /* should never be reached */
1265 extern AVPL* new_avpl_from_match(avpl_match_mode mode, const gchar* name,AVPL* src, AVPL* op, gboolean copy_avps) {
1270 avpl = new_avpl_exact_match(name,src,op,copy_avps);
1273 avpl = new_avpl_loose_match(name,src,op,copy_avps);
1276 avpl = new_avpl_every_match(name,src,op,copy_avps);
1279 avpl = new_avpl_from_avpl(name,src,copy_avps);
1280 merge_avpl(avpl, op, copy_avps);
1288 * delete_avpl_transform:
1289 * @it: a pointer to the avpl transformation object
1291 * Destroys an avpl transformation object and releases all the resources it
1295 extern void delete_avpl_transform(AVPL_Transf* op) {
1298 for (; op ; op = next) {
1304 delete_avpl(op->match,TRUE);
1308 delete_avpl(op->replace,TRUE);
1319 * @src: the source avpl for the transform operation.
1320 * @op: a pointer to the avpl transformation object to apply.
1322 * Applies the "op" transformation to an avpl, matches it and eventually
1323 * replaces or inserts the transformed avps.
1325 * Return value: whether the transformation was performed or not.
1327 extern void avpl_transform(AVPL* src, AVPL_Transf* op) {
1333 #ifdef _AVP_DEBUGGING
1334 dbg_print(dbg_avpl_op,3,dbg_fp,"avpl_transform: src=%X op=%X",src,op);
1337 for ( ; op ; op = op->next) {
1339 avpl = new_avpl_from_match(op->match_mode, src->name,src, op->match, TRUE);
1342 switch (op->replace_mode) {
1343 case AVPL_NO_REPLACE:
1344 delete_avpl(avpl,TRUE);
1347 merge_avpl(src,op->replace,TRUE);
1348 delete_avpl(avpl,TRUE);
1351 cs = src->null.next;
1352 cm = avpl->null.next;
1354 if (cm->avp && cs->avp->n == cm->avp->n && cs->avp->v == cm->avp->v) {
1357 cs->prev->next = cs->next;
1358 cs->next->prev = cs->prev;
1359 g_mem_chunk_free(avp_chunk,cs);
1368 merge_avpl(src,op->replace,TRUE);
1369 delete_avpl(avpl,TRUE);
1379 * @name: the name the loal will take.
1381 * Creates an empty list of avp lists.
1383 * Return value: a pointer to the newly created loal.
1385 extern LoAL* new_loal(const gchar* name) {
1386 LoAL* new_loal = g_mem_chunk_alloc(avp_chunk);
1392 #ifdef _AVP_DEBUGGING
1393 dbg_print(dbg_avpl_op,3,dbg_fp,"new_loal: %X name=%s",new_loal,name);
1396 new_loal->name = scs_subscribe(avp_strings,name);
1397 new_loal->null.avpl = NULL;
1398 new_loal->null.next = &new_loal->null;
1399 new_loal->null.prev = &new_loal->null;
1406 * @loal: the loal on which to operate.
1407 * @avpl: the avpl to append.
1409 * Appends an avpl to a loal.
1412 extern void loal_append(LoAL* loal, AVPL* avpl) {
1413 LoALnode* node = g_mem_chunk_alloc(avp_chunk);
1415 #ifdef _AVP_DEBUGGING
1416 dbg_print(dbg_avpl_op,3,dbg_fp,"new_loal_node: %X",node);
1420 node->next = &loal->null;
1421 node->prev = loal->null.prev;
1423 loal->null.prev->next = node;
1424 loal->null.prev = node;
1430 * extract_first_avpl:
1431 * @loal: the loal on which to operate.
1433 * Extracts the first avpl contained in a loal.
1435 * Return value: a pointer to the extracted avpl.
1438 extern AVPL* extract_first_avpl(LoAL* loal) {
1442 #ifdef _AVP_DEBUGGING
1443 dbg_print(dbg_avpl_op,3,dbg_fp,"extract_first_avpl: from: %s",loal->name);
1446 node = loal->null.next;
1448 loal->null.next->next->prev = &loal->null;
1449 loal->null.next = node->next;
1456 g_mem_chunk_free(avp_chunk,node);
1458 #ifdef _AVP_DEBUGGING
1459 dbg_print(dbg_avpl_op,3,dbg_fp,"extract_first_avpl: got %s",avpl->name);
1460 dbg_print(dbg_avpl_op,3,dbg_fp,"delete_loal_node: %X",node);
1468 * extract_first_avpl:
1469 * @loal: the loal on which to operate.
1471 * Extracts the last avpl contained in a loal.
1473 * Return value: a pointer to the extracted avpl.
1476 extern AVPL* extract_last_avpl(LoAL* loal){
1480 node = loal->null.prev;
1482 loal->null.prev->prev->next = &loal->null;
1483 loal->null.prev = node->prev;
1490 g_mem_chunk_free(avp_chunk,node);
1491 #ifdef _AVP_DEBUGGING
1492 dbg_print(dbg_avpl_op,3,dbg_fp,"delete_loal_node: %X",node);
1500 * extract_first_avpl:
1501 * @loal: the loal on which to operate.
1502 * @cookie pointer to the pointer variable to contain the state between calls
1504 * At each call will return the following avpl from a loal. The given cookie
1505 * will be used to manatain the state between calls.
1507 * Return value: a pointer to the next avpl.
1510 extern AVPL* get_next_avpl(LoAL* loal,void** cookie) {
1513 #ifdef _AVP_DEBUGGING
1514 dbg_print(dbg_avpl_op,3,dbg_fp,"get_next_avpl: loal=%X node=%X",loal,*cookie);
1518 node = (LoALnode*) *cookie;
1520 node = loal->null.next;
1523 *cookie = node->next;
1530 * @loal: the loal to be deleted.
1531 * @avpls_too: whether avpls contained by the loal should be deleted as well
1532 * @avps_too: whether avps contained by the avpls should be also deleted
1534 * Destroys a loal and eventually desstroys avpls and avps.
1537 extern void delete_loal(LoAL* loal, gboolean avpls_too, gboolean avps_too) {
1540 #ifdef _AVP_DEBUGGING
1541 dbg_print(dbg_avpl_op,3,dbg_fp,"delete_loal: %X",loal);
1544 while(( avpl = extract_last_avpl(loal) )) {
1546 delete_avpl(avpl,avps_too);
1550 scs_unsubscribe(avp_strings,loal->name);
1551 g_mem_chunk_free(avp_chunk,loal);
1556 /****************************************************************************
1557 ******************* the following are used in load_loal_from_file
1558 ****************************************************************************/
1562 * Used by loal_from_file to handle errors while loading.
1564 static LoAL* load_loal_error(FILE* fp, LoAL* loal, AVPL* curr, int linenum, const gchar* fmt, ...) {
1570 va_start( list, fmt );
1571 desc = g_strdup_vprintf(fmt, list);
1575 err = g_strdup_printf("Error Loading LoAL from file: in %s at line: %i, %s",loal->name,linenum,desc);
1576 ret = new_loal(err);
1582 if (loal) delete_loal(loal,TRUE,TRUE);
1583 if (curr) delete_avpl(curr,TRUE);
1589 /* the maximum length allowed for a line */
1590 #define MAX_ITEM_LEN 8192
1592 /* this two ugly things are used for tokenizing */
1593 #define AVP_OP_CHAR '=': case '^': case '$': case '~': case '<': case '>': case '?': case '|': case '&' : case '!'
1595 #define AVP_NAME_CHAR 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J':\
1596 case 'K': case 'L': case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T':\
1597 case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z': case 'a': case 'b': case 'c': case 'd':\
1598 case 'e': case 'f': case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':\
1599 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': case 'v': case 'w': case 'x':\
1600 case 'y': case 'z': case '_': case '0': case '1': case '2': case '3': case '4': case '5': case '6':\
1601 case '7': case '8': case '9': case '.'
1606 * @filename: the file containing a loals text representation.
1608 * Given a filename it will attempt to load a loal containing a copy of
1609 * the avpls represented in the file.
1611 * Return value: if successful a pointer to the new populated loal, else NULL.
1614 extern LoAL* loal_from_file(gchar* filename) {
1618 guint32 linenum = 1;
1619 gchar linenum_buf[MAX_ITEM_LEN];
1620 gchar name[MAX_ITEM_LEN];
1621 gchar value[MAX_ITEM_LEN];
1623 LoAL *loal = new_loal(filename);
1627 enum _load_loal_states {
1637 return load_loal_error(fp,loal,curr,linenum,"MATE Will not run as root");
1643 if (( fp = ws_fopen(filename,"r") )) {
1644 while(( c = (gchar) fgetc(fp) )){
1648 report_read_failure(filename,errno);
1649 return load_loal_error(fp,loal,curr,linenum,"Error while reading '%f'",filename);
1658 if ( i >= MAX_ITEM_LEN - 1 ) {
1659 return load_loal_error(fp,loal,curr,linenum,"Maximum item length exceeded");
1675 case ' ': case '\t':
1676 /* ignore whitespace at line start */
1679 /* ignore empty lines */
1687 g_snprintf(linenum_buf,sizeof(linenum_buf),"%s:%u",filename,linenum);
1688 curr = new_avpl(linenum_buf);
1694 return load_loal_error(fp,loal,curr,linenum,"expecting name got: '%c'",c);
1702 if (c != '\n') ungetc(c,fp);
1714 loal_append(loal,curr);
1718 return load_loal_error(fp,loal,curr,linenum,"expecting name got: '%c'",c);
1723 state = BEFORE_NAME;
1730 avp = new_avp(name,value,op);
1732 if (! insert_avp(curr,avp) ) {
1747 return load_loal_error(fp,loal,curr,linenum,"operator expected found new line");
1749 return load_loal_error(fp,loal,curr,linenum,"name or match operator expected found '%c'",c);
1754 value[i++] = fgetc(fp);
1757 state = BEFORE_NAME;
1762 avp = new_avp(name,value,op);
1764 if (! insert_avp(curr,avp) ) {
1769 return load_loal_error(fp,loal,curr,linenum,"';' expected found new line");
1781 report_open_failure(filename,errno,FALSE);
1782 return load_loal_error(NULL,loal,NULL,0,"Cannot Open file '%s'",filename);