zend_operators.c 86 KB

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