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@gmail.com>
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 <wiretap/file_util.h>
31 #include <epan/ws_strsplit.h>
33 /***************************************************************************
35 ***************************************************************************
36 * This is the debug facility of the thing.
37 ***************************************************************************/
40 * which: a pointer to the current level of debugging for a feature
41 * how: the level over which this message should be printed out
42 * where: the file on which to print (g_message if null)
43 * fmt, ...: what to print
46 void dbg_print(const gint* which, gint how, FILE* where, const gchar* fmt, ... ) {
47 static gchar debug_buffer[DEBUG_BUFFER_SIZE];
50 if ( ! which || *which < how ) return;
52 va_start( list, fmt );
53 g_vsnprintf(debug_buffer,DEBUG_BUFFER_SIZE,fmt,list);
57 g_message(debug_buffer);
59 fputs(debug_buffer,where);
66 /***************************************************************************
68 ***************************************************************************
69 * Strings repeat more often than don't. In order to save memory
70 * we'll keep only one copy of each as key to a hash with a count of
71 * subscribers as value.
72 ***************************************************************************/
76 * @collection: the scs hash
78 * Initializes the scs hash.
81 /* Don't call variables "small" or "huge". They are keywords for the MSVC compiler. Rename them to "mate_small" and "mate_huge"*/
82 struct _scs_collection {
83 GHashTable* hash; /* key: a string value: guint number of subscribers */
85 GMemChunk* mate_small;
86 GMemChunk* mate_medium;
87 GMemChunk* mate_large;
91 extern void destroy_scs_collection(SCS_collection* c) {
92 if ( c->ctrs ) g_mem_chunk_destroy(c->ctrs);
93 if ( c->mate_small ) g_mem_chunk_destroy(c->mate_small);
94 if ( c->mate_medium ) g_mem_chunk_destroy(c->mate_medium);
95 if ( c->mate_large ) g_mem_chunk_destroy(c->mate_large);
96 if ( c->mate_huge ) g_mem_chunk_destroy(c->mate_huge);
98 if (c->hash) g_hash_table_destroy(c->hash);
101 extern SCS_collection* scs_init(void) {
102 SCS_collection* c = g_malloc(sizeof(SCS_collection));
104 c->hash = g_hash_table_new(g_str_hash,g_str_equal);
106 c->ctrs = g_mem_chunk_new("ints_scs_chunk", sizeof(guint),
107 sizeof(guint) * SCS_SMALL_CHUNK_SIZE, G_ALLOC_AND_FREE);
109 c->mate_small = g_mem_chunk_new("small_scs_chunk", SCS_SMALL_SIZE,
110 SCS_SMALL_SIZE * SCS_SMALL_CHUNK_SIZE, G_ALLOC_AND_FREE);
112 c->mate_medium = g_mem_chunk_new("medium_scs_chunk", SCS_MEDIUM_SIZE,
113 SCS_MEDIUM_SIZE * SCS_MEDIUM_CHUNK_SIZE, G_ALLOC_AND_FREE);
115 c->mate_large = g_mem_chunk_new("large_scs_chunk", SCS_LARGE_SIZE,
116 SCS_LARGE_SIZE * SCS_LARGE_CHUNK_SIZE, G_ALLOC_AND_FREE);
118 c->mate_huge = g_mem_chunk_new("huge_scs_chunk", SCS_HUGE_SIZE,
119 SCS_HUGE_SIZE * SCS_HUGE_CHUNK_SIZE, G_ALLOC_AND_FREE);
126 * @collection: the scs hash
129 * Checks if the given string exists already and if so it increases the count of
130 * subsscribers and returns a pointer to the stored string. If not It will copy
131 * the given string store it in the hash and return the pointer to the copy.
132 * Remember, containment is handled internally, take care of your own strings.
134 * Return value: a pointer to the subscribed string.
136 gchar* scs_subscribe(SCS_collection* c, const gchar* s) {
140 GMemChunk* chunk = NULL;
142 g_hash_table_lookup_extended(c->hash,(gconstpointer)s,(gpointer)&orig,(gpointer)&ip);
147 ip = g_mem_chunk_alloc(c->ctrs);
152 if (len <= SCS_SMALL_SIZE) {
153 chunk = c->mate_small;
154 len = SCS_SMALL_SIZE;
155 } else if (len <= SCS_MEDIUM_SIZE) {
156 chunk = c->mate_medium;
157 len = SCS_MEDIUM_SIZE;
158 } else if (len <= SCS_LARGE_SIZE) {
159 chunk = c->mate_large;
160 len = SCS_LARGE_SIZE;
161 } else if (len < SCS_HUGE_SIZE) {
162 chunk = c->mate_huge;
165 chunk = c->mate_huge;
167 g_warning("mate SCS: string truncated to huge size");
170 orig = g_mem_chunk_alloc(chunk);
173 g_hash_table_insert(c->hash,orig,ip);
181 * @collection: the scs hash
184 * decreases the count of subscribers, if zero frees the internal copy of
187 void scs_unsubscribe(SCS_collection* c, gchar* s) {
191 GMemChunk* chunk = NULL;
193 g_hash_table_lookup_extended(c->hash,(gconstpointer)s,(gpointer)&orig,(gpointer)&ip);
197 g_hash_table_remove(c->hash,orig);
201 if (len < SCS_SMALL_SIZE) {
202 chunk = c->mate_small;
203 } else if (len < SCS_MEDIUM_SIZE) {
204 chunk = c->mate_medium;
205 } else if (len < SCS_LARGE_SIZE) {
206 chunk = c->mate_large;
208 chunk = c->mate_huge;
211 g_mem_chunk_free(chunk,orig);
212 g_mem_chunk_free(c->ctrs,ip);
218 g_warning("unsusbcribe: not subscribed");
223 * scs_subscribe_printf:
224 * @fmt: a format string ...
226 * Formats the input and subscribes it.
228 * Return value: the stored copy of the formated string.
231 gchar* scs_subscribe_printf(SCS_collection* c, gchar* fmt, ...) {
233 static gchar buf[SCS_HUGE_SIZE];
235 va_start( list, fmt );
236 g_vsnprintf(buf, SCS_HUGE_SIZE-1 ,fmt, list);
239 return scs_subscribe(c,buf);
242 /***************************************************************************
244 ***************************************************************************
245 * The Thing operates mainly on avps, avpls and loals
246 * - attribute value pairs (two strings: the name and the value and an opeartor)
247 * - avp lists a somehow sorted list of avps
248 * - loal (list of avp lists) an arbitrarily sorted list of avpls
251 ***************************************************************************/
254 typedef union _any_avp_type {
263 static GMemChunk* avp_chunk = NULL;
264 static SCS_collection* avp_strings = NULL;
266 #ifdef _AVP_DEBUGGING
267 static FILE* dbg_fp = NULL;
269 static int dbg_level = 0;
270 static int* dbg = &dbg_level;
272 static int dbg_avp_level = 0;
273 static int* dbg_avp = &dbg_avp_level;
275 static int dbg_avp_op_level = 0;
276 static int* dbg_avp_op = &dbg_avp_op_level;
278 static int dbg_avpl_level = 0;
279 static int* dbg_avpl = &dbg_avpl_level;
281 static int dbg_avpl_op_level = 0;
282 static int* dbg_avpl_op = &dbg_avpl_op_level;
286 * @fp: the file in which to send debugging output.
287 * @general: a pointer to the level of debugging of facility "general"
288 * @avp: a pointer to the level of debugging of facility "avp"
289 * @avp_op: a pointer to the level of debugging of facility "avp_op"
290 * @avpl: a pointer to the level of debugging of facility "avpl"
291 * @avpl_op: a pointer to the level of debugging of facility "avpl_op"
293 * If enabled set's up the debug facilities for the avp library.
296 extern void setup_avp_debug(FILE* fp, int* general, int* avp, int* avp_op, int* avpl, int* avpl_op) {
302 dbg_avpl_op = avpl_op;
305 #endif /* _AVP_DEBUGGING */
309 * @chunk_size: the initial chunk's size.
311 * (Re)Initializes the avp library.
314 extern void avp_init(void) {
316 if (avp_strings) destroy_scs_collection(avp_strings);
317 avp_strings = scs_init();
319 if ( avp_chunk ) g_mem_chunk_destroy(avp_chunk);
320 avp_chunk = g_mem_chunk_new("avp_chunk", sizeof(any_avp_type),
321 AVP_CHUNK_SIZE, G_ALLOC_AND_FREE);
327 * new_avp_from_finfo:
328 * @name: the name the avp will have.
329 * @finfo: the field_info from which to fetch the data.
331 * Creates an avp from a field_info record.
333 * Return value: a pointer to the newly created avp.
336 extern AVP* new_avp_from_finfo(const gchar* name, field_info* finfo) {
337 AVP* new = g_mem_chunk_alloc(avp_chunk);
340 new->n = scs_subscribe(avp_strings, name);
342 if (finfo->value.ftype->val_to_string_repr) {
343 value = scs_subscribe(avp_strings, fvalue_to_string_repr(&finfo->value,FTREPR_DISPLAY,NULL));
344 #ifdef _AVP_DEBUGGING
345 dbg_print (dbg_avp,2,dbg_fp,"new_avp_from_finfo: from string: %s",value);
348 #ifdef _AVP_DEBUGGING
349 dbg_print (dbg_avp,2,dbg_fp,"new_avp_from_finfo: a proto: %s",finfo->hfinfo->abbrev);
351 value = scs_subscribe(avp_strings, "");
358 #ifdef _AVP_DEBUGGING
359 dbg_print (dbg_avp,1,dbg_fp,"new_avp_from_finfo: %X %s%c%s;",(guint32) new,new->n,new->o,new->v);
368 * @name: the name the avp will have.
369 * @value: the value the avp will have.
370 * @o: the operator of this avp.
372 * Creates an avp given every parameter.
374 * Return value: a pointer to the newly created avp.
377 extern AVP* new_avp(const gchar* name, const gchar* value, gchar o) {
378 AVP* new = g_mem_chunk_alloc(avp_chunk);
380 new->n = scs_subscribe(avp_strings, name);
381 new->v = scs_subscribe(avp_strings, value);
384 #ifdef _AVP_DEBUGGING
385 dbg_print(dbg_avp,1,dbg_fp,"new_avp: %X %s%c%s;",(guint32) new,new->n,new->o,new->v);
393 * @avp: the avp to delete.
395 * Destroys an avp and releases the resources it uses.
398 extern void delete_avp(AVP* avp) {
399 #ifdef _AVP_DEBUGGING
400 dbg_print(dbg_avp,1,dbg_fp,"delete_avp: %X %s%c%s;",(guint32) avp,avp->n,avp->o,avp->v);
403 scs_unsubscribe(avp_strings, avp->n);
404 scs_unsubscribe(avp_strings, avp->v);
405 g_mem_chunk_free(avp_chunk,avp);
411 * @from: the avp to be copied.
413 * Creates an avp whose name op and value are copyes of the given one.
415 * Return value: a pointer to the newly created avp.
418 extern AVP* avp_copy(AVP* from) {
419 AVP* new = g_mem_chunk_alloc(avp_chunk);
421 new->n = scs_subscribe(avp_strings, from->n);
422 new->v = scs_subscribe(avp_strings, from->v);
425 #ifdef _AVP_DEBUGGING
426 dbg_print(dbg_avp,1,dbg_fp,"copy_avp: %X %s%c%s;",(guint32) new,new->n,new->o,new->v);
434 * @name: the name the avpl will have.
436 * Creates an empty avpl.
438 * Return value: a pointer to the newly created avpl.
441 extern AVPL* new_avpl(const gchar* name) {
442 AVPL* new_avpl = g_mem_chunk_alloc(avp_chunk);
444 #ifdef _AVP_DEBUGGING
445 dbg_print(dbg_avpl_op,7,dbg_fp,"new_avpl: %X name=%s",new_avpl,name);
448 new_avpl->name = name ? scs_subscribe(avp_strings, name) : scs_subscribe(avp_strings, "");
450 new_avpl->null.avp = NULL;
451 new_avpl->null.next = &new_avpl->null;
452 new_avpl->null.prev = &new_avpl->null;
458 extern void rename_avpl(AVPL* avpl, gchar* name) {
459 scs_unsubscribe(avp_strings,avpl->name);
460 avpl->name = scs_subscribe(avp_strings,name);
465 * @avpl: the avpl in which to insert.
466 * @avp: the avp to be inserted.
468 * Inserts the given AVP into the given AVPL if an identical one isn't yet there.
470 * Return value: whether it was inserted or not.
472 * BEWARE: Check the return value, you might need to delete the avp if
473 * it is not inserted.
475 extern gboolean insert_avp(AVPL* avpl, AVP* avp) {
476 AVPN* new = g_mem_chunk_alloc(avp_chunk);
481 #ifdef _AVP_DEBUGGING
482 dbg_print(dbg_avpl_op,7,dbg_fp,"new_avpn: %X",new);
483 dbg_print(dbg_avpl_op,4,dbg_fp,"insert_avp: %X %X %s%c%s;",avpl,avp,avp->n,avp->o,avp->v);
486 /* get to the insertion point */
487 for(c=avpl->null.next; c->avp; c = c->next) {
489 if ( avp->n == c->avp->n ) {
491 if (avp->v > c->avp->v) {
495 if (avp->v == c->avp->v) {
496 if (avp->o == AVP_OP_EQUAL) {
497 #ifdef _AVP_DEBUGGING
498 dbg_print(dbg_avpl_op,7,dbg_fp,"delete_avpn: %X",new);
500 g_mem_chunk_free(avp_chunk,new);
506 if (avp->n > c->avp->n) {
511 #ifdef _AVP_DEBUGGING
512 dbg_print(dbg_avpl,5,dbg_fp,"insert_avp: inserting %X in %X before %X;",avp,avpl,c);
522 #ifdef _AVP_DEBUGGING
523 dbg_print(dbg_avpl,4,dbg_fp,"avpl: %X new len: %i",avpl,avpl->len);
531 * @avpl: the avpl from which to try to get the avp.
532 * @name: the name of the avp we are looking for.
533 * @cookie: variable in which to store the state between calls.
535 * Gets pointer to the next avp whose name is given; uses cookie to store its
536 * state between calls.
538 * Return value: a pointer to the next matching avp if there's one, else NULL.
541 extern AVP* get_avp_by_name(AVPL* avpl, gchar* name, void** cookie) {
543 AVPN* start = (AVPN*) *cookie;
545 #ifdef _AVP_DEBUGGING
546 dbg_print(dbg_avpl_op,7,dbg_fp,"get_avp_by_name: entering: %X %s %X",avpl,name,*cookie);
549 name = scs_subscribe(avp_strings, name);
551 if (!start) start = avpl->null.next;
553 for ( curr = start; curr->avp; curr = curr->next ) {
554 if ( curr->avp->n == name ) {
561 #ifdef _AVP_DEBUGGING
562 dbg_print(dbg_avpl_op,5,dbg_fp,"get_avp_by_name: got avp: %X",curr);
565 scs_unsubscribe(avp_strings, name);
571 * extract_avp_by_name:
572 * @avpl: the avpl from which to try to extract the avp.
573 * @name: the name of the avp we are looking for.
575 * Extracts from the avpl the next avp whose name is given;
577 * Return value: a pointer to extracted avp if there's one, else NULL.
580 extern AVP* extract_avp_by_name(AVPL* avpl, gchar* name) {
584 #ifdef _AVP_DEBUGGING
585 dbg_print(dbg_avpl_op,7,dbg_fp,"extract_avp_by_name: entering: %X %s",avpl,name);
588 name = scs_subscribe(avp_strings, name);
590 for ( curr = avpl->null.next; curr->avp; curr = curr->next ) {
591 if ( curr->avp->n == name ) {
596 scs_unsubscribe(avp_strings, name);
598 if( ! curr->avp ) return NULL;
600 curr->next->prev = curr->prev;
601 curr->prev->next = curr->next;
605 g_mem_chunk_free(avp_chunk,curr);
609 #ifdef _AVP_DEBUGGING
610 dbg_print(dbg_avpl,4,dbg_fp,"avpl: %X new len: %i",avpl,avpl->len);
613 #ifdef _AVP_DEBUGGING
614 dbg_print(dbg_avpl_op,5,dbg_fp,"extract_avp_by_name: got avp: %X",avp);
623 * @avpl: the avpl from which to try to extract the avp.
625 * Extracts the fisrt avp from the avpl.
627 * Return value: a pointer to extracted avp if there's one, else NULL.
630 extern AVP* extract_first_avp(AVPL* avpl) {
634 #ifdef _AVP_DEBUGGING
635 dbg_print(dbg_avpl_op,7,dbg_fp,"extract_first_avp: %X",avpl);
638 node = avpl->null.next;
640 avpl->null.next->prev = &avpl->null;
641 avpl->null.next = node->next;
646 g_mem_chunk_free(avp_chunk,node);
648 #ifdef _AVP_DEBUGGING
649 dbg_print(dbg_avpl,4,dbg_fp,"avpl: %X new len: %i",avpl,avpl->len);
653 #ifdef _AVP_DEBUGGING
654 dbg_print(dbg_avpl_op,5,dbg_fp,"extract_first_avp: got avp: %X",avp);
664 * @avpl: the avpl from which to try to extract the avp.
666 * Extracts the last avp from the avpl.
668 * Return value: a pointer to extracted avp if there's one, else NULL.
671 extern AVP* extract_last_avp(AVPL* avpl) {
675 node = avpl->null.prev;
677 avpl->null.prev->next = &avpl->null;
678 avpl->null.prev = node->prev;
683 g_mem_chunk_free(avp_chunk,node);
685 #ifdef _AVP_DEBUGGING
686 dbg_print(dbg_avpl,4,dbg_fp,"avpl: %X new len: %i",avpl,avpl->len);
690 #ifdef _AVP_DEBUGGING
691 dbg_print(dbg_avpl_op,5,dbg_fp,"extract_last_avp: got avp: %X",avp);
701 * @avpl: the avpl from which to try to extract the avp.
702 * @avps_too: whether or not it should delete the avps as well.
704 * Destroys an avpl and releases the resources it uses. If told to do
705 * so releases the avps as well.
708 extern void delete_avpl(AVPL* avpl, gboolean avps_too) {
710 #ifdef _AVP_DEBUGGING
711 dbg_print(dbg_avpl,3,dbg_fp,"delete_avpl: %X",avpl);
714 while(( avp = extract_last_avp(avpl))) {
720 scs_unsubscribe(avp_strings,avpl->name);
721 g_mem_chunk_free(avp_chunk,avpl);
728 * @avpl: the avpl from which to try to get the avps.
729 * @cookie: variable in which to store the state between calls.
731 * Iterates on an avpl to get its avps.
733 * Return value: a pointer to the next avp if there's one, else NULL.
736 extern AVP* get_next_avp(AVPL* avpl, void** cookie) {
739 #ifdef _AVP_DEBUGGING
740 dbg_print(dbg_avpl_op,5,dbg_fp,"get_next_avp: avpl: %X avpn: %X",avpl,*cookie);
744 node = (AVPN*) *cookie;
746 node = avpl->null.next;
749 *cookie = node->next;
751 #ifdef _AVP_DEBUGGING
752 dbg_print(dbg_avpl_op,5,dbg_fp,"extract_last_avp: got avp: %X",node->avp);
760 * @avpl: the avpl to represent.
762 * Creates a newly allocated string containing a representation of an avpl.
764 * Return value: a pointer to the newly allocated string.
767 gchar* avpl_to_str(AVPL* avpl) {
769 GString* s = g_string_new("");
773 for(c=avpl->null.next; c->avp; c = c->next) {
774 avp_s = avp_to_str(c->avp);
775 g_string_sprintfa(s," %s;",avp_s);
780 g_string_free(s,FALSE);
782 /* g_strchug(r); ? */
786 extern gchar* avpl_to_dotstr(AVPL* avpl) {
788 GString* s = g_string_new("");
792 for(c=avpl->null.next; c->avp; c = c->next) {
793 avp_s = avp_to_str(c->avp);
794 g_string_sprintfa(s," .%s;",avp_s);
799 g_string_free(s,FALSE);
801 /* g_strchug(r); ? */
807 * @dst: the avpl in which to merge the avps.
808 * @src: the avpl from which to get the avps.
809 * @copy: whether avps should be copied instead of referenced.
811 * Adds the avps of src that are not existent in dst into dst.
813 * Return value: a pointer to the newly allocated string.
816 extern void merge_avpl(AVPL* dst, AVPL* src, gboolean copy_avps) {
822 #ifdef _AVP_DEBUGGING
823 dbg_print(dbg_avpl_op,3,dbg_fp,"merge_avpl: %X %X",dst,src);
832 c = (guint) cd->avp->n - (guint) cs->avp->n;
838 if (cd->avp) cd = cd->next;
841 copy = avp_copy(cs->avp);
842 if ( ! insert_avp(dst,copy) ) {
846 insert_avp(dst,cs->avp);
851 if ( ! cd->avp || ! (cd->avp->v == cs->avp->v) ) {
853 copy = avp_copy(cs->avp);
854 if ( ! insert_avp(dst,copy) ) {
858 insert_avp(dst,cs->avp);
862 if (cd->avp) cd = cd->next;
866 #ifdef _AVP_DEBUGGING
867 dbg_print(dbg_avpl_op,8,dbg_fp,"merge_avpl: done");
876 * @name: the name of the new avpl.
877 * @avpl: the avpl from which to get the avps.
878 * @copy_avps: whether avps should be copied instead of referenced.
880 * Creates a new avpl containing the same avps as the given avpl
881 * It will either reference or copie the avps.
883 * Return value: a pointer to the newly allocated string.
886 extern AVPL* new_avpl_from_avpl(const gchar* name, AVPL* avpl, gboolean copy_avps) {
887 AVPL* newavpl = new_avpl(name);
892 #ifdef _AVP_DEBUGGING
893 dbg_print(dbg_avpl_op,3,dbg_fp,"new_avpl_from_avpl: %X from=%X name='%s'",newavpl,avpl,name);
896 while(( avp = get_next_avp(avpl,&cookie) )) {
898 copy = avp_copy(avp);
899 if ( ! insert_avp(newavpl,copy) ) {
903 insert_avp(newavpl,avp);
907 #ifdef _AVP_DEBUGGING
908 dbg_print(dbg_avpl_op,8,dbg_fp,"new_avpl_from_avpl: done");
916 * @src: an src to be compared agains an "op" avp
917 * @op: the "op" avp that will be matched against the src avp
919 * Checks whether or not two avp's match.
921 * Return value: a pointer to the src avp if there's a match.
924 extern AVP* match_avp(AVP* src, AVP* op) {
932 gboolean lower = FALSE;
934 #ifdef _AVP_DEBUGGING
935 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);
938 if ( src->n != op->n ) {
946 return src->v == op->v ? src : NULL;
947 case AVP_OP_NOTEQUAL:
948 return !( src->v == op->v) ? src : NULL;
950 return strncmp(src->v,op->v,strlen(op->v)) == 0 ? src : NULL;
952 splited = g_strsplit(op->v,"|",0);
954 for (i=0;splited[i];i++) {
955 if(g_str_equal(splited[i],src->v)) {
968 fs = (float) strtod(src->v, NULL);
969 fo = (float) strtod(src->v, NULL);
972 if (fs<fo) return src;
975 if (fs>fo) return src;
979 /* does this work? */
986 p = src->v + ( ls - lo );
987 return g_str_equal(p,op->v) ? src : NULL;
990 /* case AVP_OP_TRANSF: */
991 /* return do_transform(src,op); */
992 case AVP_OP_CONTAINS:
996 /* will never get here */
1002 /* TODO: rename me */
1004 * new_avpl_loose_match:
1005 * @name: the name of the resulting avpl
1006 * @src: avpl to be matched agains an "op" avpl
1007 * @op: the "op" avpl that will be matched against the src avpl
1008 * @copy_avps: whether the avps in the resulting avpl should be copied
1010 * creates an avp list containing any avps in src matching any avps in op
1011 * it will eventually create an empty list in none match
1013 * Return value: a pointer to the newly created avpl containing the
1016 extern AVPL* new_avpl_loose_match(const gchar* name,
1019 gboolean copy_avps) {
1021 AVPL* newavpl = new_avpl(scs_subscribe(avp_strings, name));
1028 #ifdef _AVP_DEBUGGING
1029 dbg_print(dbg_avpl_op,3,dbg_fp,"new_avpl_loose_match: %X src=%X op=%X name='%s'",newavpl,src,op,name);
1033 cs = src->null.next;
1046 c = (guint) co->avp->n - (guint) cs->avp->n;
1049 if (co->avp) co = co->next;
1051 if (cs->avp) cs = cs->next;
1053 m = match_avp(cs->avp,co->avp);
1058 if ( ! insert_avp(newavpl,copy) ) {
1062 insert_avp(newavpl,m);
1068 if (cs->avp) cs = cs->next;
1073 #ifdef _AVP_DEBUGGING
1074 dbg_print(dbg_avpl_op,6,dbg_fp,"new_avpl_loose_match: done!");
1080 /* TODO: rename me */
1082 * new_avpl_every_match:
1083 * @name: the name of the resulting avpl
1084 * @src: avpl to be matched agains an "op" avpl
1085 * @op: the "op" avpl that will be matched against the src avpl
1086 * @copy_avps: whether the avps in the resulting avpl should be copied
1088 * creates an avp list containing any avps in src matching every avp in op
1089 * it will not create a list if there is not a match for every attribute in op
1091 * Return value: a pointer to the newly created avpl containing the
1094 extern AVPL* new_avpl_every_match(const gchar* name, AVPL* src, AVPL* op, gboolean copy_avps) {
1103 #ifdef _AVP_DEBUGGING
1104 dbg_print(dbg_avpl_op,3,dbg_fp,"new_avpl_every_match: %X src=%X op=%X name='%s'",newavpl,src,op,name);
1106 if (src->len == 0) return NULL;
1108 newavpl = new_avpl(scs_subscribe(avp_strings, name));
1115 cs = src->null.next;
1127 c = (guint) co->avp->n - (guint) cs->avp->n;
1130 delete_avpl(newavpl,TRUE);
1138 m = match_avp(cs->avp,co->avp);
1147 if ( ! insert_avp(newavpl,copy) ) {
1151 insert_avp(newavpl,m);
1164 delete_avpl(newavpl,TRUE);
1170 /* TODO: rename me */
1172 * new_avpl_exact_match:
1173 * @name: the name of the resulting avpl
1174 * @src: avpl to be matched agains an "op" avpl
1175 * @op: the "op" avpl that will be matched against the src avpl
1176 * @copy_avps: whether the avps in the resulting avpl should be copied
1178 * creates an avp list containing every avp in src matching every avp in op
1179 * it will not create a list unless every avp in op is matched only once
1180 * to every avp in op.
1182 * Return value: a pointer to the newly created avpl containing the
1185 extern AVPL* new_avpl_exact_match(const gchar* name,AVPL* src, AVPL* op, gboolean copy_avps) {
1186 AVPL* newavpl = new_avpl(name);
1193 #ifdef _AVP_DEBUGGING
1194 dbg_print(dbg_avpl_op,3,dbg_fp,"new_avpl_every_match: %X src=%X op=%X name='%s'",newavpl,src,op,name);
1200 if (src->len == 0) {
1201 delete_avpl(newavpl,FALSE);
1205 cs = src->null.next;
1209 c = (guint) co->avp->n - (guint) cs->avp->n;
1212 delete_avpl(newavpl,TRUE);
1217 delete_avpl(newavpl,TRUE);
1221 m = match_avp(cs->avp,co->avp);
1229 if ( ! insert_avp(newavpl,copy) ) {
1233 insert_avp(newavpl,m);
1241 delete_avpl(newavpl,TRUE);
1245 delete_avpl(newavpl,TRUE);
1252 /* should never be reached */
1256 extern AVPL* new_avpl_from_match(avpl_match_mode mode, const gchar* name,AVPL* src, AVPL* op, gboolean copy_avps) {
1261 avpl = new_avpl_exact_match(name,src,op,copy_avps);
1264 avpl = new_avpl_loose_match(name,src,op,copy_avps);
1267 avpl = new_avpl_every_match(name,src,op,copy_avps);
1270 avpl = new_avpl_from_avpl(name,src,copy_avps);
1271 merge_avpl(avpl, op, copy_avps);
1279 * delete_avpl_transform:
1280 * @it: a pointer to the avpl transformation object
1282 * Destroys an avpl transformation object and releases all the resources it
1286 extern void delete_avpl_transform(AVPL_Transf* op) {
1289 for (; op ; op = next) {
1295 delete_avpl(op->match,TRUE);
1299 delete_avpl(op->replace,TRUE);
1310 * @src: the source avpl for the transform operation.
1311 * @op: a pointer to the avpl transformation object to apply.
1313 * Applies the "op" transformation to an avpl, matches it and eventually
1314 * replaces or inserts the transformed avps.
1316 * Return value: whether the transformation was performed or not.
1318 extern void avpl_transform(AVPL* src, AVPL_Transf* op) {
1324 #ifdef _AVP_DEBUGGING
1325 dbg_print(dbg_avpl_op,3,dbg_fp,"avpl_transform: src=%X op=%X",src,op);
1328 for ( ; op ; op = op->next) {
1330 avpl = new_avpl_from_match(op->match_mode, src->name,src, op->match, TRUE);
1333 switch (op->replace_mode) {
1334 case AVPL_NO_REPLACE:
1335 delete_avpl(avpl,TRUE);
1338 merge_avpl(src,op->replace,TRUE);
1339 delete_avpl(avpl,TRUE);
1342 cs = src->null.next;
1343 cm = avpl->null.next;
1345 if (cm->avp && cs->avp->n == cm->avp->n && cs->avp->v == cm->avp->v) {
1348 cs->prev->next = cs->next;
1349 cs->next->prev = cs->prev;
1350 g_mem_chunk_free(avp_chunk,cs);
1359 merge_avpl(src,op->replace,TRUE);
1360 delete_avpl(avpl,TRUE);
1370 * @name: the name the loal will take.
1372 * Creates an empty list of avp lists.
1374 * Return value: a pointer to the newly created loal.
1376 extern LoAL* new_loal(const gchar* name) {
1377 LoAL* new_loal = g_mem_chunk_alloc(avp_chunk);
1383 #ifdef _AVP_DEBUGGING
1384 dbg_print(dbg_avpl_op,3,dbg_fp,"new_loal: %X name=%s",new_loal,name);
1387 new_loal->name = scs_subscribe(avp_strings,name);
1388 new_loal->null.avpl = NULL;
1389 new_loal->null.next = &new_loal->null;
1390 new_loal->null.prev = &new_loal->null;
1397 * @loal: the loal on which to operate.
1398 * @avpl: the avpl to append.
1400 * Appends an avpl to a loal.
1403 extern void loal_append(LoAL* loal, AVPL* avpl) {
1404 LoALnode* node = g_mem_chunk_alloc(avp_chunk);
1406 #ifdef _AVP_DEBUGGING
1407 dbg_print(dbg_avpl_op,3,dbg_fp,"new_loal_node: %X",node);
1411 node->next = &loal->null;
1412 node->prev = loal->null.prev;
1414 loal->null.prev->next = node;
1415 loal->null.prev = node;
1421 * extract_first_avpl:
1422 * @loal: the loal on which to operate.
1424 * Extracts the first avpl contained in a loal.
1426 * Return value: a pointer to the extracted avpl.
1429 extern AVPL* extract_first_avpl(LoAL* loal) {
1433 #ifdef _AVP_DEBUGGING
1434 dbg_print(dbg_avpl_op,3,dbg_fp,"extract_first_avpl: from: %s",loal->name);
1437 node = loal->null.next;
1439 loal->null.next->next->prev = &loal->null;
1440 loal->null.next = node->next;
1447 g_mem_chunk_free(avp_chunk,node);
1449 #ifdef _AVP_DEBUGGING
1450 dbg_print(dbg_avpl_op,3,dbg_fp,"extract_first_avpl: got %s",avpl->name);
1451 dbg_print(dbg_avpl_op,3,dbg_fp,"delete_loal_node: %X",node);
1459 * extract_first_avpl:
1460 * @loal: the loal on which to operate.
1462 * Extracts the last avpl contained in a loal.
1464 * Return value: a pointer to the extracted avpl.
1467 extern AVPL* extract_last_avpl(LoAL* loal){
1471 node = loal->null.prev;
1473 loal->null.prev->prev->next = &loal->null;
1474 loal->null.prev = node->prev;
1481 g_mem_chunk_free(avp_chunk,node);
1482 #ifdef _AVP_DEBUGGING
1483 dbg_print(dbg_avpl_op,3,dbg_fp,"delete_loal_node: %X",node);
1491 * extract_first_avpl:
1492 * @loal: the loal on which to operate.
1493 * @cookie pointer to the pointer variable to contain the state between calls
1495 * At each call will return the following avpl from a loal. The given cookie
1496 * will be used to manatain the state between calls.
1498 * Return value: a pointer to the next avpl.
1501 extern AVPL* get_next_avpl(LoAL* loal,void** cookie) {
1504 #ifdef _AVP_DEBUGGING
1505 dbg_print(dbg_avpl_op,3,dbg_fp,"get_next_avpl: loal=%X node=%X",loal,*cookie);
1509 node = (LoALnode*) *cookie;
1511 node = loal->null.next;
1514 *cookie = node->next;
1521 * @loal: the loal to be deleted.
1522 * @avpls_too: whether avpls contained by the loal should be deleted as well
1523 * @avps_too: whether avps contained by the avpls should be also deleted
1525 * Destroys a loal and eventually desstroys avpls and avps.
1528 extern void delete_loal(LoAL* loal, gboolean avpls_too, gboolean avps_too) {
1531 #ifdef _AVP_DEBUGGING
1532 dbg_print(dbg_avpl_op,3,dbg_fp,"delete_loal: %X",loal);
1535 while(( avpl = extract_last_avpl(loal) )) {
1537 delete_avpl(avpl,avps_too);
1541 scs_unsubscribe(avp_strings,loal->name);
1542 g_mem_chunk_free(avp_chunk,loal);
1547 /****************************************************************************
1548 ******************* the following are used in load_loal_from_file
1549 ****************************************************************************/
1553 * Used by loal_from_file to handle errors while loading.
1555 static LoAL* load_loal_error(FILE* fp, LoAL* loal, AVPL* curr, int linenum, const gchar* fmt, ...) {
1561 va_start( list, fmt );
1562 desc = g_strdup_vprintf(fmt, list);
1566 err = g_strdup_printf("Error Loading LoAL from file: in %s at line: %i, %s",loal->name,linenum,desc);
1567 ret = new_loal(err);
1573 if (loal) delete_loal(loal,TRUE,TRUE);
1574 if (curr) delete_avpl(curr,TRUE);
1580 /* the maximum length allowed for a line */
1581 #define MAX_ITEM_LEN 8192
1583 /* this two ugly things are used for tokenizing */
1584 #define AVP_OP_CHAR '=': case '^': case '$': case '~': case '<': case '>': case '?': case '|': case '&' : case '!'
1586 #define AVP_NAME_CHAR 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J':\
1587 case 'K': case 'L': case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T':\
1588 case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z': case 'a': case 'b': case 'c': case 'd':\
1589 case 'e': case 'f': case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':\
1590 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': case 'v': case 'w': case 'x':\
1591 case 'y': case 'z': case '_': case '0': case '1': case '2': case '3': case '4': case '5': case '6':\
1592 case '7': case '8': case '9': case '.'
1597 * @filename: the file containing a loals text representation.
1599 * Given a filename it will attempt to load a loal containing a copy of
1600 * the avpls represented in the file.
1602 * Return value: if successful a pointer to the new populated loal, else NULL.
1605 extern LoAL* loal_from_file(gchar* filename) {
1609 guint32 linenum = 1;
1610 gchar linenum_buf[MAX_ITEM_LEN];
1611 gchar name[MAX_ITEM_LEN];
1612 gchar value[MAX_ITEM_LEN];
1614 LoAL *loal = new_loal(filename);
1618 enum _load_loal_states {
1628 return load_loal_error(fp,loal,curr,linenum,"MATE Will not run as root");
1634 if (( fp = eth_fopen(filename,"r") )) {
1635 while(( c = (gchar) fgetc(fp) )){
1639 report_read_failure(filename,errno);
1640 return load_loal_error(fp,loal,curr,linenum,"Error while reading '%f'",filename);
1649 if ( i >= MAX_ITEM_LEN - 1 ) {
1650 return load_loal_error(fp,loal,curr,linenum,"Maximum item length exceeded");
1666 case ' ': case '\t':
1667 /* ignore whitespace at line start */
1670 /* ignore empty lines */
1678 g_snprintf(linenum_buf,sizeof(linenum_buf),"%s:%u",filename,linenum);
1679 curr = new_avpl(linenum_buf);
1685 return load_loal_error(fp,loal,curr,linenum,"expecting name got: '%c'",c);
1693 if (c != '\n') ungetc(c,fp);
1705 loal_append(loal,curr);
1709 return load_loal_error(fp,loal,curr,linenum,"expecting name got: '%c'",c);
1714 state = BEFORE_NAME;
1721 avp = new_avp(name,value,op);
1723 if (! insert_avp(curr,avp) ) {
1738 return load_loal_error(fp,loal,curr,linenum,"operator expected found new line");
1740 return load_loal_error(fp,loal,curr,linenum,"name or match operator expected found '%c'",c);
1745 value[i++] = fgetc(fp);
1748 state = BEFORE_NAME;
1753 avp = new_avp(name,value,op);
1755 if (! insert_avp(curr,avp) ) {
1760 return load_loal_error(fp,loal,curr,linenum,"';' expected found new line");
1772 report_open_failure(filename,errno,FALSE);
1773 return load_loal_error(NULL,loal,NULL,0,"Cannot Open file '%s'",filename);