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>
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);
432 static void rename_avp(AVP* avp, gchar* name) {
434 avp->n = scs_subscribe(avp_strings,name);
435 scs_unsubscribe(avp_strings,s);
440 * @name: the name the avpl will have.
442 * Creates an empty avpl.
444 * Return value: a pointer to the newly created avpl.
447 extern AVPL* new_avpl(const gchar* name) {
448 AVPL* new_avpl = g_mem_chunk_alloc(avp_chunk);
450 #ifdef _AVP_DEBUGGING
451 dbg_print(dbg_avpl_op,7,dbg_fp,"new_avpl: %X name=%s",new_avpl,name);
454 new_avpl->name = name ? scs_subscribe(avp_strings, name) : scs_subscribe(avp_strings, "");
456 new_avpl->null.avp = NULL;
457 new_avpl->null.next = &new_avpl->null;
458 new_avpl->null.prev = &new_avpl->null;
464 extern void rename_avpl(AVPL* avpl, gchar* name) {
465 scs_unsubscribe(avp_strings,avpl->name);
466 avpl->name = scs_subscribe(avp_strings,name);
471 * @avpl: the avpl in which to insert.
472 * @avp: the avp to be inserted.
474 * Inserts the given AVP into the given AVPL if an identical one isn't yet there.
476 * Return value: whether it was inserted or not.
478 * BEWARE: Check the return value, you might need to delete the avp if
479 * it is not inserted.
481 extern gboolean insert_avp(AVPL* avpl, AVP* avp) {
482 AVPN* new = g_mem_chunk_alloc(avp_chunk);
487 #ifdef _AVP_DEBUGGING
488 dbg_print(dbg_avpl_op,7,dbg_fp,"new_avpn: %X",new);
489 dbg_print(dbg_avpl_op,4,dbg_fp,"insert_avp: %X %X %s%c%s;",avpl,avp,avp->n,avp->o,avp->v);
492 /* get to the insertion point */
493 for(c=avpl->null.next; c->avp; c = c->next) {
495 if ( avp->n == c->avp->n ) {
497 if (avp->v > c->avp->v) {
501 if (avp->v == c->avp->v) {
502 if (avp->o == AVP_OP_EQUAL) {
503 #ifdef _AVP_DEBUGGING
504 dbg_print(dbg_avpl_op,7,dbg_fp,"delete_avpn: %X",new);
506 g_mem_chunk_free(avp_chunk,new);
512 if (avp->n > c->avp->n) {
517 #ifdef _AVP_DEBUGGING
518 dbg_print(dbg_avpl,5,dbg_fp,"insert_avp: inserting %X in %X before %X;",avp,avpl,c);
528 #ifdef _AVP_DEBUGGING
529 dbg_print(dbg_avpl,4,dbg_fp,"avpl: %X new len: %i",avpl,avpl->len);
537 * @avpl: the avpl from which to try to get the avp.
538 * @name: the name of the avp we are looking for.
539 * @cookie: variable in which to store the state between calls.
541 * Gets pointer to the next avp whose name is given; uses cookie to store its
542 * state between calls.
544 * Return value: a pointer to the next matching avp if there's one, else NULL.
547 extern AVP* get_avp_by_name(AVPL* avpl, gchar* name, void** cookie) {
549 AVPN* start = (AVPN*) *cookie;
551 #ifdef _AVP_DEBUGGING
552 dbg_print(dbg_avpl_op,7,dbg_fp,"get_avp_by_name: entering: %X %s %X",avpl,name,*cookie);
555 name = scs_subscribe(avp_strings, name);
557 if (!start) start = avpl->null.next;
559 for ( curr = start; curr->avp; curr = curr->next ) {
560 if ( curr->avp->n == name ) {
567 #ifdef _AVP_DEBUGGING
568 dbg_print(dbg_avpl_op,5,dbg_fp,"get_avp_by_name: got avp: %X",curr);
571 scs_unsubscribe(avp_strings, name);
577 * extract_avp_by_name:
578 * @avpl: the avpl from which to try to extract the avp.
579 * @name: the name of the avp we are looking for.
581 * Extracts from the avpl the next avp whose name is given;
583 * Return value: a pointer to extracted avp if there's one, else NULL.
586 extern AVP* extract_avp_by_name(AVPL* avpl, gchar* name) {
590 #ifdef _AVP_DEBUGGING
591 dbg_print(dbg_avpl_op,7,dbg_fp,"extract_avp_by_name: entering: %X %s",avpl,name);
594 name = scs_subscribe(avp_strings, name);
596 for ( curr = avpl->null.next; curr->avp; curr = curr->next ) {
597 if ( curr->avp->n == name ) {
602 scs_unsubscribe(avp_strings, name);
604 if( ! curr->avp ) return NULL;
606 curr->next->prev = curr->prev;
607 curr->prev->next = curr->next;
611 g_mem_chunk_free(avp_chunk,curr);
615 #ifdef _AVP_DEBUGGING
616 dbg_print(dbg_avpl,4,dbg_fp,"avpl: %X new len: %i",avpl,avpl->len);
619 #ifdef _AVP_DEBUGGING
620 dbg_print(dbg_avpl_op,5,dbg_fp,"extract_avp_by_name: got avp: %X",avp);
629 * @avpl: the avpl from which to try to extract the avp.
631 * Extracts the fisrt avp from the avpl.
633 * Return value: a pointer to extracted avp if there's one, else NULL.
636 extern AVP* extract_first_avp(AVPL* avpl) {
640 #ifdef _AVP_DEBUGGING
641 dbg_print(dbg_avpl_op,7,dbg_fp,"extract_first_avp: %X",avpl);
644 node = avpl->null.next;
646 avpl->null.next->prev = &avpl->null;
647 avpl->null.next = node->next;
652 g_mem_chunk_free(avp_chunk,node);
654 #ifdef _AVP_DEBUGGING
655 dbg_print(dbg_avpl,4,dbg_fp,"avpl: %X new len: %i",avpl,avpl->len);
659 #ifdef _AVP_DEBUGGING
660 dbg_print(dbg_avpl_op,5,dbg_fp,"extract_first_avp: got avp: %X",avp);
670 * @avpl: the avpl from which to try to extract the avp.
672 * Extracts the last avp from the avpl.
674 * Return value: a pointer to extracted avp if there's one, else NULL.
677 extern AVP* extract_last_avp(AVPL* avpl) {
681 node = avpl->null.prev;
683 avpl->null.prev->next = &avpl->null;
684 avpl->null.prev = node->prev;
689 g_mem_chunk_free(avp_chunk,node);
691 #ifdef _AVP_DEBUGGING
692 dbg_print(dbg_avpl,4,dbg_fp,"avpl: %X new len: %i",avpl,avpl->len);
696 #ifdef _AVP_DEBUGGING
697 dbg_print(dbg_avpl_op,5,dbg_fp,"extract_last_avp: got avp: %X",avp);
707 * @avpl: the avpl from which to try to extract the avp.
708 * @avps_too: whether or not it should delete the avps as well.
710 * Destroys an avpl and releases the resources it uses. If told to do
711 * so releases the avps as well.
714 extern void delete_avpl(AVPL* avpl, gboolean avps_too) {
716 #ifdef _AVP_DEBUGGING
717 dbg_print(dbg_avpl,3,dbg_fp,"delete_avpl: %X",avpl);
720 while(( avp = extract_last_avp(avpl))) {
726 scs_unsubscribe(avp_strings,avpl->name);
727 g_mem_chunk_free(avp_chunk,avpl);
734 * @avpl: the avpl from which to try to get the avps.
735 * @cookie: variable in which to store the state between calls.
737 * Iterates on an avpl to get its avps.
739 * Return value: a pointer to the next avp if there's one, else NULL.
742 extern AVP* get_next_avp(AVPL* avpl, void** cookie) {
745 #ifdef _AVP_DEBUGGING
746 dbg_print(dbg_avpl_op,5,dbg_fp,"get_next_avp: avpl: %X avpn: %X",avpl,*cookie);
750 node = (AVPN*) *cookie;
752 node = avpl->null.next;
755 *cookie = node->next;
757 #ifdef _AVP_DEBUGGING
758 dbg_print(dbg_avpl_op,5,dbg_fp,"extract_last_avp: got avp: %X",node->avp);
766 * @avpl: the avpl to represent.
768 * Creates a newly allocated string containing a representation of an avpl.
770 * Return value: a pointer to the newly allocated string.
773 gchar* avpl_to_str(AVPL* avpl) {
775 GString* s = g_string_new("");
779 for(c=avpl->null.next; c->avp; c = c->next) {
780 avp_s = avp_to_str(c->avp);
781 g_string_sprintfa(s," %s;",avp_s);
786 g_string_free(s,FALSE);
788 /* g_strchug(r); ? */
792 extern gchar* avpl_to_dotstr(AVPL* avpl) {
794 GString* s = g_string_new("");
798 for(c=avpl->null.next; c->avp; c = c->next) {
799 avp_s = avp_to_str(c->avp);
800 g_string_sprintfa(s," .%s;",avp_s);
805 g_string_free(s,FALSE);
807 /* g_strchug(r); ? */
813 * @dst: the avpl in which to merge the avps.
814 * @src: the avpl from which to get the avps.
815 * @copy: whether avps should be copied instead of referenced.
817 * Adds the avps of src that are not existent in dst into dst.
819 * Return value: a pointer to the newly allocated string.
822 extern void merge_avpl(AVPL* dst, AVPL* src, gboolean copy_avps) {
828 #ifdef _AVP_DEBUGGING
829 dbg_print(dbg_avpl_op,3,dbg_fp,"merge_avpl: %X %X",dst,src);
838 c = (guint) cd->avp->n - (guint) cs->avp->n;
844 if (cd->avp) cd = cd->next;
847 copy = avp_copy(cs->avp);
848 if ( ! insert_avp(dst,copy) ) {
852 insert_avp(dst,cs->avp);
857 if ( ! cd->avp || ! (cd->avp->v == cs->avp->v) ) {
859 copy = avp_copy(cs->avp);
860 if ( ! insert_avp(dst,copy) ) {
864 insert_avp(dst,cs->avp);
868 if (cd->avp) cd = cd->next;
872 #ifdef _AVP_DEBUGGING
873 dbg_print(dbg_avpl_op,8,dbg_fp,"merge_avpl: done");
882 * @name: the name of the new avpl.
883 * @avpl: the avpl from which to get the avps.
884 * @copy_avps: whether avps should be copied instead of referenced.
886 * Creates a new avpl containing the same avps as the given avpl
887 * It will either reference or copie the avps.
889 * Return value: a pointer to the newly allocated string.
892 extern AVPL* new_avpl_from_avpl(const gchar* name, AVPL* avpl, gboolean copy_avps) {
893 AVPL* newavpl = new_avpl(name);
898 #ifdef _AVP_DEBUGGING
899 dbg_print(dbg_avpl_op,3,dbg_fp,"new_avpl_from_avpl: %X from=%X name='%s'",newavpl,avpl,name);
902 while(( avp = get_next_avp(avpl,&cookie) )) {
904 copy = avp_copy(avp);
905 if ( ! insert_avp(newavpl,copy) ) {
909 insert_avp(newavpl,avp);
913 #ifdef _AVP_DEBUGGING
914 dbg_print(dbg_avpl_op,8,dbg_fp,"new_avpl_from_avpl: done");
922 * @src: an src to be compared agains an "op" avp
923 * @op: the "op" avp that will be matched against the src avp
925 * Checks whether or not two avp's match.
927 * Return value: a pointer to the src avp if there's a match.
930 extern AVP* match_avp(AVP* src, AVP* op) {
938 gboolean lower = FALSE;
940 #ifdef _AVP_DEBUGGING
941 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);
944 if ( src->n != op->n ) {
952 return src->v == op->v ? src : NULL;
953 case AVP_OP_NOTEQUAL:
954 return !( src->v == op->v) ? src : NULL;
956 return strncmp(src->v,op->v,strlen(op->v)) == 0 ? src : NULL;
958 splited = g_strsplit(op->v,"|",0);
960 for (i=0;splited[i];i++) {
961 if(g_str_equal(splited[i],src->v)) {
974 fs = (float) strtod(src->v, NULL);
975 fo = (float) strtod(src->v, NULL);
978 if (fs<fo) return src;
981 if (fs>fo) return src;
985 /* does this work? */
992 p = src->v + ( ls - lo );
993 return g_str_equal(p,op->v) ? src : NULL;
996 /* case AVP_OP_TRANSF: */
997 /* return do_transform(src,op); */
998 case AVP_OP_CONTAINS:
1002 /* will never get here */
1008 /* TODO: rename me */
1010 * new_avpl_loose_match:
1011 * @name: the name of the resulting avpl
1012 * @src: avpl to be matched agains an "op" avpl
1013 * @op: the "op" avpl that will be matched against the src avpl
1014 * @copy_avps: whether the avps in the resulting avpl should be copied
1016 * creates an avp list containing any avps in src matching any avps in op
1017 * it will eventually create an empty list in none match
1019 * Return value: a pointer to the newly created avpl containing the
1022 extern AVPL* new_avpl_loose_match(const gchar* name,
1025 gboolean copy_avps) {
1027 AVPL* newavpl = new_avpl(scs_subscribe(avp_strings, name));
1034 #ifdef _AVP_DEBUGGING
1035 dbg_print(dbg_avpl_op,3,dbg_fp,"new_avpl_loose_match: %X src=%X op=%X name='%s'",newavpl,src,op,name);
1039 cs = src->null.next;
1052 c = (guint) co->avp->n - (guint) cs->avp->n;
1055 if (co->avp) co = co->next;
1057 if (cs->avp) cs = cs->next;
1059 m = match_avp(cs->avp,co->avp);
1064 if ( ! insert_avp(newavpl,copy) ) {
1068 insert_avp(newavpl,m);
1074 if (cs->avp) cs = cs->next;
1079 #ifdef _AVP_DEBUGGING
1080 dbg_print(dbg_avpl_op,6,dbg_fp,"new_avpl_loose_match: done!");
1086 /* TODO: rename me */
1088 * new_avpl_every_match:
1089 * @name: the name of the resulting avpl
1090 * @src: avpl to be matched agains an "op" avpl
1091 * @op: the "op" avpl that will be matched against the src avpl
1092 * @copy_avps: whether the avps in the resulting avpl should be copied
1094 * creates an avp list containing any avps in src matching every avp in op
1095 * it will not create a list if there is not a match for every attribute in op
1097 * Return value: a pointer to the newly created avpl containing the
1100 extern AVPL* new_avpl_every_match(const gchar* name, AVPL* src, AVPL* op, gboolean copy_avps) {
1109 #ifdef _AVP_DEBUGGING
1110 dbg_print(dbg_avpl_op,3,dbg_fp,"new_avpl_every_match: %X src=%X op=%X name='%s'",newavpl,src,op,name);
1112 if (src->len == 0) return NULL;
1114 newavpl = new_avpl(scs_subscribe(avp_strings, name));
1121 cs = src->null.next;
1133 c = (guint) co->avp->n - (guint) cs->avp->n;
1136 delete_avpl(newavpl,TRUE);
1144 m = match_avp(cs->avp,co->avp);
1153 if ( ! insert_avp(newavpl,copy) ) {
1157 insert_avp(newavpl,m);
1170 delete_avpl(newavpl,TRUE);
1176 /* TODO: rename me */
1178 * new_avpl_exact_match:
1179 * @name: the name of the resulting avpl
1180 * @src: avpl to be matched agains an "op" avpl
1181 * @op: the "op" avpl that will be matched against the src avpl
1182 * @copy_avps: whether the avps in the resulting avpl should be copied
1184 * creates an avp list containing every avp in src matching every avp in op
1185 * it will not create a list unless every avp in op is matched only once
1186 * to every avp in op.
1188 * Return value: a pointer to the newly created avpl containing the
1191 extern AVPL* new_avpl_exact_match(const gchar* name,AVPL* src, AVPL* op, gboolean copy_avps) {
1192 AVPL* newavpl = new_avpl(name);
1199 #ifdef _AVP_DEBUGGING
1200 dbg_print(dbg_avpl_op,3,dbg_fp,"new_avpl_every_match: %X src=%X op=%X name='%s'",newavpl,src,op,name);
1206 if (src->len == 0) {
1207 delete_avpl(newavpl,FALSE);
1211 cs = src->null.next;
1215 c = (guint) co->avp->n - (guint) cs->avp->n;
1218 delete_avpl(newavpl,TRUE);
1223 delete_avpl(newavpl,TRUE);
1227 m = match_avp(cs->avp,co->avp);
1235 if ( ! insert_avp(newavpl,copy) ) {
1239 insert_avp(newavpl,m);
1247 delete_avpl(newavpl,TRUE);
1251 delete_avpl(newavpl,TRUE);
1258 /* should never be reached */
1262 extern AVPL* new_avpl_from_match(avpl_match_mode mode, const gchar* name,AVPL* src, AVPL* op, gboolean copy_avps) {
1267 avpl = new_avpl_exact_match(name,src,op,copy_avps);
1270 avpl = new_avpl_loose_match(name,src,op,copy_avps);
1273 avpl = new_avpl_every_match(name,src,op,copy_avps);
1276 avpl = new_avpl_from_avpl(name,src,copy_avps);
1277 merge_avpl(avpl, op, copy_avps);
1285 * new_avpl_transform:
1287 * creates an empty avpl transformation
1289 * Return value: a pointer to the newly created avpl transformation
1291 static AVPL_Transf* new_avpl_transform(gchar* name, AVPL* mixed, avpl_match_mode match_mode, avpl_replace_mode replace_mode) {
1292 AVPL_Transf* t = g_malloc(sizeof(AVPL_Transf));
1295 t->name = g_strdup(name);
1296 t->match = new_avpl("match");
1297 t->replace = new_avpl("replace");
1298 t->match_mode = match_mode;
1299 t->replace_mode = replace_mode;
1303 while (( avp = extract_first_avp(mixed) )) {
1304 if (*(avp->n) == '.') {
1305 rename_avp(avp,((avp->n)+1));
1306 insert_avp(t->replace, avp);
1308 insert_avp(t->match, avp);
1317 * delete_avpl_transform:
1318 * @it: a pointer to the avpl transformation object
1320 * Destroys an avpl transformation object and releases all the resources it
1324 extern void delete_avpl_transform(AVPL_Transf* op) {
1327 for (; op ; op = next) {
1333 delete_avpl(op->match,TRUE);
1337 delete_avpl(op->replace,TRUE);
1348 * @src: the source avpl for the transform operation.
1349 * @op: a pointer to the avpl transformation object to apply.
1351 * Applies the "op" transformation to an avpl, matches it and eventually
1352 * replaces or inserts the transformed avps.
1354 * Return value: whether the transformation was performed or not.
1356 extern void avpl_transform(AVPL* src, AVPL_Transf* op) {
1362 #ifdef _AVP_DEBUGGING
1363 dbg_print(dbg_avpl_op,3,dbg_fp,"avpl_transform: src=%X op=%X",src,op);
1366 for ( ; op ; op = op->next) {
1368 avpl = new_avpl_from_match(op->match_mode, src->name,src, op->match, TRUE);
1371 switch (op->replace_mode) {
1372 case AVPL_NO_REPLACE:
1373 delete_avpl(avpl,TRUE);
1376 merge_avpl(src,op->replace,TRUE);
1377 delete_avpl(avpl,TRUE);
1380 cs = src->null.next;
1381 cm = avpl->null.next;
1383 if (cm->avp && cs->avp->n == cm->avp->n && cs->avp->v == cm->avp->v) {
1386 cs->prev->next = cs->next;
1387 cs->next->prev = cs->prev;
1388 g_mem_chunk_free(avp_chunk,cs);
1397 merge_avpl(src,op->replace,TRUE);
1398 delete_avpl(avpl,TRUE);
1408 * @name: the name the loal will take.
1410 * Creates an empty list of avp lists.
1412 * Return value: a pointer to the newly created loal.
1414 extern LoAL* new_loal(const gchar* name) {
1415 LoAL* new_loal = g_mem_chunk_alloc(avp_chunk);
1421 #ifdef _AVP_DEBUGGING
1422 dbg_print(dbg_avpl_op,3,dbg_fp,"new_loal: %X name=%s",new_loal,name);
1425 new_loal->name = scs_subscribe(avp_strings,name);
1426 new_loal->null.avpl = NULL;
1427 new_loal->null.next = &new_loal->null;
1428 new_loal->null.prev = &new_loal->null;
1435 * @loal: the loal on which to operate.
1436 * @avpl: the avpl to append.
1438 * Appends an avpl to a loal.
1441 extern void loal_append(LoAL* loal, AVPL* avpl) {
1442 LoALnode* node = g_mem_chunk_alloc(avp_chunk);
1444 #ifdef _AVP_DEBUGGING
1445 dbg_print(dbg_avpl_op,3,dbg_fp,"new_loal_node: %X",node);
1449 node->next = &loal->null;
1450 node->prev = loal->null.prev;
1452 loal->null.prev->next = node;
1453 loal->null.prev = node;
1459 * extract_first_avpl:
1460 * @loal: the loal on which to operate.
1462 * Extracts the first avpl contained in a loal.
1464 * Return value: a pointer to the extracted avpl.
1467 extern AVPL* extract_first_avpl(LoAL* loal) {
1471 #ifdef _AVP_DEBUGGING
1472 dbg_print(dbg_avpl_op,3,dbg_fp,"extract_first_avpl: from: %s",loal->name);
1475 node = loal->null.next;
1477 loal->null.next->next->prev = &loal->null;
1478 loal->null.next = node->next;
1485 g_mem_chunk_free(avp_chunk,node);
1487 #ifdef _AVP_DEBUGGING
1488 dbg_print(dbg_avpl_op,3,dbg_fp,"extract_first_avpl: got %s",avpl->name);
1489 dbg_print(dbg_avpl_op,3,dbg_fp,"delete_loal_node: %X",node);
1497 * extract_first_avpl:
1498 * @loal: the loal on which to operate.
1500 * Extracts the last avpl contained in a loal.
1502 * Return value: a pointer to the extracted avpl.
1505 extern AVPL* extract_last_avpl(LoAL* loal){
1509 node = loal->null.prev;
1511 loal->null.prev->prev->next = &loal->null;
1512 loal->null.prev = node->prev;
1519 g_mem_chunk_free(avp_chunk,node);
1520 #ifdef _AVP_DEBUGGING
1521 dbg_print(dbg_avpl_op,3,dbg_fp,"delete_loal_node: %X",node);
1529 * extract_first_avpl:
1530 * @loal: the loal on which to operate.
1531 * @cookie pointer to the pointer variable to contain the state between calls
1533 * At each call will return the following avpl from a loal. The given cookie
1534 * will be used to manatain the state between calls.
1536 * Return value: a pointer to the next avpl.
1539 extern AVPL* get_next_avpl(LoAL* loal,void** cookie) {
1542 #ifdef _AVP_DEBUGGING
1543 dbg_print(dbg_avpl_op,3,dbg_fp,"get_next_avpl: loal=%X node=%X",loal,*cookie);
1547 node = (LoALnode*) *cookie;
1549 node = loal->null.next;
1552 *cookie = node->next;
1559 * @loal: the loal to be deleted.
1560 * @avpls_too: whether avpls contained by the loal should be deleted as well
1561 * @avps_too: whether avps contained by the avpls should be also deleted
1563 * Destroys a loal and eventually desstroys avpls and avps.
1566 extern void delete_loal(LoAL* loal, gboolean avpls_too, gboolean avps_too) {
1569 #ifdef _AVP_DEBUGGING
1570 dbg_print(dbg_avpl_op,3,dbg_fp,"delete_loal: %X",loal);
1573 while(( avpl = extract_last_avpl(loal) )) {
1575 delete_avpl(avpl,avps_too);
1579 scs_unsubscribe(avp_strings,loal->name);
1580 g_mem_chunk_free(avp_chunk,loal);
1585 /****************************************************************************
1586 ******************* the following are used in load_loal_from_file
1587 ****************************************************************************/
1591 * Used by loal_from_file to handle errors while loading.
1593 static LoAL* load_loal_error(FILE* fp, LoAL* loal, AVPL* curr, int linenum, const gchar* fmt, ...) {
1599 va_start( list, fmt );
1600 desc = g_strdup_vprintf(fmt, list);
1604 err = g_strdup_printf("Error Loading LoAL from file: in %s at line: %i, %s",loal->name,linenum,desc);
1605 ret = new_loal(err);
1611 if (loal) delete_loal(loal,TRUE,TRUE);
1612 if (curr) delete_avpl(curr,TRUE);
1618 /* the maximum length allowed for a line */
1619 #define MAX_ITEM_LEN 8192
1621 /* this two ugly things are used for tokenizing */
1622 #define AVP_OP_CHAR '=': case '^': case '$': case '~': case '<': case '>': case '?': case '|': case '&' : case '!'
1624 #define AVP_NAME_CHAR 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J':\
1625 case 'K': case 'L': case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T':\
1626 case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z': case 'a': case 'b': case 'c': case 'd':\
1627 case 'e': case 'f': case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':\
1628 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': case 'v': case 'w': case 'x':\
1629 case 'y': case 'z': case '_': case '0': case '1': case '2': case '3': case '4': case '5': case '6':\
1630 case '7': case '8': case '9': case '.'
1635 * @filename: the file containing a loals text representation.
1637 * Given a filename it will attempt to load a loal containing a copy of
1638 * the avpls represented in the file.
1640 * Return value: if successful a pointer to the new populated loal, else NULL.
1643 extern LoAL* loal_from_file(gchar* filename) {
1647 guint32 linenum = 1;
1648 gchar linenum_buf[MAX_ITEM_LEN];
1649 gchar name[MAX_ITEM_LEN];
1650 gchar value[MAX_ITEM_LEN];
1652 LoAL *loal = new_loal(filename);
1656 enum _load_loal_states {
1666 return load_loal_error(fp,loal,curr,linenum,"MATE Will not run as root");
1672 if (( fp = eth_fopen(filename,"r") )) {
1673 while(( c = (gchar) fgetc(fp) )){
1677 report_read_failure(filename,errno);
1678 return load_loal_error(fp,loal,curr,linenum,"Error while reading '%f'",filename);
1687 if ( i >= MAX_ITEM_LEN - 1 ) {
1688 return load_loal_error(fp,loal,curr,linenum,"Maximum item length exceeded");
1704 case ' ': case '\t':
1705 /* ignore whitespace at line start */
1708 /* ignore empty lines */
1716 g_snprintf(linenum_buf,sizeof(linenum_buf),"%s:%u",filename,linenum);
1717 curr = new_avpl(linenum_buf);
1723 return load_loal_error(fp,loal,curr,linenum,"expecting name got: '%c'",c);
1731 if (c != '\n') ungetc(c,fp);
1743 loal_append(loal,curr);
1747 return load_loal_error(fp,loal,curr,linenum,"expecting name got: '%c'",c);
1752 state = BEFORE_NAME;
1759 avp = new_avp(name,value,op);
1761 if (! insert_avp(curr,avp) ) {
1776 return load_loal_error(fp,loal,curr,linenum,"operator expected found new line");
1778 return load_loal_error(fp,loal,curr,linenum,"name or match operator expected found '%c'",c);
1783 value[i++] = fgetc(fp);
1786 state = BEFORE_NAME;
1791 avp = new_avp(name,value,op);
1793 if (! insert_avp(curr,avp) ) {
1798 return load_loal_error(fp,loal,curr,linenum,"';' expected found new line");
1810 report_open_failure(filename,errno,FALSE);
1811 return load_loal_error(NULL,loal,NULL,0,"Cannot Open file '%s'",filename);