xref: /PHP-7.2/ext/com_dotnet/com_iterator.c (revision 60a69dae)
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    | Author: Wez Furlong <wez@thebrainroom.com>                           |
16    +----------------------------------------------------------------------+
17  */
18 
19 /* $Id$ */
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include "php.h"
26 #include "php_ini.h"
27 #include "ext/standard/info.h"
28 #include "php_com_dotnet.h"
29 #include "php_com_dotnet_internal.h"
30 #include "Zend/zend_exceptions.h"
31 
32 struct php_com_iterator {
33 	zend_object_iterator iter;
34 	IEnumVARIANT *ev;
35 	zend_ulong key;
36 	VARIANT v; /* cached element */
37 	int code_page;
38 	VARIANT safe_array;
39 	VARTYPE sa_type;
40 	LONG sa_max;
41 	zval zdata;
42 };
43 
com_iter_dtor(zend_object_iterator * iter)44 static void com_iter_dtor(zend_object_iterator *iter)
45 {
46 	struct php_com_iterator *I = (struct php_com_iterator*)Z_PTR(iter->data);
47 
48 	if (I->ev) {
49 		IEnumVARIANT_Release(I->ev);
50 	}
51 	VariantClear(&I->v);
52 	VariantClear(&I->safe_array);
53 	zval_ptr_dtor(&I->zdata);
54 }
55 
com_iter_valid(zend_object_iterator * iter)56 static int com_iter_valid(zend_object_iterator *iter)
57 {
58 	struct php_com_iterator *I = (struct php_com_iterator*)Z_PTR(iter->data);
59 
60 	if (Z_TYPE(I->zdata) != IS_UNDEF) {
61 		return SUCCESS;
62 	}
63 
64 	return FAILURE;
65 }
66 
com_iter_get_data(zend_object_iterator * iter)67 static zval* com_iter_get_data(zend_object_iterator *iter)
68 {
69 	struct php_com_iterator *I = (struct php_com_iterator*)Z_PTR(iter->data);
70 
71 	return &I->zdata;
72 }
73 
com_iter_get_key(zend_object_iterator * iter,zval * key)74 static void com_iter_get_key(zend_object_iterator *iter, zval *key)
75 {
76 	struct php_com_iterator *I = (struct php_com_iterator*)Z_PTR(iter->data);
77 
78 	if (I->key == (zend_ulong)-1) {
79 		ZVAL_NULL(key);
80 	} else {
81 		ZVAL_LONG(key, I->key);
82 	}
83 }
84 
com_iter_move_forwards(zend_object_iterator * iter)85 static void com_iter_move_forwards(zend_object_iterator *iter)
86 {
87 	struct php_com_iterator *I = (struct php_com_iterator*)Z_PTR(iter->data);
88 	unsigned long n_fetched;
89 	zval ptr;
90 
91 	/* release current cached element */
92 	VariantClear(&I->v);
93 
94 	if (Z_TYPE(I->zdata) != IS_UNDEF) {
95 		zval_ptr_dtor(&I->zdata);
96 		ZVAL_UNDEF(&I->zdata);
97 	}
98 
99 	if (I->ev) {
100 		/* Get the next element */
101 		if (SUCCEEDED(IEnumVARIANT_Next(I->ev, 1, &I->v, &n_fetched)) && n_fetched > 0) {
102 			I->key++;
103 		} else {
104 			/* indicate that there are no more items */
105 			I->key = (ulong)-1;
106 			return;
107 		}
108 	} else {
109 		/* safe array */
110 		if (I->key >= (ULONG) I->sa_max) {
111 			I->key = (ulong)-1;
112 			return;
113 		}
114 		I->key++;
115 		if (php_com_safearray_get_elem(&I->safe_array, &I->v, (LONG)I->key) == 0) {
116 			I->key = (ulong)-1;
117 			return;
118 		}
119 	}
120 
121 	ZVAL_NULL(&ptr);
122 	php_com_zval_from_variant(&ptr, &I->v, I->code_page);
123 	/* php_com_wrap_variant(ptr, &I->v, I->code_page); */
124 	ZVAL_COPY_VALUE(&I->zdata, &ptr);
125 }
126 
127 
128 static zend_object_iterator_funcs com_iter_funcs = {
129 	com_iter_dtor,
130 	com_iter_valid,
131 	com_iter_get_data,
132 	com_iter_get_key,
133 	com_iter_move_forwards,
134 	NULL
135 };
136 
php_com_iter_get(zend_class_entry * ce,zval * object,int by_ref)137 zend_object_iterator *php_com_iter_get(zend_class_entry *ce, zval *object, int by_ref)
138 {
139 	php_com_dotnet_object *obj;
140 	struct php_com_iterator *I;
141 	IEnumVARIANT *iev = NULL;
142 	DISPPARAMS dp;
143 	VARIANT v;
144 	unsigned long n_fetched;
145 	zval ptr;
146 
147 	if (by_ref) {
148 		zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
149 	}
150 
151 	obj = CDNO_FETCH(object);
152 
153 	if (V_VT(&obj->v) != VT_DISPATCH && !V_ISARRAY(&obj->v)) {
154 		php_error_docref(NULL, E_WARNING, "variant is not an object or array VT=%d", V_VT(&obj->v));
155 		return NULL;
156 	}
157 
158 	memset(&dp, 0, sizeof(dp));
159 	VariantInit(&v);
160 
161 	I = (struct php_com_iterator*)ecalloc(1, sizeof(*I));
162 	zend_iterator_init(&I->iter);
163 	I->iter.funcs = &com_iter_funcs;
164 	Z_PTR(I->iter.data) = I;
165 	I->code_page = obj->code_page;
166 	ZVAL_UNDEF(&I->zdata);
167 	VariantInit(&I->safe_array);
168 	VariantInit(&I->v);
169 
170 	if (V_ISARRAY(&obj->v)) {
171 		LONG bound;
172 		UINT dims;
173 
174 		dims = SafeArrayGetDim(V_ARRAY(&obj->v));
175 
176 		if (dims != 1) {
177 			php_error_docref(NULL, E_WARNING,
178 				   "Can only handle single dimension variant arrays (this array has %d)", dims);
179 			goto fail;
180 		}
181 
182 		/* same semantics as foreach on a PHP array;
183 		 * make a copy and enumerate that copy */
184 		VariantCopy(&I->safe_array, &obj->v);
185 
186 		/* determine the key value for the array */
187 		SafeArrayGetLBound(V_ARRAY(&I->safe_array), 1, &bound);
188 		SafeArrayGetUBound(V_ARRAY(&I->safe_array), 1, &I->sa_max);
189 
190 		/* pre-fetch the element */
191 		if (php_com_safearray_get_elem(&I->safe_array, &I->v, bound)) {
192 			I->key = bound;
193 			ZVAL_NULL(&ptr);
194 			php_com_zval_from_variant(&ptr, &I->v, I->code_page);
195 			ZVAL_COPY_VALUE(&I->zdata, &ptr);
196 		} else {
197 			I->key = (ulong)-1;
198 		}
199 
200 	} else {
201 		/* can we enumerate it? */
202 		if (FAILED(IDispatch_Invoke(V_DISPATCH(&obj->v), DISPID_NEWENUM,
203 						&IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD|DISPATCH_PROPERTYGET,
204 						&dp, &v, NULL, NULL))) {
205 			goto fail;
206 		}
207 
208 		/* get something useful out of it */
209 		if (V_VT(&v) == VT_UNKNOWN) {
210 			IUnknown_QueryInterface(V_UNKNOWN(&v), &IID_IEnumVARIANT, (void**)&iev);
211 		} else if (V_VT(&v) == VT_DISPATCH) {
212 			IDispatch_QueryInterface(V_DISPATCH(&v), &IID_IEnumVARIANT, (void**)&iev);
213 		}
214 
215 		VariantClear(&v);
216 
217 		if (iev == NULL) {
218 			goto fail;
219 		}
220 
221 		I->ev = iev;
222 
223 		/* Get the first element now */
224 		if (SUCCEEDED(IEnumVARIANT_Next(I->ev, 1, &I->v, &n_fetched)) && n_fetched > 0) {
225 			/* indicate that we have element 0 */
226 			I->key = 0;
227 			ZVAL_NULL(&ptr);
228 			php_com_zval_from_variant(&ptr, &I->v, I->code_page);
229 			ZVAL_COPY_VALUE(&I->zdata, &ptr);
230 		} else {
231 			/* indicate that there are no more items */
232 			I->key = (ulong)-1;
233 		}
234 	}
235 
236 	return &I->iter;
237 
238 fail:
239 	if (I) {
240 		VariantClear(&I->safe_array);
241 		VariantClear(&I->v);
242 		efree(I);
243 	}
244 	return NULL;
245 }
246