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 the debug facility of the thing.
36 ***************************************************************************/
39 * which: a pointer to the current level of debugging for a feature
40 * how: the level over which this message should be printed out
41 * where: the file on which to print (g_message if null)
42 * fmt, ...: what to print
45 void dbg_print(const gint* which, gint how, FILE* where, const gchar* fmt, ... ) {
46 static gchar debug_buffer[DEBUG_BUFFER_SIZE];
49 if ( ! which || *which < how ) return;
51 va_start( list, fmt );
52 g_vsnprintf(debug_buffer,DEBUG_BUFFER_SIZE,fmt,list);
56 g_message(debug_buffer);
58 fputs(debug_buffer,where);
65 /***************************************************************************
67 ***************************************************************************
68 * Strings repeat more often than don't. In order to save memory
69 * we'll keep only one copy of each as key to a hash with a count of
70 * subscribers as value.
71 ***************************************************************************/
75 * @collection: the scs hash
77 * Initializes the scs hash.
80 /* Don't call variables "small" or "huge". They are keywords for the MSVC compiler. Rename them to "mate_small" and "mate_huge"*/
81 struct _scs_collection {
82 GHashTable* hash; /* key: a string value: guint number of subscribers */
84 GMemChunk* mate_small;
85 GMemChunk* mate_medium;
86 GMemChunk* mate_large;
90 extern void destroy_scs_collection(SCS_collection* c) {
91 if ( c->ctrs ) g_mem_chunk_destroy(c->ctrs);
92 if ( c->mate_small ) g_mem_chunk_destroy(c->mate_small);
93 if ( c->mate_medium ) g_mem_chunk_destroy(c->mate_medium);
94 if ( c->mate_large ) g_mem_chunk_destroy(c->mate_large);
95 if ( c->mate_huge ) g_mem_chunk_destroy(c->mate_huge);
97 if (c->hash) g_hash_table_destroy(c->hash);
100 extern SCS_collection* scs_init(void) {
101 SCS_collection* c = g_malloc(sizeof(SCS_collection));
103 c->hash = g_hash_table_new(g_str_hash,g_str_equal);
105 c->ctrs = g_mem_chunk_new("ints_scs_chunk", sizeof(guint),
106 sizeof(guint) * SCS_SMALL_CHUNK_SIZE, G_ALLOC_AND_FREE);
108 c->mate_small = g_mem_chunk_new("small_scs_chunk", SCS_SMALL_SIZE,
109 SCS_SMALL_SIZE * SCS_SMALL_CHUNK_SIZE, G_ALLOC_AND_FREE);
111 c->mate_medium = g_mem_chunk_new("medium_scs_chunk", SCS_MEDIUM_SIZE,
112 SCS_MEDIUM_SIZE * SCS_MEDIUM_CHUNK_SIZE, G_ALLOC_AND_FREE);
114 c->mate_large = g_mem_chunk_new("large_scs_chunk", SCS_LARGE_SIZE,
115 SCS_LARGE_SIZE * SCS_LARGE_CHUNK_SIZE, G_ALLOC_AND_FREE);
117 c->mate_huge = g_mem_chunk_new("huge_scs_chunk", SCS_HUGE_SIZE,
118 SCS_HUGE_SIZE * SCS_HUGE_CHUNK_SIZE, G_ALLOC_AND_FREE);
125 * @collection: the scs hash
128 * Checks if the given string exists already and if so it increases the count of
129 * subsscribers and returns a pointer to the stored string. If not It will copy
130 * the given string store it in the hash and return the pointer to the copy.
131 * Remember, containment is handled internally, take care of your own strings.
133 * Return value: a pointer to the subscribed string.
135 gchar* scs_subscribe(SCS_collection* c, const gchar* s) {
139 GMemChunk* chunk = NULL;
141 g_hash_table_lookup_extended(c->hash,(gconstpointer)s,(gpointer)&orig,(gpointer)&ip);
146 ip = g_mem_chunk_alloc(c->ctrs);
151 if (len <= SCS_SMALL_SIZE) {
152 chunk = c->mate_small;
153 len = SCS_SMALL_SIZE;
154 } else if (len <= SCS_MEDIUM_SIZE) {
155 chunk = c->mate_medium;
156 len = SCS_MEDIUM_SIZE;
157 } else if (len <= SCS_LARGE_SIZE) {
158 chunk = c->mate_large;
159 len = SCS_LARGE_SIZE;
160 } else if (len < SCS_HUGE_SIZE) {
161 chunk = c->mate_huge;
164 chunk = c->mate_huge;
166 g_warning("mate SCS: string truncated to huge size");
169 orig = g_mem_chunk_alloc(chunk);
172 g_hash_table_insert(c->hash,orig,ip);
180 * @collection: the scs hash
183 * decreases the count of subscribers, if zero frees the internal copy of
186 void scs_unsubscribe(SCS_collection* c, gchar* s) {
190 GMemChunk* chunk = NULL;
192 g_hash_table_lookup_extended(c->hash,(gconstpointer)s,(gpointer)&orig,(gpointer)&ip);
196 g_hash_table_remove(c->hash,orig);
200 if (len < SCS_SMALL_SIZE) {
201 chunk = c->mate_small;
202 } else if (len < SCS_MEDIUM_SIZE) {
203 chunk = c->mate_medium;
204 } else if (len < SCS_LARGE_SIZE) {
205 chunk = c->mate_large;
207 chunk = c->mate_huge;
210 g_mem_chunk_free(chunk,orig);
211 g_mem_chunk_free(c->ctrs,ip);
217 g_warning("unsusbcribe: not subscribed");
222 * scs_subscribe_printf:
223 * @fmt: a format string ...
225 * Formats the input and subscribes it.
227 * Return value: the stored copy of the formated string.
230 gchar* scs_subscribe_printf(SCS_collection* c, gchar* fmt, ...) {
232 static gchar buf[SCS_HUGE_SIZE];
234 va_start( list, fmt );
235 g_vsnprintf(buf, SCS_HUGE_SIZE-1 ,fmt, list);
238 return scs_subscribe(c,buf);
241 /***************************************************************************
243 ***************************************************************************
244 * The Thing operates mainly on avps, avpls and loals
245 * - attribute value pairs (two strings: the name and the value and an opeartor)
246 * - avp lists a somehow sorted list of avps
247 * - loal (list of avp lists) an arbitrarily sorted list of avpls
250 ***************************************************************************/
253 typedef union _any_avp_type {
262 static GMemChunk* avp_chunk = NULL;
263 static SCS_collection* avp_strings = NULL;
265 #ifdef _AVP_DEBUGGING
266 static FILE* dbg_fp = NULL;
268 static int dbg_level = 0;
269 static int* dbg = &dbg_level;
271 static int dbg_avp_level = 0;
272 static int* dbg_avp = &dbg_avp_level;
274 static int dbg_avp_op_level = 0;
275 static int* dbg_avp_op = &dbg_avp_op_level;
277 static int dbg_avpl_level = 0;
278 static int* dbg_avpl = &dbg_avpl_level;
280 static int dbg_avpl_op_level = 0;
281 static int* dbg_avpl_op = &dbg_avpl_op_level;
285 * @fp: the file in which to send debugging output.
286 * @general: a pointer to the level of debugging of facility "general"
287 * @avp: a pointer to the level of debugging of facility "avp"
288 * @avp_op: a pointer to the level of debugging of facility "avp_op"
289 * @avpl: a pointer to the level of debugging of facility "avpl"
290 * @avpl_op: a pointer to the level of debugging of facility "avpl_op"
292 * If enabled set's up the debug facilities for the avp library.
295 extern void setup_avp_debug(FILE* fp, int* general, int* avp, int* avp_op, int* avpl, int* avpl_op) {
301 dbg_avpl_op = avpl_op;
304 #endif /* _AVP_DEBUGGING */
308 * @chunk_size: the initial chunk's size.
310 * (Re)Initializes the avp library.
313 extern void avp_init(void) {
315 if (avp_strings) destroy_scs_collection(avp_strings);
316 avp_strings = scs_init();
318 if ( avp_chunk ) g_mem_chunk_destroy(avp_chunk);
319 avp_chunk = g_mem_chunk_new("avp_chunk", sizeof(any_avp_type),
320 AVP_CHUNK_SIZE, G_ALLOC_AND_FREE);
326 * new_avp_from_finfo:
327 * @name: the name the avp will have.
328 * @finfo: the field_info from which to fetch the data.
330 * Creates an avp from a field_info record.
332 * Return value: a pointer to the newly created avp.
335 extern AVP* new_avp_from_finfo(const gchar* name, field_info* finfo) {
336 AVP* new = g_mem_chunk_alloc(avp_chunk);
339 new->n = scs_subscribe(avp_strings, name);
341 if (finfo->value.ftype->val_to_string_repr) {
342 value = scs_subscribe(avp_strings, fvalue_to_string_repr(&finfo->value,FTREPR_DISPLAY,NULL));
343 #ifdef _AVP_DEBUGGING
344 dbg_print (dbg_avp,2,dbg_fp,"new_avp_from_finfo: from string: %s",value);
347 #ifdef _AVP_DEBUGGING
348 dbg_print (dbg_avp,2,dbg_fp,"new_avp_from_finfo: a proto: %s",finfo->hfinfo->abbrev);
350 value = scs_subscribe(avp_strings, "");
357 #ifdef _AVP_DEBUGGING
358 dbg_print (dbg_avp,1,dbg_fp,"new_avp_from_finfo: %X %s%c%s;",(guint32) new,new->n,new->o,new->v);
367 * @name: the name the avp will have.
368 * @value: the value the avp will have.
369 * @o: the operator of this avp.
371 * Creates an avp given every parameter.
373 * Return value: a pointer to the newly created avp.
376 extern AVP* new_avp(const gchar* name, const gchar* value, gchar o) {
377 AVP* new = g_mem_chunk_alloc(avp_chunk);
379 new->n = scs_subscribe(avp_strings, name);
380 new->v = scs_subscribe(avp_strings, value);
383 #ifdef _AVP_DEBUGGING
384 dbg_print(dbg_avp,1,dbg_fp,"new_avp: %X %s%c%s;",(guint32) new,new->n,new->o,new->v);
392 * @avp: the avp to delete.
394 * Destroys an avp and releases the resources it uses.
397 extern void delete_avp(AVP* avp) {
398 #ifdef _AVP_DEBUGGING
399 dbg_print(dbg_avp,1,dbg_fp,"delete_avp: %X %s%c%s;",(guint32) avp,avp->n,avp->o,avp->v);
402 scs_unsubscribe(avp_strings, avp->n);
403 scs_unsubscribe(avp_strings, avp->v);
404 g_mem_chunk_free(avp_chunk,avp);
410 * @from: the avp to be copied.
412 * Creates an avp whose name op and value are copyes of the given one.
414 * Return value: a pointer to the newly created avp.
417 extern AVP* avp_copy(AVP* from) {
418 AVP* new = g_mem_chunk_alloc(avp_chunk);
420 new->n = scs_subscribe(avp_strings, from->n);
421 new->v = scs_subscribe(avp_strings, from->v);
424 #ifdef _AVP_DEBUGGING
425 dbg_print(dbg_avp,1,dbg_fp,"copy_avp: %X %s%c%s;",(guint32) new,new->n,new->o,new->v);
433 * @name: the name the avpl will have.
435 * Creates an empty avpl.
437 * Return value: a pointer to the newly created avpl.
440 extern AVPL* new_avpl(const gchar* name) {
441 AVPL* new_avpl = g_mem_chunk_alloc(avp_chunk);
443 #ifdef _AVP_DEBUGGING
444 dbg_print(dbg_avpl_op,7,dbg_fp,"new_avpl: %X name=%s",new_avpl,name);
447 new_avpl->name = name ? scs_subscribe(avp_strings, name) : scs_subscribe(avp_strings, "");
449 new_avpl->null.avp = NULL;
450 new_avpl->null.next = &new_avpl->null;
451 new_avpl->null.prev = &new_avpl->null;
457 extern void rename_avpl(AVPL* avpl, gchar* name) {
458 scs_unsubscribe(avp_strings,avpl->name);
459 avpl->name = scs_subscribe(avp_strings,name);
464 * @avpl: the avpl in which to insert.
465 * @avp: the avp to be inserted.
467 * Inserts the given AVP into the given AVPL if an identical one isn't yet there.
469 * Return value: whether it was inserted or not.
471 * BEWARE: Check the return value, you might need to delete the avp if
472 * it is not inserted.
474 extern gboolean insert_avp(AVPL* avpl, AVP* avp) {
475 AVPN* new = g_mem_chunk_alloc(avp_chunk);
480 #ifdef _AVP_DEBUGGING
481 dbg_print(dbg_avpl_op,7,dbg_fp,"new_avpn: %X",new);
482 dbg_print(dbg_avpl_op,4,dbg_fp,"insert_avp: %X %X %s%c%s;",avpl,avp,avp->n,avp->o,avp->v);
485 /* get to the insertion point */
486 for(c=avpl->null.next; c->avp; c = c->next) {
488 if ( avp->n == c->avp->n ) {
490 if (avp->v > c->avp->v) {
494 if (avp->v == c->avp->v) {
495 if (avp->o == AVP_OP_EQUAL) {
496 #ifdef _AVP_DEBUGGING
497 dbg_print(dbg_avpl_op,7,dbg_fp,"delete_avpn: %X",new);
499 g_mem_chunk_free(avp_chunk,new);
505 if (avp->n > c->avp->n) {
510 #ifdef _AVP_DEBUGGING
511 dbg_print(dbg_avpl,5,dbg_fp,"insert_avp: inserting %X in %X before %X;",avp,avpl,c);
521 #ifdef _AVP_DEBUGGING
522 dbg_print(dbg_avpl,4,dbg_fp,"avpl: %X new len: %i",avpl,avpl->len);
530 * @avpl: the avpl from which to try to get the avp.
531 * @name: the name of the avp we are looking for.
532 * @cookie: variable in which to store the state between calls.
534 * Gets pointer to the next avp whose name is given; uses cookie to store its
535 * state between calls.
537 * Return value: a pointer to the next matching avp if there's one, else NULL.
540 extern AVP* get_avp_by_name(AVPL* avpl, gchar* name, void** cookie) {
542 AVPN* start = (AVPN*) *cookie;
544 #ifdef _AVP_DEBUGGING
545 dbg_print(dbg_avpl_op,7,dbg_fp,"get_avp_by_name: entering: %X %s %X",avpl,name,*cookie);
548 name = scs_subscribe(avp_strings, name);
550 if (!start) start = avpl->null.next;
552 for ( curr = start; curr->avp; curr = curr->next ) {
553 if ( curr->avp->n == name ) {
560 #ifdef _AVP_DEBUGGING
561 dbg_print(dbg_avpl_op,5,dbg_fp,"get_avp_by_name: got avp: %X",curr);
564 scs_unsubscribe(avp_strings, name);
570 * extract_avp_by_name:
571 * @avpl: the avpl from which to try to extract the avp.
572 * @name: the name of the avp we are looking for.
574 * Extracts from the avpl the next avp whose name is given;
576 * Return value: a pointer to extracted avp if there's one, else NULL.
579 extern AVP* extract_avp_by_name(AVPL* avpl, gchar* name) {
583 #ifdef _AVP_DEBUGGING
584 dbg_print(dbg_avpl_op,7,dbg_fp,"extract_avp_by_name: entering: %X %s",avpl,name);
587 name = scs_subscribe(avp_strings, name);
589 for ( curr = avpl->null.next; curr->avp; curr = curr->next ) {
590 if ( curr->avp->n == name ) {
595 scs_unsubscribe(avp_strings, name);
597 if( ! curr->avp ) return NULL;
599 curr->next->prev = curr->prev;
600 curr->prev->next = curr->next;
604 g_mem_chunk_free(avp_chunk,curr);
608 #ifdef _AVP_DEBUGGING
609 dbg_print(dbg_avpl,4,dbg_fp,"avpl: %X new len: %i",avpl,avpl->len);
612 #ifdef _AVP_DEBUGGING
613 dbg_print(dbg_avpl_op,5,dbg_fp,"extract_avp_by_name: got avp: %X",avp);
622 * @avpl: the avpl from which to try to extract the avp.
624 * Extracts the fisrt avp from the avpl.
626 * Return value: a pointer to extracted avp if there's one, else NULL.
629 extern AVP* extract_first_avp(AVPL* avpl) {
633 #ifdef _AVP_DEBUGGING
634 dbg_print(dbg_avpl_op,7,dbg_fp,"extract_first_avp: %X",avpl);
637 node = avpl->null.next;
639 avpl->null.next->prev = &avpl->null;
640 avpl->null.next = node->next;
645 g_mem_chunk_free(avp_chunk,node);
647 #ifdef _AVP_DEBUGGING
648 dbg_print(dbg_avpl,4,dbg_fp,"avpl: %X new len: %i",avpl,avpl->len);
652 #ifdef _AVP_DEBUGGING
653 dbg_print(dbg_avpl_op,5,dbg_fp,"extract_first_avp: got avp: %X",avp);
663 * @avpl: the avpl from which to try to extract the avp.
665 * Extracts the last avp from the avpl.
667 * Return value: a pointer to extracted avp if there's one, else NULL.
670 extern AVP* extract_last_avp(AVPL* avpl) {
674 node = avpl->null.prev;
676 avpl->null.prev->next = &avpl->null;
677 avpl->null.prev = node->prev;
682 g_mem_chunk_free(avp_chunk,node);
684 #ifdef _AVP_DEBUGGING
685 dbg_print(dbg_avpl,4,dbg_fp,"avpl: %X new len: %i",avpl,avpl->len);
689 #ifdef _AVP_DEBUGGING
690 dbg_print(dbg_avpl_op,5,dbg_fp,"extract_last_avp: got avp: %X",avp);
700 * @avpl: the avpl from which to try to extract the avp.
701 * @avps_too: whether or not it should delete the avps as well.
703 * Destroys an avpl and releases the resources it uses. If told to do
704 * so releases the avps as well.
707 extern void delete_avpl(AVPL* avpl, gboolean avps_too) {
709 #ifdef _AVP_DEBUGGING
710 dbg_print(dbg_avpl,3,dbg_fp,"delete_avpl: %X",avpl);
713 while(( avp = extract_last_avp(avpl))) {
719 scs_unsubscribe(avp_strings,avpl->name);
720 g_mem_chunk_free(avp_chunk,avpl);
727 * @avpl: the avpl from which to try to get the avps.
728 * @cookie: variable in which to store the state between calls.
730 * Iterates on an avpl to get its avps.
732 * Return value: a pointer to the next avp if there's one, else NULL.
735 extern AVP* get_next_avp(AVPL* avpl, void** cookie) {
738 #ifdef _AVP_DEBUGGING
739 dbg_print(dbg_avpl_op,5,dbg_fp,"get_next_avp: avpl: %X avpn: %X",avpl,*cookie);
743 node = (AVPN*) *cookie;
745 node = avpl->null.next;
748 *cookie = node->next;
750 #ifdef _AVP_DEBUGGING
751 dbg_print(dbg_avpl_op,5,dbg_fp,"extract_last_avp: got avp: %X",node->avp);
759 * @avpl: the avpl to represent.
761 * Creates a newly allocated string containing a representation of an avpl.
763 * Return value: a pointer to the newly allocated string.
766 gchar* avpl_to_str(AVPL* avpl) {
768 GString* s = g_string_new("");
772 for(c=avpl->null.next; c->avp; c = c->next) {
773 avp_s = avp_to_str(c->avp);
774 g_string_append_printf(s," %s;",avp_s);
779 g_string_free(s,FALSE);
781 /* g_strchug(r); ? */
785 extern gchar* avpl_to_dotstr(AVPL* avpl) {
787 GString* s = g_string_new("");
791 for(c=avpl->null.next; c->avp; c = c->next) {
792 avp_s = avp_to_str(c->avp);
793 g_string_append_printf(s," .%s;",avp_s);
798 g_string_free(s,FALSE);
800 /* g_strchug(r); ? */
806 * @dst: the avpl in which to merge the avps.
807 * @src: the avpl from which to get the avps.
808 * @copy: whether avps should be copied instead of referenced.
810 * Adds the avps of src that are not existent in dst into dst.
812 * Return value: a pointer to the newly allocated string.
815 extern void merge_avpl(AVPL* dst, AVPL* src, gboolean copy_avps) {
821 #ifdef _AVP_DEBUGGING
822 dbg_print(dbg_avpl_op,3,dbg_fp,"merge_avpl: %X %X",dst,src);
831 c = (guint) cd->avp->n - (guint) cs->avp->n;
837 if (cd->avp) cd = cd->next;
840 copy = avp_copy(cs->avp);
841 if ( ! insert_avp(dst,copy) ) {
845 insert_avp(dst,cs->avp);
850 if ( ! cd->avp || ! (cd->avp->v == cs->avp->v) ) {
852 copy = avp_copy(cs->avp);
853 if ( ! insert_avp(dst,copy) ) {
857 insert_avp(dst,cs->avp);
861 if (cd->avp) cd = cd->next;
865 #ifdef _AVP_DEBUGGING
866 dbg_print(dbg_avpl_op,8,dbg_fp,"merge_avpl: done");
875 * @name: the name of the new avpl.
876 * @avpl: the avpl from which to get the avps.
877 * @copy_avps: whether avps should be copied instead of referenced.
879 * Creates a new avpl containing the same avps as the given avpl
880 * It will either reference or copie the avps.
882 * Return value: a pointer to the newly allocated string.
885 extern AVPL* new_avpl_from_avpl(const gchar* name, AVPL* avpl, gboolean copy_avps) {
886 AVPL* newavpl = new_avpl(name);
891 #ifdef _AVP_DEBUGGING
892 dbg_print(dbg_avpl_op,3,dbg_fp,"new_avpl_from_avpl: %X from=%X name='%s'",newavpl,avpl,name);
895 while(( avp = get_next_avp(avpl,&cookie) )) {
897 copy = avp_copy(avp);
898 if ( ! insert_avp(newavpl,copy) ) {
902 insert_avp(newavpl,avp);
906 #ifdef _AVP_DEBUGGING
907 dbg_print(dbg_avpl_op,8,dbg_fp,"new_avpl_from_avpl: done");
915 * @src: an src to be compared agains an "op" avp
916 * @op: the "op" avp that will be matched against the src avp
918 * Checks whether or not two avp's match.
920 * Return value: a pointer to the src avp if there's a match.
923 extern AVP* match_avp(AVP* src, AVP* op) {
931 gboolean lower = FALSE;
933 #ifdef _AVP_DEBUGGING
934 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);
937 if ( src->n != op->n ) {
945 return src->v == op->v ? src : NULL;
946 case AVP_OP_NOTEQUAL:
947 return !( src->v == op->v) ? src : NULL;
949 return strncmp(src->v,op->v,strlen(op->v)) == 0 ? src : NULL;
951 splited = g_strsplit(op->v,"|",0);
953 for (i=0;splited[i];i++) {
954 if(g_str_equal(splited[i],src->v)) {
967 fs = (float) strtod(src->v, NULL);
968 fo = (float) strtod(src->v, NULL);
971 if (fs<fo) return src;
974 if (fs>fo) return src;
978 /* does this work? */
985 p = src->v + ( ls - lo );
986 return g_str_equal(p,op->v) ? src : NULL;
989 /* case AVP_OP_TRANSF: */
990 /* return do_transform(src,op); */
991 case AVP_OP_CONTAINS:
995 /* will never get here */
1001 /* TODO: rename me */
1003 * new_avpl_loose_match:
1004 * @name: the name of the resulting avpl
1005 * @src: avpl to be matched agains an "op" avpl
1006 * @op: the "op" avpl that will be matched against the src avpl
1007 * @copy_avps: whether the avps in the resulting avpl should be copied
1009 * creates an avp list containing any avps in src matching any avps in op
1010 * it will eventually create an empty list in none match
1012 * Return value: a pointer to the newly created avpl containing the
1015 extern AVPL* new_avpl_loose_match(const gchar* name,
1018 gboolean copy_avps) {
1020 AVPL* newavpl = new_avpl(scs_subscribe(avp_strings, name));
1027 #ifdef _AVP_DEBUGGING
1028 dbg_print(dbg_avpl_op,3,dbg_fp,"new_avpl_loose_match: %X src=%X op=%X name='%s'",newavpl,src,op,name);
1032 cs = src->null.next;
1045 c = (guint) co->avp->n - (guint) cs->avp->n;
1048 if (co->avp) co = co->next;
1050 if (cs->avp) cs = cs->next;
1052 m = match_avp(cs->avp,co->avp);
1057 if ( ! insert_avp(newavpl,copy) ) {
1061 insert_avp(newavpl,m);
1067 if (cs->avp) cs = cs->next;
1072 #ifdef _AVP_DEBUGGING
1073 dbg_print(dbg_avpl_op,6,dbg_fp,"new_avpl_loose_match: done!");
1079 /* TODO: rename me */
1081 * new_avpl_every_match:
1082 * @name: the name of the resulting avpl
1083 * @src: avpl to be matched agains an "op" avpl
1084 * @op: the "op" avpl that will be matched against the src avpl
1085 * @copy_avps: whether the avps in the resulting avpl should be copied
1087 * creates an avp list containing any avps in src matching every avp in op
1088 * it will not create a list if there is not a match for every attribute in op
1090 * Return value: a pointer to the newly created avpl containing the
1093 extern AVPL* new_avpl_every_match(const gchar* name, AVPL* src, AVPL* op, gboolean copy_avps) {
1102 #ifdef _AVP_DEBUGGING
1103 dbg_print(dbg_avpl_op,3,dbg_fp,"new_avpl_every_match: %X src=%X op=%X name='%s'",newavpl,src,op,name);
1105 if (src->len == 0) return NULL;
1107 newavpl = new_avpl(scs_subscribe(avp_strings, name));
1114 cs = src->null.next;
1126 c = (guint) co->avp->n - (guint) cs->avp->n;
1129 delete_avpl(newavpl,TRUE);
1137 m = match_avp(cs->avp,co->avp);
1146 if ( ! insert_avp(newavpl,copy) ) {
1150 insert_avp(newavpl,m);
1163 delete_avpl(newavpl,TRUE);
1169 /* TODO: rename me */
1171 * new_avpl_exact_match:
1172 * @name: the name of the resulting avpl
1173 * @src: avpl to be matched agains an "op" avpl
1174 * @op: the "op" avpl that will be matched against the src avpl
1175 * @copy_avps: whether the avps in the resulting avpl should be copied
1177 * creates an avp list containing every avp in src matching every avp in op
1178 * it will not create a list unless every avp in op is matched only once
1179 * to every avp in op.
1181 * Return value: a pointer to the newly created avpl containing the
1184 extern AVPL* new_avpl_exact_match(const gchar* name,AVPL* src, AVPL* op, gboolean copy_avps) {
1185 AVPL* newavpl = new_avpl(name);
1192 #ifdef _AVP_DEBUGGING
1193 dbg_print(dbg_avpl_op,3,dbg_fp,"new_avpl_every_match: %X src=%X op=%X name='%s'",newavpl,src,op,name);
1199 if (src->len == 0) {
1200 delete_avpl(newavpl,FALSE);
1204 cs = src->null.next;
1208 c = (guint) co->avp->n - (guint) cs->avp->n;
1211 delete_avpl(newavpl,TRUE);
1216 delete_avpl(newavpl,TRUE);
1220 m = match_avp(cs->avp,co->avp);
1228 if ( ! insert_avp(newavpl,copy) ) {
1232 insert_avp(newavpl,m);
1240 delete_avpl(newavpl,TRUE);
1244 delete_avpl(newavpl,TRUE);
1251 /* should never be reached */
1255 extern AVPL* new_avpl_from_match(avpl_match_mode mode, const gchar* name,AVPL* src, AVPL* op, gboolean copy_avps) {
1260 avpl = new_avpl_exact_match(name,src,op,copy_avps);
1263 avpl = new_avpl_loose_match(name,src,op,copy_avps);
1266 avpl = new_avpl_every_match(name,src,op,copy_avps);
1269 avpl = new_avpl_from_avpl(name,src,copy_avps);
1270 merge_avpl(avpl, op, copy_avps);
1278 * delete_avpl_transform:
1279 * @it: a pointer to the avpl transformation object
1281 * Destroys an avpl transformation object and releases all the resources it
1285 extern void delete_avpl_transform(AVPL_Transf* op) {
1288 for (; op ; op = next) {
1294 delete_avpl(op->match,TRUE);
1298 delete_avpl(op->replace,TRUE);
1309 * @src: the source avpl for the transform operation.
1310 * @op: a pointer to the avpl transformation object to apply.
1312 * Applies the "op" transformation to an avpl, matches it and eventually
1313 * replaces or inserts the transformed avps.
1315 * Return value: whether the transformation was performed or not.
1317 extern void avpl_transform(AVPL* src, AVPL_Transf* op) {
1323 #ifdef _AVP_DEBUGGING
1324 dbg_print(dbg_avpl_op,3,dbg_fp,"avpl_transform: src=%X op=%X",src,op);
1327 for ( ; op ; op = op->next) {
1329 avpl = new_avpl_from_match(op->match_mode, src->name,src, op->match, TRUE);
1332 switch (op->replace_mode) {
1333 case AVPL_NO_REPLACE:
1334 delete_avpl(avpl,TRUE);
1337 merge_avpl(src,op->replace,TRUE);
1338 delete_avpl(avpl,TRUE);
1341 cs = src->null.next;
1342 cm = avpl->null.next;
1344 if (cm->avp && cs->avp->n == cm->avp->n && cs->avp->v == cm->avp->v) {
1347 cs->prev->next = cs->next;
1348 cs->next->prev = cs->prev;
1349 g_mem_chunk_free(avp_chunk,cs);
1358 merge_avpl(src,op->replace,TRUE);
1359 delete_avpl(avpl,TRUE);
1369 * @name: the name the loal will take.
1371 * Creates an empty list of avp lists.
1373 * Return value: a pointer to the newly created loal.
1375 extern LoAL* new_loal(const gchar* name) {
1376 LoAL* new_loal = g_mem_chunk_alloc(avp_chunk);
1382 #ifdef _AVP_DEBUGGING
1383 dbg_print(dbg_avpl_op,3,dbg_fp,"new_loal: %X name=%s",new_loal,name);
1386 new_loal->name = scs_subscribe(avp_strings,name);
1387 new_loal->null.avpl = NULL;
1388 new_loal->null.next = &new_loal->null;
1389 new_loal->null.prev = &new_loal->null;
1396 * @loal: the loal on which to operate.
1397 * @avpl: the avpl to append.
1399 * Appends an avpl to a loal.
1402 extern void loal_append(LoAL* loal, AVPL* avpl) {
1403 LoALnode* node = g_mem_chunk_alloc(avp_chunk);
1405 #ifdef _AVP_DEBUGGING
1406 dbg_print(dbg_avpl_op,3,dbg_fp,"new_loal_node: %X",node);
1410 node->next = &loal->null;
1411 node->prev = loal->null.prev;
1413 loal->null.prev->next = node;
1414 loal->null.prev = node;
1420 * extract_first_avpl:
1421 * @loal: the loal on which to operate.
1423 * Extracts the first avpl contained in a loal.
1425 * Return value: a pointer to the extracted avpl.
1428 extern AVPL* extract_first_avpl(LoAL* loal) {
1432 #ifdef _AVP_DEBUGGING
1433 dbg_print(dbg_avpl_op,3,dbg_fp,"extract_first_avpl: from: %s",loal->name);
1436 node = loal->null.next;
1438 loal->null.next->next->prev = &loal->null;
1439 loal->null.next = node->next;
1446 g_mem_chunk_free(avp_chunk,node);
1448 #ifdef _AVP_DEBUGGING
1449 dbg_print(dbg_avpl_op,3,dbg_fp,"extract_first_avpl: got %s",avpl->name);
1450 dbg_print(dbg_avpl_op,3,dbg_fp,"delete_loal_node: %X",node);
1458 * extract_first_avpl:
1459 * @loal: the loal on which to operate.
1461 * Extracts the last avpl contained in a loal.
1463 * Return value: a pointer to the extracted avpl.
1466 extern AVPL* extract_last_avpl(LoAL* loal){
1470 node = loal->null.prev;
1472 loal->null.prev->prev->next = &loal->null;
1473 loal->null.prev = node->prev;
1480 g_mem_chunk_free(avp_chunk,node);
1481 #ifdef _AVP_DEBUGGING
1482 dbg_print(dbg_avpl_op,3,dbg_fp,"delete_loal_node: %X",node);
1490 * extract_first_avpl:
1491 * @loal: the loal on which to operate.
1492 * @cookie pointer to the pointer variable to contain the state between calls
1494 * At each call will return the following avpl from a loal. The given cookie
1495 * will be used to manatain the state between calls.
1497 * Return value: a pointer to the next avpl.
1500 extern AVPL* get_next_avpl(LoAL* loal,void** cookie) {
1503 #ifdef _AVP_DEBUGGING
1504 dbg_print(dbg_avpl_op,3,dbg_fp,"get_next_avpl: loal=%X node=%X",loal,*cookie);
1508 node = (LoALnode*) *cookie;
1510 node = loal->null.next;
1513 *cookie = node->next;
1520 * @loal: the loal to be deleted.
1521 * @avpls_too: whether avpls contained by the loal should be deleted as well
1522 * @avps_too: whether avps contained by the avpls should be also deleted
1524 * Destroys a loal and eventually desstroys avpls and avps.
1527 extern void delete_loal(LoAL* loal, gboolean avpls_too, gboolean avps_too) {
1530 #ifdef _AVP_DEBUGGING
1531 dbg_print(dbg_avpl_op,3,dbg_fp,"delete_loal: %X",loal);
1534 while(( avpl = extract_last_avpl(loal) )) {
1536 delete_avpl(avpl,avps_too);
1540 scs_unsubscribe(avp_strings,loal->name);
1541 g_mem_chunk_free(avp_chunk,loal);
1546 /****************************************************************************
1547 ******************* the following are used in load_loal_from_file
1548 ****************************************************************************/
1552 * Used by loal_from_file to handle errors while loading.
1554 static LoAL* load_loal_error(FILE* fp, LoAL* loal, AVPL* curr, int linenum, const gchar* fmt, ...) {
1560 va_start( list, fmt );
1561 desc = g_strdup_vprintf(fmt, list);
1565 err = g_strdup_printf("Error Loading LoAL from file: in %s at line: %i, %s",loal->name,linenum,desc);
1566 ret = new_loal(err);
1572 if (loal) delete_loal(loal,TRUE,TRUE);
1573 if (curr) delete_avpl(curr,TRUE);
1579 /* the maximum length allowed for a line */
1580 #define MAX_ITEM_LEN 8192
1582 /* this two ugly things are used for tokenizing */
1583 #define AVP_OP_CHAR '=': case '^': case '$': case '~': case '<': case '>': case '?': case '|': case '&' : case '!'
1585 #define AVP_NAME_CHAR 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J':\
1586 case 'K': case 'L': case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T':\
1587 case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z': case 'a': case 'b': case 'c': case 'd':\
1588 case 'e': case 'f': case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':\
1589 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': case 'v': case 'w': case 'x':\
1590 case 'y': case 'z': case '_': case '0': case '1': case '2': case '3': case '4': case '5': case '6':\
1591 case '7': case '8': case '9': case '.'
1596 * @filename: the file containing a loals text representation.
1598 * Given a filename it will attempt to load a loal containing a copy of
1599 * the avpls represented in the file.
1601 * Return value: if successful a pointer to the new populated loal, else NULL.
1604 extern LoAL* loal_from_file(gchar* filename) {
1608 guint32 linenum = 1;
1609 gchar linenum_buf[MAX_ITEM_LEN];
1610 gchar name[MAX_ITEM_LEN];
1611 gchar value[MAX_ITEM_LEN];
1613 LoAL *loal = new_loal(filename);
1617 enum _load_loal_states {
1627 return load_loal_error(fp,loal,curr,linenum,"MATE Will not run as root");
1633 if (( fp = ws_fopen(filename,"r") )) {
1634 while(( c = (gchar) fgetc(fp) )){
1638 report_read_failure(filename,errno);
1639 return load_loal_error(fp,loal,curr,linenum,"Error while reading '%f'",filename);
1648 if ( i >= MAX_ITEM_LEN - 1 ) {
1649 return load_loal_error(fp,loal,curr,linenum,"Maximum item length exceeded");
1665 case ' ': case '\t':
1666 /* ignore whitespace at line start */
1669 /* ignore empty lines */
1677 g_snprintf(linenum_buf,sizeof(linenum_buf),"%s:%u",filename,linenum);
1678 curr = new_avpl(linenum_buf);
1684 return load_loal_error(fp,loal,curr,linenum,"expecting name got: '%c'",c);
1692 if (c != '\n') ungetc(c,fp);
1704 loal_append(loal,curr);
1708 return load_loal_error(fp,loal,curr,linenum,"expecting name got: '%c'",c);
1713 state = BEFORE_NAME;
1720 avp = new_avp(name,value,op);
1722 if (! insert_avp(curr,avp) ) {
1737 return load_loal_error(fp,loal,curr,linenum,"operator expected found new line");
1739 return load_loal_error(fp,loal,curr,linenum,"name or match operator expected found '%c'",c);
1744 value[i++] = fgetc(fp);
1747 state = BEFORE_NAME;
1752 avp = new_avp(name,value,op);
1754 if (! insert_avp(curr,avp) ) {
1759 return load_loal_error(fp,loal,curr,linenum,"';' expected found new line");
1771 report_open_failure(filename,errno,FALSE);
1772 return load_loal_error(NULL,loal,NULL,0,"Cannot Open file '%s'",filename);