123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433 |
- /*
- +----------------------------------------------------------------------+
- | Zend Engine |
- +----------------------------------------------------------------------+
- | Copyright (c) 1998-2016 Zend Technologies Ltd. (http://www.zend.com) |
- +----------------------------------------------------------------------+
- | This source file is subject to version 2.00 of the Zend license, |
- | that is bundled with this package in the file LICENSE, and is |
- | available through the world-wide-web at the following url: |
- | http://www.zend.com/license/2_00.txt. |
- | If you did not receive a copy of the Zend license and are unable to |
- | obtain it through the world-wide-web, please send a note to |
- | license@zend.com so we can mail you a copy immediately. |
- +----------------------------------------------------------------------+
- | Authors: Andi Gutmans <andi@zend.com> |
- | Zeev Suraski <zeev@zend.com> |
- +----------------------------------------------------------------------+
- */
- /* $Id$ */
- #include <ctype.h>
- #include "zend.h"
- #include "zend_operators.h"
- #include "zend_variables.h"
- #include "zend_globals.h"
- #include "zend_list.h"
- #include "zend_API.h"
- #include "zend_strtod.h"
- #include "zend_exceptions.h"
- #include "zend_closures.h"
- #if ZEND_USE_TOLOWER_L
- #include <locale.h>
- static _locale_t current_locale = NULL;
- /* this is true global! may lead to strange effects on ZTS, but so may setlocale() */
- #define zend_tolower(c) _tolower_l(c, current_locale)
- #else
- #define zend_tolower(c) tolower(c)
- #endif
- #define TYPE_PAIR(t1,t2) (((t1) << 4) | (t2))
- static const unsigned char tolower_map[256] = {
- 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
- 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,
- 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
- 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,
- 0x40,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,
- 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x5b,0x5c,0x5d,0x5e,0x5f,
- 0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,
- 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f,
- 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,
- 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,
- 0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf,
- 0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,
- 0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,
- 0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf,
- 0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,0xee,0xef,
- 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
- };
- #define zend_tolower_ascii(c) (tolower_map[(unsigned char)(c)])
- /**
- * Functions using locale lowercase:
- zend_binary_strncasecmp_l
- zend_binary_strcasecmp_l
- zend_binary_zval_strcasecmp
- zend_binary_zval_strncasecmp
- string_compare_function_ex
- string_case_compare_function
- * Functions using ascii lowercase:
- zend_str_tolower_copy
- zend_str_tolower_dup
- zend_str_tolower
- zend_binary_strcasecmp
- zend_binary_strncasecmp
- */
- ZEND_API int zend_atoi(const char *str, int str_len) /* {{{ */
- {
- int retval;
- if (!str_len) {
- str_len = strlen(str);
- }
- retval = strtol(str, NULL, 0);
- if (str_len>0) {
- switch (str[str_len-1]) {
- case 'g':
- case 'G':
- retval *= 1024;
- /* break intentionally missing */
- case 'm':
- case 'M':
- retval *= 1024;
- /* break intentionally missing */
- case 'k':
- case 'K':
- retval *= 1024;
- break;
- }
- }
- return retval;
- }
- /* }}} */
- ZEND_API long zend_atol(const char *str, int str_len) /* {{{ */
- {
- long retval;
- if (!str_len) {
- str_len = strlen(str);
- }
- retval = strtol(str, NULL, 0);
- if (str_len>0) {
- switch (str[str_len-1]) {
- case 'g':
- case 'G':
- retval *= 1024;
- /* break intentionally missing */
- case 'm':
- case 'M':
- retval *= 1024;
- /* break intentionally missing */
- case 'k':
- case 'K':
- retval *= 1024;
- break;
- }
- }
- return retval;
- }
- /* }}} */
- ZEND_API double zend_string_to_double(const char *number, zend_uint length) /* {{{ */
- {
- double divisor = 10.0;
- double result = 0.0;
- double exponent;
- const char *end = number+length;
- const char *digit = number;
- if (!length) {
- return result;
- }
- while (digit < end) {
- if ((*digit <= '9' && *digit >= '0')) {
- result *= 10;
- result += *digit - '0';
- } else if (*digit == '.') {
- digit++;
- break;
- } else if (toupper(*digit) == 'E') {
- exponent = (double) atoi(digit+1);
- result *= pow(10.0, exponent);
- return result;
- } else {
- return result;
- }
- digit++;
- }
- while (digit < end) {
- if ((*digit <= '9' && *digit >= '0')) {
- result += (*digit - '0') / divisor;
- divisor *= 10;
- } else if (toupper(*digit) == 'E') {
- exponent = (double) atoi(digit+1);
- result *= pow(10.0, exponent);
- return result;
- } else {
- return result;
- }
- digit++;
- }
- return result;
- }
- /* }}} */
- ZEND_API void convert_scalar_to_number(zval *op TSRMLS_DC) /* {{{ */
- {
- switch (Z_TYPE_P(op)) {
- case IS_STRING:
- {
- char *strval;
- strval = Z_STRVAL_P(op);
- if ((Z_TYPE_P(op)=is_numeric_string(strval, Z_STRLEN_P(op), &Z_LVAL_P(op), &Z_DVAL_P(op), 1)) == 0) {
- ZVAL_LONG(op, 0);
- }
- str_efree(strval);
- break;
- }
- case IS_BOOL:
- Z_TYPE_P(op) = IS_LONG;
- break;
- case IS_RESOURCE:
- zend_list_delete(Z_LVAL_P(op));
- Z_TYPE_P(op) = IS_LONG;
- break;
- case IS_OBJECT:
- convert_to_long_base(op, 10);
- break;
- case IS_NULL:
- ZVAL_LONG(op, 0);
- break;
- }
- }
- /* }}} */
- /* {{{ zendi_convert_scalar_to_number */
- #define zendi_convert_scalar_to_number(op, holder, result) \
- if (op==result) { \
- if (Z_TYPE_P(op) != IS_LONG) { \
- convert_scalar_to_number(op TSRMLS_CC); \
- } \
- } else { \
- switch (Z_TYPE_P(op)) { \
- case IS_STRING: \
- { \
- if ((Z_TYPE(holder)=is_numeric_string(Z_STRVAL_P(op), Z_STRLEN_P(op), &Z_LVAL(holder), &Z_DVAL(holder), 1)) == 0) { \
- ZVAL_LONG(&(holder), 0); \
- } \
- (op) = &(holder); \
- break; \
- } \
- case IS_BOOL: \
- case IS_RESOURCE: \
- ZVAL_LONG(&(holder), Z_LVAL_P(op)); \
- (op) = &(holder); \
- break; \
- case IS_NULL: \
- ZVAL_LONG(&(holder), 0); \
- (op) = &(holder); \
- break; \
- case IS_OBJECT: \
- (holder) = (*(op)); \
- zval_copy_ctor(&(holder)); \
- convert_to_long_base(&(holder), 10); \
- if (Z_TYPE(holder) == IS_LONG) { \
- (op) = &(holder); \
- } \
- break; \
- } \
- }
- /* }}} */
- /* {{{ zendi_convert_to_long */
- #define zendi_convert_to_long(op, holder, result) \
- if (op == result) { \
- convert_to_long(op); \
- } else if (Z_TYPE_P(op) != IS_LONG) { \
- switch (Z_TYPE_P(op)) { \
- case IS_NULL: \
- Z_LVAL(holder) = 0; \
- break; \
- case IS_DOUBLE: \
- Z_LVAL(holder) = zend_dval_to_lval(Z_DVAL_P(op)); \
- break; \
- case IS_STRING: \
- Z_LVAL(holder) = strtol(Z_STRVAL_P(op), NULL, 10); \
- break; \
- case IS_ARRAY: \
- Z_LVAL(holder) = (zend_hash_num_elements(Z_ARRVAL_P(op))?1:0); \
- break; \
- case IS_OBJECT: \
- (holder) = (*(op)); \
- zval_copy_ctor(&(holder)); \
- convert_to_long_base(&(holder), 10); \
- break; \
- case IS_BOOL: \
- case IS_RESOURCE: \
- Z_LVAL(holder) = Z_LVAL_P(op); \
- break; \
- default: \
- zend_error(E_WARNING, "Cannot convert to ordinal value"); \
- Z_LVAL(holder) = 0; \
- break; \
- } \
- Z_TYPE(holder) = IS_LONG; \
- (op) = &(holder); \
- }
- /* }}} */
- /* {{{ zendi_convert_to_boolean */
- #define zendi_convert_to_boolean(op, holder, result) \
- if (op==result) { \
- convert_to_boolean(op); \
- } else if (Z_TYPE_P(op) != IS_BOOL) { \
- switch (Z_TYPE_P(op)) { \
- case IS_NULL: \
- Z_LVAL(holder) = 0; \
- break; \
- case IS_RESOURCE: \
- case IS_LONG: \
- Z_LVAL(holder) = (Z_LVAL_P(op) ? 1 : 0); \
- break; \
- case IS_DOUBLE: \
- Z_LVAL(holder) = (Z_DVAL_P(op) ? 1 : 0); \
- break; \
- case IS_STRING: \
- if (Z_STRLEN_P(op) == 0 \
- || (Z_STRLEN_P(op)==1 && Z_STRVAL_P(op)[0]=='0')) { \
- Z_LVAL(holder) = 0; \
- } else { \
- Z_LVAL(holder) = 1; \
- } \
- break; \
- case IS_ARRAY: \
- Z_LVAL(holder) = (zend_hash_num_elements(Z_ARRVAL_P(op))?1:0); \
- break; \
- case IS_OBJECT: \
- (holder) = (*(op)); \
- zval_copy_ctor(&(holder)); \
- convert_to_boolean(&(holder)); \
- break; \
- default: \
- Z_LVAL(holder) = 0; \
- break; \
- } \
- Z_TYPE(holder) = IS_BOOL; \
- (op) = &(holder); \
- }
- /* }}} */
- /* {{{ convert_object_to_type */
- #define convert_object_to_type(op, ctype, conv_func) \
- if (Z_OBJ_HT_P(op)->cast_object) { \
- zval dst; \
- if (Z_OBJ_HT_P(op)->cast_object(op, &dst, ctype TSRMLS_CC) == FAILURE) { \
- zend_error(E_RECOVERABLE_ERROR, \
- "Object of class %s could not be converted to %s", Z_OBJCE_P(op)->name, \
- zend_get_type_by_const(ctype)); \
- } else { \
- zval_dtor(op); \
- Z_TYPE_P(op) = ctype; \
- op->value = dst.value; \
- } \
- } else { \
- if (Z_OBJ_HT_P(op)->get) { \
- zval *newop = Z_OBJ_HT_P(op)->get(op TSRMLS_CC); \
- if (Z_TYPE_P(newop) != IS_OBJECT) { \
- /* for safety - avoid loop */ \
- zval_dtor(op); \
- *op = *newop; \
- FREE_ZVAL(newop); \
- conv_func(op); \
- } \
- } \
- }
- /* }}} */
- ZEND_API void convert_to_long(zval *op) /* {{{ */
- {
- if (Z_TYPE_P(op) != IS_LONG) {
- convert_to_long_base(op, 10);
- }
- }
- /* }}} */
- ZEND_API void convert_to_long_base(zval *op, int base) /* {{{ */
- {
- long tmp;
- switch (Z_TYPE_P(op)) {
- case IS_NULL:
- Z_LVAL_P(op) = 0;
- break;
- case IS_RESOURCE: {
- TSRMLS_FETCH();
- zend_list_delete(Z_LVAL_P(op));
- }
- /* break missing intentionally */
- case IS_BOOL:
- case IS_LONG:
- break;
- case IS_DOUBLE:
- Z_LVAL_P(op) = zend_dval_to_lval(Z_DVAL_P(op));
- break;
- case IS_STRING:
- {
- char *strval = Z_STRVAL_P(op);
- Z_LVAL_P(op) = strtol(strval, NULL, base);
- str_efree(strval);
- }
- break;
- case IS_ARRAY:
- tmp = (zend_hash_num_elements(Z_ARRVAL_P(op))?1:0);
- zval_dtor(op);
- Z_LVAL_P(op) = tmp;
- break;
- case IS_OBJECT:
- {
- int retval = 1;
- TSRMLS_FETCH();
- convert_object_to_type(op, IS_LONG, convert_to_long);
- if (Z_TYPE_P(op) == IS_LONG) {
- return;
- }
- zend_error(E_NOTICE, "Object of class %s could not be converted to int", Z_OBJCE_P(op)->name);
- zval_dtor(op);
- ZVAL_LONG(op, retval);
- return;
- }
- default:
- zend_error(E_WARNING, "Cannot convert to ordinal value");
- zval_dtor(op);
- Z_LVAL_P(op) = 0;
- break;
- }
- Z_TYPE_P(op) = IS_LONG;
- }
- /* }}} */
- ZEND_API void convert_to_double(zval *op) /* {{{ */
- {
- double tmp;
- switch (Z_TYPE_P(op)) {
- case IS_NULL:
- Z_DVAL_P(op) = 0.0;
- break;
- case IS_RESOURCE: {
- TSRMLS_FETCH();
- zend_list_delete(Z_LVAL_P(op));
- }
- /* break missing intentionally */
- case IS_BOOL:
- case IS_LONG:
- Z_DVAL_P(op) = (double) Z_LVAL_P(op);
- break;
- case IS_DOUBLE:
- break;
- case IS_STRING:
- {
- char *strval = Z_STRVAL_P(op);
- Z_DVAL_P(op) = zend_strtod(strval, NULL);
- str_efree(strval);
- }
- break;
- case IS_ARRAY:
- tmp = (zend_hash_num_elements(Z_ARRVAL_P(op))?1:0);
- zval_dtor(op);
- Z_DVAL_P(op) = tmp;
- break;
- case IS_OBJECT:
- {
- double retval = 1.0;
- TSRMLS_FETCH();
- convert_object_to_type(op, IS_DOUBLE, convert_to_double);
- if (Z_TYPE_P(op) == IS_DOUBLE) {
- return;
- }
- zend_error(E_NOTICE, "Object of class %s could not be converted to double", Z_OBJCE_P(op)->name);
- zval_dtor(op);
- ZVAL_DOUBLE(op, retval);
- break;
- }
- default:
- zend_error(E_WARNING, "Cannot convert to real value (type=%d)", Z_TYPE_P(op));
- zval_dtor(op);
- Z_DVAL_P(op) = 0;
- break;
- }
- Z_TYPE_P(op) = IS_DOUBLE;
- }
- /* }}} */
- ZEND_API void convert_to_null(zval *op) /* {{{ */
- {
- if (Z_TYPE_P(op) == IS_OBJECT) {
- if (Z_OBJ_HT_P(op)->cast_object) {
- zval *org;
- TSRMLS_FETCH();
- ALLOC_ZVAL(org);
- *org = *op;
- if (Z_OBJ_HT_P(op)->cast_object(org, op, IS_NULL TSRMLS_CC) == SUCCESS) {
- zval_dtor(org);
- return;
- }
- *op = *org;
- FREE_ZVAL(org);
- }
- }
- zval_dtor(op);
- Z_TYPE_P(op) = IS_NULL;
- }
- /* }}} */
- ZEND_API void convert_to_boolean(zval *op) /* {{{ */
- {
- int tmp;
- switch (Z_TYPE_P(op)) {
- case IS_BOOL:
- break;
- case IS_NULL:
- Z_LVAL_P(op) = 0;
- break;
- case IS_RESOURCE: {
- TSRMLS_FETCH();
- zend_list_delete(Z_LVAL_P(op));
- }
- /* break missing intentionally */
- case IS_LONG:
- Z_LVAL_P(op) = (Z_LVAL_P(op) ? 1 : 0);
- break;
- case IS_DOUBLE:
- Z_LVAL_P(op) = (Z_DVAL_P(op) ? 1 : 0);
- break;
- case IS_STRING:
- {
- char *strval = Z_STRVAL_P(op);
- if (Z_STRLEN_P(op) == 0
- || (Z_STRLEN_P(op)==1 && Z_STRVAL_P(op)[0]=='0')) {
- Z_LVAL_P(op) = 0;
- } else {
- Z_LVAL_P(op) = 1;
- }
- str_efree(strval);
- }
- break;
- case IS_ARRAY:
- tmp = (zend_hash_num_elements(Z_ARRVAL_P(op))?1:0);
- zval_dtor(op);
- Z_LVAL_P(op) = tmp;
- break;
- case IS_OBJECT:
- {
- zend_bool retval = 1;
- TSRMLS_FETCH();
- convert_object_to_type(op, IS_BOOL, convert_to_boolean);
- if (Z_TYPE_P(op) == IS_BOOL) {
- return;
- }
- zval_dtor(op);
- ZVAL_BOOL(op, retval);
- break;
- }
- default:
- zval_dtor(op);
- Z_LVAL_P(op) = 0;
- break;
- }
- Z_TYPE_P(op) = IS_BOOL;
- }
- /* }}} */
- ZEND_API void _convert_to_cstring(zval *op ZEND_FILE_LINE_DC) /* {{{ */
- {
- double dval;
- switch (Z_TYPE_P(op)) {
- case IS_DOUBLE: {
- TSRMLS_FETCH();
- dval = Z_DVAL_P(op);
- Z_STRLEN_P(op) = zend_spprintf(&Z_STRVAL_P(op), 0, "%.*H", (int) EG(precision), dval);
- /* %H already handles removing trailing zeros from the fractional part, yay */
- break;
- }
- default:
- _convert_to_string(op ZEND_FILE_LINE_CC);
- }
- Z_TYPE_P(op) = IS_STRING;
- }
- /* }}} */
- ZEND_API void _convert_to_string(zval *op ZEND_FILE_LINE_DC) /* {{{ */
- {
- long lval;
- double dval;
- switch (Z_TYPE_P(op)) {
- case IS_NULL:
- Z_STRVAL_P(op) = STR_EMPTY_ALLOC();
- Z_STRLEN_P(op) = 0;
- break;
- case IS_STRING:
- break;
- case IS_BOOL:
- if (Z_LVAL_P(op)) {
- Z_STRVAL_P(op) = estrndup_rel("1", 1);
- Z_STRLEN_P(op) = 1;
- } else {
- Z_STRVAL_P(op) = STR_EMPTY_ALLOC();
- Z_STRLEN_P(op) = 0;
- }
- break;
- case IS_RESOURCE: {
- long tmp = Z_LVAL_P(op);
- TSRMLS_FETCH();
- zend_list_delete(Z_LVAL_P(op));
- Z_STRLEN_P(op) = zend_spprintf(&Z_STRVAL_P(op), 0, "Resource id #%ld", tmp);
- break;
- }
- case IS_LONG:
- lval = Z_LVAL_P(op);
- Z_STRLEN_P(op) = zend_spprintf(&Z_STRVAL_P(op), 0, "%ld", lval);
- break;
- case IS_DOUBLE: {
- TSRMLS_FETCH();
- dval = Z_DVAL_P(op);
- Z_STRLEN_P(op) = zend_spprintf(&Z_STRVAL_P(op), 0, "%.*G", (int) EG(precision), dval);
- /* %G already handles removing trailing zeros from the fractional part, yay */
- break;
- }
- case IS_ARRAY:
- zend_error(E_NOTICE, "Array to string conversion");
- zval_dtor(op);
- Z_STRVAL_P(op) = estrndup_rel("Array", sizeof("Array")-1);
- Z_STRLEN_P(op) = sizeof("Array")-1;
- break;
- case IS_OBJECT: {
- TSRMLS_FETCH();
- convert_object_to_type(op, IS_STRING, convert_to_string);
- if (Z_TYPE_P(op) == IS_STRING) {
- return;
- }
- zend_error(E_NOTICE, "Object of class %s to string conversion", Z_OBJCE_P(op)->name);
- zval_dtor(op);
- Z_STRVAL_P(op) = estrndup_rel("Object", sizeof("Object")-1);
- Z_STRLEN_P(op) = sizeof("Object")-1;
- break;
- }
- default:
- zval_dtor(op);
- ZVAL_BOOL(op, 0);
- break;
- }
- Z_TYPE_P(op) = IS_STRING;
- }
- /* }}} */
- static void convert_scalar_to_array(zval *op, int type TSRMLS_DC) /* {{{ */
- {
- zval *entry;
- ALLOC_ZVAL(entry);
- *entry = *op;
- INIT_PZVAL(entry);
- switch (type) {
- case IS_ARRAY:
- ALLOC_HASHTABLE(Z_ARRVAL_P(op));
- zend_hash_init(Z_ARRVAL_P(op), 0, NULL, ZVAL_PTR_DTOR, 0);
- zend_hash_index_update(Z_ARRVAL_P(op), 0, (void *) &entry, sizeof(zval *), NULL);
- Z_TYPE_P(op) = IS_ARRAY;
- break;
- case IS_OBJECT:
- object_init(op);
- zend_hash_update(Z_OBJPROP_P(op), "scalar", sizeof("scalar"), (void *) &entry, sizeof(zval *), NULL);
- break;
- }
- }
- /* }}} */
- ZEND_API void convert_to_array(zval *op) /* {{{ */
- {
- TSRMLS_FETCH();
- switch (Z_TYPE_P(op)) {
- case IS_ARRAY:
- break;
- /* OBJECTS_OPTIMIZE */
- case IS_OBJECT:
- {
- zval *tmp;
- HashTable *ht;
- ALLOC_HASHTABLE(ht);
- zend_hash_init(ht, 0, NULL, ZVAL_PTR_DTOR, 0);
- if (Z_OBJCE_P(op) == zend_ce_closure) {
- convert_scalar_to_array(op, IS_ARRAY TSRMLS_CC);
- if (Z_TYPE_P(op) == IS_ARRAY) {
- zend_hash_destroy(ht);
- FREE_HASHTABLE(ht);
- return;
- }
- } else if (Z_OBJ_HT_P(op)->get_properties) {
- HashTable *obj_ht = Z_OBJ_HT_P(op)->get_properties(op TSRMLS_CC);
- if (obj_ht) {
- zend_hash_copy(ht, obj_ht, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
- }
- } else {
- convert_object_to_type(op, IS_ARRAY, convert_to_array);
- if (Z_TYPE_P(op) == IS_ARRAY) {
- zend_hash_destroy(ht);
- FREE_HASHTABLE(ht);
- return;
- }
- }
- zval_dtor(op);
- Z_TYPE_P(op) = IS_ARRAY;
- Z_ARRVAL_P(op) = ht;
- }
- break;
- case IS_NULL:
- ALLOC_HASHTABLE(Z_ARRVAL_P(op));
- zend_hash_init(Z_ARRVAL_P(op), 0, NULL, ZVAL_PTR_DTOR, 0);
- Z_TYPE_P(op) = IS_ARRAY;
- break;
- default:
- convert_scalar_to_array(op, IS_ARRAY TSRMLS_CC);
- break;
- }
- }
- /* }}} */
- ZEND_API void convert_to_object(zval *op) /* {{{ */
- {
- TSRMLS_FETCH();
- switch (Z_TYPE_P(op)) {
- case IS_ARRAY:
- {
- object_and_properties_init(op, zend_standard_class_def, Z_ARRVAL_P(op));
- break;
- }
- case IS_OBJECT:
- break;
- case IS_NULL:
- object_init(op);
- break;
- default:
- convert_scalar_to_array(op, IS_OBJECT TSRMLS_CC);
- break;
- }
- }
- /* }}} */
- ZEND_API void multi_convert_to_long_ex(int argc, ...) /* {{{ */
- {
- zval **arg;
- va_list ap;
- va_start(ap, argc);
- while (argc--) {
- arg = va_arg(ap, zval **);
- convert_to_long_ex(arg);
- }
- va_end(ap);
- }
- /* }}} */
- ZEND_API void multi_convert_to_double_ex(int argc, ...) /* {{{ */
- {
- zval **arg;
- va_list ap;
- va_start(ap, argc);
- while (argc--) {
- arg = va_arg(ap, zval **);
- convert_to_double_ex(arg);
- }
- va_end(ap);
- }
- /* }}} */
- ZEND_API void multi_convert_to_string_ex(int argc, ...) /* {{{ */
- {
- zval **arg;
- va_list ap;
- va_start(ap, argc);
- while (argc--) {
- arg = va_arg(ap, zval **);
- convert_to_string_ex(arg);
- }
- va_end(ap);
- }
- /* }}} */
- ZEND_API int add_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
- {
- zval op1_copy, op2_copy;
- int converted = 0;
- while (1) {
- switch (TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2))) {
- case TYPE_PAIR(IS_LONG, IS_LONG): {
- long lval = Z_LVAL_P(op1) + Z_LVAL_P(op2);
- /* check for overflow by comparing sign bits */
- if ((Z_LVAL_P(op1) & LONG_SIGN_MASK) == (Z_LVAL_P(op2) & LONG_SIGN_MASK)
- && (Z_LVAL_P(op1) & LONG_SIGN_MASK) != (lval & LONG_SIGN_MASK)) {
- ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) + (double) Z_LVAL_P(op2));
- } else {
- ZVAL_LONG(result, lval);
- }
- return SUCCESS;
- }
- case TYPE_PAIR(IS_LONG, IS_DOUBLE):
- ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) + Z_DVAL_P(op2));
- return SUCCESS;
- case TYPE_PAIR(IS_DOUBLE, IS_LONG):
- ZVAL_DOUBLE(result, Z_DVAL_P(op1) + ((double)Z_LVAL_P(op2)));
- return SUCCESS;
- case TYPE_PAIR(IS_DOUBLE, IS_DOUBLE):
- ZVAL_DOUBLE(result, Z_DVAL_P(op1) + Z_DVAL_P(op2));
- return SUCCESS;
- case TYPE_PAIR(IS_ARRAY, IS_ARRAY): {
- zval *tmp;
- if ((result == op1) && (result == op2)) {
- /* $a += $a */
- return SUCCESS;
- }
- if (result != op1) {
- *result = *op1;
- zval_copy_ctor(result);
- }
- zend_hash_merge(Z_ARRVAL_P(result), Z_ARRVAL_P(op2), (void (*)(void *pData)) zval_add_ref, (void *) &tmp, sizeof(zval *), 0);
- return SUCCESS;
- }
- default:
- if (!converted) {
- ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_ADD);
- zendi_convert_scalar_to_number(op1, op1_copy, result);
- zendi_convert_scalar_to_number(op2, op2_copy, result);
- converted = 1;
- } else {
- zend_error(E_ERROR, "Unsupported operand types");
- return FAILURE; /* unknown datatype */
- }
- }
- }
- }
- /* }}} */
- ZEND_API int sub_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
- {
- zval op1_copy, op2_copy;
- int converted = 0;
- while (1) {
- switch (TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2))) {
- case TYPE_PAIR(IS_LONG, IS_LONG): {
- long lval = Z_LVAL_P(op1) - Z_LVAL_P(op2);
- /* check for overflow by comparing sign bits */
- if ((Z_LVAL_P(op1) & LONG_SIGN_MASK) != (Z_LVAL_P(op2) & LONG_SIGN_MASK)
- && (Z_LVAL_P(op1) & LONG_SIGN_MASK) != (lval & LONG_SIGN_MASK)) {
- ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) - (double) Z_LVAL_P(op2));
- } else {
- ZVAL_LONG(result, lval);
- }
- return SUCCESS;
- }
- case TYPE_PAIR(IS_LONG, IS_DOUBLE):
- ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) - Z_DVAL_P(op2));
- return SUCCESS;
- case TYPE_PAIR(IS_DOUBLE, IS_LONG):
- ZVAL_DOUBLE(result, Z_DVAL_P(op1) - ((double)Z_LVAL_P(op2)));
- return SUCCESS;
- case TYPE_PAIR(IS_DOUBLE, IS_DOUBLE):
- ZVAL_DOUBLE(result, Z_DVAL_P(op1) - Z_DVAL_P(op2));
- return SUCCESS;
- default:
- if (!converted) {
- ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_SUB);
- zendi_convert_scalar_to_number(op1, op1_copy, result);
- zendi_convert_scalar_to_number(op2, op2_copy, result);
- converted = 1;
- } else {
- zend_error(E_ERROR, "Unsupported operand types");
- return FAILURE; /* unknown datatype */
- }
- }
- }
- }
- /* }}} */
- ZEND_API int mul_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
- {
- zval op1_copy, op2_copy;
- int converted = 0;
- while (1) {
- switch (TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2))) {
- case TYPE_PAIR(IS_LONG, IS_LONG): {
- long overflow;
- ZEND_SIGNED_MULTIPLY_LONG(Z_LVAL_P(op1),Z_LVAL_P(op2), Z_LVAL_P(result),Z_DVAL_P(result),overflow);
- Z_TYPE_P(result) = overflow ? IS_DOUBLE : IS_LONG;
- return SUCCESS;
- }
- case TYPE_PAIR(IS_LONG, IS_DOUBLE):
- ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) * Z_DVAL_P(op2));
- return SUCCESS;
- case TYPE_PAIR(IS_DOUBLE, IS_LONG):
- ZVAL_DOUBLE(result, Z_DVAL_P(op1) * ((double)Z_LVAL_P(op2)));
- return SUCCESS;
- case TYPE_PAIR(IS_DOUBLE, IS_DOUBLE):
- ZVAL_DOUBLE(result, Z_DVAL_P(op1) * Z_DVAL_P(op2));
- return SUCCESS;
- default:
- if (!converted) {
- ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_MUL);
- zendi_convert_scalar_to_number(op1, op1_copy, result);
- zendi_convert_scalar_to_number(op2, op2_copy, result);
- converted = 1;
- } else {
- zend_error(E_ERROR, "Unsupported operand types");
- return FAILURE; /* unknown datatype */
- }
- }
- }
- }
- /* }}} */
- ZEND_API int pow_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
- {
- zval op1_copy, op2_copy;
- int converted = 0;
- while (1) {
- switch (TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2))) {
- case TYPE_PAIR(IS_LONG, IS_LONG):
- if (Z_LVAL_P(op2) >= 0) {
- long l1 = 1, l2 = Z_LVAL_P(op1), i = Z_LVAL_P(op2);
- if (i == 0) {
- ZVAL_LONG(result, 1L);
- return SUCCESS;
- } else if (l2 == 0) {
- ZVAL_LONG(result, 0);
- return SUCCESS;
- }
- while (i >= 1) {
- long overflow;
- double dval = 0.0;
- if (i % 2) {
- --i;
- ZEND_SIGNED_MULTIPLY_LONG(l1, l2, l1, dval, overflow);
- if (overflow) {
- ZVAL_DOUBLE(result, dval * pow(l2, i));
- return SUCCESS;
- }
- } else {
- i /= 2;
- ZEND_SIGNED_MULTIPLY_LONG(l2, l2, l2, dval, overflow);
- if (overflow) {
- ZVAL_DOUBLE(result, (double)l1 * pow(dval, i));
- return SUCCESS;
- }
- }
- }
- /* i == 0 */
- ZVAL_LONG(result, l1);
- } else {
- ZVAL_DOUBLE(result, pow((double)Z_LVAL_P(op1), (double)Z_LVAL_P(op2)));
- }
- return SUCCESS;
- case TYPE_PAIR(IS_LONG, IS_DOUBLE):
- ZVAL_DOUBLE(result, pow((double)Z_LVAL_P(op1), Z_DVAL_P(op2)));
- return SUCCESS;
- case TYPE_PAIR(IS_DOUBLE, IS_LONG):
- ZVAL_DOUBLE(result, pow(Z_DVAL_P(op1), (double)Z_LVAL_P(op2)));
- return SUCCESS;
- case TYPE_PAIR(IS_DOUBLE, IS_DOUBLE):
- ZVAL_DOUBLE(result, pow(Z_DVAL_P(op1), Z_DVAL_P(op2)));
- return SUCCESS;
- default:
- if (!converted) {
- ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_POW);
- if (Z_TYPE_P(op1) == IS_ARRAY) {
- ZVAL_LONG(result, 0);
- return SUCCESS;
- } else {
- zendi_convert_scalar_to_number(op1, op1_copy, result);
- }
- if (Z_TYPE_P(op2) == IS_ARRAY) {
- ZVAL_LONG(result, 1L);
- return SUCCESS;
- } else {
- zendi_convert_scalar_to_number(op2, op2_copy, result);
- }
- converted = 1;
- } else {
- zend_error(E_ERROR, "Unsupported operand types");
- return FAILURE;
- }
- }
- }
- }
- /* }}} */
- ZEND_API int div_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
- {
- zval op1_copy, op2_copy;
- int converted = 0;
- while (1) {
- switch (TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2))) {
- case TYPE_PAIR(IS_LONG, IS_LONG):
- if (Z_LVAL_P(op2) == 0) {
- zend_error(E_WARNING, "Division by zero");
- ZVAL_BOOL(result, 0);
- return FAILURE; /* division by zero */
- } else if (Z_LVAL_P(op2) == -1 && Z_LVAL_P(op1) == LONG_MIN) {
- /* Prevent overflow error/crash */
- ZVAL_DOUBLE(result, (double) LONG_MIN / -1);
- return SUCCESS;
- }
- if (Z_LVAL_P(op1) % Z_LVAL_P(op2) == 0) { /* integer */
- ZVAL_LONG(result, Z_LVAL_P(op1) / Z_LVAL_P(op2));
- } else {
- ZVAL_DOUBLE(result, ((double) Z_LVAL_P(op1)) / Z_LVAL_P(op2));
- }
- return SUCCESS;
- case TYPE_PAIR(IS_DOUBLE, IS_LONG):
- if (Z_LVAL_P(op2) == 0) {
- zend_error(E_WARNING, "Division by zero");
- ZVAL_BOOL(result, 0);
- return FAILURE; /* division by zero */
- }
- ZVAL_DOUBLE(result, Z_DVAL_P(op1) / (double)Z_LVAL_P(op2));
- return SUCCESS;
- case TYPE_PAIR(IS_LONG, IS_DOUBLE):
- if (Z_DVAL_P(op2) == 0) {
- zend_error(E_WARNING, "Division by zero");
- ZVAL_BOOL(result, 0);
- return FAILURE; /* division by zero */
- }
- ZVAL_DOUBLE(result, (double)Z_LVAL_P(op1) / Z_DVAL_P(op2));
- return SUCCESS;
- case TYPE_PAIR(IS_DOUBLE, IS_DOUBLE):
- if (Z_DVAL_P(op2) == 0) {
- zend_error(E_WARNING, "Division by zero");
- ZVAL_BOOL(result, 0);
- return FAILURE; /* division by zero */
- }
- ZVAL_DOUBLE(result, Z_DVAL_P(op1) / Z_DVAL_P(op2));
- return SUCCESS;
- default:
- if (!converted) {
- ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_DIV);
- zendi_convert_scalar_to_number(op1, op1_copy, result);
- zendi_convert_scalar_to_number(op2, op2_copy, result);
- converted = 1;
- } else {
- zend_error(E_ERROR, "Unsupported operand types");
- return FAILURE; /* unknown datatype */
- }
- }
- }
- }
- /* }}} */
- ZEND_API int mod_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
- {
- zval op1_copy, op2_copy;
- long op1_lval;
- if (Z_TYPE_P(op1) != IS_LONG || Z_TYPE_P(op2) != IS_LONG) {
- ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_MOD);
- zendi_convert_to_long(op1, op1_copy, result);
- op1_lval = Z_LVAL_P(op1);
- zendi_convert_to_long(op2, op2_copy, result);
- } else {
- op1_lval = Z_LVAL_P(op1);
- }
- if (Z_LVAL_P(op2) == 0) {
- zend_error(E_WARNING, "Division by zero");
- ZVAL_BOOL(result, 0);
- return FAILURE; /* modulus by zero */
- }
- if (Z_LVAL_P(op2) == -1) {
- /* Prevent overflow error/crash if op1==LONG_MIN */
- ZVAL_LONG(result, 0);
- return SUCCESS;
- }
- ZVAL_LONG(result, op1_lval % Z_LVAL_P(op2));
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int boolean_xor_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
- {
- zval op1_copy, op2_copy;
- long op1_lval;
- if (Z_TYPE_P(op1) != IS_BOOL || Z_TYPE_P(op2) != IS_BOOL) {
- ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_BOOL_XOR);
- zendi_convert_to_boolean(op1, op1_copy, result);
- op1_lval = Z_LVAL_P(op1);
- zendi_convert_to_boolean(op2, op2_copy, result);
- } else {
- op1_lval = Z_LVAL_P(op1);
- }
- ZVAL_BOOL(result, op1_lval ^ Z_LVAL_P(op2));
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int boolean_not_function(zval *result, zval *op1 TSRMLS_DC) /* {{{ */
- {
- zval op1_copy;
- if (Z_TYPE_P(op1) != IS_BOOL) {
- ZEND_TRY_UNARY_OBJECT_OPERATION(ZEND_BOOL_NOT);
- zendi_convert_to_boolean(op1, op1_copy, result);
- }
- ZVAL_BOOL(result, !Z_LVAL_P(op1));
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int bitwise_not_function(zval *result, zval *op1 TSRMLS_DC) /* {{{ */
- {
- switch (Z_TYPE_P(op1)) {
- case IS_LONG:
- ZVAL_LONG(result, ~Z_LVAL_P(op1));
- return SUCCESS;
- case IS_DOUBLE:
- ZVAL_LONG(result, ~zend_dval_to_lval(Z_DVAL_P(op1)));
- return SUCCESS;
- case IS_STRING: {
- int i;
- zval op1_copy = *op1;
- Z_TYPE_P(result) = IS_STRING;
- Z_STRVAL_P(result) = estrndup(Z_STRVAL(op1_copy), Z_STRLEN(op1_copy));
- Z_STRLEN_P(result) = Z_STRLEN(op1_copy);
- for (i = 0; i < Z_STRLEN(op1_copy); i++) {
- Z_STRVAL_P(result)[i] = ~Z_STRVAL(op1_copy)[i];
- }
- return SUCCESS;
- }
- default:
- ZEND_TRY_UNARY_OBJECT_OPERATION(ZEND_BW_NOT);
- zend_error(E_ERROR, "Unsupported operand types");
- return FAILURE;
- }
- }
- /* }}} */
- ZEND_API int bitwise_or_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
- {
- zval op1_copy, op2_copy;
- long op1_lval;
- if (Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
- zval *longer, *shorter;
- char *result_str;
- int i, result_len;
- if (Z_STRLEN_P(op1) >= Z_STRLEN_P(op2)) {
- longer = op1;
- shorter = op2;
- } else {
- longer = op2;
- shorter = op1;
- }
- Z_TYPE_P(result) = IS_STRING;
- result_len = Z_STRLEN_P(longer);
- result_str = estrndup(Z_STRVAL_P(longer), Z_STRLEN_P(longer));
- for (i = 0; i < Z_STRLEN_P(shorter); i++) {
- result_str[i] |= Z_STRVAL_P(shorter)[i];
- }
- if (result==op1) {
- str_efree(Z_STRVAL_P(result));
- }
- Z_STRVAL_P(result) = result_str;
- Z_STRLEN_P(result) = result_len;
- return SUCCESS;
- }
- if (Z_TYPE_P(op1) != IS_LONG || Z_TYPE_P(op2) != IS_LONG) {
- ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_BW_OR);
- zendi_convert_to_long(op1, op1_copy, result);
- op1_lval = Z_LVAL_P(op1);
- zendi_convert_to_long(op2, op2_copy, result);
- } else {
- op1_lval = Z_LVAL_P(op1);
- }
- ZVAL_LONG(result, op1_lval | Z_LVAL_P(op2));
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int bitwise_and_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
- {
- zval op1_copy, op2_copy;
- long op1_lval;
- if (Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
- zval *longer, *shorter;
- char *result_str;
- int i, result_len;
- if (Z_STRLEN_P(op1) >= Z_STRLEN_P(op2)) {
- longer = op1;
- shorter = op2;
- } else {
- longer = op2;
- shorter = op1;
- }
- Z_TYPE_P(result) = IS_STRING;
- result_len = Z_STRLEN_P(shorter);
- result_str = estrndup(Z_STRVAL_P(shorter), Z_STRLEN_P(shorter));
- for (i = 0; i < Z_STRLEN_P(shorter); i++) {
- result_str[i] &= Z_STRVAL_P(longer)[i];
- }
- if (result==op1) {
- str_efree(Z_STRVAL_P(result));
- }
- Z_STRVAL_P(result) = result_str;
- Z_STRLEN_P(result) = result_len;
- return SUCCESS;
- }
- if (Z_TYPE_P(op1) != IS_LONG || Z_TYPE_P(op2) != IS_LONG) {
- ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_BW_AND);
- zendi_convert_to_long(op1, op1_copy, result);
- op1_lval = Z_LVAL_P(op1);
- zendi_convert_to_long(op2, op2_copy, result);
- } else {
- op1_lval = Z_LVAL_P(op1);
- }
- ZVAL_LONG(result, op1_lval & Z_LVAL_P(op2));
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int bitwise_xor_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
- {
- zval op1_copy, op2_copy;
- long op1_lval;
- if (Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
- zval *longer, *shorter;
- char *result_str;
- int i, result_len;
- if (Z_STRLEN_P(op1) >= Z_STRLEN_P(op2)) {
- longer = op1;
- shorter = op2;
- } else {
- longer = op2;
- shorter = op1;
- }
- Z_TYPE_P(result) = IS_STRING;
- result_len = Z_STRLEN_P(shorter);
- result_str = estrndup(Z_STRVAL_P(shorter), Z_STRLEN_P(shorter));
- for (i = 0; i < Z_STRLEN_P(shorter); i++) {
- result_str[i] ^= Z_STRVAL_P(longer)[i];
- }
- if (result==op1) {
- str_efree(Z_STRVAL_P(result));
- }
- Z_STRVAL_P(result) = result_str;
- Z_STRLEN_P(result) = result_len;
- return SUCCESS;
- }
- if (Z_TYPE_P(op1) != IS_LONG || Z_TYPE_P(op2) != IS_LONG) {
- ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_BW_XOR);
- zendi_convert_to_long(op1, op1_copy, result);
- op1_lval = Z_LVAL_P(op1);
- zendi_convert_to_long(op2, op2_copy, result);
- } else {
- op1_lval = Z_LVAL_P(op1);
- }
- ZVAL_LONG(result, op1_lval ^ Z_LVAL_P(op2));
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int shift_left_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
- {
- zval op1_copy, op2_copy;
- long op1_lval;
- if (Z_TYPE_P(op1) != IS_LONG || Z_TYPE_P(op2) != IS_LONG) {
- ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_SL);
- zendi_convert_to_long(op1, op1_copy, result);
- op1_lval = Z_LVAL_P(op1);
- zendi_convert_to_long(op2, op2_copy, result);
- } else {
- op1_lval = Z_LVAL_P(op1);
- }
- ZVAL_LONG(result, op1_lval << Z_LVAL_P(op2));
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int shift_right_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
- {
- zval op1_copy, op2_copy;
- long op1_lval;
- if (Z_TYPE_P(op1) != IS_LONG || Z_TYPE_P(op2) != IS_LONG) {
- ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_SR);
- zendi_convert_to_long(op1, op1_copy, result);
- op1_lval = Z_LVAL_P(op1);
- zendi_convert_to_long(op2, op2_copy, result);
- } else {
- op1_lval = Z_LVAL_P(op1);
- }
- ZVAL_LONG(result, op1_lval >> Z_LVAL_P(op2));
- return SUCCESS;
- }
- /* }}} */
- /* must support result==op1 */
- ZEND_API int add_char_to_string(zval *result, const zval *op1, const zval *op2) /* {{{ */
- {
- int length = Z_STRLEN_P(op1) + 1;
- char *buf;
- if (UNEXPECTED(length < 0)) {
- zend_error(E_ERROR, "String size overflow");
- }
- buf = str_erealloc(Z_STRVAL_P(op1), length + 1);
- buf[length - 1] = (char) Z_LVAL_P(op2);
- buf[length] = 0;
- ZVAL_STRINGL(result, buf, length, 0);
- return SUCCESS;
- }
- /* }}} */
- /* must support result==op1 */
- ZEND_API int add_string_to_string(zval *result, const zval *op1, const zval *op2) /* {{{ */
- {
- int length = Z_STRLEN_P(op1) + Z_STRLEN_P(op2);
- char *buf;
- if (UNEXPECTED(length < 0)) {
- zend_error(E_ERROR, "String size overflow");
- }
- buf = str_erealloc(Z_STRVAL_P(op1), length + 1);
- memcpy(buf + Z_STRLEN_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op2));
- buf[length] = 0;
- ZVAL_STRINGL(result, buf, length, 0);
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int concat_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
- {
- zval op1_copy, op2_copy;
- int use_copy1 = 0, use_copy2 = 0;
- if (Z_TYPE_P(op1) != IS_STRING || Z_TYPE_P(op2) != IS_STRING) {
- ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_CONCAT);
- if (Z_TYPE_P(op1) != IS_STRING) {
- zend_make_printable_zval(op1, &op1_copy, &use_copy1);
- }
- if (Z_TYPE_P(op2) != IS_STRING) {
- zend_make_printable_zval(op2, &op2_copy, &use_copy2);
- }
- }
- if (use_copy1) {
- /* We have created a converted copy of op1. Therefore, op1 won't become the result so
- * we have to free it.
- */
- if (result == op1) {
- zval_dtor(op1);
- }
- op1 = &op1_copy;
- }
- if (use_copy2) {
- op2 = &op2_copy;
- }
- if (result==op1 && !IS_INTERNED(Z_STRVAL_P(op1))) { /* special case, perform operations on result */
- uint res_len = Z_STRLEN_P(op1) + Z_STRLEN_P(op2);
- if (Z_STRLEN_P(result) < 0 || (int) (Z_STRLEN_P(op1) + Z_STRLEN_P(op2)) < 0) {
- efree(Z_STRVAL_P(result));
- ZVAL_EMPTY_STRING(result);
- zend_error(E_ERROR, "String size overflow");
- }
- Z_STRVAL_P(result) = safe_erealloc(Z_STRVAL_P(result), res_len, 1, 1);
- memcpy(Z_STRVAL_P(result)+Z_STRLEN_P(result), Z_STRVAL_P(op2), Z_STRLEN_P(op2));
- Z_STRVAL_P(result)[res_len]=0;
- Z_STRLEN_P(result) = res_len;
- } else {
- int length = Z_STRLEN_P(op1) + Z_STRLEN_P(op2);
- char *buf;
- if (Z_STRLEN_P(op1) < 0 || Z_STRLEN_P(op2) < 0 || (int) (Z_STRLEN_P(op1) + Z_STRLEN_P(op2)) < 0) {
- zend_error(E_ERROR, "String size overflow");
- }
- buf = (char *) safe_emalloc(length, 1, 1);
- memcpy(buf, Z_STRVAL_P(op1), Z_STRLEN_P(op1));
- memcpy(buf + Z_STRLEN_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op2));
- buf[length] = 0;
- ZVAL_STRINGL(result, buf, length, 0);
- }
- if (use_copy1) {
- zval_dtor(op1);
- }
- if (use_copy2) {
- zval_dtor(op2);
- }
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int string_compare_function_ex(zval *result, zval *op1, zval *op2, zend_bool case_insensitive TSRMLS_DC) /* {{{ */
- {
- zval op1_copy, op2_copy;
- int use_copy1 = 0, use_copy2 = 0;
- if (Z_TYPE_P(op1) != IS_STRING) {
- zend_make_printable_zval(op1, &op1_copy, &use_copy1);
- }
- if (Z_TYPE_P(op2) != IS_STRING) {
- zend_make_printable_zval(op2, &op2_copy, &use_copy2);
- }
- if (use_copy1) {
- op1 = &op1_copy;
- }
- if (use_copy2) {
- op2 = &op2_copy;
- }
- if (case_insensitive) {
- ZVAL_LONG(result, zend_binary_zval_strcasecmp(op1, op2));
- } else {
- ZVAL_LONG(result, zend_binary_zval_strcmp(op1, op2));
- }
- if (use_copy1) {
- zval_dtor(op1);
- }
- if (use_copy2) {
- zval_dtor(op2);
- }
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int string_compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
- {
- return string_compare_function_ex(result, op1, op2, 0 TSRMLS_CC);
- }
- /* }}} */
- ZEND_API int string_case_compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
- {
- return string_compare_function_ex(result, op1, op2, 1 TSRMLS_CC);
- }
- /* }}} */
- #if HAVE_STRCOLL
- ZEND_API int string_locale_compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
- {
- zval op1_copy, op2_copy;
- int use_copy1 = 0, use_copy2 = 0;
- if (Z_TYPE_P(op1) != IS_STRING) {
- zend_make_printable_zval(op1, &op1_copy, &use_copy1);
- }
- if (Z_TYPE_P(op2) != IS_STRING) {
- zend_make_printable_zval(op2, &op2_copy, &use_copy2);
- }
- if (use_copy1) {
- op1 = &op1_copy;
- }
- if (use_copy2) {
- op2 = &op2_copy;
- }
- ZVAL_LONG(result, strcoll(Z_STRVAL_P(op1), Z_STRVAL_P(op2)));
- if (use_copy1) {
- zval_dtor(op1);
- }
- if (use_copy2) {
- zval_dtor(op2);
- }
- return SUCCESS;
- }
- /* }}} */
- #endif
- ZEND_API int numeric_compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
- {
- zval op1_copy, op2_copy;
- op1_copy = *op1;
- zval_copy_ctor(&op1_copy);
- op2_copy = *op2;
- zval_copy_ctor(&op2_copy);
- convert_to_double(&op1_copy);
- convert_to_double(&op2_copy);
- ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(Z_DVAL(op1_copy)-Z_DVAL(op2_copy)));
- return SUCCESS;
- }
- /* }}} */
- static inline void zend_free_obj_get_result(zval *op TSRMLS_DC) /* {{{ */
- {
- if (Z_REFCOUNT_P(op) == 0) {
- GC_REMOVE_ZVAL_FROM_BUFFER(op);
- zval_dtor(op);
- FREE_ZVAL(op);
- } else {
- zval_ptr_dtor(&op);
- }
- }
- /* }}} */
- ZEND_API int compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
- {
- int ret;
- int converted = 0;
- zval op1_copy, op2_copy;
- zval *op_free;
- while (1) {
- switch (TYPE_PAIR(Z_TYPE_P(op1), Z_TYPE_P(op2))) {
- case TYPE_PAIR(IS_LONG, IS_LONG):
- ZVAL_LONG(result, Z_LVAL_P(op1)>Z_LVAL_P(op2)?1:(Z_LVAL_P(op1)<Z_LVAL_P(op2)?-1:0));
- return SUCCESS;
- case TYPE_PAIR(IS_DOUBLE, IS_LONG):
- Z_DVAL_P(result) = Z_DVAL_P(op1) - (double)Z_LVAL_P(op2);
- ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(Z_DVAL_P(result)));
- return SUCCESS;
- case TYPE_PAIR(IS_LONG, IS_DOUBLE):
- Z_DVAL_P(result) = (double)Z_LVAL_P(op1) - Z_DVAL_P(op2);
- ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(Z_DVAL_P(result)));
- return SUCCESS;
- case TYPE_PAIR(IS_DOUBLE, IS_DOUBLE):
- if (Z_DVAL_P(op1) == Z_DVAL_P(op2)) {
- ZVAL_LONG(result, 0);
- } else {
- Z_DVAL_P(result) = Z_DVAL_P(op1) - Z_DVAL_P(op2);
- ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(Z_DVAL_P(result)));
- }
- return SUCCESS;
- case TYPE_PAIR(IS_ARRAY, IS_ARRAY):
- zend_compare_arrays(result, op1, op2 TSRMLS_CC);
- return SUCCESS;
- case TYPE_PAIR(IS_NULL, IS_NULL):
- ZVAL_LONG(result, 0);
- return SUCCESS;
- case TYPE_PAIR(IS_NULL, IS_BOOL):
- ZVAL_LONG(result, Z_LVAL_P(op2) ? -1 : 0);
- return SUCCESS;
- case TYPE_PAIR(IS_BOOL, IS_NULL):
- ZVAL_LONG(result, Z_LVAL_P(op1) ? 1 : 0);
- return SUCCESS;
- case TYPE_PAIR(IS_BOOL, IS_BOOL):
- ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(Z_LVAL_P(op1) - Z_LVAL_P(op2)));
- return SUCCESS;
- case TYPE_PAIR(IS_STRING, IS_STRING):
- zendi_smart_strcmp(result, op1, op2);
- return SUCCESS;
- case TYPE_PAIR(IS_NULL, IS_STRING):
- ZVAL_LONG(result, zend_binary_strcmp("", 0, Z_STRVAL_P(op2), Z_STRLEN_P(op2)));
- return SUCCESS;
- case TYPE_PAIR(IS_STRING, IS_NULL):
- ZVAL_LONG(result, zend_binary_strcmp(Z_STRVAL_P(op1), Z_STRLEN_P(op1), "", 0));
- return SUCCESS;
- case TYPE_PAIR(IS_OBJECT, IS_NULL):
- ZVAL_LONG(result, 1);
- return SUCCESS;
- case TYPE_PAIR(IS_NULL, IS_OBJECT):
- ZVAL_LONG(result, -1);
- return SUCCESS;
- default:
- if (Z_TYPE_P(op1) == IS_OBJECT && Z_OBJ_HANDLER_P(op1, compare)) {
- return Z_OBJ_HANDLER_P(op1, compare)(result, op1, op2 TSRMLS_CC);
- } else if (Z_TYPE_P(op2) == IS_OBJECT && Z_OBJ_HANDLER_P(op2, compare)) {
- return Z_OBJ_HANDLER_P(op2, compare)(result, op1, op2 TSRMLS_CC);
- }
- if (Z_TYPE_P(op1) == IS_OBJECT && Z_TYPE_P(op2) == IS_OBJECT) {
- if (Z_OBJ_HANDLE_P(op1) == Z_OBJ_HANDLE_P(op2)) {
- /* object handles are identical, apparently this is the same object */
- ZVAL_LONG(result, 0);
- return SUCCESS;
- }
- if (Z_OBJ_HANDLER_P(op1, compare_objects) == Z_OBJ_HANDLER_P(op2, compare_objects)) {
- ZVAL_LONG(result, Z_OBJ_HANDLER_P(op1, compare_objects)(op1, op2 TSRMLS_CC));
- return SUCCESS;
- }
- }
- if (Z_TYPE_P(op1) == IS_OBJECT) {
- if (Z_OBJ_HT_P(op1)->get) {
- op_free = Z_OBJ_HT_P(op1)->get(op1 TSRMLS_CC);
- ret = compare_function(result, op_free, op2 TSRMLS_CC);
- zend_free_obj_get_result(op_free TSRMLS_CC);
- return ret;
- } else if (Z_TYPE_P(op2) != IS_OBJECT && Z_OBJ_HT_P(op1)->cast_object) {
- ALLOC_INIT_ZVAL(op_free);
- if (Z_OBJ_HT_P(op1)->cast_object(op1, op_free, Z_TYPE_P(op2) TSRMLS_CC) == FAILURE) {
- ZVAL_LONG(result, 1);
- zend_free_obj_get_result(op_free TSRMLS_CC);
- return SUCCESS;
- }
- ret = compare_function(result, op_free, op2 TSRMLS_CC);
- zend_free_obj_get_result(op_free TSRMLS_CC);
- return ret;
- }
- }
- if (Z_TYPE_P(op2) == IS_OBJECT) {
- if (Z_OBJ_HT_P(op2)->get) {
- op_free = Z_OBJ_HT_P(op2)->get(op2 TSRMLS_CC);
- ret = compare_function(result, op1, op_free TSRMLS_CC);
- zend_free_obj_get_result(op_free TSRMLS_CC);
- return ret;
- } else if (Z_TYPE_P(op1) != IS_OBJECT && Z_OBJ_HT_P(op2)->cast_object) {
- ALLOC_INIT_ZVAL(op_free);
- if (Z_OBJ_HT_P(op2)->cast_object(op2, op_free, Z_TYPE_P(op1) TSRMLS_CC) == FAILURE) {
- ZVAL_LONG(result, -1);
- zend_free_obj_get_result(op_free TSRMLS_CC);
- return SUCCESS;
- }
- ret = compare_function(result, op1, op_free TSRMLS_CC);
- zend_free_obj_get_result(op_free TSRMLS_CC);
- return ret;
- } else if (Z_TYPE_P(op1) == IS_OBJECT) {
- ZVAL_LONG(result, 1);
- return SUCCESS;
- }
- }
- if (!converted) {
- if (Z_TYPE_P(op1) == IS_NULL) {
- zendi_convert_to_boolean(op2, op2_copy, result);
- ZVAL_LONG(result, Z_LVAL_P(op2) ? -1 : 0);
- return SUCCESS;
- } else if (Z_TYPE_P(op2) == IS_NULL) {
- zendi_convert_to_boolean(op1, op1_copy, result);
- ZVAL_LONG(result, Z_LVAL_P(op1) ? 1 : 0);
- return SUCCESS;
- } else if (Z_TYPE_P(op1) == IS_BOOL) {
- zendi_convert_to_boolean(op2, op2_copy, result);
- ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(Z_LVAL_P(op1) - Z_LVAL_P(op2)));
- return SUCCESS;
- } else if (Z_TYPE_P(op2) == IS_BOOL) {
- zendi_convert_to_boolean(op1, op1_copy, result);
- ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(Z_LVAL_P(op1) - Z_LVAL_P(op2)));
- return SUCCESS;
- } else {
- zendi_convert_scalar_to_number(op1, op1_copy, result);
- zendi_convert_scalar_to_number(op2, op2_copy, result);
- converted = 1;
- }
- } else if (Z_TYPE_P(op1)==IS_ARRAY) {
- ZVAL_LONG(result, 1);
- return SUCCESS;
- } else if (Z_TYPE_P(op2)==IS_ARRAY) {
- ZVAL_LONG(result, -1);
- return SUCCESS;
- } else if (Z_TYPE_P(op1)==IS_OBJECT) {
- ZVAL_LONG(result, 1);
- return SUCCESS;
- } else if (Z_TYPE_P(op2)==IS_OBJECT) {
- ZVAL_LONG(result, -1);
- return SUCCESS;
- } else {
- ZVAL_LONG(result, 0);
- return FAILURE;
- }
- }
- }
- }
- /* }}} */
- static int hash_zval_identical_function(const zval **z1, const zval **z2) /* {{{ */
- {
- zval result;
- TSRMLS_FETCH();
- /* is_identical_function() returns 1 in case of identity and 0 in case
- * of a difference;
- * whereas this comparison function is expected to return 0 on identity,
- * and non zero otherwise.
- */
- if (is_identical_function(&result, (zval *) *z1, (zval *) *z2 TSRMLS_CC)==FAILURE) {
- return 1;
- }
- return !Z_LVAL(result);
- }
- /* }}} */
- ZEND_API int is_identical_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
- {
- Z_TYPE_P(result) = IS_BOOL;
- if (Z_TYPE_P(op1) != Z_TYPE_P(op2)) {
- Z_LVAL_P(result) = 0;
- return SUCCESS;
- }
- switch (Z_TYPE_P(op1)) {
- case IS_NULL:
- Z_LVAL_P(result) = 1;
- break;
- case IS_BOOL:
- case IS_LONG:
- case IS_RESOURCE:
- Z_LVAL_P(result) = (Z_LVAL_P(op1) == Z_LVAL_P(op2));
- break;
- case IS_DOUBLE:
- Z_LVAL_P(result) = (Z_DVAL_P(op1) == Z_DVAL_P(op2));
- break;
- case IS_STRING:
- Z_LVAL_P(result) = ((Z_STRLEN_P(op1) == Z_STRLEN_P(op2))
- && (!memcmp(Z_STRVAL_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op1))));
- break;
- case IS_ARRAY:
- Z_LVAL_P(result) = (Z_ARRVAL_P(op1) == Z_ARRVAL_P(op2) ||
- zend_hash_compare(Z_ARRVAL_P(op1), Z_ARRVAL_P(op2), (compare_func_t) hash_zval_identical_function, 1 TSRMLS_CC)==0);
- break;
- case IS_OBJECT:
- if (Z_OBJ_HT_P(op1) == Z_OBJ_HT_P(op2)) {
- Z_LVAL_P(result) = (Z_OBJ_HANDLE_P(op1) == Z_OBJ_HANDLE_P(op2));
- } else {
- Z_LVAL_P(result) = 0;
- }
- break;
- default:
- Z_LVAL_P(result) = 0;
- return FAILURE;
- }
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int is_not_identical_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
- {
- if (is_identical_function(result, op1, op2 TSRMLS_CC) == FAILURE) {
- return FAILURE;
- }
- Z_LVAL_P(result) = !Z_LVAL_P(result);
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int is_equal_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
- {
- if (compare_function(result, op1, op2 TSRMLS_CC) == FAILURE) {
- return FAILURE;
- }
- ZVAL_BOOL(result, (Z_LVAL_P(result) == 0));
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int is_not_equal_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
- {
- if (compare_function(result, op1, op2 TSRMLS_CC) == FAILURE) {
- return FAILURE;
- }
- ZVAL_BOOL(result, (Z_LVAL_P(result) != 0));
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int is_smaller_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
- {
- if (compare_function(result, op1, op2 TSRMLS_CC) == FAILURE) {
- return FAILURE;
- }
- ZVAL_BOOL(result, (Z_LVAL_P(result) < 0));
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int is_smaller_or_equal_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* {{{ */
- {
- if (compare_function(result, op1, op2 TSRMLS_CC) == FAILURE) {
- return FAILURE;
- }
- ZVAL_BOOL(result, (Z_LVAL_P(result) <= 0));
- return SUCCESS;
- }
- /* }}} */
- ZEND_API zend_bool instanceof_function_ex(const zend_class_entry *instance_ce, const zend_class_entry *ce, zend_bool interfaces_only TSRMLS_DC) /* {{{ */
- {
- zend_uint i;
- for (i=0; i<instance_ce->num_interfaces; i++) {
- if (instanceof_function(instance_ce->interfaces[i], ce TSRMLS_CC)) {
- return 1;
- }
- }
- if (!interfaces_only) {
- while (instance_ce) {
- if (instance_ce == ce) {
- return 1;
- }
- instance_ce = instance_ce->parent;
- }
- }
- return 0;
- }
- /* }}} */
- ZEND_API zend_bool instanceof_function(const zend_class_entry *instance_ce, const zend_class_entry *ce TSRMLS_DC) /* {{{ */
- {
- return instanceof_function_ex(instance_ce, ce, 0 TSRMLS_CC);
- }
- /* }}} */
- #define LOWER_CASE 1
- #define UPPER_CASE 2
- #define NUMERIC 3
- static void increment_string(zval *str) /* {{{ */
- {
- int carry=0;
- int pos=Z_STRLEN_P(str)-1;
- char *s=Z_STRVAL_P(str);
- char *t;
- int last=0; /* Shut up the compiler warning */
- int ch;
- if (Z_STRLEN_P(str) == 0) {
- str_efree(Z_STRVAL_P(str));
- Z_STRVAL_P(str) = estrndup("1", sizeof("1")-1);
- Z_STRLEN_P(str) = 1;
- return;
- }
- if (IS_INTERNED(s)) {
- Z_STRVAL_P(str) = s = estrndup(s, Z_STRLEN_P(str));
- }
- while (pos >= 0) {
- ch = s[pos];
- if (ch >= 'a' && ch <= 'z') {
- if (ch == 'z') {
- s[pos] = 'a';
- carry=1;
- } else {
- s[pos]++;
- carry=0;
- }
- last=LOWER_CASE;
- } else if (ch >= 'A' && ch <= 'Z') {
- if (ch == 'Z') {
- s[pos] = 'A';
- carry=1;
- } else {
- s[pos]++;
- carry=0;
- }
- last=UPPER_CASE;
- } else if (ch >= '0' && ch <= '9') {
- if (ch == '9') {
- s[pos] = '0';
- carry=1;
- } else {
- s[pos]++;
- carry=0;
- }
- last = NUMERIC;
- } else {
- carry=0;
- break;
- }
- if (carry == 0) {
- break;
- }
- pos--;
- }
- if (carry) {
- t = (char *) emalloc(Z_STRLEN_P(str)+1+1);
- memcpy(t+1, Z_STRVAL_P(str), Z_STRLEN_P(str));
- Z_STRLEN_P(str)++;
- t[Z_STRLEN_P(str)] = '\0';
- switch (last) {
- case NUMERIC:
- t[0] = '1';
- break;
- case UPPER_CASE:
- t[0] = 'A';
- break;
- case LOWER_CASE:
- t[0] = 'a';
- break;
- }
- str_efree(Z_STRVAL_P(str));
- Z_STRVAL_P(str) = t;
- }
- }
- /* }}} */
- ZEND_API int increment_function(zval *op1) /* {{{ */
- {
- switch (Z_TYPE_P(op1)) {
- case IS_LONG:
- if (Z_LVAL_P(op1) == LONG_MAX) {
- /* switch to double */
- double d = (double)Z_LVAL_P(op1);
- ZVAL_DOUBLE(op1, d+1);
- } else {
- Z_LVAL_P(op1)++;
- }
- break;
- case IS_DOUBLE:
- Z_DVAL_P(op1) = Z_DVAL_P(op1) + 1;
- break;
- case IS_NULL:
- ZVAL_LONG(op1, 1);
- break;
- case IS_STRING: {
- long lval;
- double dval;
- switch (is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), &lval, &dval, 0)) {
- case IS_LONG:
- str_efree(Z_STRVAL_P(op1));
- if (lval == LONG_MAX) {
- /* switch to double */
- double d = (double)lval;
- ZVAL_DOUBLE(op1, d+1);
- } else {
- ZVAL_LONG(op1, lval+1);
- }
- break;
- case IS_DOUBLE:
- str_efree(Z_STRVAL_P(op1));
- ZVAL_DOUBLE(op1, dval+1);
- break;
- default:
- /* Perl style string increment */
- increment_string(op1);
- break;
- }
- }
- break;
- case IS_OBJECT:
- if (Z_OBJ_HANDLER_P(op1, do_operation)) {
- zval *op2;
- int res;
- TSRMLS_FETCH();
- MAKE_STD_ZVAL(op2);
- ZVAL_LONG(op2, 1);
- res = Z_OBJ_HANDLER_P(op1, do_operation)(ZEND_ADD, op1, op1, op2 TSRMLS_CC);
- zval_ptr_dtor(&op2);
- return res;
- }
- return FAILURE;
- default:
- return FAILURE;
- }
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int decrement_function(zval *op1) /* {{{ */
- {
- long lval;
- double dval;
- switch (Z_TYPE_P(op1)) {
- case IS_LONG:
- if (Z_LVAL_P(op1) == LONG_MIN) {
- double d = (double)Z_LVAL_P(op1);
- ZVAL_DOUBLE(op1, d-1);
- } else {
- Z_LVAL_P(op1)--;
- }
- break;
- case IS_DOUBLE:
- Z_DVAL_P(op1) = Z_DVAL_P(op1) - 1;
- break;
- case IS_STRING: /* Like perl we only support string increment */
- if (Z_STRLEN_P(op1) == 0) { /* consider as 0 */
- str_efree(Z_STRVAL_P(op1));
- ZVAL_LONG(op1, -1);
- break;
- }
- switch (is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), &lval, &dval, 0)) {
- case IS_LONG:
- str_efree(Z_STRVAL_P(op1));
- if (lval == LONG_MIN) {
- double d = (double)lval;
- ZVAL_DOUBLE(op1, d-1);
- } else {
- ZVAL_LONG(op1, lval-1);
- }
- break;
- case IS_DOUBLE:
- str_efree(Z_STRVAL_P(op1));
- ZVAL_DOUBLE(op1, dval - 1);
- break;
- }
- break;
- case IS_OBJECT:
- if (Z_OBJ_HANDLER_P(op1, do_operation)) {
- zval *op2;
- int res;
- TSRMLS_FETCH();
- MAKE_STD_ZVAL(op2);
- ZVAL_LONG(op2, 1);
- res = Z_OBJ_HANDLER_P(op1, do_operation)(ZEND_SUB, op1, op1, op2 TSRMLS_CC);
- zval_ptr_dtor(&op2);
- return res;
- }
- return FAILURE;
- default:
- return FAILURE;
- }
- return SUCCESS;
- }
- /* }}} */
- ZEND_API int zval_is_true(zval *op) /* {{{ */
- {
- convert_to_boolean(op);
- return (Z_LVAL_P(op) ? 1 : 0);
- }
- /* }}} */
- #ifdef ZEND_USE_TOLOWER_L
- ZEND_API void zend_update_current_locale(void) /* {{{ */
- {
- current_locale = _get_current_locale();
- }
- /* }}} */
- #endif
- ZEND_API char *zend_str_tolower_copy(char *dest, const char *source, unsigned int length) /* {{{ */
- {
- register unsigned char *str = (unsigned char*)source;
- register unsigned char *result = (unsigned char*)dest;
- register unsigned char *end = str + length;
- while (str < end) {
- *result++ = zend_tolower_ascii(*str++);
- }
- *result = '\0';
- return dest;
- }
- /* }}} */
- ZEND_API char *zend_str_tolower_dup(const char *source, unsigned int length) /* {{{ */
- {
- return zend_str_tolower_copy((char *)emalloc(length+1), source, length);
- }
- /* }}} */
- ZEND_API void zend_str_tolower(char *str, unsigned int length) /* {{{ */
- {
- register unsigned char *p = (unsigned char*)str;
- register unsigned char *end = p + length;
- while (p < end) {
- *p = zend_tolower_ascii(*p);
- p++;
- }
- }
- /* }}} */
- ZEND_API int zend_binary_strcmp(const char *s1, uint len1, const char *s2, uint len2) /* {{{ */
- {
- int retval;
- if (s1 == s2) {
- return 0;
- }
- retval = memcmp(s1, s2, MIN(len1, len2));
- if (!retval) {
- return (len1 - len2);
- } else {
- return retval;
- }
- }
- /* }}} */
- ZEND_API int zend_binary_strncmp(const char *s1, uint len1, const char *s2, uint len2, uint length) /* {{{ */
- {
- int retval;
- if (s1 == s2) {
- return 0;
- }
- retval = memcmp(s1, s2, MIN(length, MIN(len1, len2)));
- if (!retval) {
- return (MIN(length, len1) - MIN(length, len2));
- } else {
- return retval;
- }
- }
- /* }}} */
- ZEND_API int zend_binary_strcasecmp(const char *s1, uint len1, const char *s2, uint len2) /* {{{ */
- {
- int len;
- int c1, c2;
- if (s1 == s2) {
- return 0;
- }
- len = MIN(len1, len2);
- while (len--) {
- c1 = zend_tolower_ascii(*(unsigned char *)s1++);
- c2 = zend_tolower_ascii(*(unsigned char *)s2++);
- if (c1 != c2) {
- return c1 - c2;
- }
- }
- return len1 - len2;
- }
- /* }}} */
- ZEND_API int zend_binary_strncasecmp(const char *s1, uint len1, const char *s2, uint len2, uint length) /* {{{ */
- {
- int len;
- int c1, c2;
- if (s1 == s2) {
- return 0;
- }
- len = MIN(length, MIN(len1, len2));
- while (len--) {
- c1 = zend_tolower_ascii(*(unsigned char *)s1++);
- c2 = zend_tolower_ascii(*(unsigned char *)s2++);
- if (c1 != c2) {
- return c1 - c2;
- }
- }
- return MIN(length, len1) - MIN(length, len2);
- }
- /* }}} */
- ZEND_API int zend_binary_strcasecmp_l(const char *s1, uint len1, const char *s2, uint len2) /* {{{ */
- {
- int len;
- int c1, c2;
- if (s1 == s2) {
- return 0;
- }
- len = MIN(len1, len2);
- while (len--) {
- c1 = zend_tolower((int)*(unsigned char *)s1++);
- c2 = zend_tolower((int)*(unsigned char *)s2++);
- if (c1 != c2) {
- return c1 - c2;
- }
- }
- return len1 - len2;
- }
- /* }}} */
- ZEND_API int zend_binary_strncasecmp_l(const char *s1, uint len1, const char *s2, uint len2, uint length) /* {{{ */
- {
- int len;
- int c1, c2;
- if (s1 == s2) {
- return 0;
- }
- len = MIN(length, MIN(len1, len2));
- while (len--) {
- c1 = zend_tolower((int)*(unsigned char *)s1++);
- c2 = zend_tolower((int)*(unsigned char *)s2++);
- if (c1 != c2) {
- return c1 - c2;
- }
- }
- return MIN(length, len1) - MIN(length, len2);
- }
- /* }}} */
- ZEND_API int zend_binary_zval_strcmp(zval *s1, zval *s2) /* {{{ */
- {
- return zend_binary_strcmp(Z_STRVAL_P(s1), Z_STRLEN_P(s1), Z_STRVAL_P(s2), Z_STRLEN_P(s2));
- }
- /* }}} */
- ZEND_API int zend_binary_zval_strncmp(zval *s1, zval *s2, zval *s3) /* {{{ */
- {
- return zend_binary_strncmp(Z_STRVAL_P(s1), Z_STRLEN_P(s1), Z_STRVAL_P(s2), Z_STRLEN_P(s2), Z_LVAL_P(s3));
- }
- /* }}} */
- ZEND_API int zend_binary_zval_strcasecmp(zval *s1, zval *s2) /* {{{ */
- {
- return zend_binary_strcasecmp_l(Z_STRVAL_P(s1), Z_STRLEN_P(s1), Z_STRVAL_P(s2), Z_STRLEN_P(s2));
- }
- /* }}} */
- ZEND_API int zend_binary_zval_strncasecmp(zval *s1, zval *s2, zval *s3) /* {{{ */
- {
- 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));
- }
- /* }}} */
- ZEND_API void zendi_smart_strcmp(zval *result, zval *s1, zval *s2) /* {{{ */
- {
- int ret1, ret2;
- int oflow1, oflow2;
- long lval1 = 0, lval2 = 0;
- double dval1 = 0.0, dval2 = 0.0;
- if ((ret1=is_numeric_string_ex(Z_STRVAL_P(s1), Z_STRLEN_P(s1), &lval1, &dval1, 0, &oflow1)) &&
- (ret2=is_numeric_string_ex(Z_STRVAL_P(s2), Z_STRLEN_P(s2), &lval2, &dval2, 0, &oflow2))) {
- #if ULONG_MAX == 0xFFFFFFFF
- if (oflow1 != 0 && oflow1 == oflow2 && dval1 - dval2 == 0. &&
- ((oflow1 == 1 && dval1 > 9007199254740991. /*0x1FFFFFFFFFFFFF*/)
- || (oflow1 == -1 && dval1 < -9007199254740991.))) {
- #else
- if (oflow1 != 0 && oflow1 == oflow2 && dval1 - dval2 == 0.) {
- #endif
- /* both values are integers overflown to the same side, and the
- * double comparison may have resulted in crucial accuracy lost */
- goto string_cmp;
- }
- if ((ret1==IS_DOUBLE) || (ret2==IS_DOUBLE)) {
- if (ret1!=IS_DOUBLE) {
- if (oflow2) {
- /* 2nd operand is integer > LONG_MAX (oflow2==1) or < LONG_MIN (-1) */
- ZVAL_LONG(result, -1 * oflow2);
- return;
- }
- dval1 = (double) lval1;
- } else if (ret2!=IS_DOUBLE) {
- if (oflow1) {
- ZVAL_LONG(result, oflow1);
- return;
- }
- dval2 = (double) lval2;
- } else if (dval1 == dval2 && !zend_finite(dval1)) {
- /* Both values overflowed and have the same sign,
- * so a numeric comparison would be inaccurate */
- goto string_cmp;
- }
- Z_DVAL_P(result) = dval1 - dval2;
- ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(Z_DVAL_P(result)));
- } else { /* they both have to be long's */
- ZVAL_LONG(result, lval1 > lval2 ? 1 : (lval1 < lval2 ? -1 : 0));
- }
- } else {
- string_cmp:
- Z_LVAL_P(result) = zend_binary_zval_strcmp(s1, s2);
- ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(Z_LVAL_P(result)));
- }
- }
- /* }}} */
- static int hash_zval_compare_function(const zval **z1, const zval **z2 TSRMLS_DC) /* {{{ */
- {
- zval result;
- if (compare_function(&result, (zval *) *z1, (zval *) *z2 TSRMLS_CC)==FAILURE) {
- return 1;
- }
- return Z_LVAL(result);
- }
- /* }}} */
- ZEND_API int zend_compare_symbol_tables_i(HashTable *ht1, HashTable *ht2 TSRMLS_DC) /* {{{ */
- {
- return ht1 == ht2 ? 0 : zend_hash_compare(ht1, ht2, (compare_func_t) hash_zval_compare_function, 0 TSRMLS_CC);
- }
- /* }}} */
- ZEND_API void zend_compare_symbol_tables(zval *result, HashTable *ht1, HashTable *ht2 TSRMLS_DC) /* {{{ */
- {
- ZVAL_LONG(result, ht1 == ht2 ? 0 : zend_hash_compare(ht1, ht2, (compare_func_t) hash_zval_compare_function, 0 TSRMLS_CC));
- }
- /* }}} */
- ZEND_API void zend_compare_arrays(zval *result, zval *a1, zval *a2 TSRMLS_DC) /* {{{ */
- {
- zend_compare_symbol_tables(result, Z_ARRVAL_P(a1), Z_ARRVAL_P(a2) TSRMLS_CC);
- }
- /* }}} */
- ZEND_API void zend_compare_objects(zval *result, zval *o1, zval *o2 TSRMLS_DC) /* {{{ */
- {
- Z_TYPE_P(result) = IS_LONG;
- if (Z_OBJ_HANDLE_P(o1) == Z_OBJ_HANDLE_P(o2)) {
- Z_LVAL_P(result) = 0;
- return;
- }
- if (Z_OBJ_HT_P(o1)->compare_objects == NULL) {
- Z_LVAL_P(result) = 1;
- } else {
- Z_LVAL_P(result) = Z_OBJ_HT_P(o1)->compare_objects(o1, o2 TSRMLS_CC);
- }
- }
- /* }}} */
- ZEND_API void zend_locale_sprintf_double(zval *op ZEND_FILE_LINE_DC) /* {{{ */
- {
- TSRMLS_FETCH();
- Z_STRLEN_P(op) = zend_spprintf(&Z_STRVAL_P(op), 0, "%.*G", (int) EG(precision), (double)Z_DVAL_P(op));
- }
- /* }}} */
- /*
- * Local variables:
- * tab-width: 4
- * c-basic-offset: 4
- * indent-tabs-mode: t
- * End:
- */
|