xref: /php-src/ext/dom/nodelist.c (revision 47feb579)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Christian Stocker <chregu@php.net>                          |
14    |          Rob Richards <rrichards@php.net>                            |
15    +----------------------------------------------------------------------+
16 */
17 
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21 
22 #include "php.h"
23 #if defined(HAVE_LIBXML) && defined(HAVE_DOM)
24 #include "php_dom.h"
25 #include "nodelist.h"
26 #include "zend_interfaces.h"
27 
28 /*
29 * class DOMNodeList
30 *
31 * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-536297177
32 * Since:
33 */
34 
objmap_cache_release_cached_obj(dom_nnodemap_object * objmap)35 static zend_always_inline void objmap_cache_release_cached_obj(dom_nnodemap_object *objmap)
36 {
37 	if (objmap->cached_obj) {
38 		/* Since the DOM is a tree there can be no cycles. */
39 		if (GC_DELREF(&objmap->cached_obj->std) == 0) {
40 			zend_objects_store_del(&objmap->cached_obj->std);
41 		}
42 		objmap->cached_obj = NULL;
43 		objmap->cached_obj_index = 0;
44 	}
45 }
46 
reset_objmap_cache(dom_nnodemap_object * objmap)47 static zend_always_inline void reset_objmap_cache(dom_nnodemap_object *objmap)
48 {
49 	objmap_cache_release_cached_obj(objmap);
50 	objmap->cached_length = -1;
51 }
52 
php_dom_get_nodelist_length(dom_object * obj)53 int php_dom_get_nodelist_length(dom_object *obj)
54 {
55 	dom_nnodemap_object *objmap = (dom_nnodemap_object *) obj->ptr;
56 	if (!objmap) {
57 		return 0;
58 	}
59 
60 	if (objmap->ht) {
61 		return xmlHashSize(objmap->ht);
62 	}
63 
64 	if (objmap->nodetype == DOM_NODESET) {
65 		HashTable *nodeht = HASH_OF(&objmap->baseobj_zv);
66 		return zend_hash_num_elements(nodeht);
67 	}
68 
69 	xmlNodePtr nodep = dom_object_get_node(objmap->baseobj);
70 	if (!nodep) {
71 		return 0;
72 	}
73 
74 	if (!php_dom_is_cache_tag_stale_from_node(&objmap->cache_tag, nodep)) {
75 		if (objmap->cached_length >= 0) {
76 			return objmap->cached_length;
77 		}
78 		/* Only the length is out-of-date, the cache tag is still valid.
79 		 * Therefore, only overwrite the length and keep the currently cached object. */
80 	} else {
81 		php_dom_mark_cache_tag_up_to_date_from_node(&objmap->cache_tag, nodep);
82 		reset_objmap_cache(objmap);
83 	}
84 
85 	int count = 0;
86 	if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
87 		xmlNodePtr curnode = nodep->children;
88 		if (curnode) {
89 			count++;
90 			while (curnode->next != NULL) {
91 				count++;
92 				curnode = curnode->next;
93 			}
94 		}
95 	} else {
96 		xmlNodePtr basep = nodep;
97 		nodep = php_dom_first_child_of_container_node(basep);
98 		dom_get_elements_by_tag_name_ns_raw(
99 			basep, nodep, objmap->ns, objmap->local, objmap->local_lower, &count, INT_MAX - 1 /* because of <= */);
100 	}
101 
102 	objmap->cached_length = count;
103 
104 	return count;
105 }
106 
107 /* {{{ length	int
108 readonly=yes
109 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-203510337
110 Since:
111 */
dom_nodelist_length_read(dom_object * obj,zval * retval)112 zend_result dom_nodelist_length_read(dom_object *obj, zval *retval)
113 {
114 	ZVAL_LONG(retval, php_dom_get_nodelist_length(obj));
115 	return SUCCESS;
116 }
117 
118 
119 /* {{{ */
PHP_METHOD(DOMNodeList,count)120 PHP_METHOD(DOMNodeList, count)
121 {
122 	zval *id;
123 	dom_object *intern;
124 
125 	id = ZEND_THIS;
126 	if (zend_parse_parameters_none() == FAILURE) {
127 		RETURN_THROWS();
128 	}
129 
130 	intern = Z_DOMOBJ_P(id);
131 	RETURN_LONG(php_dom_get_nodelist_length(intern));
132 }
133 /* }}} end dom_nodelist_count */
134 
php_dom_nodelist_get_item_into_zval(dom_nnodemap_object * objmap,zend_long index,zval * return_value)135 void php_dom_nodelist_get_item_into_zval(dom_nnodemap_object *objmap, zend_long index, zval *return_value)
136 {
137 	xmlNodePtr itemnode = NULL;
138 	bool cache_itemnode = false;
139 	if (index >= 0) {
140 		if (objmap != NULL) {
141 			if (objmap->ht) {
142 				itemnode = php_dom_libxml_hash_iter(objmap, index);
143 			} else {
144 				if (objmap->nodetype == DOM_NODESET) {
145 					HashTable *nodeht = HASH_OF(&objmap->baseobj_zv);
146 					zval *entry = zend_hash_index_find(nodeht, index);
147 					if (entry) {
148 						ZVAL_COPY(return_value, entry);
149 						return;
150 					}
151 				} else if (objmap->baseobj) {
152 					xmlNodePtr basep = dom_object_get_node(objmap->baseobj);
153 					if (basep) {
154 						xmlNodePtr nodep = basep;
155 						/* For now we're only able to use cache for forward search.
156 						 * TODO: in the future we could extend the logic of the node list such that backwards searches
157 						 *       are also possible. */
158 						bool restart = true;
159 						int relative_index = index;
160 						if (index >= objmap->cached_obj_index && objmap->cached_obj && !php_dom_is_cache_tag_stale_from_node(&objmap->cache_tag, nodep)) {
161 							xmlNodePtr cached_obj_xml_node = dom_object_get_node(objmap->cached_obj);
162 
163 							/* The node cannot be NULL if the cache is valid. If it is NULL, then it means we
164 							 * forgot an invalidation somewhere. Take the defensive programming approach and invalidate
165 							 * it here if it's NULL (except in debug mode where we would want to catch this). */
166 							if (UNEXPECTED(cached_obj_xml_node == NULL)) {
167 #if ZEND_DEBUG
168 								ZEND_UNREACHABLE();
169 #endif
170 								reset_objmap_cache(objmap);
171 							} else {
172 								restart = false;
173 								relative_index -= objmap->cached_obj_index;
174 								nodep = cached_obj_xml_node;
175 							}
176 						}
177 						int count = 0;
178 						if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
179 							if (restart) {
180 								nodep = nodep->children;
181 							}
182 							while (count < relative_index && nodep != NULL) {
183 								count++;
184 								nodep = nodep->next;
185 							}
186 							itemnode = nodep;
187 						} else {
188 							if (restart) {
189 								nodep = php_dom_first_child_of_container_node(basep);
190 							}
191 							itemnode = dom_get_elements_by_tag_name_ns_raw(basep, nodep, objmap->ns, objmap->local, objmap->local_lower, &count, relative_index);
192 						}
193 						cache_itemnode = true;
194 					}
195 				}
196 			}
197 		}
198 
199 		if (itemnode) {
200 			DOM_RET_OBJ(itemnode, objmap->baseobj);
201 			if (cache_itemnode) {
202 				/* Hold additional reference for the cache, must happen before releasing the cache
203 				 * because we might be the last reference holder.
204 				 * Instead of storing and copying zvals, we store the object pointer directly.
205 				 * This saves us some bytes because a pointer is smaller than a zval.
206 				 * This also means we have to manually refcount the objects here, and remove the reference count
207 				 * in reset_objmap_cache() and the destructor. */
208 				dom_object *cached_obj = Z_DOMOBJ_P(return_value);
209 				GC_ADDREF(&cached_obj->std);
210 				/* If the tag is stale, all cached data is useless. Otherwise only the cached object is useless. */
211 				if (php_dom_is_cache_tag_stale_from_node(&objmap->cache_tag, itemnode)) {
212 					php_dom_mark_cache_tag_up_to_date_from_node(&objmap->cache_tag, itemnode);
213 					reset_objmap_cache(objmap);
214 				} else {
215 					objmap_cache_release_cached_obj(objmap);
216 				}
217 				objmap->cached_obj_index = index;
218 				objmap->cached_obj = cached_obj;
219 			}
220 			return;
221 		}
222 	}
223 
224 	RETVAL_NULL();
225 }
226 
227 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-844377136
228 Since:
229 */
PHP_METHOD(DOMNodeList,item)230 PHP_METHOD(DOMNodeList, item)
231 {
232 	zend_long index;
233 	ZEND_PARSE_PARAMETERS_START(1, 1)
234 		Z_PARAM_LONG(index)
235 	ZEND_PARSE_PARAMETERS_END();
236 
237 	zval *id = ZEND_THIS;
238 	dom_object *intern = Z_DOMOBJ_P(id);
239 	dom_nnodemap_object *objmap = intern->ptr;
240 	php_dom_nodelist_get_item_into_zval(objmap, index, return_value);
241 }
242 /* }}} end dom_nodelist_item */
243 
ZEND_METHOD(DOMNodeList,getIterator)244 ZEND_METHOD(DOMNodeList, getIterator)
245 {
246 	if (zend_parse_parameters_none() == FAILURE) {
247 		return;
248 	}
249 
250 	zend_create_internal_iterator_zval(return_value, ZEND_THIS);
251 }
252 
dom_modern_nodelist_get_index(const zval * offset)253 dom_nodelist_dimension_index dom_modern_nodelist_get_index(const zval *offset)
254 {
255 	dom_nodelist_dimension_index ret;
256 
257 	ZVAL_DEREF(offset);
258 
259 	if (Z_TYPE_P(offset) == IS_LONG) {
260 		ret.type = DOM_NODELIST_DIM_LONG;
261 		ret.lval = Z_LVAL_P(offset);
262 	} else if (Z_TYPE_P(offset) == IS_DOUBLE) {
263 		ret.type = DOM_NODELIST_DIM_LONG;
264 		ret.lval = zend_dval_to_lval_safe(Z_DVAL_P(offset));
265 	} else if (Z_TYPE_P(offset) == IS_STRING) {
266 		zend_ulong lval;
267 		if (ZEND_HANDLE_NUMERIC(Z_STR_P(offset), lval)) {
268 			ret.type = DOM_NODELIST_DIM_LONG;
269 			ret.lval = (zend_long) lval;
270 		} else {
271 			ret.type = DOM_NODELIST_DIM_STRING;
272 			ret.str = Z_STR_P(offset);
273 		}
274 	} else {
275 		ret.type = DOM_NODELIST_DIM_ILLEGAL;
276 	}
277 
278 	return ret;
279 }
280 
dom_modern_nodelist_read_dimension(zend_object * object,zval * offset,int type,zval * rv)281 zval *dom_modern_nodelist_read_dimension(zend_object *object, zval *offset, int type, zval *rv)
282 {
283 	if (UNEXPECTED(!offset)) {
284 		zend_throw_error(NULL, "Cannot append to %s", ZSTR_VAL(object->ce->name));
285 		return NULL;
286 	}
287 
288 	dom_nodelist_dimension_index index = dom_modern_nodelist_get_index(offset);
289 	if (UNEXPECTED(index.type == DOM_NODELIST_DIM_ILLEGAL || index.type == DOM_NODELIST_DIM_STRING)) {
290 		zend_illegal_container_offset(object->ce->name, offset, type);
291 		return NULL;
292 	}
293 
294 	php_dom_nodelist_get_item_into_zval(php_dom_obj_from_obj(object)->ptr, index.lval, rv);
295 	return rv;
296 }
297 
dom_modern_nodelist_has_dimension(zend_object * object,zval * member,int check_empty)298 int dom_modern_nodelist_has_dimension(zend_object *object, zval *member, int check_empty)
299 {
300 	/* If it exists, it cannot be empty because nodes aren't empty. */
301 	ZEND_IGNORE_VALUE(check_empty);
302 
303 	dom_nodelist_dimension_index index = dom_modern_nodelist_get_index(member);
304 	if (UNEXPECTED(index.type == DOM_NODELIST_DIM_ILLEGAL || index.type == DOM_NODELIST_DIM_STRING)) {
305 		zend_illegal_container_offset(object->ce->name, member, BP_VAR_IS);
306 		return 0;
307 	}
308 
309 	return index.lval >= 0 && index.lval < php_dom_get_nodelist_length(php_dom_obj_from_obj(object));
310 }
311 
312 #endif
313