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