zend_operators.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2018 Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Andi Gutmans <andi@php.net> |
  16. | Zeev Suraski <zeev@php.net> |
  17. | Dmitry Stogov <dmitry@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #include <ctype.h>
  21. #include "zend.h"
  22. #include "zend_operators.h"
  23. #include "zend_variables.h"
  24. #include "zend_globals.h"
  25. #include "zend_list.h"
  26. #include "zend_API.h"
  27. #include "zend_strtod.h"
  28. #include "zend_exceptions.h"
  29. #include "zend_closures.h"
  30. #if ZEND_USE_TOLOWER_L
  31. #include <locale.h>
  32. static _locale_t current_locale = NULL;
  33. /* this is true global! may lead to strange effects on ZTS, but so may setlocale() */
  34. #define zend_tolower(c) _tolower_l(c, current_locale)
  35. #else
  36. #define zend_tolower(c) tolower(c)
  37. #endif
  38. #define TYPE_PAIR(t1,t2) (((t1) << 4) | (t2))
  39. static const unsigned char tolower_map[256] = {
  40. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
  41. 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,
  42. 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
  43. 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,
  44. 0x40,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,
  45. 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x5b,0x5c,0x5d,0x5e,0x5f,
  46. 0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,
  47. 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f,
  48. 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,
  49. 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,
  50. 0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf,
  51. 0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,
  52. 0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,
  53. 0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf,
  54. 0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,0xee,0xef,
  55. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  56. };
  57. #define zend_tolower_ascii(c) (tolower_map[(unsigned char)(c)])
  58. /**
  59. * Functions using locale lowercase:
  60. zend_binary_strncasecmp_l
  61. zend_binary_strcasecmp_l
  62. zend_binary_zval_strcasecmp
  63. zend_binary_zval_strncasecmp
  64. string_compare_function_ex
  65. string_case_compare_function
  66. * Functions using ascii lowercase:
  67. zend_str_tolower_copy
  68. zend_str_tolower_dup
  69. zend_str_tolower
  70. zend_binary_strcasecmp
  71. zend_binary_strncasecmp
  72. */
  73. ZEND_API int ZEND_FASTCALL zend_atoi(const char *str, size_t str_len) /* {{{ */
  74. {
  75. int retval;
  76. if (!str_len) {
  77. str_len = strlen(str);
  78. }
  79. retval = ZEND_STRTOL(str, NULL, 0);
  80. if (str_len>0) {
  81. switch (str[str_len-1]) {
  82. case 'g':
  83. case 'G':
  84. retval *= 1024;
  85. /* break intentionally missing */
  86. case 'm':
  87. case 'M':
  88. retval *= 1024;
  89. /* break intentionally missing */
  90. case 'k':
  91. case 'K':
  92. retval *= 1024;
  93. break;
  94. }
  95. }
  96. return retval;
  97. }
  98. /* }}} */
  99. ZEND_API zend_long ZEND_FASTCALL zend_atol(const char *str, size_t str_len) /* {{{ */
  100. {
  101. zend_long retval;
  102. if (!str_len) {
  103. str_len = strlen(str);
  104. }
  105. retval = ZEND_STRTOL(str, NULL, 0);
  106. if (str_len>0) {
  107. switch (str[str_len-1]) {
  108. case 'g':
  109. case 'G':
  110. retval *= 1024;
  111. /* break intentionally missing */
  112. case 'm':
  113. case 'M':
  114. retval *= 1024;
  115. /* break intentionally missing */
  116. case 'k':
  117. case 'K':
  118. retval *= 1024;
  119. break;
  120. }
  121. }
  122. return retval;
  123. }
  124. /* }}} */
  125. /* {{{ convert_object_to_type: dst will be either ctype or UNDEF */
  126. #define convert_object_to_type(op, dst, ctype, conv_func) \
  127. ZVAL_UNDEF(dst); \
  128. if (Z_OBJ_HT_P(op)->cast_object) { \
  129. if (Z_OBJ_HT_P(op)->cast_object(op, dst, ctype) == FAILURE) { \
  130. zend_error(E_RECOVERABLE_ERROR, \
  131. "Object of class %s could not be converted to %s", ZSTR_VAL(Z_OBJCE_P(op)->name),\
  132. zend_get_type_by_const(ctype)); \
  133. } \
  134. } else if (Z_OBJ_HT_P(op)->get) { \
  135. zval *newop = Z_OBJ_HT_P(op)->get(op, dst); \
  136. if (Z_TYPE_P(newop) != IS_OBJECT) { \
  137. /* for safety - avoid loop */ \
  138. ZVAL_COPY_VALUE(dst, newop); \
  139. conv_func(dst); \
  140. } \
  141. }
  142. /* }}} */
  143. static void ZEND_FASTCALL _convert_scalar_to_number(zval *op, zend_bool silent, zend_bool check) /* {{{ */
  144. {
  145. try_again:
  146. switch (Z_TYPE_P(op)) {
  147. case IS_REFERENCE:
  148. zend_unwrap_reference(op);
  149. goto try_again;
  150. case IS_STRING:
  151. {
  152. zend_string *str;
  153. str = Z_STR_P(op);
  154. if ((Z_TYPE_INFO_P(op)=is_numeric_string(ZSTR_VAL(str), ZSTR_LEN(str), &Z_LVAL_P(op), &Z_DVAL_P(op), silent ? 1 : -1)) == 0) {
  155. ZVAL_LONG(op, 0);
  156. if (!silent) {
  157. zend_error(E_WARNING, "A non-numeric value encountered");
  158. }
  159. }
  160. zend_string_release_ex(str, 0);
  161. break;
  162. }
  163. case IS_NULL:
  164. case IS_FALSE:
  165. ZVAL_LONG(op, 0);
  166. break;
  167. case IS_TRUE:
  168. ZVAL_LONG(op, 1);
  169. break;
  170. case IS_RESOURCE:
  171. {
  172. zend_long l = Z_RES_HANDLE_P(op);
  173. zval_ptr_dtor(op);
  174. ZVAL_LONG(op, l);
  175. }
  176. break;
  177. case IS_OBJECT:
  178. {
  179. zval dst;
  180. convert_object_to_type(op, &dst, _IS_NUMBER, convert_scalar_to_number);
  181. if (check && UNEXPECTED(EG(exception))) {
  182. return;
  183. }
  184. zval_ptr_dtor(op);
  185. if (Z_TYPE(dst) == IS_LONG || Z_TYPE(dst) == IS_DOUBLE) {
  186. ZVAL_COPY_VALUE(op, &dst);
  187. } else {
  188. ZVAL_LONG(op, 1);
  189. }
  190. }
  191. break;
  192. }
  193. }
  194. /* }}} */
  195. ZEND_API void ZEND_FASTCALL convert_scalar_to_number(zval *op) /* {{{ */
  196. {
  197. _convert_scalar_to_number(op, 1, 0);
  198. }
  199. /* }}} */
  200. /* {{{ _zendi_convert_scalar_to_number_ex */
  201. static zend_always_inline zval* _zendi_convert_scalar_to_number_ex(zval *op, zval *holder, zend_bool silent) /* {{{ */
  202. {
  203. switch (Z_TYPE_P(op)) {
  204. case IS_NULL:
  205. case IS_FALSE:
  206. ZVAL_LONG(holder, 0);
  207. return holder;
  208. case IS_TRUE:
  209. ZVAL_LONG(holder, 1);
  210. return holder;
  211. case IS_STRING:
  212. if ((Z_TYPE_INFO_P(holder) = is_numeric_string(Z_STRVAL_P(op), Z_STRLEN_P(op), &Z_LVAL_P(holder), &Z_DVAL_P(holder), silent ? 1 : -1)) == 0) {
  213. ZVAL_LONG(holder, 0);
  214. if (!silent) {
  215. zend_error(E_WARNING, "A non-numeric value encountered");
  216. }
  217. }
  218. return holder;
  219. case IS_RESOURCE:
  220. ZVAL_LONG(holder, Z_RES_HANDLE_P(op));
  221. return holder;
  222. case IS_OBJECT:
  223. convert_object_to_type(op, holder, _IS_NUMBER, convert_scalar_to_number);
  224. if (UNEXPECTED(EG(exception)) ||
  225. UNEXPECTED(Z_TYPE_P(holder) != IS_LONG && Z_TYPE_P(holder) != IS_DOUBLE)) {
  226. ZVAL_LONG(holder, 1);
  227. }
  228. return holder;
  229. case IS_LONG:
  230. case IS_DOUBLE:
  231. default:
  232. return op;
  233. }
  234. }
  235. /* }}} */
  236. /* {{{ _zendi_convert_scalar_to_number */
  237. static zend_never_inline zval* ZEND_FASTCALL _zendi_convert_scalar_to_number(zval *op, zval *holder) /* {{{ */
  238. {
  239. return _zendi_convert_scalar_to_number_ex(op, holder, 1);
  240. }
  241. /* }}} */
  242. /* {{{ _zendi_convert_scalar_to_number_noisy */
  243. static zend_never_inline zval* ZEND_FASTCALL _zendi_convert_scalar_to_number_noisy(zval *op, zval *holder) /* {{{ */
  244. {
  245. return _zendi_convert_scalar_to_number_ex(op, holder, 0);
  246. }
  247. /* }}} */
  248. #define zendi_convert_scalar_to_number(op, holder, result, silent) \
  249. ((Z_TYPE_P(op) == IS_LONG || Z_TYPE_P(op) == IS_DOUBLE) ? (op) : \
  250. (((op) == result) ? (_convert_scalar_to_number((op), silent, 1), (op)) : \
  251. (silent ? _zendi_convert_scalar_to_number((op), holder) : \
  252. _zendi_convert_scalar_to_number_noisy((op), holder))))
  253. #define convert_op1_op2_long(op1, op1_lval, op2, op2_lval, result, op, op_func) \
  254. do { \
  255. if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) { \
  256. if (Z_ISREF_P(op1)) { \
  257. op1 = Z_REFVAL_P(op1); \
  258. if (Z_TYPE_P(op1) == IS_LONG) { \
  259. op1_lval = Z_LVAL_P(op1); \
  260. break; \
  261. } \
  262. } \
  263. ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(op, op_func); \
  264. op1_lval = _zval_get_long_func_noisy(op1); \
  265. if (UNEXPECTED(EG(exception))) { \
  266. if (result != op1) { \
  267. ZVAL_UNDEF(result); \
  268. } \
  269. return FAILURE; \
  270. } \
  271. } else { \
  272. op1_lval = Z_LVAL_P(op1); \
  273. } \
  274. } while (0); \
  275. do { \
  276. if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) { \
  277. if (Z_ISREF_P(op2)) { \
  278. op2 = Z_REFVAL_P(op2); \
  279. if (Z_TYPE_P(op2) == IS_LONG) { \
  280. op2_lval = Z_LVAL_P(op2); \
  281. break; \
  282. } \
  283. } \
  284. ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(op); \
  285. op2_lval = _zval_get_long_func_noisy(op2); \
  286. if (UNEXPECTED(EG(exception))) { \
  287. if (result != op1) { \
  288. ZVAL_UNDEF(result); \
  289. } \
  290. return FAILURE; \
  291. } \
  292. } else { \
  293. op2_lval = Z_LVAL_P(op2); \
  294. } \
  295. } while (0);
  296. ZEND_API void ZEND_FASTCALL convert_to_long(zval *op) /* {{{ */
  297. {
  298. if (Z_TYPE_P(op) != IS_LONG) {
  299. convert_to_long_base(op, 10);
  300. }
  301. }
  302. /* }}} */
  303. ZEND_API void ZEND_FASTCALL convert_to_long_base(zval *op, int base) /* {{{ */
  304. {
  305. zend_long tmp;
  306. try_again:
  307. switch (Z_TYPE_P(op)) {
  308. case IS_NULL:
  309. case IS_FALSE:
  310. ZVAL_LONG(op, 0);
  311. break;
  312. case IS_TRUE:
  313. ZVAL_LONG(op, 1);
  314. break;
  315. case IS_RESOURCE:
  316. tmp = Z_RES_HANDLE_P(op);
  317. zval_ptr_dtor(op);
  318. ZVAL_LONG(op, tmp);
  319. break;
  320. case IS_LONG:
  321. break;
  322. case IS_DOUBLE:
  323. ZVAL_LONG(op, zend_dval_to_lval(Z_DVAL_P(op)));
  324. break;
  325. case IS_STRING:
  326. {
  327. zend_string *str = Z_STR_P(op);
  328. if (base == 10) {
  329. ZVAL_LONG(op, zval_get_long(op));
  330. } else {
  331. ZVAL_LONG(op, ZEND_STRTOL(ZSTR_VAL(str), NULL, base));
  332. }
  333. zend_string_release_ex(str, 0);
  334. }
  335. break;
  336. case IS_ARRAY:
  337. tmp = (zend_hash_num_elements(Z_ARRVAL_P(op))?1:0);
  338. zval_ptr_dtor(op);
  339. ZVAL_LONG(op, tmp);
  340. break;
  341. case IS_OBJECT:
  342. {
  343. zval dst;
  344. convert_object_to_type(op, &dst, IS_LONG, convert_to_long);
  345. zval_ptr_dtor(op);
  346. if (Z_TYPE(dst) == IS_LONG) {
  347. ZVAL_LONG(op, Z_LVAL(dst));
  348. } else {
  349. ZVAL_LONG(op, 1);
  350. }
  351. return;
  352. }
  353. case IS_REFERENCE:
  354. zend_unwrap_reference(op);
  355. goto try_again;
  356. EMPTY_SWITCH_DEFAULT_CASE()
  357. }
  358. }
  359. /* }}} */
  360. ZEND_API void ZEND_FASTCALL convert_to_double(zval *op) /* {{{ */
  361. {
  362. double tmp;
  363. try_again:
  364. switch (Z_TYPE_P(op)) {
  365. case IS_NULL:
  366. case IS_FALSE:
  367. ZVAL_DOUBLE(op, 0.0);
  368. break;
  369. case IS_TRUE:
  370. ZVAL_DOUBLE(op, 1.0);
  371. break;
  372. case IS_RESOURCE: {
  373. double d = (double) Z_RES_HANDLE_P(op);
  374. zval_ptr_dtor(op);
  375. ZVAL_DOUBLE(op, d);
  376. }
  377. break;
  378. case IS_LONG:
  379. ZVAL_DOUBLE(op, (double) Z_LVAL_P(op));
  380. break;
  381. case IS_DOUBLE:
  382. break;
  383. case IS_STRING:
  384. {
  385. zend_string *str = Z_STR_P(op);
  386. ZVAL_DOUBLE(op, zend_strtod(ZSTR_VAL(str), NULL));
  387. zend_string_release_ex(str, 0);
  388. }
  389. break;
  390. case IS_ARRAY:
  391. tmp = (zend_hash_num_elements(Z_ARRVAL_P(op))?1:0);
  392. zval_ptr_dtor(op);
  393. ZVAL_DOUBLE(op, tmp);
  394. break;
  395. case IS_OBJECT:
  396. {
  397. zval dst;
  398. convert_object_to_type(op, &dst, IS_DOUBLE, convert_to_double);
  399. zval_ptr_dtor(op);
  400. if (Z_TYPE(dst) == IS_DOUBLE) {
  401. ZVAL_DOUBLE(op, Z_DVAL(dst));
  402. } else {
  403. ZVAL_DOUBLE(op, 1.0);
  404. }
  405. break;
  406. }
  407. case IS_REFERENCE:
  408. zend_unwrap_reference(op);
  409. goto try_again;
  410. EMPTY_SWITCH_DEFAULT_CASE()
  411. }
  412. }
  413. /* }}} */
  414. ZEND_API void ZEND_FASTCALL convert_to_null(zval *op) /* {{{ */
  415. {
  416. zval_ptr_dtor(op);
  417. ZVAL_NULL(op);
  418. }
  419. /* }}} */
  420. ZEND_API void ZEND_FASTCALL convert_to_boolean(zval *op) /* {{{ */
  421. {
  422. int tmp;
  423. try_again:
  424. switch (Z_TYPE_P(op)) {
  425. case IS_FALSE:
  426. case IS_TRUE:
  427. break;
  428. case IS_NULL:
  429. ZVAL_FALSE(op);
  430. break;
  431. case IS_RESOURCE: {
  432. zend_long l = (Z_RES_HANDLE_P(op) ? 1 : 0);
  433. zval_ptr_dtor(op);
  434. ZVAL_BOOL(op, l);
  435. }
  436. break;
  437. case IS_LONG:
  438. ZVAL_BOOL(op, Z_LVAL_P(op) ? 1 : 0);
  439. break;
  440. case IS_DOUBLE:
  441. ZVAL_BOOL(op, Z_DVAL_P(op) ? 1 : 0);
  442. break;
  443. case IS_STRING:
  444. {
  445. zend_string *str = Z_STR_P(op);
  446. if (ZSTR_LEN(str) == 0
  447. || (ZSTR_LEN(str) == 1 && ZSTR_VAL(str)[0] == '0')) {
  448. ZVAL_FALSE(op);
  449. } else {
  450. ZVAL_TRUE(op);
  451. }
  452. zend_string_release_ex(str, 0);
  453. }
  454. break;
  455. case IS_ARRAY:
  456. tmp = (zend_hash_num_elements(Z_ARRVAL_P(op))?1:0);
  457. zval_ptr_dtor(op);
  458. ZVAL_BOOL(op, tmp);
  459. break;
  460. case IS_OBJECT:
  461. {
  462. zval dst;
  463. convert_object_to_type(op, &dst, _IS_BOOL, convert_to_boolean);
  464. zval_ptr_dtor(op);
  465. if (Z_TYPE_INFO(dst) == IS_FALSE || Z_TYPE_INFO(dst) == IS_TRUE) {
  466. Z_TYPE_INFO_P(op) = Z_TYPE_INFO(dst);
  467. } else {
  468. ZVAL_TRUE(op);
  469. }
  470. break;
  471. }
  472. case IS_REFERENCE:
  473. zend_unwrap_reference(op);
  474. goto try_again;
  475. EMPTY_SWITCH_DEFAULT_CASE()
  476. }
  477. }
  478. /* }}} */
  479. ZEND_API void ZEND_FASTCALL _convert_to_cstring(zval *op) /* {{{ */
  480. {
  481. if (Z_TYPE_P(op) == IS_DOUBLE) {
  482. zend_string *str;
  483. double dval = Z_DVAL_P(op);
  484. str = zend_strpprintf_unchecked(0, "%.*H", (int) EG(precision), dval);
  485. ZVAL_NEW_STR(op, str);
  486. } else {
  487. _convert_to_string(op);
  488. }
  489. }
  490. /* }}} */
  491. ZEND_API void ZEND_FASTCALL _convert_to_string(zval *op) /* {{{ */
  492. {
  493. try_again:
  494. switch (Z_TYPE_P(op)) {
  495. case IS_UNDEF:
  496. case IS_NULL:
  497. case IS_FALSE: {
  498. ZVAL_EMPTY_STRING(op);
  499. break;
  500. }
  501. case IS_TRUE:
  502. ZVAL_INTERNED_STR(op, ZSTR_CHAR('1'));
  503. break;
  504. case IS_STRING:
  505. break;
  506. case IS_RESOURCE: {
  507. zend_string *str = zend_strpprintf(0, "Resource id #" ZEND_LONG_FMT, (zend_long)Z_RES_HANDLE_P(op));
  508. zval_ptr_dtor(op);
  509. ZVAL_NEW_STR(op, str);
  510. break;
  511. }
  512. case IS_LONG: {
  513. ZVAL_STR(op, zend_long_to_str(Z_LVAL_P(op)));
  514. break;
  515. }
  516. case IS_DOUBLE: {
  517. zend_string *str;
  518. double dval = Z_DVAL_P(op);
  519. str = zend_strpprintf(0, "%.*G", (int) EG(precision), dval);
  520. /* %G already handles removing trailing zeros from the fractional part, yay */
  521. ZVAL_NEW_STR(op, str);
  522. break;
  523. }
  524. case IS_ARRAY:
  525. zend_error(E_NOTICE, "Array to string conversion");
  526. zval_ptr_dtor(op);
  527. ZVAL_NEW_STR(op, zend_string_init("Array", sizeof("Array")-1, 0));
  528. break;
  529. case IS_OBJECT: {
  530. zval dst;
  531. convert_object_to_type(op, &dst, IS_STRING, convert_to_string);
  532. zval_ptr_dtor(op);
  533. if (Z_TYPE(dst) == IS_STRING) {
  534. ZVAL_COPY_VALUE(op, &dst);
  535. } else {
  536. ZVAL_NEW_STR(op, zend_string_init("Object", sizeof("Object")-1, 0));
  537. }
  538. break;
  539. }
  540. case IS_REFERENCE:
  541. zend_unwrap_reference(op);
  542. goto try_again;
  543. EMPTY_SWITCH_DEFAULT_CASE()
  544. }
  545. }
  546. /* }}} */
  547. static void convert_scalar_to_array(zval *op) /* {{{ */
  548. {
  549. HashTable *ht = zend_new_array(1);
  550. zend_hash_index_add_new(ht, 0, op);
  551. ZVAL_ARR(op, ht);
  552. }
  553. /* }}} */
  554. ZEND_API void ZEND_FASTCALL convert_to_array(zval *op) /* {{{ */
  555. {
  556. try_again:
  557. switch (Z_TYPE_P(op)) {
  558. case IS_ARRAY:
  559. break;
  560. /* OBJECTS_OPTIMIZE */
  561. case IS_OBJECT:
  562. if (Z_OBJCE_P(op) == zend_ce_closure) {
  563. convert_scalar_to_array(op);
  564. } else {
  565. if (Z_OBJ_HT_P(op)->get_properties) {
  566. HashTable *obj_ht = Z_OBJ_HT_P(op)->get_properties(op);
  567. if (obj_ht) {
  568. /* fast copy */
  569. obj_ht = zend_proptable_to_symtable(obj_ht,
  570. (Z_OBJCE_P(op)->default_properties_count ||
  571. Z_OBJ_P(op)->handlers != &std_object_handlers ||
  572. GC_IS_RECURSIVE(obj_ht)));
  573. zval_ptr_dtor(op);
  574. ZVAL_ARR(op, obj_ht);
  575. return;
  576. }
  577. } else {
  578. zval dst;
  579. convert_object_to_type(op, &dst, IS_ARRAY, convert_to_array);
  580. if (Z_TYPE(dst) == IS_ARRAY) {
  581. zval_ptr_dtor(op);
  582. ZVAL_COPY_VALUE(op, &dst);
  583. return;
  584. }
  585. }
  586. zval_ptr_dtor(op);
  587. /*ZVAL_EMPTY_ARRAY(op);*/
  588. array_init(op);
  589. }
  590. break;
  591. case IS_NULL:
  592. /*ZVAL_EMPTY_ARRAY(op);*/
  593. array_init(op);
  594. break;
  595. case IS_REFERENCE:
  596. zend_unwrap_reference(op);
  597. goto try_again;
  598. default:
  599. convert_scalar_to_array(op);
  600. break;
  601. }
  602. }
  603. /* }}} */
  604. ZEND_API void ZEND_FASTCALL convert_to_object(zval *op) /* {{{ */
  605. {
  606. try_again:
  607. switch (Z_TYPE_P(op)) {
  608. case IS_ARRAY:
  609. {
  610. HashTable *ht = zend_symtable_to_proptable(Z_ARR_P(op));
  611. zend_object *obj;
  612. if (GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) {
  613. /* TODO: try not to duplicate immutable arrays as well ??? */
  614. ht = zend_array_dup(ht);
  615. } else if (ht != Z_ARR_P(op)) {
  616. zval_ptr_dtor(op);
  617. } else {
  618. GC_DELREF(ht);
  619. }
  620. obj = zend_objects_new(zend_standard_class_def);
  621. obj->properties = ht;
  622. ZVAL_OBJ(op, obj);
  623. break;
  624. }
  625. case IS_OBJECT:
  626. break;
  627. case IS_NULL:
  628. object_init(op);
  629. break;
  630. case IS_REFERENCE:
  631. zend_unwrap_reference(op);
  632. goto try_again;
  633. default: {
  634. zval tmp;
  635. ZVAL_COPY_VALUE(&tmp, op);
  636. object_init(op);
  637. zend_hash_add_new(Z_OBJPROP_P(op), ZSTR_KNOWN(ZEND_STR_SCALAR), &tmp);
  638. break;
  639. }
  640. }
  641. }
  642. /* }}} */
  643. ZEND_API void multi_convert_to_long_ex(int argc, ...) /* {{{ */
  644. {
  645. zval *arg;
  646. va_list ap;
  647. va_start(ap, argc);
  648. while (argc--) {
  649. arg = va_arg(ap, zval *);
  650. convert_to_long_ex(arg);
  651. }
  652. va_end(ap);
  653. }
  654. /* }}} */
  655. ZEND_API void multi_convert_to_double_ex(int argc, ...) /* {{{ */
  656. {
  657. zval *arg;
  658. va_list ap;
  659. va_start(ap, argc);
  660. while (argc--) {
  661. arg = va_arg(ap, zval *);
  662. convert_to_double_ex(arg);
  663. }
  664. va_end(ap);
  665. }
  666. /* }}} */
  667. ZEND_API void multi_convert_to_string_ex(int argc, ...) /* {{{ */
  668. {
  669. zval *arg;
  670. va_list ap;
  671. va_start(ap, argc);
  672. while (argc--) {
  673. arg = va_arg(ap, zval *);
  674. convert_to_string_ex(arg);
  675. }
  676. va_end(ap);
  677. }
  678. /* }}} */
  679. static zend_always_inline zend_long ZEND_FASTCALL _zval_get_long_func_ex(zval *op, zend_bool silent) /* {{{ */
  680. {
  681. try_again:
  682. switch (Z_TYPE_P(op)) {
  683. case IS_UNDEF:
  684. case IS_NULL:
  685. case IS_FALSE:
  686. return 0;
  687. case IS_TRUE:
  688. return 1;
  689. case IS_RESOURCE:
  690. return Z_RES_HANDLE_P(op);
  691. case IS_LONG:
  692. return Z_LVAL_P(op);
  693. case IS_DOUBLE:
  694. return zend_dval_to_lval(Z_DVAL_P(op));
  695. case IS_STRING:
  696. {
  697. zend_uchar type;
  698. zend_long lval;
  699. double dval;
  700. if (0 == (type = is_numeric_string(Z_STRVAL_P(op), Z_STRLEN_P(op), &lval, &dval, silent ? 1 : -1))) {
  701. if (!silent) {
  702. zend_error(E_WARNING, "A non-numeric value encountered");
  703. }
  704. return 0;
  705. } else if (EXPECTED(type == IS_LONG)) {
  706. return lval;
  707. } else {
  708. /* Previously we used strtol here, not is_numeric_string,
  709. * and strtol gives you LONG_MAX/_MIN on overflow.
  710. * We use use saturating conversion to emulate strtol()'s
  711. * behaviour.
  712. */
  713. return zend_dval_to_lval_cap(dval);
  714. }
  715. }
  716. case IS_ARRAY:
  717. return zend_hash_num_elements(Z_ARRVAL_P(op)) ? 1 : 0;
  718. case IS_OBJECT:
  719. {
  720. zval dst;
  721. convert_object_to_type(op, &dst, IS_LONG, convert_to_long);
  722. if (Z_TYPE(dst) == IS_LONG) {
  723. return Z_LVAL(dst);
  724. } else {
  725. return 1;
  726. }
  727. }
  728. case IS_REFERENCE:
  729. op = Z_REFVAL_P(op);
  730. goto try_again;
  731. EMPTY_SWITCH_DEFAULT_CASE()
  732. }
  733. return 0;
  734. }
  735. /* }}} */
  736. ZEND_API zend_long ZEND_FASTCALL zval_get_long_func(zval *op) /* {{{ */
  737. {
  738. return _zval_get_long_func_ex(op, 1);
  739. }
  740. /* }}} */
  741. static zend_long ZEND_FASTCALL _zval_get_long_func_noisy(zval *op) /* {{{ */
  742. {
  743. return _zval_get_long_func_ex(op, 0);
  744. }
  745. /* }}} */
  746. ZEND_API double ZEND_FASTCALL zval_get_double_func(zval *op) /* {{{ */
  747. {
  748. try_again:
  749. switch (Z_TYPE_P(op)) {
  750. case IS_NULL:
  751. case IS_FALSE:
  752. return 0.0;
  753. case IS_TRUE:
  754. return 1.0;
  755. case IS_RESOURCE:
  756. return (double) Z_RES_HANDLE_P(op);
  757. case IS_LONG:
  758. return (double) Z_LVAL_P(op);
  759. case IS_DOUBLE:
  760. return Z_DVAL_P(op);
  761. case IS_STRING:
  762. return zend_strtod(Z_STRVAL_P(op), NULL);
  763. case IS_ARRAY:
  764. return zend_hash_num_elements(Z_ARRVAL_P(op)) ? 1.0 : 0.0;
  765. case IS_OBJECT:
  766. {
  767. zval dst;
  768. convert_object_to_type(op, &dst, IS_DOUBLE, convert_to_double);
  769. if (Z_TYPE(dst) == IS_DOUBLE) {
  770. return Z_DVAL(dst);
  771. } else {
  772. return 1.0;
  773. }
  774. }
  775. case IS_REFERENCE:
  776. op = Z_REFVAL_P(op);
  777. goto try_again;
  778. EMPTY_SWITCH_DEFAULT_CASE()
  779. }
  780. return 0.0;
  781. }
  782. /* }}} */
  783. ZEND_API zend_string* ZEND_FASTCALL zval_get_string_func(zval *op) /* {{{ */
  784. {
  785. try_again:
  786. switch (Z_TYPE_P(op)) {
  787. case IS_UNDEF:
  788. case IS_NULL:
  789. case IS_FALSE:
  790. return ZSTR_EMPTY_ALLOC();
  791. case IS_TRUE:
  792. return ZSTR_CHAR('1');
  793. case IS_RESOURCE: {
  794. return zend_strpprintf(0, "Resource id #" ZEND_LONG_FMT, (zend_long)Z_RES_HANDLE_P(op));
  795. }
  796. case IS_LONG: {
  797. return zend_long_to_str(Z_LVAL_P(op));
  798. }
  799. case IS_DOUBLE: {
  800. return zend_strpprintf(0, "%.*G", (int) EG(precision), Z_DVAL_P(op));
  801. }
  802. case IS_ARRAY:
  803. zend_error(E_NOTICE, "Array to string conversion");
  804. return zend_string_init("Array", sizeof("Array")-1, 0);
  805. case IS_OBJECT: {
  806. zval tmp;
  807. if (Z_OBJ_HT_P(op)->cast_object) {
  808. if (Z_OBJ_HT_P(op)->cast_object(op, &tmp, IS_STRING) == SUCCESS) {
  809. return Z_STR(tmp);
  810. }
  811. } else if (Z_OBJ_HT_P(op)->get) {
  812. zval *z = Z_OBJ_HT_P(op)->get(op, &tmp);
  813. if (Z_TYPE_P(z) != IS_OBJECT) {
  814. zend_string *str = zval_get_string(z);
  815. zval_ptr_dtor(z);
  816. return str;
  817. }
  818. zval_ptr_dtor(z);
  819. }
  820. zend_error(EG(exception) ? E_ERROR : E_RECOVERABLE_ERROR, "Object of class %s could not be converted to string", ZSTR_VAL(Z_OBJCE_P(op)->name));
  821. return ZSTR_EMPTY_ALLOC();
  822. }
  823. case IS_REFERENCE:
  824. op = Z_REFVAL_P(op);
  825. goto try_again;
  826. case IS_STRING:
  827. return zend_string_copy(Z_STR_P(op));
  828. EMPTY_SWITCH_DEFAULT_CASE()
  829. }
  830. return NULL;
  831. }
  832. /* }}} */
  833. static zend_never_inline void ZEND_FASTCALL add_function_array(zval *result, zval *op1, zval *op2) /* {{{ */
  834. {
  835. if (result == op1 && Z_ARR_P(op1) == Z_ARR_P(op2)) {
  836. /* $a += $a */
  837. return;
  838. }
  839. if (result != op1) {
  840. ZVAL_ARR(result, zend_array_dup(Z_ARR_P(op1)));
  841. } else {
  842. SEPARATE_ARRAY(result);
  843. }
  844. zend_hash_merge(Z_ARRVAL_P(result), Z_ARRVAL_P(op2), zval_add_ref, 0);
  845. }
  846. /* }}} */
  847. static zend_always_inline int add_function_fast(zval *result, zval *op1, zval *op2) /* {{{ */
  848. {
  849. zend_uchar type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
  850. if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
  851. fast_long_add_function(result, op1, op2);
  852. return SUCCESS;
  853. } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
  854. ZVAL_DOUBLE(result, Z_DVAL_P(op1) + Z_DVAL_P(op2));
  855. return SUCCESS;
  856. } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
  857. ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) + Z_DVAL_P(op2));
  858. return SUCCESS;
  859. } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
  860. ZVAL_DOUBLE(result, Z_DVAL_P(op1) + ((double)Z_LVAL_P(op2)));
  861. return SUCCESS;
  862. } else if (EXPECTED(type_pair == TYPE_PAIR(IS_ARRAY, IS_ARRAY))) {
  863. add_function_array(result, op1, op2);
  864. return SUCCESS;
  865. } else {
  866. return FAILURE;
  867. }
  868. } /* }}} */
  869. static zend_never_inline int ZEND_FASTCALL add_function_slow(zval *result, zval *op1, zval *op2) /* {{{ */
  870. {
  871. zval op1_copy, op2_copy;
  872. int converted = 0;
  873. while (1) {
  874. if (Z_ISREF_P(op1)) {
  875. op1 = Z_REFVAL_P(op1);
  876. } else if (Z_ISREF_P(op2)) {
  877. op2 = Z_REFVAL_P(op2);
  878. } else if (!converted) {
  879. ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_ADD, add_function);
  880. if (EXPECTED(op1 != op2)) {
  881. op1 = zendi_convert_scalar_to_number(op1, &op1_copy, result, 0);
  882. op2 = zendi_convert_scalar_to_number(op2, &op2_copy, result, 0);
  883. } else {
  884. op1 = zendi_convert_scalar_to_number(op1, &op1_copy, result, 0);
  885. op2 = op1;
  886. }
  887. if (EG(exception)) {
  888. if (result != op1) {
  889. ZVAL_UNDEF(result);
  890. }
  891. return FAILURE;
  892. }
  893. converted = 1;
  894. } else {
  895. if (result != op1) {
  896. ZVAL_UNDEF(result);
  897. }
  898. zend_throw_error(NULL, "Unsupported operand types");
  899. return FAILURE; /* unknown datatype */
  900. }
  901. if (add_function_fast(result, op1, op2) == SUCCESS) {
  902. return SUCCESS;
  903. }
  904. }
  905. } /* }}} */
  906. ZEND_API int ZEND_FASTCALL add_function(zval *result, zval *op1, zval *op2) /* {{{ */
  907. {
  908. if (add_function_fast(result, op1, op2) == SUCCESS) {
  909. return SUCCESS;
  910. } else {
  911. return add_function_slow(result, op1, op2);
  912. }
  913. }
  914. /* }}} */
  915. static zend_always_inline int sub_function_fast(zval *result, zval *op1, zval *op2) /* {{{ */
  916. {
  917. zend_uchar type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
  918. if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
  919. fast_long_sub_function(result, op1, op2);
  920. return SUCCESS;
  921. } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
  922. ZVAL_DOUBLE(result, Z_DVAL_P(op1) - Z_DVAL_P(op2));
  923. return SUCCESS;
  924. } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
  925. ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) - Z_DVAL_P(op2));
  926. return SUCCESS;
  927. } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
  928. ZVAL_DOUBLE(result, Z_DVAL_P(op1) - ((double)Z_LVAL_P(op2)));
  929. return SUCCESS;
  930. } else {
  931. return FAILURE;
  932. }
  933. }
  934. /* }}} */
  935. static zend_never_inline int ZEND_FASTCALL sub_function_slow(zval *result, zval *op1, zval *op2) /* {{{ */
  936. {
  937. zval op1_copy, op2_copy;
  938. int converted = 0;
  939. while (1) {
  940. if (Z_ISREF_P(op1)) {
  941. op1 = Z_REFVAL_P(op1);
  942. } else if (Z_ISREF_P(op2)) {
  943. op2 = Z_REFVAL_P(op2);
  944. } else if (!converted) {
  945. ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_SUB, sub_function);
  946. if (EXPECTED(op1 != op2)) {
  947. op1 = zendi_convert_scalar_to_number(op1, &op1_copy, result, 0);
  948. op2 = zendi_convert_scalar_to_number(op2, &op2_copy, result, 0);
  949. } else {
  950. op1 = zendi_convert_scalar_to_number(op1, &op1_copy, result, 0);
  951. op2 = op1;
  952. }
  953. if (EG(exception)) {
  954. if (result != op1) {
  955. ZVAL_UNDEF(result);
  956. }
  957. return FAILURE;
  958. }
  959. converted = 1;
  960. } else {
  961. if (result != op1) {
  962. ZVAL_UNDEF(result);
  963. }
  964. zend_throw_error(NULL, "Unsupported operand types");
  965. return FAILURE; /* unknown datatype */
  966. }
  967. if (sub_function_fast(result, op1, op2) == SUCCESS) {
  968. return SUCCESS;
  969. }
  970. }
  971. }
  972. /* }}} */
  973. ZEND_API int ZEND_FASTCALL sub_function(zval *result, zval *op1, zval *op2) /* {{{ */
  974. {
  975. if (sub_function_fast(result, op1, op2) == SUCCESS) {
  976. return SUCCESS;
  977. } else {
  978. return sub_function_slow(result, op1, op2);
  979. }
  980. }
  981. /* }}} */
  982. ZEND_API int ZEND_FASTCALL mul_function(zval *result, zval *op1, zval *op2) /* {{{ */
  983. {
  984. zval op1_copy, op2_copy;
  985. int converted = 0;
  986. while (1) {
  987. zend_uchar type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
  988. if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
  989. zend_long overflow;
  990. ZEND_SIGNED_MULTIPLY_LONG(Z_LVAL_P(op1),Z_LVAL_P(op2), Z_LVAL_P(result),Z_DVAL_P(result),overflow);
  991. Z_TYPE_INFO_P(result) = overflow ? IS_DOUBLE : IS_LONG;
  992. return SUCCESS;
  993. } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
  994. ZVAL_DOUBLE(result, Z_DVAL_P(op1) * Z_DVAL_P(op2));
  995. return SUCCESS;
  996. } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
  997. ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) * Z_DVAL_P(op2));
  998. return SUCCESS;
  999. } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
  1000. ZVAL_DOUBLE(result, Z_DVAL_P(op1) * ((double)Z_LVAL_P(op2)));
  1001. return SUCCESS;
  1002. } else {
  1003. if (Z_ISREF_P(op1)) {
  1004. op1 = Z_REFVAL_P(op1);
  1005. } else if (Z_ISREF_P(op2)) {
  1006. op2 = Z_REFVAL_P(op2);
  1007. } else if (!converted) {
  1008. ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_MUL, mul_function);
  1009. if (EXPECTED(op1 != op2)) {
  1010. op1 = zendi_convert_scalar_to_number(op1, &op1_copy, result, 0);
  1011. op2 = zendi_convert_scalar_to_number(op2, &op2_copy, result, 0);
  1012. } else {
  1013. op1 = zendi_convert_scalar_to_number(op1, &op1_copy, result, 0);
  1014. op2 = op1;
  1015. }
  1016. if (EG(exception)) {
  1017. if (result != op1) {
  1018. ZVAL_UNDEF(result);
  1019. }
  1020. return FAILURE;
  1021. }
  1022. converted = 1;
  1023. } else {
  1024. if (result != op1) {
  1025. ZVAL_UNDEF(result);
  1026. }
  1027. zend_throw_error(NULL, "Unsupported operand types");
  1028. return FAILURE; /* unknown datatype */
  1029. }
  1030. }
  1031. }
  1032. }
  1033. /* }}} */
  1034. ZEND_API int ZEND_FASTCALL pow_function(zval *result, zval *op1, zval *op2) /* {{{ */
  1035. {
  1036. zval op1_copy, op2_copy;
  1037. int converted = 0;
  1038. while (1) {
  1039. zend_uchar type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
  1040. if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
  1041. if (Z_LVAL_P(op2) >= 0) {
  1042. zend_long l1 = 1, l2 = Z_LVAL_P(op1), i = Z_LVAL_P(op2);
  1043. if (i == 0) {
  1044. ZVAL_LONG(result, 1L);
  1045. return SUCCESS;
  1046. } else if (l2 == 0) {
  1047. ZVAL_LONG(result, 0);
  1048. return SUCCESS;
  1049. }
  1050. while (i >= 1) {
  1051. zend_long overflow;
  1052. double dval = 0.0;
  1053. if (i % 2) {
  1054. --i;
  1055. ZEND_SIGNED_MULTIPLY_LONG(l1, l2, l1, dval, overflow);
  1056. if (overflow) {
  1057. ZVAL_DOUBLE(result, dval * pow(l2, i));
  1058. return SUCCESS;
  1059. }
  1060. } else {
  1061. i /= 2;
  1062. ZEND_SIGNED_MULTIPLY_LONG(l2, l2, l2, dval, overflow);
  1063. if (overflow) {
  1064. ZVAL_DOUBLE(result, (double)l1 * pow(dval, i));
  1065. return SUCCESS;
  1066. }
  1067. }
  1068. }
  1069. /* i == 0 */
  1070. ZVAL_LONG(result, l1);
  1071. } else {
  1072. ZVAL_DOUBLE(result, pow((double)Z_LVAL_P(op1), (double)Z_LVAL_P(op2)));
  1073. }
  1074. return SUCCESS;
  1075. } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
  1076. ZVAL_DOUBLE(result, pow(Z_DVAL_P(op1), Z_DVAL_P(op2)));
  1077. return SUCCESS;
  1078. } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
  1079. ZVAL_DOUBLE(result, pow((double)Z_LVAL_P(op1), Z_DVAL_P(op2)));
  1080. return SUCCESS;
  1081. } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
  1082. ZVAL_DOUBLE(result, pow(Z_DVAL_P(op1), (double)Z_LVAL_P(op2)));
  1083. return SUCCESS;
  1084. } else {
  1085. if (Z_ISREF_P(op1)) {
  1086. op1 = Z_REFVAL_P(op1);
  1087. } else if (Z_ISREF_P(op2)) {
  1088. op2 = Z_REFVAL_P(op2);
  1089. } else if (!converted) {
  1090. ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_POW, pow_function);
  1091. if (EXPECTED(op1 != op2)) {
  1092. if (Z_TYPE_P(op1) == IS_ARRAY) {
  1093. if (op1 == result) {
  1094. zval_ptr_dtor(result);
  1095. }
  1096. ZVAL_LONG(result, 0);
  1097. return SUCCESS;
  1098. } else {
  1099. op1 = zendi_convert_scalar_to_number(op1, &op1_copy, result, 0);
  1100. }
  1101. if (Z_TYPE_P(op2) == IS_ARRAY) {
  1102. if (op1 == result) {
  1103. zval_ptr_dtor(result);
  1104. }
  1105. ZVAL_LONG(result, 1L);
  1106. return SUCCESS;
  1107. } else {
  1108. op2 = zendi_convert_scalar_to_number(op2, &op2_copy, result, 0);
  1109. }
  1110. } else {
  1111. if (Z_TYPE_P(op1) == IS_ARRAY) {
  1112. if (op1 == result) {
  1113. zval_ptr_dtor(result);
  1114. }
  1115. ZVAL_LONG(result, 0);
  1116. return SUCCESS;
  1117. } else {
  1118. op1 = zendi_convert_scalar_to_number(op1, &op1_copy, result, 0);
  1119. }
  1120. op2 = op1;
  1121. }
  1122. if (EG(exception)) {
  1123. if (result != op1) {
  1124. ZVAL_UNDEF(result);
  1125. }
  1126. return FAILURE;
  1127. }
  1128. converted = 1;
  1129. } else {
  1130. if (result != op1) {
  1131. ZVAL_UNDEF(result);
  1132. }
  1133. zend_throw_error(NULL, "Unsupported operand types");
  1134. return FAILURE;
  1135. }
  1136. }
  1137. }
  1138. }
  1139. /* }}} */
  1140. ZEND_API int ZEND_FASTCALL div_function(zval *result, zval *op1, zval *op2) /* {{{ */
  1141. {
  1142. zval op1_copy, op2_copy;
  1143. int converted = 0;
  1144. while (1) {
  1145. zend_uchar type_pair = TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2));
  1146. if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_LONG))) {
  1147. if (Z_LVAL_P(op2) == 0) {
  1148. zend_error(E_WARNING, "Division by zero");
  1149. ZVAL_DOUBLE(result, ((double) Z_LVAL_P(op1) / (double) Z_LVAL_P(op2)));
  1150. return SUCCESS;
  1151. } else if (Z_LVAL_P(op2) == -1 && Z_LVAL_P(op1) == ZEND_LONG_MIN) {
  1152. /* Prevent overflow error/crash */
  1153. ZVAL_DOUBLE(result, (double) ZEND_LONG_MIN / -1);
  1154. return SUCCESS;
  1155. }
  1156. if (Z_LVAL_P(op1) % Z_LVAL_P(op2) == 0) { /* integer */
  1157. ZVAL_LONG(result, Z_LVAL_P(op1) / Z_LVAL_P(op2));
  1158. } else {
  1159. ZVAL_DOUBLE(result, ((double) Z_LVAL_P(op1)) / Z_LVAL_P(op2));
  1160. }
  1161. return SUCCESS;
  1162. } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_DOUBLE))) {
  1163. if (Z_DVAL_P(op2) == 0) {
  1164. zend_error(E_WARNING, "Division by zero");
  1165. }
  1166. ZVAL_DOUBLE(result, Z_DVAL_P(op1) / Z_DVAL_P(op2));
  1167. return SUCCESS;
  1168. } else if (EXPECTED(type_pair == TYPE_PAIR(IS_DOUBLE, IS_LONG))) {
  1169. if (Z_LVAL_P(op2) == 0) {
  1170. zend_error(E_WARNING, "Division by zero");
  1171. }
  1172. ZVAL_DOUBLE(result, Z_DVAL_P(op1) / (double)Z_LVAL_P(op2));
  1173. return SUCCESS;
  1174. } else if (EXPECTED(type_pair == TYPE_PAIR(IS_LONG, IS_DOUBLE))) {
  1175. if (Z_DVAL_P(op2) == 0) {
  1176. zend_error(E_WARNING, "Division by zero");
  1177. }
  1178. ZVAL_DOUBLE(result, (double)Z_LVAL_P(op1) / Z_DVAL_P(op2));
  1179. return SUCCESS;
  1180. } else {
  1181. if (Z_ISREF_P(op1)) {
  1182. op1 = Z_REFVAL_P(op1);
  1183. } else if (Z_ISREF_P(op2)) {
  1184. op2 = Z_REFVAL_P(op2);
  1185. } else if (!converted) {
  1186. ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_DIV, div_function);
  1187. if (EXPECTED(op1 != op2)) {
  1188. op1 = zendi_convert_scalar_to_number(op1, &op1_copy, result, 0);
  1189. op2 = zendi_convert_scalar_to_number(op2, &op2_copy, result, 0);
  1190. } else {
  1191. op1 = zendi_convert_scalar_to_number(op1, &op1_copy, result, 0);
  1192. op2 = op1;
  1193. }
  1194. if (EG(exception)) {
  1195. if (result != op1) {
  1196. ZVAL_UNDEF(result);
  1197. }
  1198. return FAILURE;
  1199. }
  1200. converted = 1;
  1201. } else {
  1202. if (result != op1) {
  1203. ZVAL_UNDEF(result);
  1204. }
  1205. zend_throw_error(NULL, "Unsupported operand types");
  1206. return FAILURE; /* unknown datatype */
  1207. }
  1208. }
  1209. }
  1210. }
  1211. /* }}} */
  1212. ZEND_API int ZEND_FASTCALL mod_function(zval *result, zval *op1, zval *op2) /* {{{ */
  1213. {
  1214. zend_long op1_lval, op2_lval;
  1215. convert_op1_op2_long(op1, op1_lval, op2, op2_lval, result, ZEND_MOD, mod_function);
  1216. if (op2_lval == 0) {
  1217. /* modulus by zero */
  1218. if (EG(current_execute_data) && !CG(in_compilation)) {
  1219. zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
  1220. } else {
  1221. zend_error_noreturn(E_ERROR, "Modulo by zero");
  1222. }
  1223. if (op1 != result) {
  1224. ZVAL_UNDEF(result);
  1225. }
  1226. return FAILURE;
  1227. }
  1228. if (op1 == result) {
  1229. zval_ptr_dtor(result);
  1230. }
  1231. if (op2_lval == -1) {
  1232. /* Prevent overflow error/crash if op1==LONG_MIN */
  1233. ZVAL_LONG(result, 0);
  1234. return SUCCESS;
  1235. }
  1236. ZVAL_LONG(result, op1_lval % op2_lval);
  1237. return SUCCESS;
  1238. }
  1239. /* }}} */
  1240. ZEND_API int ZEND_FASTCALL boolean_xor_function(zval *result, zval *op1, zval *op2) /* {{{ */
  1241. {
  1242. int op1_val, op2_val;
  1243. do {
  1244. if (Z_TYPE_P(op1) == IS_FALSE) {
  1245. op1_val = 0;
  1246. } else if (EXPECTED(Z_TYPE_P(op1) == IS_TRUE)) {
  1247. op1_val = 1;
  1248. } else {
  1249. if (Z_ISREF_P(op1)) {
  1250. op1 = Z_REFVAL_P(op1);
  1251. if (Z_TYPE_P(op1) == IS_FALSE) {
  1252. op1_val = 0;
  1253. break;
  1254. } else if (EXPECTED(Z_TYPE_P(op1) == IS_TRUE)) {
  1255. op1_val = 1;
  1256. break;
  1257. }
  1258. }
  1259. ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BOOL_XOR, boolean_xor_function);
  1260. op1_val = zval_is_true(op1);
  1261. }
  1262. } while (0);
  1263. do {
  1264. if (Z_TYPE_P(op2) == IS_FALSE) {
  1265. op2_val = 0;
  1266. } else if (EXPECTED(Z_TYPE_P(op2) == IS_TRUE)) {
  1267. op2_val = 1;
  1268. } else {
  1269. if (Z_ISREF_P(op2)) {
  1270. op2 = Z_REFVAL_P(op2);
  1271. if (Z_TYPE_P(op2) == IS_FALSE) {
  1272. op2_val = 0;
  1273. break;
  1274. } else if (EXPECTED(Z_TYPE_P(op2) == IS_TRUE)) {
  1275. op2_val = 1;
  1276. break;
  1277. }
  1278. }
  1279. ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BOOL_XOR);
  1280. op2_val = zval_is_true(op2);
  1281. }
  1282. } while (0);
  1283. ZVAL_BOOL(result, op1_val ^ op2_val);
  1284. return SUCCESS;
  1285. }
  1286. /* }}} */
  1287. ZEND_API int ZEND_FASTCALL boolean_not_function(zval *result, zval *op1) /* {{{ */
  1288. {
  1289. if (Z_TYPE_P(op1) < IS_TRUE) {
  1290. ZVAL_TRUE(result);
  1291. } else if (EXPECTED(Z_TYPE_P(op1) == IS_TRUE)) {
  1292. ZVAL_FALSE(result);
  1293. } else {
  1294. if (Z_ISREF_P(op1)) {
  1295. op1 = Z_REFVAL_P(op1);
  1296. if (Z_TYPE_P(op1) < IS_TRUE) {
  1297. ZVAL_TRUE(result);
  1298. return SUCCESS;
  1299. } else if (EXPECTED(Z_TYPE_P(op1) == IS_TRUE)) {
  1300. ZVAL_FALSE(result);
  1301. return SUCCESS;
  1302. }
  1303. }
  1304. ZEND_TRY_UNARY_OBJECT_OPERATION(ZEND_BOOL_NOT);
  1305. ZVAL_BOOL(result, !zval_is_true(op1));
  1306. }
  1307. return SUCCESS;
  1308. }
  1309. /* }}} */
  1310. ZEND_API int ZEND_FASTCALL bitwise_not_function(zval *result, zval *op1) /* {{{ */
  1311. {
  1312. try_again:
  1313. switch (Z_TYPE_P(op1)) {
  1314. case IS_LONG:
  1315. ZVAL_LONG(result, ~Z_LVAL_P(op1));
  1316. return SUCCESS;
  1317. case IS_DOUBLE:
  1318. ZVAL_LONG(result, ~zend_dval_to_lval(Z_DVAL_P(op1)));
  1319. return SUCCESS;
  1320. case IS_STRING: {
  1321. size_t i;
  1322. if (Z_STRLEN_P(op1) == 1) {
  1323. zend_uchar not = (zend_uchar) ~*Z_STRVAL_P(op1);
  1324. ZVAL_INTERNED_STR(result, ZSTR_CHAR(not));
  1325. } else {
  1326. ZVAL_NEW_STR(result, zend_string_alloc(Z_STRLEN_P(op1), 0));
  1327. for (i = 0; i < Z_STRLEN_P(op1); i++) {
  1328. Z_STRVAL_P(result)[i] = ~Z_STRVAL_P(op1)[i];
  1329. }
  1330. Z_STRVAL_P(result)[i] = 0;
  1331. }
  1332. return SUCCESS;
  1333. }
  1334. case IS_REFERENCE:
  1335. op1 = Z_REFVAL_P(op1);
  1336. goto try_again;
  1337. default:
  1338. ZEND_TRY_UNARY_OBJECT_OPERATION(ZEND_BW_NOT);
  1339. if (result != op1) {
  1340. ZVAL_UNDEF(result);
  1341. }
  1342. zend_throw_error(NULL, "Unsupported operand types");
  1343. return FAILURE;
  1344. }
  1345. }
  1346. /* }}} */
  1347. ZEND_API int ZEND_FASTCALL bitwise_or_function(zval *result, zval *op1, zval *op2) /* {{{ */
  1348. {
  1349. zend_long op1_lval, op2_lval;
  1350. if (EXPECTED(Z_TYPE_P(op1) == IS_LONG) && EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
  1351. ZVAL_LONG(result, Z_LVAL_P(op1) | Z_LVAL_P(op2));
  1352. return SUCCESS;
  1353. }
  1354. ZVAL_DEREF(op1);
  1355. ZVAL_DEREF(op2);
  1356. if (Z_TYPE_P(op1) == IS_STRING && EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
  1357. zval *longer, *shorter;
  1358. zend_string *str;
  1359. size_t i;
  1360. if (EXPECTED(Z_STRLEN_P(op1) >= Z_STRLEN_P(op2))) {
  1361. if (EXPECTED(Z_STRLEN_P(op1) == Z_STRLEN_P(op2)) && Z_STRLEN_P(op1) == 1) {
  1362. zend_uchar or = (zend_uchar) (*Z_STRVAL_P(op1) | *Z_STRVAL_P(op2));
  1363. if (result==op1) {
  1364. zval_ptr_dtor_str(result);
  1365. }
  1366. ZVAL_INTERNED_STR(result, ZSTR_CHAR(or));
  1367. return SUCCESS;
  1368. }
  1369. longer = op1;
  1370. shorter = op2;
  1371. } else {
  1372. longer = op2;
  1373. shorter = op1;
  1374. }
  1375. str = zend_string_alloc(Z_STRLEN_P(longer), 0);
  1376. for (i = 0; i < Z_STRLEN_P(shorter); i++) {
  1377. ZSTR_VAL(str)[i] = Z_STRVAL_P(longer)[i] | Z_STRVAL_P(shorter)[i];
  1378. }
  1379. memcpy(ZSTR_VAL(str) + i, Z_STRVAL_P(longer) + i, Z_STRLEN_P(longer) - i + 1);
  1380. if (result==op1) {
  1381. zval_ptr_dtor_str(result);
  1382. }
  1383. ZVAL_NEW_STR(result, str);
  1384. return SUCCESS;
  1385. }
  1386. if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) {
  1387. ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BW_OR, bitwise_or_function);
  1388. op1_lval = _zval_get_long_func_noisy(op1);
  1389. if (UNEXPECTED(EG(exception))) {
  1390. if (result != op1) {
  1391. ZVAL_UNDEF(result);
  1392. }
  1393. return FAILURE;
  1394. }
  1395. } else {
  1396. op1_lval = Z_LVAL_P(op1);
  1397. }
  1398. if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) {
  1399. ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BW_OR);
  1400. op2_lval = _zval_get_long_func_noisy(op2);
  1401. if (UNEXPECTED(EG(exception))) {
  1402. if (result != op1) {
  1403. ZVAL_UNDEF(result);
  1404. }
  1405. return FAILURE;
  1406. }
  1407. } else {
  1408. op2_lval = Z_LVAL_P(op2);
  1409. }
  1410. if (op1 == result) {
  1411. zval_ptr_dtor(result);
  1412. }
  1413. ZVAL_LONG(result, op1_lval | op2_lval);
  1414. return SUCCESS;
  1415. }
  1416. /* }}} */
  1417. ZEND_API int ZEND_FASTCALL bitwise_and_function(zval *result, zval *op1, zval *op2) /* {{{ */
  1418. {
  1419. zend_long op1_lval, op2_lval;
  1420. if (EXPECTED(Z_TYPE_P(op1) == IS_LONG) && EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
  1421. ZVAL_LONG(result, Z_LVAL_P(op1) & Z_LVAL_P(op2));
  1422. return SUCCESS;
  1423. }
  1424. ZVAL_DEREF(op1);
  1425. ZVAL_DEREF(op2);
  1426. if (Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
  1427. zval *longer, *shorter;
  1428. zend_string *str;
  1429. size_t i;
  1430. if (EXPECTED(Z_STRLEN_P(op1) >= Z_STRLEN_P(op2))) {
  1431. if (EXPECTED(Z_STRLEN_P(op1) == Z_STRLEN_P(op2)) && Z_STRLEN_P(op1) == 1) {
  1432. zend_uchar and = (zend_uchar) (*Z_STRVAL_P(op1) & *Z_STRVAL_P(op2));
  1433. if (result==op1) {
  1434. zval_ptr_dtor_str(result);
  1435. }
  1436. ZVAL_INTERNED_STR(result, ZSTR_CHAR(and));
  1437. return SUCCESS;
  1438. }
  1439. longer = op1;
  1440. shorter = op2;
  1441. } else {
  1442. longer = op2;
  1443. shorter = op1;
  1444. }
  1445. str = zend_string_alloc(Z_STRLEN_P(shorter), 0);
  1446. for (i = 0; i < Z_STRLEN_P(shorter); i++) {
  1447. ZSTR_VAL(str)[i] = Z_STRVAL_P(shorter)[i] & Z_STRVAL_P(longer)[i];
  1448. }
  1449. ZSTR_VAL(str)[i] = 0;
  1450. if (result==op1) {
  1451. zval_ptr_dtor_str(result);
  1452. }
  1453. ZVAL_NEW_STR(result, str);
  1454. return SUCCESS;
  1455. }
  1456. if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) {
  1457. ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BW_AND, bitwise_and_function);
  1458. op1_lval = _zval_get_long_func_noisy(op1);
  1459. if (UNEXPECTED(EG(exception))) {
  1460. if (result != op1) {
  1461. ZVAL_UNDEF(result);
  1462. }
  1463. return FAILURE;
  1464. }
  1465. } else {
  1466. op1_lval = Z_LVAL_P(op1);
  1467. }
  1468. if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) {
  1469. ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BW_AND);
  1470. op2_lval = _zval_get_long_func_noisy(op2);
  1471. if (UNEXPECTED(EG(exception))) {
  1472. if (result != op1) {
  1473. ZVAL_UNDEF(result);
  1474. }
  1475. return FAILURE;
  1476. }
  1477. } else {
  1478. op2_lval = Z_LVAL_P(op2);
  1479. }
  1480. if (op1 == result) {
  1481. zval_ptr_dtor(result);
  1482. }
  1483. ZVAL_LONG(result, op1_lval & op2_lval);
  1484. return SUCCESS;
  1485. }
  1486. /* }}} */
  1487. ZEND_API int ZEND_FASTCALL bitwise_xor_function(zval *result, zval *op1, zval *op2) /* {{{ */
  1488. {
  1489. zend_long op1_lval, op2_lval;
  1490. if (EXPECTED(Z_TYPE_P(op1) == IS_LONG) && EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
  1491. ZVAL_LONG(result, Z_LVAL_P(op1) ^ Z_LVAL_P(op2));
  1492. return SUCCESS;
  1493. }
  1494. ZVAL_DEREF(op1);
  1495. ZVAL_DEREF(op2);
  1496. if (Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
  1497. zval *longer, *shorter;
  1498. zend_string *str;
  1499. size_t i;
  1500. if (EXPECTED(Z_STRLEN_P(op1) >= Z_STRLEN_P(op2))) {
  1501. if (EXPECTED(Z_STRLEN_P(op1) == Z_STRLEN_P(op2)) && Z_STRLEN_P(op1) == 1) {
  1502. zend_uchar xor = (zend_uchar) (*Z_STRVAL_P(op1) ^ *Z_STRVAL_P(op2));
  1503. if (result==op1) {
  1504. zval_ptr_dtor_str(result);
  1505. }
  1506. ZVAL_INTERNED_STR(result, ZSTR_CHAR(xor));
  1507. return SUCCESS;
  1508. }
  1509. longer = op1;
  1510. shorter = op2;
  1511. } else {
  1512. longer = op2;
  1513. shorter = op1;
  1514. }
  1515. str = zend_string_alloc(Z_STRLEN_P(shorter), 0);
  1516. for (i = 0; i < Z_STRLEN_P(shorter); i++) {
  1517. ZSTR_VAL(str)[i] = Z_STRVAL_P(shorter)[i] ^ Z_STRVAL_P(longer)[i];
  1518. }
  1519. ZSTR_VAL(str)[i] = 0;
  1520. if (result==op1) {
  1521. zval_ptr_dtor_str(result);
  1522. }
  1523. ZVAL_NEW_STR(result, str);
  1524. return SUCCESS;
  1525. }
  1526. if (UNEXPECTED(Z_TYPE_P(op1) != IS_LONG)) {
  1527. ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(ZEND_BW_XOR, bitwise_xor_function);
  1528. op1_lval = _zval_get_long_func_noisy(op1);
  1529. if (UNEXPECTED(EG(exception))) {
  1530. if (result != op1) {
  1531. ZVAL_UNDEF(result);
  1532. }
  1533. return FAILURE;
  1534. }
  1535. } else {
  1536. op1_lval = Z_LVAL_P(op1);
  1537. }
  1538. if (UNEXPECTED(Z_TYPE_P(op2) != IS_LONG)) {
  1539. ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_BW_XOR);
  1540. op2_lval = _zval_get_long_func_noisy(op2);
  1541. if (UNEXPECTED(EG(exception))) {
  1542. if (result != op1) {
  1543. ZVAL_UNDEF(result);
  1544. }
  1545. return FAILURE;
  1546. }
  1547. } else {
  1548. op2_lval = Z_LVAL_P(op2);
  1549. }
  1550. if (op1 == result) {
  1551. zval_ptr_dtor(result);
  1552. }
  1553. ZVAL_LONG(result, op1_lval ^ op2_lval);
  1554. return SUCCESS;
  1555. }
  1556. /* }}} */
  1557. ZEND_API int ZEND_FASTCALL shift_left_function(zval *result, zval *op1, zval *op2) /* {{{ */
  1558. {
  1559. zend_long op1_lval, op2_lval;
  1560. convert_op1_op2_long(op1, op1_lval, op2, op2_lval, result, ZEND_SL, shift_left_function);
  1561. /* prevent wrapping quirkiness on some processors where << 64 + x == << x */
  1562. if (UNEXPECTED((zend_ulong)op2_lval >= SIZEOF_ZEND_LONG * 8)) {
  1563. if (EXPECTED(op2_lval > 0)) {
  1564. if (op1 == result) {
  1565. zval_ptr_dtor(result);
  1566. }
  1567. ZVAL_LONG(result, 0);
  1568. return SUCCESS;
  1569. } else {
  1570. if (EG(current_execute_data) && !CG(in_compilation)) {
  1571. zend_throw_exception_ex(zend_ce_arithmetic_error, 0, "Bit shift by negative number");
  1572. } else {
  1573. zend_error_noreturn(E_ERROR, "Bit shift by negative number");
  1574. }
  1575. if (op1 != result) {
  1576. ZVAL_UNDEF(result);
  1577. }
  1578. return FAILURE;
  1579. }
  1580. }
  1581. if (op1 == result) {
  1582. zval_ptr_dtor(result);
  1583. }
  1584. ZVAL_LONG(result, op1_lval << op2_lval);
  1585. return SUCCESS;
  1586. }
  1587. /* }}} */
  1588. ZEND_API int ZEND_FASTCALL shift_right_function(zval *result, zval *op1, zval *op2) /* {{{ */
  1589. {
  1590. zend_long op1_lval, op2_lval;
  1591. convert_op1_op2_long(op1, op1_lval, op2, op2_lval, result, ZEND_SR, shift_right_function);
  1592. /* prevent wrapping quirkiness on some processors where >> 64 + x == >> x */
  1593. if (UNEXPECTED((zend_ulong)op2_lval >= SIZEOF_ZEND_LONG * 8)) {
  1594. if (EXPECTED(op2_lval > 0)) {
  1595. if (op1 == result) {
  1596. zval_ptr_dtor(result);
  1597. }
  1598. ZVAL_LONG(result, (op1_lval < 0) ? -1 : 0);
  1599. return SUCCESS;
  1600. } else {
  1601. if (EG(current_execute_data) && !CG(in_compilation)) {
  1602. zend_throw_exception_ex(zend_ce_arithmetic_error, 0, "Bit shift by negative number");
  1603. } else {
  1604. zend_error_noreturn(E_ERROR, "Bit shift by negative number");
  1605. }
  1606. if (op1 != result) {
  1607. ZVAL_UNDEF(result);
  1608. }
  1609. return FAILURE;
  1610. }
  1611. }
  1612. if (op1 == result) {
  1613. zval_ptr_dtor(result);
  1614. }
  1615. ZVAL_LONG(result, op1_lval >> op2_lval);
  1616. return SUCCESS;
  1617. }
  1618. /* }}} */
  1619. ZEND_API int ZEND_FASTCALL concat_function(zval *result, zval *op1, zval *op2) /* {{{ */
  1620. {
  1621. zval *orig_op1 = op1;
  1622. zval op1_copy, op2_copy;
  1623. ZVAL_UNDEF(&op1_copy);
  1624. ZVAL_UNDEF(&op2_copy);
  1625. do {
  1626. if (UNEXPECTED(Z_TYPE_P(op1) != IS_STRING)) {
  1627. if (Z_ISREF_P(op1)) {
  1628. op1 = Z_REFVAL_P(op1);
  1629. if (Z_TYPE_P(op1) == IS_STRING) break;
  1630. }
  1631. ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_CONCAT, concat_function);
  1632. ZVAL_STR(&op1_copy, zval_get_string_func(op1));
  1633. if (UNEXPECTED(EG(exception))) {
  1634. zval_ptr_dtor_str(&op1_copy);
  1635. if (orig_op1 != result) {
  1636. ZVAL_UNDEF(result);
  1637. }
  1638. return FAILURE;
  1639. }
  1640. if (result == op1) {
  1641. if (UNEXPECTED(op1 == op2)) {
  1642. op2 = &op1_copy;
  1643. }
  1644. }
  1645. op1 = &op1_copy;
  1646. }
  1647. } while (0);
  1648. do {
  1649. if (UNEXPECTED(Z_TYPE_P(op2) != IS_STRING)) {
  1650. if (Z_ISREF_P(op2)) {
  1651. op2 = Z_REFVAL_P(op2);
  1652. if (Z_TYPE_P(op2) == IS_STRING) break;
  1653. }
  1654. ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(ZEND_CONCAT);
  1655. ZVAL_STR(&op2_copy, zval_get_string_func(op2));
  1656. if (UNEXPECTED(EG(exception))) {
  1657. zval_ptr_dtor_str(&op1_copy);
  1658. zval_ptr_dtor_str(&op2_copy);
  1659. if (orig_op1 != result) {
  1660. ZVAL_UNDEF(result);
  1661. }
  1662. return FAILURE;
  1663. }
  1664. op2 = &op2_copy;
  1665. }
  1666. } while (0);
  1667. if (UNEXPECTED(Z_STRLEN_P(op1) == 0)) {
  1668. if (EXPECTED(result != op2)) {
  1669. if (result == orig_op1) {
  1670. i_zval_ptr_dtor(result ZEND_FILE_LINE_CC);
  1671. }
  1672. ZVAL_COPY(result, op2);
  1673. }
  1674. } else if (UNEXPECTED(Z_STRLEN_P(op2) == 0)) {
  1675. if (EXPECTED(result != op1)) {
  1676. if (result == orig_op1) {
  1677. i_zval_ptr_dtor(result ZEND_FILE_LINE_CC);
  1678. }
  1679. ZVAL_COPY(result, op1);
  1680. }
  1681. } else {
  1682. size_t op1_len = Z_STRLEN_P(op1);
  1683. size_t op2_len = Z_STRLEN_P(op2);
  1684. size_t result_len = op1_len + op2_len;
  1685. zend_string *result_str;
  1686. if (UNEXPECTED(op1_len > SIZE_MAX - op2_len)) {
  1687. zend_throw_error(NULL, "String size overflow");
  1688. zval_ptr_dtor_str(&op1_copy);
  1689. zval_ptr_dtor_str(&op2_copy);
  1690. if (orig_op1 != result) {
  1691. ZVAL_UNDEF(result);
  1692. }
  1693. return FAILURE;
  1694. }
  1695. if (result == op1 && Z_REFCOUNTED_P(result)) {
  1696. /* special case, perform operations on result */
  1697. result_str = zend_string_extend(Z_STR_P(result), result_len, 0);
  1698. } else {
  1699. result_str = zend_string_alloc(result_len, 0);
  1700. memcpy(ZSTR_VAL(result_str), Z_STRVAL_P(op1), op1_len);
  1701. if (result == orig_op1) {
  1702. i_zval_ptr_dtor(result ZEND_FILE_LINE_CC);
  1703. }
  1704. }
  1705. /* This has to happen first to account for the cases where result == op1 == op2 and
  1706. * the realloc is done. In this case this line will also update Z_STRVAL_P(op2) to
  1707. * point to the new string. The first op2_len bytes of result will still be the same. */
  1708. ZVAL_NEW_STR(result, result_str);
  1709. memcpy(ZSTR_VAL(result_str) + op1_len, Z_STRVAL_P(op2), op2_len);
  1710. ZSTR_VAL(result_str)[result_len] = '\0';
  1711. }
  1712. zval_ptr_dtor_str(&op1_copy);
  1713. zval_ptr_dtor_str(&op2_copy);
  1714. return SUCCESS;
  1715. }
  1716. /* }}} */
  1717. ZEND_API int ZEND_FASTCALL string_compare_function_ex(zval *op1, zval *op2, zend_bool case_insensitive) /* {{{ */
  1718. {
  1719. zend_string *tmp_str1, *tmp_str2;
  1720. zend_string *str1 = zval_get_tmp_string(op1, &tmp_str1);
  1721. zend_string *str2 = zval_get_tmp_string(op2, &tmp_str2);
  1722. int ret;
  1723. if (case_insensitive) {
  1724. ret = zend_binary_strcasecmp_l(ZSTR_VAL(str1), ZSTR_LEN(str1), ZSTR_VAL(str2), ZSTR_LEN(str1));
  1725. } else {
  1726. ret = zend_binary_strcmp(ZSTR_VAL(str1), ZSTR_LEN(str1), ZSTR_VAL(str2), ZSTR_LEN(str2));
  1727. }
  1728. zend_tmp_string_release(tmp_str1);
  1729. zend_tmp_string_release(tmp_str2);
  1730. return ret;
  1731. }
  1732. /* }}} */
  1733. ZEND_API int ZEND_FASTCALL string_compare_function(zval *op1, zval *op2) /* {{{ */
  1734. {
  1735. if (EXPECTED(Z_TYPE_P(op1) == IS_STRING) &&
  1736. EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
  1737. if (Z_STR_P(op1) == Z_STR_P(op2)) {
  1738. return 0;
  1739. } else {
  1740. return zend_binary_strcmp(Z_STRVAL_P(op1), Z_STRLEN_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op2));
  1741. }
  1742. } else {
  1743. zend_string *tmp_str1, *tmp_str2;
  1744. zend_string *str1 = zval_get_tmp_string(op1, &tmp_str1);
  1745. zend_string *str2 = zval_get_tmp_string(op2, &tmp_str2);
  1746. int ret = zend_binary_strcmp(ZSTR_VAL(str1), ZSTR_LEN(str1), ZSTR_VAL(str2), ZSTR_LEN(str2));
  1747. zend_tmp_string_release(tmp_str1);
  1748. zend_tmp_string_release(tmp_str2);
  1749. return ret;
  1750. }
  1751. }
  1752. /* }}} */
  1753. ZEND_API int ZEND_FASTCALL string_case_compare_function(zval *op1, zval *op2) /* {{{ */
  1754. {
  1755. if (EXPECTED(Z_TYPE_P(op1) == IS_STRING) &&
  1756. EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
  1757. if (Z_STR_P(op1) == Z_STR_P(op2)) {
  1758. return 0;
  1759. } else {
  1760. return zend_binary_strcasecmp_l(Z_STRVAL_P(op1), Z_STRLEN_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op2));
  1761. }
  1762. } else {
  1763. zend_string *tmp_str1, *tmp_str2;
  1764. zend_string *str1 = zval_get_tmp_string(op1, &tmp_str1);
  1765. zend_string *str2 = zval_get_tmp_string(op2, &tmp_str2);
  1766. int ret = zend_binary_strcasecmp_l(ZSTR_VAL(str1), ZSTR_LEN(str1), ZSTR_VAL(str2), ZSTR_LEN(str1));
  1767. zend_tmp_string_release(tmp_str1);
  1768. zend_tmp_string_release(tmp_str2);
  1769. return ret;
  1770. }
  1771. }
  1772. /* }}} */
  1773. #if HAVE_STRCOLL
  1774. ZEND_API int ZEND_FASTCALL string_locale_compare_function(zval *op1, zval *op2) /* {{{ */
  1775. {
  1776. zend_string *tmp_str1, *tmp_str2;
  1777. zend_string *str1 = zval_get_tmp_string(op1, &tmp_str1);
  1778. zend_string *str2 = zval_get_tmp_string(op2, &tmp_str2);
  1779. int ret = strcoll(ZSTR_VAL(str1), ZSTR_VAL(str2));
  1780. zend_tmp_string_release(tmp_str1);
  1781. zend_tmp_string_release(tmp_str2);
  1782. return ret;
  1783. }
  1784. /* }}} */
  1785. #endif
  1786. ZEND_API int ZEND_FASTCALL numeric_compare_function(zval *op1, zval *op2) /* {{{ */
  1787. {
  1788. double d1, d2;
  1789. d1 = zval_get_double(op1);
  1790. d2 = zval_get_double(op2);
  1791. return ZEND_NORMALIZE_BOOL(d1 - d2);
  1792. }
  1793. /* }}} */
  1794. static zend_always_inline void zend_free_obj_get_result(zval *op) /* {{{ */
  1795. {
  1796. ZEND_ASSERT(!Z_REFCOUNTED_P(op) || Z_REFCOUNT_P(op) != 0);
  1797. zval_ptr_dtor(op);
  1798. }
  1799. /* }}} */
  1800. static void ZEND_FASTCALL convert_compare_result_to_long(zval *result) /* {{{ */
  1801. {
  1802. if (Z_TYPE_P(result) == IS_DOUBLE) {
  1803. ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(Z_DVAL_P(result)));
  1804. } else {
  1805. convert_to_long(result);
  1806. }
  1807. }
  1808. /* }}} */
  1809. ZEND_API int ZEND_FASTCALL compare_function(zval *result, zval *op1, zval *op2) /* {{{ */
  1810. {
  1811. int ret;
  1812. int converted = 0;
  1813. zval op1_copy, op2_copy;
  1814. zval *op_free, tmp_free;
  1815. while (1) {
  1816. switch (TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2))) {
  1817. case TYPE_PAIR(IS_LONG, IS_LONG):
  1818. ZVAL_LONG(result, Z_LVAL_P(op1)>Z_LVAL_P(op2)?1:(Z_LVAL_P(op1)<Z_LVAL_P(op2)?-1:0));
  1819. return SUCCESS;
  1820. case TYPE_PAIR(IS_DOUBLE, IS_LONG):
  1821. Z_DVAL_P(result) = Z_DVAL_P(op1) - (double)Z_LVAL_P(op2);
  1822. ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(Z_DVAL_P(result)));
  1823. return SUCCESS;
  1824. case TYPE_PAIR(IS_LONG, IS_DOUBLE):
  1825. Z_DVAL_P(result) = (double)Z_LVAL_P(op1) - Z_DVAL_P(op2);
  1826. ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(Z_DVAL_P(result)));
  1827. return SUCCESS;
  1828. case TYPE_PAIR(IS_DOUBLE, IS_DOUBLE):
  1829. if (Z_DVAL_P(op1) == Z_DVAL_P(op2)) {
  1830. ZVAL_LONG(result, 0);
  1831. } else {
  1832. Z_DVAL_P(result) = Z_DVAL_P(op1) - Z_DVAL_P(op2);
  1833. ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(Z_DVAL_P(result)));
  1834. }
  1835. return SUCCESS;
  1836. case TYPE_PAIR(IS_ARRAY, IS_ARRAY):
  1837. ZVAL_LONG(result, zend_compare_arrays(op1, op2));
  1838. return SUCCESS;
  1839. case TYPE_PAIR(IS_NULL, IS_NULL):
  1840. case TYPE_PAIR(IS_NULL, IS_FALSE):
  1841. case TYPE_PAIR(IS_FALSE, IS_NULL):
  1842. case TYPE_PAIR(IS_FALSE, IS_FALSE):
  1843. case TYPE_PAIR(IS_TRUE, IS_TRUE):
  1844. ZVAL_LONG(result, 0);
  1845. return SUCCESS;
  1846. case TYPE_PAIR(IS_NULL, IS_TRUE):
  1847. ZVAL_LONG(result, -1);
  1848. return SUCCESS;
  1849. case TYPE_PAIR(IS_TRUE, IS_NULL):
  1850. ZVAL_LONG(result, 1);
  1851. return SUCCESS;
  1852. case TYPE_PAIR(IS_STRING, IS_STRING):
  1853. if (Z_STR_P(op1) == Z_STR_P(op2)) {
  1854. ZVAL_LONG(result, 0);
  1855. return SUCCESS;
  1856. }
  1857. ZVAL_LONG(result, zendi_smart_strcmp(Z_STR_P(op1), Z_STR_P(op2)));
  1858. return SUCCESS;
  1859. case TYPE_PAIR(IS_NULL, IS_STRING):
  1860. ZVAL_LONG(result, Z_STRLEN_P(op2) == 0 ? 0 : -1);
  1861. return SUCCESS;
  1862. case TYPE_PAIR(IS_STRING, IS_NULL):
  1863. ZVAL_LONG(result, Z_STRLEN_P(op1) == 0 ? 0 : 1);
  1864. return SUCCESS;
  1865. case TYPE_PAIR(IS_OBJECT, IS_NULL):
  1866. ZVAL_LONG(result, 1);
  1867. return SUCCESS;
  1868. case TYPE_PAIR(IS_NULL, IS_OBJECT):
  1869. ZVAL_LONG(result, -1);
  1870. return SUCCESS;
  1871. default:
  1872. if (Z_ISREF_P(op1)) {
  1873. op1 = Z_REFVAL_P(op1);
  1874. continue;
  1875. } else if (Z_ISREF_P(op2)) {
  1876. op2 = Z_REFVAL_P(op2);
  1877. continue;
  1878. }
  1879. if (Z_TYPE_P(op1) == IS_OBJECT && Z_OBJ_HANDLER_P(op1, compare)) {
  1880. ret = Z_OBJ_HANDLER_P(op1, compare)(result, op1, op2);
  1881. if (UNEXPECTED(Z_TYPE_P(result) != IS_LONG)) {
  1882. convert_compare_result_to_long(result);
  1883. }
  1884. return ret;
  1885. } else if (Z_TYPE_P(op2) == IS_OBJECT && Z_OBJ_HANDLER_P(op2, compare)) {
  1886. ret = Z_OBJ_HANDLER_P(op2, compare)(result, op1, op2);
  1887. if (UNEXPECTED(Z_TYPE_P(result) != IS_LONG)) {
  1888. convert_compare_result_to_long(result);
  1889. }
  1890. return ret;
  1891. }
  1892. if (Z_TYPE_P(op1) == IS_OBJECT && Z_TYPE_P(op2) == IS_OBJECT) {
  1893. if (Z_OBJ_P(op1) == Z_OBJ_P(op2)) {
  1894. /* object handles are identical, apparently this is the same object */
  1895. ZVAL_LONG(result, 0);
  1896. return SUCCESS;
  1897. }
  1898. if (Z_OBJ_HANDLER_P(op1, compare_objects) == Z_OBJ_HANDLER_P(op2, compare_objects)) {
  1899. ZVAL_LONG(result, Z_OBJ_HANDLER_P(op1, compare_objects)(op1, op2));
  1900. return SUCCESS;
  1901. }
  1902. }
  1903. if (Z_TYPE_P(op1) == IS_OBJECT) {
  1904. if (Z_OBJ_HT_P(op1)->get) {
  1905. zval rv;
  1906. op_free = Z_OBJ_HT_P(op1)->get(op1, &rv);
  1907. ret = compare_function(result, op_free, op2);
  1908. zend_free_obj_get_result(op_free);
  1909. return ret;
  1910. } else if (Z_TYPE_P(op2) != IS_OBJECT && Z_OBJ_HT_P(op1)->cast_object) {
  1911. ZVAL_UNDEF(&tmp_free);
  1912. if (Z_OBJ_HT_P(op1)->cast_object(op1, &tmp_free, ((Z_TYPE_P(op2) == IS_FALSE || Z_TYPE_P(op2) == IS_TRUE) ? _IS_BOOL : Z_TYPE_P(op2))) == FAILURE) {
  1913. ZVAL_LONG(result, 1);
  1914. zend_free_obj_get_result(&tmp_free);
  1915. return SUCCESS;
  1916. }
  1917. ret = compare_function(result, &tmp_free, op2);
  1918. zend_free_obj_get_result(&tmp_free);
  1919. return ret;
  1920. }
  1921. }
  1922. if (Z_TYPE_P(op2) == IS_OBJECT) {
  1923. if (Z_OBJ_HT_P(op2)->get) {
  1924. zval rv;
  1925. op_free = Z_OBJ_HT_P(op2)->get(op2, &rv);
  1926. ret = compare_function(result, op1, op_free);
  1927. zend_free_obj_get_result(op_free);
  1928. return ret;
  1929. } else if (Z_TYPE_P(op1) != IS_OBJECT && Z_OBJ_HT_P(op2)->cast_object) {
  1930. ZVAL_UNDEF(&tmp_free);
  1931. if (Z_OBJ_HT_P(op2)->cast_object(op2, &tmp_free, ((Z_TYPE_P(op1) == IS_FALSE || Z_TYPE_P(op1) == IS_TRUE) ? _IS_BOOL : Z_TYPE_P(op1))) == FAILURE) {
  1932. ZVAL_LONG(result, -1);
  1933. zend_free_obj_get_result(&tmp_free);
  1934. return SUCCESS;
  1935. }
  1936. ret = compare_function(result, op1, &tmp_free);
  1937. zend_free_obj_get_result(&tmp_free);
  1938. return ret;
  1939. } else if (Z_TYPE_P(op1) == IS_OBJECT) {
  1940. ZVAL_LONG(result, 1);
  1941. return SUCCESS;
  1942. }
  1943. }
  1944. if (!converted) {
  1945. if (Z_TYPE_P(op1) == IS_NULL || Z_TYPE_P(op1) == IS_FALSE) {
  1946. ZVAL_LONG(result, zval_is_true(op2) ? -1 : 0);
  1947. return SUCCESS;
  1948. } else if (Z_TYPE_P(op2) == IS_NULL || Z_TYPE_P(op2) == IS_FALSE) {
  1949. ZVAL_LONG(result, zval_is_true(op1) ? 1 : 0);
  1950. return SUCCESS;
  1951. } else if (Z_TYPE_P(op1) == IS_TRUE) {
  1952. ZVAL_LONG(result, zval_is_true(op2) ? 0 : 1);
  1953. return SUCCESS;
  1954. } else if (Z_TYPE_P(op2) == IS_TRUE) {
  1955. ZVAL_LONG(result, zval_is_true(op1) ? 0 : -1);
  1956. return SUCCESS;
  1957. } else {
  1958. op1 = zendi_convert_scalar_to_number(op1, &op1_copy, result, 1);
  1959. op2 = zendi_convert_scalar_to_number(op2, &op2_copy, result, 1);
  1960. if (EG(exception)) {
  1961. if (result != op1) {
  1962. ZVAL_UNDEF(result);
  1963. }
  1964. return FAILURE;
  1965. }
  1966. converted = 1;
  1967. }
  1968. } else if (Z_TYPE_P(op1)==IS_ARRAY) {
  1969. ZVAL_LONG(result, 1);
  1970. return SUCCESS;
  1971. } else if (Z_TYPE_P(op2)==IS_ARRAY) {
  1972. ZVAL_LONG(result, -1);
  1973. return SUCCESS;
  1974. } else if (Z_TYPE_P(op1)==IS_OBJECT) {
  1975. ZVAL_LONG(result, 1);
  1976. return SUCCESS;
  1977. } else if (Z_TYPE_P(op2)==IS_OBJECT) {
  1978. ZVAL_LONG(result, -1);
  1979. return SUCCESS;
  1980. } else {
  1981. ZVAL_LONG(result, 0);
  1982. return FAILURE;
  1983. }
  1984. }
  1985. }
  1986. }
  1987. /* }}} */
  1988. static int hash_zval_identical_function(zval *z1, zval *z2) /* {{{ */
  1989. {
  1990. zval result;
  1991. /* is_identical_function() returns 1 in case of identity and 0 in case
  1992. * of a difference;
  1993. * whereas this comparison function is expected to return 0 on identity,
  1994. * and non zero otherwise.
  1995. */
  1996. ZVAL_DEREF(z1);
  1997. ZVAL_DEREF(z2);
  1998. if (is_identical_function(&result, z1, z2)==FAILURE) {
  1999. return 1;
  2000. }
  2001. return Z_TYPE(result) != IS_TRUE;
  2002. }
  2003. /* }}} */
  2004. ZEND_API int ZEND_FASTCALL zend_is_identical(zval *op1, zval *op2) /* {{{ */
  2005. {
  2006. if (Z_TYPE_P(op1) != Z_TYPE_P(op2)) {
  2007. return 0;
  2008. }
  2009. switch (Z_TYPE_P(op1)) {
  2010. case IS_NULL:
  2011. case IS_FALSE:
  2012. case IS_TRUE:
  2013. return 1;
  2014. case IS_LONG:
  2015. return (Z_LVAL_P(op1) == Z_LVAL_P(op2));
  2016. case IS_RESOURCE:
  2017. return (Z_RES_P(op1) == Z_RES_P(op2));
  2018. case IS_DOUBLE:
  2019. return (Z_DVAL_P(op1) == Z_DVAL_P(op2));
  2020. case IS_STRING:
  2021. return zend_string_equals(Z_STR_P(op1), Z_STR_P(op2));
  2022. case IS_ARRAY:
  2023. return (Z_ARRVAL_P(op1) == Z_ARRVAL_P(op2) ||
  2024. zend_hash_compare(Z_ARRVAL_P(op1), Z_ARRVAL_P(op2), (compare_func_t) hash_zval_identical_function, 1) == 0);
  2025. case IS_OBJECT:
  2026. return (Z_OBJ_P(op1) == Z_OBJ_P(op2));
  2027. default:
  2028. return 0;
  2029. }
  2030. }
  2031. /* }}} */
  2032. ZEND_API int ZEND_FASTCALL is_identical_function(zval *result, zval *op1, zval *op2) /* {{{ */
  2033. {
  2034. ZVAL_BOOL(result, zend_is_identical(op1, op2));
  2035. return SUCCESS;
  2036. }
  2037. /* }}} */
  2038. ZEND_API int ZEND_FASTCALL is_not_identical_function(zval *result, zval *op1, zval *op2) /* {{{ */
  2039. {
  2040. ZVAL_BOOL(result, !zend_is_identical(op1, op2));
  2041. return SUCCESS;
  2042. }
  2043. /* }}} */
  2044. ZEND_API int ZEND_FASTCALL is_equal_function(zval *result, zval *op1, zval *op2) /* {{{ */
  2045. {
  2046. if (compare_function(result, op1, op2) == FAILURE) {
  2047. return FAILURE;
  2048. }
  2049. ZVAL_BOOL(result, (Z_LVAL_P(result) == 0));
  2050. return SUCCESS;
  2051. }
  2052. /* }}} */
  2053. ZEND_API int ZEND_FASTCALL is_not_equal_function(zval *result, zval *op1, zval *op2) /* {{{ */
  2054. {
  2055. if (compare_function(result, op1, op2) == FAILURE) {
  2056. return FAILURE;
  2057. }
  2058. ZVAL_BOOL(result, (Z_LVAL_P(result) != 0));
  2059. return SUCCESS;
  2060. }
  2061. /* }}} */
  2062. ZEND_API int ZEND_FASTCALL is_smaller_function(zval *result, zval *op1, zval *op2) /* {{{ */
  2063. {
  2064. if (compare_function(result, op1, op2) == FAILURE) {
  2065. return FAILURE;
  2066. }
  2067. ZVAL_BOOL(result, (Z_LVAL_P(result) < 0));
  2068. return SUCCESS;
  2069. }
  2070. /* }}} */
  2071. ZEND_API int ZEND_FASTCALL is_smaller_or_equal_function(zval *result, zval *op1, zval *op2) /* {{{ */
  2072. {
  2073. if (compare_function(result, op1, op2) == FAILURE) {
  2074. return FAILURE;
  2075. }
  2076. ZVAL_BOOL(result, (Z_LVAL_P(result) <= 0));
  2077. return SUCCESS;
  2078. }
  2079. /* }}} */
  2080. static zend_bool ZEND_FASTCALL instanceof_interface_only(const zend_class_entry *instance_ce, const zend_class_entry *ce) /* {{{ */
  2081. {
  2082. uint32_t i;
  2083. for (i = 0; i < instance_ce->num_interfaces; i++) {
  2084. if (instanceof_interface_only(instance_ce->interfaces[i], ce)) {
  2085. return 1;
  2086. }
  2087. }
  2088. return 0;
  2089. }
  2090. /* }}} */
  2091. static zend_always_inline zend_bool instanceof_class(const zend_class_entry *instance_ce, const zend_class_entry *ce) /* {{{ */
  2092. {
  2093. while (instance_ce) {
  2094. if (instance_ce == ce) {
  2095. return 1;
  2096. }
  2097. instance_ce = instance_ce->parent;
  2098. }
  2099. return 0;
  2100. }
  2101. /* }}} */
  2102. static zend_bool ZEND_FASTCALL instanceof_interface(const zend_class_entry *instance_ce, const zend_class_entry *ce) /* {{{ */
  2103. {
  2104. uint32_t i;
  2105. for (i = 0; i < instance_ce->num_interfaces; i++) {
  2106. if (instanceof_interface(instance_ce->interfaces[i], ce)) {
  2107. return 1;
  2108. }
  2109. }
  2110. return instanceof_class(instance_ce, ce);
  2111. }
  2112. /* }}} */
  2113. ZEND_API zend_bool ZEND_FASTCALL instanceof_function_ex(const zend_class_entry *instance_ce, const zend_class_entry *ce, zend_bool interfaces_only) /* {{{ */
  2114. {
  2115. if (ce->ce_flags & ZEND_ACC_INTERFACE) {
  2116. if (!interfaces_only) {
  2117. if (instanceof_interface_only(instance_ce, ce)) {
  2118. return 1;
  2119. }
  2120. } else {
  2121. return instanceof_interface(instance_ce, ce);
  2122. }
  2123. }
  2124. if (!interfaces_only) {
  2125. return instanceof_class(instance_ce, ce);
  2126. }
  2127. return 0;
  2128. }
  2129. /* }}} */
  2130. ZEND_API zend_bool ZEND_FASTCALL instanceof_function(const zend_class_entry *instance_ce, const zend_class_entry *ce) /* {{{ */
  2131. {
  2132. if (ce->ce_flags & ZEND_ACC_INTERFACE) {
  2133. return instanceof_interface(instance_ce, ce);
  2134. } else {
  2135. return instanceof_class(instance_ce, ce);
  2136. }
  2137. }
  2138. /* }}} */
  2139. #define LOWER_CASE 1
  2140. #define UPPER_CASE 2
  2141. #define NUMERIC 3
  2142. static void ZEND_FASTCALL increment_string(zval *str) /* {{{ */
  2143. {
  2144. int carry=0;
  2145. size_t pos=Z_STRLEN_P(str)-1;
  2146. char *s;
  2147. zend_string *t;
  2148. int last=0; /* Shut up the compiler warning */
  2149. int ch;
  2150. if (Z_STRLEN_P(str) == 0) {
  2151. zval_ptr_dtor_str(str);
  2152. ZVAL_INTERNED_STR(str, ZSTR_CHAR('1'));
  2153. return;
  2154. }
  2155. if (!Z_REFCOUNTED_P(str)) {
  2156. Z_STR_P(str) = zend_string_init(Z_STRVAL_P(str), Z_STRLEN_P(str), 0);
  2157. Z_TYPE_INFO_P(str) = IS_STRING_EX;
  2158. } else if (Z_REFCOUNT_P(str) > 1) {
  2159. Z_DELREF_P(str);
  2160. Z_STR_P(str) = zend_string_init(Z_STRVAL_P(str), Z_STRLEN_P(str), 0);
  2161. } else {
  2162. zend_string_forget_hash_val(Z_STR_P(str));
  2163. }
  2164. s = Z_STRVAL_P(str);
  2165. do {
  2166. ch = s[pos];
  2167. if (ch >= 'a' && ch <= 'z') {
  2168. if (ch == 'z') {
  2169. s[pos] = 'a';
  2170. carry=1;
  2171. } else {
  2172. s[pos]++;
  2173. carry=0;
  2174. }
  2175. last=LOWER_CASE;
  2176. } else if (ch >= 'A' && ch <= 'Z') {
  2177. if (ch == 'Z') {
  2178. s[pos] = 'A';
  2179. carry=1;
  2180. } else {
  2181. s[pos]++;
  2182. carry=0;
  2183. }
  2184. last=UPPER_CASE;
  2185. } else if (ch >= '0' && ch <= '9') {
  2186. if (ch == '9') {
  2187. s[pos] = '0';
  2188. carry=1;
  2189. } else {
  2190. s[pos]++;
  2191. carry=0;
  2192. }
  2193. last = NUMERIC;
  2194. } else {
  2195. carry=0;
  2196. break;
  2197. }
  2198. if (carry == 0) {
  2199. break;
  2200. }
  2201. } while (pos-- > 0);
  2202. if (carry) {
  2203. t = zend_string_alloc(Z_STRLEN_P(str)+1, 0);
  2204. memcpy(ZSTR_VAL(t) + 1, Z_STRVAL_P(str), Z_STRLEN_P(str));
  2205. ZSTR_VAL(t)[Z_STRLEN_P(str) + 1] = '\0';
  2206. switch (last) {
  2207. case NUMERIC:
  2208. ZSTR_VAL(t)[0] = '1';
  2209. break;
  2210. case UPPER_CASE:
  2211. ZSTR_VAL(t)[0] = 'A';
  2212. break;
  2213. case LOWER_CASE:
  2214. ZSTR_VAL(t)[0] = 'a';
  2215. break;
  2216. }
  2217. zend_string_free(Z_STR_P(str));
  2218. ZVAL_NEW_STR(str, t);
  2219. }
  2220. }
  2221. /* }}} */
  2222. ZEND_API int ZEND_FASTCALL increment_function(zval *op1) /* {{{ */
  2223. {
  2224. try_again:
  2225. switch (Z_TYPE_P(op1)) {
  2226. case IS_LONG:
  2227. fast_long_increment_function(op1);
  2228. break;
  2229. case IS_DOUBLE:
  2230. Z_DVAL_P(op1) = Z_DVAL_P(op1) + 1;
  2231. break;
  2232. case IS_NULL:
  2233. ZVAL_LONG(op1, 1);
  2234. break;
  2235. case IS_STRING: {
  2236. zend_long lval;
  2237. double dval;
  2238. switch (is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), &lval, &dval, 0)) {
  2239. case IS_LONG:
  2240. zval_ptr_dtor_str(op1);
  2241. if (lval == ZEND_LONG_MAX) {
  2242. /* switch to double */
  2243. double d = (double)lval;
  2244. ZVAL_DOUBLE(op1, d+1);
  2245. } else {
  2246. ZVAL_LONG(op1, lval+1);
  2247. }
  2248. break;
  2249. case IS_DOUBLE:
  2250. zval_ptr_dtor_str(op1);
  2251. ZVAL_DOUBLE(op1, dval+1);
  2252. break;
  2253. default:
  2254. /* Perl style string increment */
  2255. increment_string(op1);
  2256. break;
  2257. }
  2258. }
  2259. break;
  2260. case IS_OBJECT:
  2261. if (Z_OBJ_HANDLER_P(op1, get)
  2262. && Z_OBJ_HANDLER_P(op1, set)) {
  2263. /* proxy object */
  2264. zval rv;
  2265. zval *val;
  2266. val = Z_OBJ_HANDLER_P(op1, get)(op1, &rv);
  2267. Z_TRY_ADDREF_P(val);
  2268. increment_function(val);
  2269. Z_OBJ_HANDLER_P(op1, set)(op1, val);
  2270. zval_ptr_dtor(val);
  2271. } else if (Z_OBJ_HANDLER_P(op1, do_operation)) {
  2272. zval op2;
  2273. int res;
  2274. ZVAL_LONG(&op2, 1);
  2275. res = Z_OBJ_HANDLER_P(op1, do_operation)(ZEND_ADD, op1, op1, &op2);
  2276. return res;
  2277. }
  2278. return FAILURE;
  2279. case IS_REFERENCE:
  2280. op1 = Z_REFVAL_P(op1);
  2281. goto try_again;
  2282. default:
  2283. return FAILURE;
  2284. }
  2285. return SUCCESS;
  2286. }
  2287. /* }}} */
  2288. ZEND_API int ZEND_FASTCALL decrement_function(zval *op1) /* {{{ */
  2289. {
  2290. zend_long lval;
  2291. double dval;
  2292. try_again:
  2293. switch (Z_TYPE_P(op1)) {
  2294. case IS_LONG:
  2295. fast_long_decrement_function(op1);
  2296. break;
  2297. case IS_DOUBLE:
  2298. Z_DVAL_P(op1) = Z_DVAL_P(op1) - 1;
  2299. break;
  2300. case IS_STRING: /* Like perl we only support string increment */
  2301. if (Z_STRLEN_P(op1) == 0) { /* consider as 0 */
  2302. zval_ptr_dtor_str(op1);
  2303. ZVAL_LONG(op1, -1);
  2304. break;
  2305. }
  2306. switch (is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), &lval, &dval, 0)) {
  2307. case IS_LONG:
  2308. zval_ptr_dtor_str(op1);
  2309. if (lval == ZEND_LONG_MIN) {
  2310. double d = (double)lval;
  2311. ZVAL_DOUBLE(op1, d-1);
  2312. } else {
  2313. ZVAL_LONG(op1, lval-1);
  2314. }
  2315. break;
  2316. case IS_DOUBLE:
  2317. zval_ptr_dtor_str(op1);
  2318. ZVAL_DOUBLE(op1, dval - 1);
  2319. break;
  2320. }
  2321. break;
  2322. case IS_OBJECT:
  2323. if (Z_OBJ_HANDLER_P(op1, get)
  2324. && Z_OBJ_HANDLER_P(op1, set)) {
  2325. /* proxy object */
  2326. zval rv;
  2327. zval *val;
  2328. val = Z_OBJ_HANDLER_P(op1, get)(op1, &rv);
  2329. Z_TRY_ADDREF_P(val);
  2330. decrement_function(val);
  2331. Z_OBJ_HANDLER_P(op1, set)(op1, val);
  2332. zval_ptr_dtor(val);
  2333. } else if (Z_OBJ_HANDLER_P(op1, do_operation)) {
  2334. zval op2;
  2335. int res;
  2336. ZVAL_LONG(&op2, 1);
  2337. res = Z_OBJ_HANDLER_P(op1, do_operation)(ZEND_SUB, op1, op1, &op2);
  2338. return res;
  2339. }
  2340. return FAILURE;
  2341. case IS_REFERENCE:
  2342. op1 = Z_REFVAL_P(op1);
  2343. goto try_again;
  2344. default:
  2345. return FAILURE;
  2346. }
  2347. return SUCCESS;
  2348. }
  2349. /* }}} */
  2350. ZEND_API int ZEND_FASTCALL zend_is_true(zval *op) /* {{{ */
  2351. {
  2352. return i_zend_is_true(op);
  2353. }
  2354. /* }}} */
  2355. ZEND_API int ZEND_FASTCALL zend_object_is_true(zval *op) /* {{{ */
  2356. {
  2357. if (Z_OBJ_HT_P(op)->cast_object) {
  2358. zval tmp;
  2359. if (Z_OBJ_HT_P(op)->cast_object(op, &tmp, _IS_BOOL) == SUCCESS) {
  2360. return Z_TYPE(tmp) == IS_TRUE;
  2361. }
  2362. zend_error(E_RECOVERABLE_ERROR, "Object of class %s could not be converted to bool", ZSTR_VAL(Z_OBJ_P(op)->ce->name));
  2363. } else if (Z_OBJ_HT_P(op)->get) {
  2364. int result;
  2365. zval rv;
  2366. zval *tmp = Z_OBJ_HT_P(op)->get(op, &rv);
  2367. if (Z_TYPE_P(tmp) != IS_OBJECT) {
  2368. /* for safety - avoid loop */
  2369. result = i_zend_is_true(tmp);
  2370. zval_ptr_dtor(tmp);
  2371. return result;
  2372. }
  2373. }
  2374. return 1;
  2375. }
  2376. /* }}} */
  2377. #ifdef ZEND_USE_TOLOWER_L
  2378. ZEND_API void zend_update_current_locale(void) /* {{{ */
  2379. {
  2380. current_locale = _get_current_locale();
  2381. }
  2382. /* }}} */
  2383. #endif
  2384. ZEND_API char* ZEND_FASTCALL zend_str_tolower_copy(char *dest, const char *source, size_t length) /* {{{ */
  2385. {
  2386. register unsigned char *str = (unsigned char*)source;
  2387. register unsigned char *result = (unsigned char*)dest;
  2388. register unsigned char *end = str + length;
  2389. while (str < end) {
  2390. *result++ = zend_tolower_ascii(*str++);
  2391. }
  2392. *result = '\0';
  2393. return dest;
  2394. }
  2395. /* }}} */
  2396. ZEND_API char* ZEND_FASTCALL zend_str_tolower_dup(const char *source, size_t length) /* {{{ */
  2397. {
  2398. return zend_str_tolower_copy((char *)emalloc(length+1), source, length);
  2399. }
  2400. /* }}} */
  2401. ZEND_API void ZEND_FASTCALL zend_str_tolower(char *str, size_t length) /* {{{ */
  2402. {
  2403. register unsigned char *p = (unsigned char*)str;
  2404. register unsigned char *end = p + length;
  2405. while (p < end) {
  2406. *p = zend_tolower_ascii(*p);
  2407. p++;
  2408. }
  2409. }
  2410. /* }}} */
  2411. ZEND_API char* ZEND_FASTCALL zend_str_tolower_dup_ex(const char *source, size_t length) /* {{{ */
  2412. {
  2413. register const unsigned char *p = (const unsigned char*)source;
  2414. register const unsigned char *end = p + length;
  2415. while (p < end) {
  2416. if (*p != zend_tolower_ascii(*p)) {
  2417. char *res = (char*)emalloc(length + 1);
  2418. register unsigned char *r;
  2419. if (p != (const unsigned char*)source) {
  2420. memcpy(res, source, p - (const unsigned char*)source);
  2421. }
  2422. r = (unsigned char*)p + (res - source);
  2423. while (p < end) {
  2424. *r = zend_tolower_ascii(*p);
  2425. p++;
  2426. r++;
  2427. }
  2428. *r = '\0';
  2429. return res;
  2430. }
  2431. p++;
  2432. }
  2433. return NULL;
  2434. }
  2435. /* }}} */
  2436. ZEND_API zend_string* ZEND_FASTCALL zend_string_tolower_ex(zend_string *str, int persistent) /* {{{ */
  2437. {
  2438. register unsigned char *p = (unsigned char*)ZSTR_VAL(str);
  2439. register unsigned char *end = p + ZSTR_LEN(str);
  2440. while (p < end) {
  2441. if (*p != zend_tolower_ascii(*p)) {
  2442. zend_string *res = zend_string_alloc(ZSTR_LEN(str), persistent);
  2443. register unsigned char *r;
  2444. if (p != (unsigned char*)ZSTR_VAL(str)) {
  2445. memcpy(ZSTR_VAL(res), ZSTR_VAL(str), p - (unsigned char*)ZSTR_VAL(str));
  2446. }
  2447. r = p + (ZSTR_VAL(res) - ZSTR_VAL(str));
  2448. while (p < end) {
  2449. *r = zend_tolower_ascii(*p);
  2450. p++;
  2451. r++;
  2452. }
  2453. *r = '\0';
  2454. return res;
  2455. }
  2456. p++;
  2457. }
  2458. return zend_string_copy(str);
  2459. }
  2460. /* }}} */
  2461. ZEND_API int ZEND_FASTCALL zend_binary_strcmp(const char *s1, size_t len1, const char *s2, size_t len2) /* {{{ */
  2462. {
  2463. int retval;
  2464. if (s1 == s2) {
  2465. return 0;
  2466. }
  2467. retval = memcmp(s1, s2, MIN(len1, len2));
  2468. if (!retval) {
  2469. return (int)(len1 - len2);
  2470. } else {
  2471. return retval;
  2472. }
  2473. }
  2474. /* }}} */
  2475. ZEND_API int ZEND_FASTCALL zend_binary_strncmp(const char *s1, size_t len1, const char *s2, size_t len2, size_t length) /* {{{ */
  2476. {
  2477. int retval;
  2478. if (s1 == s2) {
  2479. return 0;
  2480. }
  2481. retval = memcmp(s1, s2, MIN(length, MIN(len1, len2)));
  2482. if (!retval) {
  2483. return (int)(MIN(length, len1) - MIN(length, len2));
  2484. } else {
  2485. return retval;
  2486. }
  2487. }
  2488. /* }}} */
  2489. ZEND_API int ZEND_FASTCALL zend_binary_strcasecmp(const char *s1, size_t len1, const char *s2, size_t len2) /* {{{ */
  2490. {
  2491. size_t len;
  2492. int c1, c2;
  2493. if (s1 == s2) {
  2494. return 0;
  2495. }
  2496. len = MIN(len1, len2);
  2497. while (len--) {
  2498. c1 = zend_tolower_ascii(*(unsigned char *)s1++);
  2499. c2 = zend_tolower_ascii(*(unsigned char *)s2++);
  2500. if (c1 != c2) {
  2501. return c1 - c2;
  2502. }
  2503. }
  2504. return (int)(len1 - len2);
  2505. }
  2506. /* }}} */
  2507. ZEND_API int ZEND_FASTCALL zend_binary_strncasecmp(const char *s1, size_t len1, const char *s2, size_t len2, size_t length) /* {{{ */
  2508. {
  2509. size_t len;
  2510. int c1, c2;
  2511. if (s1 == s2) {
  2512. return 0;
  2513. }
  2514. len = MIN(length, MIN(len1, len2));
  2515. while (len--) {
  2516. c1 = zend_tolower_ascii(*(unsigned char *)s1++);
  2517. c2 = zend_tolower_ascii(*(unsigned char *)s2++);
  2518. if (c1 != c2) {
  2519. return c1 - c2;
  2520. }
  2521. }
  2522. return (int)(MIN(length, len1) - MIN(length, len2));
  2523. }
  2524. /* }}} */
  2525. ZEND_API int ZEND_FASTCALL zend_binary_strcasecmp_l(const char *s1, size_t len1, const char *s2, size_t len2) /* {{{ */
  2526. {
  2527. size_t len;
  2528. int c1, c2;
  2529. if (s1 == s2) {
  2530. return 0;
  2531. }
  2532. len = MIN(len1, len2);
  2533. while (len--) {
  2534. c1 = zend_tolower((int)*(unsigned char *)s1++);
  2535. c2 = zend_tolower((int)*(unsigned char *)s2++);
  2536. if (c1 != c2) {
  2537. return c1 - c2;
  2538. }
  2539. }
  2540. return (int)(len1 - len2);
  2541. }
  2542. /* }}} */
  2543. ZEND_API int ZEND_FASTCALL zend_binary_strncasecmp_l(const char *s1, size_t len1, const char *s2, size_t len2, size_t length) /* {{{ */
  2544. {
  2545. size_t len;
  2546. int c1, c2;
  2547. if (s1 == s2) {
  2548. return 0;
  2549. }
  2550. len = MIN(length, MIN(len1, len2));
  2551. while (len--) {
  2552. c1 = zend_tolower((int)*(unsigned char *)s1++);
  2553. c2 = zend_tolower((int)*(unsigned char *)s2++);
  2554. if (c1 != c2) {
  2555. return c1 - c2;
  2556. }
  2557. }
  2558. return (int)(MIN(length, len1) - MIN(length, len2));
  2559. }
  2560. /* }}} */
  2561. ZEND_API int ZEND_FASTCALL zend_binary_zval_strcmp(zval *s1, zval *s2) /* {{{ */
  2562. {
  2563. return zend_binary_strcmp(Z_STRVAL_P(s1), Z_STRLEN_P(s1), Z_STRVAL_P(s2), Z_STRLEN_P(s2));
  2564. }
  2565. /* }}} */
  2566. ZEND_API int ZEND_FASTCALL zend_binary_zval_strncmp(zval *s1, zval *s2, zval *s3) /* {{{ */
  2567. {
  2568. return zend_binary_strncmp(Z_STRVAL_P(s1), Z_STRLEN_P(s1), Z_STRVAL_P(s2), Z_STRLEN_P(s2), Z_LVAL_P(s3));
  2569. }
  2570. /* }}} */
  2571. ZEND_API int ZEND_FASTCALL zend_binary_zval_strcasecmp(zval *s1, zval *s2) /* {{{ */
  2572. {
  2573. return zend_binary_strcasecmp_l(Z_STRVAL_P(s1), Z_STRLEN_P(s1), Z_STRVAL_P(s2), Z_STRLEN_P(s2));
  2574. }
  2575. /* }}} */
  2576. ZEND_API int ZEND_FASTCALL zend_binary_zval_strncasecmp(zval *s1, zval *s2, zval *s3) /* {{{ */
  2577. {
  2578. return zend_binary_strncasecmp_l(Z_STRVAL_P(s1), Z_STRLEN_P(s1), Z_STRVAL_P(s2), Z_STRLEN_P(s2), Z_LVAL_P(s3));
  2579. }
  2580. /* }}} */
  2581. ZEND_API int ZEND_FASTCALL zendi_smart_streq(zend_string *s1, zend_string *s2) /* {{{ */
  2582. {
  2583. int ret1, ret2;
  2584. int oflow1, oflow2;
  2585. zend_long lval1 = 0, lval2 = 0;
  2586. double dval1 = 0.0, dval2 = 0.0;
  2587. if ((ret1 = is_numeric_string_ex(s1->val, s1->len, &lval1, &dval1, 0, &oflow1)) &&
  2588. (ret2 = is_numeric_string_ex(s2->val, s2->len, &lval2, &dval2, 0, &oflow2))) {
  2589. #if ZEND_ULONG_MAX == 0xFFFFFFFF
  2590. if (oflow1 != 0 && oflow1 == oflow2 && dval1 - dval2 == 0. &&
  2591. ((oflow1 == 1 && dval1 > 9007199254740991. /*0x1FFFFFFFFFFFFF*/)
  2592. || (oflow1 == -1 && dval1 < -9007199254740991.))) {
  2593. #else
  2594. if (oflow1 != 0 && oflow1 == oflow2 && dval1 - dval2 == 0.) {
  2595. #endif
  2596. /* both values are integers overflown to the same side, and the
  2597. * double comparison may have resulted in crucial accuracy lost */
  2598. goto string_cmp;
  2599. }
  2600. if ((ret1 == IS_DOUBLE) || (ret2 == IS_DOUBLE)) {
  2601. if (ret1 != IS_DOUBLE) {
  2602. if (oflow2) {
  2603. /* 2nd operand is integer > LONG_MAX (oflow2==1) or < LONG_MIN (-1) */
  2604. return 0;
  2605. }
  2606. dval1 = (double) lval1;
  2607. } else if (ret2 != IS_DOUBLE) {
  2608. if (oflow1) {
  2609. return 0;
  2610. }
  2611. dval2 = (double) lval2;
  2612. } else if (dval1 == dval2 && !zend_finite(dval1)) {
  2613. /* Both values overflowed and have the same sign,
  2614. * so a numeric comparison would be inaccurate */
  2615. goto string_cmp;
  2616. }
  2617. return dval1 == dval2;
  2618. } else { /* they both have to be long's */
  2619. return lval1 == lval2;
  2620. }
  2621. } else {
  2622. string_cmp:
  2623. return zend_string_equal_content(s1, s2);
  2624. }
  2625. }
  2626. /* }}} */
  2627. ZEND_API int ZEND_FASTCALL zendi_smart_strcmp(zend_string *s1, zend_string *s2) /* {{{ */
  2628. {
  2629. int ret1, ret2;
  2630. int oflow1, oflow2;
  2631. zend_long lval1 = 0, lval2 = 0;
  2632. double dval1 = 0.0, dval2 = 0.0;
  2633. if ((ret1 = is_numeric_string_ex(s1->val, s1->len, &lval1, &dval1, 0, &oflow1)) &&
  2634. (ret2 = is_numeric_string_ex(s2->val, s2->len, &lval2, &dval2, 0, &oflow2))) {
  2635. #if ZEND_ULONG_MAX == 0xFFFFFFFF
  2636. if (oflow1 != 0 && oflow1 == oflow2 && dval1 - dval2 == 0. &&
  2637. ((oflow1 == 1 && dval1 > 9007199254740991. /*0x1FFFFFFFFFFFFF*/)
  2638. || (oflow1 == -1 && dval1 < -9007199254740991.))) {
  2639. #else
  2640. if (oflow1 != 0 && oflow1 == oflow2 && dval1 - dval2 == 0.) {
  2641. #endif
  2642. /* both values are integers overflown to the same side, and the
  2643. * double comparison may have resulted in crucial accuracy lost */
  2644. goto string_cmp;
  2645. }
  2646. if ((ret1 == IS_DOUBLE) || (ret2 == IS_DOUBLE)) {
  2647. if (ret1 != IS_DOUBLE) {
  2648. if (oflow2) {
  2649. /* 2nd operand is integer > LONG_MAX (oflow2==1) or < LONG_MIN (-1) */
  2650. return -1 * oflow2;
  2651. }
  2652. dval1 = (double) lval1;
  2653. } else if (ret2 != IS_DOUBLE) {
  2654. if (oflow1) {
  2655. return oflow1;
  2656. }
  2657. dval2 = (double) lval2;
  2658. } else if (dval1 == dval2 && !zend_finite(dval1)) {
  2659. /* Both values overflowed and have the same sign,
  2660. * so a numeric comparison would be inaccurate */
  2661. goto string_cmp;
  2662. }
  2663. dval1 = dval1 - dval2;
  2664. return ZEND_NORMALIZE_BOOL(dval1);
  2665. } else { /* they both have to be long's */
  2666. return lval1 > lval2 ? 1 : (lval1 < lval2 ? -1 : 0);
  2667. }
  2668. } else {
  2669. int strcmp_ret;
  2670. string_cmp:
  2671. strcmp_ret = zend_binary_strcmp(s1->val, s1->len, s2->val, s2->len);
  2672. return ZEND_NORMALIZE_BOOL(strcmp_ret);
  2673. }
  2674. }
  2675. /* }}} */
  2676. static int hash_zval_compare_function(zval *z1, zval *z2) /* {{{ */
  2677. {
  2678. zval result;
  2679. if (compare_function(&result, z1, z2)==FAILURE) {
  2680. return 1;
  2681. }
  2682. return Z_LVAL(result);
  2683. }
  2684. /* }}} */
  2685. ZEND_API int ZEND_FASTCALL zend_compare_symbol_tables(HashTable *ht1, HashTable *ht2) /* {{{ */
  2686. {
  2687. return ht1 == ht2 ? 0 : zend_hash_compare(ht1, ht2, (compare_func_t) hash_zval_compare_function, 0);
  2688. }
  2689. /* }}} */
  2690. ZEND_API int ZEND_FASTCALL zend_compare_arrays(zval *a1, zval *a2) /* {{{ */
  2691. {
  2692. return zend_compare_symbol_tables(Z_ARRVAL_P(a1), Z_ARRVAL_P(a2));
  2693. }
  2694. /* }}} */
  2695. ZEND_API int ZEND_FASTCALL zend_compare_objects(zval *o1, zval *o2) /* {{{ */
  2696. {
  2697. if (Z_OBJ_P(o1) == Z_OBJ_P(o2)) {
  2698. return 0;
  2699. }
  2700. if (Z_OBJ_HT_P(o1)->compare_objects == NULL) {
  2701. return 1;
  2702. } else {
  2703. return Z_OBJ_HT_P(o1)->compare_objects(o1, o2);
  2704. }
  2705. }
  2706. /* }}} */
  2707. ZEND_API void ZEND_FASTCALL zend_locale_sprintf_double(zval *op ZEND_FILE_LINE_DC) /* {{{ */
  2708. {
  2709. zend_string *str;
  2710. str = zend_strpprintf(0, "%.*G", (int) EG(precision), (double)Z_DVAL_P(op));
  2711. ZVAL_NEW_STR(op, str);
  2712. }
  2713. /* }}} */
  2714. ZEND_API zend_string* ZEND_FASTCALL zend_long_to_str(zend_long num) /* {{{ */
  2715. {
  2716. if ((zend_ulong)num <= 9) {
  2717. return ZSTR_CHAR((zend_uchar)'0' + (zend_uchar)num);
  2718. } else {
  2719. char buf[MAX_LENGTH_OF_LONG + 1];
  2720. char *res = zend_print_long_to_buf(buf + sizeof(buf) - 1, num);
  2721. return zend_string_init(res, buf + sizeof(buf) - 1 - res, 0);
  2722. }
  2723. }
  2724. /* }}} */
  2725. ZEND_API zend_uchar ZEND_FASTCALL is_numeric_str_function(const zend_string *str, zend_long *lval, double *dval) /* {{{ */ {
  2726. return is_numeric_string_ex(ZSTR_VAL(str), ZSTR_LEN(str), lval, dval, -1, NULL);
  2727. }
  2728. /* }}} */
  2729. ZEND_API zend_uchar ZEND_FASTCALL _is_numeric_string_ex(const char *str, size_t length, zend_long *lval, double *dval, int allow_errors, int *oflow_info) /* {{{ */
  2730. {
  2731. const char *ptr;
  2732. int digits = 0, dp_or_e = 0;
  2733. double local_dval = 0.0;
  2734. zend_uchar type;
  2735. zend_long tmp_lval = 0;
  2736. int neg = 0;
  2737. if (!length) {
  2738. return 0;
  2739. }
  2740. if (oflow_info != NULL) {
  2741. *oflow_info = 0;
  2742. }
  2743. /* Skip any whitespace
  2744. * This is much faster than the isspace() function */
  2745. while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r' || *str == '\v' || *str == '\f') {
  2746. str++;
  2747. length--;
  2748. }
  2749. ptr = str;
  2750. if (*ptr == '-') {
  2751. neg = 1;
  2752. ptr++;
  2753. } else if (*ptr == '+') {
  2754. ptr++;
  2755. }
  2756. if (ZEND_IS_DIGIT(*ptr)) {
  2757. /* Skip any leading 0s */
  2758. while (*ptr == '0') {
  2759. ptr++;
  2760. }
  2761. /* Count the number of digits. If a decimal point/exponent is found,
  2762. * it's a double. Otherwise, if there's a dval or no need to check for
  2763. * a full match, stop when there are too many digits for a long */
  2764. for (type = IS_LONG; !(digits >= MAX_LENGTH_OF_LONG && (dval || allow_errors == 1)); digits++, ptr++) {
  2765. check_digits:
  2766. if (ZEND_IS_DIGIT(*ptr)) {
  2767. tmp_lval = tmp_lval * 10 + (*ptr) - '0';
  2768. continue;
  2769. } else if (*ptr == '.' && dp_or_e < 1) {
  2770. goto process_double;
  2771. } else if ((*ptr == 'e' || *ptr == 'E') && dp_or_e < 2) {
  2772. const char *e = ptr + 1;
  2773. if (*e == '-' || *e == '+') {
  2774. ptr = e++;
  2775. }
  2776. if (ZEND_IS_DIGIT(*e)) {
  2777. goto process_double;
  2778. }
  2779. }
  2780. break;
  2781. }
  2782. if (digits >= MAX_LENGTH_OF_LONG) {
  2783. if (oflow_info != NULL) {
  2784. *oflow_info = *str == '-' ? -1 : 1;
  2785. }
  2786. dp_or_e = -1;
  2787. goto process_double;
  2788. }
  2789. } else if (*ptr == '.' && ZEND_IS_DIGIT(ptr[1])) {
  2790. process_double:
  2791. type = IS_DOUBLE;
  2792. /* If there's a dval, do the conversion; else continue checking
  2793. * the digits if we need to check for a full match */
  2794. if (dval) {
  2795. local_dval = zend_strtod(str, &ptr);
  2796. } else if (allow_errors != 1 && dp_or_e != -1) {
  2797. dp_or_e = (*ptr++ == '.') ? 1 : 2;
  2798. goto check_digits;
  2799. }
  2800. } else {
  2801. return 0;
  2802. }
  2803. if (ptr != str + length) {
  2804. if (!allow_errors) {
  2805. return 0;
  2806. }
  2807. if (allow_errors == -1) {
  2808. zend_error(E_NOTICE, "A non well formed numeric value encountered");
  2809. }
  2810. }
  2811. if (type == IS_LONG) {
  2812. if (digits == MAX_LENGTH_OF_LONG - 1) {
  2813. int cmp = strcmp(&ptr[-digits], long_min_digits);
  2814. if (!(cmp < 0 || (cmp == 0 && *str == '-'))) {
  2815. if (dval) {
  2816. *dval = zend_strtod(str, NULL);
  2817. }
  2818. if (oflow_info != NULL) {
  2819. *oflow_info = *str == '-' ? -1 : 1;
  2820. }
  2821. return IS_DOUBLE;
  2822. }
  2823. }
  2824. if (lval) {
  2825. if (neg) {
  2826. tmp_lval = -tmp_lval;
  2827. }
  2828. *lval = tmp_lval;
  2829. }
  2830. return IS_LONG;
  2831. } else {
  2832. if (dval) {
  2833. *dval = local_dval;
  2834. }
  2835. return IS_DOUBLE;
  2836. }
  2837. }
  2838. /* }}} */
  2839. /*
  2840. * String matching - Sunday algorithm
  2841. * http://www.iti.fh-flensburg.de/lang/algorithmen/pattern/sundayen.htm
  2842. */
  2843. static zend_always_inline void zend_memnstr_ex_pre(unsigned int td[], const char *needle, size_t needle_len, int reverse) /* {{{ */ {
  2844. int i;
  2845. for (i = 0; i < 256; i++) {
  2846. td[i] = needle_len + 1;
  2847. }
  2848. if (reverse) {
  2849. for (i = needle_len - 1; i >= 0; i--) {
  2850. td[(unsigned char)needle[i]] = i + 1;
  2851. }
  2852. } else {
  2853. size_t i;
  2854. for (i = 0; i < needle_len; i++) {
  2855. td[(unsigned char)needle[i]] = (int)needle_len - i;
  2856. }
  2857. }
  2858. }
  2859. /* }}} */
  2860. ZEND_API const char* ZEND_FASTCALL zend_memnstr_ex(const char *haystack, const char *needle, size_t needle_len, const char *end) /* {{{ */
  2861. {
  2862. unsigned int td[256];
  2863. register size_t i;
  2864. register const char *p;
  2865. if (needle_len == 0 || (end - haystack) < needle_len) {
  2866. return NULL;
  2867. }
  2868. zend_memnstr_ex_pre(td, needle, needle_len, 0);
  2869. p = haystack;
  2870. end -= needle_len;
  2871. while (p <= end) {
  2872. for (i = 0; i < needle_len; i++) {
  2873. if (needle[i] != p[i]) {
  2874. break;
  2875. }
  2876. }
  2877. if (i == needle_len) {
  2878. return p;
  2879. }
  2880. if (UNEXPECTED(p == end)) {
  2881. return NULL;
  2882. }
  2883. p += td[(unsigned char)(p[needle_len])];
  2884. }
  2885. return NULL;
  2886. }
  2887. /* }}} */
  2888. ZEND_API const char* ZEND_FASTCALL zend_memnrstr_ex(const char *haystack, const char *needle, size_t needle_len, const char *end) /* {{{ */
  2889. {
  2890. unsigned int td[256];
  2891. register size_t i;
  2892. register const char *p;
  2893. if (needle_len == 0 || (end - haystack) < needle_len) {
  2894. return NULL;
  2895. }
  2896. zend_memnstr_ex_pre(td, needle, needle_len, 1);
  2897. p = end;
  2898. p -= needle_len;
  2899. while (p >= haystack) {
  2900. for (i = 0; i < needle_len; i++) {
  2901. if (needle[i] != p[i]) {
  2902. break;
  2903. }
  2904. }
  2905. if (i == needle_len) {
  2906. return (const char *)p;
  2907. }
  2908. if (UNEXPECTED(p == haystack)) {
  2909. return NULL;
  2910. }
  2911. p -= td[(unsigned char)(p[-1])];
  2912. }
  2913. return NULL;
  2914. }
  2915. /* }}} */
  2916. #if !ZEND_DVAL_TO_LVAL_CAST_OK
  2917. # if SIZEOF_ZEND_LONG == 4
  2918. ZEND_API zend_long ZEND_FASTCALL zend_dval_to_lval_slow(double d)
  2919. {
  2920. double two_pow_32 = pow(2., 32.),
  2921. dmod;
  2922. dmod = fmod(d, two_pow_32);
  2923. if (dmod < 0) {
  2924. /* we're going to make this number positive; call ceil()
  2925. * to simulate rounding towards 0 of the negative number */
  2926. dmod = ceil(dmod) + two_pow_32;
  2927. }
  2928. return (zend_long)(zend_ulong)dmod;
  2929. }
  2930. #else
  2931. ZEND_API zend_long ZEND_FASTCALL zend_dval_to_lval_slow(double d)
  2932. {
  2933. double two_pow_64 = pow(2., 64.),
  2934. dmod;
  2935. dmod = fmod(d, two_pow_64);
  2936. if (dmod < 0) {
  2937. /* no need to call ceil; original double must have had no
  2938. * fractional part, hence dmod does not have one either */
  2939. dmod += two_pow_64;
  2940. }
  2941. return (zend_long)(zend_ulong)dmod;
  2942. }
  2943. #endif
  2944. #endif
  2945. /*
  2946. * Local variables:
  2947. * tab-width: 4
  2948. * c-basic-offset: 4
  2949. * indent-tabs-mode: t
  2950. * End:
  2951. * vim600: sw=4 ts=4 fdm=marker
  2952. * vim<600: sw=4 ts=4
  2953. */