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: Marcus Boerger <helly@php.net>                              |
16   +----------------------------------------------------------------------+
17*/
18
19/* $Id$ */
20
21#include "zend.h"
22#include "zend_API.h"
23#include "zend_interfaces.h"
24#include "zend_exceptions.h"
25
26ZEND_API zend_class_entry *zend_ce_traversable;
27ZEND_API zend_class_entry *zend_ce_aggregate;
28ZEND_API zend_class_entry *zend_ce_iterator;
29ZEND_API zend_class_entry *zend_ce_arrayaccess;
30ZEND_API zend_class_entry *zend_ce_serializable;
31
32/* {{{ zend_call_method
33 Only returns the returned zval if retval_ptr != NULL */
34ZEND_API zval* zend_call_method(zval *object, zend_class_entry *obj_ce, zend_function **fn_proxy, const char *function_name, size_t function_name_len, zval *retval_ptr, int param_count, zval* arg1, zval* arg2)
35{
36	int result;
37	zend_fcall_info fci;
38	zval retval;
39	HashTable *function_table;
40
41	zval params[2];
42
43	if (param_count > 0) {
44		ZVAL_COPY_VALUE(&params[0], arg1);
45	}
46	if (param_count > 1) {
47		ZVAL_COPY_VALUE(&params[1], arg2);
48	}
49
50	fci.size = sizeof(fci);
51	/*fci.function_table = NULL; will be read form zend_class_entry of object if needed */
52	fci.object = (object && Z_TYPE_P(object) == IS_OBJECT) ? Z_OBJ_P(object) : NULL;
53	ZVAL_STRINGL(&fci.function_name, function_name, function_name_len);
54	fci.retval = retval_ptr ? retval_ptr : &retval;
55	fci.param_count = param_count;
56	fci.params = params;
57	fci.no_separation = 1;
58	fci.symbol_table = NULL;
59
60	if (!fn_proxy && !obj_ce) {
61		/* no interest in caching and no information already present that is
62		 * needed later inside zend_call_function. */
63		fci.function_table = !object ? EG(function_table) : NULL;
64		result = zend_call_function(&fci, NULL);
65		zval_ptr_dtor(&fci.function_name);
66	} else {
67		zend_fcall_info_cache fcic;
68
69		fcic.initialized = 1;
70		if (!obj_ce) {
71			obj_ce = object ? Z_OBJCE_P(object) : NULL;
72		}
73		if (obj_ce) {
74			function_table = &obj_ce->function_table;
75		} else {
76			function_table = EG(function_table);
77		}
78		if (!fn_proxy || !*fn_proxy) {
79			if ((fcic.function_handler = zend_hash_find_ptr(function_table, Z_STR(fci.function_name))) == NULL) {
80				/* error at c-level */
81				zend_error_noreturn(E_CORE_ERROR, "Couldn't find implementation for method %s%s%s", obj_ce ? ZSTR_VAL(obj_ce->name) : "", obj_ce ? "::" : "", function_name);
82			}
83			if (fn_proxy) {
84				*fn_proxy = fcic.function_handler;
85			}
86		} else {
87			fcic.function_handler = *fn_proxy;
88		}
89		fcic.calling_scope = obj_ce;
90		if (object) {
91			fcic.called_scope = Z_OBJCE_P(object);
92		} else {
93			zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data));
94
95			if (obj_ce &&
96			    (!called_scope ||
97			     !instanceof_function(called_scope, obj_ce))) {
98				fcic.called_scope = obj_ce;
99			} else {
100				fcic.called_scope = called_scope;
101			}
102		}
103		fcic.object = object ? Z_OBJ_P(object) : NULL;
104		result = zend_call_function(&fci, &fcic);
105		zval_ptr_dtor(&fci.function_name);
106	}
107	if (result == FAILURE) {
108		/* error at c-level */
109		if (!obj_ce) {
110			obj_ce = object ? Z_OBJCE_P(object) : NULL;
111		}
112		if (!EG(exception)) {
113			zend_error_noreturn(E_CORE_ERROR, "Couldn't execute method %s%s%s", obj_ce ? ZSTR_VAL(obj_ce->name) : "", obj_ce ? "::" : "", function_name);
114		}
115	}
116	/* copy arguments back, they might be changed by references */
117	if (param_count > 0 && Z_ISREF(params[0]) && !Z_ISREF_P(arg1)) {
118		ZVAL_COPY_VALUE(arg1, &params[0]);
119	}
120	if (param_count > 1 && Z_ISREF(params[1]) && !Z_ISREF_P(arg2)) {
121		ZVAL_COPY_VALUE(arg2, &params[1]);
122	}
123	if (!retval_ptr) {
124		zval_ptr_dtor(&retval);
125		return NULL;
126	}
127	return retval_ptr;
128}
129/* }}} */
130
131/* iterator interface, c-level functions used by engine */
132
133/* {{{ zend_user_it_new_iterator */
134ZEND_API void zend_user_it_new_iterator(zend_class_entry *ce, zval *object, zval *retval)
135{
136	zend_call_method_with_0_params(object, ce, &ce->iterator_funcs.zf_new_iterator, "getiterator", retval);
137}
138/* }}} */
139
140/* {{{ zend_user_it_invalidate_current */
141ZEND_API void zend_user_it_invalidate_current(zend_object_iterator *_iter)
142{
143	zend_user_iterator *iter = (zend_user_iterator*)_iter;
144
145	if (!Z_ISUNDEF(iter->value)) {
146		zval_ptr_dtor(&iter->value);
147		ZVAL_UNDEF(&iter->value);
148	}
149}
150/* }}} */
151
152/* {{{ zend_user_it_dtor */
153static void zend_user_it_dtor(zend_object_iterator *_iter)
154{
155	zend_user_iterator *iter = (zend_user_iterator*)_iter;
156	zval *object = &iter->it.data;
157
158	zend_user_it_invalidate_current(_iter);
159	zval_ptr_dtor(object);
160}
161/* }}} */
162
163/* {{{ zend_user_it_valid */
164ZEND_API int zend_user_it_valid(zend_object_iterator *_iter)
165{
166	if (_iter) {
167		zend_user_iterator *iter = (zend_user_iterator*)_iter;
168		zval *object = &iter->it.data;
169		zval more;
170		int result;
171
172		zend_call_method_with_0_params(object, iter->ce, &iter->ce->iterator_funcs.zf_valid, "valid", &more);
173		if (Z_TYPE(more) != IS_UNDEF) {
174			result = i_zend_is_true(&more);
175			zval_ptr_dtor(&more);
176			return result ? SUCCESS : FAILURE;
177		}
178	}
179	return FAILURE;
180}
181/* }}} */
182
183/* {{{ zend_user_it_get_current_data */
184ZEND_API zval *zend_user_it_get_current_data(zend_object_iterator *_iter)
185{
186	zend_user_iterator *iter = (zend_user_iterator*)_iter;
187	zval *object = &iter->it.data;
188
189	if (Z_ISUNDEF(iter->value)) {
190		zend_call_method_with_0_params(object, iter->ce, &iter->ce->iterator_funcs.zf_current, "current", &iter->value);
191	}
192	return &iter->value;
193}
194/* }}} */
195
196/* {{{ zend_user_it_get_current_key_default */
197#if 0
198static int zend_user_it_get_current_key_default(zend_object_iterator *_iter, char **str_key, uint *str_key_len, ulong *int_key)
199{
200	*int_key = _iter->index;
201	return HASH_KEY_IS_LONG;
202}
203#endif
204/* }}} */
205
206/* {{{ zend_user_it_get_current_key */
207ZEND_API void zend_user_it_get_current_key(zend_object_iterator *_iter, zval *key)
208{
209	zend_user_iterator *iter = (zend_user_iterator*)_iter;
210	zval *object = &iter->it.data;
211	zval retval;
212
213	zend_call_method_with_0_params(object, iter->ce, &iter->ce->iterator_funcs.zf_key, "key", &retval);
214
215	if (Z_TYPE(retval) != IS_UNDEF) {
216		ZVAL_ZVAL(key, &retval, 1, 1);
217	} else {
218		if (!EG(exception)) {
219			zend_error(E_WARNING, "Nothing returned from %s::key()", ZSTR_VAL(iter->ce->name));
220		}
221
222		ZVAL_LONG(key, 0);
223	}
224}
225/* }}} */
226
227/* {{{ zend_user_it_move_forward */
228ZEND_API void zend_user_it_move_forward(zend_object_iterator *_iter)
229{
230	zend_user_iterator *iter = (zend_user_iterator*)_iter;
231	zval *object = &iter->it.data;
232
233	zend_user_it_invalidate_current(_iter);
234	zend_call_method_with_0_params(object, iter->ce, &iter->ce->iterator_funcs.zf_next, "next", NULL);
235}
236/* }}} */
237
238/* {{{ zend_user_it_rewind */
239ZEND_API void zend_user_it_rewind(zend_object_iterator *_iter)
240{
241	zend_user_iterator *iter = (zend_user_iterator*)_iter;
242	zval *object = &iter->it.data;
243
244	zend_user_it_invalidate_current(_iter);
245	zend_call_method_with_0_params(object, iter->ce, &iter->ce->iterator_funcs.zf_rewind, "rewind", NULL);
246}
247/* }}} */
248
249zend_object_iterator_funcs zend_interface_iterator_funcs_iterator = {
250	zend_user_it_dtor,
251	zend_user_it_valid,
252	zend_user_it_get_current_data,
253	zend_user_it_get_current_key,
254	zend_user_it_move_forward,
255	zend_user_it_rewind,
256	zend_user_it_invalidate_current
257};
258
259/* {{{ zend_user_it_get_iterator */
260static zend_object_iterator *zend_user_it_get_iterator(zend_class_entry *ce, zval *object, int by_ref)
261{
262	zend_user_iterator *iterator;
263
264	if (by_ref) {
265		zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
266		return NULL;
267	}
268
269	iterator = emalloc(sizeof(zend_user_iterator));
270
271	zend_iterator_init((zend_object_iterator*)iterator);
272
273	ZVAL_COPY(&iterator->it.data, object);
274	iterator->it.funcs = ce->iterator_funcs.funcs;
275	iterator->ce = Z_OBJCE_P(object);
276	ZVAL_UNDEF(&iterator->value);
277	return (zend_object_iterator*)iterator;
278}
279/* }}} */
280
281/* {{{ zend_user_it_get_new_iterator */
282ZEND_API zend_object_iterator *zend_user_it_get_new_iterator(zend_class_entry *ce, zval *object, int by_ref)
283{
284	zval iterator;
285	zend_object_iterator *new_iterator;
286	zend_class_entry *ce_it;
287
288	zend_user_it_new_iterator(ce, object, &iterator);
289	ce_it = (Z_TYPE(iterator) == IS_OBJECT) ? Z_OBJCE(iterator) : NULL;
290
291	if (!ce_it || !ce_it->get_iterator || (ce_it->get_iterator == zend_user_it_get_new_iterator && Z_OBJ(iterator) == Z_OBJ_P(object))) {
292		if (!EG(exception)) {
293			zend_throw_exception_ex(NULL, 0, "Objects returned by %s::getIterator() must be traversable or implement interface Iterator", ce ? ZSTR_VAL(ce->name) : ZSTR_VAL(Z_OBJCE_P(object)->name));
294		}
295		zval_ptr_dtor(&iterator);
296		return NULL;
297	}
298
299	new_iterator = ce_it->get_iterator(ce_it, &iterator, by_ref);
300	zval_ptr_dtor(&iterator);
301	return new_iterator;
302}
303/* }}} */
304
305/* {{{ zend_implement_traversable */
306static int zend_implement_traversable(zend_class_entry *interface, zend_class_entry *class_type)
307{
308	/* check that class_type is traversable at c-level or implements at least one of 'aggregate' and 'Iterator' */
309	uint32_t i;
310
311	if (class_type->get_iterator || (class_type->parent && class_type->parent->get_iterator)) {
312		return SUCCESS;
313	}
314	for (i = 0; i < class_type->num_interfaces; i++) {
315		if (class_type->interfaces[i] == zend_ce_aggregate || class_type->interfaces[i] == zend_ce_iterator) {
316			return SUCCESS;
317		}
318	}
319	zend_error_noreturn(E_CORE_ERROR, "Class %s must implement interface %s as part of either %s or %s",
320		ZSTR_VAL(class_type->name),
321		ZSTR_VAL(zend_ce_traversable->name),
322		ZSTR_VAL(zend_ce_iterator->name),
323		ZSTR_VAL(zend_ce_aggregate->name));
324	return FAILURE;
325}
326/* }}} */
327
328/* {{{ zend_implement_aggregate */
329static int zend_implement_aggregate(zend_class_entry *interface, zend_class_entry *class_type)
330{
331	uint32_t i;
332	int t = -1;
333
334	if (class_type->get_iterator) {
335		if (class_type->type == ZEND_INTERNAL_CLASS) {
336			/* inheritance ensures the class has necessary userland methods */
337			return SUCCESS;
338		} else if (class_type->get_iterator != zend_user_it_get_new_iterator) {
339			/* c-level get_iterator cannot be changed (exception being only Traversable is implmented) */
340			if (class_type->num_interfaces) {
341				for (i = 0; i < class_type->num_interfaces; i++) {
342					if (class_type->interfaces[i] == zend_ce_iterator) {
343						zend_error_noreturn(E_ERROR, "Class %s cannot implement both %s and %s at the same time",
344									ZSTR_VAL(class_type->name),
345									ZSTR_VAL(interface->name),
346									ZSTR_VAL(zend_ce_iterator->name));
347						return FAILURE;
348					}
349					if (class_type->interfaces[i] == zend_ce_traversable) {
350						t = i;
351					}
352				}
353			}
354			if (t == -1) {
355				return FAILURE;
356			}
357		}
358	}
359	class_type->iterator_funcs.zf_new_iterator = NULL;
360	class_type->get_iterator = zend_user_it_get_new_iterator;
361	return SUCCESS;
362}
363/* }}} */
364
365/* {{{ zend_implement_iterator */
366static int zend_implement_iterator(zend_class_entry *interface, zend_class_entry *class_type)
367{
368	if (class_type->get_iterator && class_type->get_iterator != zend_user_it_get_iterator) {
369		if (class_type->type == ZEND_INTERNAL_CLASS) {
370			/* inheritance ensures the class has the necessary userland methods */
371			return SUCCESS;
372		} else {
373			/* c-level get_iterator cannot be changed */
374			if (class_type->get_iterator == zend_user_it_get_new_iterator) {
375				zend_error_noreturn(E_ERROR, "Class %s cannot implement both %s and %s at the same time",
376							ZSTR_VAL(class_type->name),
377							ZSTR_VAL(interface->name),
378							ZSTR_VAL(zend_ce_aggregate->name));
379			}
380			return FAILURE;
381		}
382	}
383	class_type->get_iterator = zend_user_it_get_iterator;
384	class_type->iterator_funcs.zf_valid = NULL;
385	class_type->iterator_funcs.zf_current = NULL;
386	class_type->iterator_funcs.zf_key = NULL;
387	class_type->iterator_funcs.zf_next = NULL;
388	class_type->iterator_funcs.zf_rewind = NULL;
389	if (!class_type->iterator_funcs.funcs) {
390		class_type->iterator_funcs.funcs = &zend_interface_iterator_funcs_iterator;
391	}
392	return SUCCESS;
393}
394/* }}} */
395
396/* {{{ zend_implement_arrayaccess */
397static int zend_implement_arrayaccess(zend_class_entry *interface, zend_class_entry *class_type)
398{
399#if 0
400	/* get ht from ce */
401	if (ht->read_dimension != zend_std_read_dimension
402	||  ht->write_dimension != zend_std_write_dimension
403	||  ht->has_dimension != zend_std_has_dimension
404	||  ht->unset_dimension != zend_std_unset_dimension) {
405		return FAILURE;
406	}
407#endif
408	return SUCCESS;
409}
410/* }}}*/
411
412/* {{{ zend_user_serialize */
413ZEND_API int zend_user_serialize(zval *object, unsigned char **buffer, size_t *buf_len, zend_serialize_data *data)
414{
415	zend_class_entry * ce = Z_OBJCE_P(object);
416	zval retval;
417	int result;
418
419	zend_call_method_with_0_params(object, ce, &ce->serialize_func, "serialize", &retval);
420
421
422	if (Z_TYPE(retval) == IS_UNDEF || EG(exception)) {
423		result = FAILURE;
424	} else {
425		switch(Z_TYPE(retval)) {
426		case IS_NULL:
427			/* we could also make this '*buf_len = 0' but this allows to skip variables */
428			zval_ptr_dtor(&retval);
429			return FAILURE;
430		case IS_STRING:
431			*buffer = (unsigned char*)estrndup(Z_STRVAL(retval), Z_STRLEN(retval));
432			*buf_len = Z_STRLEN(retval);
433			result = SUCCESS;
434			break;
435		default: /* failure */
436			result = FAILURE;
437			break;
438		}
439		zval_ptr_dtor(&retval);
440	}
441
442	if (result == FAILURE && !EG(exception)) {
443		zend_throw_exception_ex(NULL, 0, "%s::serialize() must return a string or NULL", ZSTR_VAL(ce->name));
444	}
445	return result;
446}
447/* }}} */
448
449/* {{{ zend_user_unserialize */
450ZEND_API int zend_user_unserialize(zval *object, zend_class_entry *ce, const unsigned char *buf, size_t buf_len, zend_unserialize_data *data)
451{
452	zval zdata;
453
454	if (UNEXPECTED(object_init_ex(object, ce) != SUCCESS)) {
455		return FAILURE;
456	}
457
458	ZVAL_STRINGL(&zdata, (char*)buf, buf_len);
459
460	zend_call_method_with_1_params(object, ce, &ce->unserialize_func, "unserialize", NULL, &zdata);
461
462	zval_ptr_dtor(&zdata);
463
464	if (EG(exception)) {
465		return FAILURE;
466	} else {
467		return SUCCESS;
468	}
469}
470/* }}} */
471
472ZEND_API int zend_class_serialize_deny(zval *object, unsigned char **buffer, size_t *buf_len, zend_serialize_data *data) /* {{{ */
473{
474	zend_class_entry *ce = Z_OBJCE_P(object);
475	zend_throw_exception_ex(NULL, 0, "Serialization of '%s' is not allowed", ZSTR_VAL(ce->name));
476	return FAILURE;
477}
478/* }}} */
479
480ZEND_API int zend_class_unserialize_deny(zval *object, zend_class_entry *ce, const unsigned char *buf, size_t buf_len, zend_unserialize_data *data) /* {{{ */
481{
482	zend_throw_exception_ex(NULL, 0, "Unserialization of '%s' is not allowed", ZSTR_VAL(ce->name));
483	return FAILURE;
484}
485/* }}} */
486
487/* {{{ zend_implement_serializable */
488static int zend_implement_serializable(zend_class_entry *interface, zend_class_entry *class_type)
489{
490	if (class_type->parent
491		&& (class_type->parent->serialize || class_type->parent->unserialize)
492		&& !instanceof_function_ex(class_type->parent, zend_ce_serializable, 1)) {
493		return FAILURE;
494	}
495	if (!class_type->serialize) {
496		class_type->serialize = zend_user_serialize;
497	}
498	if (!class_type->unserialize) {
499		class_type->unserialize = zend_user_unserialize;
500	}
501	return SUCCESS;
502}
503/* }}}*/
504
505/* {{{ function tables */
506const zend_function_entry zend_funcs_aggregate[] = {
507	ZEND_ABSTRACT_ME(iterator, getIterator, NULL)
508	ZEND_FE_END
509};
510
511const zend_function_entry zend_funcs_iterator[] = {
512	ZEND_ABSTRACT_ME(iterator, current,  NULL)
513	ZEND_ABSTRACT_ME(iterator, next,     NULL)
514	ZEND_ABSTRACT_ME(iterator, key,      NULL)
515	ZEND_ABSTRACT_ME(iterator, valid,    NULL)
516	ZEND_ABSTRACT_ME(iterator, rewind,   NULL)
517	ZEND_FE_END
518};
519
520const zend_function_entry *zend_funcs_traversable    = NULL;
521
522ZEND_BEGIN_ARG_INFO_EX(arginfo_arrayaccess_offset, 0, 0, 1)
523	ZEND_ARG_INFO(0, offset)
524ZEND_END_ARG_INFO()
525
526ZEND_BEGIN_ARG_INFO_EX(arginfo_arrayaccess_offset_get, 0, 0, 1) /* actually this should be return by ref but atm cannot be */
527	ZEND_ARG_INFO(0, offset)
528ZEND_END_ARG_INFO()
529
530ZEND_BEGIN_ARG_INFO_EX(arginfo_arrayaccess_offset_value, 0, 0, 2)
531	ZEND_ARG_INFO(0, offset)
532	ZEND_ARG_INFO(0, value)
533ZEND_END_ARG_INFO()
534
535const zend_function_entry zend_funcs_arrayaccess[] = {
536	ZEND_ABSTRACT_ME(arrayaccess, offsetExists, arginfo_arrayaccess_offset)
537	ZEND_ABSTRACT_ME(arrayaccess, offsetGet,    arginfo_arrayaccess_offset_get)
538	ZEND_ABSTRACT_ME(arrayaccess, offsetSet,    arginfo_arrayaccess_offset_value)
539	ZEND_ABSTRACT_ME(arrayaccess, offsetUnset,  arginfo_arrayaccess_offset)
540	ZEND_FE_END
541};
542
543ZEND_BEGIN_ARG_INFO(arginfo_serializable_serialize, 0)
544	ZEND_ARG_INFO(0, serialized)
545ZEND_END_ARG_INFO()
546
547const zend_function_entry zend_funcs_serializable[] = {
548	ZEND_ABSTRACT_ME(serializable, serialize,   NULL)
549	ZEND_FENTRY(unserialize, NULL, arginfo_serializable_serialize, ZEND_ACC_PUBLIC|ZEND_ACC_ABSTRACT|ZEND_ACC_CTOR)
550	ZEND_FE_END
551};
552/* }}} */
553
554/* {{{ zend_register_interfaces */
555ZEND_API void zend_register_interfaces(void)
556{
557	REGISTER_MAGIC_INTERFACE(traversable, Traversable);
558
559	REGISTER_MAGIC_INTERFACE(aggregate, IteratorAggregate);
560	REGISTER_MAGIC_IMPLEMENT(aggregate, traversable);
561
562	REGISTER_MAGIC_INTERFACE(iterator, Iterator);
563	REGISTER_MAGIC_IMPLEMENT(iterator, traversable);
564
565	REGISTER_MAGIC_INTERFACE(arrayaccess, ArrayAccess);
566
567	REGISTER_MAGIC_INTERFACE(serializable, Serializable);
568}
569/* }}} */
570
571/*
572 * Local variables:
573 * tab-width: 4
574 * c-basic-offset: 4
575 * indent-tabs-mode: t
576 * End:
577 */
578