zend_API.c 127 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2018 Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Andi Gutmans <andi@php.net> |
  16. | Zeev Suraski <zeev@php.net> |
  17. | Andrei Zmievski <andrei@php.net> |
  18. | Dmitry Stogov <dmitry@php.net> |
  19. +----------------------------------------------------------------------+
  20. */
  21. #include "zend.h"
  22. #include "zend_execute.h"
  23. #include "zend_API.h"
  24. #include "zend_modules.h"
  25. #include "zend_extensions.h"
  26. #include "zend_constants.h"
  27. #include "zend_interfaces.h"
  28. #include "zend_exceptions.h"
  29. #include "zend_closures.h"
  30. #include "zend_inheritance.h"
  31. #ifdef HAVE_STDARG_H
  32. #include <stdarg.h>
  33. #endif
  34. /* these variables are true statics/globals, and have to be mutex'ed on every access */
  35. ZEND_API HashTable module_registry;
  36. static zend_module_entry **module_request_startup_handlers;
  37. static zend_module_entry **module_request_shutdown_handlers;
  38. static zend_module_entry **module_post_deactivate_handlers;
  39. static zend_class_entry **class_cleanup_handlers;
  40. ZEND_API int _zend_get_parameters_array_ex(int param_count, zval *argument_array) /* {{{ */
  41. {
  42. zval *param_ptr;
  43. int arg_count;
  44. param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
  45. arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
  46. if (param_count>arg_count) {
  47. return FAILURE;
  48. }
  49. while (param_count-->0) {
  50. ZVAL_COPY_VALUE(argument_array, param_ptr);
  51. argument_array++;
  52. param_ptr++;
  53. }
  54. return SUCCESS;
  55. }
  56. /* }}} */
  57. ZEND_API int zend_copy_parameters_array(int param_count, zval *argument_array) /* {{{ */
  58. {
  59. zval *param_ptr;
  60. int arg_count;
  61. param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
  62. arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
  63. if (param_count>arg_count) {
  64. return FAILURE;
  65. }
  66. while (param_count-->0) {
  67. Z_TRY_ADDREF_P(param_ptr);
  68. zend_hash_next_index_insert_new(Z_ARRVAL_P(argument_array), param_ptr);
  69. param_ptr++;
  70. }
  71. return SUCCESS;
  72. }
  73. /* }}} */
  74. ZEND_API ZEND_COLD void zend_wrong_param_count(void) /* {{{ */
  75. {
  76. const char *space;
  77. const char *class_name = get_active_class_name(&space);
  78. zend_internal_argument_count_error(ZEND_ARG_USES_STRICT_TYPES(), "Wrong parameter count for %s%s%s()", class_name, space, get_active_function_name());
  79. }
  80. /* }}} */
  81. /* Argument parsing API -- andrei */
  82. ZEND_API char *zend_get_type_by_const(int type) /* {{{ */
  83. {
  84. switch(type) {
  85. case IS_FALSE:
  86. case IS_TRUE:
  87. case _IS_BOOL:
  88. return "bool";
  89. case IS_LONG:
  90. return "int";
  91. case IS_DOUBLE:
  92. return "float";
  93. case IS_STRING:
  94. return "string";
  95. case IS_OBJECT:
  96. return "object";
  97. case IS_RESOURCE:
  98. return "resource";
  99. case IS_NULL:
  100. return "null";
  101. case IS_CALLABLE:
  102. return "callable";
  103. case IS_ITERABLE:
  104. return "iterable";
  105. case IS_ARRAY:
  106. return "array";
  107. case IS_VOID:
  108. return "void";
  109. case _IS_NUMBER:
  110. return "number";
  111. default:
  112. return "unknown";
  113. }
  114. }
  115. /* }}} */
  116. ZEND_API char *zend_zval_type_name(const zval *arg) /* {{{ */
  117. {
  118. ZVAL_DEREF(arg);
  119. return zend_get_type_by_const(Z_TYPE_P(arg));
  120. }
  121. /* }}} */
  122. ZEND_API zend_string *zend_zval_get_type(const zval *arg) /* {{{ */
  123. {
  124. switch (Z_TYPE_P(arg)) {
  125. case IS_NULL:
  126. return ZSTR_KNOWN(ZEND_STR_NULL);
  127. case IS_FALSE:
  128. case IS_TRUE:
  129. return ZSTR_KNOWN(ZEND_STR_BOOLEAN);
  130. case IS_LONG:
  131. return ZSTR_KNOWN(ZEND_STR_INTEGER);
  132. case IS_DOUBLE:
  133. return ZSTR_KNOWN(ZEND_STR_DOUBLE);
  134. case IS_STRING:
  135. return ZSTR_KNOWN(ZEND_STR_STRING);
  136. case IS_ARRAY:
  137. return ZSTR_KNOWN(ZEND_STR_ARRAY);
  138. case IS_OBJECT:
  139. return ZSTR_KNOWN(ZEND_STR_OBJECT);
  140. case IS_RESOURCE:
  141. if (zend_rsrc_list_get_rsrc_type(Z_RES_P(arg))) {
  142. return ZSTR_KNOWN(ZEND_STR_RESOURCE);
  143. } else {
  144. return ZSTR_KNOWN(ZEND_STR_CLOSED_RESOURCE);
  145. }
  146. default:
  147. return NULL;
  148. }
  149. }
  150. /* }}} */
  151. ZEND_API ZEND_COLD int ZEND_FASTCALL zend_wrong_parameters_none_error(void) /* {{{ */
  152. {
  153. int num_args = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
  154. zend_function *active_function = EG(current_execute_data)->func;
  155. const char *class_name = active_function->common.scope ? ZSTR_VAL(active_function->common.scope->name) : "";
  156. zend_internal_argument_count_error(
  157. ZEND_ARG_USES_STRICT_TYPES(),
  158. "%s%s%s() expects %s %d parameter%s, %d given",
  159. class_name, \
  160. class_name[0] ? "::" : "", \
  161. ZSTR_VAL(active_function->common.function_name),
  162. "exactly",
  163. 0,
  164. "s",
  165. num_args);
  166. return FAILURE;
  167. }
  168. /* }}} */
  169. ZEND_API ZEND_COLD int ZEND_FASTCALL zend_wrong_parameters_none_exception(void) /* {{{ */
  170. {
  171. int num_args = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
  172. zend_function *active_function = EG(current_execute_data)->func;
  173. const char *class_name = active_function->common.scope ? ZSTR_VAL(active_function->common.scope->name) : "";
  174. zend_internal_argument_count_error(
  175. 1,
  176. "%s%s%s() expects %s %d parameter%s, %d given",
  177. class_name, \
  178. class_name[0] ? "::" : "", \
  179. ZSTR_VAL(active_function->common.function_name),
  180. "exactly",
  181. 0,
  182. "s",
  183. num_args);
  184. return FAILURE;
  185. }
  186. /* }}} */
  187. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameters_count_error(int min_num_args, int max_num_args) /* {{{ */
  188. {
  189. int num_args = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
  190. zend_function *active_function = EG(current_execute_data)->func;
  191. const char *class_name = active_function->common.scope ? ZSTR_VAL(active_function->common.scope->name) : "";
  192. zend_internal_argument_count_error(
  193. ZEND_ARG_USES_STRICT_TYPES(),
  194. "%s%s%s() expects %s %d parameter%s, %d given",
  195. class_name, \
  196. class_name[0] ? "::" : "", \
  197. ZSTR_VAL(active_function->common.function_name),
  198. min_num_args == max_num_args ? "exactly" : num_args < min_num_args ? "at least" : "at most",
  199. num_args < min_num_args ? min_num_args : max_num_args,
  200. (num_args < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
  201. num_args);
  202. }
  203. /* }}} */
  204. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameters_count_exception(int min_num_args, int max_num_args) /* {{{ */
  205. {
  206. int num_args = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
  207. zend_function *active_function = EG(current_execute_data)->func;
  208. const char *class_name = active_function->common.scope ? ZSTR_VAL(active_function->common.scope->name) : "";
  209. zend_internal_argument_count_error(
  210. 1,
  211. "%s%s%s() expects %s %d parameter%s, %d given",
  212. class_name, \
  213. class_name[0] ? "::" : "", \
  214. ZSTR_VAL(active_function->common.function_name),
  215. min_num_args == max_num_args ? "exactly" : num_args < min_num_args ? "at least" : "at most",
  216. num_args < min_num_args ? min_num_args : max_num_args,
  217. (num_args < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
  218. num_args);
  219. }
  220. /* }}} */
  221. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_type_error(int num, zend_expected_type expected_type, zval *arg) /* {{{ */
  222. {
  223. const char *space;
  224. const char *class_name = get_active_class_name(&space);
  225. static const char * const expected_error[] = {
  226. Z_EXPECTED_TYPES(Z_EXPECTED_TYPE_STR)
  227. NULL
  228. };
  229. zend_internal_type_error(ZEND_ARG_USES_STRICT_TYPES(), "%s%s%s() expects parameter %d to be %s, %s given",
  230. class_name, space, get_active_function_name(), num, expected_error[expected_type], zend_zval_type_name(arg));
  231. }
  232. /* }}} */
  233. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_type_exception(int num, zend_expected_type expected_type, zval *arg) /* {{{ */
  234. {
  235. const char *space;
  236. const char *class_name = get_active_class_name(&space);
  237. static const char * const expected_error[] = {
  238. Z_EXPECTED_TYPES(Z_EXPECTED_TYPE_STR)
  239. NULL
  240. };
  241. zend_internal_type_error(1, "%s%s%s() expects parameter %d to be %s, %s given",
  242. class_name, space, get_active_function_name(), num, expected_error[expected_type], zend_zval_type_name(arg));
  243. }
  244. /* }}} */
  245. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_error(int num, char *name, zval *arg) /* {{{ */
  246. {
  247. const char *space;
  248. const char *class_name = get_active_class_name(&space);
  249. zend_internal_type_error(ZEND_ARG_USES_STRICT_TYPES(), "%s%s%s() expects parameter %d to be %s, %s given",
  250. class_name, space, get_active_function_name(), num, name, zend_zval_type_name(arg));
  251. }
  252. /* }}} */
  253. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_exception(int num, char *name, zval *arg) /* {{{ */
  254. {
  255. const char *space;
  256. const char *class_name = get_active_class_name(&space);
  257. zend_internal_type_error(1, "%s%s%s() expects parameter %d to be %s, %s given",
  258. class_name, space, get_active_function_name(), num, name, zend_zval_type_name(arg));
  259. }
  260. /* }}} */
  261. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_callback_error(int num, char *error) /* {{{ */
  262. {
  263. const char *space;
  264. const char *class_name = get_active_class_name(&space);
  265. zend_internal_type_error(ZEND_ARG_USES_STRICT_TYPES(), "%s%s%s() expects parameter %d to be a valid callback, %s",
  266. class_name, space, get_active_function_name(), num, error);
  267. efree(error);
  268. }
  269. /* }}} */
  270. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_callback_exception(int num, char *error) /* {{{ */
  271. {
  272. const char *space;
  273. const char *class_name = get_active_class_name(&space);
  274. zend_internal_type_error(1, "%s%s%s() expects parameter %d to be a valid callback, %s",
  275. class_name, space, get_active_function_name(), num, error);
  276. efree(error);
  277. }
  278. /* }}} */
  279. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_callback_deprecated(int num, char *error) /* {{{ */
  280. {
  281. const char *space;
  282. const char *class_name = get_active_class_name(&space);
  283. zend_error(E_DEPRECATED, "%s%s%s() expects parameter %d to be a valid callback, %s",
  284. class_name, space, get_active_function_name(), num, error);
  285. efree(error);
  286. }
  287. /* }}} */
  288. ZEND_API int ZEND_FASTCALL zend_parse_arg_class(zval *arg, zend_class_entry **pce, int num, int check_null) /* {{{ */
  289. {
  290. zend_class_entry *ce_base = *pce;
  291. if (check_null && Z_TYPE_P(arg) == IS_NULL) {
  292. *pce = NULL;
  293. return 1;
  294. }
  295. convert_to_string_ex(arg);
  296. *pce = zend_lookup_class(Z_STR_P(arg));
  297. if (ce_base) {
  298. if ((!*pce || !instanceof_function(*pce, ce_base))) {
  299. const char *space;
  300. const char *class_name = get_active_class_name(&space);
  301. zend_internal_type_error(ZEND_ARG_USES_STRICT_TYPES(), "%s%s%s() expects parameter %d to be a class name derived from %s, '%s' given",
  302. class_name, space, get_active_function_name(), num,
  303. ZSTR_VAL(ce_base->name), Z_STRVAL_P(arg));
  304. *pce = NULL;
  305. return 0;
  306. }
  307. }
  308. if (!*pce) {
  309. const char *space;
  310. const char *class_name = get_active_class_name(&space);
  311. zend_internal_type_error(ZEND_ARG_USES_STRICT_TYPES(), "%s%s%s() expects parameter %d to be a valid class name, '%s' given",
  312. class_name, space, get_active_function_name(), num,
  313. Z_STRVAL_P(arg));
  314. return 0;
  315. }
  316. return 1;
  317. }
  318. /* }}} */
  319. ZEND_API int ZEND_FASTCALL zend_parse_arg_bool_weak(zval *arg, zend_bool *dest) /* {{{ */
  320. {
  321. if (EXPECTED(Z_TYPE_P(arg) <= IS_STRING)) {
  322. *dest = zend_is_true(arg);
  323. } else {
  324. return 0;
  325. }
  326. return 1;
  327. }
  328. /* }}} */
  329. ZEND_API int ZEND_FASTCALL zend_parse_arg_bool_slow(zval *arg, zend_bool *dest) /* {{{ */
  330. {
  331. if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
  332. return 0;
  333. }
  334. return zend_parse_arg_bool_weak(arg, dest);
  335. }
  336. /* }}} */
  337. ZEND_API int ZEND_FASTCALL zend_parse_arg_long_weak(zval *arg, zend_long *dest) /* {{{ */
  338. {
  339. if (EXPECTED(Z_TYPE_P(arg) == IS_DOUBLE)) {
  340. if (UNEXPECTED(zend_isnan(Z_DVAL_P(arg)))) {
  341. return 0;
  342. }
  343. if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(Z_DVAL_P(arg)))) {
  344. return 0;
  345. } else {
  346. *dest = zend_dval_to_lval(Z_DVAL_P(arg));
  347. }
  348. } else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
  349. double d;
  350. int type;
  351. if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), dest, &d)) != IS_LONG)) {
  352. if (EXPECTED(type != 0)) {
  353. if (UNEXPECTED(zend_isnan(d))) {
  354. return 0;
  355. }
  356. if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(d))) {
  357. return 0;
  358. } else {
  359. *dest = zend_dval_to_lval(d);
  360. }
  361. } else {
  362. return 0;
  363. }
  364. }
  365. } else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
  366. *dest = 0;
  367. } else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
  368. *dest = 1;
  369. } else {
  370. return 0;
  371. }
  372. return 1;
  373. }
  374. /* }}} */
  375. ZEND_API int ZEND_FASTCALL zend_parse_arg_long_slow(zval *arg, zend_long *dest) /* {{{ */
  376. {
  377. if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
  378. return 0;
  379. }
  380. return zend_parse_arg_long_weak(arg, dest);
  381. }
  382. /* }}} */
  383. ZEND_API int ZEND_FASTCALL zend_parse_arg_long_cap_weak(zval *arg, zend_long *dest) /* {{{ */
  384. {
  385. if (EXPECTED(Z_TYPE_P(arg) == IS_DOUBLE)) {
  386. if (UNEXPECTED(zend_isnan(Z_DVAL_P(arg)))) {
  387. return 0;
  388. }
  389. *dest = zend_dval_to_lval_cap(Z_DVAL_P(arg));
  390. } else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
  391. double d;
  392. int type;
  393. if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), dest, &d)) != IS_LONG)) {
  394. if (EXPECTED(type != 0)) {
  395. if (UNEXPECTED(zend_isnan(d))) {
  396. return 0;
  397. }
  398. *dest = zend_dval_to_lval_cap(d);
  399. } else {
  400. return 0;
  401. }
  402. }
  403. } else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
  404. *dest = 0;
  405. } else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
  406. *dest = 1;
  407. } else {
  408. return 0;
  409. }
  410. return 1;
  411. }
  412. /* }}} */
  413. ZEND_API int ZEND_FASTCALL zend_parse_arg_long_cap_slow(zval *arg, zend_long *dest) /* {{{ */
  414. {
  415. if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
  416. return 0;
  417. }
  418. return zend_parse_arg_long_cap_weak(arg, dest);
  419. }
  420. /* }}} */
  421. ZEND_API int ZEND_FASTCALL zend_parse_arg_double_weak(zval *arg, double *dest) /* {{{ */
  422. {
  423. if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
  424. *dest = (double)Z_LVAL_P(arg);
  425. } else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
  426. zend_long l;
  427. int type;
  428. if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), &l, dest)) != IS_DOUBLE)) {
  429. if (EXPECTED(type != 0)) {
  430. *dest = (double)(l);
  431. } else {
  432. return 0;
  433. }
  434. }
  435. } else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
  436. *dest = 0.0;
  437. } else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
  438. *dest = 1.0;
  439. } else {
  440. return 0;
  441. }
  442. return 1;
  443. }
  444. /* }}} */
  445. ZEND_API int ZEND_FASTCALL zend_parse_arg_double_slow(zval *arg, double *dest) /* {{{ */
  446. {
  447. if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
  448. /* SSTH Exception: IS_LONG may be accepted instead as IS_DOUBLE */
  449. *dest = (double)Z_LVAL_P(arg);
  450. } else if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
  451. return 0;
  452. }
  453. return zend_parse_arg_double_weak(arg, dest);
  454. }
  455. /* }}} */
  456. ZEND_API int ZEND_FASTCALL zend_parse_arg_str_weak(zval *arg, zend_string **dest) /* {{{ */
  457. {
  458. if (EXPECTED(Z_TYPE_P(arg) < IS_STRING)) {
  459. convert_to_string(arg);
  460. *dest = Z_STR_P(arg);
  461. } else if (UNEXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
  462. if (Z_OBJ_HANDLER_P(arg, cast_object)) {
  463. zval obj;
  464. if (Z_OBJ_HANDLER_P(arg, cast_object)(arg, &obj, IS_STRING) == SUCCESS) {
  465. zval_ptr_dtor(arg);
  466. ZVAL_COPY_VALUE(arg, &obj);
  467. *dest = Z_STR_P(arg);
  468. return 1;
  469. }
  470. } else if (Z_OBJ_HANDLER_P(arg, get)) {
  471. zval rv;
  472. zval *z = Z_OBJ_HANDLER_P(arg, get)(arg, &rv);
  473. if (Z_TYPE_P(z) != IS_OBJECT) {
  474. zval_ptr_dtor(arg);
  475. if (Z_TYPE_P(z) == IS_STRING) {
  476. ZVAL_COPY_VALUE(arg, z);
  477. } else {
  478. ZVAL_STR(arg, zval_get_string_func(z));
  479. zval_ptr_dtor(z);
  480. }
  481. *dest = Z_STR_P(arg);
  482. return 1;
  483. }
  484. zval_ptr_dtor(z);
  485. }
  486. return 0;
  487. } else {
  488. return 0;
  489. }
  490. return 1;
  491. }
  492. /* }}} */
  493. ZEND_API int ZEND_FASTCALL zend_parse_arg_str_slow(zval *arg, zend_string **dest) /* {{{ */
  494. {
  495. if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
  496. return 0;
  497. }
  498. return zend_parse_arg_str_weak(arg, dest);
  499. }
  500. /* }}} */
  501. static const char *zend_parse_arg_impl(int arg_num, zval *arg, va_list *va, const char **spec, char **error, int *severity) /* {{{ */
  502. {
  503. const char *spec_walk = *spec;
  504. char c = *spec_walk++;
  505. int check_null = 0;
  506. int separate = 0;
  507. zval *real_arg = arg;
  508. /* scan through modifiers */
  509. ZVAL_DEREF(arg);
  510. while (1) {
  511. if (*spec_walk == '/') {
  512. SEPARATE_ZVAL_NOREF(arg);
  513. real_arg = arg;
  514. separate = 1;
  515. } else if (*spec_walk == '!') {
  516. check_null = 1;
  517. } else {
  518. break;
  519. }
  520. spec_walk++;
  521. }
  522. switch (c) {
  523. case 'l':
  524. case 'L':
  525. {
  526. zend_long *p = va_arg(*va, zend_long *);
  527. zend_bool *is_null = NULL;
  528. if (check_null) {
  529. is_null = va_arg(*va, zend_bool *);
  530. }
  531. if (!zend_parse_arg_long(arg, p, is_null, check_null, c == 'L')) {
  532. return "int";
  533. }
  534. }
  535. break;
  536. case 'd':
  537. {
  538. double *p = va_arg(*va, double *);
  539. zend_bool *is_null = NULL;
  540. if (check_null) {
  541. is_null = va_arg(*va, zend_bool *);
  542. }
  543. if (!zend_parse_arg_double(arg, p, is_null, check_null)) {
  544. return "float";
  545. }
  546. }
  547. break;
  548. case 's':
  549. {
  550. char **p = va_arg(*va, char **);
  551. size_t *pl = va_arg(*va, size_t *);
  552. if (!zend_parse_arg_string(arg, p, pl, check_null)) {
  553. return "string";
  554. }
  555. }
  556. break;
  557. case 'p':
  558. {
  559. char **p = va_arg(*va, char **);
  560. size_t *pl = va_arg(*va, size_t *);
  561. if (!zend_parse_arg_path(arg, p, pl, check_null)) {
  562. return "a valid path";
  563. }
  564. }
  565. break;
  566. case 'P':
  567. {
  568. zend_string **str = va_arg(*va, zend_string **);
  569. if (!zend_parse_arg_path_str(arg, str, check_null)) {
  570. return "a valid path";
  571. }
  572. }
  573. break;
  574. case 'S':
  575. {
  576. zend_string **str = va_arg(*va, zend_string **);
  577. if (!zend_parse_arg_str(arg, str, check_null)) {
  578. return "string";
  579. }
  580. }
  581. break;
  582. case 'b':
  583. {
  584. zend_bool *p = va_arg(*va, zend_bool *);
  585. zend_bool *is_null = NULL;
  586. if (check_null) {
  587. is_null = va_arg(*va, zend_bool *);
  588. }
  589. if (!zend_parse_arg_bool(arg, p, is_null, check_null)) {
  590. return "bool";
  591. }
  592. }
  593. break;
  594. case 'r':
  595. {
  596. zval **p = va_arg(*va, zval **);
  597. if (!zend_parse_arg_resource(arg, p, check_null)) {
  598. return "resource";
  599. }
  600. }
  601. break;
  602. case 'A':
  603. case 'a':
  604. {
  605. zval **p = va_arg(*va, zval **);
  606. if (!zend_parse_arg_array(arg, p, check_null, c == 'A')) {
  607. return "array";
  608. }
  609. }
  610. break;
  611. case 'H':
  612. case 'h':
  613. {
  614. HashTable **p = va_arg(*va, HashTable **);
  615. if (!zend_parse_arg_array_ht(arg, p, check_null, c == 'H', separate)) {
  616. return "array";
  617. }
  618. }
  619. break;
  620. case 'o':
  621. {
  622. zval **p = va_arg(*va, zval **);
  623. if (!zend_parse_arg_object(arg, p, NULL, check_null)) {
  624. return "object";
  625. }
  626. }
  627. break;
  628. case 'O':
  629. {
  630. zval **p = va_arg(*va, zval **);
  631. zend_class_entry *ce = va_arg(*va, zend_class_entry *);
  632. if (!zend_parse_arg_object(arg, p, ce, check_null)) {
  633. if (ce) {
  634. return ZSTR_VAL(ce->name);
  635. } else {
  636. return "object";
  637. }
  638. }
  639. }
  640. break;
  641. case 'C':
  642. {
  643. zend_class_entry *lookup, **pce = va_arg(*va, zend_class_entry **);
  644. zend_class_entry *ce_base = *pce;
  645. if (check_null && Z_TYPE_P(arg) == IS_NULL) {
  646. *pce = NULL;
  647. break;
  648. }
  649. convert_to_string_ex(arg);
  650. if ((lookup = zend_lookup_class(Z_STR_P(arg))) == NULL) {
  651. *pce = NULL;
  652. } else {
  653. *pce = lookup;
  654. }
  655. if (ce_base) {
  656. if ((!*pce || !instanceof_function(*pce, ce_base))) {
  657. zend_spprintf(error, 0, "to be a class name derived from %s, '%s' given",
  658. ZSTR_VAL(ce_base->name), Z_STRVAL_P(arg));
  659. *pce = NULL;
  660. return "";
  661. }
  662. }
  663. if (!*pce) {
  664. zend_spprintf(error, 0, "to be a valid class name, '%s' given",
  665. Z_STRVAL_P(arg));
  666. return "";
  667. }
  668. break;
  669. }
  670. break;
  671. case 'f':
  672. {
  673. zend_fcall_info *fci = va_arg(*va, zend_fcall_info *);
  674. zend_fcall_info_cache *fcc = va_arg(*va, zend_fcall_info_cache *);
  675. char *is_callable_error = NULL;
  676. if (check_null && Z_TYPE_P(arg) == IS_NULL) {
  677. fci->size = 0;
  678. fcc->function_handler = 0;
  679. break;
  680. }
  681. if (zend_fcall_info_init(arg, 0, fci, fcc, NULL, &is_callable_error) == SUCCESS) {
  682. if (is_callable_error) {
  683. *severity = E_DEPRECATED;
  684. zend_spprintf(error, 0, "to be a valid callback, %s", is_callable_error);
  685. efree(is_callable_error);
  686. *spec = spec_walk;
  687. return "";
  688. }
  689. break;
  690. } else {
  691. if (is_callable_error) {
  692. *severity = E_ERROR;
  693. zend_spprintf(error, 0, "to be a valid callback, %s", is_callable_error);
  694. efree(is_callable_error);
  695. return "";
  696. } else {
  697. return "valid callback";
  698. }
  699. }
  700. }
  701. case 'z':
  702. {
  703. zval **p = va_arg(*va, zval **);
  704. zend_parse_arg_zval_deref(real_arg, p, check_null);
  705. }
  706. break;
  707. case 'Z':
  708. /* 'Z' iz not supported anymore and should be replaced with 'z' */
  709. ZEND_ASSERT(c != 'Z');
  710. default:
  711. return "unknown";
  712. }
  713. *spec = spec_walk;
  714. return NULL;
  715. }
  716. /* }}} */
  717. static int zend_parse_arg(int arg_num, zval *arg, va_list *va, const char **spec, int flags) /* {{{ */
  718. {
  719. const char *expected_type = NULL;
  720. char *error = NULL;
  721. int severity = 0;
  722. expected_type = zend_parse_arg_impl(arg_num, arg, va, spec, &error, &severity);
  723. if (expected_type) {
  724. if (!(flags & ZEND_PARSE_PARAMS_QUIET) && (*expected_type || error)) {
  725. const char *space;
  726. const char *class_name = get_active_class_name(&space);
  727. zend_bool throw_exception =
  728. ZEND_ARG_USES_STRICT_TYPES() || (flags & ZEND_PARSE_PARAMS_THROW);
  729. if (error) {
  730. zend_internal_type_error(throw_exception, "%s%s%s() expects parameter %d %s",
  731. class_name, space, get_active_function_name(), arg_num, error);
  732. efree(error);
  733. } else {
  734. zend_internal_type_error(throw_exception,
  735. "%s%s%s() expects parameter %d to be %s, %s given",
  736. class_name, space, get_active_function_name(), arg_num, expected_type,
  737. zend_zval_type_name(arg));
  738. }
  739. }
  740. if (severity != E_DEPRECATED) {
  741. return FAILURE;
  742. }
  743. }
  744. return SUCCESS;
  745. }
  746. /* }}} */
  747. ZEND_API int zend_parse_parameter(int flags, int arg_num, zval *arg, const char *spec, ...)
  748. {
  749. va_list va;
  750. int ret;
  751. va_start(va, spec);
  752. ret = zend_parse_arg(arg_num, arg, &va, &spec, flags);
  753. va_end(va);
  754. return ret;
  755. }
  756. static ZEND_COLD void zend_parse_parameters_debug_error(const char *msg) {
  757. zend_function *active_function = EG(current_execute_data)->func;
  758. const char *class_name = active_function->common.scope
  759. ? ZSTR_VAL(active_function->common.scope->name) : "";
  760. zend_error_noreturn(E_CORE_ERROR, "%s%s%s(): %s",
  761. class_name, class_name[0] ? "::" : "",
  762. ZSTR_VAL(active_function->common.function_name), msg);
  763. }
  764. static int zend_parse_va_args(int num_args, const char *type_spec, va_list *va, int flags) /* {{{ */
  765. {
  766. const char *spec_walk;
  767. int c, i;
  768. int min_num_args = -1;
  769. int max_num_args = 0;
  770. int post_varargs = 0;
  771. zval *arg;
  772. int arg_count;
  773. zend_bool have_varargs = 0;
  774. zval **varargs = NULL;
  775. int *n_varargs = NULL;
  776. for (spec_walk = type_spec; *spec_walk; spec_walk++) {
  777. c = *spec_walk;
  778. switch (c) {
  779. case 'l': case 'd':
  780. case 's': case 'b':
  781. case 'r': case 'a':
  782. case 'o': case 'O':
  783. case 'z': case 'Z':
  784. case 'C': case 'h':
  785. case 'f': case 'A':
  786. case 'H': case 'p':
  787. case 'S': case 'P':
  788. case 'L':
  789. max_num_args++;
  790. break;
  791. case '|':
  792. min_num_args = max_num_args;
  793. break;
  794. case '/':
  795. case '!':
  796. /* Pass */
  797. break;
  798. case '*':
  799. case '+':
  800. if (have_varargs) {
  801. zend_parse_parameters_debug_error(
  802. "only one varargs specifier (* or +) is permitted");
  803. return FAILURE;
  804. }
  805. have_varargs = 1;
  806. /* we expect at least one parameter in varargs */
  807. if (c == '+') {
  808. max_num_args++;
  809. }
  810. /* mark the beginning of varargs */
  811. post_varargs = max_num_args;
  812. break;
  813. default:
  814. zend_parse_parameters_debug_error("bad type specifier while parsing parameters");
  815. return FAILURE;
  816. }
  817. }
  818. if (min_num_args < 0) {
  819. min_num_args = max_num_args;
  820. }
  821. if (have_varargs) {
  822. /* calculate how many required args are at the end of the specifier list */
  823. post_varargs = max_num_args - post_varargs;
  824. max_num_args = -1;
  825. }
  826. if (num_args < min_num_args || (num_args > max_num_args && max_num_args >= 0)) {
  827. if (!(flags & ZEND_PARSE_PARAMS_QUIET)) {
  828. zend_function *active_function = EG(current_execute_data)->func;
  829. const char *class_name = active_function->common.scope ? ZSTR_VAL(active_function->common.scope->name) : "";
  830. zend_bool throw_exception = ZEND_ARG_USES_STRICT_TYPES() || (flags & ZEND_PARSE_PARAMS_THROW);
  831. zend_internal_argument_count_error(throw_exception, "%s%s%s() expects %s %d parameter%s, %d given",
  832. class_name,
  833. class_name[0] ? "::" : "",
  834. ZSTR_VAL(active_function->common.function_name),
  835. min_num_args == max_num_args ? "exactly" : num_args < min_num_args ? "at least" : "at most",
  836. num_args < min_num_args ? min_num_args : max_num_args,
  837. (num_args < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
  838. num_args);
  839. }
  840. return FAILURE;
  841. }
  842. arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
  843. if (num_args > arg_count) {
  844. zend_parse_parameters_debug_error("could not obtain parameters for parsing");
  845. return FAILURE;
  846. }
  847. i = 0;
  848. while (num_args-- > 0) {
  849. if (*type_spec == '|') {
  850. type_spec++;
  851. }
  852. if (*type_spec == '*' || *type_spec == '+') {
  853. int num_varargs = num_args + 1 - post_varargs;
  854. /* eat up the passed in storage even if it won't be filled in with varargs */
  855. varargs = va_arg(*va, zval **);
  856. n_varargs = va_arg(*va, int *);
  857. type_spec++;
  858. if (num_varargs > 0) {
  859. *n_varargs = num_varargs;
  860. *varargs = ZEND_CALL_ARG(EG(current_execute_data), i + 1);
  861. /* adjust how many args we have left and restart loop */
  862. num_args += 1 - num_varargs;
  863. i += num_varargs;
  864. continue;
  865. } else {
  866. *varargs = NULL;
  867. *n_varargs = 0;
  868. }
  869. }
  870. arg = ZEND_CALL_ARG(EG(current_execute_data), i + 1);
  871. if (zend_parse_arg(i+1, arg, va, &type_spec, flags) == FAILURE) {
  872. /* clean up varargs array if it was used */
  873. if (varargs && *varargs) {
  874. *varargs = NULL;
  875. }
  876. return FAILURE;
  877. }
  878. i++;
  879. }
  880. return SUCCESS;
  881. }
  882. /* }}} */
  883. ZEND_API int zend_parse_parameters_ex(int flags, int num_args, const char *type_spec, ...) /* {{{ */
  884. {
  885. va_list va;
  886. int retval;
  887. va_start(va, type_spec);
  888. retval = zend_parse_va_args(num_args, type_spec, &va, flags);
  889. va_end(va);
  890. return retval;
  891. }
  892. /* }}} */
  893. ZEND_API int zend_parse_parameters(int num_args, const char *type_spec, ...) /* {{{ */
  894. {
  895. va_list va;
  896. int retval;
  897. int flags = 0;
  898. va_start(va, type_spec);
  899. retval = zend_parse_va_args(num_args, type_spec, &va, flags);
  900. va_end(va);
  901. return retval;
  902. }
  903. /* }}} */
  904. ZEND_API int zend_parse_parameters_throw(int num_args, const char *type_spec, ...) /* {{{ */
  905. {
  906. va_list va;
  907. int retval;
  908. int flags = ZEND_PARSE_PARAMS_THROW;
  909. va_start(va, type_spec);
  910. retval = zend_parse_va_args(num_args, type_spec, &va, flags);
  911. va_end(va);
  912. return retval;
  913. }
  914. /* }}} */
  915. ZEND_API int zend_parse_method_parameters(int num_args, zval *this_ptr, const char *type_spec, ...) /* {{{ */
  916. {
  917. va_list va;
  918. int retval;
  919. int flags = 0;
  920. const char *p = type_spec;
  921. zval **object;
  922. zend_class_entry *ce;
  923. /* Just checking this_ptr is not enough, because fcall_common_helper does not set
  924. * Z_OBJ(EG(This)) to NULL when calling an internal function with common.scope == NULL.
  925. * In that case EG(This) would still be the $this from the calling code and we'd take the
  926. * wrong branch here. */
  927. zend_bool is_method = EG(current_execute_data)->func->common.scope != NULL;
  928. if (!is_method || !this_ptr || Z_TYPE_P(this_ptr) != IS_OBJECT) {
  929. va_start(va, type_spec);
  930. retval = zend_parse_va_args(num_args, type_spec, &va, flags);
  931. va_end(va);
  932. } else {
  933. p++;
  934. va_start(va, type_spec);
  935. object = va_arg(va, zval **);
  936. ce = va_arg(va, zend_class_entry *);
  937. *object = this_ptr;
  938. if (ce && !instanceof_function(Z_OBJCE_P(this_ptr), ce)) {
  939. zend_error_noreturn(E_CORE_ERROR, "%s::%s() must be derived from %s::%s",
  940. ZSTR_VAL(Z_OBJCE_P(this_ptr)->name), get_active_function_name(), ZSTR_VAL(ce->name), get_active_function_name());
  941. }
  942. retval = zend_parse_va_args(num_args, p, &va, flags);
  943. va_end(va);
  944. }
  945. return retval;
  946. }
  947. /* }}} */
  948. ZEND_API int zend_parse_method_parameters_ex(int flags, int num_args, zval *this_ptr, const char *type_spec, ...) /* {{{ */
  949. {
  950. va_list va;
  951. int retval;
  952. const char *p = type_spec;
  953. zval **object;
  954. zend_class_entry *ce;
  955. if (!this_ptr) {
  956. va_start(va, type_spec);
  957. retval = zend_parse_va_args(num_args, type_spec, &va, flags);
  958. va_end(va);
  959. } else {
  960. p++;
  961. va_start(va, type_spec);
  962. object = va_arg(va, zval **);
  963. ce = va_arg(va, zend_class_entry *);
  964. *object = this_ptr;
  965. if (ce && !instanceof_function(Z_OBJCE_P(this_ptr), ce)) {
  966. if (!(flags & ZEND_PARSE_PARAMS_QUIET)) {
  967. zend_error_noreturn(E_CORE_ERROR, "%s::%s() must be derived from %s::%s",
  968. ZSTR_VAL(ce->name), get_active_function_name(), ZSTR_VAL(Z_OBJCE_P(this_ptr)->name), get_active_function_name());
  969. }
  970. va_end(va);
  971. return FAILURE;
  972. }
  973. retval = zend_parse_va_args(num_args, p, &va, flags);
  974. va_end(va);
  975. }
  976. return retval;
  977. }
  978. /* }}} */
  979. /* This function should be called after the constructor has been called
  980. * because it may call __set from the uninitialized object otherwise. */
  981. ZEND_API void zend_merge_properties(zval *obj, HashTable *properties) /* {{{ */
  982. {
  983. const zend_object_handlers *obj_ht = Z_OBJ_HT_P(obj);
  984. zend_class_entry *old_scope = EG(fake_scope);
  985. zend_string *key;
  986. zval *value;
  987. EG(fake_scope) = Z_OBJCE_P(obj);
  988. ZEND_HASH_FOREACH_STR_KEY_VAL(properties, key, value) {
  989. if (key) {
  990. zval member;
  991. ZVAL_STR(&member, key);
  992. obj_ht->write_property(obj, &member, value, NULL);
  993. }
  994. } ZEND_HASH_FOREACH_END();
  995. EG(fake_scope) = old_scope;
  996. }
  997. /* }}} */
  998. ZEND_API int zend_update_class_constants(zend_class_entry *class_type) /* {{{ */
  999. {
  1000. if (!(class_type->ce_flags & ZEND_ACC_CONSTANTS_UPDATED)) {
  1001. zend_class_entry *ce;
  1002. zend_class_constant *c;
  1003. zval *val;
  1004. zend_property_info *prop_info;
  1005. if (class_type->parent) {
  1006. if (UNEXPECTED(zend_update_class_constants(class_type->parent) != SUCCESS)) {
  1007. return FAILURE;
  1008. }
  1009. }
  1010. ZEND_HASH_FOREACH_PTR(&class_type->constants_table, c) {
  1011. val = &c->value;
  1012. if (Z_TYPE_P(val) == IS_CONSTANT_AST) {
  1013. if (UNEXPECTED(zval_update_constant_ex(val, c->ce) != SUCCESS)) {
  1014. return FAILURE;
  1015. }
  1016. }
  1017. } ZEND_HASH_FOREACH_END();
  1018. ce = class_type;
  1019. while (ce) {
  1020. ZEND_HASH_FOREACH_PTR(&ce->properties_info, prop_info) {
  1021. if (prop_info->ce == ce) {
  1022. if (prop_info->flags & ZEND_ACC_STATIC) {
  1023. val = CE_STATIC_MEMBERS(class_type) + prop_info->offset;
  1024. } else {
  1025. val = (zval*)((char*)class_type->default_properties_table + prop_info->offset - OBJ_PROP_TO_OFFSET(0));
  1026. }
  1027. ZVAL_DEREF(val);
  1028. if (Z_TYPE_P(val) == IS_CONSTANT_AST) {
  1029. if (UNEXPECTED(zval_update_constant_ex(val, ce) != SUCCESS)) {
  1030. return FAILURE;
  1031. }
  1032. }
  1033. }
  1034. } ZEND_HASH_FOREACH_END();
  1035. ce = ce->parent;
  1036. }
  1037. class_type->ce_flags |= ZEND_ACC_CONSTANTS_UPDATED;
  1038. }
  1039. return SUCCESS;
  1040. }
  1041. /* }}} */
  1042. ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type) /* {{{ */
  1043. {
  1044. if (class_type->default_properties_count) {
  1045. zval *src = class_type->default_properties_table;
  1046. zval *dst = object->properties_table;
  1047. zval *end = src + class_type->default_properties_count;
  1048. if (UNEXPECTED(class_type->type == ZEND_INTERNAL_CLASS)) {
  1049. do {
  1050. ZVAL_COPY_OR_DUP(dst, src);
  1051. src++;
  1052. dst++;
  1053. } while (src != end);
  1054. } else {
  1055. do {
  1056. ZVAL_COPY(dst, src);
  1057. src++;
  1058. dst++;
  1059. } while (src != end);
  1060. }
  1061. object->properties = NULL;
  1062. }
  1063. }
  1064. /* }}} */
  1065. ZEND_API void object_properties_init_ex(zend_object *object, HashTable *properties) /* {{{ */
  1066. {
  1067. object->properties = properties;
  1068. if (object->ce->default_properties_count) {
  1069. zval *prop;
  1070. zend_string *key;
  1071. zend_property_info *property_info;
  1072. ZEND_HASH_FOREACH_STR_KEY_VAL(properties, key, prop) {
  1073. property_info = zend_get_property_info(object->ce, key, 1);
  1074. if (property_info != ZEND_WRONG_PROPERTY_INFO &&
  1075. property_info &&
  1076. (property_info->flags & ZEND_ACC_STATIC) == 0) {
  1077. zval *slot = OBJ_PROP(object, property_info->offset);
  1078. ZVAL_COPY_VALUE(slot, prop);
  1079. ZVAL_INDIRECT(prop, slot);
  1080. }
  1081. } ZEND_HASH_FOREACH_END();
  1082. }
  1083. }
  1084. /* }}} */
  1085. ZEND_API void object_properties_load(zend_object *object, HashTable *properties) /* {{{ */
  1086. {
  1087. zval *prop, tmp;
  1088. zend_string *key;
  1089. zend_long h;
  1090. zend_property_info *property_info;
  1091. ZEND_HASH_FOREACH_KEY_VAL(properties, h, key, prop) {
  1092. if (key) {
  1093. if (ZSTR_VAL(key)[0] == '\0') {
  1094. const char *class_name, *prop_name;
  1095. size_t prop_name_len;
  1096. if (zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_name_len) == SUCCESS) {
  1097. zend_string *pname = zend_string_init(prop_name, prop_name_len, 0);
  1098. zend_class_entry *prev_scope = EG(fake_scope);
  1099. if (class_name && class_name[0] != '*') {
  1100. zend_string *cname = zend_string_init(class_name, strlen(class_name), 0);
  1101. EG(fake_scope) = zend_lookup_class(cname);
  1102. zend_string_release_ex(cname, 0);
  1103. }
  1104. property_info = zend_get_property_info(object->ce, pname, 1);
  1105. zend_string_release_ex(pname, 0);
  1106. EG(fake_scope) = prev_scope;
  1107. } else {
  1108. property_info = ZEND_WRONG_PROPERTY_INFO;
  1109. }
  1110. } else {
  1111. property_info = zend_get_property_info(object->ce, key, 1);
  1112. }
  1113. if (property_info != ZEND_WRONG_PROPERTY_INFO &&
  1114. property_info &&
  1115. (property_info->flags & ZEND_ACC_STATIC) == 0) {
  1116. zval *slot = OBJ_PROP(object, property_info->offset);
  1117. zval_ptr_dtor(slot);
  1118. ZVAL_COPY_VALUE(slot, prop);
  1119. zval_add_ref(slot);
  1120. if (object->properties) {
  1121. ZVAL_INDIRECT(&tmp, slot);
  1122. zend_hash_update(object->properties, key, &tmp);
  1123. }
  1124. } else {
  1125. if (!object->properties) {
  1126. rebuild_object_properties(object);
  1127. }
  1128. prop = zend_hash_update(object->properties, key, prop);
  1129. zval_add_ref(prop);
  1130. }
  1131. } else {
  1132. if (!object->properties) {
  1133. rebuild_object_properties(object);
  1134. }
  1135. prop = zend_hash_index_update(object->properties, h, prop);
  1136. zval_add_ref(prop);
  1137. }
  1138. } ZEND_HASH_FOREACH_END();
  1139. }
  1140. /* }}} */
  1141. /* This function requires 'properties' to contain all props declared in the
  1142. * class and all props being public. If only a subset is given or the class
  1143. * has protected members then you need to merge the properties separately by
  1144. * calling zend_merge_properties(). */
  1145. ZEND_API int object_and_properties_init(zval *arg, zend_class_entry *class_type, HashTable *properties) /* {{{ */
  1146. {
  1147. if (UNEXPECTED(class_type->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS))) {
  1148. if (class_type->ce_flags & ZEND_ACC_INTERFACE) {
  1149. zend_throw_error(NULL, "Cannot instantiate interface %s", ZSTR_VAL(class_type->name));
  1150. } else if (class_type->ce_flags & ZEND_ACC_TRAIT) {
  1151. zend_throw_error(NULL, "Cannot instantiate trait %s", ZSTR_VAL(class_type->name));
  1152. } else {
  1153. zend_throw_error(NULL, "Cannot instantiate abstract class %s", ZSTR_VAL(class_type->name));
  1154. }
  1155. ZVAL_NULL(arg);
  1156. Z_OBJ_P(arg) = NULL;
  1157. return FAILURE;
  1158. }
  1159. if (UNEXPECTED(!(class_type->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
  1160. if (UNEXPECTED(zend_update_class_constants(class_type) != SUCCESS)) {
  1161. ZVAL_NULL(arg);
  1162. Z_OBJ_P(arg) = NULL;
  1163. return FAILURE;
  1164. }
  1165. }
  1166. if (class_type->create_object == NULL) {
  1167. ZVAL_OBJ(arg, zend_objects_new(class_type));
  1168. if (properties) {
  1169. object_properties_init_ex(Z_OBJ_P(arg), properties);
  1170. } else {
  1171. object_properties_init(Z_OBJ_P(arg), class_type);
  1172. }
  1173. } else {
  1174. ZVAL_OBJ(arg, class_type->create_object(class_type));
  1175. }
  1176. return SUCCESS;
  1177. }
  1178. /* }}} */
  1179. ZEND_API int object_init_ex(zval *arg, zend_class_entry *class_type) /* {{{ */
  1180. {
  1181. return object_and_properties_init(arg, class_type, 0);
  1182. }
  1183. /* }}} */
  1184. ZEND_API int object_init(zval *arg) /* {{{ */
  1185. {
  1186. ZVAL_OBJ(arg, zend_objects_new(zend_standard_class_def));
  1187. return SUCCESS;
  1188. }
  1189. /* }}} */
  1190. ZEND_API int add_assoc_long_ex(zval *arg, const char *key, size_t key_len, zend_long n) /* {{{ */
  1191. {
  1192. zval tmp;
  1193. ZVAL_LONG(&tmp, n);
  1194. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1195. return SUCCESS;
  1196. }
  1197. /* }}} */
  1198. ZEND_API int add_assoc_null_ex(zval *arg, const char *key, size_t key_len) /* {{{ */
  1199. {
  1200. zval tmp;
  1201. ZVAL_NULL(&tmp);
  1202. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1203. return SUCCESS;
  1204. }
  1205. /* }}} */
  1206. ZEND_API int add_assoc_bool_ex(zval *arg, const char *key, size_t key_len, int b) /* {{{ */
  1207. {
  1208. zval tmp;
  1209. ZVAL_BOOL(&tmp, b);
  1210. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1211. return SUCCESS;
  1212. }
  1213. /* }}} */
  1214. ZEND_API int add_assoc_resource_ex(zval *arg, const char *key, size_t key_len, zend_resource *r) /* {{{ */
  1215. {
  1216. zval tmp;
  1217. ZVAL_RES(&tmp, r);
  1218. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1219. return SUCCESS;
  1220. }
  1221. /* }}} */
  1222. ZEND_API int add_assoc_double_ex(zval *arg, const char *key, size_t key_len, double d) /* {{{ */
  1223. {
  1224. zval tmp;
  1225. ZVAL_DOUBLE(&tmp, d);
  1226. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1227. return SUCCESS;
  1228. }
  1229. /* }}} */
  1230. ZEND_API int add_assoc_str_ex(zval *arg, const char *key, size_t key_len, zend_string *str) /* {{{ */
  1231. {
  1232. zval tmp;
  1233. ZVAL_STR(&tmp, str);
  1234. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1235. return SUCCESS;
  1236. }
  1237. /* }}} */
  1238. ZEND_API int add_assoc_string_ex(zval *arg, const char *key, size_t key_len, const char *str) /* {{{ */
  1239. {
  1240. zval tmp;
  1241. ZVAL_STRING(&tmp, str);
  1242. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1243. return SUCCESS;
  1244. }
  1245. /* }}} */
  1246. ZEND_API int add_assoc_stringl_ex(zval *arg, const char *key, size_t key_len, const char *str, size_t length) /* {{{ */
  1247. {
  1248. zval tmp;
  1249. ZVAL_STRINGL(&tmp, str, length);
  1250. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1251. return SUCCESS;
  1252. }
  1253. /* }}} */
  1254. ZEND_API int add_assoc_zval_ex(zval *arg, const char *key, size_t key_len, zval *value) /* {{{ */
  1255. {
  1256. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, value);
  1257. return SUCCESS;
  1258. }
  1259. /* }}} */
  1260. ZEND_API int add_index_long(zval *arg, zend_ulong index, zend_long n) /* {{{ */
  1261. {
  1262. zval tmp;
  1263. ZVAL_LONG(&tmp, n);
  1264. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1265. return SUCCESS;
  1266. }
  1267. /* }}} */
  1268. ZEND_API int add_index_null(zval *arg, zend_ulong index) /* {{{ */
  1269. {
  1270. zval tmp;
  1271. ZVAL_NULL(&tmp);
  1272. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1273. return SUCCESS;
  1274. }
  1275. /* }}} */
  1276. ZEND_API int add_index_bool(zval *arg, zend_ulong index, int b) /* {{{ */
  1277. {
  1278. zval tmp;
  1279. ZVAL_BOOL(&tmp, b);
  1280. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1281. return SUCCESS;
  1282. }
  1283. /* }}} */
  1284. ZEND_API int add_index_resource(zval *arg, zend_ulong index, zend_resource *r) /* {{{ */
  1285. {
  1286. zval tmp;
  1287. ZVAL_RES(&tmp, r);
  1288. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1289. return SUCCESS;
  1290. }
  1291. /* }}} */
  1292. ZEND_API int add_index_double(zval *arg, zend_ulong index, double d) /* {{{ */
  1293. {
  1294. zval tmp;
  1295. ZVAL_DOUBLE(&tmp, d);
  1296. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1297. return SUCCESS;
  1298. }
  1299. /* }}} */
  1300. ZEND_API int add_index_str(zval *arg, zend_ulong index, zend_string *str) /* {{{ */
  1301. {
  1302. zval tmp;
  1303. ZVAL_STR(&tmp, str);
  1304. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1305. return SUCCESS;
  1306. }
  1307. /* }}} */
  1308. ZEND_API int add_index_string(zval *arg, zend_ulong index, const char *str) /* {{{ */
  1309. {
  1310. zval tmp;
  1311. ZVAL_STRING(&tmp, str);
  1312. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1313. return SUCCESS;
  1314. }
  1315. /* }}} */
  1316. ZEND_API int add_index_stringl(zval *arg, zend_ulong index, const char *str, size_t length) /* {{{ */
  1317. {
  1318. zval tmp;
  1319. ZVAL_STRINGL(&tmp, str, length);
  1320. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1321. return SUCCESS;
  1322. }
  1323. /* }}} */
  1324. ZEND_API int add_index_zval(zval *arg, zend_ulong index, zval *value) /* {{{ */
  1325. {
  1326. zend_hash_index_update(Z_ARRVAL_P(arg), index, value);
  1327. return SUCCESS;
  1328. }
  1329. /* }}} */
  1330. ZEND_API int add_next_index_long(zval *arg, zend_long n) /* {{{ */
  1331. {
  1332. zval tmp;
  1333. ZVAL_LONG(&tmp, n);
  1334. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1335. }
  1336. /* }}} */
  1337. ZEND_API int add_next_index_null(zval *arg) /* {{{ */
  1338. {
  1339. zval tmp;
  1340. ZVAL_NULL(&tmp);
  1341. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1342. }
  1343. /* }}} */
  1344. ZEND_API int add_next_index_bool(zval *arg, int b) /* {{{ */
  1345. {
  1346. zval tmp;
  1347. ZVAL_BOOL(&tmp, b);
  1348. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1349. }
  1350. /* }}} */
  1351. ZEND_API int add_next_index_resource(zval *arg, zend_resource *r) /* {{{ */
  1352. {
  1353. zval tmp;
  1354. ZVAL_RES(&tmp, r);
  1355. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1356. }
  1357. /* }}} */
  1358. ZEND_API int add_next_index_double(zval *arg, double d) /* {{{ */
  1359. {
  1360. zval tmp;
  1361. ZVAL_DOUBLE(&tmp, d);
  1362. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1363. }
  1364. /* }}} */
  1365. ZEND_API int add_next_index_str(zval *arg, zend_string *str) /* {{{ */
  1366. {
  1367. zval tmp;
  1368. ZVAL_STR(&tmp, str);
  1369. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1370. }
  1371. /* }}} */
  1372. ZEND_API int add_next_index_string(zval *arg, const char *str) /* {{{ */
  1373. {
  1374. zval tmp;
  1375. ZVAL_STRING(&tmp, str);
  1376. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1377. }
  1378. /* }}} */
  1379. ZEND_API int add_next_index_stringl(zval *arg, const char *str, size_t length) /* {{{ */
  1380. {
  1381. zval tmp;
  1382. ZVAL_STRINGL(&tmp, str, length);
  1383. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1384. }
  1385. /* }}} */
  1386. ZEND_API int add_next_index_zval(zval *arg, zval *value) /* {{{ */
  1387. {
  1388. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), value) ? SUCCESS : FAILURE;
  1389. }
  1390. /* }}} */
  1391. ZEND_API zval *add_get_assoc_string_ex(zval *arg, const char *key, uint32_t key_len, const char *str) /* {{{ */
  1392. {
  1393. zval tmp, *ret;
  1394. ZVAL_STRING(&tmp, str);
  1395. ret = zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1396. return ret;
  1397. }
  1398. /* }}} */
  1399. ZEND_API zval *add_get_assoc_stringl_ex(zval *arg, const char *key, uint32_t key_len, const char *str, size_t length) /* {{{ */
  1400. {
  1401. zval tmp, *ret;
  1402. ZVAL_STRINGL(&tmp, str, length);
  1403. ret = zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1404. return ret;
  1405. }
  1406. /* }}} */
  1407. ZEND_API zval *add_get_index_long(zval *arg, zend_ulong index, zend_long l) /* {{{ */
  1408. {
  1409. zval tmp;
  1410. ZVAL_LONG(&tmp, l);
  1411. return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1412. }
  1413. /* }}} */
  1414. ZEND_API zval *add_get_index_double(zval *arg, zend_ulong index, double d) /* {{{ */
  1415. {
  1416. zval tmp;
  1417. ZVAL_DOUBLE(&tmp, d);
  1418. return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1419. }
  1420. /* }}} */
  1421. ZEND_API zval *add_get_index_str(zval *arg, zend_ulong index, zend_string *str) /* {{{ */
  1422. {
  1423. zval tmp;
  1424. ZVAL_STR(&tmp, str);
  1425. return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1426. }
  1427. /* }}} */
  1428. ZEND_API zval *add_get_index_string(zval *arg, zend_ulong index, const char *str) /* {{{ */
  1429. {
  1430. zval tmp;
  1431. ZVAL_STRING(&tmp, str);
  1432. return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1433. }
  1434. /* }}} */
  1435. ZEND_API zval *add_get_index_stringl(zval *arg, zend_ulong index, const char *str, size_t length) /* {{{ */
  1436. {
  1437. zval tmp;
  1438. ZVAL_STRINGL(&tmp, str, length);
  1439. return zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1440. }
  1441. /* }}} */
  1442. ZEND_API int array_set_zval_key(HashTable *ht, zval *key, zval *value) /* {{{ */
  1443. {
  1444. zval *result;
  1445. switch (Z_TYPE_P(key)) {
  1446. case IS_STRING:
  1447. result = zend_symtable_update(ht, Z_STR_P(key), value);
  1448. break;
  1449. case IS_NULL:
  1450. result = zend_symtable_update(ht, ZSTR_EMPTY_ALLOC(), value);
  1451. break;
  1452. case IS_RESOURCE:
  1453. zend_error(E_NOTICE, "Resource ID#%d used as offset, casting to integer (%d)", Z_RES_HANDLE_P(key), Z_RES_HANDLE_P(key));
  1454. result = zend_hash_index_update(ht, Z_RES_HANDLE_P(key), value);
  1455. break;
  1456. case IS_FALSE:
  1457. result = zend_hash_index_update(ht, 0, value);
  1458. break;
  1459. case IS_TRUE:
  1460. result = zend_hash_index_update(ht, 1, value);
  1461. break;
  1462. case IS_LONG:
  1463. result = zend_hash_index_update(ht, Z_LVAL_P(key), value);
  1464. break;
  1465. case IS_DOUBLE:
  1466. result = zend_hash_index_update(ht, zend_dval_to_lval(Z_DVAL_P(key)), value);
  1467. break;
  1468. default:
  1469. zend_error(E_WARNING, "Illegal offset type");
  1470. result = NULL;
  1471. }
  1472. if (result) {
  1473. Z_TRY_ADDREF_P(result);
  1474. return SUCCESS;
  1475. } else {
  1476. return FAILURE;
  1477. }
  1478. }
  1479. /* }}} */
  1480. ZEND_API int add_property_long_ex(zval *arg, const char *key, size_t key_len, zend_long n) /* {{{ */
  1481. {
  1482. zval tmp;
  1483. zval z_key;
  1484. ZVAL_LONG(&tmp, n);
  1485. ZVAL_STRINGL(&z_key, key, key_len);
  1486. Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, &tmp, NULL);
  1487. zval_ptr_dtor(&z_key);
  1488. return SUCCESS;
  1489. }
  1490. /* }}} */
  1491. ZEND_API int add_property_bool_ex(zval *arg, const char *key, size_t key_len, zend_long b) /* {{{ */
  1492. {
  1493. zval tmp;
  1494. zval z_key;
  1495. ZVAL_BOOL(&tmp, b);
  1496. ZVAL_STRINGL(&z_key, key, key_len);
  1497. Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, &tmp, NULL);
  1498. zval_ptr_dtor(&z_key);
  1499. return SUCCESS;
  1500. }
  1501. /* }}} */
  1502. ZEND_API int add_property_null_ex(zval *arg, const char *key, size_t key_len) /* {{{ */
  1503. {
  1504. zval tmp;
  1505. zval z_key;
  1506. ZVAL_NULL(&tmp);
  1507. ZVAL_STRINGL(&z_key, key, key_len);
  1508. Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, &tmp, NULL);
  1509. zval_ptr_dtor(&z_key);
  1510. return SUCCESS;
  1511. }
  1512. /* }}} */
  1513. ZEND_API int add_property_resource_ex(zval *arg, const char *key, size_t key_len, zend_resource *r) /* {{{ */
  1514. {
  1515. zval tmp;
  1516. zval z_key;
  1517. ZVAL_RES(&tmp, r);
  1518. ZVAL_STRINGL(&z_key, key, key_len);
  1519. Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, &tmp, NULL);
  1520. zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
  1521. zval_ptr_dtor(&z_key);
  1522. return SUCCESS;
  1523. }
  1524. /* }}} */
  1525. ZEND_API int add_property_double_ex(zval *arg, const char *key, size_t key_len, double d) /* {{{ */
  1526. {
  1527. zval tmp;
  1528. zval z_key;
  1529. ZVAL_DOUBLE(&tmp, d);
  1530. ZVAL_STRINGL(&z_key, key, key_len);
  1531. Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, &tmp, NULL);
  1532. zval_ptr_dtor(&z_key);
  1533. return SUCCESS;
  1534. }
  1535. /* }}} */
  1536. ZEND_API int add_property_str_ex(zval *arg, const char *key, size_t key_len, zend_string *str) /* {{{ */
  1537. {
  1538. zval tmp;
  1539. zval z_key;
  1540. ZVAL_STR(&tmp, str);
  1541. ZVAL_STRINGL(&z_key, key, key_len);
  1542. Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, &tmp, NULL);
  1543. zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
  1544. zval_ptr_dtor(&z_key);
  1545. return SUCCESS;
  1546. }
  1547. /* }}} */
  1548. ZEND_API int add_property_string_ex(zval *arg, const char *key, size_t key_len, const char *str) /* {{{ */
  1549. {
  1550. zval tmp;
  1551. zval z_key;
  1552. ZVAL_STRING(&tmp, str);
  1553. ZVAL_STRINGL(&z_key, key, key_len);
  1554. Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, &tmp, NULL);
  1555. zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
  1556. zval_ptr_dtor(&z_key);
  1557. return SUCCESS;
  1558. }
  1559. /* }}} */
  1560. ZEND_API int add_property_stringl_ex(zval *arg, const char *key, size_t key_len, const char *str, size_t length) /* {{{ */
  1561. {
  1562. zval tmp;
  1563. zval z_key;
  1564. ZVAL_STRINGL(&tmp, str, length);
  1565. ZVAL_STRINGL(&z_key, key, key_len);
  1566. Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, &tmp, NULL);
  1567. zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
  1568. zval_ptr_dtor(&z_key);
  1569. return SUCCESS;
  1570. }
  1571. /* }}} */
  1572. ZEND_API int add_property_zval_ex(zval *arg, const char *key, size_t key_len, zval *value) /* {{{ */
  1573. {
  1574. zval z_key;
  1575. ZVAL_STRINGL(&z_key, key, key_len);
  1576. Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, value, NULL);
  1577. zval_ptr_dtor(&z_key);
  1578. return SUCCESS;
  1579. }
  1580. /* }}} */
  1581. ZEND_API int zend_startup_module_ex(zend_module_entry *module) /* {{{ */
  1582. {
  1583. size_t name_len;
  1584. zend_string *lcname;
  1585. if (module->module_started) {
  1586. return SUCCESS;
  1587. }
  1588. module->module_started = 1;
  1589. /* Check module dependencies */
  1590. if (module->deps) {
  1591. const zend_module_dep *dep = module->deps;
  1592. while (dep->name) {
  1593. if (dep->type == MODULE_DEP_REQUIRED) {
  1594. zend_module_entry *req_mod;
  1595. name_len = strlen(dep->name);
  1596. lcname = zend_string_alloc(name_len, 0);
  1597. zend_str_tolower_copy(ZSTR_VAL(lcname), dep->name, name_len);
  1598. if ((req_mod = zend_hash_find_ptr(&module_registry, lcname)) == NULL || !req_mod->module_started) {
  1599. zend_string_efree(lcname);
  1600. /* TODO: Check version relationship */
  1601. zend_error(E_CORE_WARNING, "Cannot load module '%s' because required module '%s' is not loaded", module->name, dep->name);
  1602. module->module_started = 0;
  1603. return FAILURE;
  1604. }
  1605. zend_string_efree(lcname);
  1606. }
  1607. ++dep;
  1608. }
  1609. }
  1610. /* Initialize module globals */
  1611. if (module->globals_size) {
  1612. #ifdef ZTS
  1613. ts_allocate_id(module->globals_id_ptr, module->globals_size, (ts_allocate_ctor) module->globals_ctor, (ts_allocate_dtor) module->globals_dtor);
  1614. #else
  1615. if (module->globals_ctor) {
  1616. module->globals_ctor(module->globals_ptr);
  1617. }
  1618. #endif
  1619. }
  1620. if (module->module_startup_func) {
  1621. EG(current_module) = module;
  1622. if (module->module_startup_func(module->type, module->module_number)==FAILURE) {
  1623. zend_error_noreturn(E_CORE_ERROR,"Unable to start %s module", module->name);
  1624. EG(current_module) = NULL;
  1625. return FAILURE;
  1626. }
  1627. EG(current_module) = NULL;
  1628. }
  1629. return SUCCESS;
  1630. }
  1631. /* }}} */
  1632. static int zend_startup_module_zval(zval *zv) /* {{{ */
  1633. {
  1634. zend_module_entry *module = Z_PTR_P(zv);
  1635. return (zend_startup_module_ex(module) == SUCCESS) ? ZEND_HASH_APPLY_KEEP : ZEND_HASH_APPLY_REMOVE;
  1636. }
  1637. /* }}} */
  1638. static void zend_sort_modules(void *base, size_t count, size_t siz, compare_func_t compare, swap_func_t swp) /* {{{ */
  1639. {
  1640. Bucket *b1 = base;
  1641. Bucket *b2;
  1642. Bucket *end = b1 + count;
  1643. Bucket tmp;
  1644. zend_module_entry *m, *r;
  1645. while (b1 < end) {
  1646. try_again:
  1647. m = (zend_module_entry*)Z_PTR(b1->val);
  1648. if (!m->module_started && m->deps) {
  1649. const zend_module_dep *dep = m->deps;
  1650. while (dep->name) {
  1651. if (dep->type == MODULE_DEP_REQUIRED || dep->type == MODULE_DEP_OPTIONAL) {
  1652. b2 = b1 + 1;
  1653. while (b2 < end) {
  1654. r = (zend_module_entry*)Z_PTR(b2->val);
  1655. if (strcasecmp(dep->name, r->name) == 0) {
  1656. tmp = *b1;
  1657. *b1 = *b2;
  1658. *b2 = tmp;
  1659. goto try_again;
  1660. }
  1661. b2++;
  1662. }
  1663. }
  1664. dep++;
  1665. }
  1666. }
  1667. b1++;
  1668. }
  1669. }
  1670. /* }}} */
  1671. ZEND_API void zend_collect_module_handlers(void) /* {{{ */
  1672. {
  1673. zend_module_entry *module;
  1674. int startup_count = 0;
  1675. int shutdown_count = 0;
  1676. int post_deactivate_count = 0;
  1677. zend_class_entry *ce;
  1678. int class_count = 0;
  1679. /* Collect extensions with request startup/shutdown handlers */
  1680. ZEND_HASH_FOREACH_PTR(&module_registry, module) {
  1681. if (module->request_startup_func) {
  1682. startup_count++;
  1683. }
  1684. if (module->request_shutdown_func) {
  1685. shutdown_count++;
  1686. }
  1687. if (module->post_deactivate_func) {
  1688. post_deactivate_count++;
  1689. }
  1690. } ZEND_HASH_FOREACH_END();
  1691. module_request_startup_handlers = (zend_module_entry**)malloc(
  1692. sizeof(zend_module_entry*) *
  1693. (startup_count + 1 +
  1694. shutdown_count + 1 +
  1695. post_deactivate_count + 1));
  1696. module_request_startup_handlers[startup_count] = NULL;
  1697. module_request_shutdown_handlers = module_request_startup_handlers + startup_count + 1;
  1698. module_request_shutdown_handlers[shutdown_count] = NULL;
  1699. module_post_deactivate_handlers = module_request_shutdown_handlers + shutdown_count + 1;
  1700. module_post_deactivate_handlers[post_deactivate_count] = NULL;
  1701. startup_count = 0;
  1702. ZEND_HASH_FOREACH_PTR(&module_registry, module) {
  1703. if (module->request_startup_func) {
  1704. module_request_startup_handlers[startup_count++] = module;
  1705. }
  1706. if (module->request_shutdown_func) {
  1707. module_request_shutdown_handlers[--shutdown_count] = module;
  1708. }
  1709. if (module->post_deactivate_func) {
  1710. module_post_deactivate_handlers[--post_deactivate_count] = module;
  1711. }
  1712. } ZEND_HASH_FOREACH_END();
  1713. /* Collect internal classes with static members */
  1714. ZEND_HASH_FOREACH_PTR(CG(class_table), ce) {
  1715. if (ce->type == ZEND_INTERNAL_CLASS &&
  1716. ce->default_static_members_count > 0) {
  1717. class_count++;
  1718. }
  1719. } ZEND_HASH_FOREACH_END();
  1720. class_cleanup_handlers = (zend_class_entry**)malloc(
  1721. sizeof(zend_class_entry*) *
  1722. (class_count + 1));
  1723. class_cleanup_handlers[class_count] = NULL;
  1724. if (class_count) {
  1725. ZEND_HASH_FOREACH_PTR(CG(class_table), ce) {
  1726. if (ce->type == ZEND_INTERNAL_CLASS &&
  1727. ce->default_static_members_count > 0) {
  1728. class_cleanup_handlers[--class_count] = ce;
  1729. }
  1730. } ZEND_HASH_FOREACH_END();
  1731. }
  1732. }
  1733. /* }}} */
  1734. ZEND_API int zend_startup_modules(void) /* {{{ */
  1735. {
  1736. zend_hash_sort_ex(&module_registry, zend_sort_modules, NULL, 0);
  1737. zend_hash_apply(&module_registry, zend_startup_module_zval);
  1738. return SUCCESS;
  1739. }
  1740. /* }}} */
  1741. ZEND_API void zend_destroy_modules(void) /* {{{ */
  1742. {
  1743. free(class_cleanup_handlers);
  1744. free(module_request_startup_handlers);
  1745. zend_hash_graceful_reverse_destroy(&module_registry);
  1746. }
  1747. /* }}} */
  1748. ZEND_API zend_module_entry* zend_register_module_ex(zend_module_entry *module) /* {{{ */
  1749. {
  1750. size_t name_len;
  1751. zend_string *lcname;
  1752. zend_module_entry *module_ptr;
  1753. if (!module) {
  1754. return NULL;
  1755. }
  1756. #if 0
  1757. zend_printf("%s: Registering module %d\n", module->name, module->module_number);
  1758. #endif
  1759. /* Check module dependencies */
  1760. if (module->deps) {
  1761. const zend_module_dep *dep = module->deps;
  1762. while (dep->name) {
  1763. if (dep->type == MODULE_DEP_CONFLICTS) {
  1764. name_len = strlen(dep->name);
  1765. lcname = zend_string_alloc(name_len, 0);
  1766. zend_str_tolower_copy(ZSTR_VAL(lcname), dep->name, name_len);
  1767. if (zend_hash_exists(&module_registry, lcname) || zend_get_extension(dep->name)) {
  1768. zend_string_efree(lcname);
  1769. /* TODO: Check version relationship */
  1770. zend_error(E_CORE_WARNING, "Cannot load module '%s' because conflicting module '%s' is already loaded", module->name, dep->name);
  1771. return NULL;
  1772. }
  1773. zend_string_efree(lcname);
  1774. }
  1775. ++dep;
  1776. }
  1777. }
  1778. name_len = strlen(module->name);
  1779. lcname = zend_string_alloc(name_len, module->type == MODULE_PERSISTENT);
  1780. zend_str_tolower_copy(ZSTR_VAL(lcname), module->name, name_len);
  1781. lcname = zend_new_interned_string(lcname);
  1782. if ((module_ptr = zend_hash_add_mem(&module_registry, lcname, module, sizeof(zend_module_entry))) == NULL) {
  1783. zend_error(E_CORE_WARNING, "Module '%s' already loaded", module->name);
  1784. zend_string_release(lcname);
  1785. return NULL;
  1786. }
  1787. module = module_ptr;
  1788. EG(current_module) = module;
  1789. if (module->functions && zend_register_functions(NULL, module->functions, NULL, module->type)==FAILURE) {
  1790. zend_hash_del(&module_registry, lcname);
  1791. zend_string_release(lcname);
  1792. EG(current_module) = NULL;
  1793. zend_error(E_CORE_WARNING,"%s: Unable to register functions, unable to load", module->name);
  1794. return NULL;
  1795. }
  1796. EG(current_module) = NULL;
  1797. zend_string_release(lcname);
  1798. return module;
  1799. }
  1800. /* }}} */
  1801. ZEND_API zend_module_entry* zend_register_internal_module(zend_module_entry *module) /* {{{ */
  1802. {
  1803. module->module_number = zend_next_free_module();
  1804. module->type = MODULE_PERSISTENT;
  1805. return zend_register_module_ex(module);
  1806. }
  1807. /* }}} */
  1808. ZEND_API void zend_check_magic_method_implementation(const zend_class_entry *ce, const zend_function *fptr, int error_type) /* {{{ */
  1809. {
  1810. char lcname[16];
  1811. size_t name_len;
  1812. if (ZSTR_VAL(fptr->common.function_name)[0] != '_'
  1813. || ZSTR_VAL(fptr->common.function_name)[1] != '_') {
  1814. return;
  1815. }
  1816. /* we don't care if the function name is longer, in fact lowercasing only
  1817. * the beginning of the name speeds up the check process */
  1818. name_len = ZSTR_LEN(fptr->common.function_name);
  1819. zend_str_tolower_copy(lcname, ZSTR_VAL(fptr->common.function_name), MIN(name_len, sizeof(lcname)-1));
  1820. lcname[sizeof(lcname)-1] = '\0'; /* zend_str_tolower_copy won't necessarily set the zero byte */
  1821. if (name_len == sizeof(ZEND_DESTRUCTOR_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_DESTRUCTOR_FUNC_NAME, sizeof(ZEND_DESTRUCTOR_FUNC_NAME) - 1) && fptr->common.num_args != 0) {
  1822. zend_error(error_type, "Destructor %s::%s() cannot take arguments", ZSTR_VAL(ce->name), ZEND_DESTRUCTOR_FUNC_NAME);
  1823. } else if (name_len == sizeof(ZEND_CLONE_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_CLONE_FUNC_NAME, sizeof(ZEND_CLONE_FUNC_NAME) - 1) && fptr->common.num_args != 0) {
  1824. zend_error(error_type, "Method %s::%s() cannot accept any arguments", ZSTR_VAL(ce->name), ZEND_CLONE_FUNC_NAME);
  1825. } else if (name_len == sizeof(ZEND_GET_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_GET_FUNC_NAME, sizeof(ZEND_GET_FUNC_NAME) - 1)) {
  1826. if (fptr->common.num_args != 1) {
  1827. zend_error(error_type, "Method %s::%s() must take exactly 1 argument", ZSTR_VAL(ce->name), ZEND_GET_FUNC_NAME);
  1828. } else if (ARG_SHOULD_BE_SENT_BY_REF(fptr, 1)) {
  1829. zend_error(error_type, "Method %s::%s() cannot take arguments by reference", ZSTR_VAL(ce->name), ZEND_GET_FUNC_NAME);
  1830. }
  1831. } else if (name_len == sizeof(ZEND_SET_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_SET_FUNC_NAME, sizeof(ZEND_SET_FUNC_NAME) - 1)) {
  1832. if (fptr->common.num_args != 2) {
  1833. zend_error(error_type, "Method %s::%s() must take exactly 2 arguments", ZSTR_VAL(ce->name), ZEND_SET_FUNC_NAME);
  1834. } else if (ARG_SHOULD_BE_SENT_BY_REF(fptr, 1) || ARG_SHOULD_BE_SENT_BY_REF(fptr, 2)) {
  1835. zend_error(error_type, "Method %s::%s() cannot take arguments by reference", ZSTR_VAL(ce->name), ZEND_SET_FUNC_NAME);
  1836. }
  1837. } else if (name_len == sizeof(ZEND_UNSET_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_UNSET_FUNC_NAME, sizeof(ZEND_UNSET_FUNC_NAME) - 1)) {
  1838. if (fptr->common.num_args != 1) {
  1839. zend_error(error_type, "Method %s::%s() must take exactly 1 argument", ZSTR_VAL(ce->name), ZEND_UNSET_FUNC_NAME);
  1840. } else if (ARG_SHOULD_BE_SENT_BY_REF(fptr, 1)) {
  1841. zend_error(error_type, "Method %s::%s() cannot take arguments by reference", ZSTR_VAL(ce->name), ZEND_UNSET_FUNC_NAME);
  1842. }
  1843. } else if (name_len == sizeof(ZEND_ISSET_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_ISSET_FUNC_NAME, sizeof(ZEND_ISSET_FUNC_NAME) - 1)) {
  1844. if (fptr->common.num_args != 1) {
  1845. zend_error(error_type, "Method %s::%s() must take exactly 1 argument", ZSTR_VAL(ce->name), ZEND_ISSET_FUNC_NAME);
  1846. } else if (ARG_SHOULD_BE_SENT_BY_REF(fptr, 1)) {
  1847. zend_error(error_type, "Method %s::%s() cannot take arguments by reference", ZSTR_VAL(ce->name), ZEND_ISSET_FUNC_NAME);
  1848. }
  1849. } else if (name_len == sizeof(ZEND_CALL_FUNC_NAME) - 1 && !memcmp(lcname, ZEND_CALL_FUNC_NAME, sizeof(ZEND_CALL_FUNC_NAME) - 1)) {
  1850. if (fptr->common.num_args != 2) {
  1851. zend_error(error_type, "Method %s::%s() must take exactly 2 arguments", ZSTR_VAL(ce->name), ZEND_CALL_FUNC_NAME);
  1852. } else if (ARG_SHOULD_BE_SENT_BY_REF(fptr, 1) || ARG_SHOULD_BE_SENT_BY_REF(fptr, 2)) {
  1853. zend_error(error_type, "Method %s::%s() cannot take arguments by reference", ZSTR_VAL(ce->name), ZEND_CALL_FUNC_NAME);
  1854. }
  1855. } else if (name_len == sizeof(ZEND_CALLSTATIC_FUNC_NAME) - 1 &&
  1856. !memcmp(lcname, ZEND_CALLSTATIC_FUNC_NAME, sizeof(ZEND_CALLSTATIC_FUNC_NAME)-1)
  1857. ) {
  1858. if (fptr->common.num_args != 2) {
  1859. zend_error(error_type, "Method %s::__callStatic() must take exactly 2 arguments", ZSTR_VAL(ce->name));
  1860. } else if (ARG_SHOULD_BE_SENT_BY_REF(fptr, 1) || ARG_SHOULD_BE_SENT_BY_REF(fptr, 2)) {
  1861. zend_error(error_type, "Method %s::__callStatic() cannot take arguments by reference", ZSTR_VAL(ce->name));
  1862. }
  1863. } else if (name_len == sizeof(ZEND_TOSTRING_FUNC_NAME) - 1 &&
  1864. !memcmp(lcname, ZEND_TOSTRING_FUNC_NAME, sizeof(ZEND_TOSTRING_FUNC_NAME)-1) && fptr->common.num_args != 0
  1865. ) {
  1866. zend_error(error_type, "Method %s::%s() cannot take arguments", ZSTR_VAL(ce->name), ZEND_TOSTRING_FUNC_NAME);
  1867. } else if (name_len == sizeof(ZEND_DEBUGINFO_FUNC_NAME) - 1 &&
  1868. !memcmp(lcname, ZEND_DEBUGINFO_FUNC_NAME, sizeof(ZEND_DEBUGINFO_FUNC_NAME)-1) && fptr->common.num_args != 0) {
  1869. zend_error(error_type, "Method %s::%s() cannot take arguments", ZSTR_VAL(ce->name), ZEND_DEBUGINFO_FUNC_NAME);
  1870. }
  1871. }
  1872. /* }}} */
  1873. /* registers all functions in *library_functions in the function hash */
  1874. ZEND_API int zend_register_functions(zend_class_entry *scope, const zend_function_entry *functions, HashTable *function_table, int type) /* {{{ */
  1875. {
  1876. const zend_function_entry *ptr = functions;
  1877. zend_function function, *reg_function;
  1878. zend_internal_function *internal_function = (zend_internal_function *)&function;
  1879. int count=0, unload=0;
  1880. HashTable *target_function_table = function_table;
  1881. int error_type;
  1882. zend_function *ctor = NULL, *dtor = NULL, *clone = NULL, *__get = NULL, *__set = NULL, *__unset = NULL, *__isset = NULL, *__call = NULL, *__callstatic = NULL, *__tostring = NULL, *__debugInfo = NULL;
  1883. zend_string *lowercase_name;
  1884. size_t fname_len;
  1885. const char *lc_class_name = NULL;
  1886. size_t class_name_len = 0;
  1887. if (type==MODULE_PERSISTENT) {
  1888. error_type = E_CORE_WARNING;
  1889. } else {
  1890. error_type = E_WARNING;
  1891. }
  1892. if (!target_function_table) {
  1893. target_function_table = CG(function_table);
  1894. }
  1895. internal_function->type = ZEND_INTERNAL_FUNCTION;
  1896. internal_function->module = EG(current_module);
  1897. memset(internal_function->reserved, 0, ZEND_MAX_RESERVED_RESOURCES * sizeof(void*));
  1898. if (scope) {
  1899. class_name_len = ZSTR_LEN(scope->name);
  1900. if ((lc_class_name = zend_memrchr(ZSTR_VAL(scope->name), '\\', class_name_len))) {
  1901. ++lc_class_name;
  1902. class_name_len -= (lc_class_name - ZSTR_VAL(scope->name));
  1903. lc_class_name = zend_str_tolower_dup(lc_class_name, class_name_len);
  1904. } else {
  1905. lc_class_name = zend_str_tolower_dup(ZSTR_VAL(scope->name), class_name_len);
  1906. }
  1907. }
  1908. while (ptr->fname) {
  1909. fname_len = strlen(ptr->fname);
  1910. internal_function->handler = ptr->handler;
  1911. internal_function->function_name = zend_string_init_interned(ptr->fname, fname_len, 1);
  1912. internal_function->scope = scope;
  1913. internal_function->prototype = NULL;
  1914. if (ptr->flags) {
  1915. if (!(ptr->flags & ZEND_ACC_PPP_MASK)) {
  1916. if (ptr->flags != ZEND_ACC_DEPRECATED && scope) {
  1917. zend_error(error_type, "Invalid access level for %s%s%s() - access must be exactly one of public, protected or private", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
  1918. }
  1919. internal_function->fn_flags = ZEND_ACC_PUBLIC | ptr->flags;
  1920. } else {
  1921. internal_function->fn_flags = ptr->flags;
  1922. }
  1923. } else {
  1924. internal_function->fn_flags = ZEND_ACC_PUBLIC;
  1925. }
  1926. if (ptr->arg_info) {
  1927. zend_internal_function_info *info = (zend_internal_function_info*)ptr->arg_info;
  1928. internal_function->arg_info = (zend_internal_arg_info*)ptr->arg_info+1;
  1929. internal_function->num_args = ptr->num_args;
  1930. /* Currently you cannot denote that the function can accept less arguments than num_args */
  1931. if (info->required_num_args == (zend_uintptr_t)-1) {
  1932. internal_function->required_num_args = ptr->num_args;
  1933. } else {
  1934. internal_function->required_num_args = info->required_num_args;
  1935. }
  1936. if (info->return_reference) {
  1937. internal_function->fn_flags |= ZEND_ACC_RETURN_REFERENCE;
  1938. }
  1939. if (ptr->arg_info[ptr->num_args].is_variadic) {
  1940. internal_function->fn_flags |= ZEND_ACC_VARIADIC;
  1941. /* Don't count the variadic argument */
  1942. internal_function->num_args--;
  1943. }
  1944. if (ZEND_TYPE_IS_SET(info->type)) {
  1945. if (ZEND_TYPE_IS_CLASS(info->type)) {
  1946. const char *type_name = (const char*)info->type;
  1947. if (type_name[0] == '?') {
  1948. type_name++;
  1949. }
  1950. if (!scope && (!strcasecmp(type_name, "self") || !strcasecmp(type_name, "parent"))) {
  1951. zend_error_noreturn(E_CORE_ERROR, "Cannot declare a return type of %s outside of a class scope", type_name);
  1952. }
  1953. }
  1954. internal_function->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
  1955. }
  1956. } else {
  1957. internal_function->arg_info = NULL;
  1958. internal_function->num_args = 0;
  1959. internal_function->required_num_args = 0;
  1960. }
  1961. zend_set_function_arg_flags((zend_function*)internal_function);
  1962. if (ptr->flags & ZEND_ACC_ABSTRACT) {
  1963. if (scope) {
  1964. /* This is a class that must be abstract itself. Here we set the check info. */
  1965. scope->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
  1966. if (!(scope->ce_flags & ZEND_ACC_INTERFACE)) {
  1967. /* Since the class is not an interface it needs to be declared as a abstract class. */
  1968. /* Since here we are handling internal functions only we can add the keyword flag. */
  1969. /* This time we set the flag for the keyword 'abstract'. */
  1970. scope->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
  1971. }
  1972. }
  1973. if (ptr->flags & ZEND_ACC_STATIC && (!scope || !(scope->ce_flags & ZEND_ACC_INTERFACE))) {
  1974. zend_error(error_type, "Static function %s%s%s() cannot be abstract", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
  1975. }
  1976. } else {
  1977. if (scope && (scope->ce_flags & ZEND_ACC_INTERFACE)) {
  1978. efree((char*)lc_class_name);
  1979. zend_error(error_type, "Interface %s cannot contain non abstract method %s()", ZSTR_VAL(scope->name), ptr->fname);
  1980. return FAILURE;
  1981. }
  1982. if (!internal_function->handler) {
  1983. if (scope) {
  1984. efree((char*)lc_class_name);
  1985. }
  1986. zend_error(error_type, "Method %s%s%s() cannot be a NULL function", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
  1987. zend_unregister_functions(functions, count, target_function_table);
  1988. return FAILURE;
  1989. }
  1990. }
  1991. lowercase_name = zend_string_tolower_ex(internal_function->function_name, type == MODULE_PERSISTENT);
  1992. lowercase_name = zend_new_interned_string(lowercase_name);
  1993. reg_function = malloc(sizeof(zend_internal_function));
  1994. memcpy(reg_function, &function, sizeof(zend_internal_function));
  1995. if (zend_hash_add_ptr(target_function_table, lowercase_name, reg_function) == NULL) {
  1996. unload=1;
  1997. free(reg_function);
  1998. zend_string_release(lowercase_name);
  1999. break;
  2000. }
  2001. /* If types of arguments have to be checked */
  2002. if (reg_function->common.arg_info && reg_function->common.num_args) {
  2003. uint32_t i;
  2004. for (i = 0; i < reg_function->common.num_args; i++) {
  2005. if (ZEND_TYPE_IS_SET(reg_function->common.arg_info[i].type)) {
  2006. reg_function->common.fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
  2007. break;
  2008. }
  2009. }
  2010. }
  2011. if (reg_function->common.arg_info &&
  2012. (reg_function->common.fn_flags & (ZEND_ACC_HAS_RETURN_TYPE|ZEND_ACC_HAS_TYPE_HINTS))) {
  2013. /* convert "const char*" class type names into "zend_string*" */
  2014. uint32_t i;
  2015. uint32_t num_args = reg_function->common.num_args + 1;
  2016. zend_arg_info *arg_info = reg_function->common.arg_info - 1;
  2017. zend_arg_info *new_arg_info;
  2018. if (reg_function->common.fn_flags & ZEND_ACC_VARIADIC) {
  2019. num_args++;
  2020. }
  2021. new_arg_info = malloc(sizeof(zend_arg_info) * num_args);
  2022. memcpy(new_arg_info, arg_info, sizeof(zend_arg_info) * num_args);
  2023. reg_function->common.arg_info = new_arg_info + 1;
  2024. for (i = 0; i < num_args; i++) {
  2025. if (ZEND_TYPE_IS_CLASS(new_arg_info[i].type)) {
  2026. const char *class_name = (const char*)new_arg_info[i].type;
  2027. zend_bool allow_null = 0;
  2028. zend_string *str;
  2029. if (class_name[0] == '?') {
  2030. class_name++;
  2031. allow_null = 1;
  2032. }
  2033. str = zend_string_init_interned(class_name, strlen(class_name), 1);
  2034. new_arg_info[i].type = ZEND_TYPE_ENCODE_CLASS(str, allow_null);
  2035. }
  2036. }
  2037. }
  2038. if (scope) {
  2039. /* Look for ctor, dtor, clone
  2040. * If it's an old-style constructor, store it only if we don't have
  2041. * a constructor already.
  2042. */
  2043. if ((fname_len == class_name_len) && !ctor && !memcmp(ZSTR_VAL(lowercase_name), lc_class_name, class_name_len+1)) {
  2044. ctor = reg_function;
  2045. } else if (ZSTR_VAL(lowercase_name)[0] != '_' || ZSTR_VAL(lowercase_name)[1] != '_') {
  2046. reg_function = NULL;
  2047. } else if (zend_string_equals_literal(lowercase_name, ZEND_CONSTRUCTOR_FUNC_NAME)) {
  2048. ctor = reg_function;
  2049. } else if (zend_string_equals_literal(lowercase_name, ZEND_DESTRUCTOR_FUNC_NAME)) {
  2050. dtor = reg_function;
  2051. if (internal_function->num_args) {
  2052. zend_error(error_type, "Destructor %s::%s() cannot take arguments", ZSTR_VAL(scope->name), ptr->fname);
  2053. }
  2054. } else if (zend_string_equals_literal(lowercase_name, ZEND_CLONE_FUNC_NAME)) {
  2055. clone = reg_function;
  2056. } else if (zend_string_equals_literal(lowercase_name, ZEND_CALL_FUNC_NAME)) {
  2057. __call = reg_function;
  2058. } else if (zend_string_equals_literal(lowercase_name, ZEND_CALLSTATIC_FUNC_NAME)) {
  2059. __callstatic = reg_function;
  2060. } else if (zend_string_equals_literal(lowercase_name, ZEND_TOSTRING_FUNC_NAME)) {
  2061. __tostring = reg_function;
  2062. } else if (zend_string_equals_literal(lowercase_name, ZEND_GET_FUNC_NAME)) {
  2063. __get = reg_function;
  2064. scope->ce_flags |= ZEND_ACC_USE_GUARDS;
  2065. } else if (zend_string_equals_literal(lowercase_name, ZEND_SET_FUNC_NAME)) {
  2066. __set = reg_function;
  2067. scope->ce_flags |= ZEND_ACC_USE_GUARDS;
  2068. } else if (zend_string_equals_literal(lowercase_name, ZEND_UNSET_FUNC_NAME)) {
  2069. __unset = reg_function;
  2070. scope->ce_flags |= ZEND_ACC_USE_GUARDS;
  2071. } else if (zend_string_equals_literal(lowercase_name, ZEND_ISSET_FUNC_NAME)) {
  2072. __isset = reg_function;
  2073. scope->ce_flags |= ZEND_ACC_USE_GUARDS;
  2074. } else if (zend_string_equals_literal(lowercase_name, ZEND_DEBUGINFO_FUNC_NAME)) {
  2075. __debugInfo = reg_function;
  2076. } else {
  2077. reg_function = NULL;
  2078. }
  2079. if (reg_function) {
  2080. zend_check_magic_method_implementation(scope, reg_function, error_type);
  2081. }
  2082. }
  2083. ptr++;
  2084. count++;
  2085. zend_string_release(lowercase_name);
  2086. }
  2087. if (unload) { /* before unloading, display all remaining bad function in the module */
  2088. if (scope) {
  2089. efree((char*)lc_class_name);
  2090. }
  2091. while (ptr->fname) {
  2092. fname_len = strlen(ptr->fname);
  2093. lowercase_name = zend_string_alloc(fname_len, 0);
  2094. zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ptr->fname, fname_len);
  2095. if (zend_hash_exists(target_function_table, lowercase_name)) {
  2096. zend_error(error_type, "Function registration failed - duplicate name - %s%s%s", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
  2097. }
  2098. zend_string_efree(lowercase_name);
  2099. ptr++;
  2100. }
  2101. zend_unregister_functions(functions, count, target_function_table);
  2102. return FAILURE;
  2103. }
  2104. if (scope) {
  2105. scope->constructor = ctor;
  2106. scope->destructor = dtor;
  2107. scope->clone = clone;
  2108. scope->__call = __call;
  2109. scope->__callstatic = __callstatic;
  2110. scope->__tostring = __tostring;
  2111. scope->__get = __get;
  2112. scope->__set = __set;
  2113. scope->__unset = __unset;
  2114. scope->__isset = __isset;
  2115. scope->__debugInfo = __debugInfo;
  2116. if (ctor) {
  2117. ctor->common.fn_flags |= ZEND_ACC_CTOR;
  2118. if (ctor->common.fn_flags & ZEND_ACC_STATIC) {
  2119. zend_error(error_type, "Constructor %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(ctor->common.function_name));
  2120. }
  2121. ctor->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
  2122. }
  2123. if (dtor) {
  2124. dtor->common.fn_flags |= ZEND_ACC_DTOR;
  2125. if (dtor->common.fn_flags & ZEND_ACC_STATIC) {
  2126. zend_error(error_type, "Destructor %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(dtor->common.function_name));
  2127. }
  2128. dtor->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
  2129. }
  2130. if (clone) {
  2131. if (clone->common.fn_flags & ZEND_ACC_STATIC) {
  2132. zend_error(error_type, "%s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(clone->common.function_name));
  2133. }
  2134. clone->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
  2135. }
  2136. if (__call) {
  2137. if (__call->common.fn_flags & ZEND_ACC_STATIC) {
  2138. zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__call->common.function_name));
  2139. }
  2140. __call->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
  2141. }
  2142. if (__callstatic) {
  2143. if (!(__callstatic->common.fn_flags & ZEND_ACC_STATIC)) {
  2144. zend_error(error_type, "Method %s::%s() must be static", ZSTR_VAL(scope->name), ZSTR_VAL(__callstatic->common.function_name));
  2145. }
  2146. __callstatic->common.fn_flags |= ZEND_ACC_STATIC;
  2147. }
  2148. if (__tostring) {
  2149. if (__tostring->common.fn_flags & ZEND_ACC_STATIC) {
  2150. zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__tostring->common.function_name));
  2151. }
  2152. __tostring->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
  2153. }
  2154. if (__get) {
  2155. if (__get->common.fn_flags & ZEND_ACC_STATIC) {
  2156. zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__get->common.function_name));
  2157. }
  2158. __get->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
  2159. }
  2160. if (__set) {
  2161. if (__set->common.fn_flags & ZEND_ACC_STATIC) {
  2162. zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__set->common.function_name));
  2163. }
  2164. __set->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
  2165. }
  2166. if (__unset) {
  2167. if (__unset->common.fn_flags & ZEND_ACC_STATIC) {
  2168. zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__unset->common.function_name));
  2169. }
  2170. __unset->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
  2171. }
  2172. if (__isset) {
  2173. if (__isset->common.fn_flags & ZEND_ACC_STATIC) {
  2174. zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__isset->common.function_name));
  2175. }
  2176. __isset->common.fn_flags &= ~ZEND_ACC_ALLOW_STATIC;
  2177. }
  2178. if (__debugInfo) {
  2179. if (__debugInfo->common.fn_flags & ZEND_ACC_STATIC) {
  2180. zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__debugInfo->common.function_name));
  2181. }
  2182. }
  2183. if (ctor && ctor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE && ctor->common.fn_flags & ZEND_ACC_CTOR) {
  2184. zend_error_noreturn(E_CORE_ERROR, "Constructor %s::%s() cannot declare a return type", ZSTR_VAL(scope->name), ZSTR_VAL(ctor->common.function_name));
  2185. }
  2186. if (dtor && dtor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE && dtor->common.fn_flags & ZEND_ACC_DTOR) {
  2187. zend_error_noreturn(E_CORE_ERROR, "Destructor %s::%s() cannot declare a return type", ZSTR_VAL(scope->name), ZSTR_VAL(dtor->common.function_name));
  2188. }
  2189. if (clone && (clone->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
  2190. zend_error_noreturn(E_CORE_ERROR, "%s::%s() cannot declare a return type", ZSTR_VAL(scope->name), ZSTR_VAL(clone->common.function_name));
  2191. }
  2192. efree((char*)lc_class_name);
  2193. }
  2194. return SUCCESS;
  2195. }
  2196. /* }}} */
  2197. /* count=-1 means erase all functions, otherwise,
  2198. * erase the first count functions
  2199. */
  2200. ZEND_API void zend_unregister_functions(const zend_function_entry *functions, int count, HashTable *function_table) /* {{{ */
  2201. {
  2202. const zend_function_entry *ptr = functions;
  2203. int i=0;
  2204. HashTable *target_function_table = function_table;
  2205. zend_string *lowercase_name;
  2206. size_t fname_len;
  2207. if (!target_function_table) {
  2208. target_function_table = CG(function_table);
  2209. }
  2210. while (ptr->fname) {
  2211. if (count!=-1 && i>=count) {
  2212. break;
  2213. }
  2214. fname_len = strlen(ptr->fname);
  2215. lowercase_name = zend_string_alloc(fname_len, 0);
  2216. zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ptr->fname, fname_len);
  2217. zend_hash_del(target_function_table, lowercase_name);
  2218. zend_string_efree(lowercase_name);
  2219. ptr++;
  2220. i++;
  2221. }
  2222. }
  2223. /* }}} */
  2224. ZEND_API int zend_startup_module(zend_module_entry *module) /* {{{ */
  2225. {
  2226. if ((module = zend_register_internal_module(module)) != NULL && zend_startup_module_ex(module) == SUCCESS) {
  2227. return SUCCESS;
  2228. }
  2229. return FAILURE;
  2230. }
  2231. /* }}} */
  2232. ZEND_API int zend_get_module_started(const char *module_name) /* {{{ */
  2233. {
  2234. zend_module_entry *module;
  2235. module = zend_hash_str_find_ptr(&module_registry, module_name, strlen(module_name));
  2236. return (module && module->module_started) ? SUCCESS : FAILURE;
  2237. }
  2238. /* }}} */
  2239. static int clean_module_class(zval *el, void *arg) /* {{{ */
  2240. {
  2241. zend_class_entry *ce = (zend_class_entry *)Z_PTR_P(el);
  2242. int module_number = *(int *)arg;
  2243. if (ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module->module_number == module_number) {
  2244. return ZEND_HASH_APPLY_REMOVE;
  2245. } else {
  2246. return ZEND_HASH_APPLY_KEEP;
  2247. }
  2248. }
  2249. /* }}} */
  2250. static void clean_module_classes(int module_number) /* {{{ */
  2251. {
  2252. zend_hash_apply_with_argument(EG(class_table), clean_module_class, (void *) &module_number);
  2253. }
  2254. /* }}} */
  2255. void module_destructor(zend_module_entry *module) /* {{{ */
  2256. {
  2257. if (module->type == MODULE_TEMPORARY) {
  2258. zend_clean_module_rsrc_dtors(module->module_number);
  2259. clean_module_constants(module->module_number);
  2260. clean_module_classes(module->module_number);
  2261. }
  2262. if (module->module_started && module->module_shutdown_func) {
  2263. #if 0
  2264. zend_printf("%s: Module shutdown\n", module->name);
  2265. #endif
  2266. module->module_shutdown_func(module->type, module->module_number);
  2267. }
  2268. /* Deinitilaise module globals */
  2269. if (module->globals_size) {
  2270. #ifdef ZTS
  2271. if (*module->globals_id_ptr) {
  2272. ts_free_id(*module->globals_id_ptr);
  2273. }
  2274. #else
  2275. if (module->globals_dtor) {
  2276. module->globals_dtor(module->globals_ptr);
  2277. }
  2278. #endif
  2279. }
  2280. module->module_started=0;
  2281. if (module->type == MODULE_TEMPORARY && module->functions) {
  2282. zend_unregister_functions(module->functions, -1, NULL);
  2283. }
  2284. #if HAVE_LIBDL
  2285. if (module->handle && !getenv("ZEND_DONT_UNLOAD_MODULES")) {
  2286. DL_UNLOAD(module->handle);
  2287. }
  2288. #endif
  2289. }
  2290. /* }}} */
  2291. ZEND_API void zend_activate_modules(void) /* {{{ */
  2292. {
  2293. zend_module_entry **p = module_request_startup_handlers;
  2294. while (*p) {
  2295. zend_module_entry *module = *p;
  2296. if (module->request_startup_func(module->type, module->module_number)==FAILURE) {
  2297. zend_error(E_WARNING, "request_startup() for %s module failed", module->name);
  2298. exit(1);
  2299. }
  2300. p++;
  2301. }
  2302. }
  2303. /* }}} */
  2304. /* call request shutdown for all modules */
  2305. static int module_registry_cleanup(zval *zv) /* {{{ */
  2306. {
  2307. zend_module_entry *module = Z_PTR_P(zv);
  2308. if (module->request_shutdown_func) {
  2309. #if 0
  2310. zend_printf("%s: Request shutdown\n", module->name);
  2311. #endif
  2312. module->request_shutdown_func(module->type, module->module_number);
  2313. }
  2314. return 0;
  2315. }
  2316. /* }}} */
  2317. ZEND_API void zend_deactivate_modules(void) /* {{{ */
  2318. {
  2319. EG(current_execute_data) = NULL; /* we're no longer executing anything */
  2320. zend_try {
  2321. if (EG(full_tables_cleanup)) {
  2322. zend_hash_reverse_apply(&module_registry, module_registry_cleanup);
  2323. } else {
  2324. zend_module_entry **p = module_request_shutdown_handlers;
  2325. while (*p) {
  2326. zend_module_entry *module = *p;
  2327. module->request_shutdown_func(module->type, module->module_number);
  2328. p++;
  2329. }
  2330. }
  2331. } zend_end_try();
  2332. }
  2333. /* }}} */
  2334. ZEND_API void zend_cleanup_internal_classes(void) /* {{{ */
  2335. {
  2336. zend_class_entry **p = class_cleanup_handlers;
  2337. while (*p) {
  2338. zend_cleanup_internal_class_data(*p);
  2339. p++;
  2340. }
  2341. }
  2342. /* }}} */
  2343. int module_registry_unload_temp(const zend_module_entry *module) /* {{{ */
  2344. {
  2345. return (module->type == MODULE_TEMPORARY) ? ZEND_HASH_APPLY_REMOVE : ZEND_HASH_APPLY_STOP;
  2346. }
  2347. /* }}} */
  2348. static int module_registry_unload_temp_wrapper(zval *el) /* {{{ */
  2349. {
  2350. zend_module_entry *module = (zend_module_entry *)Z_PTR_P(el);
  2351. return module_registry_unload_temp((const zend_module_entry *)module);
  2352. }
  2353. /* }}} */
  2354. static int exec_done_cb(zval *el) /* {{{ */
  2355. {
  2356. zend_module_entry *module = (zend_module_entry *)Z_PTR_P(el);
  2357. if (module->post_deactivate_func) {
  2358. module->post_deactivate_func();
  2359. }
  2360. return 0;
  2361. }
  2362. /* }}} */
  2363. ZEND_API void zend_post_deactivate_modules(void) /* {{{ */
  2364. {
  2365. if (EG(full_tables_cleanup)) {
  2366. zend_hash_apply(&module_registry, exec_done_cb);
  2367. zend_hash_reverse_apply(&module_registry, module_registry_unload_temp_wrapper);
  2368. } else {
  2369. zend_module_entry **p = module_post_deactivate_handlers;
  2370. while (*p) {
  2371. zend_module_entry *module = *p;
  2372. module->post_deactivate_func();
  2373. p++;
  2374. }
  2375. }
  2376. }
  2377. /* }}} */
  2378. /* return the next free module number */
  2379. ZEND_API int zend_next_free_module(void) /* {{{ */
  2380. {
  2381. return zend_hash_num_elements(&module_registry) + 1;
  2382. }
  2383. /* }}} */
  2384. static zend_class_entry *do_register_internal_class(zend_class_entry *orig_class_entry, uint32_t ce_flags) /* {{{ */
  2385. {
  2386. zend_class_entry *class_entry = malloc(sizeof(zend_class_entry));
  2387. zend_string *lowercase_name;
  2388. *class_entry = *orig_class_entry;
  2389. class_entry->type = ZEND_INTERNAL_CLASS;
  2390. zend_initialize_class_data(class_entry, 0);
  2391. class_entry->ce_flags = ce_flags | ZEND_ACC_CONSTANTS_UPDATED;
  2392. class_entry->info.internal.module = EG(current_module);
  2393. if (class_entry->info.internal.builtin_functions) {
  2394. zend_register_functions(class_entry, class_entry->info.internal.builtin_functions, &class_entry->function_table, EG(current_module)->type);
  2395. }
  2396. lowercase_name = zend_string_tolower_ex(orig_class_entry->name, EG(current_module)->type == MODULE_PERSISTENT);
  2397. lowercase_name = zend_new_interned_string(lowercase_name);
  2398. zend_hash_update_ptr(CG(class_table), lowercase_name, class_entry);
  2399. zend_string_release_ex(lowercase_name, 1);
  2400. return class_entry;
  2401. }
  2402. /* }}} */
  2403. /* If parent_ce is not NULL then it inherits from parent_ce
  2404. * If parent_ce is NULL and parent_name isn't then it looks for the parent and inherits from it
  2405. * If both parent_ce and parent_name are NULL it does a regular class registration
  2406. * If parent_name is specified but not found NULL is returned
  2407. */
  2408. ZEND_API zend_class_entry *zend_register_internal_class_ex(zend_class_entry *class_entry, zend_class_entry *parent_ce) /* {{{ */
  2409. {
  2410. zend_class_entry *register_class;
  2411. register_class = zend_register_internal_class(class_entry);
  2412. if (parent_ce) {
  2413. zend_do_inheritance(register_class, parent_ce);
  2414. }
  2415. return register_class;
  2416. }
  2417. /* }}} */
  2418. ZEND_API void zend_class_implements(zend_class_entry *class_entry, int num_interfaces, ...) /* {{{ */
  2419. {
  2420. zend_class_entry *interface_entry;
  2421. va_list interface_list;
  2422. va_start(interface_list, num_interfaces);
  2423. while (num_interfaces--) {
  2424. interface_entry = va_arg(interface_list, zend_class_entry *);
  2425. zend_do_implement_interface(class_entry, interface_entry);
  2426. }
  2427. va_end(interface_list);
  2428. }
  2429. /* }}} */
  2430. /* A class that contains at least one abstract method automatically becomes an abstract class.
  2431. */
  2432. ZEND_API zend_class_entry *zend_register_internal_class(zend_class_entry *orig_class_entry) /* {{{ */
  2433. {
  2434. return do_register_internal_class(orig_class_entry, 0);
  2435. }
  2436. /* }}} */
  2437. ZEND_API zend_class_entry *zend_register_internal_interface(zend_class_entry *orig_class_entry) /* {{{ */
  2438. {
  2439. return do_register_internal_class(orig_class_entry, ZEND_ACC_INTERFACE);
  2440. }
  2441. /* }}} */
  2442. ZEND_API int zend_register_class_alias_ex(const char *name, size_t name_len, zend_class_entry *ce, int persistent) /* {{{ */
  2443. {
  2444. zend_string *lcname;
  2445. /* TODO: Move this out of here in 7.4. */
  2446. if (persistent && EG(current_module) && EG(current_module)->type == MODULE_TEMPORARY) {
  2447. persistent = 0;
  2448. }
  2449. if (name[0] == '\\') {
  2450. lcname = zend_string_alloc(name_len-1, persistent);
  2451. zend_str_tolower_copy(ZSTR_VAL(lcname), name+1, name_len-1);
  2452. } else {
  2453. lcname = zend_string_alloc(name_len, persistent);
  2454. zend_str_tolower_copy(ZSTR_VAL(lcname), name, name_len);
  2455. }
  2456. zend_assert_valid_class_name(lcname);
  2457. lcname = zend_new_interned_string(lcname);
  2458. ce = zend_hash_add_ptr(CG(class_table), lcname, ce);
  2459. zend_string_release_ex(lcname, 0);
  2460. if (ce) {
  2461. ce->refcount++;
  2462. return SUCCESS;
  2463. }
  2464. return FAILURE;
  2465. }
  2466. /* }}} */
  2467. ZEND_API int zend_set_hash_symbol(zval *symbol, const char *name, int name_length, zend_bool is_ref, int num_symbol_tables, ...) /* {{{ */
  2468. {
  2469. HashTable *symbol_table;
  2470. va_list symbol_table_list;
  2471. if (num_symbol_tables <= 0) return FAILURE;
  2472. if (is_ref) {
  2473. ZVAL_MAKE_REF(symbol);
  2474. }
  2475. va_start(symbol_table_list, num_symbol_tables);
  2476. while (num_symbol_tables-- > 0) {
  2477. symbol_table = va_arg(symbol_table_list, HashTable *);
  2478. zend_hash_str_update(symbol_table, name, name_length, symbol);
  2479. Z_TRY_ADDREF_P(symbol);
  2480. }
  2481. va_end(symbol_table_list);
  2482. return SUCCESS;
  2483. }
  2484. /* }}} */
  2485. /* Disabled functions support */
  2486. /* {{{ proto void display_disabled_function(void)
  2487. Dummy function which displays an error when a disabled function is called. */
  2488. ZEND_API ZEND_FUNCTION(display_disabled_function)
  2489. {
  2490. zend_error(E_WARNING, "%s() has been disabled for security reasons", get_active_function_name());
  2491. }
  2492. /* }}} */
  2493. ZEND_API int zend_disable_function(char *function_name, size_t function_name_length) /* {{{ */
  2494. {
  2495. zend_internal_function *func;
  2496. if ((func = zend_hash_str_find_ptr(CG(function_table), function_name, function_name_length))) {
  2497. func->fn_flags &= ~(ZEND_ACC_VARIADIC | ZEND_ACC_HAS_TYPE_HINTS | ZEND_ACC_HAS_RETURN_TYPE);
  2498. func->num_args = 0;
  2499. func->arg_info = NULL;
  2500. func->handler = ZEND_FN(display_disabled_function);
  2501. return SUCCESS;
  2502. }
  2503. return FAILURE;
  2504. }
  2505. /* }}} */
  2506. #ifdef ZEND_WIN32
  2507. #pragma optimize("", off)
  2508. #endif
  2509. static zend_object *display_disabled_class(zend_class_entry *class_type) /* {{{ */
  2510. {
  2511. zend_object *intern;
  2512. intern = zend_objects_new(class_type);
  2513. /* Initialize default properties */
  2514. if (EXPECTED(class_type->default_properties_count != 0)) {
  2515. zval *p = intern->properties_table;
  2516. zval *end = p + class_type->default_properties_count;
  2517. do {
  2518. ZVAL_UNDEF(p);
  2519. p++;
  2520. } while (p != end);
  2521. }
  2522. zend_error(E_WARNING, "%s() has been disabled for security reasons", ZSTR_VAL(class_type->name));
  2523. return intern;
  2524. }
  2525. #ifdef ZEND_WIN32
  2526. #pragma optimize("", on)
  2527. #endif
  2528. /* }}} */
  2529. static const zend_function_entry disabled_class_new[] = {
  2530. ZEND_FE_END
  2531. };
  2532. ZEND_API int zend_disable_class(char *class_name, size_t class_name_length) /* {{{ */
  2533. {
  2534. zend_class_entry *disabled_class;
  2535. zend_string *key;
  2536. key = zend_string_alloc(class_name_length, 0);
  2537. zend_str_tolower_copy(ZSTR_VAL(key), class_name, class_name_length);
  2538. disabled_class = zend_hash_find_ptr(CG(class_table), key);
  2539. zend_string_release_ex(key, 0);
  2540. if (!disabled_class) {
  2541. return FAILURE;
  2542. }
  2543. INIT_CLASS_ENTRY_INIT_METHODS((*disabled_class), disabled_class_new);
  2544. disabled_class->create_object = display_disabled_class;
  2545. zend_hash_clean(&disabled_class->function_table);
  2546. return SUCCESS;
  2547. }
  2548. /* }}} */
  2549. static int zend_is_callable_check_class(zend_string *name, zend_class_entry *scope, zend_fcall_info_cache *fcc, int *strict_class, char **error) /* {{{ */
  2550. {
  2551. int ret = 0;
  2552. zend_class_entry *ce;
  2553. size_t name_len = ZSTR_LEN(name);
  2554. zend_string *lcname;
  2555. ALLOCA_FLAG(use_heap);
  2556. ZSTR_ALLOCA_ALLOC(lcname, name_len, use_heap);
  2557. zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(name), name_len);
  2558. *strict_class = 0;
  2559. if (zend_string_equals_literal(lcname, "self")) {
  2560. if (!scope) {
  2561. if (error) *error = estrdup("cannot access self:: when no class scope is active");
  2562. } else {
  2563. fcc->called_scope = zend_get_called_scope(EG(current_execute_data));
  2564. fcc->calling_scope = scope;
  2565. if (!fcc->object) {
  2566. fcc->object = zend_get_this_object(EG(current_execute_data));
  2567. }
  2568. ret = 1;
  2569. }
  2570. } else if (zend_string_equals_literal(lcname, "parent")) {
  2571. if (!scope) {
  2572. if (error) *error = estrdup("cannot access parent:: when no class scope is active");
  2573. } else if (!scope->parent) {
  2574. if (error) *error = estrdup("cannot access parent:: when current class scope has no parent");
  2575. } else {
  2576. fcc->called_scope = zend_get_called_scope(EG(current_execute_data));
  2577. fcc->calling_scope = scope->parent;
  2578. if (!fcc->object) {
  2579. fcc->object = zend_get_this_object(EG(current_execute_data));
  2580. }
  2581. *strict_class = 1;
  2582. ret = 1;
  2583. }
  2584. } else if (zend_string_equals_literal(lcname, "static")) {
  2585. zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data));
  2586. if (!called_scope) {
  2587. if (error) *error = estrdup("cannot access static:: when no class scope is active");
  2588. } else {
  2589. fcc->called_scope = called_scope;
  2590. fcc->calling_scope = called_scope;
  2591. if (!fcc->object) {
  2592. fcc->object = zend_get_this_object(EG(current_execute_data));
  2593. }
  2594. *strict_class = 1;
  2595. ret = 1;
  2596. }
  2597. } else if ((ce = zend_lookup_class_ex(name, NULL, 1)) != NULL) {
  2598. zend_class_entry *scope;
  2599. zend_execute_data *ex = EG(current_execute_data);
  2600. while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
  2601. ex = ex->prev_execute_data;
  2602. }
  2603. scope = ex ? ex->func->common.scope : NULL;
  2604. fcc->calling_scope = ce;
  2605. if (scope && !fcc->object) {
  2606. zend_object *object = zend_get_this_object(EG(current_execute_data));
  2607. if (object &&
  2608. instanceof_function(object->ce, scope) &&
  2609. instanceof_function(scope, ce)) {
  2610. fcc->object = object;
  2611. fcc->called_scope = object->ce;
  2612. } else {
  2613. fcc->called_scope = ce;
  2614. }
  2615. } else {
  2616. fcc->called_scope = fcc->object ? fcc->object->ce : ce;
  2617. }
  2618. *strict_class = 1;
  2619. ret = 1;
  2620. } else {
  2621. if (error) zend_spprintf(error, 0, "class '%.*s' not found", (int)name_len, ZSTR_VAL(name));
  2622. }
  2623. ZSTR_ALLOCA_FREE(lcname, use_heap);
  2624. return ret;
  2625. }
  2626. /* }}} */
  2627. static zend_always_inline int zend_is_callable_check_func(int check_flags, zval *callable, zend_fcall_info_cache *fcc, int strict_class, char **error) /* {{{ */
  2628. {
  2629. zend_class_entry *ce_org = fcc->calling_scope;
  2630. int retval = 0;
  2631. zend_string *mname, *cname;
  2632. zend_string *lmname;
  2633. const char *colon;
  2634. size_t clen;
  2635. HashTable *ftable;
  2636. int call_via_handler = 0;
  2637. zend_class_entry *scope;
  2638. zval *zv;
  2639. ALLOCA_FLAG(use_heap)
  2640. fcc->calling_scope = NULL;
  2641. if (!ce_org) {
  2642. zend_function *func;
  2643. zend_string *lmname;
  2644. /* Check if function with given name exists.
  2645. * This may be a compound name that includes namespace name */
  2646. if (UNEXPECTED(Z_STRVAL_P(callable)[0] == '\\')) {
  2647. /* Skip leading \ */
  2648. ZSTR_ALLOCA_ALLOC(lmname, Z_STRLEN_P(callable) - 1, use_heap);
  2649. zend_str_tolower_copy(ZSTR_VAL(lmname), Z_STRVAL_P(callable) + 1, Z_STRLEN_P(callable) - 1);
  2650. func = zend_fetch_function(lmname);
  2651. ZSTR_ALLOCA_FREE(lmname, use_heap);
  2652. } else {
  2653. lmname = Z_STR_P(callable);
  2654. func = zend_fetch_function(lmname);
  2655. if (!func) {
  2656. ZSTR_ALLOCA_ALLOC(lmname, Z_STRLEN_P(callable), use_heap);
  2657. zend_str_tolower_copy(ZSTR_VAL(lmname), Z_STRVAL_P(callable), Z_STRLEN_P(callable));
  2658. func = zend_fetch_function(lmname);
  2659. ZSTR_ALLOCA_FREE(lmname, use_heap);
  2660. }
  2661. }
  2662. if (EXPECTED(func != NULL)) {
  2663. fcc->function_handler = func;
  2664. return 1;
  2665. }
  2666. }
  2667. /* Split name into class/namespace and method/function names */
  2668. if ((colon = zend_memrchr(Z_STRVAL_P(callable), ':', Z_STRLEN_P(callable))) != NULL &&
  2669. colon > Z_STRVAL_P(callable) &&
  2670. *(colon-1) == ':'
  2671. ) {
  2672. size_t mlen;
  2673. colon--;
  2674. clen = colon - Z_STRVAL_P(callable);
  2675. mlen = Z_STRLEN_P(callable) - clen - 2;
  2676. if (colon == Z_STRVAL_P(callable)) {
  2677. if (error) *error = estrdup("invalid function name");
  2678. return 0;
  2679. }
  2680. /* This is a compound name.
  2681. * Try to fetch class and then find static method. */
  2682. if (ce_org) {
  2683. scope = ce_org;
  2684. } else {
  2685. scope = zend_get_executed_scope();
  2686. }
  2687. cname = zend_string_init(Z_STRVAL_P(callable), clen, 0);
  2688. if (!zend_is_callable_check_class(cname, scope, fcc, &strict_class, error)) {
  2689. zend_string_release_ex(cname, 0);
  2690. return 0;
  2691. }
  2692. zend_string_release_ex(cname, 0);
  2693. ftable = &fcc->calling_scope->function_table;
  2694. if (ce_org && !instanceof_function(ce_org, fcc->calling_scope)) {
  2695. if (error) zend_spprintf(error, 0, "class '%s' is not a subclass of '%s'", ZSTR_VAL(ce_org->name), ZSTR_VAL(fcc->calling_scope->name));
  2696. return 0;
  2697. }
  2698. mname = zend_string_init(Z_STRVAL_P(callable) + clen + 2, mlen, 0);
  2699. } else if (ce_org) {
  2700. /* Try to fetch find static method of given class. */
  2701. mname = Z_STR_P(callable);
  2702. zend_string_addref(mname);
  2703. ftable = &ce_org->function_table;
  2704. fcc->calling_scope = ce_org;
  2705. } else {
  2706. /* We already checked for plain function before. */
  2707. if (error && !(check_flags & IS_CALLABLE_CHECK_SILENT)) {
  2708. zend_spprintf(error, 0, "function '%s' not found or invalid function name", Z_STRVAL_P(callable));
  2709. }
  2710. return 0;
  2711. }
  2712. lmname = zend_string_tolower(mname);
  2713. if (strict_class &&
  2714. fcc->calling_scope &&
  2715. zend_string_equals_literal(lmname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
  2716. fcc->function_handler = fcc->calling_scope->constructor;
  2717. if (fcc->function_handler) {
  2718. retval = 1;
  2719. }
  2720. } else if ((zv = zend_hash_find(ftable, lmname)) != NULL) {
  2721. fcc->function_handler = Z_PTR_P(zv);
  2722. retval = 1;
  2723. if ((fcc->function_handler->op_array.fn_flags & ZEND_ACC_CHANGED) &&
  2724. !strict_class) {
  2725. scope = zend_get_executed_scope();
  2726. if (scope &&
  2727. instanceof_function(fcc->function_handler->common.scope, scope)) {
  2728. zv = zend_hash_find(&scope->function_table, lmname);
  2729. if (zv != NULL) {
  2730. zend_function *priv_fbc = Z_PTR_P(zv);
  2731. if (priv_fbc->common.fn_flags & ZEND_ACC_PRIVATE
  2732. && priv_fbc->common.scope == scope) {
  2733. fcc->function_handler = priv_fbc;
  2734. }
  2735. }
  2736. }
  2737. }
  2738. if ((check_flags & IS_CALLABLE_CHECK_NO_ACCESS) == 0 &&
  2739. (fcc->calling_scope &&
  2740. ((fcc->object && fcc->calling_scope->__call) ||
  2741. (!fcc->object && fcc->calling_scope->__callstatic)))) {
  2742. if (fcc->function_handler->op_array.fn_flags & ZEND_ACC_PRIVATE) {
  2743. scope = zend_get_executed_scope();
  2744. if (!zend_check_private(fcc->function_handler, fcc->object ? fcc->object->ce : scope, lmname)) {
  2745. retval = 0;
  2746. fcc->function_handler = NULL;
  2747. goto get_function_via_handler;
  2748. }
  2749. } else if (fcc->function_handler->common.fn_flags & ZEND_ACC_PROTECTED) {
  2750. scope = zend_get_executed_scope();
  2751. if (!zend_check_protected(fcc->function_handler->common.scope, scope)) {
  2752. retval = 0;
  2753. fcc->function_handler = NULL;
  2754. goto get_function_via_handler;
  2755. }
  2756. }
  2757. }
  2758. } else {
  2759. get_function_via_handler:
  2760. if (fcc->object && fcc->calling_scope == ce_org) {
  2761. if (strict_class && ce_org->__call) {
  2762. fcc->function_handler = zend_get_call_trampoline_func(ce_org, mname, 0);
  2763. call_via_handler = 1;
  2764. retval = 1;
  2765. } else if (fcc->object->handlers->get_method) {
  2766. fcc->function_handler = fcc->object->handlers->get_method(&fcc->object, mname, NULL);
  2767. if (fcc->function_handler) {
  2768. if (strict_class &&
  2769. (!fcc->function_handler->common.scope ||
  2770. !instanceof_function(ce_org, fcc->function_handler->common.scope))) {
  2771. if (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
  2772. if (fcc->function_handler->type != ZEND_OVERLOADED_FUNCTION &&
  2773. fcc->function_handler->common.function_name) {
  2774. zend_string_release_ex(fcc->function_handler->common.function_name, 0);
  2775. }
  2776. zend_free_trampoline(fcc->function_handler);
  2777. }
  2778. } else {
  2779. retval = 1;
  2780. call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0;
  2781. }
  2782. }
  2783. }
  2784. } else if (fcc->calling_scope) {
  2785. if (fcc->calling_scope->get_static_method) {
  2786. fcc->function_handler = fcc->calling_scope->get_static_method(fcc->calling_scope, mname);
  2787. } else {
  2788. fcc->function_handler = zend_std_get_static_method(fcc->calling_scope, mname, NULL);
  2789. }
  2790. if (fcc->function_handler) {
  2791. retval = 1;
  2792. call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0;
  2793. if (call_via_handler && !fcc->object) {
  2794. zend_object *object = zend_get_this_object(EG(current_execute_data));
  2795. if (object &&
  2796. instanceof_function(object->ce, fcc->calling_scope)) {
  2797. fcc->object = object;
  2798. }
  2799. }
  2800. }
  2801. }
  2802. }
  2803. if (retval) {
  2804. if (fcc->calling_scope && !call_via_handler) {
  2805. if (fcc->function_handler->common.fn_flags & ZEND_ACC_ABSTRACT) {
  2806. if (error) {
  2807. zend_spprintf(error, 0, "cannot call abstract method %s::%s()", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
  2808. retval = 0;
  2809. } else {
  2810. zend_throw_error(NULL, "Cannot call abstract method %s::%s()", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
  2811. retval = 0;
  2812. }
  2813. } else if (!fcc->object && !(fcc->function_handler->common.fn_flags & ZEND_ACC_STATIC)) {
  2814. int severity;
  2815. char *verb;
  2816. if (fcc->function_handler->common.fn_flags & ZEND_ACC_ALLOW_STATIC) {
  2817. severity = E_DEPRECATED;
  2818. verb = "should not";
  2819. } else {
  2820. /* An internal function assumes $this is present and won't check that. So PHP would crash by allowing the call. */
  2821. severity = E_ERROR;
  2822. verb = "cannot";
  2823. }
  2824. if ((check_flags & IS_CALLABLE_CHECK_IS_STATIC) != 0) {
  2825. retval = 0;
  2826. }
  2827. if (error) {
  2828. zend_spprintf(error, 0, "non-static method %s::%s() %s be called statically", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name), verb);
  2829. if (severity != E_DEPRECATED) {
  2830. retval = 0;
  2831. }
  2832. } else if (retval) {
  2833. if (severity == E_ERROR) {
  2834. zend_throw_error(NULL, "Non-static method %s::%s() %s be called statically", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name), verb);
  2835. } else {
  2836. zend_error(severity, "Non-static method %s::%s() %s be called statically", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name), verb);
  2837. }
  2838. }
  2839. }
  2840. if (retval && (check_flags & IS_CALLABLE_CHECK_NO_ACCESS) == 0) {
  2841. if (fcc->function_handler->op_array.fn_flags & ZEND_ACC_PRIVATE) {
  2842. scope = zend_get_executed_scope();
  2843. if (!zend_check_private(fcc->function_handler, fcc->object ? fcc->object->ce : scope, lmname)) {
  2844. if (error) {
  2845. if (*error) {
  2846. efree(*error);
  2847. }
  2848. zend_spprintf(error, 0, "cannot access private method %s::%s()", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
  2849. }
  2850. retval = 0;
  2851. }
  2852. } else if ((fcc->function_handler->common.fn_flags & ZEND_ACC_PROTECTED)) {
  2853. scope = zend_get_executed_scope();
  2854. if (!zend_check_protected(fcc->function_handler->common.scope, scope)) {
  2855. if (error) {
  2856. if (*error) {
  2857. efree(*error);
  2858. }
  2859. zend_spprintf(error, 0, "cannot access protected method %s::%s()", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
  2860. }
  2861. retval = 0;
  2862. }
  2863. }
  2864. }
  2865. }
  2866. } else if (error && !(check_flags & IS_CALLABLE_CHECK_SILENT)) {
  2867. if (fcc->calling_scope) {
  2868. if (error) zend_spprintf(error, 0, "class '%s' does not have a method '%s'", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(mname));
  2869. } else {
  2870. if (error) zend_spprintf(error, 0, "function '%s' does not exist", ZSTR_VAL(mname));
  2871. }
  2872. }
  2873. zend_string_release_ex(lmname, 0);
  2874. zend_string_release_ex(mname, 0);
  2875. if (fcc->object) {
  2876. fcc->called_scope = fcc->object->ce;
  2877. }
  2878. return retval;
  2879. }
  2880. /* }}} */
  2881. static zend_string *zend_create_method_string(zend_string *class_name, zend_string *method_name) {
  2882. zend_string *callable_name = zend_string_alloc(
  2883. ZSTR_LEN(class_name) + ZSTR_LEN(method_name) + sizeof("::") - 1, 0);
  2884. char *ptr = ZSTR_VAL(callable_name);
  2885. memcpy(ptr, ZSTR_VAL(class_name), ZSTR_LEN(class_name));
  2886. ptr += ZSTR_LEN(class_name);
  2887. memcpy(ptr, "::", sizeof("::") - 1);
  2888. ptr += sizeof("::") - 1;
  2889. memcpy(ptr, ZSTR_VAL(method_name), ZSTR_LEN(method_name) + 1);
  2890. return callable_name;
  2891. }
  2892. ZEND_API zend_string *zend_get_callable_name_ex(zval *callable, zend_object *object) /* {{{ */
  2893. {
  2894. try_again:
  2895. switch (Z_TYPE_P(callable)) {
  2896. case IS_STRING:
  2897. if (object) {
  2898. return zend_create_method_string(object->ce->name, Z_STR_P(callable));
  2899. }
  2900. return zend_string_copy(Z_STR_P(callable));
  2901. case IS_ARRAY:
  2902. {
  2903. zval *method = NULL;
  2904. zval *obj = NULL;
  2905. if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
  2906. obj = zend_hash_index_find_deref(Z_ARRVAL_P(callable), 0);
  2907. method = zend_hash_index_find_deref(Z_ARRVAL_P(callable), 1);
  2908. }
  2909. if (obj == NULL || method == NULL || Z_TYPE_P(method) != IS_STRING) {
  2910. return zend_string_init("Array", sizeof("Array")-1, 0);
  2911. }
  2912. if (Z_TYPE_P(obj) == IS_STRING) {
  2913. return zend_create_method_string(Z_STR_P(obj), Z_STR_P(method));
  2914. } else if (Z_TYPE_P(obj) == IS_OBJECT) {
  2915. return zend_create_method_string(Z_OBJCE_P(obj)->name, Z_STR_P(method));
  2916. } else {
  2917. return zend_string_init("Array", sizeof("Array")-1, 0);
  2918. }
  2919. }
  2920. case IS_OBJECT:
  2921. {
  2922. zend_class_entry *calling_scope;
  2923. zend_function *fptr;
  2924. zend_object *object;
  2925. if (Z_OBJ_HANDLER_P(callable, get_closure)
  2926. && Z_OBJ_HANDLER_P(callable, get_closure)(callable, &calling_scope, &fptr, &object) == SUCCESS) {
  2927. zend_class_entry *ce = Z_OBJCE_P(callable);
  2928. zend_string *callable_name = zend_string_alloc(
  2929. ZSTR_LEN(ce->name) + sizeof("::__invoke") - 1, 0);
  2930. memcpy(ZSTR_VAL(callable_name), ZSTR_VAL(ce->name), ZSTR_LEN(ce->name));
  2931. memcpy(ZSTR_VAL(callable_name) + ZSTR_LEN(ce->name), "::__invoke", sizeof("::__invoke"));
  2932. return callable_name;
  2933. }
  2934. return zval_get_string(callable);
  2935. }
  2936. case IS_REFERENCE:
  2937. callable = Z_REFVAL_P(callable);
  2938. goto try_again;
  2939. default:
  2940. return zval_get_string_func(callable);
  2941. }
  2942. }
  2943. /* }}} */
  2944. ZEND_API zend_string *zend_get_callable_name(zval *callable) /* {{{ */
  2945. {
  2946. return zend_get_callable_name_ex(callable, NULL);
  2947. }
  2948. /* }}} */
  2949. static zend_always_inline zend_bool zend_is_callable_impl(zval *callable, zend_object *object, uint32_t check_flags, zend_fcall_info_cache *fcc, char **error) /* {{{ */
  2950. {
  2951. zend_bool ret;
  2952. zend_fcall_info_cache fcc_local;
  2953. int strict_class = 0;
  2954. if (fcc == NULL) {
  2955. fcc = &fcc_local;
  2956. }
  2957. if (error) {
  2958. *error = NULL;
  2959. }
  2960. fcc->calling_scope = NULL;
  2961. fcc->called_scope = NULL;
  2962. fcc->function_handler = NULL;
  2963. fcc->object = NULL;
  2964. again:
  2965. switch (Z_TYPE_P(callable)) {
  2966. case IS_STRING:
  2967. if (object) {
  2968. fcc->object = object;
  2969. fcc->calling_scope = object->ce;
  2970. }
  2971. if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
  2972. fcc->called_scope = fcc->calling_scope;
  2973. return 1;
  2974. }
  2975. check_func:
  2976. ret = zend_is_callable_check_func(check_flags, callable, fcc, strict_class, error);
  2977. if (fcc == &fcc_local &&
  2978. fcc->function_handler &&
  2979. ((fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) ||
  2980. fcc->function_handler->type == ZEND_OVERLOADED_FUNCTION_TEMPORARY ||
  2981. fcc->function_handler->type == ZEND_OVERLOADED_FUNCTION)) {
  2982. if (fcc->function_handler->type != ZEND_OVERLOADED_FUNCTION &&
  2983. fcc->function_handler->common.function_name) {
  2984. zend_string_release_ex(fcc->function_handler->common.function_name, 0);
  2985. }
  2986. zend_free_trampoline(fcc->function_handler);
  2987. }
  2988. return ret;
  2989. case IS_ARRAY:
  2990. {
  2991. zval *method = NULL;
  2992. zval *obj = NULL;
  2993. if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
  2994. obj = zend_hash_index_find(Z_ARRVAL_P(callable), 0);
  2995. method = zend_hash_index_find(Z_ARRVAL_P(callable), 1);
  2996. }
  2997. do {
  2998. if (obj == NULL || method == NULL) {
  2999. break;
  3000. }
  3001. ZVAL_DEREF(method);
  3002. if (Z_TYPE_P(method) != IS_STRING) {
  3003. break;
  3004. }
  3005. ZVAL_DEREF(obj);
  3006. if (Z_TYPE_P(obj) == IS_STRING) {
  3007. if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
  3008. return 1;
  3009. }
  3010. if (!zend_is_callable_check_class(Z_STR_P(obj), zend_get_executed_scope(), fcc, &strict_class, error)) {
  3011. return 0;
  3012. }
  3013. } else if (Z_TYPE_P(obj) == IS_OBJECT) {
  3014. fcc->calling_scope = Z_OBJCE_P(obj); /* TBFixed: what if it's overloaded? */
  3015. fcc->object = Z_OBJ_P(obj);
  3016. if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
  3017. fcc->called_scope = fcc->calling_scope;
  3018. return 1;
  3019. }
  3020. } else {
  3021. break;
  3022. }
  3023. callable = method;
  3024. goto check_func;
  3025. } while (0);
  3026. if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
  3027. if (!obj || (!Z_ISREF_P(obj)?
  3028. (Z_TYPE_P(obj) != IS_STRING && Z_TYPE_P(obj) != IS_OBJECT) :
  3029. (Z_TYPE_P(Z_REFVAL_P(obj)) != IS_STRING && Z_TYPE_P(Z_REFVAL_P(obj)) != IS_OBJECT))) {
  3030. if (error) *error = estrdup("first array member is not a valid class name or object");
  3031. } else {
  3032. if (error) *error = estrdup("second array member is not a valid method");
  3033. }
  3034. } else {
  3035. if (error) *error = estrdup("array must have exactly two members");
  3036. }
  3037. }
  3038. return 0;
  3039. case IS_OBJECT:
  3040. if (Z_OBJ_HANDLER_P(callable, get_closure) && Z_OBJ_HANDLER_P(callable, get_closure)(callable, &fcc->calling_scope, &fcc->function_handler, &fcc->object) == SUCCESS) {
  3041. fcc->called_scope = fcc->calling_scope;
  3042. return 1;
  3043. }
  3044. if (error) *error = estrdup("no array or string given");
  3045. return 0;
  3046. case IS_REFERENCE:
  3047. callable = Z_REFVAL_P(callable);
  3048. goto again;
  3049. default:
  3050. if (error) *error = estrdup("no array or string given");
  3051. return 0;
  3052. }
  3053. }
  3054. /* }}} */
  3055. ZEND_API zend_bool zend_is_callable_ex(zval *callable, zend_object *object, uint32_t check_flags, zend_string **callable_name, zend_fcall_info_cache *fcc, char **error) /* {{{ */
  3056. {
  3057. zend_bool ret = zend_is_callable_impl(callable, object, check_flags, fcc, error);
  3058. if (callable_name) {
  3059. *callable_name = zend_get_callable_name_ex(callable, object);
  3060. }
  3061. return ret;
  3062. }
  3063. ZEND_API zend_bool zend_is_callable(zval *callable, uint32_t check_flags, zend_string **callable_name) /* {{{ */
  3064. {
  3065. return zend_is_callable_ex(callable, NULL, check_flags, callable_name, NULL, NULL);
  3066. }
  3067. /* }}} */
  3068. ZEND_API zend_bool zend_make_callable(zval *callable, zend_string **callable_name) /* {{{ */
  3069. {
  3070. zend_fcall_info_cache fcc;
  3071. if (zend_is_callable_ex(callable, NULL, IS_CALLABLE_STRICT, callable_name, &fcc, NULL)) {
  3072. if (Z_TYPE_P(callable) == IS_STRING && fcc.calling_scope) {
  3073. zval_ptr_dtor_str(callable);
  3074. array_init(callable);
  3075. add_next_index_str(callable, zend_string_copy(fcc.calling_scope->name));
  3076. add_next_index_str(callable, zend_string_copy(fcc.function_handler->common.function_name));
  3077. }
  3078. if (fcc.function_handler &&
  3079. ((fcc.function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) ||
  3080. fcc.function_handler->type == ZEND_OVERLOADED_FUNCTION_TEMPORARY ||
  3081. fcc.function_handler->type == ZEND_OVERLOADED_FUNCTION)) {
  3082. if (fcc.function_handler->type != ZEND_OVERLOADED_FUNCTION) {
  3083. zend_string_release_ex(fcc.function_handler->common.function_name, 0);
  3084. }
  3085. zend_free_trampoline(fcc.function_handler);
  3086. }
  3087. return 1;
  3088. }
  3089. return 0;
  3090. }
  3091. /* }}} */
  3092. ZEND_API int zend_fcall_info_init(zval *callable, uint32_t check_flags, zend_fcall_info *fci, zend_fcall_info_cache *fcc, zend_string **callable_name, char **error) /* {{{ */
  3093. {
  3094. if (!zend_is_callable_ex(callable, NULL, check_flags, callable_name, fcc, error)) {
  3095. return FAILURE;
  3096. }
  3097. fci->size = sizeof(*fci);
  3098. fci->object = fcc->object;
  3099. ZVAL_COPY_VALUE(&fci->function_name, callable);
  3100. fci->retval = NULL;
  3101. fci->param_count = 0;
  3102. fci->params = NULL;
  3103. fci->no_separation = 1;
  3104. return SUCCESS;
  3105. }
  3106. /* }}} */
  3107. ZEND_API void zend_fcall_info_args_clear(zend_fcall_info *fci, int free_mem) /* {{{ */
  3108. {
  3109. if (fci->params) {
  3110. zval *p = fci->params;
  3111. zval *end = p + fci->param_count;
  3112. while (p != end) {
  3113. i_zval_ptr_dtor(p ZEND_FILE_LINE_CC);
  3114. p++;
  3115. }
  3116. if (free_mem) {
  3117. efree(fci->params);
  3118. fci->params = NULL;
  3119. }
  3120. }
  3121. fci->param_count = 0;
  3122. }
  3123. /* }}} */
  3124. ZEND_API void zend_fcall_info_args_save(zend_fcall_info *fci, int *param_count, zval **params) /* {{{ */
  3125. {
  3126. *param_count = fci->param_count;
  3127. *params = fci->params;
  3128. fci->param_count = 0;
  3129. fci->params = NULL;
  3130. }
  3131. /* }}} */
  3132. ZEND_API void zend_fcall_info_args_restore(zend_fcall_info *fci, int param_count, zval *params) /* {{{ */
  3133. {
  3134. zend_fcall_info_args_clear(fci, 1);
  3135. fci->param_count = param_count;
  3136. fci->params = params;
  3137. }
  3138. /* }}} */
  3139. ZEND_API int zend_fcall_info_args_ex(zend_fcall_info *fci, zend_function *func, zval *args) /* {{{ */
  3140. {
  3141. zval *arg, *params;
  3142. uint32_t n = 1;
  3143. zend_fcall_info_args_clear(fci, !args);
  3144. if (!args) {
  3145. return SUCCESS;
  3146. }
  3147. if (Z_TYPE_P(args) != IS_ARRAY) {
  3148. return FAILURE;
  3149. }
  3150. fci->param_count = zend_hash_num_elements(Z_ARRVAL_P(args));
  3151. fci->params = params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
  3152. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(args), arg) {
  3153. if (func && !Z_ISREF_P(arg) && ARG_SHOULD_BE_SENT_BY_REF(func, n)) {
  3154. ZVAL_NEW_REF(params, arg);
  3155. Z_TRY_ADDREF_P(arg);
  3156. } else {
  3157. ZVAL_COPY(params, arg);
  3158. }
  3159. params++;
  3160. n++;
  3161. } ZEND_HASH_FOREACH_END();
  3162. return SUCCESS;
  3163. }
  3164. /* }}} */
  3165. ZEND_API int zend_fcall_info_args(zend_fcall_info *fci, zval *args) /* {{{ */
  3166. {
  3167. return zend_fcall_info_args_ex(fci, NULL, args);
  3168. }
  3169. /* }}} */
  3170. ZEND_API int zend_fcall_info_argp(zend_fcall_info *fci, int argc, zval *argv) /* {{{ */
  3171. {
  3172. int i;
  3173. if (argc < 0) {
  3174. return FAILURE;
  3175. }
  3176. zend_fcall_info_args_clear(fci, !argc);
  3177. if (argc) {
  3178. fci->param_count = argc;
  3179. fci->params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
  3180. for (i = 0; i < argc; ++i) {
  3181. ZVAL_COPY(&fci->params[i], &argv[i]);
  3182. }
  3183. }
  3184. return SUCCESS;
  3185. }
  3186. /* }}} */
  3187. ZEND_API int zend_fcall_info_argv(zend_fcall_info *fci, int argc, va_list *argv) /* {{{ */
  3188. {
  3189. int i;
  3190. zval *arg;
  3191. if (argc < 0) {
  3192. return FAILURE;
  3193. }
  3194. zend_fcall_info_args_clear(fci, !argc);
  3195. if (argc) {
  3196. fci->param_count = argc;
  3197. fci->params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
  3198. for (i = 0; i < argc; ++i) {
  3199. arg = va_arg(*argv, zval *);
  3200. ZVAL_COPY(&fci->params[i], arg);
  3201. }
  3202. }
  3203. return SUCCESS;
  3204. }
  3205. /* }}} */
  3206. ZEND_API int zend_fcall_info_argn(zend_fcall_info *fci, int argc, ...) /* {{{ */
  3207. {
  3208. int ret;
  3209. va_list argv;
  3210. va_start(argv, argc);
  3211. ret = zend_fcall_info_argv(fci, argc, &argv);
  3212. va_end(argv);
  3213. return ret;
  3214. }
  3215. /* }}} */
  3216. ZEND_API int zend_fcall_info_call(zend_fcall_info *fci, zend_fcall_info_cache *fcc, zval *retval_ptr, zval *args) /* {{{ */
  3217. {
  3218. zval retval, *org_params = NULL;
  3219. int result, org_count = 0;
  3220. fci->retval = retval_ptr ? retval_ptr : &retval;
  3221. if (args) {
  3222. zend_fcall_info_args_save(fci, &org_count, &org_params);
  3223. zend_fcall_info_args(fci, args);
  3224. }
  3225. result = zend_call_function(fci, fcc);
  3226. if (!retval_ptr && Z_TYPE(retval) != IS_UNDEF) {
  3227. zval_ptr_dtor(&retval);
  3228. }
  3229. if (args) {
  3230. zend_fcall_info_args_restore(fci, org_count, org_params);
  3231. }
  3232. return result;
  3233. }
  3234. /* }}} */
  3235. ZEND_API const char *zend_get_module_version(const char *module_name) /* {{{ */
  3236. {
  3237. zend_string *lname;
  3238. size_t name_len = strlen(module_name);
  3239. zend_module_entry *module;
  3240. lname = zend_string_alloc(name_len, 0);
  3241. zend_str_tolower_copy(ZSTR_VAL(lname), module_name, name_len);
  3242. module = zend_hash_find_ptr(&module_registry, lname);
  3243. zend_string_efree(lname);
  3244. return module ? module->version : NULL;
  3245. }
  3246. /* }}} */
  3247. static inline zend_string *zval_make_interned_string(zval *zv) /* {{{ */
  3248. {
  3249. ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
  3250. Z_STR_P(zv) = zend_new_interned_string(Z_STR_P(zv));
  3251. if (ZSTR_IS_INTERNED(Z_STR_P(zv))) {
  3252. Z_TYPE_FLAGS_P(zv) = 0;
  3253. }
  3254. return Z_STR_P(zv);
  3255. }
  3256. static zend_always_inline zend_bool is_persistent_class(zend_class_entry *ce) {
  3257. return (ce->type & ZEND_INTERNAL_CLASS)
  3258. && ce->info.internal.module->type == MODULE_PERSISTENT;
  3259. }
  3260. ZEND_API int zend_declare_property_ex(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment) /* {{{ */
  3261. {
  3262. zend_property_info *property_info, *property_info_ptr;
  3263. if (ce->type == ZEND_INTERNAL_CLASS) {
  3264. property_info = pemalloc(sizeof(zend_property_info), 1);
  3265. } else {
  3266. property_info = zend_arena_alloc(&CG(arena), sizeof(zend_property_info));
  3267. if (Z_TYPE_P(property) == IS_CONSTANT_AST) {
  3268. ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
  3269. }
  3270. }
  3271. if (Z_TYPE_P(property) == IS_STRING && !ZSTR_IS_INTERNED(Z_STR_P(property))) {
  3272. zval_make_interned_string(property);
  3273. }
  3274. if (!(access_type & ZEND_ACC_PPP_MASK)) {
  3275. access_type |= ZEND_ACC_PUBLIC;
  3276. }
  3277. if (access_type & ZEND_ACC_STATIC) {
  3278. if ((property_info_ptr = zend_hash_find_ptr(&ce->properties_info, name)) != NULL &&
  3279. (property_info_ptr->flags & ZEND_ACC_STATIC) != 0) {
  3280. property_info->offset = property_info_ptr->offset;
  3281. zval_ptr_dtor(&ce->default_static_members_table[property_info->offset]);
  3282. zend_hash_del(&ce->properties_info, name);
  3283. } else {
  3284. property_info->offset = ce->default_static_members_count++;
  3285. ce->default_static_members_table = perealloc(ce->default_static_members_table, sizeof(zval) * ce->default_static_members_count, ce->type == ZEND_INTERNAL_CLASS);
  3286. }
  3287. ZVAL_COPY_VALUE(&ce->default_static_members_table[property_info->offset], property);
  3288. if (ce->type == ZEND_USER_CLASS) {
  3289. ce->static_members_table = ce->default_static_members_table;
  3290. }
  3291. } else {
  3292. if ((property_info_ptr = zend_hash_find_ptr(&ce->properties_info, name)) != NULL &&
  3293. (property_info_ptr->flags & ZEND_ACC_STATIC) == 0) {
  3294. property_info->offset = property_info_ptr->offset;
  3295. zval_ptr_dtor(&ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)]);
  3296. zend_hash_del(&ce->properties_info, name);
  3297. } else {
  3298. property_info->offset = OBJ_PROP_TO_OFFSET(ce->default_properties_count);
  3299. ce->default_properties_count++;
  3300. ce->default_properties_table = perealloc(ce->default_properties_table, sizeof(zval) * ce->default_properties_count, ce->type == ZEND_INTERNAL_CLASS);
  3301. }
  3302. ZVAL_COPY_VALUE(&ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)], property);
  3303. }
  3304. if (ce->type & ZEND_INTERNAL_CLASS) {
  3305. switch(Z_TYPE_P(property)) {
  3306. case IS_ARRAY:
  3307. case IS_OBJECT:
  3308. case IS_RESOURCE:
  3309. zend_error_noreturn(E_CORE_ERROR, "Internal zval's can't be arrays, objects or resources");
  3310. break;
  3311. default:
  3312. break;
  3313. }
  3314. /* Must be interned to avoid ZTS data races */
  3315. name = zend_new_interned_string(zend_string_copy(name));
  3316. }
  3317. if (access_type & ZEND_ACC_PUBLIC) {
  3318. property_info->name = zend_string_copy(name);
  3319. } else if (access_type & ZEND_ACC_PRIVATE) {
  3320. property_info->name = zend_mangle_property_name(ZSTR_VAL(ce->name), ZSTR_LEN(ce->name), ZSTR_VAL(name), ZSTR_LEN(name), is_persistent_class(ce));
  3321. } else {
  3322. ZEND_ASSERT(access_type & ZEND_ACC_PROTECTED);
  3323. property_info->name = zend_mangle_property_name("*", 1, ZSTR_VAL(name), ZSTR_LEN(name), is_persistent_class(ce));
  3324. }
  3325. property_info->name = zend_new_interned_string(property_info->name);
  3326. property_info->flags = access_type;
  3327. property_info->doc_comment = doc_comment;
  3328. property_info->ce = ce;
  3329. zend_hash_update_ptr(&ce->properties_info, name, property_info);
  3330. return SUCCESS;
  3331. }
  3332. /* }}} */
  3333. ZEND_API int zend_declare_property(zend_class_entry *ce, const char *name, size_t name_length, zval *property, int access_type) /* {{{ */
  3334. {
  3335. zend_string *key = zend_string_init(name, name_length, is_persistent_class(ce));
  3336. int ret = zend_declare_property_ex(ce, key, property, access_type, NULL);
  3337. zend_string_release(key);
  3338. return ret;
  3339. }
  3340. /* }}} */
  3341. ZEND_API int zend_declare_property_null(zend_class_entry *ce, const char *name, size_t name_length, int access_type) /* {{{ */
  3342. {
  3343. zval property;
  3344. ZVAL_NULL(&property);
  3345. return zend_declare_property(ce, name, name_length, &property, access_type);
  3346. }
  3347. /* }}} */
  3348. ZEND_API int zend_declare_property_bool(zend_class_entry *ce, const char *name, size_t name_length, zend_long value, int access_type) /* {{{ */
  3349. {
  3350. zval property;
  3351. ZVAL_BOOL(&property, value);
  3352. return zend_declare_property(ce, name, name_length, &property, access_type);
  3353. }
  3354. /* }}} */
  3355. ZEND_API int zend_declare_property_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value, int access_type) /* {{{ */
  3356. {
  3357. zval property;
  3358. ZVAL_LONG(&property, value);
  3359. return zend_declare_property(ce, name, name_length, &property, access_type);
  3360. }
  3361. /* }}} */
  3362. ZEND_API int zend_declare_property_double(zend_class_entry *ce, const char *name, size_t name_length, double value, int access_type) /* {{{ */
  3363. {
  3364. zval property;
  3365. ZVAL_DOUBLE(&property, value);
  3366. return zend_declare_property(ce, name, name_length, &property, access_type);
  3367. }
  3368. /* }}} */
  3369. ZEND_API int zend_declare_property_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value, int access_type) /* {{{ */
  3370. {
  3371. zval property;
  3372. ZVAL_NEW_STR(&property, zend_string_init(value, strlen(value), ce->type & ZEND_INTERNAL_CLASS));
  3373. return zend_declare_property(ce, name, name_length, &property, access_type);
  3374. }
  3375. /* }}} */
  3376. ZEND_API int zend_declare_property_stringl(zend_class_entry *ce, const char *name, size_t name_length, const char *value, size_t value_len, int access_type) /* {{{ */
  3377. {
  3378. zval property;
  3379. ZVAL_NEW_STR(&property, zend_string_init(value, value_len, ce->type & ZEND_INTERNAL_CLASS));
  3380. return zend_declare_property(ce, name, name_length, &property, access_type);
  3381. }
  3382. /* }}} */
  3383. ZEND_API int zend_declare_class_constant_ex(zend_class_entry *ce, zend_string *name, zval *value, int access_type, zend_string *doc_comment) /* {{{ */
  3384. {
  3385. zend_class_constant *c;
  3386. if (ce->ce_flags & ZEND_ACC_INTERFACE) {
  3387. if (access_type != ZEND_ACC_PUBLIC) {
  3388. zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface constant %s::%s must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  3389. }
  3390. }
  3391. if (zend_string_equals_literal_ci(name, "class")) {
  3392. zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
  3393. "A class constant must not be called 'class'; it is reserved for class name fetching");
  3394. }
  3395. if (Z_TYPE_P(value) == IS_STRING && !ZSTR_IS_INTERNED(Z_STR_P(value))) {
  3396. zval_make_interned_string(value);
  3397. }
  3398. if (ce->type == ZEND_INTERNAL_CLASS) {
  3399. c = pemalloc(sizeof(zend_class_constant), 1);
  3400. } else {
  3401. c = zend_arena_alloc(&CG(arena), sizeof(zend_class_constant));
  3402. }
  3403. ZVAL_COPY_VALUE(&c->value, value);
  3404. Z_ACCESS_FLAGS(c->value) = access_type;
  3405. c->doc_comment = doc_comment;
  3406. c->ce = ce;
  3407. if (Z_TYPE_P(value) == IS_CONSTANT_AST) {
  3408. ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
  3409. }
  3410. if (!zend_hash_add_ptr(&ce->constants_table, name, c)) {
  3411. zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
  3412. "Cannot redefine class constant %s::%s", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  3413. }
  3414. return SUCCESS;
  3415. }
  3416. /* }}} */
  3417. ZEND_API int zend_declare_class_constant(zend_class_entry *ce, const char *name, size_t name_length, zval *value) /* {{{ */
  3418. {
  3419. int ret;
  3420. zend_string *key;
  3421. if (ce->type == ZEND_INTERNAL_CLASS) {
  3422. key = zend_string_init_interned(name, name_length, 1);
  3423. } else {
  3424. key = zend_string_init(name, name_length, 0);
  3425. }
  3426. ret = zend_declare_class_constant_ex(ce, key, value, ZEND_ACC_PUBLIC, NULL);
  3427. zend_string_release(key);
  3428. return ret;
  3429. }
  3430. /* }}} */
  3431. ZEND_API int zend_declare_class_constant_null(zend_class_entry *ce, const char *name, size_t name_length) /* {{{ */
  3432. {
  3433. zval constant;
  3434. ZVAL_NULL(&constant);
  3435. return zend_declare_class_constant(ce, name, name_length, &constant);
  3436. }
  3437. /* }}} */
  3438. ZEND_API int zend_declare_class_constant_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value) /* {{{ */
  3439. {
  3440. zval constant;
  3441. ZVAL_LONG(&constant, value);
  3442. return zend_declare_class_constant(ce, name, name_length, &constant);
  3443. }
  3444. /* }}} */
  3445. ZEND_API int zend_declare_class_constant_bool(zend_class_entry *ce, const char *name, size_t name_length, zend_bool value) /* {{{ */
  3446. {
  3447. zval constant;
  3448. ZVAL_BOOL(&constant, value);
  3449. return zend_declare_class_constant(ce, name, name_length, &constant);
  3450. }
  3451. /* }}} */
  3452. ZEND_API int zend_declare_class_constant_double(zend_class_entry *ce, const char *name, size_t name_length, double value) /* {{{ */
  3453. {
  3454. zval constant;
  3455. ZVAL_DOUBLE(&constant, value);
  3456. return zend_declare_class_constant(ce, name, name_length, &constant);
  3457. }
  3458. /* }}} */
  3459. ZEND_API int zend_declare_class_constant_stringl(zend_class_entry *ce, const char *name, size_t name_length, const char *value, size_t value_length) /* {{{ */
  3460. {
  3461. zval constant;
  3462. ZVAL_NEW_STR(&constant, zend_string_init(value, value_length, ce->type & ZEND_INTERNAL_CLASS));
  3463. return zend_declare_class_constant(ce, name, name_length, &constant);
  3464. }
  3465. /* }}} */
  3466. ZEND_API int zend_declare_class_constant_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value) /* {{{ */
  3467. {
  3468. return zend_declare_class_constant_stringl(ce, name, name_length, value, strlen(value));
  3469. }
  3470. /* }}} */
  3471. ZEND_API void zend_update_property_ex(zend_class_entry *scope, zval *object, zend_string *name, zval *value) /* {{{ */
  3472. {
  3473. zval property;
  3474. zend_class_entry *old_scope = EG(fake_scope);
  3475. EG(fake_scope) = scope;
  3476. if (!Z_OBJ_HT_P(object)->write_property) {
  3477. zend_error_noreturn(E_CORE_ERROR, "Property %s of class %s cannot be updated", ZSTR_VAL(name), ZSTR_VAL(Z_OBJCE_P(object)->name));
  3478. }
  3479. ZVAL_STR(&property, name);
  3480. Z_OBJ_HT_P(object)->write_property(object, &property, value, NULL);
  3481. EG(fake_scope) = old_scope;
  3482. }
  3483. /* }}} */
  3484. ZEND_API void zend_update_property(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zval *value) /* {{{ */
  3485. {
  3486. zval property;
  3487. zend_class_entry *old_scope = EG(fake_scope);
  3488. EG(fake_scope) = scope;
  3489. if (!Z_OBJ_HT_P(object)->write_property) {
  3490. zend_error_noreturn(E_CORE_ERROR, "Property %s of class %s cannot be updated", name, ZSTR_VAL(Z_OBJCE_P(object)->name));
  3491. }
  3492. ZVAL_STRINGL(&property, name, name_length);
  3493. Z_OBJ_HT_P(object)->write_property(object, &property, value, NULL);
  3494. zval_ptr_dtor(&property);
  3495. EG(fake_scope) = old_scope;
  3496. }
  3497. /* }}} */
  3498. ZEND_API void zend_update_property_null(zend_class_entry *scope, zval *object, const char *name, size_t name_length) /* {{{ */
  3499. {
  3500. zval tmp;
  3501. ZVAL_NULL(&tmp);
  3502. zend_update_property(scope, object, name, name_length, &tmp);
  3503. }
  3504. /* }}} */
  3505. ZEND_API void zend_unset_property(zend_class_entry *scope, zval *object, const char *name, size_t name_length) /* {{{ */
  3506. {
  3507. zval property;
  3508. zend_class_entry *old_scope = EG(fake_scope);
  3509. EG(fake_scope) = scope;
  3510. if (!Z_OBJ_HT_P(object)->unset_property) {
  3511. zend_error_noreturn(E_CORE_ERROR, "Property %s of class %s cannot be unset", name, ZSTR_VAL(Z_OBJCE_P(object)->name));
  3512. }
  3513. ZVAL_STRINGL(&property, name, name_length);
  3514. Z_OBJ_HT_P(object)->unset_property(object, &property, 0);
  3515. zval_ptr_dtor(&property);
  3516. EG(fake_scope) = old_scope;
  3517. }
  3518. /* }}} */
  3519. ZEND_API void zend_update_property_bool(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zend_long value) /* {{{ */
  3520. {
  3521. zval tmp;
  3522. ZVAL_BOOL(&tmp, value);
  3523. zend_update_property(scope, object, name, name_length, &tmp);
  3524. }
  3525. /* }}} */
  3526. ZEND_API void zend_update_property_long(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zend_long value) /* {{{ */
  3527. {
  3528. zval tmp;
  3529. ZVAL_LONG(&tmp, value);
  3530. zend_update_property(scope, object, name, name_length, &tmp);
  3531. }
  3532. /* }}} */
  3533. ZEND_API void zend_update_property_double(zend_class_entry *scope, zval *object, const char *name, size_t name_length, double value) /* {{{ */
  3534. {
  3535. zval tmp;
  3536. ZVAL_DOUBLE(&tmp, value);
  3537. zend_update_property(scope, object, name, name_length, &tmp);
  3538. }
  3539. /* }}} */
  3540. ZEND_API void zend_update_property_str(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zend_string *value) /* {{{ */
  3541. {
  3542. zval tmp;
  3543. ZVAL_STR(&tmp, value);
  3544. zend_update_property(scope, object, name, name_length, &tmp);
  3545. }
  3546. /* }}} */
  3547. ZEND_API void zend_update_property_string(zend_class_entry *scope, zval *object, const char *name, size_t name_length, const char *value) /* {{{ */
  3548. {
  3549. zval tmp;
  3550. ZVAL_STRING(&tmp, value);
  3551. Z_SET_REFCOUNT(tmp, 0);
  3552. zend_update_property(scope, object, name, name_length, &tmp);
  3553. }
  3554. /* }}} */
  3555. ZEND_API void zend_update_property_stringl(zend_class_entry *scope, zval *object, const char *name, size_t name_length, const char *value, size_t value_len) /* {{{ */
  3556. {
  3557. zval tmp;
  3558. ZVAL_STRINGL(&tmp, value, value_len);
  3559. Z_SET_REFCOUNT(tmp, 0);
  3560. zend_update_property(scope, object, name, name_length, &tmp);
  3561. }
  3562. /* }}} */
  3563. ZEND_API int zend_update_static_property_ex(zend_class_entry *scope, zend_string *name, zval *value) /* {{{ */
  3564. {
  3565. zval *property;
  3566. zend_class_entry *old_scope = EG(fake_scope);
  3567. if (UNEXPECTED(!(scope->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
  3568. if (UNEXPECTED(zend_update_class_constants(scope)) != SUCCESS) {
  3569. return FAILURE;
  3570. }
  3571. }
  3572. EG(fake_scope) = scope;
  3573. property = zend_std_get_static_property(scope, name, 0);
  3574. EG(fake_scope) = old_scope;
  3575. if (!property) {
  3576. return FAILURE;
  3577. }
  3578. if (property != value) {
  3579. zval garbage;
  3580. ZVAL_DEREF(property);
  3581. ZVAL_DEREF(value);
  3582. ZVAL_COPY_VALUE(&garbage, property);
  3583. ZVAL_COPY(property, value);
  3584. zval_ptr_dtor(&garbage);
  3585. }
  3586. return SUCCESS;
  3587. }
  3588. /* }}} */
  3589. ZEND_API int zend_update_static_property(zend_class_entry *scope, const char *name, size_t name_length, zval *value) /* {{{ */
  3590. {
  3591. zend_string *key = zend_string_init(name, name_length, 0);
  3592. int retval = zend_update_static_property_ex(scope, key, value);
  3593. zend_string_efree(key);
  3594. return retval;
  3595. }
  3596. /* }}} */
  3597. ZEND_API int zend_update_static_property_null(zend_class_entry *scope, const char *name, size_t name_length) /* {{{ */
  3598. {
  3599. zval tmp;
  3600. ZVAL_NULL(&tmp);
  3601. return zend_update_static_property(scope, name, name_length, &tmp);
  3602. }
  3603. /* }}} */
  3604. ZEND_API int zend_update_static_property_bool(zend_class_entry *scope, const char *name, size_t name_length, zend_long value) /* {{{ */
  3605. {
  3606. zval tmp;
  3607. ZVAL_BOOL(&tmp, value);
  3608. return zend_update_static_property(scope, name, name_length, &tmp);
  3609. }
  3610. /* }}} */
  3611. ZEND_API int zend_update_static_property_long(zend_class_entry *scope, const char *name, size_t name_length, zend_long value) /* {{{ */
  3612. {
  3613. zval tmp;
  3614. ZVAL_LONG(&tmp, value);
  3615. return zend_update_static_property(scope, name, name_length, &tmp);
  3616. }
  3617. /* }}} */
  3618. ZEND_API int zend_update_static_property_double(zend_class_entry *scope, const char *name, size_t name_length, double value) /* {{{ */
  3619. {
  3620. zval tmp;
  3621. ZVAL_DOUBLE(&tmp, value);
  3622. return zend_update_static_property(scope, name, name_length, &tmp);
  3623. }
  3624. /* }}} */
  3625. ZEND_API int zend_update_static_property_string(zend_class_entry *scope, const char *name, size_t name_length, const char *value) /* {{{ */
  3626. {
  3627. zval tmp;
  3628. ZVAL_STRING(&tmp, value);
  3629. Z_SET_REFCOUNT(tmp, 0);
  3630. return zend_update_static_property(scope, name, name_length, &tmp);
  3631. }
  3632. /* }}} */
  3633. ZEND_API int zend_update_static_property_stringl(zend_class_entry *scope, const char *name, size_t name_length, const char *value, size_t value_len) /* {{{ */
  3634. {
  3635. zval tmp;
  3636. ZVAL_STRINGL(&tmp, value, value_len);
  3637. Z_SET_REFCOUNT(tmp, 0);
  3638. return zend_update_static_property(scope, name, name_length, &tmp);
  3639. }
  3640. /* }}} */
  3641. ZEND_API zval *zend_read_property_ex(zend_class_entry *scope, zval *object, zend_string *name, zend_bool silent, zval *rv) /* {{{ */
  3642. {
  3643. zval property, *value;
  3644. zend_class_entry *old_scope = EG(fake_scope);
  3645. EG(fake_scope) = scope;
  3646. if (!Z_OBJ_HT_P(object)->read_property) {
  3647. zend_error_noreturn(E_CORE_ERROR, "Property %s of class %s cannot be read", ZSTR_VAL(name), ZSTR_VAL(Z_OBJCE_P(object)->name));
  3648. }
  3649. ZVAL_STR(&property, name);
  3650. value = Z_OBJ_HT_P(object)->read_property(object, &property, silent?BP_VAR_IS:BP_VAR_R, NULL, rv);
  3651. EG(fake_scope) = old_scope;
  3652. return value;
  3653. }
  3654. /* }}} */
  3655. ZEND_API zval *zend_read_property(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zend_bool silent, zval *rv) /* {{{ */
  3656. {
  3657. zval *value;
  3658. zend_string *str;
  3659. str = zend_string_init(name, name_length, 0);
  3660. value = zend_read_property_ex(scope, object, str, silent, rv);
  3661. zend_string_release_ex(str, 0);
  3662. return value;
  3663. }
  3664. /* }}} */
  3665. ZEND_API zval *zend_read_static_property_ex(zend_class_entry *scope, zend_string *name, zend_bool silent) /* {{{ */
  3666. {
  3667. zval *property;
  3668. zend_class_entry *old_scope = EG(fake_scope);
  3669. EG(fake_scope) = scope;
  3670. property = zend_std_get_static_property(scope, name, silent);
  3671. EG(fake_scope) = old_scope;
  3672. return property;
  3673. }
  3674. /* }}} */
  3675. ZEND_API zval *zend_read_static_property(zend_class_entry *scope, const char *name, size_t name_length, zend_bool silent) /* {{{ */
  3676. {
  3677. zend_string *key = zend_string_init(name, name_length, 0);
  3678. zval *property = zend_read_static_property_ex(scope, key, silent);
  3679. zend_string_efree(key);
  3680. return property;
  3681. }
  3682. /* }}} */
  3683. ZEND_API void zend_save_error_handling(zend_error_handling *current) /* {{{ */
  3684. {
  3685. current->handling = EG(error_handling);
  3686. current->exception = EG(exception_class);
  3687. ZVAL_UNDEF(&current->user_handler);
  3688. }
  3689. /* }}} */
  3690. ZEND_API void zend_replace_error_handling(zend_error_handling_t error_handling, zend_class_entry *exception_class, zend_error_handling *current) /* {{{ */
  3691. {
  3692. if (current) {
  3693. zend_save_error_handling(current);
  3694. }
  3695. ZEND_ASSERT(error_handling == EH_THROW || exception_class == NULL);
  3696. EG(error_handling) = error_handling;
  3697. EG(exception_class) = exception_class;
  3698. }
  3699. /* }}} */
  3700. ZEND_API void zend_restore_error_handling(zend_error_handling *saved) /* {{{ */
  3701. {
  3702. EG(error_handling) = saved->handling;
  3703. EG(exception_class) = saved->exception;
  3704. }
  3705. /* }}} */
  3706. ZEND_API zend_string* zend_find_alias_name(zend_class_entry *ce, zend_string *name) /* {{{ */
  3707. {
  3708. zend_trait_alias *alias, **alias_ptr;
  3709. if ((alias_ptr = ce->trait_aliases)) {
  3710. alias = *alias_ptr;
  3711. while (alias) {
  3712. if (alias->alias && zend_string_equals_ci(alias->alias, name)) {
  3713. return alias->alias;
  3714. }
  3715. alias_ptr++;
  3716. alias = *alias_ptr;
  3717. }
  3718. }
  3719. return name;
  3720. }
  3721. /* }}} */
  3722. ZEND_API zend_string *zend_resolve_method_name(zend_class_entry *ce, zend_function *f) /* {{{ */
  3723. {
  3724. zend_function *func;
  3725. HashTable *function_table;
  3726. zend_string *name;
  3727. if (f->common.type != ZEND_USER_FUNCTION ||
  3728. (f->op_array.refcount && *(f->op_array.refcount) < 2) ||
  3729. !f->common.scope ||
  3730. !f->common.scope->trait_aliases) {
  3731. return f->common.function_name;
  3732. }
  3733. function_table = &ce->function_table;
  3734. ZEND_HASH_FOREACH_STR_KEY_PTR(function_table, name, func) {
  3735. if (func == f) {
  3736. if (!name) {
  3737. return f->common.function_name;
  3738. }
  3739. if (ZSTR_LEN(name) == ZSTR_LEN(f->common.function_name) &&
  3740. !strncasecmp(ZSTR_VAL(name), ZSTR_VAL(f->common.function_name), ZSTR_LEN(f->common.function_name))) {
  3741. return f->common.function_name;
  3742. }
  3743. return zend_find_alias_name(f->common.scope, name);
  3744. }
  3745. } ZEND_HASH_FOREACH_END();
  3746. return f->common.function_name;
  3747. }
  3748. /* }}} */
  3749. ZEND_API const char *zend_get_object_type(const zend_class_entry *ce) /* {{{ */
  3750. {
  3751. if(ce->ce_flags & ZEND_ACC_TRAIT) {
  3752. return "trait";
  3753. } else if (ce->ce_flags & ZEND_ACC_INTERFACE) {
  3754. return "interface";
  3755. } else {
  3756. return "class";
  3757. }
  3758. }
  3759. /* }}} */
  3760. ZEND_API zend_bool zend_is_iterable(zval *iterable) /* {{{ */
  3761. {
  3762. switch (Z_TYPE_P(iterable)) {
  3763. case IS_ARRAY:
  3764. return 1;
  3765. case IS_OBJECT:
  3766. return instanceof_function(Z_OBJCE_P(iterable), zend_ce_traversable);
  3767. default:
  3768. return 0;
  3769. }
  3770. }
  3771. /* }}} */
  3772. ZEND_API zend_bool zend_is_countable(zval *countable) /* {{{ */
  3773. {
  3774. switch (Z_TYPE_P(countable)) {
  3775. case IS_ARRAY:
  3776. return 1;
  3777. case IS_OBJECT:
  3778. if (Z_OBJ_HT_P(countable)->count_elements) {
  3779. return 1;
  3780. }
  3781. return instanceof_function(Z_OBJCE_P(countable), zend_ce_countable);
  3782. default:
  3783. return 0;
  3784. }
  3785. }
  3786. /* }}} */
  3787. /*
  3788. * Local variables:
  3789. * tab-width: 4
  3790. * c-basic-offset: 4
  3791. * indent-tabs-mode: t
  3792. * End:
  3793. * vim600: sw=4 ts=4 fdm=marker
  3794. * vim<600: sw=4 ts=4
  3795. */