spl_array.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Marcus Boerger <helly@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include "config.h"
  20. #endif
  21. #include "php.h"
  22. #include "php_ini.h"
  23. #include "ext/standard/info.h"
  24. #include "ext/standard/php_var.h"
  25. #include "zend_smart_str.h"
  26. #include "zend_interfaces.h"
  27. #include "zend_exceptions.h"
  28. #include "php_spl.h"
  29. #include "spl_functions.h"
  30. #include "spl_engine.h"
  31. #include "spl_iterators.h"
  32. #include "spl_array.h"
  33. #include "spl_exceptions.h"
  34. zend_object_handlers spl_handler_ArrayObject;
  35. PHPAPI zend_class_entry *spl_ce_ArrayObject;
  36. zend_object_handlers spl_handler_ArrayIterator;
  37. PHPAPI zend_class_entry *spl_ce_ArrayIterator;
  38. PHPAPI zend_class_entry *spl_ce_RecursiveArrayIterator;
  39. #define SPL_ARRAY_STD_PROP_LIST 0x00000001
  40. #define SPL_ARRAY_ARRAY_AS_PROPS 0x00000002
  41. #define SPL_ARRAY_CHILD_ARRAYS_ONLY 0x00000004
  42. #define SPL_ARRAY_OVERLOADED_REWIND 0x00010000
  43. #define SPL_ARRAY_OVERLOADED_VALID 0x00020000
  44. #define SPL_ARRAY_OVERLOADED_KEY 0x00040000
  45. #define SPL_ARRAY_OVERLOADED_CURRENT 0x00080000
  46. #define SPL_ARRAY_OVERLOADED_NEXT 0x00100000
  47. #define SPL_ARRAY_IS_SELF 0x01000000
  48. #define SPL_ARRAY_USE_OTHER 0x02000000
  49. #define SPL_ARRAY_INT_MASK 0xFFFF0000
  50. #define SPL_ARRAY_CLONE_MASK 0x0100FFFF
  51. #define SPL_ARRAY_METHOD_NO_ARG 0
  52. #define SPL_ARRAY_METHOD_USE_ARG 1
  53. #define SPL_ARRAY_METHOD_MAY_USER_ARG 2
  54. typedef struct _spl_array_object {
  55. zval array;
  56. uint32_t ht_iter;
  57. int ar_flags;
  58. unsigned char nApplyCount;
  59. zend_function *fptr_offset_get;
  60. zend_function *fptr_offset_set;
  61. zend_function *fptr_offset_has;
  62. zend_function *fptr_offset_del;
  63. zend_function *fptr_count;
  64. zend_class_entry* ce_get_iterator;
  65. zend_object std;
  66. } spl_array_object;
  67. static inline spl_array_object *spl_array_from_obj(zend_object *obj) /* {{{ */ {
  68. return (spl_array_object*)((char*)(obj) - XtOffsetOf(spl_array_object, std));
  69. }
  70. /* }}} */
  71. #define Z_SPLARRAY_P(zv) spl_array_from_obj(Z_OBJ_P((zv)))
  72. static inline HashTable **spl_array_get_hash_table_ptr(spl_array_object* intern) { /* {{{ */
  73. //??? TODO: Delay duplication for arrays; only duplicate for write operations
  74. if (intern->ar_flags & SPL_ARRAY_IS_SELF) {
  75. if (!intern->std.properties) {
  76. rebuild_object_properties(&intern->std);
  77. }
  78. return &intern->std.properties;
  79. } else if (intern->ar_flags & SPL_ARRAY_USE_OTHER) {
  80. spl_array_object *other = Z_SPLARRAY_P(&intern->array);
  81. return spl_array_get_hash_table_ptr(other);
  82. } else if (Z_TYPE(intern->array) == IS_ARRAY) {
  83. return &Z_ARRVAL(intern->array);
  84. } else {
  85. zend_object *obj = Z_OBJ(intern->array);
  86. if (!obj->properties) {
  87. rebuild_object_properties(obj);
  88. } else if (GC_REFCOUNT(obj->properties) > 1) {
  89. if (EXPECTED(!(GC_FLAGS(obj->properties) & IS_ARRAY_IMMUTABLE))) {
  90. GC_DELREF(obj->properties);
  91. }
  92. obj->properties = zend_array_dup(obj->properties);
  93. }
  94. return &obj->properties;
  95. }
  96. }
  97. /* }}} */
  98. static inline HashTable *spl_array_get_hash_table(spl_array_object* intern) { /* {{{ */
  99. return *spl_array_get_hash_table_ptr(intern);
  100. }
  101. /* }}} */
  102. static inline void spl_array_replace_hash_table(spl_array_object* intern, HashTable *ht) { /* {{{ */
  103. HashTable **ht_ptr = spl_array_get_hash_table_ptr(intern);
  104. zend_array_destroy(*ht_ptr);
  105. *ht_ptr = ht;
  106. }
  107. /* }}} */
  108. static inline zend_bool spl_array_is_object(spl_array_object *intern) /* {{{ */
  109. {
  110. while (intern->ar_flags & SPL_ARRAY_USE_OTHER) {
  111. intern = Z_SPLARRAY_P(&intern->array);
  112. }
  113. return (intern->ar_flags & SPL_ARRAY_IS_SELF) || Z_TYPE(intern->array) == IS_OBJECT;
  114. }
  115. /* }}} */
  116. static int spl_array_skip_protected(spl_array_object *intern, HashTable *aht);
  117. static zend_never_inline void spl_array_create_ht_iter(HashTable *ht, spl_array_object* intern) /* {{{ */
  118. {
  119. intern->ht_iter = zend_hash_iterator_add(ht, zend_hash_get_current_pos(ht));
  120. zend_hash_internal_pointer_reset_ex(ht, &EG(ht_iterators)[intern->ht_iter].pos);
  121. spl_array_skip_protected(intern, ht);
  122. }
  123. /* }}} */
  124. static zend_always_inline uint32_t *spl_array_get_pos_ptr(HashTable *ht, spl_array_object* intern) /* {{{ */
  125. {
  126. if (UNEXPECTED(intern->ht_iter == (uint32_t)-1)) {
  127. spl_array_create_ht_iter(ht, intern);
  128. }
  129. return &EG(ht_iterators)[intern->ht_iter].pos;
  130. }
  131. /* }}} */
  132. /* {{{ spl_array_object_free_storage */
  133. static void spl_array_object_free_storage(zend_object *object)
  134. {
  135. spl_array_object *intern = spl_array_from_obj(object);
  136. if (intern->ht_iter != (uint32_t) -1) {
  137. zend_hash_iterator_del(intern->ht_iter);
  138. }
  139. zend_object_std_dtor(&intern->std);
  140. zval_ptr_dtor(&intern->array);
  141. }
  142. /* }}} */
  143. zend_object_iterator *spl_array_get_iterator(zend_class_entry *ce, zval *object, int by_ref);
  144. /* {{{ spl_array_object_new_ex */
  145. static zend_object *spl_array_object_new_ex(zend_class_entry *class_type, zval *orig, int clone_orig)
  146. {
  147. spl_array_object *intern;
  148. zend_class_entry *parent = class_type;
  149. int inherited = 0;
  150. intern = zend_object_alloc(sizeof(spl_array_object), parent);
  151. zend_object_std_init(&intern->std, class_type);
  152. object_properties_init(&intern->std, class_type);
  153. intern->ar_flags = 0;
  154. intern->ce_get_iterator = spl_ce_ArrayIterator;
  155. if (orig) {
  156. spl_array_object *other = Z_SPLARRAY_P(orig);
  157. intern->ar_flags &= ~ SPL_ARRAY_CLONE_MASK;
  158. intern->ar_flags |= (other->ar_flags & SPL_ARRAY_CLONE_MASK);
  159. intern->ce_get_iterator = other->ce_get_iterator;
  160. if (clone_orig) {
  161. if (other->ar_flags & SPL_ARRAY_IS_SELF) {
  162. ZVAL_UNDEF(&intern->array);
  163. } else if (Z_OBJ_HT_P(orig) == &spl_handler_ArrayObject) {
  164. ZVAL_ARR(&intern->array,
  165. zend_array_dup(spl_array_get_hash_table(other)));
  166. } else {
  167. ZEND_ASSERT(Z_OBJ_HT_P(orig) == &spl_handler_ArrayIterator);
  168. ZVAL_COPY(&intern->array, orig);
  169. intern->ar_flags |= SPL_ARRAY_USE_OTHER;
  170. }
  171. } else {
  172. ZVAL_COPY(&intern->array, orig);
  173. intern->ar_flags |= SPL_ARRAY_USE_OTHER;
  174. }
  175. } else {
  176. array_init(&intern->array);
  177. }
  178. while (parent) {
  179. if (parent == spl_ce_ArrayIterator || parent == spl_ce_RecursiveArrayIterator) {
  180. intern->std.handlers = &spl_handler_ArrayIterator;
  181. class_type->get_iterator = spl_array_get_iterator;
  182. break;
  183. } else if (parent == spl_ce_ArrayObject) {
  184. intern->std.handlers = &spl_handler_ArrayObject;
  185. break;
  186. }
  187. parent = parent->parent;
  188. inherited = 1;
  189. }
  190. if (!parent) { /* this must never happen */
  191. php_error_docref(NULL, E_COMPILE_ERROR, "Internal compiler error, Class is not child of ArrayObject or ArrayIterator");
  192. }
  193. if (inherited) {
  194. intern->fptr_offset_get = zend_hash_str_find_ptr(&class_type->function_table, "offsetget", sizeof("offsetget") - 1);
  195. if (intern->fptr_offset_get->common.scope == parent) {
  196. intern->fptr_offset_get = NULL;
  197. }
  198. intern->fptr_offset_set = zend_hash_str_find_ptr(&class_type->function_table, "offsetset", sizeof("offsetset") - 1);
  199. if (intern->fptr_offset_set->common.scope == parent) {
  200. intern->fptr_offset_set = NULL;
  201. }
  202. intern->fptr_offset_has = zend_hash_str_find_ptr(&class_type->function_table, "offsetexists", sizeof("offsetexists") - 1);
  203. if (intern->fptr_offset_has->common.scope == parent) {
  204. intern->fptr_offset_has = NULL;
  205. }
  206. intern->fptr_offset_del = zend_hash_str_find_ptr(&class_type->function_table, "offsetunset", sizeof("offsetunset") - 1);
  207. if (intern->fptr_offset_del->common.scope == parent) {
  208. intern->fptr_offset_del = NULL;
  209. }
  210. intern->fptr_count = zend_hash_str_find_ptr(&class_type->function_table, "count", sizeof("count") - 1);
  211. if (intern->fptr_count->common.scope == parent) {
  212. intern->fptr_count = NULL;
  213. }
  214. }
  215. /* Cache iterator functions if ArrayIterator or derived. Check current's */
  216. /* cache since only current is always required */
  217. if (intern->std.handlers == &spl_handler_ArrayIterator) {
  218. if (!class_type->iterator_funcs_ptr->zf_current) {
  219. class_type->iterator_funcs_ptr->zf_rewind = zend_hash_str_find_ptr(&class_type->function_table, "rewind", sizeof("rewind") - 1);
  220. class_type->iterator_funcs_ptr->zf_valid = zend_hash_str_find_ptr(&class_type->function_table, "valid", sizeof("valid") - 1);
  221. class_type->iterator_funcs_ptr->zf_key = zend_hash_str_find_ptr(&class_type->function_table, "key", sizeof("key") - 1);
  222. class_type->iterator_funcs_ptr->zf_current = zend_hash_str_find_ptr(&class_type->function_table, "current", sizeof("current") - 1);
  223. class_type->iterator_funcs_ptr->zf_next = zend_hash_str_find_ptr(&class_type->function_table, "next", sizeof("next") - 1);
  224. }
  225. if (inherited) {
  226. if (class_type->iterator_funcs_ptr->zf_rewind->common.scope != parent) intern->ar_flags |= SPL_ARRAY_OVERLOADED_REWIND;
  227. if (class_type->iterator_funcs_ptr->zf_valid->common.scope != parent) intern->ar_flags |= SPL_ARRAY_OVERLOADED_VALID;
  228. if (class_type->iterator_funcs_ptr->zf_key->common.scope != parent) intern->ar_flags |= SPL_ARRAY_OVERLOADED_KEY;
  229. if (class_type->iterator_funcs_ptr->zf_current->common.scope != parent) intern->ar_flags |= SPL_ARRAY_OVERLOADED_CURRENT;
  230. if (class_type->iterator_funcs_ptr->zf_next->common.scope != parent) intern->ar_flags |= SPL_ARRAY_OVERLOADED_NEXT;
  231. }
  232. }
  233. intern->ht_iter = (uint32_t)-1;
  234. return &intern->std;
  235. }
  236. /* }}} */
  237. /* {{{ spl_array_object_new */
  238. static zend_object *spl_array_object_new(zend_class_entry *class_type)
  239. {
  240. return spl_array_object_new_ex(class_type, NULL, 0);
  241. }
  242. /* }}} */
  243. /* {{{ spl_array_object_clone */
  244. static zend_object *spl_array_object_clone(zval *zobject)
  245. {
  246. zend_object *old_object;
  247. zend_object *new_object;
  248. old_object = Z_OBJ_P(zobject);
  249. new_object = spl_array_object_new_ex(old_object->ce, zobject, 1);
  250. zend_objects_clone_members(new_object, old_object);
  251. return new_object;
  252. }
  253. /* }}} */
  254. static zval *spl_array_get_dimension_ptr(int check_inherited, spl_array_object *intern, zval *offset, int type) /* {{{ */
  255. {
  256. zval *retval;
  257. zend_long index;
  258. zend_string *offset_key;
  259. HashTable *ht = spl_array_get_hash_table(intern);
  260. if (!offset || Z_ISUNDEF_P(offset) || !ht) {
  261. return &EG(uninitialized_zval);
  262. }
  263. if ((type == BP_VAR_W || type == BP_VAR_RW) && intern->nApplyCount > 0) {
  264. zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
  265. return &EG(error_zval);
  266. }
  267. try_again:
  268. switch (Z_TYPE_P(offset)) {
  269. case IS_NULL:
  270. offset_key = ZSTR_EMPTY_ALLOC();
  271. goto fetch_dim_string;
  272. case IS_STRING:
  273. offset_key = Z_STR_P(offset);
  274. fetch_dim_string:
  275. retval = zend_symtable_find(ht, offset_key);
  276. if (retval) {
  277. if (Z_TYPE_P(retval) == IS_INDIRECT) {
  278. retval = Z_INDIRECT_P(retval);
  279. if (Z_TYPE_P(retval) == IS_UNDEF) {
  280. switch (type) {
  281. case BP_VAR_R:
  282. zend_error(E_NOTICE, "Undefined index: %s", ZSTR_VAL(offset_key));
  283. case BP_VAR_UNSET:
  284. case BP_VAR_IS:
  285. retval = &EG(uninitialized_zval);
  286. break;
  287. case BP_VAR_RW:
  288. zend_error(E_NOTICE,"Undefined index: %s", ZSTR_VAL(offset_key));
  289. case BP_VAR_W: {
  290. ZVAL_NULL(retval);
  291. }
  292. }
  293. }
  294. }
  295. } else {
  296. switch (type) {
  297. case BP_VAR_R:
  298. zend_error(E_NOTICE, "Undefined index: %s", ZSTR_VAL(offset_key));
  299. case BP_VAR_UNSET:
  300. case BP_VAR_IS:
  301. retval = &EG(uninitialized_zval);
  302. break;
  303. case BP_VAR_RW:
  304. zend_error(E_NOTICE,"Undefined index: %s", ZSTR_VAL(offset_key));
  305. case BP_VAR_W: {
  306. zval value;
  307. ZVAL_NULL(&value);
  308. retval = zend_symtable_update(ht, offset_key, &value);
  309. }
  310. }
  311. }
  312. return retval;
  313. case IS_RESOURCE:
  314. zend_error(E_NOTICE, "Resource ID#%d used as offset, casting to integer (%d)", Z_RES_P(offset)->handle, Z_RES_P(offset)->handle);
  315. index = Z_RES_P(offset)->handle;
  316. goto num_index;
  317. case IS_DOUBLE:
  318. index = (zend_long)Z_DVAL_P(offset);
  319. goto num_index;
  320. case IS_FALSE:
  321. index = 0;
  322. goto num_index;
  323. case IS_TRUE:
  324. index = 1;
  325. goto num_index;
  326. case IS_LONG:
  327. index = Z_LVAL_P(offset);
  328. num_index:
  329. if ((retval = zend_hash_index_find(ht, index)) == NULL) {
  330. switch (type) {
  331. case BP_VAR_R:
  332. zend_error(E_NOTICE, "Undefined offset: " ZEND_LONG_FMT, index);
  333. case BP_VAR_UNSET:
  334. case BP_VAR_IS:
  335. retval = &EG(uninitialized_zval);
  336. break;
  337. case BP_VAR_RW:
  338. zend_error(E_NOTICE, "Undefined offset: " ZEND_LONG_FMT, index);
  339. case BP_VAR_W: {
  340. zval value;
  341. ZVAL_UNDEF(&value);
  342. retval = zend_hash_index_update(ht, index, &value);
  343. }
  344. }
  345. }
  346. return retval;
  347. case IS_REFERENCE:
  348. ZVAL_DEREF(offset);
  349. goto try_again;
  350. default:
  351. zend_error(E_WARNING, "Illegal offset type");
  352. return (type == BP_VAR_W || type == BP_VAR_RW) ?
  353. &EG(error_zval) : &EG(uninitialized_zval);
  354. }
  355. } /* }}} */
  356. static int spl_array_has_dimension(zval *object, zval *offset, int check_empty);
  357. static zval *spl_array_read_dimension_ex(int check_inherited, zval *object, zval *offset, int type, zval *rv) /* {{{ */
  358. {
  359. spl_array_object *intern = Z_SPLARRAY_P(object);
  360. zval *ret;
  361. if (check_inherited &&
  362. (intern->fptr_offset_get || (type == BP_VAR_IS && intern->fptr_offset_has))) {
  363. if (type == BP_VAR_IS) {
  364. if (!spl_array_has_dimension(object, offset, 0)) {
  365. return &EG(uninitialized_zval);
  366. }
  367. }
  368. if (intern->fptr_offset_get) {
  369. zval tmp;
  370. if (!offset) {
  371. ZVAL_UNDEF(&tmp);
  372. offset = &tmp;
  373. } else {
  374. SEPARATE_ARG_IF_REF(offset);
  375. }
  376. zend_call_method_with_1_params(object, Z_OBJCE_P(object), &intern->fptr_offset_get, "offsetGet", rv, offset);
  377. zval_ptr_dtor(offset);
  378. if (!Z_ISUNDEF_P(rv)) {
  379. return rv;
  380. }
  381. return &EG(uninitialized_zval);
  382. }
  383. }
  384. ret = spl_array_get_dimension_ptr(check_inherited, intern, offset, type);
  385. /* When in a write context,
  386. * ZE has to be fooled into thinking this is in a reference set
  387. * by separating (if necessary) and returning as IS_REFERENCE (with refcount == 1)
  388. */
  389. if ((type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) &&
  390. !Z_ISREF_P(ret) &&
  391. EXPECTED(ret != &EG(uninitialized_zval))) {
  392. ZVAL_NEW_REF(ret, ret);
  393. }
  394. return ret;
  395. } /* }}} */
  396. static zval *spl_array_read_dimension(zval *object, zval *offset, int type, zval *rv) /* {{{ */
  397. {
  398. return spl_array_read_dimension_ex(1, object, offset, type, rv);
  399. } /* }}} */
  400. static void spl_array_write_dimension_ex(int check_inherited, zval *object, zval *offset, zval *value) /* {{{ */
  401. {
  402. spl_array_object *intern = Z_SPLARRAY_P(object);
  403. zend_long index;
  404. HashTable *ht;
  405. if (check_inherited && intern->fptr_offset_set) {
  406. zval tmp;
  407. if (!offset) {
  408. ZVAL_NULL(&tmp);
  409. offset = &tmp;
  410. } else {
  411. SEPARATE_ARG_IF_REF(offset);
  412. }
  413. zend_call_method_with_2_params(object, Z_OBJCE_P(object), &intern->fptr_offset_set, "offsetSet", NULL, offset, value);
  414. zval_ptr_dtor(offset);
  415. return;
  416. }
  417. if (intern->nApplyCount > 0) {
  418. zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
  419. return;
  420. }
  421. Z_TRY_ADDREF_P(value);
  422. if (!offset) {
  423. ht = spl_array_get_hash_table(intern);
  424. zend_hash_next_index_insert(ht, value);
  425. return;
  426. }
  427. try_again:
  428. switch (Z_TYPE_P(offset)) {
  429. case IS_STRING:
  430. ht = spl_array_get_hash_table(intern);
  431. zend_symtable_update_ind(ht, Z_STR_P(offset), value);
  432. return;
  433. case IS_DOUBLE:
  434. index = (zend_long)Z_DVAL_P(offset);
  435. goto num_index;
  436. case IS_RESOURCE:
  437. index = Z_RES_HANDLE_P(offset);
  438. goto num_index;
  439. case IS_FALSE:
  440. index = 0;
  441. goto num_index;
  442. case IS_TRUE:
  443. index = 1;
  444. goto num_index;
  445. case IS_LONG:
  446. index = Z_LVAL_P(offset);
  447. num_index:
  448. ht = spl_array_get_hash_table(intern);
  449. zend_hash_index_update(ht, index, value);
  450. return;
  451. case IS_NULL:
  452. ht = spl_array_get_hash_table(intern);
  453. zend_hash_next_index_insert(ht, value);
  454. return;
  455. case IS_REFERENCE:
  456. ZVAL_DEREF(offset);
  457. goto try_again;
  458. default:
  459. zend_error(E_WARNING, "Illegal offset type");
  460. zval_ptr_dtor(value);
  461. return;
  462. }
  463. } /* }}} */
  464. static void spl_array_write_dimension(zval *object, zval *offset, zval *value) /* {{{ */
  465. {
  466. spl_array_write_dimension_ex(1, object, offset, value);
  467. } /* }}} */
  468. static void spl_array_unset_dimension_ex(int check_inherited, zval *object, zval *offset) /* {{{ */
  469. {
  470. zend_long index;
  471. HashTable *ht;
  472. spl_array_object *intern = Z_SPLARRAY_P(object);
  473. if (check_inherited && intern->fptr_offset_del) {
  474. SEPARATE_ARG_IF_REF(offset);
  475. zend_call_method_with_1_params(object, Z_OBJCE_P(object), &intern->fptr_offset_del, "offsetUnset", NULL, offset);
  476. zval_ptr_dtor(offset);
  477. return;
  478. }
  479. if (intern->nApplyCount > 0) {
  480. zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
  481. return;
  482. }
  483. try_again:
  484. switch (Z_TYPE_P(offset)) {
  485. case IS_STRING:
  486. ht = spl_array_get_hash_table(intern);
  487. if (ht == &EG(symbol_table)) {
  488. if (zend_delete_global_variable(Z_STR_P(offset))) {
  489. zend_error(E_NOTICE,"Undefined index: %s", Z_STRVAL_P(offset));
  490. }
  491. } else {
  492. zval *data = zend_symtable_find(ht, Z_STR_P(offset));
  493. if (data) {
  494. if (Z_TYPE_P(data) == IS_INDIRECT) {
  495. data = Z_INDIRECT_P(data);
  496. if (Z_TYPE_P(data) == IS_UNDEF) {
  497. zend_error(E_NOTICE,"Undefined index: %s", Z_STRVAL_P(offset));
  498. } else {
  499. zval_ptr_dtor(data);
  500. ZVAL_UNDEF(data);
  501. HT_FLAGS(ht) |= HASH_FLAG_HAS_EMPTY_IND;
  502. zend_hash_move_forward_ex(ht, spl_array_get_pos_ptr(ht, intern));
  503. if (spl_array_is_object(intern)) {
  504. spl_array_skip_protected(intern, ht);
  505. }
  506. }
  507. } else if (zend_symtable_del(ht, Z_STR_P(offset)) == FAILURE) {
  508. zend_error(E_NOTICE,"Undefined index: %s", Z_STRVAL_P(offset));
  509. }
  510. } else {
  511. zend_error(E_NOTICE,"Undefined index: %s", Z_STRVAL_P(offset));
  512. }
  513. }
  514. break;
  515. case IS_DOUBLE:
  516. index = (zend_long)Z_DVAL_P(offset);
  517. goto num_index;
  518. case IS_RESOURCE:
  519. index = Z_RES_HANDLE_P(offset);
  520. goto num_index;
  521. case IS_FALSE:
  522. index = 0;
  523. goto num_index;
  524. case IS_TRUE:
  525. index = 1;
  526. goto num_index;
  527. case IS_LONG:
  528. index = Z_LVAL_P(offset);
  529. num_index:
  530. ht = spl_array_get_hash_table(intern);
  531. if (zend_hash_index_del(ht, index) == FAILURE) {
  532. zend_error(E_NOTICE,"Undefined offset: " ZEND_LONG_FMT, index);
  533. }
  534. break;
  535. case IS_REFERENCE:
  536. ZVAL_DEREF(offset);
  537. goto try_again;
  538. default:
  539. zend_error(E_WARNING, "Illegal offset type");
  540. return;
  541. }
  542. } /* }}} */
  543. static void spl_array_unset_dimension(zval *object, zval *offset) /* {{{ */
  544. {
  545. spl_array_unset_dimension_ex(1, object, offset);
  546. } /* }}} */
  547. static int spl_array_has_dimension_ex(int check_inherited, zval *object, zval *offset, int check_empty) /* {{{ */
  548. {
  549. spl_array_object *intern = Z_SPLARRAY_P(object);
  550. zend_long index;
  551. zval rv, *value = NULL, *tmp;
  552. if (check_inherited && intern->fptr_offset_has) {
  553. SEPARATE_ARG_IF_REF(offset);
  554. zend_call_method_with_1_params(object, Z_OBJCE_P(object), &intern->fptr_offset_has, "offsetExists", &rv, offset);
  555. zval_ptr_dtor(offset);
  556. if (zend_is_true(&rv)) {
  557. zval_ptr_dtor(&rv);
  558. if (check_empty != 1) {
  559. return 1;
  560. } else if (intern->fptr_offset_get) {
  561. value = spl_array_read_dimension_ex(1, object, offset, BP_VAR_R, &rv);
  562. }
  563. } else {
  564. zval_ptr_dtor(&rv);
  565. return 0;
  566. }
  567. }
  568. if (!value) {
  569. HashTable *ht = spl_array_get_hash_table(intern);
  570. try_again:
  571. switch (Z_TYPE_P(offset)) {
  572. case IS_STRING:
  573. if ((tmp = zend_symtable_find(ht, Z_STR_P(offset))) != NULL) {
  574. if (check_empty == 2) {
  575. return 1;
  576. }
  577. } else {
  578. return 0;
  579. }
  580. break;
  581. case IS_DOUBLE:
  582. index = (zend_long)Z_DVAL_P(offset);
  583. goto num_index;
  584. case IS_RESOURCE:
  585. index = Z_RES_HANDLE_P(offset);
  586. goto num_index;
  587. case IS_FALSE:
  588. index = 0;
  589. goto num_index;
  590. case IS_TRUE:
  591. index = 1;
  592. goto num_index;
  593. case IS_LONG:
  594. index = Z_LVAL_P(offset);
  595. num_index:
  596. if ((tmp = zend_hash_index_find(ht, index)) != NULL) {
  597. if (check_empty == 2) {
  598. return 1;
  599. }
  600. } else {
  601. return 0;
  602. }
  603. break;
  604. case IS_REFERENCE:
  605. ZVAL_DEREF(offset);
  606. goto try_again;
  607. default:
  608. zend_error(E_WARNING, "Illegal offset type");
  609. return 0;
  610. }
  611. if (check_empty && check_inherited && intern->fptr_offset_get) {
  612. value = spl_array_read_dimension_ex(1, object, offset, BP_VAR_R, &rv);
  613. } else {
  614. value = tmp;
  615. }
  616. }
  617. {
  618. zend_bool result = check_empty ? zend_is_true(value) : Z_TYPE_P(value) != IS_NULL;
  619. if (value == &rv) {
  620. zval_ptr_dtor(&rv);
  621. }
  622. return result;
  623. }
  624. } /* }}} */
  625. static int spl_array_has_dimension(zval *object, zval *offset, int check_empty) /* {{{ */
  626. {
  627. return spl_array_has_dimension_ex(1, object, offset, check_empty);
  628. } /* }}} */
  629. /* {{{ spl_array_object_verify_pos_ex */
  630. static inline int spl_array_object_verify_pos_ex(spl_array_object *object, HashTable *ht, const char *msg_prefix)
  631. {
  632. if (!ht) {
  633. php_error_docref(NULL, E_NOTICE, "%sArray was modified outside object and is no longer an array", msg_prefix);
  634. return FAILURE;
  635. }
  636. return SUCCESS;
  637. } /* }}} */
  638. /* {{{ spl_array_object_verify_pos */
  639. static inline int spl_array_object_verify_pos(spl_array_object *object, HashTable *ht)
  640. {
  641. return spl_array_object_verify_pos_ex(object, ht, "");
  642. } /* }}} */
  643. /* {{{ proto bool ArrayObject::offsetExists(mixed $index)
  644. proto bool ArrayIterator::offsetExists(mixed $index)
  645. Returns whether the requested $index exists. */
  646. SPL_METHOD(Array, offsetExists)
  647. {
  648. zval *index;
  649. if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &index) == FAILURE) {
  650. return;
  651. }
  652. RETURN_BOOL(spl_array_has_dimension_ex(0, getThis(), index, 2));
  653. } /* }}} */
  654. /* {{{ proto mixed ArrayObject::offsetGet(mixed $index)
  655. proto mixed ArrayIterator::offsetGet(mixed $index)
  656. Returns the value at the specified $index. */
  657. SPL_METHOD(Array, offsetGet)
  658. {
  659. zval *value, *index;
  660. if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &index) == FAILURE) {
  661. return;
  662. }
  663. value = spl_array_read_dimension_ex(0, getThis(), index, BP_VAR_R, return_value);
  664. if (value != return_value) {
  665. ZVAL_COPY_DEREF(return_value, value);
  666. }
  667. } /* }}} */
  668. /* {{{ proto void ArrayObject::offsetSet(mixed $index, mixed $newval)
  669. proto void ArrayIterator::offsetSet(mixed $index, mixed $newval)
  670. Sets the value at the specified $index to $newval. */
  671. SPL_METHOD(Array, offsetSet)
  672. {
  673. zval *index, *value;
  674. if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &index, &value) == FAILURE) {
  675. return;
  676. }
  677. spl_array_write_dimension_ex(0, getThis(), index, value);
  678. } /* }}} */
  679. void spl_array_iterator_append(zval *object, zval *append_value) /* {{{ */
  680. {
  681. spl_array_object *intern = Z_SPLARRAY_P(object);
  682. HashTable *aht = spl_array_get_hash_table(intern);
  683. if (!aht) {
  684. php_error_docref(NULL, E_NOTICE, "Array was modified outside object and is no longer an array");
  685. return;
  686. }
  687. if (spl_array_is_object(intern)) {
  688. zend_throw_error(NULL, "Cannot append properties to objects, use %s::offsetSet() instead", ZSTR_VAL(Z_OBJCE_P(object)->name));
  689. return;
  690. }
  691. spl_array_write_dimension(object, NULL, append_value);
  692. } /* }}} */
  693. /* {{{ proto void ArrayObject::append(mixed $newval)
  694. proto void ArrayIterator::append(mixed $newval)
  695. Appends the value (cannot be called for objects). */
  696. SPL_METHOD(Array, append)
  697. {
  698. zval *value;
  699. if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &value) == FAILURE) {
  700. return;
  701. }
  702. spl_array_iterator_append(getThis(), value);
  703. } /* }}} */
  704. /* {{{ proto void ArrayObject::offsetUnset(mixed $index)
  705. proto void ArrayIterator::offsetUnset(mixed $index)
  706. Unsets the value at the specified $index. */
  707. SPL_METHOD(Array, offsetUnset)
  708. {
  709. zval *index;
  710. if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &index) == FAILURE) {
  711. return;
  712. }
  713. spl_array_unset_dimension_ex(0, getThis(), index);
  714. } /* }}} */
  715. /* {{{ proto array ArrayObject::getArrayCopy()
  716. proto array ArrayIterator::getArrayCopy()
  717. Return a copy of the contained array */
  718. SPL_METHOD(Array, getArrayCopy)
  719. {
  720. zval *object = getThis();
  721. spl_array_object *intern = Z_SPLARRAY_P(object);
  722. RETURN_ARR(zend_array_dup(spl_array_get_hash_table(intern)));
  723. } /* }}} */
  724. static HashTable *spl_array_get_properties(zval *object) /* {{{ */
  725. {
  726. spl_array_object *intern = Z_SPLARRAY_P(object);
  727. if (intern->ar_flags & SPL_ARRAY_STD_PROP_LIST) {
  728. if (!intern->std.properties) {
  729. rebuild_object_properties(&intern->std);
  730. }
  731. return intern->std.properties;
  732. }
  733. return spl_array_get_hash_table(intern);
  734. } /* }}} */
  735. static HashTable* spl_array_get_debug_info(zval *obj, int *is_temp) /* {{{ */
  736. {
  737. zval *storage;
  738. zend_string *zname;
  739. zend_class_entry *base;
  740. spl_array_object *intern = Z_SPLARRAY_P(obj);
  741. if (!intern->std.properties) {
  742. rebuild_object_properties(&intern->std);
  743. }
  744. if (intern->ar_flags & SPL_ARRAY_IS_SELF) {
  745. *is_temp = 0;
  746. return intern->std.properties;
  747. } else {
  748. HashTable *debug_info;
  749. *is_temp = 1;
  750. debug_info = zend_new_array(zend_hash_num_elements(intern->std.properties) + 1);
  751. zend_hash_copy(debug_info, intern->std.properties, (copy_ctor_func_t) zval_add_ref);
  752. storage = &intern->array;
  753. Z_TRY_ADDREF_P(storage);
  754. base = Z_OBJ_HT_P(obj) == &spl_handler_ArrayIterator
  755. ? spl_ce_ArrayIterator : spl_ce_ArrayObject;
  756. zname = spl_gen_private_prop_name(base, "storage", sizeof("storage")-1);
  757. zend_symtable_update(debug_info, zname, storage);
  758. zend_string_release_ex(zname, 0);
  759. return debug_info;
  760. }
  761. }
  762. /* }}} */
  763. static HashTable *spl_array_get_gc(zval *obj, zval **gc_data, int *gc_data_count) /* {{{ */
  764. {
  765. spl_array_object *intern = Z_SPLARRAY_P(obj);
  766. *gc_data = &intern->array;
  767. *gc_data_count = 1;
  768. return zend_std_get_properties(obj);
  769. }
  770. /* }}} */
  771. static zval *spl_array_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv) /* {{{ */
  772. {
  773. spl_array_object *intern = Z_SPLARRAY_P(object);
  774. if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
  775. && !zend_std_has_property(object, member, ZEND_PROPERTY_EXISTS, NULL)) {
  776. return spl_array_read_dimension(object, member, type, rv);
  777. }
  778. return zend_std_read_property(object, member, type, cache_slot, rv);
  779. } /* }}} */
  780. static void spl_array_write_property(zval *object, zval *member, zval *value, void **cache_slot) /* {{{ */
  781. {
  782. spl_array_object *intern = Z_SPLARRAY_P(object);
  783. if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
  784. && !zend_std_has_property(object, member, ZEND_PROPERTY_EXISTS, NULL)) {
  785. spl_array_write_dimension(object, member, value);
  786. return;
  787. }
  788. zend_std_write_property(object, member, value, cache_slot);
  789. } /* }}} */
  790. static zval *spl_array_get_property_ptr_ptr(zval *object, zval *member, int type, void **cache_slot) /* {{{ */
  791. {
  792. spl_array_object *intern = Z_SPLARRAY_P(object);
  793. if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
  794. && !zend_std_has_property(object, member, ZEND_PROPERTY_EXISTS, NULL)) {
  795. /* If object has offsetGet() overridden, then fallback to read_property,
  796. * which will call offsetGet(). */
  797. if (intern->fptr_offset_get) {
  798. return NULL;
  799. }
  800. return spl_array_get_dimension_ptr(1, intern, member, type);
  801. }
  802. return zend_std_get_property_ptr_ptr(object, member, type, cache_slot);
  803. } /* }}} */
  804. static int spl_array_has_property(zval *object, zval *member, int has_set_exists, void **cache_slot) /* {{{ */
  805. {
  806. spl_array_object *intern = Z_SPLARRAY_P(object);
  807. if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
  808. && !zend_std_has_property(object, member, ZEND_PROPERTY_EXISTS, NULL)) {
  809. return spl_array_has_dimension(object, member, has_set_exists);
  810. }
  811. return zend_std_has_property(object, member, has_set_exists, cache_slot);
  812. } /* }}} */
  813. static void spl_array_unset_property(zval *object, zval *member, void **cache_slot) /* {{{ */
  814. {
  815. spl_array_object *intern = Z_SPLARRAY_P(object);
  816. if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
  817. && !zend_std_has_property(object, member, ZEND_PROPERTY_EXISTS, NULL)) {
  818. spl_array_unset_dimension(object, member);
  819. return;
  820. }
  821. zend_std_unset_property(object, member, cache_slot);
  822. } /* }}} */
  823. static int spl_array_compare_objects(zval *o1, zval *o2) /* {{{ */
  824. {
  825. HashTable *ht1,
  826. *ht2;
  827. spl_array_object *intern1,
  828. *intern2;
  829. int result = 0;
  830. intern1 = Z_SPLARRAY_P(o1);
  831. intern2 = Z_SPLARRAY_P(o2);
  832. ht1 = spl_array_get_hash_table(intern1);
  833. ht2 = spl_array_get_hash_table(intern2);
  834. result = zend_compare_symbol_tables(ht1, ht2);
  835. /* if we just compared std.properties, don't do it again */
  836. if (result == 0 &&
  837. !(ht1 == intern1->std.properties && ht2 == intern2->std.properties)) {
  838. result = zend_std_compare_objects(o1, o2);
  839. }
  840. return result;
  841. } /* }}} */
  842. static int spl_array_skip_protected(spl_array_object *intern, HashTable *aht) /* {{{ */
  843. {
  844. zend_string *string_key;
  845. zend_ulong num_key;
  846. zval *data;
  847. if (spl_array_is_object(intern)) {
  848. uint32_t *pos_ptr = spl_array_get_pos_ptr(aht, intern);
  849. do {
  850. if (zend_hash_get_current_key_ex(aht, &string_key, &num_key, pos_ptr) == HASH_KEY_IS_STRING) {
  851. data = zend_hash_get_current_data_ex(aht, pos_ptr);
  852. if (data && Z_TYPE_P(data) == IS_INDIRECT &&
  853. Z_TYPE_P(data = Z_INDIRECT_P(data)) == IS_UNDEF) {
  854. /* skip */
  855. } else if (!ZSTR_LEN(string_key) || ZSTR_VAL(string_key)[0]) {
  856. return SUCCESS;
  857. }
  858. } else {
  859. return SUCCESS;
  860. }
  861. if (zend_hash_has_more_elements_ex(aht, pos_ptr) != SUCCESS) {
  862. return FAILURE;
  863. }
  864. zend_hash_move_forward_ex(aht, pos_ptr);
  865. } while (1);
  866. }
  867. return FAILURE;
  868. } /* }}} */
  869. static int spl_array_next_ex(spl_array_object *intern, HashTable *aht) /* {{{ */
  870. {
  871. uint32_t *pos_ptr = spl_array_get_pos_ptr(aht, intern);
  872. zend_hash_move_forward_ex(aht, pos_ptr);
  873. if (spl_array_is_object(intern)) {
  874. return spl_array_skip_protected(intern, aht);
  875. } else {
  876. return zend_hash_has_more_elements_ex(aht, pos_ptr);
  877. }
  878. } /* }}} */
  879. static int spl_array_next(spl_array_object *intern) /* {{{ */
  880. {
  881. HashTable *aht = spl_array_get_hash_table(intern);
  882. return spl_array_next_ex(intern, aht);
  883. } /* }}} */
  884. static void spl_array_it_dtor(zend_object_iterator *iter) /* {{{ */
  885. {
  886. zend_user_it_invalidate_current(iter);
  887. zval_ptr_dtor(&iter->data);
  888. }
  889. /* }}} */
  890. static int spl_array_it_valid(zend_object_iterator *iter) /* {{{ */
  891. {
  892. spl_array_object *object = Z_SPLARRAY_P(&iter->data);
  893. HashTable *aht = spl_array_get_hash_table(object);
  894. if (object->ar_flags & SPL_ARRAY_OVERLOADED_VALID) {
  895. return zend_user_it_valid(iter);
  896. } else {
  897. if (spl_array_object_verify_pos_ex(object, aht, "ArrayIterator::valid(): ") == FAILURE) {
  898. return FAILURE;
  899. }
  900. return zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, object));
  901. }
  902. }
  903. /* }}} */
  904. static zval *spl_array_it_get_current_data(zend_object_iterator *iter) /* {{{ */
  905. {
  906. spl_array_object *object = Z_SPLARRAY_P(&iter->data);
  907. HashTable *aht = spl_array_get_hash_table(object);
  908. if (object->ar_flags & SPL_ARRAY_OVERLOADED_CURRENT) {
  909. return zend_user_it_get_current_data(iter);
  910. } else {
  911. zval *data = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, object));
  912. if (data && Z_TYPE_P(data) == IS_INDIRECT) {
  913. data = Z_INDIRECT_P(data);
  914. }
  915. return data;
  916. }
  917. }
  918. /* }}} */
  919. static void spl_array_it_get_current_key(zend_object_iterator *iter, zval *key) /* {{{ */
  920. {
  921. spl_array_object *object = Z_SPLARRAY_P(&iter->data);
  922. HashTable *aht = spl_array_get_hash_table(object);
  923. if (object->ar_flags & SPL_ARRAY_OVERLOADED_KEY) {
  924. zend_user_it_get_current_key(iter, key);
  925. } else {
  926. if (spl_array_object_verify_pos_ex(object, aht, "ArrayIterator::current(): ") == FAILURE) {
  927. ZVAL_NULL(key);
  928. } else {
  929. zend_hash_get_current_key_zval_ex(aht, key, spl_array_get_pos_ptr(aht, object));
  930. }
  931. }
  932. }
  933. /* }}} */
  934. static void spl_array_it_move_forward(zend_object_iterator *iter) /* {{{ */
  935. {
  936. spl_array_object *object = Z_SPLARRAY_P(&iter->data);
  937. HashTable *aht = spl_array_get_hash_table(object);
  938. if (object->ar_flags & SPL_ARRAY_OVERLOADED_NEXT) {
  939. zend_user_it_move_forward(iter);
  940. } else {
  941. zend_user_it_invalidate_current(iter);
  942. if (!aht) {
  943. php_error_docref(NULL, E_NOTICE, "ArrayIterator::current(): Array was modified outside object and is no longer an array");
  944. return;
  945. }
  946. spl_array_next_ex(object, aht);
  947. }
  948. }
  949. /* }}} */
  950. static void spl_array_rewind(spl_array_object *intern) /* {{{ */
  951. {
  952. HashTable *aht = spl_array_get_hash_table(intern);
  953. if (!aht) {
  954. php_error_docref(NULL, E_NOTICE, "ArrayIterator::rewind(): Array was modified outside object and is no longer an array");
  955. return;
  956. }
  957. if (intern->ht_iter == (uint32_t)-1) {
  958. spl_array_get_pos_ptr(aht, intern);
  959. } else {
  960. zend_hash_internal_pointer_reset_ex(aht, spl_array_get_pos_ptr(aht, intern));
  961. spl_array_skip_protected(intern, aht);
  962. }
  963. }
  964. /* }}} */
  965. static void spl_array_it_rewind(zend_object_iterator *iter) /* {{{ */
  966. {
  967. spl_array_object *object = Z_SPLARRAY_P(&iter->data);
  968. if (object->ar_flags & SPL_ARRAY_OVERLOADED_REWIND) {
  969. zend_user_it_rewind(iter);
  970. } else {
  971. zend_user_it_invalidate_current(iter);
  972. spl_array_rewind(object);
  973. }
  974. }
  975. /* }}} */
  976. /* {{{ spl_array_set_array */
  977. static void spl_array_set_array(zval *object, spl_array_object *intern, zval *array, zend_long ar_flags, int just_array) {
  978. if (Z_TYPE_P(array) != IS_OBJECT && Z_TYPE_P(array) != IS_ARRAY) {
  979. zend_throw_exception(spl_ce_InvalidArgumentException, "Passed variable is not an array or object", 0);
  980. return;
  981. }
  982. if (Z_TYPE_P(array) == IS_ARRAY) {
  983. zval_ptr_dtor(&intern->array);
  984. if (Z_REFCOUNT_P(array) == 1) {
  985. ZVAL_COPY(&intern->array, array);
  986. } else {
  987. //??? TODO: try to avoid array duplication
  988. ZVAL_ARR(&intern->array, zend_array_dup(Z_ARR_P(array)));
  989. }
  990. } else {
  991. if (Z_OBJ_HT_P(array) == &spl_handler_ArrayObject || Z_OBJ_HT_P(array) == &spl_handler_ArrayIterator) {
  992. zval_ptr_dtor(&intern->array);
  993. if (just_array) {
  994. spl_array_object *other = Z_SPLARRAY_P(array);
  995. ar_flags = other->ar_flags & ~SPL_ARRAY_INT_MASK;
  996. }
  997. if (Z_OBJ_P(object) == Z_OBJ_P(array)) {
  998. ar_flags |= SPL_ARRAY_IS_SELF;
  999. ZVAL_UNDEF(&intern->array);
  1000. } else {
  1001. ar_flags |= SPL_ARRAY_USE_OTHER;
  1002. ZVAL_COPY(&intern->array, array);
  1003. }
  1004. } else {
  1005. zend_object_get_properties_t handler = Z_OBJ_HANDLER_P(array, get_properties);
  1006. if (handler != zend_std_get_properties) {
  1007. zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0,
  1008. "Overloaded object of type %s is not compatible with %s",
  1009. ZSTR_VAL(Z_OBJCE_P(array)->name), ZSTR_VAL(intern->std.ce->name));
  1010. return;
  1011. }
  1012. zval_ptr_dtor(&intern->array);
  1013. ZVAL_COPY(&intern->array, array);
  1014. }
  1015. }
  1016. intern->ar_flags &= ~SPL_ARRAY_IS_SELF & ~SPL_ARRAY_USE_OTHER;
  1017. intern->ar_flags |= ar_flags;
  1018. intern->ht_iter = (uint32_t)-1;
  1019. }
  1020. /* }}} */
  1021. /* iterator handler table */
  1022. static const zend_object_iterator_funcs spl_array_it_funcs = {
  1023. spl_array_it_dtor,
  1024. spl_array_it_valid,
  1025. spl_array_it_get_current_data,
  1026. spl_array_it_get_current_key,
  1027. spl_array_it_move_forward,
  1028. spl_array_it_rewind,
  1029. NULL
  1030. };
  1031. zend_object_iterator *spl_array_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
  1032. {
  1033. zend_user_iterator *iterator;
  1034. spl_array_object *array_object = Z_SPLARRAY_P(object);
  1035. if (by_ref && (array_object->ar_flags & SPL_ARRAY_OVERLOADED_CURRENT)) {
  1036. zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0);
  1037. return NULL;
  1038. }
  1039. iterator = emalloc(sizeof(zend_user_iterator));
  1040. zend_iterator_init(&iterator->it);
  1041. ZVAL_COPY(&iterator->it.data, object);
  1042. iterator->it.funcs = &spl_array_it_funcs;
  1043. iterator->ce = ce;
  1044. ZVAL_UNDEF(&iterator->value);
  1045. return &iterator->it;
  1046. }
  1047. /* }}} */
  1048. /* {{{ proto ArrayObject::__construct([array|object ar = array() [, int flags = 0 [, string iterator_class = "ArrayIterator"]]])
  1049. Constructs a new array object from an array or object. */
  1050. SPL_METHOD(Array, __construct)
  1051. {
  1052. zval *object = getThis();
  1053. spl_array_object *intern;
  1054. zval *array;
  1055. zend_long ar_flags = 0;
  1056. zend_class_entry *ce_get_iterator = spl_ce_Iterator;
  1057. if (ZEND_NUM_ARGS() == 0) {
  1058. return; /* nothing to do */
  1059. }
  1060. if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "z|lC", &array, &ar_flags, &ce_get_iterator) == FAILURE) {
  1061. return;
  1062. }
  1063. intern = Z_SPLARRAY_P(object);
  1064. if (ZEND_NUM_ARGS() > 2) {
  1065. intern->ce_get_iterator = ce_get_iterator;
  1066. }
  1067. ar_flags &= ~SPL_ARRAY_INT_MASK;
  1068. spl_array_set_array(object, intern, array, ar_flags, ZEND_NUM_ARGS() == 1);
  1069. }
  1070. /* }}} */
  1071. /* {{{ proto ArrayIterator::__construct([array|object ar = array() [, int flags = 0]])
  1072. Constructs a new array iterator from an array or object. */
  1073. SPL_METHOD(ArrayIterator, __construct)
  1074. {
  1075. zval *object = getThis();
  1076. spl_array_object *intern;
  1077. zval *array;
  1078. zend_long ar_flags = 0;
  1079. if (ZEND_NUM_ARGS() == 0) {
  1080. return; /* nothing to do */
  1081. }
  1082. if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "z|l", &array, &ar_flags) == FAILURE) {
  1083. return;
  1084. }
  1085. intern = Z_SPLARRAY_P(object);
  1086. ar_flags &= ~SPL_ARRAY_INT_MASK;
  1087. spl_array_set_array(object, intern, array, ar_flags, ZEND_NUM_ARGS() == 1);
  1088. }
  1089. /* }}} */
  1090. /* {{{ proto void ArrayObject::setIteratorClass(string iterator_class)
  1091. Set the class used in getIterator. */
  1092. SPL_METHOD(Array, setIteratorClass)
  1093. {
  1094. zval *object = getThis();
  1095. spl_array_object *intern = Z_SPLARRAY_P(object);
  1096. zend_class_entry * ce_get_iterator = spl_ce_Iterator;
  1097. ZEND_PARSE_PARAMETERS_START(1, 1)
  1098. Z_PARAM_CLASS(ce_get_iterator)
  1099. ZEND_PARSE_PARAMETERS_END();
  1100. intern->ce_get_iterator = ce_get_iterator;
  1101. }
  1102. /* }}} */
  1103. /* {{{ proto string ArrayObject::getIteratorClass()
  1104. Get the class used in getIterator. */
  1105. SPL_METHOD(Array, getIteratorClass)
  1106. {
  1107. zval *object = getThis();
  1108. spl_array_object *intern = Z_SPLARRAY_P(object);
  1109. if (zend_parse_parameters_none() == FAILURE) {
  1110. return;
  1111. }
  1112. zend_string_addref(intern->ce_get_iterator->name);
  1113. RETURN_STR(intern->ce_get_iterator->name);
  1114. }
  1115. /* }}} */
  1116. /* {{{ proto int ArrayObject::getFlags()
  1117. Get flags */
  1118. SPL_METHOD(Array, getFlags)
  1119. {
  1120. zval *object = getThis();
  1121. spl_array_object *intern = Z_SPLARRAY_P(object);
  1122. if (zend_parse_parameters_none() == FAILURE) {
  1123. return;
  1124. }
  1125. RETURN_LONG(intern->ar_flags & ~SPL_ARRAY_INT_MASK);
  1126. }
  1127. /* }}} */
  1128. /* {{{ proto void ArrayObject::setFlags(int flags)
  1129. Set flags */
  1130. SPL_METHOD(Array, setFlags)
  1131. {
  1132. zval *object = getThis();
  1133. spl_array_object *intern = Z_SPLARRAY_P(object);
  1134. zend_long ar_flags = 0;
  1135. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &ar_flags) == FAILURE) {
  1136. return;
  1137. }
  1138. intern->ar_flags = (intern->ar_flags & SPL_ARRAY_INT_MASK) | (ar_flags & ~SPL_ARRAY_INT_MASK);
  1139. }
  1140. /* }}} */
  1141. /* {{{ proto Array|Object ArrayObject::exchangeArray(Array|Object ar = array())
  1142. Replace the referenced array or object with a new one and return the old one (right now copy - to be changed) */
  1143. SPL_METHOD(Array, exchangeArray)
  1144. {
  1145. zval *object = getThis(), *array;
  1146. spl_array_object *intern = Z_SPLARRAY_P(object);
  1147. if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &array) == FAILURE) {
  1148. return;
  1149. }
  1150. if (intern->nApplyCount > 0) {
  1151. zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
  1152. return;
  1153. }
  1154. RETVAL_ARR(zend_array_dup(spl_array_get_hash_table(intern)));
  1155. spl_array_set_array(object, intern, array, 0L, 1);
  1156. }
  1157. /* }}} */
  1158. /* {{{ proto ArrayIterator ArrayObject::getIterator()
  1159. Create a new iterator from a ArrayObject instance */
  1160. SPL_METHOD(Array, getIterator)
  1161. {
  1162. zval *object = getThis();
  1163. spl_array_object *intern = Z_SPLARRAY_P(object);
  1164. HashTable *aht = spl_array_get_hash_table(intern);
  1165. if (zend_parse_parameters_none() == FAILURE) {
  1166. return;
  1167. }
  1168. if (!aht) {
  1169. php_error_docref(NULL, E_NOTICE, "Array was modified outside object and is no longer an array");
  1170. return;
  1171. }
  1172. ZVAL_OBJ(return_value, spl_array_object_new_ex(intern->ce_get_iterator, object, 0));
  1173. }
  1174. /* }}} */
  1175. /* {{{ proto void ArrayIterator::rewind()
  1176. Rewind array back to the start */
  1177. SPL_METHOD(Array, rewind)
  1178. {
  1179. zval *object = getThis();
  1180. spl_array_object *intern = Z_SPLARRAY_P(object);
  1181. if (zend_parse_parameters_none() == FAILURE) {
  1182. return;
  1183. }
  1184. spl_array_rewind(intern);
  1185. }
  1186. /* }}} */
  1187. /* {{{ proto void ArrayIterator::seek(int $position)
  1188. Seek to position. */
  1189. SPL_METHOD(Array, seek)
  1190. {
  1191. zend_long opos, position;
  1192. zval *object = getThis();
  1193. spl_array_object *intern = Z_SPLARRAY_P(object);
  1194. HashTable *aht = spl_array_get_hash_table(intern);
  1195. int result;
  1196. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &position) == FAILURE) {
  1197. return;
  1198. }
  1199. if (!aht) {
  1200. php_error_docref(NULL, E_NOTICE, "Array was modified outside object and is no longer an array");
  1201. return;
  1202. }
  1203. opos = position;
  1204. if (position >= 0) { /* negative values are not supported */
  1205. spl_array_rewind(intern);
  1206. result = SUCCESS;
  1207. while (position-- > 0 && (result = spl_array_next(intern)) == SUCCESS);
  1208. if (result == SUCCESS && zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, intern)) == SUCCESS) {
  1209. return; /* ok */
  1210. }
  1211. }
  1212. zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0, "Seek position " ZEND_LONG_FMT " is out of range", opos);
  1213. } /* }}} */
  1214. static int spl_array_object_count_elements_helper(spl_array_object *intern, zend_long *count) /* {{{ */
  1215. {
  1216. HashTable *aht = spl_array_get_hash_table(intern);
  1217. HashPosition pos, *pos_ptr;
  1218. if (!aht) {
  1219. php_error_docref(NULL, E_NOTICE, "Array was modified outside object and is no longer an array");
  1220. *count = 0;
  1221. return FAILURE;
  1222. }
  1223. if (spl_array_is_object(intern)) {
  1224. /* We need to store the 'pos' since we'll modify it in the functions
  1225. * we're going to call and which do not support 'pos' as parameter. */
  1226. pos_ptr = spl_array_get_pos_ptr(aht, intern);
  1227. pos = *pos_ptr;
  1228. *count = 0;
  1229. spl_array_rewind(intern);
  1230. while (*pos_ptr < aht->nNumUsed && spl_array_next(intern) == SUCCESS) {
  1231. (*count)++;
  1232. }
  1233. *pos_ptr = pos;
  1234. return SUCCESS;
  1235. } else {
  1236. *count = zend_hash_num_elements(aht);
  1237. return SUCCESS;
  1238. }
  1239. } /* }}} */
  1240. int spl_array_object_count_elements(zval *object, zend_long *count) /* {{{ */
  1241. {
  1242. spl_array_object *intern = Z_SPLARRAY_P(object);
  1243. if (intern->fptr_count) {
  1244. zval rv;
  1245. zend_call_method_with_0_params(object, intern->std.ce, &intern->fptr_count, "count", &rv);
  1246. if (Z_TYPE(rv) != IS_UNDEF) {
  1247. *count = zval_get_long(&rv);
  1248. zval_ptr_dtor(&rv);
  1249. return SUCCESS;
  1250. }
  1251. *count = 0;
  1252. return FAILURE;
  1253. }
  1254. return spl_array_object_count_elements_helper(intern, count);
  1255. } /* }}} */
  1256. /* {{{ proto int ArrayObject::count()
  1257. proto int ArrayIterator::count()
  1258. Return the number of elements in the Iterator. */
  1259. SPL_METHOD(Array, count)
  1260. {
  1261. zend_long count;
  1262. spl_array_object *intern = Z_SPLARRAY_P(getThis());
  1263. if (zend_parse_parameters_none() == FAILURE) {
  1264. return;
  1265. }
  1266. spl_array_object_count_elements_helper(intern, &count);
  1267. RETURN_LONG(count);
  1268. } /* }}} */
  1269. static void spl_array_method(INTERNAL_FUNCTION_PARAMETERS, char *fname, int fname_len, int use_arg) /* {{{ */
  1270. {
  1271. spl_array_object *intern = Z_SPLARRAY_P(getThis());
  1272. HashTable *aht = spl_array_get_hash_table(intern);
  1273. zval function_name, params[2], *arg = NULL;
  1274. ZVAL_STRINGL(&function_name, fname, fname_len);
  1275. ZVAL_NEW_EMPTY_REF(&params[0]);
  1276. ZVAL_ARR(Z_REFVAL(params[0]), aht);
  1277. GC_ADDREF(aht);
  1278. if (!use_arg) {
  1279. intern->nApplyCount++;
  1280. call_user_function(EG(function_table), NULL, &function_name, return_value, 1, params);
  1281. intern->nApplyCount--;
  1282. } else if (use_arg == SPL_ARRAY_METHOD_MAY_USER_ARG) {
  1283. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "|z", &arg) == FAILURE) {
  1284. zend_throw_exception(spl_ce_BadMethodCallException, "Function expects one argument at most", 0);
  1285. goto exit;
  1286. }
  1287. if (arg) {
  1288. ZVAL_COPY_VALUE(&params[1], arg);
  1289. }
  1290. intern->nApplyCount++;
  1291. call_user_function(EG(function_table), NULL, &function_name, return_value, arg ? 2 : 1, params);
  1292. intern->nApplyCount--;
  1293. } else {
  1294. if (ZEND_NUM_ARGS() != 1 || zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
  1295. zend_throw_exception(spl_ce_BadMethodCallException, "Function expects exactly one argument", 0);
  1296. goto exit;
  1297. }
  1298. ZVAL_COPY_VALUE(&params[1], arg);
  1299. intern->nApplyCount++;
  1300. call_user_function(EG(function_table), NULL, &function_name, return_value, 2, params);
  1301. intern->nApplyCount--;
  1302. }
  1303. exit:
  1304. {
  1305. HashTable *new_ht = Z_ARRVAL_P(Z_REFVAL(params[0]));
  1306. if (aht != new_ht) {
  1307. spl_array_replace_hash_table(intern, new_ht);
  1308. } else {
  1309. GC_DELREF(aht);
  1310. }
  1311. efree(Z_REF(params[0]));
  1312. zend_string_free(Z_STR(function_name));
  1313. }
  1314. } /* }}} */
  1315. #define SPL_ARRAY_METHOD(cname, fname, use_arg) \
  1316. SPL_METHOD(cname, fname) \
  1317. { \
  1318. spl_array_method(INTERNAL_FUNCTION_PARAM_PASSTHRU, #fname, sizeof(#fname)-1, use_arg); \
  1319. }
  1320. /* {{{ proto int ArrayObject::asort([int $sort_flags = SORT_REGULAR ])
  1321. proto int ArrayIterator::asort([int $sort_flags = SORT_REGULAR ])
  1322. Sort the entries by values. */
  1323. SPL_ARRAY_METHOD(Array, asort, SPL_ARRAY_METHOD_MAY_USER_ARG) /* }}} */
  1324. /* {{{ proto int ArrayObject::ksort([int $sort_flags = SORT_REGULAR ])
  1325. proto int ArrayIterator::ksort([int $sort_flags = SORT_REGULAR ])
  1326. Sort the entries by key. */
  1327. SPL_ARRAY_METHOD(Array, ksort, SPL_ARRAY_METHOD_MAY_USER_ARG) /* }}} */
  1328. /* {{{ proto int ArrayObject::uasort(callback cmp_function)
  1329. proto int ArrayIterator::uasort(callback cmp_function)
  1330. Sort the entries by values user defined function. */
  1331. SPL_ARRAY_METHOD(Array, uasort, SPL_ARRAY_METHOD_USE_ARG) /* }}} */
  1332. /* {{{ proto int ArrayObject::uksort(callback cmp_function)
  1333. proto int ArrayIterator::uksort(callback cmp_function)
  1334. Sort the entries by key using user defined function. */
  1335. SPL_ARRAY_METHOD(Array, uksort, SPL_ARRAY_METHOD_USE_ARG) /* }}} */
  1336. /* {{{ proto int ArrayObject::natsort()
  1337. proto int ArrayIterator::natsort()
  1338. Sort the entries by values using "natural order" algorithm. */
  1339. SPL_ARRAY_METHOD(Array, natsort, SPL_ARRAY_METHOD_NO_ARG) /* }}} */
  1340. /* {{{ proto int ArrayObject::natcasesort()
  1341. proto int ArrayIterator::natcasesort()
  1342. Sort the entries by key using case insensitive "natural order" algorithm. */
  1343. SPL_ARRAY_METHOD(Array, natcasesort, SPL_ARRAY_METHOD_NO_ARG) /* }}} */
  1344. /* {{{ proto mixed|NULL ArrayIterator::current()
  1345. Return current array entry */
  1346. SPL_METHOD(Array, current)
  1347. {
  1348. zval *object = getThis();
  1349. spl_array_object *intern = Z_SPLARRAY_P(object);
  1350. zval *entry;
  1351. HashTable *aht = spl_array_get_hash_table(intern);
  1352. if (zend_parse_parameters_none() == FAILURE) {
  1353. return;
  1354. }
  1355. if (spl_array_object_verify_pos(intern, aht) == FAILURE) {
  1356. return;
  1357. }
  1358. if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
  1359. return;
  1360. }
  1361. if (Z_TYPE_P(entry) == IS_INDIRECT) {
  1362. entry = Z_INDIRECT_P(entry);
  1363. if (Z_TYPE_P(entry) == IS_UNDEF) {
  1364. return;
  1365. }
  1366. }
  1367. ZVAL_COPY_DEREF(return_value, entry);
  1368. }
  1369. /* }}} */
  1370. /* {{{ proto mixed|NULL ArrayIterator::key()
  1371. Return current array key */
  1372. SPL_METHOD(Array, key)
  1373. {
  1374. if (zend_parse_parameters_none() == FAILURE) {
  1375. return;
  1376. }
  1377. spl_array_iterator_key(getThis(), return_value);
  1378. } /* }}} */
  1379. void spl_array_iterator_key(zval *object, zval *return_value) /* {{{ */
  1380. {
  1381. spl_array_object *intern = Z_SPLARRAY_P(object);
  1382. HashTable *aht = spl_array_get_hash_table(intern);
  1383. if (spl_array_object_verify_pos(intern, aht) == FAILURE) {
  1384. return;
  1385. }
  1386. zend_hash_get_current_key_zval_ex(aht, return_value, spl_array_get_pos_ptr(aht, intern));
  1387. }
  1388. /* }}} */
  1389. /* {{{ proto void ArrayIterator::next()
  1390. Move to next entry */
  1391. SPL_METHOD(Array, next)
  1392. {
  1393. zval *object = getThis();
  1394. spl_array_object *intern = Z_SPLARRAY_P(object);
  1395. HashTable *aht = spl_array_get_hash_table(intern);
  1396. if (zend_parse_parameters_none() == FAILURE) {
  1397. return;
  1398. }
  1399. if (spl_array_object_verify_pos(intern, aht) == FAILURE) {
  1400. return;
  1401. }
  1402. spl_array_next_ex(intern, aht);
  1403. }
  1404. /* }}} */
  1405. /* {{{ proto bool ArrayIterator::valid()
  1406. Check whether array contains more entries */
  1407. SPL_METHOD(Array, valid)
  1408. {
  1409. zval *object = getThis();
  1410. spl_array_object *intern = Z_SPLARRAY_P(object);
  1411. HashTable *aht = spl_array_get_hash_table(intern);
  1412. if (zend_parse_parameters_none() == FAILURE) {
  1413. return;
  1414. }
  1415. if (spl_array_object_verify_pos(intern, aht) == FAILURE) {
  1416. RETURN_FALSE;
  1417. } else {
  1418. RETURN_BOOL(zend_hash_has_more_elements_ex(aht, spl_array_get_pos_ptr(aht, intern)) == SUCCESS);
  1419. }
  1420. }
  1421. /* }}} */
  1422. /* {{{ proto bool RecursiveArrayIterator::hasChildren()
  1423. Check whether current element has children (e.g. is an array) */
  1424. SPL_METHOD(Array, hasChildren)
  1425. {
  1426. zval *object = getThis(), *entry;
  1427. spl_array_object *intern = Z_SPLARRAY_P(object);
  1428. HashTable *aht = spl_array_get_hash_table(intern);
  1429. if (zend_parse_parameters_none() == FAILURE) {
  1430. return;
  1431. }
  1432. if (spl_array_object_verify_pos(intern, aht) == FAILURE) {
  1433. RETURN_FALSE;
  1434. }
  1435. if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
  1436. RETURN_FALSE;
  1437. }
  1438. if (Z_TYPE_P(entry) == IS_INDIRECT) {
  1439. entry = Z_INDIRECT_P(entry);
  1440. }
  1441. ZVAL_DEREF(entry);
  1442. RETURN_BOOL(Z_TYPE_P(entry) == IS_ARRAY || (Z_TYPE_P(entry) == IS_OBJECT && (intern->ar_flags & SPL_ARRAY_CHILD_ARRAYS_ONLY) == 0));
  1443. }
  1444. /* }}} */
  1445. /* {{{ proto object RecursiveArrayIterator::getChildren()
  1446. Create a sub iterator for the current element (same class as $this) */
  1447. SPL_METHOD(Array, getChildren)
  1448. {
  1449. zval *object = getThis(), *entry, flags;
  1450. spl_array_object *intern = Z_SPLARRAY_P(object);
  1451. HashTable *aht = spl_array_get_hash_table(intern);
  1452. if (zend_parse_parameters_none() == FAILURE) {
  1453. return;
  1454. }
  1455. if (spl_array_object_verify_pos(intern, aht) == FAILURE) {
  1456. return;
  1457. }
  1458. if ((entry = zend_hash_get_current_data_ex(aht, spl_array_get_pos_ptr(aht, intern))) == NULL) {
  1459. return;
  1460. }
  1461. if (Z_TYPE_P(entry) == IS_INDIRECT) {
  1462. entry = Z_INDIRECT_P(entry);
  1463. }
  1464. ZVAL_DEREF(entry);
  1465. if (Z_TYPE_P(entry) == IS_OBJECT) {
  1466. if ((intern->ar_flags & SPL_ARRAY_CHILD_ARRAYS_ONLY) != 0) {
  1467. return;
  1468. }
  1469. if (instanceof_function(Z_OBJCE_P(entry), Z_OBJCE_P(getThis()))) {
  1470. ZVAL_OBJ(return_value, Z_OBJ_P(entry));
  1471. Z_ADDREF_P(return_value);
  1472. return;
  1473. }
  1474. }
  1475. ZVAL_LONG(&flags, intern->ar_flags);
  1476. spl_instantiate_arg_ex2(Z_OBJCE_P(getThis()), return_value, entry, &flags);
  1477. }
  1478. /* }}} */
  1479. /* {{{ proto string ArrayObject::serialize()
  1480. Serialize the object */
  1481. SPL_METHOD(Array, serialize)
  1482. {
  1483. zval *object = getThis();
  1484. spl_array_object *intern = Z_SPLARRAY_P(object);
  1485. HashTable *aht = spl_array_get_hash_table(intern);
  1486. zval members, flags;
  1487. php_serialize_data_t var_hash;
  1488. smart_str buf = {0};
  1489. if (zend_parse_parameters_none() == FAILURE) {
  1490. return;
  1491. }
  1492. if (!aht) {
  1493. php_error_docref(NULL, E_NOTICE, "Array was modified outside object and is no longer an array");
  1494. return;
  1495. }
  1496. PHP_VAR_SERIALIZE_INIT(var_hash);
  1497. ZVAL_LONG(&flags, (intern->ar_flags & SPL_ARRAY_CLONE_MASK));
  1498. /* storage */
  1499. smart_str_appendl(&buf, "x:", 2);
  1500. php_var_serialize(&buf, &flags, &var_hash);
  1501. if (!(intern->ar_flags & SPL_ARRAY_IS_SELF)) {
  1502. php_var_serialize(&buf, &intern->array, &var_hash);
  1503. smart_str_appendc(&buf, ';');
  1504. }
  1505. /* members */
  1506. smart_str_appendl(&buf, "m:", 2);
  1507. if (!intern->std.properties) {
  1508. rebuild_object_properties(&intern->std);
  1509. }
  1510. ZVAL_ARR(&members, intern->std.properties);
  1511. php_var_serialize(&buf, &members, &var_hash); /* finishes the string */
  1512. /* done */
  1513. PHP_VAR_SERIALIZE_DESTROY(var_hash);
  1514. if (buf.s) {
  1515. RETURN_NEW_STR(buf.s);
  1516. }
  1517. RETURN_NULL();
  1518. } /* }}} */
  1519. /* {{{ proto void ArrayObject::unserialize(string serialized)
  1520. * unserialize the object
  1521. */
  1522. SPL_METHOD(Array, unserialize)
  1523. {
  1524. zval *object = getThis();
  1525. spl_array_object *intern = Z_SPLARRAY_P(object);
  1526. char *buf;
  1527. size_t buf_len;
  1528. const unsigned char *p, *s;
  1529. php_unserialize_data_t var_hash;
  1530. zval *members, *zflags, *array;
  1531. zend_long flags;
  1532. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &buf, &buf_len) == FAILURE) {
  1533. return;
  1534. }
  1535. if (buf_len == 0) {
  1536. return;
  1537. }
  1538. if (intern->nApplyCount > 0) {
  1539. zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
  1540. return;
  1541. }
  1542. /* storage */
  1543. s = p = (const unsigned char*)buf;
  1544. PHP_VAR_UNSERIALIZE_INIT(var_hash);
  1545. if (*p!= 'x' || *++p != ':') {
  1546. goto outexcept;
  1547. }
  1548. ++p;
  1549. zflags = var_tmp_var(&var_hash);
  1550. if (!php_var_unserialize(zflags, &p, s + buf_len, &var_hash) || Z_TYPE_P(zflags) != IS_LONG) {
  1551. goto outexcept;
  1552. }
  1553. --p; /* for ';' */
  1554. flags = Z_LVAL_P(zflags);
  1555. /* flags needs to be verified and we also need to verify whether the next
  1556. * thing we get is ';'. After that we require an 'm' or something else
  1557. * where 'm' stands for members and anything else should be an array. If
  1558. * neither 'a' or 'm' follows we have an error. */
  1559. if (*p != ';') {
  1560. goto outexcept;
  1561. }
  1562. ++p;
  1563. if (flags & SPL_ARRAY_IS_SELF) {
  1564. /* If IS_SELF is used, the flags are not followed by an array/object */
  1565. intern->ar_flags &= ~SPL_ARRAY_CLONE_MASK;
  1566. intern->ar_flags |= flags & SPL_ARRAY_CLONE_MASK;
  1567. zval_ptr_dtor(&intern->array);
  1568. ZVAL_UNDEF(&intern->array);
  1569. } else {
  1570. if (*p!='a' && *p!='O' && *p!='C' && *p!='r') {
  1571. goto outexcept;
  1572. }
  1573. array = var_tmp_var(&var_hash);
  1574. if (!php_var_unserialize(array, &p, s + buf_len, &var_hash)
  1575. || (Z_TYPE_P(array) != IS_ARRAY && Z_TYPE_P(array) != IS_OBJECT)) {
  1576. goto outexcept;
  1577. }
  1578. intern->ar_flags &= ~SPL_ARRAY_CLONE_MASK;
  1579. intern->ar_flags |= flags & SPL_ARRAY_CLONE_MASK;
  1580. if (Z_TYPE_P(array) == IS_ARRAY) {
  1581. zval_ptr_dtor(&intern->array);
  1582. ZVAL_COPY_VALUE(&intern->array, array);
  1583. ZVAL_NULL(array);
  1584. SEPARATE_ARRAY(&intern->array);
  1585. } else {
  1586. spl_array_set_array(object, intern, array, 0L, 1);
  1587. }
  1588. if (*p != ';') {
  1589. goto outexcept;
  1590. }
  1591. ++p;
  1592. }
  1593. /* members */
  1594. if (*p!= 'm' || *++p != ':') {
  1595. goto outexcept;
  1596. }
  1597. ++p;
  1598. members = var_tmp_var(&var_hash);
  1599. if (!php_var_unserialize(members, &p, s + buf_len, &var_hash) || Z_TYPE_P(members) != IS_ARRAY) {
  1600. goto outexcept;
  1601. }
  1602. /* copy members */
  1603. object_properties_load(&intern->std, Z_ARRVAL_P(members));
  1604. /* done reading $serialized */
  1605. PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
  1606. return;
  1607. outexcept:
  1608. PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
  1609. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Error at offset " ZEND_LONG_FMT " of %zd bytes", (zend_long)((char*)p - buf), buf_len);
  1610. return;
  1611. } /* }}} */
  1612. /* {{{ arginfo and function table */
  1613. ZEND_BEGIN_ARG_INFO_EX(arginfo_array___construct, 0, 0, 0)
  1614. ZEND_ARG_INFO(0, input)
  1615. ZEND_ARG_INFO(0, flags)
  1616. ZEND_ARG_INFO(0, iterator_class)
  1617. ZEND_END_ARG_INFO()
  1618. /* ArrayIterator::__construct and ArrayObject::__construct have different signatures */
  1619. ZEND_BEGIN_ARG_INFO_EX(arginfo_array_iterator___construct, 0, 0, 0)
  1620. ZEND_ARG_INFO(0, array)
  1621. ZEND_ARG_INFO(0, ar_flags)
  1622. ZEND_END_ARG_INFO()
  1623. ZEND_BEGIN_ARG_INFO_EX(arginfo_array_offsetGet, 0, 0, 1)
  1624. ZEND_ARG_INFO(0, index)
  1625. ZEND_END_ARG_INFO()
  1626. ZEND_BEGIN_ARG_INFO_EX(arginfo_array_offsetSet, 0, 0, 2)
  1627. ZEND_ARG_INFO(0, index)
  1628. ZEND_ARG_INFO(0, newval)
  1629. ZEND_END_ARG_INFO()
  1630. ZEND_BEGIN_ARG_INFO(arginfo_array_append, 0)
  1631. ZEND_ARG_INFO(0, value)
  1632. ZEND_END_ARG_INFO()
  1633. ZEND_BEGIN_ARG_INFO(arginfo_array_seek, 0)
  1634. ZEND_ARG_INFO(0, position)
  1635. ZEND_END_ARG_INFO()
  1636. ZEND_BEGIN_ARG_INFO(arginfo_array_exchangeArray, 0)
  1637. ZEND_ARG_INFO(0, array)
  1638. ZEND_END_ARG_INFO()
  1639. ZEND_BEGIN_ARG_INFO(arginfo_array_setFlags, 0)
  1640. ZEND_ARG_INFO(0, flags)
  1641. ZEND_END_ARG_INFO()
  1642. ZEND_BEGIN_ARG_INFO(arginfo_array_setIteratorClass, 0)
  1643. ZEND_ARG_INFO(0, iteratorClass)
  1644. ZEND_END_ARG_INFO()
  1645. ZEND_BEGIN_ARG_INFO(arginfo_array_uXsort, 0)
  1646. ZEND_ARG_INFO(0, cmp_function)
  1647. ZEND_END_ARG_INFO();
  1648. ZEND_BEGIN_ARG_INFO(arginfo_array_unserialize, 0)
  1649. ZEND_ARG_INFO(0, serialized)
  1650. ZEND_END_ARG_INFO();
  1651. ZEND_BEGIN_ARG_INFO(arginfo_array_void, 0)
  1652. ZEND_END_ARG_INFO()
  1653. static const zend_function_entry spl_funcs_ArrayObject[] = {
  1654. SPL_ME(Array, __construct, arginfo_array___construct, ZEND_ACC_PUBLIC)
  1655. SPL_ME(Array, offsetExists, arginfo_array_offsetGet, ZEND_ACC_PUBLIC)
  1656. SPL_ME(Array, offsetGet, arginfo_array_offsetGet, ZEND_ACC_PUBLIC)
  1657. SPL_ME(Array, offsetSet, arginfo_array_offsetSet, ZEND_ACC_PUBLIC)
  1658. SPL_ME(Array, offsetUnset, arginfo_array_offsetGet, ZEND_ACC_PUBLIC)
  1659. SPL_ME(Array, append, arginfo_array_append, ZEND_ACC_PUBLIC)
  1660. SPL_ME(Array, getArrayCopy, arginfo_array_void, ZEND_ACC_PUBLIC)
  1661. SPL_ME(Array, count, arginfo_array_void, ZEND_ACC_PUBLIC)
  1662. SPL_ME(Array, getFlags, arginfo_array_void, ZEND_ACC_PUBLIC)
  1663. SPL_ME(Array, setFlags, arginfo_array_setFlags, ZEND_ACC_PUBLIC)
  1664. SPL_ME(Array, asort, arginfo_array_void, ZEND_ACC_PUBLIC)
  1665. SPL_ME(Array, ksort, arginfo_array_void, ZEND_ACC_PUBLIC)
  1666. SPL_ME(Array, uasort, arginfo_array_uXsort, ZEND_ACC_PUBLIC)
  1667. SPL_ME(Array, uksort, arginfo_array_uXsort, ZEND_ACC_PUBLIC)
  1668. SPL_ME(Array, natsort, arginfo_array_void, ZEND_ACC_PUBLIC)
  1669. SPL_ME(Array, natcasesort, arginfo_array_void, ZEND_ACC_PUBLIC)
  1670. SPL_ME(Array, unserialize, arginfo_array_unserialize, ZEND_ACC_PUBLIC)
  1671. SPL_ME(Array, serialize, arginfo_array_void, ZEND_ACC_PUBLIC)
  1672. /* ArrayObject specific */
  1673. SPL_ME(Array, getIterator, arginfo_array_void, ZEND_ACC_PUBLIC)
  1674. SPL_ME(Array, exchangeArray, arginfo_array_exchangeArray, ZEND_ACC_PUBLIC)
  1675. SPL_ME(Array, setIteratorClass, arginfo_array_setIteratorClass, ZEND_ACC_PUBLIC)
  1676. SPL_ME(Array, getIteratorClass, arginfo_array_void, ZEND_ACC_PUBLIC)
  1677. PHP_FE_END
  1678. };
  1679. static const zend_function_entry spl_funcs_ArrayIterator[] = {
  1680. SPL_ME(ArrayIterator, __construct, arginfo_array_iterator___construct, ZEND_ACC_PUBLIC)
  1681. SPL_ME(Array, offsetExists, arginfo_array_offsetGet, ZEND_ACC_PUBLIC)
  1682. SPL_ME(Array, offsetGet, arginfo_array_offsetGet, ZEND_ACC_PUBLIC)
  1683. SPL_ME(Array, offsetSet, arginfo_array_offsetSet, ZEND_ACC_PUBLIC)
  1684. SPL_ME(Array, offsetUnset, arginfo_array_offsetGet, ZEND_ACC_PUBLIC)
  1685. SPL_ME(Array, append, arginfo_array_append, ZEND_ACC_PUBLIC)
  1686. SPL_ME(Array, getArrayCopy, arginfo_array_void, ZEND_ACC_PUBLIC)
  1687. SPL_ME(Array, count, arginfo_array_void, ZEND_ACC_PUBLIC)
  1688. SPL_ME(Array, getFlags, arginfo_array_void, ZEND_ACC_PUBLIC)
  1689. SPL_ME(Array, setFlags, arginfo_array_setFlags, ZEND_ACC_PUBLIC)
  1690. SPL_ME(Array, asort, arginfo_array_void, ZEND_ACC_PUBLIC)
  1691. SPL_ME(Array, ksort, arginfo_array_void, ZEND_ACC_PUBLIC)
  1692. SPL_ME(Array, uasort, arginfo_array_uXsort, ZEND_ACC_PUBLIC)
  1693. SPL_ME(Array, uksort, arginfo_array_uXsort, ZEND_ACC_PUBLIC)
  1694. SPL_ME(Array, natsort, arginfo_array_void, ZEND_ACC_PUBLIC)
  1695. SPL_ME(Array, natcasesort, arginfo_array_void, ZEND_ACC_PUBLIC)
  1696. SPL_ME(Array, unserialize, arginfo_array_unserialize, ZEND_ACC_PUBLIC)
  1697. SPL_ME(Array, serialize, arginfo_array_void, ZEND_ACC_PUBLIC)
  1698. /* ArrayIterator specific */
  1699. SPL_ME(Array, rewind, arginfo_array_void, ZEND_ACC_PUBLIC)
  1700. SPL_ME(Array, current, arginfo_array_void, ZEND_ACC_PUBLIC)
  1701. SPL_ME(Array, key, arginfo_array_void, ZEND_ACC_PUBLIC)
  1702. SPL_ME(Array, next, arginfo_array_void, ZEND_ACC_PUBLIC)
  1703. SPL_ME(Array, valid, arginfo_array_void, ZEND_ACC_PUBLIC)
  1704. SPL_ME(Array, seek, arginfo_array_seek, ZEND_ACC_PUBLIC)
  1705. PHP_FE_END
  1706. };
  1707. static const zend_function_entry spl_funcs_RecursiveArrayIterator[] = {
  1708. SPL_ME(Array, hasChildren, arginfo_array_void, ZEND_ACC_PUBLIC)
  1709. SPL_ME(Array, getChildren, arginfo_array_void, ZEND_ACC_PUBLIC)
  1710. PHP_FE_END
  1711. };
  1712. /* }}} */
  1713. /* {{{ PHP_MINIT_FUNCTION(spl_array) */
  1714. PHP_MINIT_FUNCTION(spl_array)
  1715. {
  1716. REGISTER_SPL_STD_CLASS_EX(ArrayObject, spl_array_object_new, spl_funcs_ArrayObject);
  1717. REGISTER_SPL_IMPLEMENTS(ArrayObject, Aggregate);
  1718. REGISTER_SPL_IMPLEMENTS(ArrayObject, ArrayAccess);
  1719. REGISTER_SPL_IMPLEMENTS(ArrayObject, Serializable);
  1720. REGISTER_SPL_IMPLEMENTS(ArrayObject, Countable);
  1721. memcpy(&spl_handler_ArrayObject, &std_object_handlers, sizeof(zend_object_handlers));
  1722. spl_handler_ArrayObject.offset = XtOffsetOf(spl_array_object, std);
  1723. spl_handler_ArrayObject.clone_obj = spl_array_object_clone;
  1724. spl_handler_ArrayObject.read_dimension = spl_array_read_dimension;
  1725. spl_handler_ArrayObject.write_dimension = spl_array_write_dimension;
  1726. spl_handler_ArrayObject.unset_dimension = spl_array_unset_dimension;
  1727. spl_handler_ArrayObject.has_dimension = spl_array_has_dimension;
  1728. spl_handler_ArrayObject.count_elements = spl_array_object_count_elements;
  1729. spl_handler_ArrayObject.get_properties = spl_array_get_properties;
  1730. spl_handler_ArrayObject.get_debug_info = spl_array_get_debug_info;
  1731. spl_handler_ArrayObject.get_gc = spl_array_get_gc;
  1732. spl_handler_ArrayObject.read_property = spl_array_read_property;
  1733. spl_handler_ArrayObject.write_property = spl_array_write_property;
  1734. spl_handler_ArrayObject.get_property_ptr_ptr = spl_array_get_property_ptr_ptr;
  1735. spl_handler_ArrayObject.has_property = spl_array_has_property;
  1736. spl_handler_ArrayObject.unset_property = spl_array_unset_property;
  1737. spl_handler_ArrayObject.compare_objects = spl_array_compare_objects;
  1738. spl_handler_ArrayObject.dtor_obj = zend_objects_destroy_object;
  1739. spl_handler_ArrayObject.free_obj = spl_array_object_free_storage;
  1740. REGISTER_SPL_STD_CLASS_EX(ArrayIterator, spl_array_object_new, spl_funcs_ArrayIterator);
  1741. REGISTER_SPL_IMPLEMENTS(ArrayIterator, Iterator);
  1742. REGISTER_SPL_IMPLEMENTS(ArrayIterator, ArrayAccess);
  1743. REGISTER_SPL_IMPLEMENTS(ArrayIterator, SeekableIterator);
  1744. REGISTER_SPL_IMPLEMENTS(ArrayIterator, Serializable);
  1745. REGISTER_SPL_IMPLEMENTS(ArrayIterator, Countable);
  1746. memcpy(&spl_handler_ArrayIterator, &spl_handler_ArrayObject, sizeof(zend_object_handlers));
  1747. spl_ce_ArrayIterator->get_iterator = spl_array_get_iterator;
  1748. REGISTER_SPL_CLASS_CONST_LONG(ArrayObject, "STD_PROP_LIST", SPL_ARRAY_STD_PROP_LIST);
  1749. REGISTER_SPL_CLASS_CONST_LONG(ArrayObject, "ARRAY_AS_PROPS", SPL_ARRAY_ARRAY_AS_PROPS);
  1750. REGISTER_SPL_CLASS_CONST_LONG(ArrayIterator, "STD_PROP_LIST", SPL_ARRAY_STD_PROP_LIST);
  1751. REGISTER_SPL_CLASS_CONST_LONG(ArrayIterator, "ARRAY_AS_PROPS", SPL_ARRAY_ARRAY_AS_PROPS);
  1752. REGISTER_SPL_SUB_CLASS_EX(RecursiveArrayIterator, ArrayIterator, spl_array_object_new, spl_funcs_RecursiveArrayIterator);
  1753. REGISTER_SPL_IMPLEMENTS(RecursiveArrayIterator, RecursiveIterator);
  1754. spl_ce_RecursiveArrayIterator->get_iterator = spl_array_get_iterator;
  1755. REGISTER_SPL_CLASS_CONST_LONG(RecursiveArrayIterator, "CHILD_ARRAYS_ONLY", SPL_ARRAY_CHILD_ARRAYS_ONLY);
  1756. return SUCCESS;
  1757. }
  1758. /* }}} */
  1759. /*
  1760. * Local variables:
  1761. * tab-width: 4
  1762. * c-basic-offset: 4
  1763. * End:
  1764. * vim600: fdm=marker
  1765. * vim: noet sw=4 ts=4
  1766. */