rvalue.c 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858
  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. * @file
  18. * @brief SIP-router core :: rvalue expressions
  19. * @ingroup core
  20. * Module: \ref core
  21. */
  22. /*
  23. * History:
  24. * --------
  25. * 2008-12-01 initial version (andrei)
  26. * 2009-04-24 added support for defined, strempty, strlen (andrei)
  27. * 2009-04-28 int and str automatic conversions: (int)undef=0,
  28. * (str)undef="", (int)""=0, (int)"123"=123, (int)"abc"=0
  29. * handle undef == expr, in function of the UNDEF_EQ_* defines.
  30. * (andrei)
  31. * 2009-05-05 casts operator for int & string (andrei)
  32. */
  33. /* special defines:
  34. *
  35. * UNDEF_EQ_* - how to behave when undef is on the right side of a generic
  36. * compare operator
  37. * UNDEF_EQ_ALWAYS_FALSE: undef == something is always false
  38. * UNDEF_EQ_UNDEF_TRUE : undef == something false except for undef==undef
  39. * which is true
  40. * no UNDEF_EQ* define : undef == expr => convert undef to typeof(expr)
  41. * and perform normal ==. undef == undef will be
  42. * converted to string and it will be true
  43. * ("" == "")
  44. * NOTE: expr == undef, with defined(expr) is always evaluated this way:
  45. expr == (type_of(expr))undef
  46. * RV_STR2INT_VERBOSE_ERR - if a string conversion to int fails, log (L_WARN)
  47. * the string that caused it (only the string, not
  48. * the expression position).
  49. * RV_STR2INT_ERR - if a string conversion to int fails, don't ignore
  50. * the error (return error).
  51. * RVAL_GET_INT_ERR_WARN - if a conversion to int fails, log a warning with
  52. * the expression position.
  53. * Depends on RV_STR2INT_ERR.
  54. * RVAL_GET_INT_ERR_IGN - if a conversion to int fails, ignore the error
  55. * (the result will be 0). Can be combined with
  56. * RVAL_GET_INT_ERR_WARN.
  57. * Depends on RV_STR2INT_ERR.
  58. */
  59. #include "rvalue.h"
  60. #include <stdlib.h> /* abort() */
  61. /* if defined warn when str2int conversions fail */
  62. #define RV_STR2INT_VERBOSE_ERR
  63. /* if defined rval_get_int will fail if str2int conversion fail
  64. (else convert to 0) */
  65. #define RV_STR2INT_ERR
  66. /* if a rval_get_int fails (conversion to int), warn
  67. Depends on RV_STR2INT_ERR.
  68. */
  69. #define RVAL_GET_INT_ERR_WARN
  70. /* if a rval_get_int fails, ignore it (expression evaluation will not fail,
  71. the int conversion will result in 0).
  72. Can be combined with RVAL_GET_INT_ERR_WARN.
  73. Depends on RV_STR2INT_ERR.
  74. */
  75. #define RVAL_GET_INT_ERR_IGN
  76. /* minimum size alloc'ed for STR RVs (to accomodate
  77. * strops without reallocs) */
  78. #define RV_STR_EXTRA 80
  79. #define rv_ref(rv) ((rv)->refcnt++)
  80. /** unref rv and returns true if 0 */
  81. #define rv_unref(rv) ((--(rv)->refcnt)==0)
  82. inline static void rval_force_clean(struct rvalue* rv)
  83. {
  84. if (rv->flags & RV_CNT_ALLOCED_F){
  85. switch(rv->type){
  86. case RV_STR:
  87. pkg_free(rv->v.s.s);
  88. rv->v.s.s=0;
  89. rv->v.s.len=0;
  90. break;
  91. default:
  92. BUG("RV_CNT_ALLOCED_F not supported for type %d\n", rv->type);
  93. }
  94. rv->flags&=~RV_CNT_ALLOCED_F;
  95. }
  96. if (rv->flags & RV_RE_ALLOCED_F){
  97. if (rv->v.re.regex){
  98. if (unlikely(rv->type!=RV_STR || !(rv->flags & RV_RE_F))){
  99. BUG("RV_RE_ALLOCED_F not supported for type %d or "
  100. "bad flags %x\n", rv->type, rv->flags);
  101. }
  102. regfree(rv->v.re.regex);
  103. pkg_free(rv->v.re.regex);
  104. rv->v.re.regex=0;
  105. }
  106. rv->flags&=~(RV_RE_ALLOCED_F|RV_RE_F);
  107. }
  108. }
  109. /** frees a rval returned by rval_new(), rval_convert() or rval_expr_eval().
  110. * Note: it will be freed only when refcnt reaches 0
  111. */
  112. void rval_destroy(struct rvalue* rv)
  113. {
  114. if (rv && rv_unref(rv)){
  115. rval_force_clean(rv);
  116. /* still an un-regfreed RE ? */
  117. if ((rv->flags & RV_RE_F) && rv->v.re.regex){
  118. if (unlikely(rv->type!=RV_STR))
  119. BUG("RV_RE_F not supported for type %d\n", rv->type);
  120. regfree(rv->v.re.regex);
  121. }
  122. if (rv->flags & RV_RV_ALLOCED_F){
  123. pkg_free(rv);
  124. }
  125. }
  126. }
  127. void rval_clean(struct rvalue* rv)
  128. {
  129. if (rv_unref(rv))
  130. rval_force_clean(rv);
  131. }
  132. void rve_destroy(struct rval_expr* rve)
  133. {
  134. if (rve){
  135. if (rve->op==RVE_RVAL_OP){
  136. if (rve->left.rval.refcnt){
  137. if (rve->left.rval.refcnt==1)
  138. rval_destroy(&rve->left.rval);
  139. else
  140. BUG("rval expr rval with invalid refcnt: %d (%d,%d-%d,%d)"
  141. "\n", rve->left.rval.refcnt,
  142. rve->fpos.s_line, rve->fpos.s_col,
  143. rve->fpos.e_line, rve->fpos.e_col);
  144. }
  145. if (rve->right.rval.refcnt){
  146. if (rve->right.rval.refcnt==1)
  147. rval_destroy(&rve->right.rval);
  148. else
  149. BUG("rval expr rval with invalid refcnt: %d (%d,%d-%d,%d)"
  150. "\n", rve->right.rval.refcnt,
  151. rve->fpos.s_line, rve->fpos.s_col,
  152. rve->fpos.e_line, rve->fpos.e_col);
  153. }
  154. }else{
  155. if (rve->left.rve)
  156. rve_destroy(rve->left.rve);
  157. if (rve->right.rve)
  158. rve_destroy(rve->right.rve);
  159. }
  160. pkg_free(rve);
  161. }
  162. }
  163. void rval_cache_clean(struct rval_cache* rvc)
  164. {
  165. if ((rvc->cache_type==RV_CACHE_PVAR) && (rvc->val_type!=RV_NONE)){
  166. pv_value_destroy(&rvc->c.pval);
  167. }
  168. rvc->cache_type=RV_CACHE_EMPTY;
  169. rvc->val_type=RV_NONE;
  170. }
  171. #define rv_chg_in_place(rv) ((rv)->refcnt==1)
  172. /** init a rvalue structure.
  173. * Note: not needed if the structure is allocate with one of the
  174. * rval_new* functions
  175. */
  176. void rval_init(struct rvalue* rv, enum rval_type t, union rval_val* v,
  177. int flags)
  178. {
  179. rv->flags=flags;
  180. rv->refcnt=1;
  181. rv->type=t;
  182. if (v){
  183. rv->v=*v;
  184. }else{
  185. memset (&rv->v, 0, sizeof(rv->v));
  186. }
  187. }
  188. /** create a new pk_malloc'ed empty rvalue.
  189. *
  190. * @param extra_size - extra space to allocate
  191. * (e.g.: so that future string operation can reuse
  192. * the space)
  193. * @return new rv or 0 on error
  194. */
  195. struct rvalue* rval_new_empty(int extra_size)
  196. {
  197. struct rvalue* rv;
  198. int size; /* extra size at the end */
  199. size=ROUND_LONG(sizeof(*rv)-sizeof(rv->buf)+extra_size); /* round up */
  200. rv=pkg_malloc(size);
  201. if (likely(rv)){
  202. rv->bsize=size-sizeof(*rv)-sizeof(rv->buf); /* remaining size->buffer*/
  203. rv->flags=RV_RV_ALLOCED_F;
  204. rv->refcnt=1;
  205. rv->type=RV_NONE;
  206. }
  207. return rv;
  208. }
  209. /** create a new pk_malloc'ed rv from a str.
  210. *
  211. * @param s - pointer to str, must be non-null
  212. * @param extra_size - extra space to allocate
  213. * (so that future string operation can reuse
  214. * the space)
  215. * @return new rv or 0 on error
  216. */
  217. struct rvalue* rval_new_str(str* s, int extra_size)
  218. {
  219. struct rvalue* rv;
  220. rv=rval_new_empty(extra_size+s->len+1/* 0 term */);
  221. if (likely(rv)){
  222. rv->type=RV_STR;
  223. rv->v.s.s=&rv->buf[0];
  224. rv->v.s.len=s->len;
  225. memcpy(rv->v.s.s, s->s, s->len);
  226. rv->v.s.s[s->len]=0;
  227. }
  228. return rv;
  229. }
  230. /** create a new pk_malloc'ed RE rv from a str re.
  231. * It acts as rval_new_str, but also compiles a RE from the str
  232. * and sets v->re.regex.
  233. * @param s - pointer to str, must be non-null, zero-term'ed and a valid RE.
  234. * @return new rv or 0 on error
  235. */
  236. struct rvalue* rval_new_re(str* s)
  237. {
  238. struct rvalue* rv;
  239. long offs;
  240. offs=(long)&((struct rvalue*)0)->buf[0]; /* offset of the buf. member */
  241. /* make sure we reserve enough space so that we can satisfy any regex_t
  242. alignment requirement (pointer) */
  243. rv=rval_new_empty(ROUND_POINTER(offs)-offs+sizeof(*rv->v.re.regex)+
  244. s->len+1/* 0 */);
  245. if (likely(rv)){
  246. rv->type=RV_STR;
  247. /* make sure regex points to a properly aligned address
  248. (use max./pointer alignment to be sure ) */
  249. rv->v.re.regex=(regex_t*)((char*)&rv->buf[0]+ROUND_POINTER(offs)-offs);
  250. rv->v.s.s=(char*)rv->v.re.regex+sizeof(*rv->v.re.regex);
  251. rv->v.s.len=s->len;
  252. memcpy(rv->v.s.s, s->s, s->len);
  253. rv->v.s.s[s->len]=0;
  254. /* compile the regex */
  255. /* same flags as for expr. =~ (fix_expr()) */
  256. if (unlikely(regcomp(rv->v.re.regex, s->s,
  257. REG_EXTENDED|REG_NOSUB|REG_ICASE))){
  258. /* error */
  259. pkg_free(rv);
  260. rv=0;
  261. }else /* success */
  262. rv->flags|=RV_RE_F;
  263. }
  264. return rv;
  265. }
  266. /** get string name for a type.
  267. *
  268. * @return - null terminated name of the type
  269. */
  270. char* rval_type_name(enum rval_type type)
  271. {
  272. switch(type){
  273. case RV_NONE:
  274. return "none";
  275. case RV_INT:
  276. return "int";
  277. case RV_STR:
  278. return "str";
  279. case RV_BEXPR:
  280. return "bexpr_t";
  281. case RV_ACTION_ST:
  282. return "action_t";
  283. case RV_PVAR:
  284. return "pvar";
  285. case RV_AVP:
  286. return "avp";
  287. break;
  288. case RV_SEL:
  289. return "select";
  290. }
  291. return "error_unknown_type";
  292. }
  293. /**
  294. * @brief create a new pk_malloc'ed rvalue from a rval_val union
  295. * @param t rvalue type
  296. * @param v rvalue value
  297. * @param extra_size extra space to allocate
  298. * (so that future string operation can reuse the space)
  299. * @return new rv or 0 on error
  300. */
  301. struct rvalue* rval_new(enum rval_type t, union rval_val* v, int extra_size)
  302. {
  303. struct rvalue* rv;
  304. if (t==RV_STR && v && v->s.s)
  305. return rval_new_str(&v->s, extra_size);
  306. rv=rval_new_empty(extra_size);
  307. if (likely(rv)){
  308. rv->type=t;
  309. if (likely(v && t!=RV_STR)){
  310. rv->v=*v;
  311. }else if (t==RV_STR){
  312. rv->v.s.s=&rv->buf[0];
  313. rv->v.s.len=0;
  314. if (likely(extra_size)) rv->v.s.s[0]=0;
  315. }else
  316. memset (&rv->v, 0, sizeof(rv->v));
  317. }
  318. return rv;
  319. }
  320. /**
  321. * @brief get rvalue basic type (RV_INT or RV_STR)
  322. *
  323. * Given a rvalue it tries to determinte its basic type.
  324. * Fills val_cache if non-null and empty (can be used in other rval*
  325. * function calls, to avoid re-resolving avps or pvars). It must be
  326. * rval_cache_clean()'en when no longer needed.
  327. *
  328. * @param h run action context
  329. * @param msg SIP message
  330. * @param rv target rvalue
  331. * @param val_cache write-only value cache, might be filled if non-null,
  332. * it _must_ be rval_cache_clean()'en when done.
  333. * @return basic type or RV_NONE on error
  334. */
  335. inline static enum rval_type rval_get_btype(struct run_act_ctx* h,
  336. struct sip_msg* msg,
  337. struct rvalue* rv,
  338. struct rval_cache* val_cache)
  339. {
  340. avp_t* r_avp;
  341. int_str tmp_avp_val;
  342. int_str* avpv;
  343. pv_value_t tmp_pval;
  344. pv_value_t* pv;
  345. enum rval_type tmp;
  346. enum rval_type* ptype;
  347. switch(rv->type){
  348. case RV_INT:
  349. case RV_STR:
  350. return rv->type;
  351. case RV_BEXPR:
  352. case RV_ACTION_ST:
  353. return RV_INT;
  354. case RV_PVAR:
  355. if (likely(val_cache && val_cache->cache_type==RV_CACHE_EMPTY)){
  356. pv=&val_cache->c.pval;
  357. val_cache->cache_type=RV_CACHE_PVAR;
  358. }else{
  359. val_cache=0;
  360. pv=&tmp_pval;
  361. }
  362. memset(pv, 0, sizeof(tmp_pval));
  363. if (likely(pv_get_spec_value(msg, &rv->v.pvs, pv)==0)){
  364. if (pv->flags & PV_TYPE_INT){
  365. if (likely(val_cache!=0))
  366. val_cache->val_type=RV_INT;
  367. else
  368. pv_value_destroy(pv);
  369. return RV_INT;
  370. }else if (pv->flags & PV_VAL_STR){
  371. if (likely(val_cache!=0))
  372. val_cache->val_type=RV_STR;
  373. else
  374. pv_value_destroy(pv);
  375. return RV_STR;
  376. }else{
  377. pv_value_destroy(pv);
  378. if (likely(val_cache!=0))
  379. val_cache->val_type=RV_NONE; /* undefined */
  380. goto error;
  381. }
  382. }else{
  383. if (likely(val_cache!=0))
  384. val_cache->val_type=RV_NONE; /* undefined */
  385. goto error;
  386. }
  387. break;
  388. case RV_AVP:
  389. if (likely(val_cache && val_cache->cache_type==RV_CACHE_EMPTY)){
  390. ptype=&val_cache->val_type;
  391. avpv=&val_cache->c.avp_val;
  392. val_cache->cache_type=RV_CACHE_AVP;
  393. }else{
  394. ptype=&tmp;
  395. avpv=&tmp_avp_val;
  396. }
  397. r_avp = search_avp_by_index(rv->v.avps.type, rv->v.avps.name,
  398. avpv, rv->v.avps.index);
  399. if (likely(r_avp)){
  400. if (r_avp->flags & AVP_VAL_STR){
  401. *ptype=RV_STR;
  402. return RV_STR;
  403. }else{
  404. *ptype=RV_INT;
  405. return RV_INT;
  406. }
  407. }else{
  408. *ptype=RV_NONE;
  409. goto error;
  410. }
  411. break;
  412. case RV_SEL:
  413. return RV_STR;
  414. default:
  415. BUG("rv type %d not handled\n", rv->type);
  416. }
  417. error:
  418. return RV_NONE;
  419. }
  420. /** guess the type of an expression.
  421. * @return RV_INT, RV_STR or RV_NONE (when type could not be found,
  422. * e.g. avp or pvar)
  423. */
  424. enum rval_type rve_guess_type( struct rval_expr* rve)
  425. {
  426. switch(rve->op){
  427. case RVE_RVAL_OP:
  428. switch(rve->left.rval.type){
  429. case RV_STR:
  430. case RV_SEL:
  431. return RV_STR;
  432. case RV_INT:
  433. case RV_BEXPR:
  434. case RV_ACTION_ST:
  435. return RV_INT;
  436. case RV_PVAR:
  437. case RV_AVP:
  438. case RV_NONE:
  439. return RV_NONE;
  440. }
  441. break;
  442. case RVE_UMINUS_OP:
  443. case RVE_BOOL_OP:
  444. case RVE_LNOT_OP:
  445. case RVE_BNOT_OP:
  446. case RVE_MINUS_OP:
  447. case RVE_MUL_OP:
  448. case RVE_DIV_OP:
  449. case RVE_MOD_OP:
  450. case RVE_BOR_OP:
  451. case RVE_BAND_OP:
  452. case RVE_BXOR_OP:
  453. case RVE_BLSHIFT_OP:
  454. case RVE_BRSHIFT_OP:
  455. case RVE_LAND_OP:
  456. case RVE_LOR_OP:
  457. case RVE_GT_OP:
  458. case RVE_GTE_OP:
  459. case RVE_LT_OP:
  460. case RVE_LTE_OP:
  461. case RVE_EQ_OP:
  462. case RVE_DIFF_OP:
  463. case RVE_IEQ_OP:
  464. case RVE_IDIFF_OP:
  465. case RVE_STREQ_OP:
  466. case RVE_STRDIFF_OP:
  467. case RVE_MATCH_OP:
  468. case RVE_IPLUS_OP:
  469. case RVE_STRLEN_OP:
  470. case RVE_STREMPTY_OP:
  471. case RVE_DEFINED_OP:
  472. case RVE_NOTDEFINED_OP:
  473. case RVE_INT_OP:
  474. return RV_INT;
  475. case RVE_PLUS_OP:
  476. /* '+' evaluates to the type of the left operand */
  477. return rve_guess_type(rve->left.rve);
  478. case RVE_CONCAT_OP:
  479. case RVE_STR_OP:
  480. return RV_STR;
  481. case RVE_NONE_OP:
  482. break;
  483. }
  484. return RV_NONE;
  485. }
  486. /** returns true if expression is constant.
  487. * @return 0 or 1 on
  488. * non constant type
  489. */
  490. int rve_is_constant(struct rval_expr* rve)
  491. {
  492. switch(rve->op){
  493. case RVE_RVAL_OP:
  494. switch(rve->left.rval.type){
  495. case RV_STR:
  496. return 1;
  497. case RV_INT:
  498. return 1;
  499. case RV_SEL:
  500. case RV_BEXPR:
  501. case RV_ACTION_ST:
  502. case RV_PVAR:
  503. case RV_AVP:
  504. case RV_NONE:
  505. return 0;
  506. }
  507. break;
  508. case RVE_UMINUS_OP:
  509. case RVE_BOOL_OP:
  510. case RVE_LNOT_OP:
  511. case RVE_BNOT_OP:
  512. case RVE_STRLEN_OP:
  513. case RVE_STREMPTY_OP:
  514. case RVE_DEFINED_OP:
  515. case RVE_NOTDEFINED_OP:
  516. case RVE_INT_OP:
  517. case RVE_STR_OP:
  518. return rve_is_constant(rve->left.rve);
  519. case RVE_MINUS_OP:
  520. case RVE_MUL_OP:
  521. case RVE_DIV_OP:
  522. case RVE_MOD_OP:
  523. case RVE_BOR_OP:
  524. case RVE_BAND_OP:
  525. case RVE_BXOR_OP:
  526. case RVE_BLSHIFT_OP:
  527. case RVE_BRSHIFT_OP:
  528. case RVE_LAND_OP:
  529. case RVE_LOR_OP:
  530. case RVE_GT_OP:
  531. case RVE_GTE_OP:
  532. case RVE_LT_OP:
  533. case RVE_LTE_OP:
  534. case RVE_EQ_OP:
  535. case RVE_DIFF_OP:
  536. case RVE_IEQ_OP:
  537. case RVE_IDIFF_OP:
  538. case RVE_STREQ_OP:
  539. case RVE_STRDIFF_OP:
  540. case RVE_MATCH_OP:
  541. case RVE_PLUS_OP:
  542. case RVE_IPLUS_OP:
  543. case RVE_CONCAT_OP:
  544. return rve_is_constant(rve->left.rve) &&
  545. rve_is_constant(rve->right.rve);
  546. case RVE_NONE_OP:
  547. break;
  548. }
  549. return 0;
  550. }
  551. /** returns true if the expression has side-effects.
  552. * @return 1 for possible side-effects, 0 for no side-effects
  553. * TODO: add better checks
  554. */
  555. int rve_has_side_effects(struct rval_expr* rve)
  556. {
  557. return !rve_is_constant(rve);
  558. }
  559. /** returns true if operator is unary (takes only 1 arg).
  560. * @return 0 or 1 on
  561. */
  562. static int rve_op_unary(enum rval_expr_op op)
  563. {
  564. switch(op){
  565. case RVE_RVAL_OP: /* not realy an operator */
  566. return -1;
  567. case RVE_UMINUS_OP:
  568. case RVE_BOOL_OP:
  569. case RVE_LNOT_OP:
  570. case RVE_BNOT_OP:
  571. case RVE_STRLEN_OP:
  572. case RVE_STREMPTY_OP:
  573. case RVE_DEFINED_OP:
  574. case RVE_NOTDEFINED_OP:
  575. case RVE_INT_OP:
  576. case RVE_STR_OP:
  577. return 1;
  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 0;
  604. case RVE_NONE_OP:
  605. return -1;
  606. break;
  607. }
  608. return 0;
  609. }
  610. /**
  611. * @brief Returns 1 if expression is valid (type-wise)
  612. * @param type filled with the type of the expression (RV_INT, RV_STR or
  613. * RV_NONE if it's dynamic)
  614. * @param rve checked expression
  615. * @param bad_rve set on failure to the subexpression for which the
  616. * type check failed
  617. * @param bad_t set on failure to the type of the bad subexpression
  618. * @param exp_t set on failure to the expected type for the bad
  619. * subexpression
  620. * @return 0 or 1 and sets *type to the resulting type
  621. * (RV_INT, RV_STR or RV_NONE if it can be found only at runtime)
  622. */
  623. int rve_check_type(enum rval_type* type, struct rval_expr* rve,
  624. struct rval_expr** bad_rve,
  625. enum rval_type* bad_t,
  626. enum rval_type* exp_t)
  627. {
  628. enum rval_type type1, type2;
  629. switch(rve->op){
  630. case RVE_RVAL_OP:
  631. *type=rve_guess_type(rve);
  632. return 1;
  633. case RVE_UMINUS_OP:
  634. case RVE_BOOL_OP:
  635. case RVE_LNOT_OP:
  636. case RVE_BNOT_OP:
  637. *type=RV_INT;
  638. if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
  639. if (type1==RV_STR){
  640. if (bad_rve) *bad_rve=rve->left.rve;
  641. if (bad_t) *bad_t=type1;
  642. if (exp_t) *exp_t=RV_INT;
  643. return 0;
  644. }
  645. return 1;
  646. }
  647. return 0;
  648. break;
  649. case RVE_MINUS_OP:
  650. case RVE_MUL_OP:
  651. case RVE_DIV_OP:
  652. case RVE_MOD_OP:
  653. case RVE_BOR_OP:
  654. case RVE_BAND_OP:
  655. case RVE_BXOR_OP:
  656. case RVE_BLSHIFT_OP:
  657. case RVE_BRSHIFT_OP:
  658. case RVE_LAND_OP:
  659. case RVE_LOR_OP:
  660. case RVE_GT_OP:
  661. case RVE_GTE_OP:
  662. case RVE_LT_OP:
  663. case RVE_LTE_OP:
  664. case RVE_IEQ_OP:
  665. case RVE_IDIFF_OP:
  666. case RVE_IPLUS_OP:
  667. *type=RV_INT;
  668. if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
  669. if (type1==RV_STR){
  670. if (bad_rve) *bad_rve=rve->left.rve;
  671. if (bad_t) *bad_t=type1;
  672. if (exp_t) *exp_t=RV_INT;
  673. return 0;
  674. }
  675. if (rve_check_type(&type2, rve->right.rve, bad_rve,
  676. bad_t, exp_t)){
  677. if (type2==RV_STR){
  678. if (bad_rve) *bad_rve=rve->right.rve;
  679. if (bad_t) *bad_t=type2;
  680. if (exp_t) *exp_t=RV_INT;
  681. return 0;
  682. }
  683. return 1;
  684. }
  685. }
  686. return 0;
  687. case RVE_EQ_OP:
  688. case RVE_DIFF_OP:
  689. *type=RV_INT;
  690. if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
  691. if (rve_check_type(&type2, rve->right.rve, bad_rve, bad_t,
  692. exp_t)){
  693. if ((type2!=type1) && (type1!=RV_NONE) &&
  694. (type2!=RV_NONE) &&
  695. !(type1==RV_STR && type2==RV_INT)){
  696. if (bad_rve) *bad_rve=rve->right.rve;
  697. if (bad_t) *bad_t=type2;
  698. if (exp_t) *exp_t=type1;
  699. return 0;
  700. }
  701. return 1;
  702. }
  703. }
  704. return 0;
  705. case RVE_PLUS_OP:
  706. *type=RV_NONE;
  707. if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
  708. if (rve_check_type(&type2, rve->right.rve, bad_rve, bad_t,
  709. exp_t)){
  710. if ((type2!=type1) && (type1!=RV_NONE) &&
  711. (type2!=RV_NONE) &&
  712. !(type1==RV_STR && type2==RV_INT)){
  713. if (bad_rve) *bad_rve=rve->right.rve;
  714. if (bad_t) *bad_t=type2;
  715. if (exp_t) *exp_t=type1;
  716. return 0;
  717. }
  718. *type=type1;
  719. return 1;
  720. }
  721. }
  722. break;
  723. case RVE_CONCAT_OP:
  724. *type=RV_STR;
  725. if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
  726. if (rve_check_type(&type2, rve->right.rve, bad_rve, bad_t,
  727. exp_t)){
  728. if ((type2!=type1) && (type1!=RV_NONE) &&
  729. (type2!=RV_NONE) &&
  730. !(type1==RV_STR && type2==RV_INT)){
  731. if (bad_rve) *bad_rve=rve->right.rve;
  732. if (bad_t) *bad_t=type2;
  733. if (exp_t) *exp_t=type1;
  734. return 0;
  735. }
  736. if (type1==RV_INT){
  737. if (bad_rve) *bad_rve=rve->left.rve;
  738. if (bad_t) *bad_t=type1;
  739. if (exp_t) *exp_t=RV_STR;
  740. return 0;
  741. }
  742. return 1;
  743. }
  744. }
  745. break;
  746. case RVE_STREQ_OP:
  747. case RVE_STRDIFF_OP:
  748. case RVE_MATCH_OP:
  749. *type=RV_INT;
  750. if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
  751. if (rve_check_type(&type2, rve->right.rve, bad_rve, bad_t,
  752. exp_t)){
  753. if ((type2!=type1) && (type1!=RV_NONE) &&
  754. (type2!=RV_NONE) &&
  755. !(type1==RV_STR && type2==RV_INT)){
  756. if (bad_rve) *bad_rve=rve->right.rve;
  757. if (bad_t) *bad_t=type2;
  758. if (exp_t) *exp_t=type1;
  759. return 0;
  760. }
  761. if (type1==RV_INT){
  762. if (bad_rve) *bad_rve=rve->left.rve;
  763. if (bad_t) *bad_t=type1;
  764. if (exp_t) *exp_t=RV_STR;
  765. return 0;
  766. }
  767. return 1;
  768. }
  769. }
  770. break;
  771. case RVE_STRLEN_OP:
  772. case RVE_STREMPTY_OP:
  773. case RVE_DEFINED_OP:
  774. case RVE_NOTDEFINED_OP:
  775. *type=RV_INT;
  776. if (rve_check_type(&type1, rve->left.rve, bad_rve, bad_t, exp_t)){
  777. if (type1==RV_INT){
  778. if (bad_rve) *bad_rve=rve->left.rve;
  779. if (bad_t) *bad_t=type1;
  780. if (exp_t) *exp_t=RV_STR;
  781. return 0;
  782. }
  783. return 1;
  784. }
  785. break;
  786. case RVE_INT_OP:
  787. *type=RV_INT;
  788. return 1;
  789. break;
  790. case RVE_STR_OP:
  791. *type=RV_STR;
  792. return 1;
  793. break;
  794. case RVE_NONE_OP:
  795. default:
  796. BUG("unexpected rve op %d (%d,%d-%d,%d)\n", rve->op,
  797. rve->fpos.s_line, rve->fpos.s_col,
  798. rve->fpos.e_line, rve->fpos.e_col);
  799. if (bad_rve) *bad_rve=rve;
  800. if (bad_t) *bad_t=RV_NONE;
  801. if (exp_t) *exp_t=RV_STR;
  802. break;
  803. }
  804. return 0;
  805. }
  806. /** get the integer value of an rvalue.
  807. * *i=(int)rv
  808. * if rv == undefined select, avp or pvar, return 0.
  809. * if an error occurs while evaluating a select, avp or pvar, behave as
  810. * for the undefined case (and return success).
  811. * @param h - script context handle
  812. * @param msg - sip msg
  813. * @param i - pointer to int, where the conversion result will be stored
  814. * @param rv - rvalue to be converted
  815. * @param cache - cached rv value (read-only), can be 0
  816. *
  817. * @return 0 on success, \<0 on error and EXPR_DROP on drop
  818. */
  819. int rval_get_int(struct run_act_ctx* h, struct sip_msg* msg,
  820. int* i, struct rvalue* rv,
  821. struct rval_cache* cache)
  822. {
  823. avp_t* r_avp;
  824. int_str avp_val;
  825. pv_value_t pval;
  826. str tmp;
  827. str* s;
  828. int r, ret;
  829. int destroy_pval;
  830. destroy_pval=0;
  831. s=0;
  832. ret=0;
  833. switch(rv->type){
  834. case RV_INT:
  835. *i=rv->v.l;
  836. break;
  837. case RV_STR:
  838. s=&rv->v.s;
  839. goto rv_str;
  840. case RV_BEXPR:
  841. *i=eval_expr(h, rv->v.bexpr, msg);
  842. if (*i==EXPR_DROP){
  843. *i=0; /* false */
  844. return EXPR_DROP;
  845. }
  846. break;
  847. case RV_ACTION_ST:
  848. if (rv->v.action) {
  849. *i=(run_actions_safe(h, rv->v.action, msg)>0);
  850. h->run_flags &= ~(RETURN_R_F|BREAK_R_F); /* catch return &
  851. break in expr*/
  852. } else
  853. *i=0;
  854. break;
  855. case RV_SEL:
  856. r=run_select(&tmp, &rv->v.sel, msg);
  857. if (unlikely(r!=0)){
  858. if (r<0)
  859. goto eval_error;
  860. else /* i>0 => undefined */
  861. goto undef;
  862. }
  863. s=&tmp;
  864. goto rv_str;
  865. case RV_AVP:
  866. if (unlikely(cache && cache->cache_type==RV_CACHE_AVP)){
  867. if (likely(cache->val_type==RV_INT)){
  868. *i=cache->c.avp_val.n;
  869. }else if (cache->val_type==RV_STR){
  870. s=&cache->c.avp_val.s;
  871. goto rv_str;
  872. }else if (cache->val_type==RV_NONE)
  873. goto undef;
  874. else goto error_cache;
  875. }else{
  876. r_avp = search_avp_by_index(rv->v.avps.type, rv->v.avps.name,
  877. &avp_val, rv->v.avps.index);
  878. if (likely(r_avp)){
  879. if (unlikely(r_avp->flags & AVP_VAL_STR)){
  880. s=&avp_val.s;
  881. goto rv_str;
  882. }else{
  883. *i=avp_val.n;
  884. }
  885. }else{
  886. goto undef;
  887. }
  888. }
  889. break;
  890. case RV_PVAR:
  891. if (unlikely(cache && cache->cache_type==RV_CACHE_PVAR)){
  892. if (likely((cache->val_type==RV_INT) ||
  893. (cache->c.pval.flags & PV_VAL_INT))){
  894. *i=cache->c.pval.ri;
  895. }else if (cache->val_type==RV_STR){
  896. s=&cache->c.pval.rs;
  897. goto rv_str;
  898. }else if (cache->val_type==RV_NONE)
  899. goto undef;
  900. else goto error_cache;
  901. }else{
  902. memset(&pval, 0, sizeof(pval));
  903. if (likely(pv_get_spec_value(msg, &rv->v.pvs, &pval)==0)){
  904. if (likely(pval.flags & PV_VAL_INT)){
  905. *i=pval.ri;
  906. pv_value_destroy(&pval);
  907. }else if (likely(pval.flags & PV_VAL_STR)){
  908. destroy_pval=1; /* we must pv_value_destroy() later*/
  909. s=&pval.rs;
  910. goto rv_str;
  911. }else{
  912. /* no PV_VAL_STR and no PV_VAL_INT => undef
  913. (PV_VAL_NULL) */
  914. pv_value_destroy(&pval);
  915. goto undef;
  916. }
  917. }else{
  918. goto eval_error;
  919. }
  920. }
  921. break;
  922. default:
  923. BUG("rv type %d not handled\n", rv->type);
  924. goto error;
  925. }
  926. return ret;
  927. undef:
  928. eval_error: /* same as undefined */
  929. /* handle undefined => result 0, return success */
  930. *i=0;
  931. return 0;
  932. rv_str:
  933. /* rv is of string type => try to convert it to int */
  934. /* if "" => 0 (most likely case) */
  935. if (likely(s->len==0)) *i=0;
  936. else if (unlikely(str2sint(s, i)!=0)){
  937. /* error converting to int => non numeric => 0 */
  938. *i=0;
  939. #ifdef RV_STR2INT_VERBOSE_ERR
  940. WARN("automatic string to int conversion for \"%.*s\" failed\n",
  941. s->len, ZSW(s->s));
  942. /* return an error code */
  943. #endif
  944. #ifdef RV_STR2INT_ERR
  945. ret=-1;
  946. #endif
  947. }
  948. if (destroy_pval)
  949. pv_value_destroy(&pval);
  950. return ret;
  951. error_cache:
  952. BUG("invalid cached value:cache type %d, value type %d\n",
  953. cache?cache->cache_type:0, cache?cache->val_type:0);
  954. error:
  955. if (destroy_pval)
  956. pv_value_destroy(&pval);
  957. *i=0;
  958. return -1;
  959. }
  960. /** log a message, appending rve position and a '\n'.*/
  961. #define RVE_LOG(lev, rve, txt) \
  962. LOG((lev), txt " (%d,%d-%d,%d)\n", \
  963. (rve)->fpos.s_line, rve->fpos.s_col, \
  964. (rve)->fpos.e_line, rve->fpos.e_col )
  965. /** macro for checking and handling rval_get_int() retcode.
  966. * check if the return code is an rval_get_int error and if so
  967. * handle the error (e.g. print a log message, ignore the error by
  968. * setting ret to 0 a.s.o.)
  969. * @param ret - retcode as returned by rval_get_int() (might be changed)
  970. * @param txt - warning message txt (no pointer allowed)
  971. * @param rve - rval_expr, used to access the config. pos
  972. */
  973. #if defined RVAL_GET_INT_ERR_WARN && defined RVAL_GET_INT_ERR_IGN
  974. #define rval_get_int_handle_ret(ret, txt, rve) \
  975. do { \
  976. if (unlikely((ret)<0)) { \
  977. RVE_LOG(L_WARN, rve, txt); \
  978. (ret)=0; \
  979. } \
  980. }while(0)
  981. #elif defined RVAL_GET_INT_ERR_WARN
  982. #define rval_get_int_handle_ret(ret, txt, rve) \
  983. do { \
  984. if (unlikely((ret)<0)) \
  985. RVE_LOG(L_WARN, rve, txt); \
  986. }while(0)
  987. #elif defined RVAL_GET_INT_ERR_IGN
  988. #define rval_get_int_handle_ret(ret, txt, rve) \
  989. do { \
  990. if (unlikely((ret)<0)) \
  991. (ret)=0; \
  992. } while(0)
  993. #else
  994. #define rval_get_int_handle_ret(ret, txt, rve) /* do nothing */
  995. #endif
  996. /** get the string value of an rv in a tmp variable
  997. * *s=(str)rv
  998. * if rv == undefined select, avp or pvar, return "".
  999. * if an error occurs while evaluating a select, avp or pvar, behave as
  1000. * for the undefined case (and return success).
  1001. * The result points either inside the passed rv, inside
  1002. * new_cache or inside an avp. new_cache must be non zero,
  1003. * initialized previously and it _must_ be rval_cache_clean(...)'ed when
  1004. * done.
  1005. * WARNING: it's not intended for general use. It might return a pointer
  1006. * inside rv so the result _must_ be treated as read-only. rv and new_cache
  1007. * must not be released/freed until the result is no longer needed.
  1008. * For general use see rval_get_str().
  1009. * @param h - script context handle
  1010. * @param msg - sip msg
  1011. * @param tmpv - str return value (pointer to a str struct that will be
  1012. * be filled with the conversion result)
  1013. * @param rv - rvalue to be converted
  1014. * @param cache - cached rv value (read-only), can be 0
  1015. * @param tmp_cache - used for temporary storage (so that tmpv will not
  1016. * point to possible freed data), it must be non-null,
  1017. * initialized and cleaned afterwards.
  1018. * @return 0 on success, <0 on error and EXPR_DROP on drop
  1019. */
  1020. int rval_get_tmp_str(struct run_act_ctx* h, struct sip_msg* msg,
  1021. str* tmpv, struct rvalue* rv,
  1022. struct rval_cache* cache,
  1023. struct rval_cache* tmp_cache)
  1024. {
  1025. avp_t* r_avp;
  1026. int i;
  1027. switch(rv->type){
  1028. case RV_INT:
  1029. tmpv->s=sint2strbuf(rv->v.l, tmp_cache->i2s,
  1030. sizeof(tmp_cache->i2s), &tmpv->len);
  1031. tmp_cache->cache_type = RV_CACHE_INT2STR;
  1032. break;
  1033. case RV_STR:
  1034. *tmpv=rv->v.s;
  1035. break;
  1036. case RV_ACTION_ST:
  1037. if (rv->v.action) {
  1038. i=(run_actions_safe(h, rv->v.action, msg)>0);
  1039. h->run_flags &= ~(RETURN_R_F|BREAK_R_F); /* catch return &
  1040. break in expr*/
  1041. } else
  1042. i=0;
  1043. tmpv->s=sint2strbuf(i, tmp_cache->i2s,
  1044. sizeof(tmp_cache->i2s), &tmpv->len);
  1045. tmp_cache->cache_type = RV_CACHE_INT2STR;
  1046. break;
  1047. case RV_BEXPR:
  1048. i=eval_expr(h, rv->v.bexpr, msg);
  1049. if (i==EXPR_DROP){
  1050. i=0; /* false */
  1051. tmpv->s=sint2strbuf(i, tmp_cache->i2s,
  1052. sizeof(tmp_cache->i2s), &tmpv->len);
  1053. tmp_cache->cache_type = RV_CACHE_INT2STR;
  1054. return EXPR_DROP;
  1055. }
  1056. tmpv->s=sint2strbuf(i, tmp_cache->i2s, sizeof(tmp_cache->i2s),
  1057. &tmpv->len);
  1058. tmp_cache->cache_type = RV_CACHE_INT2STR;
  1059. break;
  1060. case RV_SEL:
  1061. i=run_select(tmpv, &rv->v.sel, msg);
  1062. if (unlikely(i!=0)){
  1063. if (i<0){
  1064. goto eval_error;
  1065. }else { /* i>0 => undefined */
  1066. goto undef;
  1067. }
  1068. }
  1069. break;
  1070. case RV_AVP:
  1071. if (likely(cache && cache->cache_type==RV_CACHE_AVP)){
  1072. if (likely(cache->val_type==RV_STR)){
  1073. *tmpv=cache->c.avp_val.s;
  1074. }else if (cache->val_type==RV_INT){
  1075. i=cache->c.avp_val.n;
  1076. tmpv->s=sint2strbuf(i, tmp_cache->i2s,
  1077. sizeof(tmp_cache->i2s), &tmpv->len);
  1078. tmp_cache->cache_type = RV_CACHE_INT2STR;
  1079. }else if (cache->val_type==RV_NONE){
  1080. goto undef;
  1081. }else goto error_cache;
  1082. }else{
  1083. r_avp = search_avp_by_index(rv->v.avps.type, rv->v.avps.name,
  1084. &tmp_cache->c.avp_val,
  1085. rv->v.avps.index);
  1086. if (likely(r_avp)){
  1087. if (likely(r_avp->flags & AVP_VAL_STR)){
  1088. tmp_cache->cache_type=RV_CACHE_AVP;
  1089. tmp_cache->val_type=RV_STR;
  1090. *tmpv=tmp_cache->c.avp_val.s;
  1091. }else{
  1092. i=tmp_cache->c.avp_val.n;
  1093. tmpv->s=sint2strbuf(i, tmp_cache->i2s,
  1094. sizeof(tmp_cache->i2s), &tmpv->len);
  1095. tmp_cache->cache_type = RV_CACHE_INT2STR;
  1096. }
  1097. }else goto undef;
  1098. }
  1099. break;
  1100. case RV_PVAR:
  1101. if (likely(cache && cache->cache_type==RV_CACHE_PVAR)){
  1102. if (likely(cache->val_type==RV_STR)){
  1103. *tmpv=cache->c.pval.rs;
  1104. }else if (cache->val_type==RV_INT){
  1105. i=cache->c.pval.ri;
  1106. tmpv->s=sint2strbuf(i, tmp_cache->i2s,
  1107. sizeof(tmp_cache->i2s), &tmpv->len);
  1108. tmp_cache->cache_type = RV_CACHE_INT2STR;
  1109. }else if (cache->val_type==RV_NONE){
  1110. goto undef;
  1111. }else goto error_cache;
  1112. }else{
  1113. memset(&tmp_cache->c.pval, 0, sizeof(tmp_cache->c.pval));
  1114. if (likely(pv_get_spec_value(msg, &rv->v.pvs,
  1115. &tmp_cache->c.pval)==0)){
  1116. if (likely(tmp_cache->c.pval.flags & PV_VAL_STR)){
  1117. /* the value is not destroyed, but saved instead
  1118. in tmp_cache so that it can be destroyed later
  1119. when no longer needed */
  1120. tmp_cache->cache_type=RV_CACHE_PVAR;
  1121. tmp_cache->val_type=RV_STR;
  1122. *tmpv=tmp_cache->c.pval.rs;
  1123. }else if (likely(tmp_cache->c.pval.flags & PV_VAL_INT)){
  1124. i=tmp_cache->c.pval.ri;
  1125. pv_value_destroy(&tmp_cache->c.pval);
  1126. tmpv->s=sint2strbuf(i, tmp_cache->i2s,
  1127. sizeof(tmp_cache->i2s), &tmpv->len);
  1128. tmp_cache->cache_type = RV_CACHE_INT2STR;
  1129. }else{
  1130. /* no PV_VAL_STR and no PV_VAL_INT => undef
  1131. (PV_VAL_NULL) */
  1132. pv_value_destroy(&tmp_cache->c.pval);
  1133. goto undef;
  1134. }
  1135. }else{
  1136. goto eval_error;
  1137. }
  1138. }
  1139. break;
  1140. default:
  1141. BUG("rv type %d not handled\n", rv->type);
  1142. goto error;
  1143. }
  1144. return 0;
  1145. undef:
  1146. eval_error: /* same as undefined */
  1147. /* handle undefined => result "", return success */
  1148. tmpv->s="";
  1149. tmpv->len=0;
  1150. return 0;
  1151. error_cache:
  1152. BUG("invalid cached value:cache type %d, value type %d\n",
  1153. cache?cache->cache_type:0, cache?cache->val_type:0);
  1154. error:
  1155. tmpv->s="";
  1156. tmpv->len=0;
  1157. return -1;
  1158. }
  1159. /** get the string value of an rv.
  1160. * *s=(str)rv
  1161. * The result is pkg malloc'ed (so it should be pkg_free()'ed when finished.
  1162. * @return 0 on success, <0 on error and EXPR_DROP on drop
  1163. */
  1164. int rval_get_str(struct run_act_ctx* h, struct sip_msg* msg,
  1165. str* s, struct rvalue* rv,
  1166. struct rval_cache* cache)
  1167. {
  1168. str tmp;
  1169. struct rval_cache tmp_cache;
  1170. rval_cache_init(&tmp_cache);
  1171. if (unlikely(rval_get_tmp_str(h, msg, &tmp, rv, cache, &tmp_cache)<0))
  1172. goto error;
  1173. s->s=pkg_malloc(tmp.len+1/* 0 term */);
  1174. if (unlikely(s->s==0)){
  1175. ERR("memory allocation error\n");
  1176. goto error;
  1177. }
  1178. s->len=tmp.len;
  1179. memcpy(s->s, tmp.s, tmp.len);
  1180. s->s[tmp.len]=0; /* 0 term */
  1181. rval_cache_clean(&tmp_cache);
  1182. return 0;
  1183. error:
  1184. rval_cache_clean(&tmp_cache);
  1185. return -1;
  1186. }
  1187. /**
  1188. * @brief Convert a rvalue to another rvalue, of a specific type
  1189. *
  1190. * Convert a rvalue to another rvalue, of a specific type.
  1191. * The result is read-only in most cases (can be a reference
  1192. * to another rvalue, can be checked by using rv_chg_in_place()) and
  1193. * _must_ be rval_destroy()'ed.
  1194. *
  1195. * @param h run action context
  1196. * @param msg SIP mesasge
  1197. * @param type - type to convert to
  1198. * @param v - rvalue to convert
  1199. * @param c - rval_cache (cached v value if known/filled by another
  1200. * function), can be 0 (unknown/not needed)
  1201. * @return pointer to a rvalue (reference to an existing one or a new
  1202. * one, @see rv_chg_in_place() and the above comment), or 0 on error.
  1203. */
  1204. struct rvalue* rval_convert(struct run_act_ctx* h, struct sip_msg* msg,
  1205. enum rval_type type, struct rvalue* v,
  1206. struct rval_cache* c)
  1207. {
  1208. int i;
  1209. struct rval_cache tmp_cache;
  1210. str tmp;
  1211. struct rvalue* ret;
  1212. union rval_val val;
  1213. if (v->type==type){
  1214. rv_ref(v);
  1215. return v;
  1216. }
  1217. switch(type){
  1218. case RV_INT:
  1219. if (unlikely(rval_get_int(h, msg, &i, v, c) < 0))
  1220. return 0;
  1221. val.l=i;
  1222. return rval_new(RV_INT, &val, 0);
  1223. case RV_STR:
  1224. rval_cache_init(&tmp_cache);
  1225. if (unlikely(rval_get_tmp_str(h, msg, &tmp, v, c, &tmp_cache) < 0))
  1226. {
  1227. rval_cache_clean(&tmp_cache);
  1228. return 0;
  1229. }
  1230. ret=rval_new_str(&tmp, RV_STR_EXTRA);
  1231. rval_cache_clean(&tmp_cache);
  1232. return ret;
  1233. case RV_NONE:
  1234. default:
  1235. BUG("unsupported conversion to type %d\n", type);
  1236. return 0;
  1237. }
  1238. return 0;
  1239. }
  1240. /** integer operation: *res= op v.
  1241. * @return 0 on succes, \<0 on error
  1242. */
  1243. inline static int int_intop1(int* res, enum rval_expr_op op, int v)
  1244. {
  1245. switch(op){
  1246. case RVE_UMINUS_OP:
  1247. *res=-v;
  1248. break;
  1249. case RVE_BOOL_OP:
  1250. *res=!!v;
  1251. break;
  1252. case RVE_LNOT_OP:
  1253. *res=!v;
  1254. break;
  1255. case RVE_BNOT_OP:
  1256. *res=~v;
  1257. break;
  1258. default:
  1259. BUG("rv unsupported intop1 %d\n", op);
  1260. return -1;
  1261. }
  1262. return 0;
  1263. }
  1264. /** integer operation: *res= v1 op v2
  1265. * @return 0 on succes, \<0 on error
  1266. */
  1267. inline static int int_intop2(int* res, enum rval_expr_op op, int v1, int v2)
  1268. {
  1269. switch(op){
  1270. case RVE_PLUS_OP:
  1271. case RVE_IPLUS_OP:
  1272. *res=v1+v2;
  1273. break;
  1274. case RVE_MINUS_OP:
  1275. *res=v1-v2;
  1276. break;
  1277. case RVE_MUL_OP:
  1278. *res=v1*v2;
  1279. break;
  1280. case RVE_DIV_OP:
  1281. if (unlikely(v2==0)){
  1282. ERR("rv div by 0\n");
  1283. return -1;
  1284. }
  1285. *res=v1/v2;
  1286. break;
  1287. case RVE_MOD_OP:
  1288. if (unlikely(v2==0)){
  1289. ERR("rv mod by 0\n");
  1290. return -1;
  1291. }
  1292. *res=v1%v2;
  1293. break;
  1294. case RVE_BOR_OP:
  1295. *res=v1|v2;
  1296. break;
  1297. case RVE_BAND_OP:
  1298. *res=v1&v2;
  1299. break;
  1300. case RVE_BXOR_OP:
  1301. *res=v1^v2;
  1302. break;
  1303. case RVE_BLSHIFT_OP:
  1304. *res=v1<<v2;
  1305. break;
  1306. case RVE_BRSHIFT_OP:
  1307. *res=v1>>v2;
  1308. break;
  1309. case RVE_LAND_OP:
  1310. *res=v1 && v2;
  1311. break;
  1312. case RVE_LOR_OP:
  1313. *res=v1 || v2;
  1314. break;
  1315. case RVE_GT_OP:
  1316. *res=v1 > v2;
  1317. break;
  1318. case RVE_GTE_OP:
  1319. *res=v1 >= v2;
  1320. break;
  1321. case RVE_LT_OP:
  1322. *res=v1 < v2;
  1323. break;
  1324. case RVE_LTE_OP:
  1325. *res=v1 <= v2;
  1326. break;
  1327. case RVE_EQ_OP:
  1328. case RVE_IEQ_OP:
  1329. *res=v1 == v2;
  1330. break;
  1331. case RVE_DIFF_OP:
  1332. case RVE_IDIFF_OP:
  1333. *res=v1 != v2;
  1334. break;
  1335. case RVE_CONCAT_OP:
  1336. *res=0;
  1337. /* invalid operand for int */
  1338. return -1;
  1339. default:
  1340. BUG("rv unsupported intop %d\n", op);
  1341. return -1;
  1342. }
  1343. return 0;
  1344. }
  1345. /** internal helper: compare 2 RV_STR RVs.
  1346. * Warning: rv1 & rv2 must be RV_STR
  1347. * @return 0 on success, -1 on error
  1348. */
  1349. inline static int bool_rvstrop2( enum rval_expr_op op, int* res,
  1350. struct rvalue* rv1, struct rvalue* rv2)
  1351. {
  1352. str* s1;
  1353. str* s2;
  1354. regex_t tmp_re;
  1355. s1=&rv1->v.s;
  1356. s2=&rv2->v.s;
  1357. switch(op){
  1358. case RVE_EQ_OP:
  1359. case RVE_STREQ_OP:
  1360. *res= (s1->len==s2->len) && (memcmp(s1->s, s2->s, s1->len)==0);
  1361. break;
  1362. case RVE_DIFF_OP:
  1363. case RVE_STRDIFF_OP:
  1364. *res= (s1->len!=s2->len) || (memcmp(s1->s, s2->s, s1->len)!=0);
  1365. break;
  1366. case RVE_MATCH_OP:
  1367. if (likely(rv2->flags & RV_RE_F)){
  1368. *res=(regexec(rv2->v.re.regex, rv1->v.s.s, 0, 0, 0)==0);
  1369. }else{
  1370. /* we need to compile the RE on the fly */
  1371. if (unlikely(regcomp(&tmp_re, s2->s,
  1372. REG_EXTENDED|REG_NOSUB|REG_ICASE))){
  1373. /* error */
  1374. ERR("Bad regular expression \"%s\"\n", s2->s);
  1375. goto error;
  1376. }
  1377. *res=(regexec(&tmp_re, s1->s, 0, 0, 0)==0);
  1378. regfree(&tmp_re);
  1379. }
  1380. break;
  1381. default:
  1382. BUG("rv unsupported intop %d\n", op);
  1383. goto error;
  1384. }
  1385. return 0;
  1386. error:
  1387. *res=0; /* false */
  1388. return -1;
  1389. }
  1390. /** integer returning operation on string: *res= op str (returns integer)
  1391. * @return 0 on succes, \<0 on error
  1392. */
  1393. inline static int int_strop1(int* res, enum rval_expr_op op, str* s1)
  1394. {
  1395. switch(op){
  1396. case RVE_STRLEN_OP:
  1397. *res=s1->len;
  1398. break;
  1399. case RVE_STREMPTY_OP:
  1400. *res=(s1->len==0);
  1401. break;
  1402. default:
  1403. BUG("rv unsupported int_strop1 %d\n", op);
  1404. *res=0;
  1405. return -1;
  1406. }
  1407. return 0;
  1408. }
  1409. /** integer operation: ret= op v (returns a rvalue).
  1410. * @return rvalue on success, 0 on error
  1411. */
  1412. inline static struct rvalue* rval_intop1(struct run_act_ctx* h,
  1413. struct sip_msg* msg,
  1414. enum rval_expr_op op,
  1415. struct rvalue* v)
  1416. {
  1417. struct rvalue* rv2;
  1418. struct rvalue* ret;
  1419. int i;
  1420. i=0;
  1421. rv2=rval_convert(h, msg, RV_INT, v, 0);
  1422. if (unlikely(rv2==0)){
  1423. ERR("rval int conversion failed\n");
  1424. goto error;
  1425. }
  1426. if (unlikely(int_intop1(&i, op, rv2->v.l)<0))
  1427. goto error;
  1428. if (rv_chg_in_place(rv2)){
  1429. ret=rv2;
  1430. rv_ref(ret);
  1431. }else if (rv_chg_in_place(v)){
  1432. ret=v;
  1433. rv_ref(ret);
  1434. }else{
  1435. ret=rval_new(RV_INT, &rv2->v, 0);
  1436. if (unlikely(ret==0)){
  1437. ERR("eval out of memory\n");
  1438. goto error;
  1439. }
  1440. }
  1441. rval_destroy(rv2);
  1442. ret->v.l=i;
  1443. return ret;
  1444. error:
  1445. rval_destroy(rv2);
  1446. return 0;
  1447. }
  1448. /** integer operation: ret= l op r (returns a rvalue).
  1449. * @return rvalue on success, 0 on error
  1450. */
  1451. inline static struct rvalue* rval_intop2(struct run_act_ctx* h,
  1452. struct sip_msg* msg,
  1453. enum rval_expr_op op,
  1454. struct rvalue* l,
  1455. struct rvalue* r)
  1456. {
  1457. struct rvalue* rv1;
  1458. struct rvalue* rv2;
  1459. struct rvalue* ret;
  1460. int i;
  1461. rv2=rv1=0;
  1462. ret=0;
  1463. if ((rv1=rval_convert(h, msg, RV_INT, l, 0))==0)
  1464. goto error;
  1465. if ((rv2=rval_convert(h, msg, RV_INT, r, 0))==0)
  1466. goto error;
  1467. if (unlikely(int_intop2(&i, op, rv1->v.l, rv2->v.l)<0))
  1468. goto error;
  1469. if (rv_chg_in_place(rv1)){
  1470. /* try reusing rv1 */
  1471. ret=rv1;
  1472. rv_ref(ret);
  1473. }else if (rv_chg_in_place(rv2)){
  1474. /* try reusing rv2 */
  1475. ret=rv2;
  1476. rv_ref(ret);
  1477. }else if ((l->type==RV_INT) && (rv_chg_in_place(l))){
  1478. ret=l;
  1479. rv_ref(ret);
  1480. } else if ((r->type==RV_INT) && (rv_chg_in_place(r))){
  1481. ret=r;
  1482. rv_ref(ret);
  1483. }else{
  1484. ret=rval_new(RV_INT, &rv1->v, 0);
  1485. if (unlikely(ret==0)){
  1486. ERR("rv eval out of memory\n");
  1487. goto error;
  1488. }
  1489. }
  1490. rval_destroy(rv1);
  1491. rval_destroy(rv2);
  1492. ret->v.l=i;
  1493. return ret;
  1494. error:
  1495. rval_destroy(rv1);
  1496. rval_destroy(rv2);
  1497. return 0;
  1498. }
  1499. /** string add operation: ret= l . r (returns a rvalue).
  1500. * Can use cached rvalues (c1 & c2).
  1501. * @return rvalue on success, 0 on error
  1502. */
  1503. inline static struct rvalue* rval_str_add2(struct run_act_ctx* h,
  1504. struct sip_msg* msg,
  1505. struct rvalue* l,
  1506. struct rval_cache* c1,
  1507. struct rvalue* r,
  1508. struct rval_cache* c2
  1509. )
  1510. {
  1511. struct rvalue* rv1;
  1512. struct rvalue* rv2;
  1513. struct rvalue* ret;
  1514. str* s1;
  1515. str* s2;
  1516. str tmp;
  1517. short flags;
  1518. int len;
  1519. rv2=rv1=0;
  1520. ret=0;
  1521. flags=0;
  1522. s1=0;
  1523. s2=0;
  1524. if ((rv1=rval_convert(h, msg, RV_STR, l, c1))==0)
  1525. goto error;
  1526. if ((rv2=rval_convert(h, msg, RV_STR, r, c2))==0)
  1527. goto error;
  1528. len=rv1->v.s.len + rv2->v.s.len + 1 /* 0 */;
  1529. if (rv_chg_in_place(rv1) && (rv1->bsize>=len)){
  1530. /* try reusing rv1 */
  1531. ret=rv1;
  1532. rv_ref(ret);
  1533. s2=&rv2->v.s;
  1534. if (ret->v.s.s == &ret->buf[0]) s1=0;
  1535. else{
  1536. tmp=ret->v.s;
  1537. flags=ret->flags;
  1538. ret->flags &= ~RV_CNT_ALLOCED_F;
  1539. ret->v.s.s=&ret->buf[0];
  1540. ret->v.s.len=0;
  1541. s1=&tmp;
  1542. }
  1543. }else if (rv_chg_in_place(rv2) && (rv2->bsize>=len)){
  1544. /* try reusing rv2 */
  1545. ret=rv2;
  1546. rv_ref(ret);
  1547. s1=&rv1->v.s;
  1548. if (ret->v.s.s == &ret->buf[0])
  1549. s2=&ret->v.s;
  1550. else{
  1551. tmp=ret->v.s;
  1552. flags=ret->flags;
  1553. ret->flags &= ~RV_CNT_ALLOCED_F;
  1554. ret->v.s.s=&ret->buf[0];
  1555. ret->v.s.len=0;
  1556. s2=&tmp;
  1557. }
  1558. }else if ((l->type==RV_STR) && (rv_chg_in_place(l)) && (l->bsize>=len)){
  1559. ret=l;
  1560. rv_ref(ret);
  1561. s2=&rv2->v.s;
  1562. if (ret->v.s.s == &ret->buf[0]) s1=0;
  1563. else{
  1564. tmp=ret->v.s;
  1565. flags=ret->flags;
  1566. ret->flags &= ~RV_CNT_ALLOCED_F;
  1567. ret->v.s.s=&ret->buf[0];
  1568. ret->v.s.len=0;
  1569. s1=&tmp;
  1570. }
  1571. } else if ((r->type==RV_STR) && (rv_chg_in_place(r) && (r->bsize>=len))){
  1572. ret=r;
  1573. rv_ref(ret);
  1574. s1=&rv1->v.s;
  1575. if (ret->v.s.s == &ret->buf[0])
  1576. s2=&ret->v.s;
  1577. else{
  1578. tmp=ret->v.s;
  1579. flags=ret->flags;
  1580. ret->flags &= ~RV_CNT_ALLOCED_F;
  1581. ret->v.s.s=&ret->buf[0];
  1582. ret->v.s.len=0;
  1583. s2=&tmp;
  1584. }
  1585. }else{
  1586. ret=rval_new(RV_STR, &rv1->v, len + RV_STR_EXTRA);
  1587. if (unlikely(ret==0)){
  1588. ERR("rv eval out of memory\n");
  1589. goto error;
  1590. }
  1591. s1=0;
  1592. s2=&rv2->v.s;
  1593. }
  1594. /* do the actual copy */
  1595. memmove(ret->buf+rv1->v.s.len, s2->s, s2->len);
  1596. if (s1){
  1597. memcpy(ret->buf, s1->s, s1->len);
  1598. }
  1599. ret->v.s.len=rv1->v.s.len+s2->len;
  1600. ret->v.s.s[ret->v.s.len]=0;
  1601. /* cleanup if needed */
  1602. if (flags & RV_CNT_ALLOCED_F)
  1603. pkg_free(tmp.s);
  1604. rval_destroy(rv1);
  1605. rval_destroy(rv2);
  1606. return ret;
  1607. error:
  1608. rval_destroy(rv1);
  1609. rval_destroy(rv2);
  1610. return 0;
  1611. }
  1612. /** bool operation on rval evaluated as strings.
  1613. * Can use cached rvalues (c1 & c2).
  1614. * @return 0 success, -1 on error
  1615. */
  1616. inline static int rval_str_lop2(struct run_act_ctx* h,
  1617. struct sip_msg* msg,
  1618. int* res,
  1619. enum rval_expr_op op,
  1620. struct rvalue* l,
  1621. struct rval_cache* c1,
  1622. struct rvalue* r,
  1623. struct rval_cache* c2)
  1624. {
  1625. struct rvalue* rv1;
  1626. struct rvalue* rv2;
  1627. int ret;
  1628. rv2=rv1=0;
  1629. ret=0;
  1630. if ((rv1=rval_convert(h, msg, RV_STR, l, c1))==0)
  1631. goto error;
  1632. if ((rv2=rval_convert(h, msg, RV_STR, r, c2))==0)
  1633. goto error;
  1634. ret=bool_rvstrop2(op, res, rv1, rv2);
  1635. rval_destroy(rv1);
  1636. rval_destroy(rv2);
  1637. return ret;
  1638. error:
  1639. rval_destroy(rv1);
  1640. rval_destroy(rv2);
  1641. return 0;
  1642. }
  1643. /**
  1644. * @brief Integer operation on rval evaluated as string
  1645. *
  1646. * Integer operation on rval evaluated as string, can use cached
  1647. * rvalues (c1 & c2).
  1648. * @param h run action context
  1649. * @param msg SIP message
  1650. * @param res will be set to the result
  1651. * @param op rvalue expression operation
  1652. * @param l rvalue
  1653. * @param c1 rvalue cache
  1654. * @return 0 success, -1 on error
  1655. */
  1656. inline static int rval_int_strop1(struct run_act_ctx* h,
  1657. struct sip_msg* msg,
  1658. int* res,
  1659. enum rval_expr_op op,
  1660. struct rvalue* l,
  1661. struct rval_cache* c1)
  1662. {
  1663. struct rvalue* rv1;
  1664. int ret;
  1665. rv1=0;
  1666. ret=0;
  1667. if ((rv1=rval_convert(h, msg, RV_STR, l, c1))==0)
  1668. goto error;
  1669. ret=int_strop1(res, op, &rv1->v.s);
  1670. rval_destroy(rv1);
  1671. return ret;
  1672. error:
  1673. *res=0;
  1674. rval_destroy(rv1);
  1675. return -1;
  1676. }
  1677. /**
  1678. * @brief Checks if rv is defined
  1679. * @param h run action context
  1680. * @param msg SIP message
  1681. * @param res set to the result 1 is defined, 0 not defined
  1682. * @param rv rvalue
  1683. * @param cache rvalue cache
  1684. * @return 0 on success, -1 on error
  1685. * @note Can use cached rvalues (cache). A rv can be undefined if it's
  1686. * an undefined avp or pvar or select or if it's NONE
  1687. * @note An error in the avp, pvar or select search is equivalent to
  1688. * undefined (and it's not reported)
  1689. */
  1690. inline static int rv_defined(struct run_act_ctx* h,
  1691. struct sip_msg* msg, int* res,
  1692. struct rvalue* rv, struct rval_cache* cache)
  1693. {
  1694. avp_t* r_avp;
  1695. int_str avp_val;
  1696. pv_value_t pval;
  1697. str tmp;
  1698. *res=1;
  1699. switch(rv->type){
  1700. case RV_SEL:
  1701. if (unlikely(cache && cache->cache_type==RV_CACHE_SELECT)){
  1702. *res=(cache->val_type!=RV_NONE);
  1703. }else
  1704. /* run select returns 0 on success, -1 on error and >0 on
  1705. undefined. error is considered undefined */
  1706. *res=(run_select(&tmp, &rv->v.sel, msg)==0);
  1707. break;
  1708. case RV_AVP:
  1709. if (unlikely(cache && cache->cache_type==RV_CACHE_AVP)){
  1710. *res=(cache->val_type!=RV_NONE);
  1711. }else{
  1712. r_avp = search_avp_by_index(rv->v.avps.type, rv->v.avps.name,
  1713. &avp_val, rv->v.avps.index);
  1714. if (unlikely(r_avp==0)){
  1715. *res=0;
  1716. }
  1717. }
  1718. break;
  1719. case RV_PVAR:
  1720. /* PV_VAL_NULL or pv_get_spec_value error => undef */
  1721. if (unlikely(cache && cache->cache_type==RV_CACHE_PVAR)){
  1722. *res=(cache->val_type!=RV_NONE);
  1723. }else{
  1724. memset(&pval, 0, sizeof(pval));
  1725. if (likely(pv_get_spec_value(msg, &rv->v.pvs, &pval)==0)){
  1726. if ((pval.flags & PV_VAL_NULL) &&
  1727. ! (pval.flags & (PV_VAL_INT|PV_VAL_STR))){
  1728. *res=0;
  1729. }
  1730. pv_value_destroy(&pval);
  1731. }else{
  1732. *res=0; /* in case of error, consider it undef */
  1733. }
  1734. }
  1735. break;
  1736. case RV_NONE:
  1737. *res=0;
  1738. break;
  1739. default:
  1740. break;
  1741. }
  1742. return 0;
  1743. }
  1744. /**
  1745. * @brief Defined (integer) operation on rve
  1746. * @param h run action context
  1747. * @param msg SIP message
  1748. * @param res - set to 1 defined, 0 not defined
  1749. * @param rve rvalue expression
  1750. * @return 0 on success, -1 on error
  1751. */
  1752. inline static int int_rve_defined(struct run_act_ctx* h,
  1753. struct sip_msg* msg, int* res,
  1754. struct rval_expr* rve)
  1755. {
  1756. /* only a rval can be undefined, any expression consisting on more
  1757. then one rval => defined */
  1758. if (likely(rve->op==RVE_RVAL_OP))
  1759. return rv_defined(h, msg, res, &rve->left.rval, 0);
  1760. *res=1;
  1761. return 0;
  1762. }
  1763. /** evals an integer expr to an int.
  1764. *
  1765. * *res=(int)eval(rve)
  1766. * @return 0 on success, \<0 on error
  1767. */
  1768. int rval_expr_eval_int( struct run_act_ctx* h, struct sip_msg* msg,
  1769. int* res, struct rval_expr* rve)
  1770. {
  1771. int i1, i2, ret;
  1772. struct rval_cache c1, c2;
  1773. struct rvalue* rv1;
  1774. struct rvalue* rv2;
  1775. ret=-1;
  1776. switch(rve->op){
  1777. case RVE_RVAL_OP:
  1778. ret=rval_get_int(h, msg, res, &rve->left.rval, 0);
  1779. rval_get_int_handle_ret(ret, "rval expression conversion to int"
  1780. " failed", rve);
  1781. break;
  1782. case RVE_UMINUS_OP:
  1783. case RVE_BOOL_OP:
  1784. case RVE_LNOT_OP:
  1785. case RVE_BNOT_OP:
  1786. if (unlikely(
  1787. (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
  1788. break;
  1789. ret=int_intop1(res, rve->op, i1);
  1790. break;
  1791. case RVE_INT_OP:
  1792. ret=rval_expr_eval_int(h, msg, res, rve->left.rve);
  1793. break;
  1794. case RVE_MUL_OP:
  1795. case RVE_DIV_OP:
  1796. case RVE_MOD_OP:
  1797. case RVE_MINUS_OP:
  1798. case RVE_PLUS_OP:
  1799. case RVE_IPLUS_OP:
  1800. case RVE_BOR_OP:
  1801. case RVE_BAND_OP:
  1802. case RVE_BXOR_OP:
  1803. case RVE_BLSHIFT_OP:
  1804. case RVE_BRSHIFT_OP:
  1805. case RVE_GT_OP:
  1806. case RVE_GTE_OP:
  1807. case RVE_LT_OP:
  1808. case RVE_LTE_OP:
  1809. case RVE_IEQ_OP:
  1810. case RVE_IDIFF_OP:
  1811. if (unlikely(
  1812. (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
  1813. break;
  1814. if (unlikely(
  1815. (ret=rval_expr_eval_int(h, msg, &i2, rve->right.rve)) <0) )
  1816. break;
  1817. ret=int_intop2(res, rve->op, i1, i2);
  1818. break;
  1819. case RVE_LAND_OP:
  1820. if (unlikely(
  1821. (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
  1822. break;
  1823. if (i1==0){
  1824. *res=0;
  1825. }else{
  1826. if (unlikely( (ret=rval_expr_eval_int(h, msg, &i2,
  1827. rve->right.rve)) <0) )
  1828. break;
  1829. *res=i1 && i2;
  1830. }
  1831. ret=0;
  1832. break;
  1833. case RVE_LOR_OP:
  1834. if (unlikely(
  1835. (ret=rval_expr_eval_int(h, msg, &i1, rve->left.rve)) <0) )
  1836. break;
  1837. if (i1){
  1838. *res=1;
  1839. }else{
  1840. if (unlikely( (ret=rval_expr_eval_int(h, msg, &i2,
  1841. rve->right.rve)) <0) )
  1842. break;
  1843. *res=i1 || i2;
  1844. }
  1845. ret=0;
  1846. break;
  1847. case RVE_EQ_OP:
  1848. case RVE_DIFF_OP:
  1849. /* if left is string, eval left & right as string and
  1850. * use string diff.
  1851. * if left is int eval as int using int diff
  1852. * if left is undef, look at right and convert to right type
  1853. */
  1854. rval_cache_init(&c1);
  1855. if (unlikely( (ret=rval_expr_eval_rvint(h, msg, &rv1, &i1,
  1856. rve->left.rve, &c1))<0)){
  1857. /* error */
  1858. rval_cache_clean(&c1);
  1859. break;
  1860. }
  1861. if (likely(rv1==0)){
  1862. /* int */
  1863. rval_cache_clean(&c1);
  1864. if (unlikely( (ret=rval_expr_eval_int(h, msg, &i2,
  1865. rve->right.rve)) <0) )
  1866. break; /* error */
  1867. ret=int_intop2(res, rve->op, i1, i2);
  1868. }else{
  1869. /* not int => str or undef */
  1870. /* check for undefined left operand */
  1871. if (unlikely( c1.cache_type!=RV_CACHE_EMPTY &&
  1872. c1.val_type==RV_NONE)){
  1873. #ifdef UNDEF_EQ_ALWAYS_FALSE
  1874. /* undef == something always false
  1875. undef != something always true*/
  1876. ret=(rve->op==RVE_DIFF_OP);
  1877. #elif defined UNDEF_EQ_UNDEF_TRUE
  1878. /* undef == something defined always false
  1879. undef == undef true */
  1880. if (int_rve_defined(h, msg, &i2, rve->right.rve)<0){
  1881. /* error */
  1882. rval_cache_clean(&c1);
  1883. rval_destroy(rv1);
  1884. break;
  1885. }
  1886. ret=(!i2) ^ (rve->op==RVE_DIFF_OP);
  1887. #else /* ! UNDEF_EQ_* */
  1888. /* undef == val
  1889. * => convert to (type_of(val)) (undef) == val */
  1890. rval_cache_init(&c2);
  1891. if (unlikely( (ret=rval_expr_eval_rvint(h, msg, &rv2, &i2,
  1892. rve->right.rve, &c2))<0)){
  1893. /* error */
  1894. rval_cache_clean(&c1);
  1895. rval_cache_clean(&c2);
  1896. rval_destroy(rv1);
  1897. break;
  1898. }
  1899. if (rv2==0){
  1900. /* int */
  1901. ret=int_intop2(res, rve->op, 0 /* undef */, i2);
  1902. }else{
  1903. /* str or undef */
  1904. ret=rval_str_lop2(h, msg, res, rve->op, rv1, &c1,
  1905. rv2, &c2);
  1906. rval_cache_clean(&c2);
  1907. rval_destroy(rv2);
  1908. }
  1909. #endif /* UNDEF_EQ_* */
  1910. rval_cache_clean(&c1);
  1911. rval_destroy(rv1);
  1912. }else{
  1913. /* left value == defined and != int => str
  1914. * => lval == (str) val */
  1915. if (unlikely((rv2=rval_expr_eval(h, msg,
  1916. rve->right.rve))==0)){
  1917. /* error */
  1918. rval_destroy(rv1);
  1919. rval_cache_clean(&c1);
  1920. break;
  1921. }
  1922. ret=rval_str_lop2(h, msg, res, rve->op, rv1, &c1, rv2, 0);
  1923. rval_cache_clean(&c1);
  1924. rval_destroy(rv1);
  1925. rval_destroy(rv2);
  1926. }
  1927. }
  1928. break;
  1929. case RVE_CONCAT_OP:
  1930. /* eval expression => string */
  1931. if (unlikely((rv1=rval_expr_eval(h, msg, rve))==0)){
  1932. ret=-1;
  1933. break;
  1934. }
  1935. /* convert to int */
  1936. ret=rval_get_int(h, msg, res, rv1, 0); /* convert to int */
  1937. rval_get_int_handle_ret(ret, "rval expression conversion to int"
  1938. " failed", rve);
  1939. rval_destroy(rv1);
  1940. break;
  1941. case RVE_STR_OP:
  1942. /* (str)expr => eval expression */
  1943. rval_cache_init(&c1);
  1944. if (unlikely((ret=rval_expr_eval_rvint(h, msg, &rv1, res,
  1945. rve->left.rve, &c1))<0)){
  1946. /* error */
  1947. rval_cache_clean(&c1);
  1948. break;
  1949. }
  1950. if (unlikely(rv1)){
  1951. /* expr evaluated to string => (int)(str)v == (int)v */
  1952. ret=rval_get_int(h, msg, res, rv1, &c1); /* convert to int */
  1953. rval_get_int_handle_ret(ret, "rval expression conversion"
  1954. " to int failed", rve);
  1955. rval_destroy(rv1);
  1956. rval_cache_clean(&c1);
  1957. } /* else (rv1==0)
  1958. => expr evaluated to int =>
  1959. return (int)(str)v == (int)v => do nothing */
  1960. break;
  1961. #if 0
  1962. /* same thing as above, but in a not optimized, easier to
  1963. understand way */
  1964. /* 1. (str) expr => eval expr */
  1965. if (unlikely((rv1=rval_expr_eval(h, msg, rve->left.rve))==0)){
  1966. ret=-1;
  1967. break;
  1968. }
  1969. /* 2. convert to str and then convert to int
  1970. but since (int)(str)v == (int)v skip over (str)v */
  1971. ret=rval_get_int(h, msg, res, rv1, 0); /* convert to int */
  1972. rval_destroy(rv1);
  1973. break;
  1974. #endif
  1975. case RVE_DEFINED_OP:
  1976. ret=int_rve_defined(h, msg, res, rve->left.rve);
  1977. break;
  1978. case RVE_NOTDEFINED_OP:
  1979. ret=int_rve_defined(h, msg, res, rve->left.rve);
  1980. *res = !(*res);
  1981. break;
  1982. case RVE_STREQ_OP:
  1983. case RVE_STRDIFF_OP:
  1984. case RVE_MATCH_OP:
  1985. if (unlikely((rv1=rval_expr_eval(h, msg, rve->left.rve))==0)){
  1986. ret=-1;
  1987. break;
  1988. }
  1989. if (unlikely((rv2=rval_expr_eval(h, msg, rve->right.rve))==0)){
  1990. rval_destroy(rv1);
  1991. ret=-1;
  1992. break;
  1993. }
  1994. ret=rval_str_lop2(h, msg, res, rve->op, rv1, 0, rv2, 0);
  1995. rval_destroy(rv1);
  1996. rval_destroy(rv2);
  1997. break;
  1998. case RVE_STRLEN_OP:
  1999. case RVE_STREMPTY_OP:
  2000. if (unlikely((rv1=rval_expr_eval(h, msg, rve->left.rve))==0)){
  2001. ret=-1;
  2002. break;
  2003. }
  2004. ret=rval_int_strop1(h, msg, res, rve->op, rv1, 0);
  2005. rval_destroy(rv1);
  2006. break;
  2007. case RVE_NONE_OP:
  2008. /*default:*/
  2009. BUG("invalid rval int expression operation %d (%d,%d-%d,%d)\n",
  2010. rve->op, rve->fpos.s_line, rve->fpos.s_col,
  2011. rve->fpos.e_line, rve->fpos.e_col);
  2012. ret=-1;
  2013. };
  2014. return ret;
  2015. }
  2016. /**
  2017. * @brief Evals a rval expression into an int or another rv(str)
  2018. * @warning rv result (rv_res) must be rval_destroy()'ed if non-null
  2019. * (it might be a reference to another rval). The result can be
  2020. * modified only if rv_chg_in_place() returns true.
  2021. * @param h run action context
  2022. * @param msg SIP message
  2023. * @param res_rv pointer to rvalue result, if non-null it means the
  2024. * expression evaluated to a non-int (str), which will be stored here.
  2025. * @param res_i pointer to int result, if res_rv==0 and the function
  2026. * returns success => the result is an int which will be stored here.
  2027. * @param rve expression that will be evaluated.
  2028. * @param cache write-only value cache, it might be filled if non-null and
  2029. * empty (rval_cache_init()). If non-null, it _must_ be rval_cache_clean()'ed
  2030. * when done.
  2031. * @return 0 on success, -1 on error, sets *res_rv or *res_i.
  2032. */
  2033. int rval_expr_eval_rvint( struct run_act_ctx* h,
  2034. struct sip_msg* msg,
  2035. struct rvalue** res_rv,
  2036. int* res_i,
  2037. struct rval_expr* rve,
  2038. struct rval_cache* cache
  2039. )
  2040. {
  2041. struct rvalue* rv1;
  2042. struct rvalue* rv2;
  2043. struct rval_cache c1; /* local cache */
  2044. int ret;
  2045. int r, i, j;
  2046. enum rval_type type;
  2047. rv1=0;
  2048. rv2=0;
  2049. ret=-1;
  2050. switch(rve->op){
  2051. case RVE_RVAL_OP:
  2052. rv1=&rve->left.rval;
  2053. rv_ref(rv1);
  2054. type=rval_get_btype(h, msg, rv1, cache);
  2055. if (type==RV_INT){
  2056. r=rval_get_int(h, msg, res_i, rv1, cache);
  2057. rval_get_int_handle_ret(r, "rval expression conversion"
  2058. " to int failed", rve);
  2059. *res_rv=0;
  2060. ret=r; /* equiv. to if (r<0) goto error */
  2061. }else{
  2062. /* RV_STR, RV_PVAR, RV_AVP a.s.o => return rv1 and the
  2063. cached resolved value in cache*/
  2064. *res_rv=rv1;
  2065. rv_ref(rv1);
  2066. ret=0;
  2067. }
  2068. break;
  2069. case RVE_UMINUS_OP:
  2070. case RVE_BOOL_OP:
  2071. case RVE_LNOT_OP:
  2072. case RVE_BNOT_OP:
  2073. case RVE_MINUS_OP:
  2074. case RVE_MUL_OP:
  2075. case RVE_DIV_OP:
  2076. case RVE_MOD_OP:
  2077. case RVE_BOR_OP:
  2078. case RVE_BAND_OP:
  2079. case RVE_BXOR_OP:
  2080. case RVE_BLSHIFT_OP:
  2081. case RVE_BRSHIFT_OP:
  2082. case RVE_LAND_OP:
  2083. case RVE_LOR_OP:
  2084. case RVE_GT_OP:
  2085. case RVE_GTE_OP:
  2086. case RVE_LT_OP:
  2087. case RVE_LTE_OP:
  2088. case RVE_EQ_OP:
  2089. case RVE_DIFF_OP:
  2090. case RVE_IEQ_OP:
  2091. case RVE_IDIFF_OP:
  2092. case RVE_IPLUS_OP:
  2093. case RVE_STREQ_OP:
  2094. case RVE_STRDIFF_OP:
  2095. case RVE_MATCH_OP:
  2096. case RVE_STRLEN_OP:
  2097. case RVE_STREMPTY_OP:
  2098. case RVE_DEFINED_OP:
  2099. case RVE_NOTDEFINED_OP:
  2100. case RVE_INT_OP:
  2101. /* operator forces integer type */
  2102. ret=rval_expr_eval_int(h, msg, res_i, rve);
  2103. *res_rv=0;
  2104. break;
  2105. case RVE_PLUS_OP:
  2106. rval_cache_init(&c1);
  2107. r=rval_expr_eval_rvint(h, msg, &rv1, &i, rve->left.rve, &c1);
  2108. if (unlikely(r<0)){
  2109. ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
  2110. rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
  2111. rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col
  2112. );
  2113. rval_cache_clean(&c1);
  2114. goto error;
  2115. }
  2116. if (rv1==0){
  2117. if (unlikely((r=rval_expr_eval_int(h, msg, &j,
  2118. rve->right.rve))<0)){
  2119. ERR("rval expression evaluation failed (%d,%d-%d,%d)"
  2120. "\n", rve->right.rve->fpos.s_line,
  2121. rve->right.rve->fpos.s_col,
  2122. rve->right.rve->fpos.e_line,
  2123. rve->right.rve->fpos.e_col);
  2124. rval_cache_clean(&c1);
  2125. goto error;
  2126. }
  2127. ret=int_intop2(res_i, rve->op, i, j);
  2128. *res_rv=0;
  2129. }else{
  2130. rv2=rval_expr_eval(h, msg, rve->right.rve);
  2131. if (unlikely(rv2==0)){
  2132. ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
  2133. rve->right.rve->fpos.s_line,
  2134. rve->right.rve->fpos.s_col,
  2135. rve->right.rve->fpos.e_line,
  2136. rve->right.rve->fpos.e_col);
  2137. rval_cache_clean(&c1);
  2138. goto error;
  2139. }
  2140. *res_rv=rval_str_add2(h, msg, rv1, &c1, rv2, 0);
  2141. ret=-(*res_rv==0);
  2142. }
  2143. rval_cache_clean(&c1);
  2144. break;
  2145. case RVE_CONCAT_OP:
  2146. case RVE_STR_OP:
  2147. *res_rv=rval_expr_eval(h, msg, rve);
  2148. ret=-(*res_rv==0);
  2149. break;
  2150. case RVE_NONE_OP:
  2151. /*default:*/
  2152. BUG("invalid rval expression operation %d (%d,%d-%d,%d)\n",
  2153. rve->op, rve->fpos.s_line, rve->fpos.s_col,
  2154. rve->fpos.e_line, rve->fpos.e_col);
  2155. goto error;
  2156. };
  2157. rval_destroy(rv1);
  2158. rval_destroy(rv2);
  2159. return ret;
  2160. error:
  2161. rval_destroy(rv1);
  2162. rval_destroy(rv2);
  2163. return -1;
  2164. }
  2165. /**
  2166. * @brief Evals a rval expression
  2167. * @warning result must be rval_destroy()'ed if non-null (it might be
  2168. * a reference to another rval). The result can be modified only
  2169. * if rv_chg_in_place() returns true.
  2170. * @param h run action context
  2171. * @param msg SIP message
  2172. * @param rve rvalue expression
  2173. * @return rvalue on success, 0 on error
  2174. */
  2175. struct rvalue* rval_expr_eval(struct run_act_ctx* h, struct sip_msg* msg,
  2176. struct rval_expr* rve)
  2177. {
  2178. struct rvalue* rv1;
  2179. struct rvalue* rv2;
  2180. struct rvalue* ret;
  2181. struct rval_cache c1;
  2182. union rval_val v;
  2183. int r, i, j;
  2184. enum rval_type type;
  2185. rv1=0;
  2186. rv2=0;
  2187. ret=0;
  2188. switch(rve->op){
  2189. case RVE_RVAL_OP:
  2190. rv_ref(&rve->left.rval);
  2191. return &rve->left.rval;
  2192. break;
  2193. case RVE_UMINUS_OP:
  2194. case RVE_BOOL_OP:
  2195. case RVE_LNOT_OP:
  2196. case RVE_BNOT_OP:
  2197. case RVE_MINUS_OP:
  2198. case RVE_MUL_OP:
  2199. case RVE_DIV_OP:
  2200. case RVE_MOD_OP:
  2201. case RVE_BOR_OP:
  2202. case RVE_BAND_OP:
  2203. case RVE_BXOR_OP:
  2204. case RVE_BLSHIFT_OP:
  2205. case RVE_BRSHIFT_OP:
  2206. case RVE_LAND_OP:
  2207. case RVE_LOR_OP:
  2208. case RVE_GT_OP:
  2209. case RVE_GTE_OP:
  2210. case RVE_LT_OP:
  2211. case RVE_LTE_OP:
  2212. case RVE_EQ_OP:
  2213. case RVE_DIFF_OP:
  2214. case RVE_IEQ_OP:
  2215. case RVE_IDIFF_OP:
  2216. case RVE_IPLUS_OP:
  2217. case RVE_STREQ_OP:
  2218. case RVE_STRDIFF_OP:
  2219. case RVE_MATCH_OP:
  2220. case RVE_STRLEN_OP:
  2221. case RVE_STREMPTY_OP:
  2222. case RVE_DEFINED_OP:
  2223. case RVE_NOTDEFINED_OP:
  2224. case RVE_INT_OP:
  2225. /* operator forces integer type */
  2226. r=rval_expr_eval_int(h, msg, &i, rve);
  2227. if (likely(r==0)){
  2228. v.l=i;
  2229. ret=rval_new(RV_INT, &v, 0);
  2230. if (unlikely(ret==0)){
  2231. ERR("rv eval int expression: out of memory\n");
  2232. goto error;
  2233. }
  2234. return ret;
  2235. }else{
  2236. ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
  2237. rve->fpos.s_line, rve->fpos.s_col,
  2238. rve->fpos.e_line, rve->fpos.e_col);
  2239. goto error;
  2240. }
  2241. break;
  2242. case RVE_PLUS_OP:
  2243. rv1=rval_expr_eval(h, msg, rve->left.rve);
  2244. if (unlikely(rv1==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. goto error;
  2249. }
  2250. rval_cache_init(&c1);
  2251. type=rval_get_btype(h, msg, rv1, &c1);
  2252. switch(type){
  2253. case RV_INT:
  2254. r=rval_get_int(h, msg, &i, rv1, &c1);
  2255. rval_get_int_handle_ret(r, "rval expression left side "
  2256. "conversion to int failed",
  2257. rve);
  2258. if (unlikely(r<0)){
  2259. rval_cache_clean(&c1);
  2260. goto error;
  2261. }
  2262. if (unlikely((r=rval_expr_eval_int(h, msg, &j,
  2263. rve->right.rve))<0)){
  2264. rval_cache_clean(&c1);
  2265. ERR("rval expression evaluation failed (%d,%d-%d,%d):"
  2266. " could not evaluate right side to int\n",
  2267. rve->fpos.s_line, rve->fpos.s_col,
  2268. rve->fpos.e_line, rve->fpos.e_col);
  2269. goto error;
  2270. }
  2271. int_intop2(&r, rve->op, i, j);
  2272. if (rv_chg_in_place(rv1)){
  2273. rv1->v.l=r;
  2274. ret=rv1;
  2275. rv_ref(ret);
  2276. }else{
  2277. v.l=r;
  2278. ret=rval_new(RV_INT, &v, 0);
  2279. if (unlikely(ret==0)){
  2280. rval_cache_clean(&c1);
  2281. ERR("rv eval int expression: out of memory\n");
  2282. goto error;
  2283. }
  2284. }
  2285. break;
  2286. case RV_STR:
  2287. case RV_NONE:
  2288. rv2=rval_expr_eval(h, msg, rve->right.rve);
  2289. if (unlikely(rv2==0)){
  2290. ERR("rval expression evaluation failed (%d,%d-%d,%d)"
  2291. "\n", rve->right.rve->fpos.s_line,
  2292. rve->right.rve->fpos.s_col,
  2293. rve->right.rve->fpos.e_line,
  2294. rve->right.rve->fpos.e_col);
  2295. rval_cache_clean(&c1);
  2296. goto error;
  2297. }
  2298. ret=rval_str_add2(h, msg, rv1, &c1, rv2, 0);
  2299. break;
  2300. default:
  2301. BUG("rv unsupported basic type %d (%d,%d-%d,%d)\n", type,
  2302. rve->fpos.s_line, rve->fpos.s_col,
  2303. rve->fpos.e_line, rve->fpos.e_col);
  2304. }
  2305. rval_cache_clean(&c1);
  2306. break;
  2307. case RVE_CONCAT_OP:
  2308. rv1=rval_expr_eval(h, msg, rve->left.rve);
  2309. if (unlikely(rv1==0)){
  2310. ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
  2311. rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
  2312. rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col);
  2313. goto error;
  2314. }
  2315. rv2=rval_expr_eval(h, msg, rve->right.rve);
  2316. if (unlikely(rv2==0)){
  2317. ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
  2318. rve->right.rve->fpos.s_line,
  2319. rve->right.rve->fpos.s_col,
  2320. rve->right.rve->fpos.e_line,
  2321. rve->right.rve->fpos.e_col);
  2322. goto error;
  2323. }
  2324. ret=rval_str_add2(h, msg, rv1, 0, rv2, 0);
  2325. break;
  2326. case RVE_STR_OP:
  2327. rv1=rval_expr_eval(h, msg, rve->left.rve);
  2328. if (unlikely(rv1==0)){
  2329. ERR("rval expression evaluation failed (%d,%d-%d,%d)\n",
  2330. rve->left.rve->fpos.s_line, rve->left.rve->fpos.s_col,
  2331. rve->left.rve->fpos.e_line, rve->left.rve->fpos.e_col);
  2332. goto error;
  2333. }
  2334. ret=rval_convert(h, msg, RV_STR, rv1, 0);
  2335. break;
  2336. case RVE_NONE_OP:
  2337. /*default:*/
  2338. BUG("invalid rval expression operation %d (%d,%d-%d,%d)\n",
  2339. rve->op, rve->fpos.s_line, rve->fpos.s_col,
  2340. rve->fpos.e_line, rve->fpos.e_col);
  2341. goto error;
  2342. };
  2343. rval_destroy(rv1);
  2344. rval_destroy(rv2);
  2345. return ret;
  2346. error:
  2347. rval_destroy(rv1);
  2348. rval_destroy(rv2);
  2349. return 0;
  2350. }
  2351. /** evals a rval expr and always returns a new rval.
  2352. * like rval_expr_eval, but always returns a new rvalue (never a reference
  2353. * to an exisiting one).
  2354. * WARNING: result must be rval_destroy()'ed if non-null (it might be
  2355. * a reference to another rval). The result can be modified only
  2356. * if rv_chg_in_place() returns true.
  2357. * @result rvalue on success, 0 on error
  2358. */
  2359. struct rvalue* rval_expr_eval_new(struct run_act_ctx* h, struct sip_msg* msg,
  2360. struct rval_expr* rve)
  2361. {
  2362. struct rvalue* ret;
  2363. struct rvalue* rv;
  2364. ret=rval_expr_eval(h, msg, rve);
  2365. if (ret && !rv_chg_in_place(ret)){
  2366. rv=ret;
  2367. /* create a new rv */
  2368. ret=rval_new(rv->type, &rv->v, 0);
  2369. rval_destroy(rv);
  2370. }
  2371. return ret;
  2372. }
  2373. /** create a RVE_RVAL_OP rval_expr, containing a single rval of the given type.
  2374. *
  2375. * @param rv_type - rval type
  2376. * @param val - rval value
  2377. * @param pos - config position
  2378. * @return new pkg_malloc'ed rval_expr or 0 on error.
  2379. */
  2380. struct rval_expr* mk_rval_expr_v(enum rval_type rv_type, void* val,
  2381. struct cfg_pos* pos)
  2382. {
  2383. struct rval_expr* rve;
  2384. union rval_val v;
  2385. str* s;
  2386. int flags;
  2387. rve=pkg_malloc(sizeof(*rve));
  2388. if (rve==0)
  2389. return 0;
  2390. memset(rve, 0, sizeof(*rve));
  2391. flags=0;
  2392. switch(rv_type){
  2393. case RV_INT:
  2394. v.l=(long)val;
  2395. break;
  2396. case RV_STR:
  2397. s=(str*)val;
  2398. v.s.s=pkg_malloc(s->len+1 /*0*/);
  2399. if (v.s.s==0){
  2400. pkg_free(rve);
  2401. ERR("memory allocation failure\n");
  2402. return 0;
  2403. }
  2404. v.s.len=s->len;
  2405. memcpy(v.s.s, s->s, s->len);
  2406. v.s.s[s->len]=0;
  2407. flags=RV_CNT_ALLOCED_F;
  2408. break;
  2409. case RV_AVP:
  2410. v.avps=*(avp_spec_t*)val;
  2411. break;
  2412. case RV_PVAR:
  2413. v.pvs=*(pv_spec_t*)val;
  2414. break;
  2415. case RV_SEL:
  2416. v.sel=*(select_t*)val;
  2417. break;
  2418. case RV_BEXPR:
  2419. v.bexpr=(struct expr*)val;
  2420. break;
  2421. case RV_ACTION_ST:
  2422. v.action=(struct action*)val;
  2423. break;
  2424. default:
  2425. BUG("unsupported rv type %d\n", rv_type);
  2426. pkg_free(rve);
  2427. return 0;
  2428. }
  2429. rval_init(&rve->left.rval, rv_type, &v, flags);
  2430. rve->op=RVE_RVAL_OP;
  2431. if (pos) rve->fpos=*pos;
  2432. return rve;
  2433. }
  2434. /**
  2435. * @brief Create a unary op. rval_expr
  2436. * ret= op rve1
  2437. * @param op - rval expr. unary operator
  2438. * @param rve1 - rval expr. on which the operator will act.
  2439. * @param pos configuration position
  2440. * @return new pkg_malloc'ed rval_expr or 0 on error.
  2441. */
  2442. struct rval_expr* mk_rval_expr1(enum rval_expr_op op, struct rval_expr* rve1,
  2443. struct cfg_pos* pos)
  2444. {
  2445. struct rval_expr* ret;
  2446. switch(op){
  2447. case RVE_UMINUS_OP:
  2448. case RVE_BOOL_OP:
  2449. case RVE_LNOT_OP:
  2450. case RVE_BNOT_OP:
  2451. case RVE_STRLEN_OP:
  2452. case RVE_STREMPTY_OP:
  2453. case RVE_DEFINED_OP:
  2454. case RVE_NOTDEFINED_OP:
  2455. case RVE_INT_OP:
  2456. case RVE_STR_OP:
  2457. break;
  2458. default:
  2459. BUG("unsupported unary operator %d\n", op);
  2460. return 0;
  2461. }
  2462. ret=pkg_malloc(sizeof(*ret));
  2463. if (ret==0)
  2464. return 0;
  2465. memset(ret, 0, sizeof(*ret));
  2466. ret->op=op;
  2467. ret->left.rve=rve1;
  2468. if (pos) ret->fpos=*pos;
  2469. return ret;
  2470. }
  2471. /**
  2472. * @brief Create a rval_expr. from 2 other rval exprs, using op
  2473. * ret = rve1 op rve2
  2474. * @param op - rval expr. operator
  2475. * @param rve1 - rval expr. on which the operator will act.
  2476. * @param rve2 - rval expr. on which the operator will act.
  2477. * @param pos configuration position
  2478. * @return new pkg_malloc'ed rval_expr or 0 on error.
  2479. */
  2480. struct rval_expr* mk_rval_expr2(enum rval_expr_op op, struct rval_expr* rve1,
  2481. struct rval_expr* rve2,
  2482. struct cfg_pos* pos)
  2483. {
  2484. struct rval_expr* ret;
  2485. switch(op){
  2486. case RVE_MUL_OP:
  2487. case RVE_DIV_OP:
  2488. case RVE_MOD_OP:
  2489. case RVE_MINUS_OP:
  2490. case RVE_BOR_OP:
  2491. case RVE_BAND_OP:
  2492. case RVE_BXOR_OP:
  2493. case RVE_BLSHIFT_OP:
  2494. case RVE_BRSHIFT_OP:
  2495. case RVE_LAND_OP:
  2496. case RVE_LOR_OP:
  2497. case RVE_GT_OP:
  2498. case RVE_GTE_OP:
  2499. case RVE_LT_OP:
  2500. case RVE_LTE_OP:
  2501. case RVE_PLUS_OP:
  2502. case RVE_IPLUS_OP:
  2503. case RVE_EQ_OP:
  2504. case RVE_DIFF_OP:
  2505. case RVE_IEQ_OP:
  2506. case RVE_IDIFF_OP:
  2507. case RVE_STREQ_OP:
  2508. case RVE_STRDIFF_OP:
  2509. case RVE_MATCH_OP:
  2510. case RVE_CONCAT_OP:
  2511. break;
  2512. default:
  2513. BUG("unsupported operator %d\n", op);
  2514. return 0;
  2515. }
  2516. ret=pkg_malloc(sizeof(*ret));
  2517. if (ret==0)
  2518. return 0;
  2519. memset(ret, 0, sizeof(*ret));
  2520. ret->op=op;
  2521. ret->left.rve=rve1;
  2522. ret->right.rve=rve2;
  2523. if (pos) ret->fpos=*pos;
  2524. return ret;
  2525. }
  2526. /** returns true if the operator is associative. */
  2527. static int rve_op_is_assoc(enum rval_expr_op op)
  2528. {
  2529. switch(op){
  2530. case RVE_NONE_OP:
  2531. case RVE_RVAL_OP:
  2532. case RVE_UMINUS_OP:
  2533. case RVE_BOOL_OP:
  2534. case RVE_LNOT_OP:
  2535. case RVE_BNOT_OP:
  2536. case RVE_STRLEN_OP:
  2537. case RVE_STREMPTY_OP:
  2538. case RVE_DEFINED_OP:
  2539. case RVE_NOTDEFINED_OP:
  2540. case RVE_INT_OP:
  2541. case RVE_STR_OP:
  2542. /* one operand expression => cannot be assoc. */
  2543. return 0;
  2544. case RVE_DIV_OP:
  2545. case RVE_MOD_OP:
  2546. case RVE_MINUS_OP:
  2547. case RVE_BLSHIFT_OP:
  2548. case RVE_BRSHIFT_OP:
  2549. return 0;
  2550. case RVE_PLUS_OP:
  2551. /* the generic plus is not assoc, e.g.
  2552. "a" + 1 + "2" => "a12" in one case and "a3" in the other */
  2553. return 0;
  2554. case RVE_IPLUS_OP:
  2555. case RVE_CONCAT_OP:
  2556. case RVE_MUL_OP:
  2557. case RVE_BAND_OP:
  2558. case RVE_BOR_OP:
  2559. case RVE_BXOR_OP:
  2560. return 1;
  2561. case RVE_LAND_OP:
  2562. case RVE_LOR_OP:
  2563. return 1;
  2564. case RVE_GT_OP:
  2565. case RVE_GTE_OP:
  2566. case RVE_LT_OP:
  2567. case RVE_LTE_OP:
  2568. case RVE_EQ_OP:
  2569. case RVE_DIFF_OP:
  2570. case RVE_IEQ_OP:
  2571. case RVE_IDIFF_OP:
  2572. case RVE_STREQ_OP:
  2573. case RVE_STRDIFF_OP:
  2574. case RVE_MATCH_OP:
  2575. return 0;
  2576. }
  2577. return 0;
  2578. }
  2579. /** returns true if the operator is commutative. */
  2580. static int rve_op_is_commutative(enum rval_expr_op op)
  2581. {
  2582. switch(op){
  2583. case RVE_NONE_OP:
  2584. case RVE_RVAL_OP:
  2585. case RVE_UMINUS_OP:
  2586. case RVE_BOOL_OP:
  2587. case RVE_LNOT_OP:
  2588. case RVE_BNOT_OP:
  2589. case RVE_STRLEN_OP:
  2590. case RVE_STREMPTY_OP:
  2591. case RVE_DEFINED_OP:
  2592. case RVE_NOTDEFINED_OP:
  2593. case RVE_INT_OP:
  2594. case RVE_STR_OP:
  2595. /* one operand expression => cannot be commut. */
  2596. return 0;
  2597. case RVE_DIV_OP:
  2598. case RVE_MOD_OP:
  2599. case RVE_MINUS_OP:
  2600. case RVE_BLSHIFT_OP:
  2601. case RVE_BRSHIFT_OP:
  2602. return 0;
  2603. case RVE_PLUS_OP:
  2604. /* non commut. when diff. type
  2605. (e.g 1 + "2" != "2" + 1 ) => non commut. in general
  2606. (specific same type versions are covered by IPLUS & CONCAT) */
  2607. return 0;
  2608. case RVE_IPLUS_OP:
  2609. case RVE_MUL_OP:
  2610. case RVE_BAND_OP:
  2611. case RVE_BOR_OP:
  2612. case RVE_BXOR_OP:
  2613. case RVE_LAND_OP:
  2614. case RVE_LOR_OP:
  2615. case RVE_IEQ_OP:
  2616. case RVE_IDIFF_OP:
  2617. case RVE_STREQ_OP:
  2618. case RVE_STRDIFF_OP:
  2619. return 1;
  2620. case RVE_GT_OP:
  2621. case RVE_GTE_OP:
  2622. case RVE_LT_OP:
  2623. case RVE_LTE_OP:
  2624. case RVE_CONCAT_OP:
  2625. case RVE_MATCH_OP:
  2626. return 0;
  2627. case RVE_DIFF_OP:
  2628. case RVE_EQ_OP:
  2629. /* non. commut. in general, only for same type e.g.:
  2630. "" == 0 diff. 0 == "" ( "" == "0" and 0 == 0)
  2631. same type versions are covered by IEQ, IDIFF, STREQ, STRDIFF
  2632. */
  2633. return 0 /* asymmetrical undef handling */;
  2634. }
  2635. return 0;
  2636. }
  2637. #if 0
  2638. /** returns true if the rval expr can be optimized to an int.
  2639. * (if left & right are leafs (RVE_RVAL_OP) and both of them are
  2640. * ints return true, else false)
  2641. * @return 0 or 1
  2642. */
  2643. static int rve_can_optimize_int(struct rval_expr* rve)
  2644. {
  2645. if (scr_opt_lev<1)
  2646. return 0;
  2647. if (rve->op == RVE_RVAL_OP)
  2648. return 0;
  2649. if (rve->left.rve->op != RVE_RVAL_OP)
  2650. return 0;
  2651. if (rve->left.rve->left.rval.type!=RV_INT)
  2652. return 0;
  2653. if (rve->right.rve){
  2654. if (rve->right.rve->op != RVE_RVAL_OP)
  2655. return 0;
  2656. if (rve->right.rve->left.rval.type!=RV_INT)
  2657. return 0;
  2658. }
  2659. DBG("rve_can_optimize_int: left %d, right %d\n",
  2660. rve->left.rve->op, rve->right.rve?rve->right.rve->op:0);
  2661. return 1;
  2662. }
  2663. /** returns true if the rval expr can be optimized to a str.
  2664. * (if left & right are leafs (RVE_RVAL_OP) and both of them are
  2665. * str or left is str and right is int return true, else false)
  2666. * @return 0 or 1
  2667. */
  2668. static int rve_can_optimize_str(struct rval_expr* rve)
  2669. {
  2670. if (scr_opt_lev<1)
  2671. return 0;
  2672. if (rve->op == RVE_RVAL_OP)
  2673. return 0;
  2674. DBG("rve_can_optimize_str: left %d, right %d\n",
  2675. rve->left.rve->op, rve->right.rve?rve->right.rve->op:0);
  2676. if (rve->left.rve->op != RVE_RVAL_OP)
  2677. return 0;
  2678. if (rve->left.rve->left.rval.type!=RV_STR)
  2679. return 0;
  2680. if (rve->right.rve){
  2681. if (rve->right.rve->op != RVE_RVAL_OP)
  2682. return 0;
  2683. if ((rve->right.rve->left.rval.type!=RV_STR) &&
  2684. (rve->right.rve->left.rval.type!=RV_INT))
  2685. return 0;
  2686. }
  2687. return 1;
  2688. }
  2689. #endif
  2690. static int fix_rval(struct rvalue* rv)
  2691. {
  2692. DBG("RV fixing type %d\n", rv->type);
  2693. switch(rv->type){
  2694. case RV_INT:
  2695. /*nothing to do*/
  2696. DBG("RV is int: %d\n", (int)rv->v.l);
  2697. return 0;
  2698. case RV_STR:
  2699. /*nothing to do*/
  2700. DBG("RV is str: \"%s\"\n", rv->v.s.s);
  2701. return 0;
  2702. case RV_BEXPR:
  2703. return fix_expr(rv->v.bexpr);
  2704. case RV_ACTION_ST:
  2705. return fix_actions(rv->v.action);
  2706. case RV_SEL:
  2707. if (resolve_select(&rv->v.sel)<0){
  2708. ERR("Unable to resolve select\n");
  2709. print_select(&rv->v.sel);
  2710. }
  2711. return 0;
  2712. case RV_AVP:
  2713. /* nothing to do, resolved at runtime */
  2714. return 0;
  2715. case RV_PVAR:
  2716. /* nothing to do, resolved at parsing time */
  2717. return 0;
  2718. case RV_NONE:
  2719. BUG("uninitialized rvalue\n");
  2720. return -1;
  2721. }
  2722. BUG("unknown rvalue type %d\n", rv->type);
  2723. return -1;
  2724. }
  2725. /**
  2726. * @brief Helper function: replace a rve (in-place) with a constant rval_val
  2727. * @warning since it replaces in-place, one should make sure that if
  2728. * rve is in fact a rval (rve->op==RVE_RVAL_OP), no reference is kept
  2729. * to the rval!
  2730. * @param rve expression to be replaced (in-place)
  2731. * @param type rvalue type
  2732. * @param v pointer to a rval_val union containing the replacement value.
  2733. * @param flags value flags (how it was alloc'ed, e.g.: RV_CNT_ALLOCED_F)
  2734. * @return 0 on success, -1 on error
  2735. */
  2736. static int rve_replace_with_val(struct rval_expr* rve, enum rval_type type,
  2737. union rval_val* v, int flags)
  2738. {
  2739. int refcnt;
  2740. refcnt=1; /* replaced-in-place rval refcnt */
  2741. if (rve->op!=RVE_RVAL_OP){
  2742. rve_destroy(rve->left.rve);
  2743. if (rve_op_unary(rve->op)==0)
  2744. rve_destroy(rve->right.rve);
  2745. }else{
  2746. if (rve->left.rval.refcnt!=1){
  2747. BUG("trying to replace a referenced rval! (refcnt=%d)\n",
  2748. rve->left.rval.refcnt);
  2749. /* try to recover */
  2750. refcnt=rve->left.rval.refcnt;
  2751. abort(); /* find bugs quicker -- andrei */
  2752. }
  2753. rval_destroy(&rve->left.rval);
  2754. }
  2755. rval_init(&rve->left.rval, type, v, flags);
  2756. rve->left.rval.refcnt=refcnt;
  2757. rval_init(&rve->right.rval, RV_NONE, 0, 0);
  2758. rve->op=RVE_RVAL_OP;
  2759. return 0;
  2760. }
  2761. /** helper function: replace a rve (in-place) with a constant rvalue.
  2762. * @param rve - expression to be replaced (in-place)
  2763. * @param rv - pointer to the replacement _constant_ rvalue structure
  2764. * @return 0 on success, -1 on error */
  2765. static int rve_replace_with_ct_rv(struct rval_expr* rve, struct rvalue* rv)
  2766. {
  2767. enum rval_type type;
  2768. int flags;
  2769. int i;
  2770. union rval_val v;
  2771. type=rv->type;
  2772. flags=0;
  2773. if (rv->type==RV_INT){
  2774. if (rval_get_int(0, 0, &i, rv, 0)!=0){
  2775. BUG("unexpected int evaluation failure (%d,%d-%d,%d)\n",
  2776. rve->fpos.s_line, rve->fpos.s_col,
  2777. rve->fpos.e_line, rve->fpos.e_col);
  2778. return -1;
  2779. }
  2780. v.l=i;
  2781. }else if(rv->type==RV_STR){
  2782. if (rval_get_str(0, 0, &v.s, rv, 0)<0){
  2783. BUG("unexpected str evaluation failure(%d,%d-%d,%d)\n",
  2784. rve->fpos.s_line, rve->fpos.s_col,
  2785. rve->fpos.e_line, rve->fpos.e_col);
  2786. return -1;
  2787. }
  2788. flags|=RV_CNT_ALLOCED_F;
  2789. }else{
  2790. BUG("unknown constant expression type %d (%d,%d-%d,%d)\n", rv->type,
  2791. rve->fpos.s_line, rve->fpos.s_col,
  2792. rve->fpos.e_line, rve->fpos.e_col);
  2793. return -1;
  2794. }
  2795. return rve_replace_with_val(rve, type, &v, flags);
  2796. }
  2797. /** try to replace the right side of the rve with a compiled regex.
  2798. * @return 0 on success and -1 on error.
  2799. */
  2800. static int fix_match_rve(struct rval_expr* rve)
  2801. {
  2802. struct rvalue* rv;
  2803. regex_t* re;
  2804. union rval_val v;
  2805. int flags;
  2806. int ret;
  2807. rv=0;
  2808. v.s.s=0;
  2809. v.re.regex=0;
  2810. /* normal fix-up for the left side */
  2811. ret=fix_rval_expr((void*)rve->left.rve);
  2812. if (ret<0) return ret;
  2813. /* fixup the right side (RE) */
  2814. if (rve_is_constant(rve->right.rve)){
  2815. if ((rve_guess_type(rve->right.rve)!=RV_STR)){
  2816. ERR("fixup failure(%d,%d-%d,%d): left side of =~ is not string"
  2817. " (%d,%d)\n", rve->fpos.s_line, rve->fpos.s_col,
  2818. rve->fpos.e_line, rve->fpos.e_col,
  2819. rve->right.rve->fpos.s_line,
  2820. rve->right.rve->fpos.s_col);
  2821. goto error;
  2822. }
  2823. if ((rv=rval_expr_eval(0, 0, rve->right.rve))==0){
  2824. ERR("fixup failure(%d,%d-%d,%d): bad RE expression\n",
  2825. rve->right.rve->fpos.s_line, rve->right.rve->fpos.s_col,
  2826. rve->right.rve->fpos.e_line, rve->right.rve->fpos.e_col);
  2827. goto error;
  2828. }
  2829. if (rval_get_str(0, 0, &v.s, rv, 0)<0){
  2830. BUG("fixup unexpected failure (%d,%d-%d,%d)\n",
  2831. rve->fpos.s_line, rve->fpos.s_col,
  2832. rve->fpos.e_line, rve->fpos.e_col);
  2833. goto error;
  2834. }
  2835. /* we have the str, we don't need the rv anymore */
  2836. rval_destroy(rv);
  2837. rv=0;
  2838. re=pkg_malloc(sizeof(*re));
  2839. if (re==0){
  2840. ERR("out of memory\n");
  2841. goto error;
  2842. }
  2843. /* same flags as for expr. =~ (fix_expr()) */
  2844. if (regcomp(re, v.s.s, REG_EXTENDED|REG_NOSUB|REG_ICASE)){
  2845. pkg_free(re);
  2846. ERR("Bad regular expression \"%s\"(%d,%d-%d,%d)\n", v.s.s,
  2847. rve->right.rve->fpos.s_line, rve->right.rve->fpos.s_col,
  2848. rve->right.rve->fpos.e_line, rve->right.rve->fpos.e_col);
  2849. goto error;
  2850. }
  2851. v.re.regex=re;
  2852. flags=RV_RE_F|RV_RE_ALLOCED_F|RV_CNT_ALLOCED_F;
  2853. if (rve_replace_with_val(rve->right.rve, RV_STR, &v, flags)<0)
  2854. goto error;
  2855. }else{
  2856. /* right side is not constant => normal fixup */
  2857. return fix_rval_expr((void*)rve->right.rve);
  2858. }
  2859. return 0;
  2860. error:
  2861. if (rv) rval_destroy(rv);
  2862. if (v.s.s) pkg_free(v.s.s);
  2863. if (v.re.regex){
  2864. regfree(v.re.regex);
  2865. pkg_free(v.re.regex);
  2866. }
  2867. return -1;
  2868. }
  2869. /** optimize op($v, 0) or op($v, 1).
  2870. * Note: internal use only from rve_optimize
  2871. * It should be called after ct optimization, for non-contant
  2872. * expressions (the left or right side is not constant).
  2873. * @return 1 on success (rve was changed), 0 on failure and -1 on error
  2874. */
  2875. static int rve_opt_01(struct rval_expr* rve, enum rval_type rve_type)
  2876. {
  2877. struct rvalue* rv;
  2878. struct rval_expr* ct_rve;
  2879. struct rval_expr* v_rve;
  2880. int i;
  2881. int ret;
  2882. enum rval_expr_op op;
  2883. struct cfg_pos pos;
  2884. int right; /* debugging msg */
  2885. int dbg; /* debugging msg on/off */
  2886. /* helper macro: replace in-place a <ctype> type rve with v (another rve).
  2887. * if type_of(v)== <ctype> => rve:=*v (copy v contents into rve and free v)
  2888. * else if type_of(v)!=<ctype> => rve:= (ctype) v (casts v to <ctype>)
  2889. * Uses pos.
  2890. * ctype can be INT or STR
  2891. * WARNING: - v might be pkg_free()'d
  2892. * - rve members _are_ _not_ freed or destroyed
  2893. */
  2894. #define replace_rve_type_cast(e, v, ctype) \
  2895. do{\
  2896. if ( rve_guess_type((v)) == RV_##ctype ){\
  2897. /* if type_of($v)==int we don't need to add an \
  2898. int cast operator => replace with v */\
  2899. pos=(e)->fpos; \
  2900. *(e)=*(v); /* replace e with v (in-place) */ \
  2901. (e)->fpos=pos; \
  2902. pkg_free((v)); /* rve_destroy(v_rve) would free everything*/ \
  2903. }else{\
  2904. /* unknown type or str => (int) $v */ \
  2905. (e)->op=RVE_##ctype##_OP; \
  2906. (e)->left.rve=(v); \
  2907. (e)->right.rve=0; \
  2908. }\
  2909. }while(0)
  2910. /* helper macro: replace in-place an int type rve with v (another rve).*/
  2911. #define replace_int_rve(e, v) replace_rve_type_cast(e, v, INT)
  2912. /* helper macro: replace in-place a str type rve with v (another rve).*/
  2913. #define replace_str_rve(e, v) replace_rve_type_cast(e, v, STR)
  2914. rv=0;
  2915. ret=0;
  2916. right=0;
  2917. dbg=1;
  2918. if (rve_is_constant(rve->right.rve)){
  2919. ct_rve=rve->right.rve;
  2920. v_rve=rve->left.rve;
  2921. right=1;
  2922. }else if (rve_is_constant(rve->left.rve)){
  2923. ct_rve=rve->left.rve;
  2924. v_rve=rve->right.rve;
  2925. right=0;
  2926. }else
  2927. return 0; /* op($v, $w) */
  2928. /* rval_expr_eval_new() instead of rval_expr_eval() to avoid
  2929. referencing a ct_rve->left.rval if ct_rve is a rval, which
  2930. would prevent rve_destroy(ct_rve) from working */
  2931. if ((rv=rval_expr_eval_new(0, 0, ct_rve))==0){
  2932. ERR("optimization failure, bad expression (%d,%d-%d,%d)\n",
  2933. ct_rve->fpos.s_line, ct_rve->fpos.s_col,
  2934. ct_rve->fpos.e_line, ct_rve->fpos.e_col);
  2935. goto error;
  2936. }
  2937. op=rve->op;
  2938. if (rv->type==RV_INT){
  2939. i=rv->v.l;
  2940. switch(op){
  2941. case RVE_MUL_OP:
  2942. if (i==0){
  2943. /* $v * 0 -> 0
  2944. * 0 * $v -> 0 */
  2945. if (rve_replace_with_ct_rv(rve, rv)<0)
  2946. goto error;
  2947. ret=1;
  2948. }else if (i==1){
  2949. /* $v * 1 -> (int)$v
  2950. * 1 * $v -> (int)$v */
  2951. rve_destroy(ct_rve);
  2952. replace_int_rve(rve, v_rve);
  2953. ret=1;
  2954. }
  2955. break;
  2956. case RVE_DIV_OP:
  2957. if (i==0){
  2958. if (ct_rve==rve->left.rve){
  2959. /* 0 / $v -> 0 */
  2960. if (rve_replace_with_ct_rv(rve, rv)<0)
  2961. goto error;
  2962. ret=1;
  2963. }else{
  2964. /* $v / 0 */
  2965. ERR("RVE divide by 0 at %d,%d\n",
  2966. ct_rve->fpos.s_line, ct_rve->fpos.s_col);
  2967. }
  2968. }else if (i==1){
  2969. if (ct_rve==rve->right.rve){
  2970. /* $v / 1 -> (int)$v */
  2971. rve_destroy(ct_rve);
  2972. replace_int_rve(rve, v_rve);
  2973. ret=1;
  2974. }
  2975. }
  2976. break;
  2977. case RVE_MOD_OP:
  2978. if (i==0){
  2979. if (ct_rve==rve->left.rve){
  2980. /* 0 % $v -> 0 */
  2981. if (rve_replace_with_ct_rv(rve, rv)<0)
  2982. goto error;
  2983. ret=1;
  2984. }else{
  2985. /* $v % 0 */
  2986. ERR("RVE modulo by 0 at %d,%d\n",
  2987. ct_rve->fpos.s_line, ct_rve->fpos.s_col);
  2988. }
  2989. }
  2990. /* $v % 1 -> 0 ? */
  2991. break;
  2992. case RVE_MINUS_OP:
  2993. if (i==0){
  2994. if (ct_rve==rve->right.rve){
  2995. /* $v - 0 -> $v */
  2996. rve_destroy(ct_rve);
  2997. replace_int_rve(rve, v_rve);
  2998. ret=1;
  2999. }
  3000. /* ? 0 - $v -> -($v) ? */
  3001. }
  3002. break;
  3003. case RVE_BAND_OP:
  3004. if (i==0){
  3005. /* $v & 0 -> 0
  3006. * 0 & $v -> 0 */
  3007. if (rve_replace_with_ct_rv(rve, rv)<0)
  3008. goto error;
  3009. ret=1;
  3010. }
  3011. /* no 0xffffff optimization for now (haven't decided on
  3012. the number of bits ) */
  3013. break;
  3014. case RVE_BOR_OP:
  3015. if (i==0){
  3016. /* $v | 0 -> (int)$v
  3017. * 0 | $v -> (int)$v */
  3018. rve_destroy(ct_rve);
  3019. replace_int_rve(rve, v_rve);
  3020. ret=1;
  3021. }
  3022. break;
  3023. case RVE_LAND_OP:
  3024. if (i==0){
  3025. /* $v && 0 -> 0
  3026. * 0 && $v -> 0 */
  3027. if (rve_replace_with_ct_rv(rve, rv)<0)
  3028. goto error;
  3029. ret=1;
  3030. }else if (i==1){
  3031. /* $v && 1 -> (int)$v
  3032. * 1 && $v -> (int)$v */
  3033. rve_destroy(ct_rve);
  3034. replace_int_rve(rve, v_rve);
  3035. ret=1;
  3036. }
  3037. break;
  3038. case RVE_LOR_OP:
  3039. if (i==1){
  3040. /* $v || 1 -> 1
  3041. * 1 || $v -> 1 */
  3042. if (rve_replace_with_ct_rv(rve, rv)<0)
  3043. goto error;
  3044. ret=1;
  3045. }else if (i==0){
  3046. /* $v || 0 -> (int)$v
  3047. * 0 && $v -> (int)$v */
  3048. rve_destroy(ct_rve);
  3049. replace_int_rve(rve, v_rve);
  3050. ret=1;
  3051. }
  3052. break;
  3053. case RVE_PLUS_OP:
  3054. case RVE_IPLUS_OP:
  3055. /* we must make sure that this is an int PLUS
  3056. (because "foo"+0 is valid => "foo0") =>
  3057. check if it's an IPLUS or the result is an integer
  3058. (which generally means unoptimized <int> + <something>).
  3059. */
  3060. if ((i==0) && ((op==RVE_IPLUS_OP) || (rve_type==RV_INT))){
  3061. /* $v + 0 -> (int)$v
  3062. * 0 + $v -> (int)$v */
  3063. rve_destroy(ct_rve);
  3064. replace_int_rve(rve, v_rve);
  3065. ret=1;
  3066. }
  3067. break;
  3068. default:
  3069. /* do nothing */
  3070. break;
  3071. }
  3072. /* debugging messages */
  3073. if (ret==1 && dbg){
  3074. if (right){
  3075. if (rve->op==RVE_RVAL_OP){
  3076. if (rve->left.rval.type==RV_INT)
  3077. DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
  3078. " op%d($v, %d) -> %d\n",
  3079. rve->fpos.s_line, rve->fpos.s_col,
  3080. rve->fpos.e_line, rve->fpos.e_col,
  3081. op, i, (int)rve->left.rval.v.l);
  3082. else
  3083. DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
  3084. " op%d($v, %d) -> $v (rval)\n",
  3085. rve->fpos.s_line, rve->fpos.s_col,
  3086. rve->fpos.e_line, rve->fpos.e_col,
  3087. op, i);
  3088. }else if (rve->op==RVE_INT_OP){
  3089. if (rve->left.rve->op==RVE_RVAL_OP &&
  3090. rve->left.rve->left.rval.type==RV_INT)
  3091. DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
  3092. " op%d($v, %d) -> (int)%d\n",
  3093. rve->fpos.s_line, rve->fpos.s_col,
  3094. rve->fpos.e_line, rve->fpos.e_col,
  3095. op, i, (int)rve->left.rve->left.rval.v.l);
  3096. else
  3097. DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
  3098. " op%d($v, %d) -> (int)$v\n",
  3099. rve->fpos.s_line, rve->fpos.s_col,
  3100. rve->fpos.e_line, rve->fpos.e_col,
  3101. op, i);
  3102. }else{
  3103. DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
  3104. " op%d($v, %d) -> $v\n",
  3105. rve->fpos.s_line, rve->fpos.s_col,
  3106. rve->fpos.e_line, rve->fpos.e_col,
  3107. op, i);
  3108. }
  3109. }else{
  3110. if (rve->op==RVE_RVAL_OP){
  3111. if (rve->left.rval.type==RV_INT)
  3112. DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
  3113. " op%d(%d, $v) -> %d\n",
  3114. rve->fpos.s_line, rve->fpos.s_col,
  3115. rve->fpos.e_line, rve->fpos.e_col,
  3116. op, i, (int)rve->left.rval.v.l);
  3117. else
  3118. DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
  3119. " op%d(%d, $v) -> $v (rval)\n",
  3120. rve->fpos.s_line, rve->fpos.s_col,
  3121. rve->fpos.e_line, rve->fpos.e_col,
  3122. op, i);
  3123. }else if (rve->op==RVE_INT_OP){
  3124. if (rve->left.rve->op==RVE_RVAL_OP &&
  3125. rve->left.rve->left.rval.type==RV_INT)
  3126. DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
  3127. " op%d(%d, $v) -> (int)%d\n",
  3128. rve->fpos.s_line, rve->fpos.s_col,
  3129. rve->fpos.e_line, rve->fpos.e_col,
  3130. op, i, (int)rve->left.rve->left.rval.v.l);
  3131. else
  3132. DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
  3133. " op%d(%d, $v) -> (int)$v\n",
  3134. rve->fpos.s_line, rve->fpos.s_col,
  3135. rve->fpos.e_line, rve->fpos.e_col,
  3136. op, i);
  3137. }else{
  3138. DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
  3139. " op%d(%d, $v) -> $v\n",
  3140. rve->fpos.s_line, rve->fpos.s_col,
  3141. rve->fpos.e_line, rve->fpos.e_col,
  3142. op, i);
  3143. }
  3144. }
  3145. }
  3146. }else if (rv->type==RV_STR){
  3147. switch(op){
  3148. case RVE_CONCAT_OP:
  3149. if (rv->v.s.len==0){
  3150. /* $v . "" -> (str)$v
  3151. "" . $v -> (str)$v */
  3152. rve_destroy(ct_rve);
  3153. replace_str_rve(rve, v_rve);
  3154. ret=1;
  3155. }
  3156. break;
  3157. case RVE_EQ_OP:
  3158. case RVE_STREQ_OP:
  3159. if (rv->v.s.len==0){
  3160. /* $v == "" -> strempty($v)
  3161. "" == $v -> strempty ($v) */
  3162. rve_destroy(ct_rve);
  3163. /* replace current expr. with strempty(rve) */
  3164. rve->op=RVE_STREMPTY_OP;
  3165. rve->left.rve=v_rve;
  3166. rve->right.rve=0;
  3167. ret=1;
  3168. if (dbg)
  3169. DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
  3170. " op%d($v, \"\") -> strempty($v)\n",
  3171. rve->fpos.s_line, rve->fpos.s_col,
  3172. rve->fpos.e_line, rve->fpos.e_col,
  3173. op);
  3174. dbg=0;
  3175. }
  3176. break;
  3177. default:
  3178. break;
  3179. }
  3180. /* no optimization for generic RVE_PLUS_OP for now, only for RVE_CONCAT_OP
  3181. (RVE_PLUS_OP should be converted to RVE_CONCAT_OP if it's supposed
  3182. to work on strings. If it's not converted/optimized it means it's type
  3183. can be determined only at runtime => we cannot optimize */
  3184. /* debugging messages */
  3185. if (ret==1 && dbg){
  3186. if (right){
  3187. if (rve->op==RVE_RVAL_OP){
  3188. if (rve->left.rval.type==RV_STR)
  3189. DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
  3190. " op%d($v, <string>) -> \"%s\"\n",
  3191. rve->fpos.s_line, rve->fpos.s_col,
  3192. rve->fpos.e_line, rve->fpos.e_col,
  3193. op, rve->left.rval.v.s.s);
  3194. else
  3195. DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
  3196. " op%d($v, <string>) -> $v (rval)\n",
  3197. rve->fpos.s_line, rve->fpos.s_col,
  3198. rve->fpos.e_line, rve->fpos.e_col, op);
  3199. }else if (rve->op==RVE_STR_OP){
  3200. if (rve->left.rve->op==RVE_RVAL_OP &&
  3201. rve->left.rve->left.rval.type==RV_STR)
  3202. DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
  3203. " op%d($v, <string>) -> (str)\"%s\"\n",
  3204. rve->fpos.s_line, rve->fpos.s_col,
  3205. rve->fpos.e_line, rve->fpos.e_col,
  3206. op, rve->left.rve->left.rval.v.s.s);
  3207. else
  3208. DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
  3209. " op%d($v, <string>) -> (str)$v\n",
  3210. rve->fpos.s_line, rve->fpos.s_col,
  3211. rve->fpos.e_line, rve->fpos.e_col, op);
  3212. }else{
  3213. DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
  3214. " op%d($v, <string>) -> $v\n",
  3215. rve->fpos.s_line, rve->fpos.s_col,
  3216. rve->fpos.e_line, rve->fpos.e_col, op);
  3217. }
  3218. }else{
  3219. if (rve->op==RVE_RVAL_OP){
  3220. if (rve->left.rval.type==RV_STR)
  3221. DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
  3222. " op%d(<string>, $v) -> \"%s\"\n",
  3223. rve->fpos.s_line, rve->fpos.s_col,
  3224. rve->fpos.e_line, rve->fpos.e_col,
  3225. op, rve->left.rval.v.s.s);
  3226. else
  3227. DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
  3228. " op%d(<string>, $v) -> $v (rval)\n",
  3229. rve->fpos.s_line, rve->fpos.s_col,
  3230. rve->fpos.e_line, rve->fpos.e_col, op);
  3231. }else if (rve->op==RVE_STR_OP){
  3232. if (rve->left.rve->op==RVE_RVAL_OP &&
  3233. rve->left.rve->left.rval.type==RV_STR)
  3234. DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
  3235. " op%d(<string>, $v) -> (str)\"%s\"\n",
  3236. rve->fpos.s_line, rve->fpos.s_col,
  3237. rve->fpos.e_line, rve->fpos.e_col,
  3238. op, rve->left.rve->left.rval.v.s.s);
  3239. else
  3240. DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
  3241. " op%d(<string>, $v) -> (str)$v\n",
  3242. rve->fpos.s_line, rve->fpos.s_col,
  3243. rve->fpos.e_line, rve->fpos.e_col, op);
  3244. }else{
  3245. DBG("FIXUP RVE: (%d,%d-%d,%d) optimized"
  3246. " op%d(<string>, $v) -> $v\n",
  3247. rve->fpos.s_line, rve->fpos.s_col,
  3248. rve->fpos.e_line, rve->fpos.e_col, op);
  3249. }
  3250. }
  3251. }
  3252. }
  3253. if (rv) rval_destroy(rv);
  3254. return ret;
  3255. error:
  3256. if (rv) rval_destroy(rv);
  3257. return -1;
  3258. }
  3259. /** tries to optimize a rval_expr. */
  3260. static int rve_optimize(struct rval_expr* rve)
  3261. {
  3262. int ret;
  3263. struct rvalue* rv;
  3264. struct rvalue* trv; /* used only for DBG() */
  3265. enum rval_expr_op op;
  3266. struct rval_expr tmp_rve;
  3267. enum rval_type type, l_type;
  3268. struct rval_expr* bad_rve;
  3269. enum rval_type bad_type, exp_type;
  3270. ret=0;
  3271. rv=0;
  3272. if (scr_opt_lev<1)
  3273. return 0;
  3274. if (rve->op == RVE_RVAL_OP) /* if rval, nothing to do */
  3275. return 0;
  3276. if (rve_is_constant(rve)){
  3277. if ((rv=rval_expr_eval_new(0, 0, rve))==0){
  3278. ERR("optimization failure, bad expression (%d,%d-%d,%d)\n",
  3279. rve->fpos.s_line, rve->fpos.s_col,
  3280. rve->fpos.e_line, rve->fpos.e_col);
  3281. goto error;
  3282. }
  3283. op=rve->op;
  3284. if (rve_replace_with_ct_rv(rve, rv)<0)
  3285. goto error;
  3286. rval_destroy(rv);
  3287. rv=0;
  3288. trv=&rve->left.rval;
  3289. if (trv->type==RV_INT)
  3290. DBG("FIXUP RVE (%d,%d-%d,%d): optimized constant int rve "
  3291. "(old op %d) to %d\n",
  3292. rve->fpos.s_line, rve->fpos.s_col,
  3293. rve->fpos.e_line, rve->fpos.e_col,
  3294. op, (int)trv->v.l);
  3295. else if (trv->type==RV_STR)
  3296. DBG("FIXUP RVE (%d,%d-%d,%d): optimized constant str rve "
  3297. "(old op %d) to \"%.*s\"\n",
  3298. rve->fpos.s_line, rve->fpos.s_col,
  3299. rve->fpos.e_line, rve->fpos.e_col,
  3300. op, trv->v.s.len, trv->v.s.s);
  3301. ret=1;
  3302. }else{
  3303. /* expression is not constant */
  3304. /* if unary => nothing to do */
  3305. if (rve_op_unary(rve->op))
  3306. return rve_optimize(rve->left.rve);
  3307. rve_optimize(rve->left.rve);
  3308. rve_optimize(rve->right.rve);
  3309. if (!rve_check_type(&type, rve, &bad_rve, &bad_type, &exp_type)){
  3310. ERR("optimization failure while optimizing %d,%d-%d,%d:"
  3311. " type mismatch in expression (%d,%d-%d,%d), "
  3312. "type %s, but expected %s\n",
  3313. rve->fpos.s_line, rve->fpos.s_col,
  3314. rve->fpos.e_line, rve->fpos.e_col,
  3315. bad_rve->fpos.s_line, bad_rve->fpos.s_col,
  3316. bad_rve->fpos.e_line, bad_rve->fpos.e_col,
  3317. rval_type_name(bad_type), rval_type_name(exp_type));
  3318. return 0;
  3319. }
  3320. /* $v - a => $v + (-a) (easier to optimize)*/
  3321. if ((rve->op==RVE_MINUS_OP) && (rve_is_constant(rve->right.rve))){
  3322. if ((rv=rval_expr_eval_new(0, 0, rve->right.rve))==0){
  3323. ERR("optimization failure, bad expression (%d,%d-%d,%d)\n",
  3324. rve->right.rve->fpos.s_line,
  3325. rve->right.rve->fpos.s_col,
  3326. rve->right.rve->fpos.e_line,
  3327. rve->right.rve->fpos.e_col);
  3328. goto error;
  3329. }
  3330. if (rv->type==RV_INT){
  3331. rv->v.l=-rv->v.l;
  3332. if (rve_replace_with_ct_rv(rve->right.rve, rv)<0)
  3333. goto error;
  3334. rve->op=RVE_IPLUS_OP;
  3335. DBG("FIXUP RVE (%d,%d-%d,%d): optimized $v - a into "
  3336. "$v + (%d)\n",
  3337. rve->fpos.s_line, rve->fpos.s_col,
  3338. rve->fpos.e_line, rve->fpos.e_col,
  3339. (int)rve->right.rve->left.rval.v.l);
  3340. }
  3341. rval_destroy(rv);
  3342. rv=0;
  3343. }
  3344. /* e1 PLUS_OP e2 -> change op if we know e1 basic type */
  3345. if (rve->op==RVE_PLUS_OP){
  3346. l_type=rve_guess_type(rve->left.rve);
  3347. if (l_type==RV_INT){
  3348. rve->op=RVE_IPLUS_OP;
  3349. DBG("FIXUP RVE (%d,%d-%d,%d): changed + into integer plus\n",
  3350. rve->fpos.s_line, rve->fpos.s_col,
  3351. rve->fpos.e_line, rve->fpos.e_col);
  3352. }else if (l_type==RV_STR){
  3353. rve->op=RVE_CONCAT_OP;
  3354. DBG("FIXUP RVE (%d,%d-%d,%d): changed + into string concat\n",
  3355. rve->fpos.s_line, rve->fpos.s_col,
  3356. rve->fpos.e_line, rve->fpos.e_col);
  3357. }
  3358. }
  3359. /* e1 EQ_OP e2 -> change op if we know e1 basic type
  3360. e1 DIFF_OP e2 -> change op if we know e2 basic type */
  3361. if (rve->op==RVE_EQ_OP || rve->op==RVE_DIFF_OP){
  3362. l_type=rve_guess_type(rve->left.rve);
  3363. if (l_type==RV_INT){
  3364. rve->op=(rve->op==RVE_EQ_OP)?RVE_IEQ_OP:RVE_IDIFF_OP;
  3365. DBG("FIXUP RVE (%d,%d-%d,%d): changed ==/!= into integer"
  3366. " ==/!=\n",
  3367. rve->fpos.s_line, rve->fpos.s_col,
  3368. rve->fpos.e_line, rve->fpos.e_col);
  3369. }else if (l_type==RV_STR){
  3370. rve->op=(rve->op==RVE_EQ_OP)?RVE_STREQ_OP:RVE_STRDIFF_OP;
  3371. DBG("FIXUP RVE (%d,%d-%d,%d): changed ==/!= into string"
  3372. " ==/!=\n",
  3373. rve->fpos.s_line, rve->fpos.s_col,
  3374. rve->fpos.e_line, rve->fpos.e_col);
  3375. }
  3376. }
  3377. /* $v * 0 => 0; $v * 1 => $v (for *, /, &, |, &&, ||, +, -) */
  3378. if (rve_opt_01(rve, type)==1){
  3379. /* success, rve was changed => return now
  3380. (since this is recursively invoked the "new" rve
  3381. is already optimized) */
  3382. ret=1;
  3383. goto end;
  3384. }
  3385. /* op(op($v, a), b) => op($v, op(a,b)) */
  3386. if (rve_is_constant(rve->right.rve)){
  3387. /* op1(op2(...), b) */
  3388. if ((rve->op==rve->left.rve->op) && rve_op_is_assoc(rve->op)){
  3389. /* op(op(...), b) */
  3390. if (rve_is_constant(rve->left.rve->right.rve)){
  3391. /* op(op($v, a), b) => op($v, op(a, b)) */
  3392. /* rv= op(a, b) */
  3393. tmp_rve.op=rve->op;
  3394. tmp_rve.left.rve=rve->left.rve->right.rve;
  3395. tmp_rve.right.rve=rve->right.rve;
  3396. /* hack for RVE_PLUS_OP which can work on string, ints
  3397. or a combination of them */
  3398. if ((rve->op==RVE_PLUS_OP) &&
  3399. (rve_guess_type(tmp_rve.left.rve)!=RV_STR)){
  3400. DBG("RVE optimization failed (%d,%d-%d,%d): cannot "
  3401. "optimize +(+($v, a), b) when typeof(a)==INT\n",
  3402. rve->fpos.s_line, rve->fpos.s_col,
  3403. rve->fpos.e_line, rve->fpos.e_col);
  3404. return 0;
  3405. }
  3406. if ((rv=rval_expr_eval_new(0, 0, &tmp_rve))==0){
  3407. ERR("optimization failure, bad expression\n");
  3408. goto error;
  3409. }
  3410. /* op($v, rv) */
  3411. if (rve_replace_with_ct_rv(rve->right.rve, rv)<0)
  3412. goto error;
  3413. rval_destroy(rv);
  3414. rv=0;
  3415. rve_destroy(tmp_rve.left.rve);
  3416. rve->left.rve=rve->left.rve->left.rve;
  3417. trv=&rve->right.rve->left.rval;
  3418. if (trv->type==RV_INT)
  3419. DBG("FIXUP RVE (%d,%d-%d,%d): optimized int rve: "
  3420. "op(op($v, a), b) with op($v, %d); op=%d\n",
  3421. rve->fpos.s_line, rve->fpos.s_col,
  3422. rve->fpos.e_line, rve->fpos.e_col,
  3423. (int)trv->v.l, rve->op);
  3424. else if (trv->type==RV_STR)
  3425. DBG("FIXUP RVE (%d,%d-%d,%d): optimized str rve "
  3426. "op(op($v, a), b) with op($v, \"%.*s\");"
  3427. " op=%d\n",
  3428. rve->fpos.s_line, rve->fpos.s_col,
  3429. rve->fpos.e_line, rve->fpos.e_col,
  3430. trv->v.s.len, trv->v.s.s, rve->op);
  3431. ret=1;
  3432. }else if (rve_is_constant(rve->left.rve->left.rve) &&
  3433. rve_op_is_commutative(rve->op)){
  3434. /* op(op(a, $v), b) => op(op(a, b), $v) */
  3435. /* rv= op(a, b) */
  3436. tmp_rve.op=rve->op;
  3437. tmp_rve.left.rve=rve->left.rve->left.rve;
  3438. tmp_rve.right.rve=rve->right.rve;
  3439. /* no need for the RVE_PLUS_OP hack, all the bad
  3440. cases are caught by rve_op_is_commutative()
  3441. (in this case type will be typeof(a)) => ok only if
  3442. typeof(a) is int) */
  3443. if ((rv=rval_expr_eval_new(0, 0, &tmp_rve))==0){
  3444. ERR("optimization failure, bad expression\n");
  3445. goto error;
  3446. }
  3447. /* op(rv, $v) */
  3448. rve_destroy(rve->right.rve);
  3449. rve->right.rve=rve->left.rve->right.rve;
  3450. rve->left.rve->right.rve=0;
  3451. if (rve_replace_with_ct_rv(rve->left.rve, rv)<0)
  3452. goto error;
  3453. rval_destroy(rv);
  3454. rv=0;
  3455. trv=&rve->left.rve->left.rval;
  3456. if (trv->type==RV_INT)
  3457. DBG("FIXUP RVE (%d,%d-%d,%d): optimized int rve: "
  3458. "op(op(a, $v), b) with op(%d, $v); op=%d\n",
  3459. rve->fpos.s_line, rve->fpos.s_col,
  3460. rve->fpos.e_line, rve->fpos.e_col,
  3461. (int)trv->v.l, rve->op);
  3462. else if (trv->type==RV_STR)
  3463. DBG("FIXUP RVE (%d,%d-%d,%d): optimized str rve "
  3464. "op(op(a, $v), b) with op(\"%.*s\", $v);"
  3465. " op=%d\n",
  3466. rve->fpos.s_line, rve->fpos.s_col,
  3467. rve->fpos.e_line, rve->fpos.e_col,
  3468. trv->v.s.len, trv->v.s.s, rve->op);
  3469. ret=1;
  3470. }
  3471. /* op(op($v, $w),b) => can't optimize */
  3472. }
  3473. /* op1(op2(...), b) and op1!=op2 or op is non assoc.
  3474. => can't optimize */
  3475. }else if (rve_is_constant(rve->left.rve)){
  3476. /* op1(a, op2(...)) */
  3477. if ((rve->op==rve->right.rve->op) && rve_op_is_assoc(rve->op)){
  3478. /* op(a, op(...)) */
  3479. if (rve_is_constant(rve->right.rve->right.rve) &&
  3480. rve_op_is_commutative(rve->op)){
  3481. /* op(a, op($v, b)) => op(op(a, b), $v) */
  3482. /* rv= op(a, b) */
  3483. tmp_rve.op=rve->op;
  3484. tmp_rve.left.rve=rve->left.rve;
  3485. tmp_rve.right.rve=rve->right.rve->right.rve;
  3486. /* no need for the RVE_PLUS_OP hack, all the bad
  3487. cases are caught by rve_op_is_commutative()
  3488. (in this case type will be typeof(a)) => ok only if
  3489. typeof(a) is int) */
  3490. if ((rv=rval_expr_eval_new(0, 0, &tmp_rve))==0){
  3491. ERR("optimization failure, bad expression\n");
  3492. goto error;
  3493. }
  3494. /* op(rv, $v) */
  3495. if (rve_replace_with_ct_rv(rve->left.rve, rv)<0)
  3496. goto error;
  3497. rval_destroy(rv);
  3498. rv=0;
  3499. rve_destroy(tmp_rve.right.rve);
  3500. rve->right.rve=rve->right.rve->left.rve;
  3501. trv=&rve->left.rve->left.rval;
  3502. if (trv->type==RV_INT)
  3503. DBG("FIXUP RVE (%d,%d-%d,%d): optimized int rve: "
  3504. "op(a, op($v, b)) with op(%d, $v); op=%d\n",
  3505. rve->fpos.s_line, rve->fpos.s_col,
  3506. rve->fpos.e_line, rve->fpos.e_col,
  3507. (int)trv->v.l, rve->op);
  3508. else if (trv->type==RV_STR)
  3509. DBG("FIXUP RVE (%d,%d-%d,%d): optimized str rve "
  3510. "op(a, op($v, b)) with op(\"%.*s\", $v);"
  3511. " op=%d\n",
  3512. rve->fpos.s_line, rve->fpos.s_col,
  3513. rve->fpos.e_line, rve->fpos.e_col,
  3514. trv->v.s.len, trv->v.s.s, rve->op);
  3515. ret=1;
  3516. }else if (rve_is_constant(rve->right.rve->left.rve)){
  3517. /* op(a, op(b, $v)) => op(op(a, b), $v) */
  3518. /* rv= op(a, b) */
  3519. tmp_rve.op=rve->op;
  3520. tmp_rve.left.rve=rve->left.rve;
  3521. tmp_rve.right.rve=rve->right.rve->left.rve;
  3522. /* hack for RVE_PLUS_OP which can work on string, ints
  3523. or a combination of them */
  3524. if ((rve->op==RVE_PLUS_OP) &&
  3525. (rve_guess_type(tmp_rve.left.rve) !=
  3526. rve_guess_type(tmp_rve.right.rve))){
  3527. DBG("RVE optimization failed (%d,%d-%d,%d): cannot "
  3528. "optimize +(a, +(b, $v)) when "
  3529. "typeof(a)!=typeof(b)\n",
  3530. rve->fpos.s_line, rve->fpos.s_col,
  3531. rve->fpos.e_line, rve->fpos.e_col);
  3532. return 0;
  3533. }
  3534. if ((rv=rval_expr_eval_new(0, 0, &tmp_rve))==0){
  3535. ERR("optimization failure, bad expression\n");
  3536. goto error;
  3537. }
  3538. /* op(rv, $v) */
  3539. if (rve_replace_with_ct_rv(rve->left.rve, rv)<0)
  3540. goto error;
  3541. rval_destroy(rv);
  3542. rv=0;
  3543. rve_destroy(tmp_rve.right.rve);
  3544. rve->right.rve=rve->right.rve->right.rve;
  3545. trv=&rve->left.rve->left.rval;
  3546. if (trv->type==RV_INT)
  3547. DBG("FIXUP RVE (%d,%d-%d,%d): optimized int rve: "
  3548. "op(a, op(b, $v)) with op(%d, $v); op=%d\n",
  3549. rve->fpos.s_line, rve->fpos.s_col,
  3550. rve->fpos.e_line, rve->fpos.e_col,
  3551. (int)trv->v.l, rve->op);
  3552. else if (trv->type==RV_STR)
  3553. DBG("FIXUP RVE (%d,%d-%d,%d): optimized str rve "
  3554. "op(a, op(b, $v)) with op(\"%.*s\", $v);"
  3555. " op=%d\n",
  3556. rve->fpos.s_line, rve->fpos.s_col,
  3557. rve->fpos.e_line, rve->fpos.e_col,
  3558. trv->v.s.len, trv->v.s.s, rve->op);
  3559. ret=1;
  3560. }
  3561. /* op(a, op($v, $w)) => can't optimize */
  3562. }
  3563. /* op1(a, op2(...)) and op1!=op2 or op is non assoc.
  3564. => can't optimize */
  3565. }
  3566. /* op(op($v,a), op($w,b)) => no optimizations for now (TODO) */
  3567. }
  3568. end:
  3569. return ret;
  3570. error:
  3571. if (rv) rval_destroy(rv);
  3572. return -1;
  3573. }
  3574. /** fix a rval_expr.
  3575. * fixes action, bexprs, resolves selects, pvars and
  3576. * optimizes simple sub expressions (e.g. 1+2).
  3577. *
  3578. * @param p - pointer to a rval_expr
  3579. * @return 0 on success, <0 on error (modifies also *(struct rval_expr*)p)
  3580. */
  3581. int fix_rval_expr(void* p)
  3582. {
  3583. struct rval_expr* rve;
  3584. int ret;
  3585. rve=(struct rval_expr*)p;
  3586. switch(rve->op){
  3587. case RVE_NONE_OP:
  3588. BUG("empty rval expr\n");
  3589. break;
  3590. case RVE_RVAL_OP:
  3591. return fix_rval(&rve->left.rval);
  3592. case RVE_UMINUS_OP: /* unary operators */
  3593. case RVE_BOOL_OP:
  3594. case RVE_LNOT_OP:
  3595. case RVE_BNOT_OP:
  3596. case RVE_STRLEN_OP:
  3597. case RVE_STREMPTY_OP:
  3598. case RVE_DEFINED_OP:
  3599. case RVE_NOTDEFINED_OP:
  3600. case RVE_INT_OP:
  3601. case RVE_STR_OP:
  3602. ret=fix_rval_expr((void*)rve->left.rve);
  3603. if (ret<0) return ret;
  3604. break;
  3605. case RVE_MUL_OP:
  3606. case RVE_DIV_OP:
  3607. case RVE_MOD_OP:
  3608. case RVE_MINUS_OP:
  3609. case RVE_BOR_OP:
  3610. case RVE_BAND_OP:
  3611. case RVE_BXOR_OP:
  3612. case RVE_BLSHIFT_OP:
  3613. case RVE_BRSHIFT_OP:
  3614. case RVE_LAND_OP:
  3615. case RVE_LOR_OP:
  3616. case RVE_GT_OP:
  3617. case RVE_GTE_OP:
  3618. case RVE_LT_OP:
  3619. case RVE_LTE_OP:
  3620. case RVE_PLUS_OP:
  3621. case RVE_IPLUS_OP:
  3622. case RVE_EQ_OP:
  3623. case RVE_DIFF_OP:
  3624. case RVE_IEQ_OP:
  3625. case RVE_IDIFF_OP:
  3626. case RVE_STREQ_OP:
  3627. case RVE_STRDIFF_OP:
  3628. case RVE_CONCAT_OP:
  3629. ret=fix_rval_expr((void*)rve->left.rve);
  3630. if (ret<0) return ret;
  3631. ret=fix_rval_expr((void*)rve->right.rve);
  3632. if (ret<0) return ret;
  3633. break;
  3634. case RVE_MATCH_OP:
  3635. ret=fix_match_rve(rve);
  3636. if (ret<0) return ret;
  3637. break;
  3638. default:
  3639. BUG("unsupported op type %d\n", rve->op);
  3640. }
  3641. /* try to optimize */
  3642. rve_optimize(rve);
  3643. return 0;
  3644. }