rvalue.c 97 KB

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