core: fix all reported doxygen errors in PV and value evaluation code
[sip-router] / rvalue.c
1 /* 
2  * Copyright (C) 2008 iptelorg GmbH
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 /**
18  * @file 
19  * @brief SIP-router core :: rvalue expressions
20  * @ingroup core
21  * Module: \ref core
22  */
23
24 /* 
25  * History:
26  * --------
27  *  2008-12-01  initial version (andrei)
28  *  2009-04-24  added support for defined, strempty, strlen (andrei)
29  *  2009-04-28  int and str automatic conversions: (int)undef=0,
30  *               (str)undef="", (int)""=0, (int)"123"=123, (int)"abc"=0
31  *              handle undef == expr, in function of the UNDEF_EQ_* defines.
32  *              (andrei)
33  *  2009-05-05  casts operator for int & string (andrei)
34  */
35
36 /* special defines:
37  *
38  *  UNDEF_EQ_* - how to behave when undef is on the right side of a generic
39  *               compare operator
40  *  UNDEF_EQ_ALWAYS_FALSE:  undef  == something  is always false
41  *  UNDEF_EQ_UNDEF_TRUE  :  undef == something false except for undef==undef
42  *                          which is true
43  *  no UNDEF_EQ* define  :  undef == expr => convert undef to typeof(expr)
44  *                          and perform normal ==. undef == undef will be
45  *                          converted to string and it will be true
46  *                          ("" == "")
47  * NOTE: expr == undef, with defined(expr) is always evaluated this way:
48          expr == (type_of(expr))undef
49  *  RV_STR2INT_VERBOSE_ERR - if a string conversion to int fails, log (L_WARN)
50  *                           the string that caused it (only the string, not
51  *                           the expression position).
52  *  RV_STR2INT_ERR         - if a string conversion to int fails, don't ignore
53  *                           the error (return error).
54  *  RVAL_GET_INT_ERR_WARN  - if a conversion to int fails, log a warning with
55  *                           the expression position.
56  *                           Depends on RV_STR2INT_ERR.
57  *  RVAL_GET_INT_ERR_IGN   - if a conversion to int fails, ignore the error
58  *                           (the result will be 0). Can be combined with
59  *                           RVAL_GET_INT_ERR_WARN.
60  *                           Depends on RV_STR2INT_ERR.
61  */
62
63
64 #include "rvalue.h"
65
66 #include <stdlib.h> /* abort() */
67
68 /* if defined warn when str2int conversions fail */
69 #define RV_STR2INT_VERBOSE_ERR
70
71 /* if defined rval_get_int will fail if str2int conversion fail
72    (else convert to 0) */
73 #define RV_STR2INT_ERR
74
75 /* if a rval_get_int fails (conversion to int), warn
76    Depends on RV_STR2INT_ERR.
77  */
78 #define RVAL_GET_INT_ERR_WARN
79
80 /* if a rval_get_int fails, ignore it (expression evaluation will not fail,
81    the int conversion will result in 0).
82    Can be combined with RVAL_GET_INT_ERR_WARN.
83    Depends on RV_STR2INT_ERR.
84  */
85 #define RVAL_GET_INT_ERR_IGN
86
87 /* minimum size alloc'ed for STR RVs (to accomodate
88  * strops without reallocs) */
89 #define RV_STR_EXTRA 80
90
91 #define rv_ref(rv) ((rv)->refcnt++)
92
93 /** unref rv and returns true if 0 */
94 #define rv_unref(rv) ((--(rv)->refcnt)==0)
95
96
97 inline static void rval_force_clean(struct rvalue* rv)
98 {
99         if (rv->flags & RV_CNT_ALLOCED_F){
100                 switch(rv->type){
101                         case RV_STR:
102                                 pkg_free(rv->v.s.s);
103                                 rv->v.s.s=0;
104                                 rv->v.s.len=0;
105                                 break;
106                         default:
107                                 BUG("RV_CNT_ALLOCED_F not supported for type %d\n", rv->type);
108                 }
109                 rv->flags&=~RV_CNT_ALLOCED_F;
110         }
111         if (rv->flags & RV_RE_ALLOCED_F){
112                 if (rv->v.re.regex){
113                         if (unlikely(rv->type!=RV_STR || !(rv->flags & RV_RE_F))){
114                                 BUG("RV_RE_ALLOCED_F not supported for type %d or "
115                                                 "bad flags %x\n", rv->type, rv->flags);
116                         }
117                         regfree(rv->v.re.regex);
118                         pkg_free(rv->v.re.regex);
119                         rv->v.re.regex=0;
120                 }
121                 rv->flags&=~(RV_RE_ALLOCED_F|RV_RE_F);
122         }
123 }
124
125
126
127 /** frees a rval returned by rval_new(), rval_convert() or rval_expr_eval().
128  *   Note: it will be freed only when refcnt reaches 0
129  */
130 void rval_destroy(struct rvalue* rv)
131 {
132         if (rv && rv_unref(rv)){
133                 rval_force_clean(rv);
134                 /* still an un-regfreed RE ? */
135                 if ((rv->flags & RV_RE_F) && rv->v.re.regex){
136                         if (unlikely(rv->type!=RV_STR))
137                                 BUG("RV_RE_F not supported for type %d\n", rv->type);
138                         regfree(rv->v.re.regex);
139                 }
140                 if (rv->flags & RV_RV_ALLOCED_F){
141                         pkg_free(rv);
142                 }
143         }
144 }
145
146
147
148 void rval_clean(struct rvalue* rv)
149 {
150         if (rv_unref(rv))
151                 rval_force_clean(rv);
152 }
153
154
155
156 void rve_destroy(struct rval_expr* rve)
157 {
158         if (rve){
159                 if (rve->op==RVE_RVAL_OP){
160                         if (rve->left.rval.refcnt){
161                                 if (rve->left.rval.refcnt==1)
162                                         rval_destroy(&rve->left.rval);
163                                 else
164                                         BUG("rval expr rval with invalid refcnt: %d (%d,%d-%d,%d)"
165                                                         "\n", rve->left.rval.refcnt,
166                                                         rve->fpos.s_line, rve->fpos.s_col,
167                                                         rve->fpos.e_line, rve->fpos.e_col);
168                         }
169                         if (rve->right.rval.refcnt){
170                                 if (rve->right.rval.refcnt==1)
171                                         rval_destroy(&rve->right.rval);
172                                 else
173                                         BUG("rval expr rval with invalid refcnt: %d (%d,%d-%d,%d)"
174                                                         "\n", rve->right.rval.refcnt,
175                                                         rve->fpos.s_line, rve->fpos.s_col,
176                                                         rve->fpos.e_line, rve->fpos.e_col);
177                         }
178                 }else{
179                         if (rve->left.rve)
180                                 rve_destroy(rve->left.rve);
181                         if (rve->right.rve)
182                                 rve_destroy(rve->right.rve);
183                 }
184                 pkg_free(rve);
185         }
186 }
187
188
189
190 void rval_cache_clean(struct rval_cache* rvc)
191 {
192         if ((rvc->cache_type==RV_CACHE_PVAR) && (rvc->val_type!=RV_NONE)){
193                 pv_value_destroy(&rvc->c.pval);
194         }
195         rvc->cache_type=RV_CACHE_EMPTY;
196         rvc->val_type=RV_NONE;
197 }
198
199
200 #define rv_chg_in_place(rv)  ((rv)->refcnt==1)
201
202
203
204 /** init a rvalue structure.
205  * Note: not needed if the structure is allocate with one of the 
206  * rval_new* functions
207  */
208 void rval_init(struct rvalue* rv, enum rval_type t, union rval_val* v, 
209                                 int flags)
210 {
211         rv->flags=flags;
212         rv->refcnt=1;
213         rv->type=t;
214         if (v){
215                 rv->v=*v;
216         }else{
217                 memset (&rv->v, 0, sizeof(rv->v));
218         }
219 }
220
221
222
223 /** create a new pk_malloc'ed empty rvalue.
224   *
225   * @param extra_size - extra space to allocate
226   *                    (e.g.: so that future string operation can reuse
227   *                     the space)
228   * @return new rv or 0 on error
229   */
230 struct rvalue* rval_new_empty(int extra_size)
231 {
232         struct rvalue* rv;
233         int size; /* extra size at the end */
234         
235         size=ROUND_LONG(sizeof(*rv)-sizeof(rv->buf)+extra_size); /* round up */
236         rv=pkg_malloc(size);
237         if (likely(rv)){
238                 rv->bsize=size-sizeof(*rv)-sizeof(rv->buf); /* remaining size->buffer*/
239                 rv->flags=RV_RV_ALLOCED_F;
240                 rv->refcnt=1;
241                 rv->type=RV_NONE;
242         }
243         return rv;
244 }
245
246
247
248 /** create a new pk_malloc'ed rv from a str.
249   *
250   * @param s - pointer to str, must be non-null
251   * @param extra_size - extra space to allocate
252   *                    (so that future string operation can reuse
253   *                     the space)
254   * @return new rv or 0 on error
255   */
256 struct rvalue* rval_new_str(str* s, int extra_size)
257 {
258         struct rvalue* rv;
259         
260         rv=rval_new_empty(extra_size+s->len+1/* 0 term */);
261         if (likely(rv)){
262                 rv->type=RV_STR;
263                 rv->v.s.s=&rv->buf[0];
264                 rv->v.s.len=s->len;
265                 memcpy(rv->v.s.s, s->s, s->len);
266                 rv->v.s.s[s->len]=0;
267         }
268         return rv;
269 }
270
271
272
273 /** create a new pk_malloc'ed RE rv from a str re.
274   * It acts as rval_new_str, but also compiles a RE from the str
275   * and sets v->re.regex.
276   * @param s - pointer to str, must be non-null, zero-term'ed and a valid RE.
277   * @return new rv or 0 on error
278   */
279 struct rvalue* rval_new_re(str* s)
280 {
281         struct rvalue* rv;
282         long offs;
283         
284         offs=(long)&((struct rvalue*)0)->buf[0]; /* offset of the buf. member */
285         /* make sure we reserve enough space so that we can satisfy any regex_t
286            alignment requirement (pointer) */
287         rv=rval_new_empty(ROUND_POINTER(offs)-offs+sizeof(*rv->v.re.regex)+
288                                                 s->len+1/* 0 */);
289         if (likely(rv)){
290                 rv->type=RV_STR;
291                 /* make sure regex points to a properly aligned address
292                    (use max./pointer alignment to be sure ) */
293                 rv->v.re.regex=(regex_t*)((char*)&rv->buf[0]+ROUND_POINTER(offs)-offs);
294                 rv->v.s.s=(char*)rv->v.re.regex+sizeof(*rv->v.re.regex);
295                 rv->v.s.len=s->len;
296                 memcpy(rv->v.s.s, s->s, s->len);
297                 rv->v.s.s[s->len]=0;
298                 /* compile the regex */
299                 /* same flags as for expr. =~ (fix_expr()) */
300                 if (unlikely(regcomp(rv->v.re.regex, s->s,
301                                                                 REG_EXTENDED|REG_NOSUB|REG_ICASE))){
302                         /* error */
303                         pkg_free(rv);
304                         rv=0;
305                 }else /* success */
306                         rv->flags|=RV_RE_F;
307         }
308         return rv;
309 }
310
311
312
313 /** get string name for a type.
314   *
315   * @return - null terminated name of the type
316   */
317 char* rval_type_name(enum rval_type type)
318 {
319         switch(type){
320                 case RV_NONE:
321                         return "none";
322                 case RV_INT:
323                         return "int";
324                 case RV_STR:
325                         return "str";
326                 case RV_BEXPR:
327                         return "bexpr_t";
328                 case RV_ACTION_ST:
329                         return "action_t";
330                 case RV_PVAR:
331                         return "pvar";
332                 case RV_AVP:
333                         return "avp";
334                         break;
335                 case RV_SEL:
336                         return "select";
337         }
338         return "error_unkown_type";
339 }
340
341
342
343 /**
344  * @brief create a new pk_malloc'ed rvalue from a rval_val union
345  * @param t rvalue type
346  * @param v rvalue value
347  * @param extra_size extra space to allocate
348  * (so that future string operation can reuse the space)
349  * @return new rv or 0 on error
350  */
351 struct rvalue* rval_new(enum rval_type t, union rval_val* v, int extra_size)
352 {
353         struct rvalue* rv;
354         
355         if (t==RV_STR && v && v->s.s)
356                 return rval_new_str(&v->s, extra_size);
357         rv=rval_new_empty(extra_size);
358         if (likely(rv)){
359                 rv->type=t;
360                 if (likely(v && t!=RV_STR)){
361                         rv->v=*v;
362                 }else if (t==RV_STR){
363                         rv->v.s.s=&rv->buf[0];
364                         rv->v.s.len=0;
365                         if (likely(extra_size)) rv->v.s.s[0]=0;
366                 }else
367                         memset (&rv->v, 0, sizeof(rv->v));
368         }
369         return rv;
370 }
371
372
373
374 /**
375  * @brief get rvalue basic type (RV_INT or RV_STR)
376  *
377  * Given a rvalue it tries to determinte its basic type.
378  * Fills val_cache if non-null and empty (can be used in other rval*
379  * function calls, to avoid re-resolving avps or pvars). It must be
380  * rval_cache_clean()'en when no longer needed.
381  *
382  * @param h run action context
383  * @param msg SIP message
384  * @param rv target rvalue
385  * @param val_cache write-only value cache, might be filled if non-null,
386  * it _must_ be rval_cache_clean()'en when done.
387  * @return basic type or RV_NONE on error
388  */
389 inline static enum rval_type rval_get_btype(struct run_act_ctx* h,
390                                                                                         struct sip_msg* msg,
391                                                                                         struct rvalue* rv,
392                                                                                         struct rval_cache* val_cache)
393 {
394         avp_t* r_avp;
395         int_str tmp_avp_val;
396         int_str* avpv;
397         pv_value_t tmp_pval;
398         pv_value_t* pv;
399         enum rval_type tmp;
400         enum rval_type* ptype;
401         
402         switch(rv->type){
403                 case RV_INT:
404                 case RV_STR:
405                         return rv->type;
406                 case RV_BEXPR:
407                 case RV_ACTION_ST:
408                         return RV_INT;
409                 case RV_PVAR:
410                         if (likely(val_cache && val_cache->cache_type==RV_CACHE_EMPTY)){
411                                 pv=&val_cache->c.pval;
412                                 val_cache->cache_type=RV_CACHE_PVAR;
413                         }else{
414                                 val_cache=0;
415                                 pv=&tmp_pval;
416                         }
417                         memset(pv, 0, sizeof(tmp_pval));
418                         if (likely(pv_get_spec_value(msg, &rv->v.pvs, pv)==0)){
419                                 if (pv->flags & PV_TYPE_INT){
420                                         if (likely(val_cache!=0))
421                                                 val_cache->val_type=RV_INT;
422                                         else
423                                                 pv_value_destroy(pv);
424                                         return RV_INT;
425                                 }else if (pv->flags & PV_VAL_STR){
426                                         if (likely(val_cache!=0))
427                                                 val_cache->val_type=RV_STR;
428                                         else
429                                                 pv_value_destroy(pv);
430                                         return RV_STR;
431                                 }else{
432                                         pv_value_destroy(pv);
433                                         if (likely(val_cache!=0))
434                                                 val_cache->val_type=RV_NONE; /* undefined */
435                                         goto error;
436                                 }
437                         }else{
438                                 if (likely(val_cache!=0))
439                                         val_cache->val_type=RV_NONE; /* undefined */
440                                 goto error;
441                         }
442                         break;
443                 case RV_AVP:
444                         if (likely(val_cache && val_cache->cache_type==RV_CACHE_EMPTY)){
445                                 ptype=&val_cache->val_type;
446                                 avpv=&val_cache->c.avp_val;
447                                 val_cache->cache_type=RV_CACHE_AVP;
448                         }else{
449                                 ptype=&tmp;
450                                 avpv=&tmp_avp_val;
451                         }
452                         r_avp = search_avp_by_index(rv->v.avps.type, rv->v.avps.name,
453                                                                                         avpv, rv->v.avps.index);
454                         if (likely(r_avp)){
455                                 if (r_avp->flags & AVP_VAL_STR){
456                                         *ptype=RV_STR;
457                                         return RV_STR;
458                                 }else{
459                                         *ptype=RV_INT;
460                                         return RV_INT;
461                                 }
462                         }else{
463                                 *ptype=RV_NONE;
464                                 goto error;
465                         }
466                         break;
467                 case RV_SEL:
468                         return RV_STR;
469                 default:
470                         BUG("rv type %d not handled\n", rv->type);
471         }
472 error:
473         return RV_NONE;
474 }
475
476
477
478 /** guess the type of an expression.
479   * @return RV_INT, RV_STR or RV_NONE (when type could not be found,
480   * e.g. avp or pvar)
481   */
482 enum rval_type rve_guess_type( struct rval_expr* rve)
483 {
484         switch(rve->op){
485                 case RVE_RVAL_OP:
486                         switch(rve->left.rval.type){
487                                 case RV_STR:
488                                 case RV_SEL:
489                                         return RV_STR;
490                                 case RV_INT:
491                                 case RV_BEXPR:
492                                 case RV_ACTION_ST:
493                                         return RV_INT;
494                                 case RV_PVAR:
495                                 case RV_AVP:
496                                 case RV_NONE:
497                                         return RV_NONE;
498                         }
499                         break;
500                 case RVE_UMINUS_OP:
501                 case RVE_BOOL_OP:
502                 case RVE_LNOT_OP:
503                 case RVE_BNOT_OP:
504                 case RVE_MINUS_OP:
505                 case RVE_MUL_OP:
506                 case RVE_DIV_OP:
507                 case RVE_MOD_OP:
508                 case RVE_BOR_OP:
509                 case RVE_BAND_OP:
510                 case RVE_BXOR_OP:
511                 case RVE_BLSHIFT_OP:
512                 case RVE_BRSHIFT_OP:
513                 case RVE_LAND_OP:
514                 case RVE_LOR_OP:
515                 case RVE_GT_OP:
516                 case RVE_GTE_OP:
517                 case RVE_LT_OP:
518                 case RVE_LTE_OP:
519                 case RVE_EQ_OP:
520                 case RVE_DIFF_OP:
521                 case RVE_IEQ_OP:
522                 case RVE_IDIFF_OP:
523                 case RVE_STREQ_OP:
524                 case RVE_STRDIFF_OP:
525                 case RVE_MATCH_OP:
526                 case RVE_IPLUS_OP:
527                 case RVE_STRLEN_OP:
528                 case RVE_STREMPTY_OP:
529                 case RVE_DEFINED_OP:
530                 case RVE_INT_OP:
531                         return RV_INT;
532                 case RVE_PLUS_OP:
533                         /* '+' evaluates to the type of the left operand */
534                         return rve_guess_type(rve->left.rve);
535                 case RVE_CONCAT_OP:
536                 case RVE_STR_OP:
537                         return RV_STR;
538                 case RVE_NONE_OP:
539                         break;
540         }
541         return RV_NONE;
542 }
543
544
545
546 /** returns true if expression is constant.
547   * @return 0 or 1 on
548   *  non constant type
549   */
550 int rve_is_constant(struct rval_expr* rve)
551 {
552         switch(rve->op){
553                 case RVE_RVAL_OP:
554                         switch(rve->left.rval.type){
555                                 case RV_STR:
556                                         return 1;
557                                 case RV_INT:
558                                         return 1;
559                                 case RV_SEL:
560                                 case RV_BEXPR:
561                                 case RV_ACTION_ST:
562                                 case RV_PVAR:
563                                 case RV_AVP:
564                                 case RV_NONE:
565                                         return 0;
566                         }
567                         break;
568                 case RVE_UMINUS_OP:
569                 case RVE_BOOL_OP:
570                 case RVE_LNOT_OP:
571                 case RVE_BNOT_OP:
572                 case RVE_STRLEN_OP:
573                 case RVE_STREMPTY_OP:
574                 case RVE_DEFINED_OP:
575                 case RVE_INT_OP:
576                 case RVE_STR_OP:
577                         return rve_is_constant(rve->left.rve);
578                 case RVE_MINUS_OP:
579                 case RVE_MUL_OP:
580                 case RVE_DIV_OP:
581                 case RVE_MOD_OP:
582                 case RVE_BOR_OP:
583                 case RVE_BAND_OP:
584                 case RVE_BXOR_OP:
585                 case RVE_BLSHIFT_OP:
586                 case RVE_BRSHIFT_OP:
587                 case RVE_LAND_OP:
588                 case RVE_LOR_OP:
589                 case RVE_GT_OP:
590                 case RVE_GTE_OP:
591                 case RVE_LT_OP:
592                 case RVE_LTE_OP:
593                 case RVE_EQ_OP:
594                 case RVE_DIFF_OP:
595                 case RVE_IEQ_OP:
596                 case RVE_IDIFF_OP:
597                 case RVE_STREQ_OP:
598                 case RVE_STRDIFF_OP:
599                 case RVE_MATCH_OP:
600                 case RVE_PLUS_OP:
601                 case RVE_IPLUS_OP:
602                 case RVE_CONCAT_OP:
603                         return rve_is_constant(rve->left.rve) &&
604                                         rve_is_constant(rve->right.rve);
605                 case RVE_NONE_OP:
606                         break;
607         }
608         return 0;
609 }
610
611
612
613 /** returns true if the expression has side-effects.
614   * @return  1 for possible side-effects, 0 for no side-effects
615   * TODO: add better checks
616   */
617 int rve_has_side_effects(struct rval_expr* rve)
618 {
619         return !rve_is_constant(rve);
620 }
621
622
623
624 /** returns true if operator is unary (takes only 1 arg).
625   * @return 0 or 1 on
626   */
627 static int rve_op_unary(enum rval_expr_op op)
628 {
629         switch(op){
630                 case RVE_RVAL_OP: /* not realy an operator */
631                         return -1;
632                 case RVE_UMINUS_OP:
633                 case RVE_BOOL_OP:
634                 case RVE_LNOT_OP:
635                 case RVE_BNOT_OP:
636                 case RVE_STRLEN_OP:
637                 case RVE_STREMPTY_OP:
638                 case RVE_DEFINED_OP:
639                 case RVE_INT_OP:
640                 case RVE_STR_OP:
641                         return 1;
642                 case RVE_MINUS_OP:
643                 case RVE_MUL_OP:
644                 case RVE_DIV_OP:
645                 case RVE_MOD_OP:
646                 case RVE_BOR_OP:
647                 case RVE_BAND_OP:
648                 case RVE_BXOR_OP:
649                 case RVE_BLSHIFT_OP:
650                 case RVE_BRSHIFT_OP:
651                 case RVE_LAND_OP:
652                 case RVE_LOR_OP:
653                 case RVE_GT_OP:
654                 case RVE_GTE_OP:
655                 case RVE_LT_OP:
656                 case RVE_LTE_OP:
657                 case RVE_EQ_OP:
658                 case RVE_DIFF_OP:
659                 case RVE_IEQ_OP:
660                 case RVE_IDIFF_OP:
661                 case RVE_STREQ_OP:
662                 case RVE_STRDIFF_OP:
663                 case RVE_MATCH_OP:
664                 case RVE_PLUS_OP:
665                 case RVE_IPLUS_OP:
666                 case RVE_CONCAT_OP:
667                         return 0;
668                 case RVE_NONE_OP:
669                         return -1;
670                         break;
671         }
672         return 0;
673 }
674
675
676
677 /**
678  * @brief Returns 1 if expression is valid (type-wise)
679  * @param type filled with the type of the expression (RV_INT, RV_STR or
680  *                RV_NONE if it's dynamic)
681  * @param rve  checked expression
682  * @param bad_rve set on failure to the subexpression for which the 
683  * type check failed
684  * @param bad_t set on failure to the type of the bad subexpression
685  * @param exp_t set on failure to the expected type for the bad
686  * subexpression
687  * @return 0 or 1 and sets *type to the resulting type
688  * (RV_INT, RV_STR or RV_NONE if it can be found only at runtime)
689  */
690 int rve_check_type(enum rval_type* type, struct rval_expr* rve,
691                                         struct rval_expr** bad_rve, 
692                                         enum rval_type* bad_t,
693                                         enum rval_type* exp_t)
694 {
695         enum rval_type type1, type2;
696         
697         switch(rve->op){
698                 case RVE_RVAL_OP:
699                         *type=rve_guess_type(rve);
700                         return 1;
701                 case RVE_UMINUS_OP:
702                 case RVE_BOOL_OP:
703                 case RVE_LNOT_OP:
704                 case RVE_BNOT_OP:
705                         *type=RV_INT;
706                         if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
707                                 if (type1==RV_STR){
708                                         if (bad_rve) *bad_rve=rve->left.rve;
709                                         if (bad_t) *bad_t=type1;
710                                         if (exp_t) *exp_t=RV_INT;
711                                         return 0;
712                                 }
713                                 return 1;
714                         }
715                         return 0;
716                         break;
717                 case RVE_MINUS_OP:
718                 case RVE_MUL_OP:
719                 case RVE_DIV_OP:
720                 case RVE_MOD_OP:
721                 case RVE_BOR_OP:
722                 case RVE_BAND_OP:
723                 case RVE_BXOR_OP:
724                 case RVE_BLSHIFT_OP:
725                 case RVE_BRSHIFT_OP:
726                 case RVE_LAND_OP:
727                 case RVE_LOR_OP:
728                 case RVE_GT_OP:
729                 case RVE_GTE_OP:
730                 case RVE_LT_OP:
731                 case RVE_LTE_OP:
732                 case RVE_IEQ_OP:
733                 case RVE_IDIFF_OP:
734                 case RVE_IPLUS_OP:
735                         *type=RV_INT;
736                         if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
737                                 if (type1==RV_STR){
738                                         if (bad_rve) *bad_rve=rve->left.rve;
739                                         if (bad_t) *bad_t=type1;
740                                         if (exp_t) *exp_t=RV_INT;
741                                         return 0;
742                                 }
743                                 if (rve_check_type(&type2, rve->right.rve, bad_rve,
744                                                                         bad_t, exp_t)){
745                                         if (type2==RV_STR){
746                                                 if (bad_rve) *bad_rve=rve->right.rve;
747                                                 if (bad_t) *bad_t=type2;
748                                                 if (exp_t) *exp_t=RV_INT;
749                                                 return 0;
750                                         }
751                                         return 1;
752                                 }
753                         }
754                         return 0;
755                 case RVE_EQ_OP:
756                 case RVE_DIFF_OP:
757                         *type=RV_INT;
758                         if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
759                                 if (rve_check_type(&type2, rve->right.rve, bad_rve, bad_t,
760                                                                                 exp_t)){
761                                         if ((type2!=type1) && (type1!=RV_NONE) &&
762                                                         (type2!=RV_NONE) && 
763                                                         !(type1==RV_STR && type2==RV_INT)){
764                                                 if (bad_rve) *bad_rve=rve->right.rve;
765                                                 if (bad_t) *bad_t=type2;
766                                                 if (exp_t) *exp_t=type1;
767                                                 return 0;
768                                         }
769                                         return 1;
770                                 }
771                         }
772                         return 0;
773                 case RVE_PLUS_OP:
774                         *type=RV_NONE;
775                         if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
776                                 if (rve_check_type(&type2, rve->right.rve, bad_rve, bad_t,
777                                                                         exp_t)){
778                                         if ((type2!=type1) && (type1!=RV_NONE) &&
779                                                         (type2!=RV_NONE) && 
780                                                         !(type1==RV_STR && type2==RV_INT)){
781                                                 if (bad_rve) *bad_rve=rve->right.rve;
782                                                 if (bad_t) *bad_t=type2;
783                                                 if (exp_t) *exp_t=type1;
784                                                 return 0;
785                                         }
786                                         *type=type1;
787                                         return 1;
788                                 }
789                         }
790                         break;
791                 case RVE_CONCAT_OP:
792                         *type=RV_STR;
793                         if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
794                                 if (rve_check_type(&type2, rve->right.rve, bad_rve, bad_t,
795                                                                         exp_t)){
796                                         if ((type2!=type1) && (type1!=RV_NONE) &&
797                                                         (type2!=RV_NONE) && 
798                                                         !(type1==RV_STR && type2==RV_INT)){
799                                                 if (bad_rve) *bad_rve=rve->right.rve;
800                                                 if (bad_t) *bad_t=type2;
801                                                 if (exp_t) *exp_t=type1;
802                                                 return 0;
803                                         }
804                                         if (type1==RV_INT){
805                                                 if (bad_rve) *bad_rve=rve->left.rve;
806                                                 if (bad_t) *bad_t=type1;
807                                                 if (exp_t) *exp_t=RV_STR;
808                                                 return 0;
809                                         }
810                                         return 1;
811                                 }
812                         }
813                         break;
814                 case RVE_STREQ_OP:
815                 case RVE_STRDIFF_OP:
816                 case RVE_MATCH_OP:
817                         *type=RV_INT;
818                         if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
819                                 if (rve_check_type(&type2, rve->right.rve, bad_rve, bad_t,
820                                                                         exp_t)){
821                                         if ((type2!=type1) && (type1!=RV_NONE) &&
822                                                         (type2!=RV_NONE) &&
823                                                         !(type1==RV_STR && type2==RV_INT)){
824                                                 if (bad_rve) *bad_rve=rve->right.rve;
825                                                 if (bad_t) *bad_t=type2;
826                                                 if (exp_t) *exp_t=type1;
827                                                 return 0;
828                                         }
829                                         if (type1==RV_INT){
830                                                 if (bad_rve) *bad_rve=rve->left.rve;
831                                                 if (bad_t) *bad_t=type1;
832                                                 if (exp_t) *exp_t=RV_STR;
833                                                 return 0;
834                                         }
835                                         return 1;
836                                 }
837                         }
838                         break;
839                 case RVE_STRLEN_OP:
840                 case RVE_STREMPTY_OP:
841                 case RVE_DEFINED_OP:
842                         *type=RV_INT;
843                         if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
844                                 if (type1==RV_INT){
845                                         if (bad_rve) *bad_rve=rve->left.rve;
846                                         if (bad_t) *bad_t=type1;
847                                         if (exp_t) *exp_t=RV_STR;
848                                         return 0;
849                                 }
850                                 return 1;
851                         }
852                         break;
853                 case RVE_INT_OP:
854                         *type=RV_INT;
855                         return 1;
856                         break;
857                 case RVE_STR_OP:
858                         *type=RV_STR;
859                         return 1;
860                         break;
861                 case RVE_NONE_OP:
862                 default:
863                         BUG("unexpected rve op %d (%d,%d-%d,%d)\n", rve->op,
864                                         rve->fpos.s_line, rve->fpos.s_col,
865                                         rve->fpos.e_line, rve->fpos.e_col);
866                         if (bad_rve) *bad_rve=rve;
867                         if (bad_t) *bad_t=RV_NONE;
868                         if (exp_t) *exp_t=RV_STR;
869                         break;
870         }
871         return 0;
872 }
873
874
875
876 /** get the integer value of an rvalue.
877   * *i=(int)rv
878   * if rv == undefined select, avp or pvar, return 0.
879   * if an error occurs while evaluating a select, avp or pvar, behave as
880   * for the undefined case (and return success).
881   * @param h - script context handle
882   * @param msg - sip msg
883   * @param i   - pointer to int, where the conversion result will be stored
884   * @param rv   - rvalue to be converted
885   * @param cache - cached rv value (read-only), can be 0
886   *
887   * @return 0 on success, \<0 on error and EXPR_DROP on drop
888  */
889 int rval_get_int(struct run_act_ctx* h, struct sip_msg* msg,
890                                                                 int* i, struct rvalue* rv,
891                                                                 struct rval_cache* cache)
892 {
893         avp_t* r_avp;
894         int_str avp_val;
895         pv_value_t pval;
896         str tmp;
897         str* s;
898         int r, ret;
899         int destroy_pval;
900         
901         destroy_pval=0;
902         s=0;
903         ret=0;
904         switch(rv->type){
905                 case RV_INT:
906                         *i=rv->v.l;
907                         break;
908                 case RV_STR:
909                         s=&rv->v.s;
910                         goto rv_str;
911                 case RV_BEXPR:
912                         *i=eval_expr(h, rv->v.bexpr, msg);
913                         if (*i==EXPR_DROP){
914                                 *i=0; /* false */
915                                 return EXPR_DROP;
916                         }
917                         break;
918                 case RV_ACTION_ST:
919                         if (rv->v.action) {
920                                 *i=(run_actions_safe(h, rv->v.action, msg)>0);
921                                 h->run_flags &= ~(RETURN_R_F|BREAK_R_F); /* catch return &
922                                                                                                                     break in expr*/
923                         } else
924                                 *i=0;
925                         break;
926                 case RV_SEL:
927                         r=run_select(&tmp, &rv->v.sel, msg);
928                         if (unlikely(r!=0)){
929                                 if (r<0)
930                                         goto eval_error;
931                                 else /* i>0  => undefined */
932                                         goto undef;
933                         }
934                         s=&tmp;
935                         goto rv_str;
936                 case RV_AVP:
937                         if (unlikely(cache && cache->cache_type==RV_CACHE_AVP)){
938                                 if (likely(cache->val_type==RV_INT)){
939                                         *i=cache->c.avp_val.n;
940                                 }else if (cache->val_type==RV_STR){
941                                         s=&cache->c.avp_val.s;
942                                         goto rv_str;
943                                 }else if (cache->val_type==RV_NONE)
944                                         goto undef;
945                                 else goto error_cache;
946                         }else{
947                                 r_avp = search_avp_by_index(rv->v.avps.type, rv->v.avps.name,
948                                                                                         &avp_val, rv->v.avps.index);
949                                 if (likely(r_avp)){
950                                         if (unlikely(r_avp->flags & AVP_VAL_STR)){
951                                                 s=&avp_val.s;
952                                                 goto rv_str;
953                                         }else{
954                                                 *i=avp_val.n;
955                                         }
956                                 }else{
957                                         goto undef;
958                                 }
959                         }
960                         break;
961                 case RV_PVAR:
962                         if (unlikely(cache && cache->cache_type==RV_CACHE_PVAR)){
963                                 if (likely((cache->val_type==RV_INT) ||
964                                                                 (cache->c.pval.flags & PV_VAL_INT))){
965                                         *i=cache->c.pval.ri;
966                                 }else if (cache->val_type==RV_STR){
967                                         s=&cache->c.pval.rs;
968                                         goto rv_str;
969                                 }else if (cache->val_type==RV_NONE)
970                                         goto undef;
971                                 else goto error_cache;
972                         }else{
973                                 memset(&pval, 0, sizeof(pval));
974                                 if (likely(pv_get_spec_value(msg, &rv->v.pvs, &pval)==0)){
975                                         if (likely(pval.flags & PV_VAL_INT)){
976                                                 *i=pval.ri;
977                                                 pv_value_destroy(&pval);
978                                         }else if (likely(pval.flags & PV_VAL_STR)){
979                                                 destroy_pval=1; /* we must pv_value_destroy() later*/
980                                                 s=&pval.rs;
981                                                 goto rv_str;
982                                         }else{
983                                                 /* no PV_VAL_STR and no PV_VAL_INT => undef
984                                                    (PV_VAL_NULL) */
985                                                 pv_value_destroy(&pval);
986                                                 goto undef;
987                                         }
988                                 }else{
989                                         goto eval_error;
990                                 }
991                         }
992                         break;
993                 default:
994                         BUG("rv type %d not handled\n", rv->type);
995                         goto error;
996         }
997         return ret;
998 undef:
999 eval_error: /* same as undefined */
1000         /* handle undefined => result 0, return success */
1001         *i=0;
1002         return 0;
1003 rv_str:
1004         /* rv is of string type => try to convert it to int */
1005         /* if "" => 0 (most likely case) */
1006         if (likely(s->len==0)) *i=0;
1007         else if (unlikely(str2sint(s, i)!=0)){
1008                 /* error converting to int => non numeric => 0 */
1009                 *i=0;
1010 #ifdef RV_STR2INT_VERBOSE_ERR
1011                 WARN("automatic string to int conversion for \"%.*s\" failed\n",
1012                                 s->len, ZSW(s->s));
1013                 /* return an error code */
1014 #endif
1015 #ifdef RV_STR2INT_ERR
1016                 ret=-1;
1017 #endif
1018         }
1019         if (destroy_pval)
1020                 pv_value_destroy(&pval);
1021         return ret;
1022 error_cache:
1023         BUG("invalid cached value:cache type %d, value type %d\n",
1024                         cache?cache->cache_type:0, cache?cache->val_type:0);
1025 error:
1026         if (destroy_pval)
1027                 pv_value_destroy(&pval);
1028         *i=0;
1029         return -1;
1030 }
1031
1032
1033
1034 /** log a message, appending rve position and a '\n'.*/
1035 #define RVE_LOG(lev, rve, txt) \
1036         LOG((lev), txt " (%d,%d-%d,%d)\n", \
1037                         (rve)->fpos.s_line, rve->fpos.s_col, \
1038                         (rve)->fpos.e_line, rve->fpos.e_col )
1039
1040
1041 /** macro for checking and handling rval_get_int() retcode.
1042  * check if the return code is an rval_get_int error and if so
1043  * handle the error (e.g. print a log message, ignore the error by
1044  * setting ret to 0 a.s.o.)
1045  * @param ret - retcode as returned by rval_get_int() (might be changed)
1046  * @param txt - warning message txt (no pointer allowed)
1047  * @param rve - rval_expr, used to access the config. pos
1048  */
1049 #if defined RVAL_GET_INT_ERR_WARN && defined RVAL_GET_INT_ERR_IGN
1050 #define rval_get_int_handle_ret(ret, txt, rve) \
1051         do { \
1052                 if (unlikely((ret)<0)) { \
1053                         RVE_LOG(L_WARN, rve, txt); \
1054                         (ret)=0; \
1055                 } \
1056         }while(0)
1057 #elif defined RVAL_GET_INT_ERR_WARN
1058 #define rval_get_int_handle_ret(ret, txt, rve) \
1059         do { \
1060                 if (unlikely((ret)<0)) \
1061                         RVE_LOG(L_WARN, rve, txt); \
1062         }while(0)
1063 #elif defined RVAL_GET_INT_ERR_IGN
1064 #define rval_get_int_handle_ret(ret, txt, rve) \
1065         do { \
1066                 if (unlikely((ret)<0)) \
1067                                 (ret)=0; \
1068         } while(0)
1069 #else
1070 #define rval_get_int_handle_ret(ret, txt, rve) /* do nothing */
1071 #endif
1072
1073
1074
1075
1076
1077
1078 /** get the string value of an rv in a tmp variable
1079   * *s=(str)rv
1080   * if rv == undefined select, avp or pvar, return "".
1081   * if an error occurs while evaluating a select, avp or pvar, behave as
1082   * for the undefined case (and return success).
1083   * The result points either inside the passed rv, inside
1084   * new_cache or inside an avp. new_cache must be non zero,
1085   * initialized previously and it _must_ be rval_cache_clean(...)'ed when
1086   * done.
1087   * WARNING: it's not intended for general use. It might return a pointer
1088   * inside rv so the result _must_ be treated as read-only. rv and new_cache
1089   * must not be released/freed until the result is no longer needed.
1090   * For general use see  rval_get_str().
1091   * @param h - script context handle
1092   * @param msg - sip msg
1093   * @param tmpv - str return value (pointer to a str struct that will be
1094   *               be filled with the conversion result)
1095   * @param rv   - rvalue to be converted
1096   * @param cache - cached rv value (read-only), can be 0
1097   * @param tmp_cache - used for temporary storage (so that tmpv will not
1098   *                 point to possible freed data), it must be non-null,
1099   *                 initialized and cleaned afterwards.
1100   * @return 0 on success, <0 on error and EXPR_DROP on drop
1101  */
1102 int rval_get_tmp_str(struct run_act_ctx* h, struct sip_msg* msg,
1103                                                                 str* tmpv, struct rvalue* rv,
1104                                                                 struct rval_cache* cache,
1105                                                                 struct rval_cache* tmp_cache)
1106 {
1107         avp_t* r_avp;
1108         int i;
1109         
1110         switch(rv->type){
1111                 case RV_INT:
1112                         tmpv->s=sint2strbuf(rv->v.l, tmp_cache->i2s,
1113                                                                 sizeof(tmp_cache->i2s), &tmpv->len);
1114                         tmp_cache->cache_type = RV_CACHE_INT2STR;
1115                         break;
1116                 case RV_STR:
1117                         *tmpv=rv->v.s;
1118                         break;
1119                 case RV_ACTION_ST:
1120                         if (rv->v.action) {
1121                                 i=(run_actions_safe(h, rv->v.action, msg)>0);
1122                                 h->run_flags &= ~(RETURN_R_F|BREAK_R_F); /* catch return &
1123                                                                                                                     break in expr*/
1124                         } else
1125                                 i=0;
1126                         tmpv->s=sint2strbuf(i, tmp_cache->i2s,
1127                                                                 sizeof(tmp_cache->i2s), &tmpv->len);
1128                         tmp_cache->cache_type = RV_CACHE_INT2STR;
1129                         break;
1130                 case RV_BEXPR:
1131                         i=eval_expr(h, rv->v.bexpr, msg);
1132                         if (i==EXPR_DROP){
1133                                 i=0; /* false */
1134                                 tmpv->s=sint2strbuf(i, tmp_cache->i2s,
1135                                                 sizeof(tmp_cache->i2s), &tmpv->len);
1136                                 tmp_cache->cache_type = RV_CACHE_INT2STR;
1137                                 return EXPR_DROP;
1138                         }
1139                         tmpv->s=sint2strbuf(i, tmp_cache->i2s, sizeof(tmp_cache->i2s),
1140                                                                 &tmpv->len);
1141                         tmp_cache->cache_type = RV_CACHE_INT2STR;
1142                         break;
1143                 case RV_SEL:
1144                         i=run_select(tmpv, &rv->v.sel, msg);
1145                         if (unlikely(i!=0)){
1146                                 if (i<0){
1147                                         goto eval_error;
1148                                 }else { /* i>0  => undefined */
1149                                         goto undef;
1150                                 }
1151                         }
1152                         break;
1153                 case RV_AVP:
1154                         if (likely(cache && cache->cache_type==RV_CACHE_AVP)){
1155                                 if (likely(cache->val_type==RV_STR)){
1156                                         *tmpv=cache->c.avp_val.s;
1157                                 }else if (cache->val_type==RV_INT){
1158                                         i=cache->c.avp_val.n;
1159                                         tmpv->s=sint2strbuf(i, tmp_cache->i2s,
1160                                                                                 sizeof(tmp_cache->i2s), &tmpv->len);
1161                                         tmp_cache->cache_type = RV_CACHE_INT2STR;
1162                                 }else if (cache->val_type==RV_NONE){
1163                                         goto undef;
1164                                 }else goto error_cache;
1165                         }else{
1166                                 r_avp = search_avp_by_index(rv->v.avps.type, rv->v.avps.name,
1167                                                                                         &tmp_cache->c.avp_val,
1168                                                                                         rv->v.avps.index);
1169                                 if (likely(r_avp)){
1170                                         if (likely(r_avp->flags & AVP_VAL_STR)){
1171                                                 tmp_cache->cache_type=RV_CACHE_AVP;
1172                                                 tmp_cache->val_type=RV_STR;
1173                                                 *tmpv=tmp_cache->c.avp_val.s;
1174                                         }else{
1175                                                 i=tmp_cache->c.avp_val.n;
1176                                                 tmpv->s=sint2strbuf(i, tmp_cache->i2s,
1177                                                                                 sizeof(tmp_cache->i2s), &tmpv->len);
1178                                                 tmp_cache->cache_type = RV_CACHE_INT2STR;
1179                                         }
1180                                 }else goto undef;
1181                         }
1182                         break;
1183                 case RV_PVAR:
1184                         if (likely(cache && cache->cache_type==RV_CACHE_PVAR)){
1185                                 if (likely(cache->val_type==RV_STR)){
1186                                         *tmpv=cache->c.pval.rs;
1187                                 }else if (cache->val_type==RV_INT){
1188                                         i=cache->c.pval.ri;
1189                                         tmpv->s=sint2strbuf(i, tmp_cache->i2s,
1190                                                                                 sizeof(tmp_cache->i2s), &tmpv->len);
1191                                         tmp_cache->cache_type = RV_CACHE_INT2STR;
1192                                 }else if (cache->val_type==RV_NONE){
1193                                         goto undef;
1194                                 }else goto error_cache;
1195                         }else{
1196                                 memset(&tmp_cache->c.pval, 0, sizeof(tmp_cache->c.pval));
1197                                 if (likely(pv_get_spec_value(msg, &rv->v.pvs,
1198                                                                                                 &tmp_cache->c.pval)==0)){
1199                                         if (likely(tmp_cache->c.pval.flags & PV_VAL_STR)){
1200                                                 /*  the value is not destroyed, but saved instead
1201                                                         in tmp_cache so that it can be destroyed later
1202                                                         when no longer needed */
1203                                                 tmp_cache->cache_type=RV_CACHE_PVAR;
1204                                                 tmp_cache->val_type=RV_STR;
1205                                                 *tmpv=tmp_cache->c.pval.rs;
1206                                         }else if (likely(tmp_cache->c.pval.flags & PV_VAL_INT)){
1207                                                 i=tmp_cache->c.pval.ri;
1208                                                 pv_value_destroy(&tmp_cache->c.pval);
1209                                                 tmpv->s=sint2strbuf(i, tmp_cache->i2s,
1210                                                                                 sizeof(tmp_cache->i2s), &tmpv->len);
1211                                                 tmp_cache->cache_type = RV_CACHE_INT2STR;
1212                                         }else{
1213                                                 /* no PV_VAL_STR and no PV_VAL_INT => undef
1214                                                    (PV_VAL_NULL) */
1215                                                 pv_value_destroy(&tmp_cache->c.pval);
1216                                                 goto undef;
1217                                         }
1218                                 }else{
1219                                         goto eval_error;
1220                                 }
1221                         }
1222                         break;
1223                 default:
1224                         BUG("rv type %d not handled\n", rv->type);
1225                         goto error;
1226         }
1227         return 0;
1228 undef:
1229 eval_error: /* same as undefined */
1230         /* handle undefined => result "", return success */
1231         tmpv->s="";
1232         tmpv->len=0;
1233         return 0;
1234 error_cache:
1235         BUG("invalid cached value:cache type %d, value type %d\n",
1236                         cache?cache->cache_type:0, cache?cache->val_type:0);
1237 error:
1238         tmpv->s="";
1239         tmpv->len=0;
1240         return -1;
1241 }
1242
1243
1244
1245 /** get the string value of an rv.
1246   * *s=(str)rv
1247   * The result is pkg malloc'ed (so it should be pkg_free()'ed when finished.
1248   * @return 0 on success, <0 on error and EXPR_DROP on drop
1249  */
1250 int rval_get_str(struct run_act_ctx* h, struct sip_msg* msg,
1251                                                                 str* s, struct rvalue* rv,
1252                                                                 struct rval_cache* cache)
1253 {
1254         str tmp;
1255         struct rval_cache tmp_cache;
1256         
1257         rval_cache_init(&tmp_cache);
1258         if (unlikely(rval_get_tmp_str(h, msg, &tmp, rv, cache, &tmp_cache)<0))
1259                 goto error;
1260         s->s=pkg_malloc(tmp.len+1/* 0 term */);
1261         if (unlikely(s->s==0)){
1262                 ERR("memory allocation error\n");
1263                 goto error;
1264         }
1265         s->len=tmp.len;
1266         memcpy(s->s, tmp.s, tmp.len);
1267         s->s[tmp.len]=0; /* 0 term */
1268         rval_cache_clean(&tmp_cache);
1269         return 0;
1270 error:
1271         rval_cache_clean(&tmp_cache);
1272         return -1;
1273 }
1274
1275
1276
1277 /**
1278  * @brief Convert a rvalue to another rvalue, of a specific type
1279  *
1280  * Convert a rvalue to another rvalue, of a specific type.
1281  * The result is read-only in most cases (can be a reference
1282  * to another rvalue, can be checked by using rv_chg_in_place()) and
1283  * _must_ be rval_destroy()'ed.
1284  *
1285  * @param h run action context
1286  * @param msg SIP mesasge
1287  * @param type - type to convert to
1288  * @param v - rvalue to convert
1289  * @param c - rval_cache (cached v value if known/filled by another
1290  *            function), can be 0 (unknown/not needed)
1291  * @return pointer to a rvalue (reference to an existing one or a new
1292  * one, @see rv_chg_in_place() and the above comment), or 0 on error.
1293  */
1294 struct rvalue* rval_convert(struct run_act_ctx* h, struct sip_msg* msg,
1295                                                         enum rval_type type, struct rvalue* v,
1296                                                         struct rval_cache* c)
1297 {
1298         int i;
1299         struct rval_cache tmp_cache;
1300         str tmp;
1301         struct rvalue* ret;
1302         union rval_val val;
1303         
1304         if (v->type==type){
1305                 rv_ref(v);
1306                 return v;
1307         }
1308         switch(type){
1309                 case RV_INT:
1310                         if (unlikely(rval_get_int(h, msg, &i, v, c) < 0))
1311                                 return 0;
1312                         val.l=i;
1313                         return rval_new(RV_INT, &val, 0);
1314                 case RV_STR:
1315                         rval_cache_init(&tmp_cache);
1316                         if (unlikely(rval_get_tmp_str(h, msg, &tmp, v, c, &tmp_cache) < 0))
1317                         {
1318                                 rval_cache_clean(&tmp_cache);
1319                                 return 0;
1320                         }
1321                         ret=rval_new_str(&tmp, RV_STR_EXTRA);
1322                         rval_cache_clean(&tmp_cache);
1323                         return ret;
1324                 case RV_NONE:
1325                 default:
1326                         BUG("unsupported conversion to type %d\n", type);
1327                         return 0;
1328         }
1329         return 0;
1330 }
1331
1332
1333
1334 /** integer operation: *res= op v.
1335   * @return 0 on succes, \<0 on error
1336   */
1337 inline static int int_intop1(int* res, enum rval_expr_op op, int v)
1338 {
1339         switch(op){
1340                 case RVE_UMINUS_OP:
1341                         *res=-v;
1342                         break;
1343                 case RVE_BOOL_OP:
1344                         *res=!!v;
1345                         break;
1346                 case RVE_LNOT_OP:
1347                         *res=!v;
1348                         break;
1349                 case RVE_BNOT_OP:
1350                         *res=~v;
1351                         break;
1352                 default:
1353                         BUG("rv unsupported intop1 %d\n", op);
1354                         return -1;
1355         }
1356         return 0;
1357 }
1358
1359
1360
1361 /** integer operation: *res= v1 op v2
1362   * @return 0 on succes, \<0 on error
1363   */
1364 inline static int int_intop2(int* res, enum rval_expr_op op, int v1, int v2)
1365 {
1366         switch(op){
1367                 case RVE_PLUS_OP:
1368                 case RVE_IPLUS_OP:
1369                         *res=v1+v2;
1370                         break;
1371                 case RVE_MINUS_OP:
1372                         *res=v1-v2;
1373                         break;
1374                 case RVE_MUL_OP:
1375                         *res=v1*v2;
1376                         break;
1377                 case RVE_DIV_OP:
1378                         if (unlikely(v2==0)){
1379                                 ERR("rv div by 0\n");
1380                                 return -1;
1381                         }
1382                         *res=v1/v2;
1383                         break;
1384                 case RVE_MOD_OP:
1385                         if (unlikely(v2==0)){
1386                                 ERR("rv mod by 0\n");
1387                                 return -1;
1388                         }
1389                         *res=v1%v2;
1390                         break;
1391                 case RVE_BOR_OP:
1392                         *res=v1|v2;
1393                         break;
1394                 case RVE_BAND_OP:
1395                         *res=v1&v2;
1396                         break;
1397                 case RVE_BXOR_OP:
1398                         *res=v1^v2;
1399                         break;
1400                 case RVE_BLSHIFT_OP:
1401                         *res=v1<<v2;
1402                         break;
1403                 case RVE_BRSHIFT_OP:
1404                         *res=v1>>v2;
1405                         break;
1406                 case RVE_LAND_OP:
1407                         *res=v1 && v2;
1408                         break;
1409                 case RVE_LOR_OP:
1410                         *res=v1 || v2;
1411                         break;
1412                 case RVE_GT_OP:
1413                         *res=v1 > v2;
1414                         break;
1415                 case RVE_GTE_OP:
1416                         *res=v1 >= v2;
1417                         break;
1418                 case RVE_LT_OP:
1419                         *res=v1 < v2;
1420                         break;
1421                 case RVE_LTE_OP:
1422                         *res=v1 <= v2;
1423                         break;
1424                 case RVE_EQ_OP:
1425                 case RVE_IEQ_OP:
1426                         *res=v1 == v2;
1427                         break;
1428                 case RVE_DIFF_OP:
1429                 case RVE_IDIFF_OP:
1430                         *res=v1 != v2;
1431                         break;
1432                 case RVE_CONCAT_OP:
1433                         *res=0;
1434                         /* invalid operand for int */
1435                         return -1;
1436                 default:
1437                         BUG("rv unsupported intop %d\n", op);
1438                         return -1;
1439         }
1440         return 0;
1441 }
1442
1443
1444
1445 /** internal helper: compare 2 RV_STR RVs.
1446   * Warning: rv1 & rv2 must be RV_STR
1447   * @return 0 on success, -1 on error
1448   */
1449 inline static int bool_rvstrop2( enum rval_expr_op op, int* res,
1450                                                                 struct rvalue* rv1, struct rvalue* rv2)
1451 {
1452         str* s1;
1453         str* s2;
1454         regex_t tmp_re;
1455         
1456         s1=&rv1->v.s;
1457         s2=&rv2->v.s;
1458         switch(op){
1459                 case RVE_EQ_OP:
1460                 case RVE_STREQ_OP:
1461                         *res= (s1->len==s2->len) && (memcmp(s1->s, s2->s, s1->len)==0);
1462                         break;
1463                 case RVE_DIFF_OP:
1464                 case RVE_STRDIFF_OP:
1465                         *res= (s1->len!=s2->len) || (memcmp(s1->s, s2->s, s1->len)!=0);
1466                         break;
1467                 case RVE_MATCH_OP:
1468                         if (likely(rv2->flags & RV_RE_F)){
1469                                 *res=(regexec(rv2->v.re.regex, rv1->v.s.s, 0, 0, 0)==0);
1470                         }else{
1471                                 /* we need to compile the RE on the fly */
1472                                 if (unlikely(regcomp(&tmp_re, s2->s,
1473                                                                                 REG_EXTENDED|REG_NOSUB|REG_ICASE))){
1474                                         /* error */
1475                                         ERR("Bad regular expression \"%s\"\n", s2->s);
1476                                         goto error;
1477                                 }
1478                                 *res=(regexec(&tmp_re, s1->s, 0, 0, 0)==0);
1479                                 regfree(&tmp_re);
1480                         }
1481                         break;
1482                 default:
1483                         BUG("rv unsupported intop %d\n", op);
1484                         goto error;
1485         }
1486         return 0;
1487 error:
1488         *res=0; /* false */
1489         return -1;
1490 }
1491
1492
1493
1494 /** integer returning operation on string: *res= op str (returns integer)
1495   * @return 0 on succes, \<0 on error
1496   */
1497 inline static int int_strop1(int* res, enum rval_expr_op op, str* s1)
1498 {
1499         switch(op){
1500                 case RVE_STRLEN_OP:
1501                         *res=s1->len;
1502                         break;
1503                 case RVE_STREMPTY_OP:
1504                         *res=(s1->len==0);
1505                         break;
1506                 default:
1507                         BUG("rv unsupported int_strop1 %d\n", op);
1508                         *res=0;
1509                         return -1;
1510         }
1511         return 0;
1512 }
1513
1514
1515
1516 /** integer operation: ret= op v (returns a rvalue).
1517  * @return rvalue on success, 0 on error
1518  */
1519 inline static struct rvalue* rval_intop1(struct run_act_ctx* h,
1520                                                                                         struct sip_msg* msg,
1521                                                                                         enum rval_expr_op op,
1522                                                                                         struct rvalue* v)
1523 {
1524         struct rvalue* rv2;
1525         struct rvalue* ret;
1526         int i;
1527         
1528         i=0;
1529         rv2=rval_convert(h, msg, RV_INT, v, 0);
1530         if (unlikely(rv2==0)){
1531                 ERR("rval int conversion failed\n");
1532                 goto error;
1533         }
1534         if (unlikely(int_intop1(&i, op, rv2->v.l)<0))
1535                 goto error;
1536         if (rv_chg_in_place(rv2)){
1537                 ret=rv2;
1538                 rv_ref(ret);
1539         }else if (rv_chg_in_place(v)){
1540                 ret=v;
1541                 rv_ref(ret);
1542         }else{
1543                 ret=rval_new(RV_INT, &rv2->v, 0);
1544                 if (unlikely(ret==0)){
1545                         ERR("eval out of memory\n");
1546                         goto error;
1547                 }
1548         }
1549         rval_destroy(rv2);
1550         ret->v.l=i;
1551         return ret;
1552 error:
1553         rval_destroy(rv2);
1554         return 0;
1555 }
1556
1557
1558
1559 /** integer operation: ret= l op r (returns a rvalue).
1560  * @return rvalue on success, 0 on error
1561  */
1562 inline static struct rvalue* rval_intop2(struct run_act_ctx* h,
1563                                                                                         struct sip_msg* msg,
1564                                                                                         enum rval_expr_op op,
1565                                                                                         struct rvalue* l,
1566                                                                                         struct rvalue* r)
1567 {
1568         struct rvalue* rv1;
1569         struct rvalue* rv2;
1570         struct rvalue* ret;
1571         int i;
1572
1573         rv2=rv1=0;
1574         ret=0;
1575         if ((rv1=rval_convert(h, msg, RV_INT, l, 0))==0)
1576                 goto error;
1577         if ((rv2=rval_convert(h, msg, RV_INT, r, 0))==0)
1578                 goto error;
1579         if (unlikely(int_intop2(&i, op, rv1->v.l, rv2->v.l)<0))
1580                 goto error;
1581         if (rv_chg_in_place(rv1)){
1582                 /* try reusing rv1 */
1583                 ret=rv1;
1584                 rv_ref(ret);
1585         }else if (rv_chg_in_place(rv2)){
1586                 /* try reusing rv2 */
1587                 ret=rv2;
1588                 rv_ref(ret);
1589         }else if ((l->type==RV_INT) && (rv_chg_in_place(l))){
1590                 ret=l;
1591                 rv_ref(ret);
1592         } else if ((r->type==RV_INT) && (rv_chg_in_place(r))){
1593                 ret=r;
1594                 rv_ref(ret);
1595         }else{
1596                 ret=rval_new(RV_INT, &rv1->v, 0);
1597                 if (unlikely(ret==0)){
1598                         ERR("rv eval out of memory\n");
1599                         goto error;
1600                 }
1601         }
1602         rval_destroy(rv1); 
1603         rval_destroy(rv2); 
1604         ret->v.l=i;
1605         return ret;
1606 error:
1607         rval_destroy(rv1); 
1608         rval_destroy(rv2); 
1609         return 0;
1610 }
1611
1612
1613
1614 /** string add operation: ret= l . r (returns a rvalue).
1615  * Can use cached rvalues (c1 & c2).
1616  * @return rvalue on success, 0 on error
1617  */
1618 inline static struct rvalue* rval_str_add2(struct run_act_ctx* h,
1619                                                                                         struct sip_msg* msg,
1620                                                                                         struct rvalue* l,
1621                                                                                         struct rval_cache* c1,
1622                                                                                         struct rvalue* r,
1623                                                                                         struct rval_cache* c2
1624                                                                                         )
1625 {
1626         struct rvalue* rv1;
1627         struct rvalue* rv2;
1628         struct rvalue* ret;
1629         str* s1;
1630         str* s2;
1631         str tmp;
1632         short flags;
1633         int len;
1634         
1635         rv2=rv1=0;
1636         ret=0;
1637         flags=0;
1638         s1=0;
1639         s2=0;
1640         if ((rv1=rval_convert(h, msg, RV_STR, l, c1))==0)
1641                 goto error;
1642         if ((rv2=rval_convert(h, msg, RV_STR, r, c2))==0)
1643                 goto error;
1644         
1645         len=rv1->v.s.len + rv2->v.s.len + 1 /* 0 */;
1646         
1647         if (rv_chg_in_place(rv1) && (rv1->bsize>=len)){
1648                 /* try reusing rv1 */
1649                 ret=rv1;
1650                 rv_ref(ret);
1651                 s2=&rv2->v.s;
1652                 if (ret->v.s.s == &ret->buf[0]) s1=0;
1653                 else{
1654                         tmp=ret->v.s;
1655                         flags=ret->flags;
1656                         ret->flags &= ~RV_CNT_ALLOCED_F;
1657                         ret->v.s.s=&ret->buf[0];
1658                         ret->v.s.len=0;
1659                         s1=&tmp;
1660                 }
1661         }else if (rv_chg_in_place(rv2) && (rv2->bsize>=len)){
1662                 /* try reusing rv2 */
1663                 ret=rv2;
1664                 rv_ref(ret);
1665                 s1=&rv1->v.s;
1666                 if (ret->v.s.s == &ret->buf[0]) 
1667                         s2=&ret->v.s;
1668                 else{
1669                         tmp=ret->v.s;
1670                         flags=ret->flags;
1671                         ret->flags &= ~RV_CNT_ALLOCED_F;
1672                         ret->v.s.s=&ret->buf[0];
1673                         ret->v.s.len=0;
1674                         s2=&tmp;
1675                 }
1676         }else if ((l->type==RV_STR) && (rv_chg_in_place(l)) && (l->bsize>=len)){
1677                 ret=l;
1678                 rv_ref(ret);
1679                 s2=&rv2->v.s;
1680                 if (ret->v.s.s == &ret->buf[0]) s1=0;
1681                 else{
1682                         tmp=ret->v.s;
1683                         flags=ret->flags;
1684                         ret->flags &= ~RV_CNT_ALLOCED_F;
1685                         ret->v.s.s=&ret->buf[0];
1686                         ret->v.s.len=0;
1687                         s1=&tmp;
1688                 }
1689         } else if ((r->type==RV_STR) && (rv_chg_in_place(r) && (r->bsize>=len))){
1690                 ret=r;
1691                 rv_ref(ret);
1692                 s1=&rv1->v.s;
1693                 if (ret->v.s.s == &ret->buf[0]) 
1694                         s2=&ret->v.s;
1695                 else{
1696                         tmp=ret->v.s;
1697                         flags=ret->flags;
1698                         ret->flags &= ~RV_CNT_ALLOCED_F;
1699                         ret->v.s.s=&ret->buf[0];
1700                         ret->v.s.len=0;
1701                         s2=&tmp;
1702                 }
1703         }else{
1704                 ret=rval_new(RV_STR, &rv1->v, len + RV_STR_EXTRA);
1705                 if (unlikely(ret==0)){
1706                         ERR("rv eval out of memory\n");
1707                         goto error;
1708                 }
1709                 s1=0;
1710                 s2=&rv2->v.s;
1711         }
1712         /* do the actual copy */
1713         memmove(ret->buf+rv1->v.s.len, s2->s, s2->len);
1714         if (s1){
1715                 memcpy(ret->buf, s1->s, s1->len);
1716         }
1717         ret->v.s.len=rv1->v.s.len+s2->len;
1718         ret->v.s.s[ret->v.s.len]=0;
1719         /* cleanup if needed */
1720         if (flags & RV_CNT_ALLOCED_F)
1721                 pkg_free(tmp.s);
1722         rval_destroy(rv1); 
1723         rval_destroy(rv2); 
1724         return ret;
1725 error:
1726         rval_destroy(rv1); 
1727         rval_destroy(rv2); 
1728         return 0;
1729 }
1730
1731
1732
1733 /** bool operation on rval evaluated as strings.
1734  * Can use cached rvalues (c1 & c2).
1735  * @return 0 success, -1 on error
1736  */
1737 inline static int rval_str_lop2(struct run_act_ctx* h,
1738                                                  struct sip_msg* msg,
1739                                                  int* res,
1740                                                  enum rval_expr_op op,
1741                                                  struct rvalue* l,
1742                                                  struct rval_cache* c1,
1743                                                  struct rvalue* r,
1744                                                  struct rval_cache* c2)
1745 {
1746         struct rvalue* rv1;
1747         struct rvalue* rv2;
1748         int ret;
1749         
1750         rv2=rv1=0;
1751         ret=0;
1752         if ((rv1=rval_convert(h, msg, RV_STR, l, c1))==0)
1753                 goto error;
1754         if ((rv2=rval_convert(h, msg, RV_STR, r, c2))==0)
1755                 goto error;
1756         ret=bool_rvstrop2(op, res, rv1, rv2);
1757         rval_destroy(rv1); 
1758         rval_destroy(rv2); 
1759         return ret;
1760 error:
1761         rval_destroy(rv1); 
1762         rval_destroy(rv2); 
1763         return 0;
1764 }
1765
1766
1767
1768 /**
1769  * @brief Integer operation on rval evaluated as string
1770  * 
1771  * Integer operation on rval evaluated as string, can use cached
1772  * rvalues (c1 & c2).
1773  * @param h run action context
1774  * @param msg SIP message
1775  * @param res will be set to the result
1776  * @param op rvalue expression operation
1777  * @param l rvalue
1778  * @param c1 rvalue cache
1779  * @return 0 success, -1 on error
1780  */
1781 inline static int rval_int_strop1(struct run_act_ctx* h,
1782                                                  struct sip_msg* msg,
1783                                                  int* res,
1784                                                  enum rval_expr_op op,
1785                                                  struct rvalue* l,
1786                                                  struct rval_cache* c1)
1787 {
1788         struct rvalue* rv1;
1789         int ret;
1790         
1791         rv1=0;
1792         ret=0;
1793         if ((rv1=rval_convert(h, msg, RV_STR, l, c1))==0)
1794                 goto error;
1795         ret=int_strop1(res, op, &rv1->v.s);
1796         rval_destroy(rv1); 
1797         return ret;
1798 error:
1799         *res=0;
1800         rval_destroy(rv1); 
1801         return -1;
1802 }
1803
1804
1805
1806 /**
1807  * @brief Checks if rv is defined
1808  * @param h run action context
1809  * @param msg SIP message
1810  * @param res set to the result 1 is defined, 0 not defined
1811  * @param rv rvalue
1812  * @param cache rvalue cache
1813  * @return 0 on success, -1 on error
1814  * @note Can use cached rvalues (cache). A rv can be undefined if it's
1815  * an undefined avp or pvar or select or if it's NONE
1816  * @note An error in the avp, pvar or select search is equivalent to
1817  * undefined (and it's not reported)
1818  */
1819 inline static int rv_defined(struct run_act_ctx* h,
1820                                                  struct sip_msg* msg, int* res,
1821                                                  struct rvalue* rv, struct rval_cache* cache)
1822 {
1823         avp_t* r_avp;
1824         int_str avp_val;
1825         pv_value_t pval;
1826         str tmp;
1827         
1828         *res=1;
1829         switch(rv->type){
1830                 case RV_SEL:
1831                         if (unlikely(cache && cache->cache_type==RV_CACHE_SELECT)){
1832                                 *res=(cache->val_type!=RV_NONE);
1833                         }else
1834                                 /* run select returns 0 on success, -1 on error and >0 on 
1835                                    undefined. error is considered undefined */
1836                                 *res=(run_select(&tmp, &rv->v.sel, msg)==0);
1837                         break;
1838                 case RV_AVP:
1839                         if (unlikely(cache && cache->cache_type==RV_CACHE_AVP)){
1840                                 *res=(cache->val_type!=RV_NONE);
1841                         }else{
1842                                 r_avp = search_avp_by_index(rv->v.avps.type, rv->v.avps.name,
1843                                                                                         &avp_val, rv->v.avps.index);
1844                                 if (unlikely(r_avp==0)){
1845                                         *res=0;
1846                                 }
1847                         }
1848                         break;
1849                 case RV_PVAR:
1850                         /* PV_VAL_NULL or pv_get_spec_value error => undef */
1851                         if (unlikely(cache && cache->cache_type==RV_CACHE_PVAR)){
1852                                 *res=(cache->val_type!=RV_NONE);
1853                         }else{
1854                                 memset(&pval, 0, sizeof(pval));
1855                                 if (likely(pv_get_spec_value(msg, &rv->v.pvs, &pval)==0)){
1856                                         if ((pval.flags & PV_VAL_NULL) &&
1857                                                         ! (pval.flags & (PV_VAL_INT|PV_VAL_STR))){
1858                                                 *res=0;
1859                                         }
1860                                         pv_value_destroy(&pval);
1861                                 }else{
1862                                         *res=0; /* in case of error, consider it undef */
1863                                 }
1864                         }
1865                         break;
1866                 case RV_NONE:
1867                         *res=0;
1868                         break;
1869                 default:
1870                         break;
1871         }
1872         return 0;
1873 }
1874
1875
1876 /**
1877  * @brief Defined (integer) operation on rve
1878  * @param h run action context
1879  * @param msg SIP message
1880  * @param res - set to  1 defined, 0 not defined
1881  * @param rve rvalue expression
1882  * @return 0 on success, -1 on error
1883  */
1884 inline static int int_rve_defined(struct run_act_ctx* h,
1885                                                  struct sip_msg* msg, int* res,
1886                                                  struct rval_expr* rve)
1887 {
1888         /* only a rval can be undefined, any expression consisting on more
1889            then one rval => defined */
1890         if (likely(rve->op==RVE_RVAL_OP))
1891                 return rv_defined(h, msg, res, &rve->left.rval, 0);
1892         *res=1;
1893         return 0;
1894 }
1895
1896
1897
1898 /** evals an integer expr  to an int.
1899  * 
1900  *  *res=(int)eval(rve)
1901  *  @return 0 on success, \<0 on error
1902  */
1903 int rval_expr_eval_int( struct run_act_ctx* h, struct sip_msg* msg,
1904                                                 int* res, struct rval_expr* rve)
1905 {
1906         int i1, i2, ret;
1907         struct rval_cache c1, c2;
1908         struct rvalue* rv1;
1909         struct rvalue* rv2;
1910         
1911         ret=-1;
1912         switch(rve->op){
1913                 case RVE_RVAL_OP:
1914                         ret=rval_get_int(h, msg, res,  &rve->left.rval, 0);
1915                         rval_get_int_handle_ret(ret, "rval expression conversion to int"
1916                                                                                 " failed", rve);
1917                         break;
1918                 case RVE_UMINUS_OP:
1919                 case RVE_BOOL_OP:
1920                 case RVE_LNOT_OP:
1921                 case RVE_BNOT_OP:
1922                         if (unlikely(
1923                                         (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1924                                 break;
1925                         ret=int_intop1(res, rve->op, i1);
1926                         break;
1927                 case RVE_INT_OP:
1928                         ret=rval_expr_eval_int(h, msg, res, rve->left.rve);
1929                         break;
1930                 case RVE_MUL_OP:
1931                 case RVE_DIV_OP:
1932                 case RVE_MOD_OP:
1933                 case RVE_MINUS_OP:
1934                 case RVE_PLUS_OP:
1935                 case RVE_IPLUS_OP:
1936                 case RVE_BOR_OP:
1937                 case RVE_BAND_OP:
1938                 case RVE_BXOR_OP:
1939                 case RVE_BLSHIFT_OP:
1940                 case RVE_BRSHIFT_OP:
1941                 case RVE_GT_OP:
1942                 case RVE_GTE_OP:
1943                 case RVE_LT_OP:
1944                 case RVE_LTE_OP:
1945                 case RVE_IEQ_OP:
1946                 case RVE_IDIFF_OP:
1947                         if (unlikely(
1948                                         (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1949                                 break;
1950                         if (unlikely(
1951                                         (ret=rval_expr_eval_int(h, msg, &i2, rve->right.rve)) <0) )
1952                                 break;
1953                         ret=int_intop2(res, rve->op, i1, i2);
1954                         break;
1955                 case RVE_LAND_OP:
1956                         if (unlikely(
1957                                         (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1958                                 break;
1959                         if (i1==0){
1960                                 *res=0;
1961                         }else{
1962                                 if (unlikely( (ret=rval_expr_eval_int(h, msg, &i2,
1963                                                                                 rve->right.rve)) <0) )
1964                                         break;
1965                                 *res=i1 && i2;
1966                         }
1967                         ret=0;
1968                         break;
1969                 case RVE_LOR_OP:
1970                         if (unlikely(
1971                                         (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
1972                                 break;
1973                         if (i1){
1974                                 *res=1;
1975                         }else{
1976                                 if (unlikely( (ret=rval_expr_eval_int(h, msg, &i2,
1977                                                                                 rve->right.rve)) <0) )
1978                                         break;
1979                                 *res=i1 || i2;
1980                         }
1981                         ret=0;
1982                         break;
1983                 case RVE_EQ_OP:
1984                 case RVE_DIFF_OP:
1985                         /* if left is string, eval left & right as string and
1986                          *   use string diff.
1987                          * if left is int eval as int using int diff
1988                          * if left is undef, look at right and convert to right type
1989                          */
1990                         rval_cache_init(&c1);
1991                         if (unlikely( (ret=rval_expr_eval_rvint(h, msg, &rv1, &i1,
1992                                                                                                         rve->left.rve, &c1))<0)){
1993                                 /* error */
1994                                 rval_cache_clean(&c1);
1995                                 break;
1996                         }
1997                         if (likely(rv1==0)){
1998                                 /* int */
1999                                 rval_cache_clean(&c1);
2000                                 if (unlikely( (ret=rval_expr_eval_int(h, msg, &i2,
2001                                                                                                                 rve->right.rve)) <0) )
2002                                         break;  /* error */
2003                                 ret=int_intop2(res, rve->op, i1, i2);
2004                         }else{
2005                                 /* not int => str or undef */
2006                                 /* check for undefined left operand */
2007                                 if (unlikely( c1.cache_type!=RV_CACHE_EMPTY &&
2008                                                                 c1.val_type==RV_NONE)){
2009 #ifdef UNDEF_EQ_ALWAYS_FALSE
2010                                         /* undef == something  always false
2011                                            undef != something  always true*/
2012                                         ret=(rve->op==RVE_DIFF_OP);
2013 #elif defined UNDEF_EQ_UNDEF_TRUE
2014                                         /* undef == something defined always false
2015                                            undef == undef true */
2016                                         if (int_rve_defined(h, msg, &i2, rve->right.rve)<0){
2017                                                 /* error */
2018                                                 rval_cache_clean(&c1);
2019                                                 rval_destroy(rv1);
2020                                                 break;
2021                                         }
2022                                         ret=(!i2) ^ (rve->op==RVE_DIFF_OP);
2023 #else  /* ! UNDEF_EQ_* */
2024                                         /*  undef == val
2025                                          *  => convert to (type_of(val)) (undef) == val */
2026                                         rval_cache_init(&c2);
2027                                         if (unlikely( (ret=rval_expr_eval_rvint(h, msg, &rv2, &i2,
2028                                                                                                         rve->right.rve, &c2))<0)){
2029                                                 /* error */
2030                                                 rval_cache_clean(&c1);
2031                                                 rval_cache_clean(&c2);
2032                                                 rval_destroy(rv1);
2033                                                 break;
2034                                         }
2035                                         if (rv2==0){
2036                                                 /* int */
2037                                                 ret=int_intop2(res, rve->op, 0 /* undef */, i2);
2038                                         }else{
2039                                                 /* str or undef */
2040                                                 ret=rval_str_lop2(h, msg, res, rve->op, rv1, &c1,
2041                                                                                         rv2, &c2);
2042                                                 rval_cache_clean(&c2);
2043                                                 rval_destroy(rv2);
2044                                         }
2045 #endif /* UNDEF_EQ_* */
2046                                         rval_cache_clean(&c1);
2047                                         rval_destroy(rv1);
2048                                 }else{
2049                                         /* left value == defined and != int => str
2050                                          * => lval == (str) val */
2051                                         if (unlikely((rv2=rval_expr_eval(h, msg,
2052                                                                                                                 rve->right.rve))==0)){
2053                                                 /* error */
2054                                                 rval_destroy(rv1);
2055                                                 rval_cache_clean(&c1);
2056                                                 break;
2057                                         }
2058                                         ret=rval_str_lop2(h, msg, res, rve->op, rv1, &c1, rv2, 0);
2059                                         rval_cache_clean(&c1);
2060                                         rval_destroy(rv1);
2061                                         rval_destroy(rv2);
2062                                 }
2063                         }
2064                         break;
2065                 case RVE_CONCAT_OP:
2066                         /* eval expression => string */
2067                         if (unlikely((rv1=rval_expr_eval(h, msg, rve))==0)){
2068                                 ret=-1;
2069                                 break;
2070                         }
2071                         /* convert to int */
2072                         ret=rval_get_int(h, msg, res, rv1, 0); /* convert to int */
2073                         rval_get_int_handle_ret(ret, "rval expression conversion to int"
2074                                                                                 " failed", rve);
2075                         rval_destroy(rv1);
2076                         break;
2077                 case RVE_STR_OP:
2078                         /* (str)expr => eval expression */
2079                         rval_cache_init(&c1);
2080                         if (unlikely((ret=rval_expr_eval_rvint(h, msg, &rv1, res,
2081                                                                                                         rve->left.rve, &c1))<0)){
2082                                 /* error */
2083                                 rval_cache_clean(&c1);
2084                                 break;
2085                         }
2086                         if (unlikely(rv1)){
2087                                 /* expr evaluated to string => (int)(str)v == (int)v */
2088                                 ret=rval_get_int(h, msg, res, rv1, &c1); /* convert to int */
2089                                 rval_get_int_handle_ret(ret, "rval expression conversion"
2090                                                                                                 " to int failed", rve);
2091                                 rval_destroy(rv1);
2092                                 rval_cache_clean(&c1);
2093                         } /* else (rv1==0)
2094                                  => expr evaluated to int => 
2095                                  return (int)(str)v == (int)v => do nothing */
2096                         break;
2097
2098 #if 0
2099                         /* same thing as above, but in a not optimized, easier to
2100                            understand way */
2101                         /* 1. (str) expr => eval expr */
2102                         if (unlikely((rv1=rval_expr_eval(h, msg, rve->left.rve))==0)){
2103                                 ret=-1;
2104                                 break;
2105                         }
2106                         /* 2. convert to str and then convert to int
2107                            but since (int)(str)v == (int)v skip over (str)v */
2108                         ret=rval_get_int(h, msg, res, rv1, 0); /* convert to int */
2109                         rval_destroy(rv1);
2110                         break;
2111 #endif
2112                 case RVE_DEFINED_OP:
2113                         ret=int_rve_defined(h, msg, res, rve->left.rve);
2114                         break;
2115                 case RVE_STREQ_OP:
2116                 case RVE_STRDIFF_OP:
2117                 case RVE_MATCH_OP:
2118                         if (unlikely((rv1=rval_expr_eval(h, msg, rve->left.rve))==0)){
2119                                 ret=-1;
2120                                 break;
2121                         }
2122                         if (unlikely((rv2=rval_expr_eval(h, msg, rve->right.rve))==0)){
2123                                 rval_destroy(rv1);
2124                                 ret=-1;
2125                                 break;
2126                         }
2127                         ret=rval_str_lop2(h, msg, res, rve->op, rv1, 0, rv2, 0);
2128                         rval_destroy(rv1);
2129                         rval_destroy(rv2);
2130                         break;
2131                 case RVE_STRLEN_OP:
2132                 case RVE_STREMPTY_OP:
2133                         if (unlikely((rv1=rval_expr_eval(h, msg, rve->left.rve))==0)){
2134                                         ret=-1;
2135                                         break;
2136                         }
2137                         ret=rval_int_strop1(h, msg, res, rve->op, rv1, 0);
2138                         rval_destroy(rv1);
2139                         break;
2140                 case RVE_NONE_OP:
2141                 /*default:*/
2142                         BUG("invalid rval int expression operation %d (%d,%d-%d,%d)\n",
2143                                         rve->op, rve->fpos.s_line, rve->fpos.s_col,
2144                                         rve->fpos.e_line, rve->fpos.e_col);
2145                         ret=-1;
2146         };
2147         return ret;
2148 }
2149
2150
2151
2152 /**
2153  * @brief Evals a rval expression into an int or another rv(str)
2154  * @warning rv result (rv_res) must be rval_destroy()'ed if non-null
2155  * (it might be a reference to another rval). The result can be
2156  * modified only if rv_chg_in_place() returns true.
2157  * @param h run action context
2158  * @param msg SIP message
2159  * @param res_rv pointer to rvalue result, if non-null it means the 
2160  * expression evaluated to a non-int (str), which will be stored here.
2161  * @param res_i pointer to int result, if res_rv==0 and the function
2162  * returns success => the result is an int which will be stored here.
2163  * @param rve expression that will be evaluated.
2164  * @param cache write-only value cache, it might be filled if non-null and
2165  * empty (rval_cache_init()). If non-null, it _must_ be rval_cache_clean()'ed
2166  * when done. 
2167  * @return 0 on success, -1 on error, sets *res_rv or *res_i.
2168  */
2169 int rval_expr_eval_rvint(                          struct run_act_ctx* h,
2170                                                                            struct sip_msg* msg,
2171                                                                            struct rvalue** res_rv,
2172                                                                            int* res_i,
2173                                                                            struct rval_expr* rve,
2174                                                                            struct rval_cache* cache
2175                                                                            )
2176 {
2177         struct rvalue* rv1;
2178         struct rvalue* rv2;
2179         struct rval_cache c1; /* local cache */
2180         int ret;
2181         int r, i, j;
2182         enum rval_type type;
2183         
2184         rv1=0;
2185         rv2=0;
2186         ret=-1;
2187         switch(rve->op){
2188                 case RVE_RVAL_OP:
2189                         rv1=&rve->left.rval;
2190                         rv_ref(rv1);
2191                         type=rval_get_btype(h, msg, rv1, cache);
2192                         if (type==RV_INT){
2193                                         r=rval_get_int(h, msg, res_i, rv1, cache);
2194                                         rval_get_int_handle_ret(r, "rval expression conversion"
2195                                                                                                 " to int failed", rve);
2196                                         *res_rv=0;
2197                                         ret=r; /* equiv. to if (r<0) goto error */
2198                         }else{
2199                                 /* RV_STR, RV_PVAR, RV_AVP a.s.o => return rv1 and the 
2200                                    cached resolved value in cache*/
2201                                         *res_rv=rv1;
2202                                         rv_ref(rv1);
2203                                         ret=0;
2204                         }
2205                         break;
2206                 case RVE_UMINUS_OP:
2207                 case RVE_BOOL_OP:
2208                 case RVE_LNOT_OP:
2209                 case RVE_BNOT_OP:
2210                 case RVE_MINUS_OP:
2211                 case RVE_MUL_OP:
2212                 case RVE_DIV_OP:
2213                 case RVE_MOD_OP:
2214                 case RVE_BOR_OP:
2215                 case RVE_BAND_OP:
2216                 case RVE_BXOR_OP:
2217                 case RVE_BLSHIFT_OP:
2218                 case RVE_BRSHIFT_OP:
2219                 case RVE_LAND_OP:
2220                 case RVE_LOR_OP:
2221                 case RVE_GT_OP:
2222                 case RVE_GTE_OP:
2223                 case RVE_LT_OP:
2224                 case RVE_LTE_OP:
2225                 case RVE_EQ_OP:
2226                 case RVE_DIFF_OP:
2227                 case RVE_IEQ_OP:
2228                 case RVE_IDIFF_OP:
2229                 case RVE_IPLUS_OP:
2230                 case RVE_STREQ_OP:
2231                 case RVE_STRDIFF_OP:
2232                 case RVE_MATCH_OP:
2233                 case RVE_STRLEN_OP:
2234                 case RVE_STREMPTY_OP:
2235                 case RVE_DEFINED_OP:
2236                 case RVE_INT_OP:
2237                         /* operator forces integer type */
2238                         ret=rval_expr_eval_int(h, msg, res_i, rve);
2239                         *res_rv=0;
2240                         break;
2241                 case RVE_PLUS_OP:
2242                         rval_cache_init(&c1);
2243                         r=rval_expr_eval_rvint(h, msg, &rv1, &i, rve->left.rve, &c1);
2244                         if (unlikely(r<0)){
2245                                 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2246                                                 rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
2247                                                 rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col
2248                                         );
2249                                 rval_cache_clean(&c1);
2250                                 goto error;
2251                         }
2252                         if (rv1==0){
2253                                 if (unlikely((r=rval_expr_eval_int(h, msg, &j,
2254                                                                                                                 rve->right.rve))<0)){
2255                                                 ERR("rval expression evaluation failed (%d,%d-%d,%d)"
2256                                                                 "\n", rve->right.rve->fpos.s_line,
2257                                                                 rve->right.rve->fpos.s_col,
2258                                                                 rve->right.rve->fpos.e_line,
2259                                                                 rve->right.rve->fpos.e_col);
2260                                                 rval_cache_clean(&c1);
2261                                                 goto error;
2262                                 }
2263                                 ret=int_intop2(res_i, rve->op, i, j);
2264                                 *res_rv=0;
2265                         }else{
2266                                 rv2=rval_expr_eval(h, msg, rve->right.rve);
2267                                 if (unlikely(rv2==0)){
2268                                         ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2269                                                                 rve->right.rve->fpos.s_line,
2270                                                                 rve->right.rve->fpos.s_col,
2271                                                                 rve->right.rve->fpos.e_line,
2272                                                                 rve->right.rve->fpos.e_col);
2273                                         rval_cache_clean(&c1);
2274                                         goto error;
2275                                 }
2276                                 *res_rv=rval_str_add2(h, msg, rv1, &c1, rv2, 0);
2277                                 ret=-(*res_rv==0);
2278                         }
2279                         rval_cache_clean(&c1);
2280                         break;
2281                 case RVE_CONCAT_OP:
2282                 case RVE_STR_OP:
2283                         *res_rv=rval_expr_eval(h, msg, rve);
2284                         ret=-(*res_rv==0);
2285                         break;
2286                 case RVE_NONE_OP:
2287                 /*default:*/
2288                         BUG("invalid rval expression operation %d (%d,%d-%d,%d)\n",
2289                                         rve->op, rve->fpos.s_line, rve->fpos.s_col,
2290                                         rve->fpos.e_line, rve->fpos.e_col);
2291                         goto error;
2292         };
2293         rval_destroy(rv1);
2294         rval_destroy(rv2);
2295         return ret;
2296 error:
2297         rval_destroy(rv1);
2298         rval_destroy(rv2);
2299         return -1;
2300 }
2301
2302
2303
2304 /**
2305  * @brief Evals a rval expression
2306  * @warning result must be rval_destroy()'ed if non-null (it might be
2307  * a reference to another rval). The result can be modified only
2308  * if rv_chg_in_place() returns true.
2309  * @param h run action context
2310  * @param msg SIP message
2311  * @param rve rvalue expression
2312  * @return rvalue on success, 0 on error
2313  */
2314 struct rvalue* rval_expr_eval(struct run_act_ctx* h, struct sip_msg* msg,
2315                                                                 struct rval_expr* rve)
2316 {
2317         struct rvalue* rv1;
2318         struct rvalue* rv2;
2319         struct rvalue* ret;
2320         struct rval_cache c1;
2321         union rval_val v;
2322         int r, i, j;
2323         enum rval_type type;
2324         
2325         rv1=0;
2326         rv2=0;
2327         ret=0;
2328         switch(rve->op){
2329                 case RVE_RVAL_OP:
2330                         rv_ref(&rve->left.rval);
2331                         return &rve->left.rval;
2332                         break;
2333                 case RVE_UMINUS_OP:
2334                 case RVE_BOOL_OP:
2335                 case RVE_LNOT_OP:
2336                 case RVE_BNOT_OP:
2337                 case RVE_MINUS_OP:
2338                 case RVE_MUL_OP:
2339                 case RVE_DIV_OP:
2340                 case RVE_MOD_OP:
2341                 case RVE_BOR_OP:
2342                 case RVE_BAND_OP:
2343                 case RVE_BXOR_OP:
2344                 case RVE_BLSHIFT_OP:
2345                 case RVE_BRSHIFT_OP:
2346                 case RVE_LAND_OP:
2347                 case RVE_LOR_OP:
2348                 case RVE_GT_OP:
2349                 case RVE_GTE_OP:
2350                 case RVE_LT_OP:
2351                 case RVE_LTE_OP:
2352                 case RVE_EQ_OP:
2353                 case RVE_DIFF_OP:
2354                 case RVE_IEQ_OP:
2355                 case RVE_IDIFF_OP:
2356                 case RVE_IPLUS_OP:
2357                 case RVE_STREQ_OP:
2358                 case RVE_STRDIFF_OP:
2359                 case RVE_MATCH_OP:
2360                 case RVE_STRLEN_OP:
2361                 case RVE_STREMPTY_OP:
2362                 case RVE_DEFINED_OP:
2363                 case RVE_INT_OP:
2364                         /* operator forces integer type */
2365                         r=rval_expr_eval_int(h, msg, &i, rve);
2366                         if (likely(r==0)){
2367                                 v.l=i;
2368                                 ret=rval_new(RV_INT, &v, 0);
2369                                 if (unlikely(ret==0)){
2370                                         ERR("rv eval int expression: out of memory\n");
2371                                         goto error;
2372                                 }
2373                                 return ret;
2374                         }else{
2375                                 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2376                                                 rve->fpos.s_line, rve->fpos.s_col,
2377                                                 rve->fpos.e_line, rve->fpos.e_col);
2378                                 goto error;
2379                         }
2380                         break;
2381                 case RVE_PLUS_OP:
2382                         rv1=rval_expr_eval(h, msg, rve->left.rve);
2383                         if (unlikely(rv1==0)){
2384                                 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2385                                                 rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
2386                                                 rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col);
2387                                 goto error;
2388                         }
2389                         rval_cache_init(&c1);
2390                         type=rval_get_btype(h, msg, rv1, &c1);
2391                         switch(type){
2392                                 case RV_INT:
2393                                         r=rval_get_int(h, msg, &i, rv1, &c1);
2394                                         rval_get_int_handle_ret(r, "rval expression left side "
2395                                                                                                 "conversion to int failed",
2396                                                                                         rve);
2397                                         if (unlikely(r<0)){
2398                                                 rval_cache_clean(&c1);
2399                                                 goto error;
2400                                         }
2401                                         if (unlikely((r=rval_expr_eval_int(h, msg, &j,
2402                                                                                                                 rve->right.rve))<0)){
2403                                                 rval_cache_clean(&c1);
2404                                                 ERR("rval expression evaluation failed (%d,%d-%d,%d):"
2405                                                                 " could not evaluate right side to int\n",
2406                                                                 rve->fpos.s_line, rve->fpos.s_col,
2407                                                                 rve->fpos.e_line, rve->fpos.e_col);
2408                                                 goto error;
2409                                         }
2410                                         int_intop2(&r, rve->op, i, j);
2411                                         if (rv_chg_in_place(rv1)){
2412                                                 rv1->v.l=r;
2413                                                 ret=rv1;
2414                                                 rv_ref(ret);
2415                                         }else{
2416                                                 v.l=r;
2417                                                 ret=rval_new(RV_INT, &v, 0);
2418                                                 if (unlikely(ret==0)){
2419                                                         rval_cache_clean(&c1);
2420                                                         ERR("rv eval int expression: out of memory\n");
2421                                                         goto error;
2422                                                 }
2423                                         }
2424                                         break;
2425                                 case RV_STR:
2426                                 case RV_NONE:
2427                                         rv2=rval_expr_eval(h, msg, rve->right.rve);
2428                                         if (unlikely(rv2==0)){
2429                                                 ERR("rval expression evaluation failed (%d,%d-%d,%d)"
2430                                                                 "\n", rve->right.rve->fpos.s_line,
2431                                                                 rve->right.rve->fpos.s_col,
2432                                                                 rve->right.rve->fpos.e_line,
2433                                                                 rve->right.rve->fpos.e_col);
2434                                                 rval_cache_clean(&c1);
2435                                                 goto error;
2436                                         }
2437                                         ret=rval_str_add2(h, msg, rv1, &c1, rv2, 0);
2438                                         break;
2439                                 default:
2440                                         BUG("rv unsupported basic type %d (%d,%d-%d,%d)\n", type,
2441                                                         rve->fpos.s_line, rve->fpos.s_col,
2442                                                         rve->fpos.e_line, rve->fpos.e_col);
2443                         }
2444                         rval_cache_clean(&c1);
2445                         break;
2446                 case RVE_CONCAT_OP:
2447                         rv1=rval_expr_eval(h, msg, rve->left.rve);
2448                         if (unlikely(rv1==0)){
2449                                 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2450                                                 rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
2451                                                 rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col);
2452                                 goto error;
2453                         }
2454                         rv2=rval_expr_eval(h, msg, rve->right.rve);
2455                         if (unlikely(rv2==0)){
2456                                 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2457                                                 rve->right.rve->fpos.s_line,
2458                                                 rve->right.rve->fpos.s_col,
2459                                                 rve->right.rve->fpos.e_line,
2460                                                 rve->right.rve->fpos.e_col);
2461                                 goto error;
2462                         }
2463                         ret=rval_str_add2(h, msg, rv1, 0, rv2, 0);
2464                         break;
2465                 case RVE_STR_OP:
2466                         rv1=rval_expr_eval(h, msg, rve->left.rve);
2467                         if (unlikely(rv1==0)){
2468                                 ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
2469                                                 rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
2470                                                 rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col);
2471                                 goto error;
2472                         }
2473                         ret=rval_convert(h, msg, RV_STR, rv1, 0);
2474                         break;
2475                 case RVE_NONE_OP:
2476                 /*default:*/
2477                         BUG("invalid rval expression operation %d (%d,%d-%d,%d)\n",
2478                                         rve->op, rve->fpos.s_line, rve->fpos.s_col,
2479                                         rve->fpos.e_line, rve->fpos.e_col);
2480                         goto error;
2481         };
2482         rval_destroy(rv1);
2483         rval_destroy(rv2);
2484         return ret;
2485 error:
2486         rval_destroy(rv1);
2487         rval_destroy(rv2);
2488         return 0;
2489 }
2490
2491
2492
2493 /** evals a rval expr and always returns a new rval.
2494  * like rval_expr_eval, but always returns a new rvalue (never a reference
2495  * to an exisiting one).
2496  * WARNING: result must be rval_destroy()'ed if non-null (it might be
2497  * a reference to another rval). The result can be modified only
2498  * if rv_chg_in_place() returns true.
2499  * @result rvalue on success, 0 on error
2500  */
2501 struct rvalue* rval_expr_eval_new(struct run_act_ctx* h, struct sip_msg* msg,
2502                                                                 struct rval_expr* rve)
2503 {
2504         struct rvalue* ret;
2505         struct rvalue* rv;
2506         
2507         ret=rval_expr_eval(h, msg, rve);
2508         if (ret && !rv_chg_in_place(ret)){
2509                 rv=ret;
2510                 /* create a new rv */
2511                 ret=rval_new(rv->type, &rv->v, 0);
2512                 rval_destroy(rv);
2513         }
2514         return ret;
2515 }
2516
2517
2518
2519 /** create a RVE_RVAL_OP rval_expr, containing a single rval of the given type.
2520  *
2521  * @param rv_type - rval type
2522  * @param val     - rval value
2523  * @param pos     - config position
2524  * @return new pkg_malloc'ed rval_expr or 0 on error.
2525  */
2526 struct rval_expr* mk_rval_expr_v(enum rval_type rv_type, void* val, 
2527                                                                         struct cfg_pos* pos)
2528 {
2529         struct rval_expr* rve;
2530         union rval_val v;
2531         str* s;
2532         int flags;
2533         
2534         rve=pkg_malloc(sizeof(*rve));
2535         if (rve==0) 
2536                 return 0;
2537         memset(rve, 0, sizeof(*rve));
2538         flags=0;
2539         switch(rv_type){
2540                 case RV_INT:
2541                         v.l=(long)val;
2542                         break;
2543                 case RV_STR:
2544                         s=(str*)val;
2545                         v.s.s=pkg_malloc(s->len+1 /*0*/);
2546                         if (v.s.s==0){
2547                                 ERR("memory allocation failure\n");
2548                                 return 0;
2549                         }
2550                         v.s.len=s->len;
2551                         memcpy(v.s.s, s->s, s->len);
2552                         v.s.s[s->len]=0;
2553                         flags=RV_CNT_ALLOCED_F;
2554                         break;
2555                 case RV_AVP:
2556                         v.avps=*(avp_spec_t*)val;
2557                         break;
2558                 case RV_PVAR:
2559                         v.pvs=*(pv_spec_t*)val;
2560                         break;
2561                 case RV_SEL:
2562                         v.sel=*(select_t*)val;
2563                         break;
2564                 case RV_BEXPR:
2565                         v.bexpr=(struct expr*)val;
2566                         break;
2567                 case RV_ACTION_ST:
2568                         v.action=(struct action*)val;
2569                         break;
2570                 default:
2571                         BUG("unsupported rv type %d\n", rv_type);
2572                         pkg_free(rve);
2573                         return 0;
2574         }
2575         rval_init(&rve->left.rval, rv_type, &v, flags);
2576         rve->op=RVE_RVAL_OP;
2577         if (pos) rve->fpos=*pos;
2578         return rve;
2579 }
2580
2581
2582
2583 /**
2584  * @brief Create a unary op. rval_expr
2585  * ret= op rve1
2586  * @param op   - rval expr. unary operator
2587  * @param rve1 - rval expr. on which the operator will act.
2588  * @param pos configuration position
2589  * @return new pkg_malloc'ed rval_expr or 0 on error.
2590  */
2591 struct rval_expr* mk_rval_expr1(enum rval_expr_op op, struct rval_expr* rve1,
2592                                                                 struct cfg_pos* pos)
2593 {
2594         struct rval_expr* ret;
2595         
2596         switch(op){
2597                 case RVE_UMINUS_OP:
2598                 case RVE_BOOL_OP:
2599                 case RVE_LNOT_OP:
2600                 case RVE_BNOT_OP:
2601                 case RVE_STRLEN_OP:
2602                 case RVE_STREMPTY_OP:
2603                 case RVE_DEFINED_OP:
2604                 case RVE_INT_OP:
2605                 case RVE_STR_OP:
2606                         break;
2607                 default:
2608                         BUG("unsupported unary operator %d\n", op);
2609                         return 0;
2610         }
2611         ret=pkg_malloc(sizeof(*ret));
2612         if (ret==0) 
2613                 return 0;
2614         memset(ret, 0, sizeof(*ret));
2615         ret->op=op;
2616         ret->left.rve=rve1;
2617         if (pos) ret->fpos=*pos;
2618         return ret;
2619 }
2620
2621
2622
2623 /**
2624  * @brief Create a rval_expr. from 2 other rval exprs, using op
2625  * ret = rve1 op rve2
2626  * @param op   - rval expr. operator
2627  * @param rve1 - rval expr. on which the operator will act.
2628  * @param rve2 - rval expr. on which the operator will act.
2629  * @param pos configuration position
2630  * @return new pkg_malloc'ed rval_expr or 0 on error.
2631  */
2632 struct rval_expr* mk_rval_expr2(enum rval_expr_op op, struct rval_expr* rve1,
2633                                                                                                           struct rval_expr* rve2,
2634                                                                                                           struct cfg_pos* pos)
2635 {
2636         struct rval_expr* ret;
2637         
2638         switch(op){
2639                 case RVE_MUL_OP:
2640                 case RVE_DIV_OP:
2641                 case RVE_MOD_OP:
2642                 case RVE_MINUS_OP:
2643                 case RVE_BOR_OP:
2644                 case RVE_BAND_OP:
2645                 case RVE_BXOR_OP:
2646                 case RVE_BLSHIFT_OP:
2647                 case RVE_BRSHIFT_OP:
2648                 case RVE_LAND_OP:
2649                 case RVE_LOR_OP:
2650                 case RVE_GT_OP:
2651                 case RVE_GTE_OP:
2652                 case RVE_LT_OP:
2653                 case RVE_LTE_OP:
2654                 case RVE_PLUS_OP:
2655                 case RVE_IPLUS_OP:
2656                 case RVE_EQ_OP:
2657                 case RVE_DIFF_OP:
2658                 case RVE_IEQ_OP:
2659                 case RVE_IDIFF_OP:
2660                 case RVE_STREQ_OP:
2661                 case RVE_STRDIFF_OP:
2662                 case RVE_MATCH_OP:
2663                 case RVE_CONCAT_OP:
2664                         break;
2665                 default:
2666                         BUG("unsupported operator %d\n", op);
2667                         return 0;
2668         }
2669         ret=pkg_malloc(sizeof(*ret));
2670         if (ret==0) 
2671                 return 0;
2672         memset(ret, 0, sizeof(*ret));
2673         ret->op=op;
2674         ret->left.rve=rve1;
2675         ret->right.rve=rve2;
2676         if (pos) ret->fpos=*pos;
2677         return ret;
2678 }
2679
2680
2681
2682 /** returns true if the operator is associative. */
2683 static int rve_op_is_assoc(enum rval_expr_op op)
2684 {
2685         switch(op){
2686                 case RVE_NONE_OP:
2687                 case RVE_RVAL_OP:
2688                 case RVE_UMINUS_OP:
2689                 case RVE_BOOL_OP:
2690                 case RVE_LNOT_OP:
2691                 case RVE_BNOT_OP:
2692                 case RVE_STRLEN_OP:
2693                 case RVE_STREMPTY_OP:
2694                 case RVE_DEFINED_OP:
2695                 case RVE_INT_OP:
2696                 case RVE_STR_OP:
2697                         /* one operand expression => cannot be assoc. */
2698                         return 0;
2699                 case RVE_DIV_OP:
2700                 case RVE_MOD_OP:
2701                 case RVE_MINUS_OP:
2702                 case RVE_BLSHIFT_OP:
2703                 case RVE_BRSHIFT_OP:
2704                         return 0;
2705                 case RVE_PLUS_OP:
2706                         /* the generic plus is not assoc, e.g.
2707                            "a" + 1 + "2" => "a12" in one case and "a3" in the other */
2708                         return 0;
2709                 case RVE_IPLUS_OP:
2710                 case RVE_CONCAT_OP:
2711                 case RVE_MUL_OP:
2712                 case RVE_BAND_OP:
2713                 case RVE_BOR_OP:
2714                 case RVE_BXOR_OP:
2715                         return 1;
2716                 case RVE_LAND_OP:
2717                 case RVE_LOR_OP:
2718                         return 1;
2719                 case RVE_GT_OP:
2720                 case RVE_GTE_OP:
2721                 case RVE_LT_OP:
2722                 case RVE_LTE_OP:
2723                 case RVE_EQ_OP:
2724                 case RVE_DIFF_OP:
2725                 case RVE_IEQ_OP:
2726                 case RVE_IDIFF_OP:
2727                 case RVE_STREQ_OP:
2728                 case RVE_STRDIFF_OP:
2729                 case RVE_MATCH_OP:
2730                         return 0;
2731         }
2732         return 0;
2733 }
2734
2735
2736
2737 /** returns true if the operator is commutative. */
2738 static int rve_op_is_commutative(enum rval_expr_op op)
2739 {
2740         switch(op){
2741                 case RVE_NONE_OP:
2742                 case RVE_RVAL_OP:
2743                 case RVE_UMINUS_OP:
2744                 case RVE_BOOL_OP:
2745                 case RVE_LNOT_OP:
2746                 case RVE_BNOT_OP:
2747                 case RVE_STRLEN_OP:
2748                 case RVE_STREMPTY_OP:
2749                 case RVE_DEFINED_OP:
2750                 case RVE_INT_OP:
2751                 case RVE_STR_OP:
2752                         /* one operand expression => cannot be commut. */
2753                         return 0;
2754                 case RVE_DIV_OP:
2755                 case RVE_MOD_OP:
2756                 case RVE_MINUS_OP:
2757                 case RVE_BLSHIFT_OP:
2758                 case RVE_BRSHIFT_OP:
2759                         return 0;
2760                 case RVE_PLUS_OP:
2761                         /* non commut. when diff. type 
2762                            (e.g 1 + "2" != "2" + 1 ) => non commut. in general
2763                            (specific same type versions are covered by IPLUS & CONCAT) */
2764                         return 0;
2765                 case RVE_IPLUS_OP:
2766                 case RVE_MUL_OP:
2767                 case RVE_BAND_OP:
2768                 case RVE_BOR_OP:
2769                 case RVE_BXOR_OP:
2770                 case RVE_LAND_OP:
2771                 case RVE_LOR_OP:
2772                 case RVE_IEQ_OP:
2773                 case RVE_IDIFF_OP:
2774                 case RVE_STREQ_OP:
2775                 case RVE_STRDIFF_OP:
2776                         return 1;
2777                 case RVE_GT_OP:
2778                 case RVE_GTE_OP:
2779                 case RVE_LT_OP:
2780                 case RVE_LTE_OP:
2781                 case RVE_CONCAT_OP:
2782                 case RVE_MATCH_OP:
2783                         return 0;
2784                 case RVE_DIFF_OP:
2785                 case RVE_EQ_OP:
2786                         /* non. commut. in general, only for same type e.g.:
2787                            "" == 0  diff. 0 == "" ( "" == "0" and 0 == 0)
2788                            same type versions are covered by IEQ, IDIFF, STREQ, STRDIFF
2789                          */
2790                         return 0 /* asymmetrical undef handling */;
2791         }
2792         return 0;
2793 }
2794
2795
2796 #if 0
2797 /** returns true if the rval expr can be optimized to an int.
2798  *  (if left & right are leafs (RVE_RVAL_OP) and both of them are
2799  *   ints return true, else false)
2800  *  @return 0 or 1
2801  */
2802 static int rve_can_optimize_int(struct rval_expr* rve)
2803 {
2804         if (scr_opt_lev<1)
2805                 return 0;
2806         if (rve->op == RVE_RVAL_OP)
2807                 return 0;
2808         if (rve->left.rve->op != RVE_RVAL_OP)
2809                 return 0;
2810         if (rve->left.rve->left.rval.type!=RV_INT)
2811                 return 0;
2812         if (rve->right.rve){
2813                 if  (rve->right.rve->op != RVE_RVAL_OP)
2814                         return 0;
2815                 if (rve->right.rve->left.rval.type!=RV_INT)
2816                         return 0;
2817         }
2818         DBG("rve_can_optimize_int: left %d, right %d\n", 
2819                         rve->left.rve->op, rve->right.rve?rve->right.rve->op:0);
2820         return 1;
2821 }
2822
2823
2824
2825 /** returns true if the rval expr can be optimized to a str.
2826  *  (if left & right are leafs (RVE_RVAL_OP) and both of them are
2827  *   str or left is str and right is int return true, else false)
2828  *  @return 0 or 1
2829  */
2830 static int rve_can_optimize_str(struct rval_expr* rve)
2831 {
2832         if (scr_opt_lev<1)
2833                 return 0;
2834         if (rve->op == RVE_RVAL_OP)
2835                 return 0;
2836         DBG("rve_can_optimize_str: left %d, right %d\n", 
2837                         rve->left.rve->op, rve->right.rve?rve->right.rve->op:0);
2838         if (rve->left.rve->op != RVE_RVAL_OP)
2839                 return 0;
2840         if (rve->left.rve->left.rval.type!=RV_STR)
2841                 return 0;
2842         if (rve->right.rve){
2843                 if  (rve->right.rve->op != RVE_RVAL_OP)
2844                         return 0;
2845                 if ((rve->right.rve->left.rval.type!=RV_STR) && 
2846                                 (rve->right.rve->left.rval.type!=RV_INT))
2847                         return 0;
2848         }
2849         return 1;
2850 }
2851 #endif
2852
2853
2854
2855 static int fix_rval(struct rvalue* rv)
2856 {
2857         DBG("RV fixing type %d\n", rv->type);
2858         switch(rv->type){
2859                 case RV_INT:
2860                         /*nothing to do*/
2861                         DBG("RV is int: %d\n", (int)rv->v.l);
2862                         return 0;
2863                 case RV_STR:
2864                         /*nothing to do*/
2865                         DBG("RV is str: \"%s\"\n", rv->v.s.s);
2866                         return 0;
2867                 case RV_BEXPR:
2868                         return fix_expr(rv->v.bexpr);
2869                 case RV_ACTION_ST:
2870                         return fix_actions(rv->v.action);
2871                 case RV_SEL:
2872                         if (resolve_select(&rv->v.sel)<0){
2873                                 ERR("Unable to resolve select\n");
2874                                 print_select(&rv->v.sel);
2875                         }
2876                         return 0;
2877                 case RV_AVP:
2878                         /* nothing to do, resolved at runtime */
2879                         return 0;
2880                 case RV_PVAR:
2881                         /* nothing to do, resolved at parsing time */
2882                         return 0;
2883                 case RV_NONE:
2884                         BUG("uninitialized rvalue\n");
2885                         return -1;
2886         }
2887         BUG("unknown rvalue type %d\n", rv->type);
2888         return -1;
2889 }
2890
2891
2892
2893 /**
2894  * @brief Helper function: replace a rve (in-place) with a constant rval_val
2895  * @warning since it replaces in-place, one should make sure that if
2896  * rve is in fact a rval (rve->op==RVE_RVAL_OP), no reference is kept
2897  * to the rval!
2898  * @param rve expression to be replaced (in-place)
2899  * @param type rvalue type
2900  * @param v pointer to a rval_val union containing the replacement value.