zend_API.c 121 KB

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