xref: /PHP-8.4/ext/dom/nodelist.c (revision 9cb23a3d)
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 
dom_nodelist_iter_start_first_child(xmlNodePtr nodep)53 xmlNodePtr dom_nodelist_iter_start_first_child(xmlNodePtr nodep)
54 {
55 	if (nodep->type == XML_ENTITY_REF_NODE) {
56 		/* See entityreference.c */
57 		dom_entity_reference_fetch_and_sync_declaration(nodep);
58 	}
59 
60 	return nodep->children;
61 }
62 
php_dom_get_nodelist_length(dom_object * obj)63 zend_long php_dom_get_nodelist_length(dom_object *obj)
64 {
65 	dom_nnodemap_object *objmap = (dom_nnodemap_object *) obj->ptr;
66 	if (!objmap) {
67 		return 0;
68 	}
69 
70 	if (objmap->ht) {
71 		return xmlHashSize(objmap->ht);
72 	}
73 
74 	if (objmap->nodetype == DOM_NODESET) {
75 		HashTable *nodeht = HASH_OF(&objmap->baseobj_zv);
76 		return zend_hash_num_elements(nodeht);
77 	}
78 
79 	xmlNodePtr nodep = dom_object_get_node(objmap->baseobj);
80 	if (!nodep) {
81 		return 0;
82 	}
83 
84 	if (!php_dom_is_cache_tag_stale_from_node(&objmap->cache_tag, nodep)) {
85 		if (objmap->cached_length >= 0) {
86 			return objmap->cached_length;
87 		}
88 		/* Only the length is out-of-date, the cache tag is still valid.
89 		 * Therefore, only overwrite the length and keep the currently cached object. */
90 	} else {
91 		php_dom_mark_cache_tag_up_to_date_from_node(&objmap->cache_tag, nodep);
92 		reset_objmap_cache(objmap);
93 	}
94 
95 	zend_long count = 0;
96 	if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
97 		xmlNodePtr curnode = dom_nodelist_iter_start_first_child(nodep);
98 		if (curnode) {
99 			count++;
100 			while (curnode->next != NULL) {
101 				count++;
102 				curnode = curnode->next;
103 			}
104 		}
105 	} else {
106 		xmlNodePtr basep = nodep;
107 		nodep = php_dom_first_child_of_container_node(basep);
108 		dom_get_elements_by_tag_name_ns_raw(
109 			basep, nodep, objmap->ns, objmap->local, objmap->local_lower, &count, ZEND_LONG_MAX - 1 /* because of <= */);
110 	}
111 
112 	objmap->cached_length = count;
113 
114 	return count;
115 }
116 
117 /* {{{ length	int
118 readonly=yes
119 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-203510337
120 Since:
121 */
dom_nodelist_length_read(dom_object * obj,zval * retval)122 zend_result dom_nodelist_length_read(dom_object *obj, zval *retval)
123 {
124 	ZVAL_LONG(retval, php_dom_get_nodelist_length(obj));
125 	return SUCCESS;
126 }
127 /* }}} */
128 
129 /* {{{ */
PHP_METHOD(DOMNodeList,count)130 PHP_METHOD(DOMNodeList, count)
131 {
132 	ZEND_PARSE_PARAMETERS_NONE();
133 	dom_object *intern = Z_DOMOBJ_P(ZEND_THIS);
134 	RETURN_LONG(php_dom_get_nodelist_length(intern));
135 }
136 /* }}} end dom_nodelist_count */
137 
php_dom_nodelist_get_item_into_zval(dom_nnodemap_object * objmap,zend_long index,zval * return_value)138 void php_dom_nodelist_get_item_into_zval(dom_nnodemap_object *objmap, zend_long index, zval *return_value)
139 {
140 	xmlNodePtr itemnode = NULL;
141 	bool cache_itemnode = false;
142 	if (index >= 0) {
143 		if (objmap != NULL) {
144 			if (objmap->ht) {
145 				itemnode = php_dom_libxml_hash_iter(objmap, index);
146 			} else {
147 				if (objmap->nodetype == DOM_NODESET) {
148 					HashTable *nodeht = HASH_OF(&objmap->baseobj_zv);
149 					zval *entry = zend_hash_index_find(nodeht, index);
150 					if (entry) {
151 						ZVAL_COPY(return_value, entry);
152 						return;
153 					}
154 				} else if (objmap->baseobj) {
155 					xmlNodePtr basep = dom_object_get_node(objmap->baseobj);
156 					if (basep) {
157 						xmlNodePtr nodep = basep;
158 						/* For now we're only able to use cache for forward search.
159 						 * TODO: in the future we could extend the logic of the node list such that backwards searches
160 						 *       are also possible. */
161 						bool restart = true;
162 						zend_long relative_index = index;
163 						if (index >= objmap->cached_obj_index && objmap->cached_obj && !php_dom_is_cache_tag_stale_from_node(&objmap->cache_tag, nodep)) {
164 							xmlNodePtr cached_obj_xml_node = dom_object_get_node(objmap->cached_obj);
165 
166 							/* The node cannot be NULL if the cache is valid. If it is NULL, then it means we
167 							 * forgot an invalidation somewhere. Take the defensive programming approach and invalidate
168 							 * it here if it's NULL (except in debug mode where we would want to catch this). */
169 							if (UNEXPECTED(cached_obj_xml_node == NULL)) {
170 #if ZEND_DEBUG
171 								ZEND_UNREACHABLE();
172 #endif
173 								reset_objmap_cache(objmap);
174 							} else {
175 								restart = false;
176 								relative_index -= objmap->cached_obj_index;
177 								nodep = cached_obj_xml_node;
178 							}
179 						}
180 						zend_long count = 0;
181 						if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
182 							if (restart) {
183 								nodep = dom_nodelist_iter_start_first_child(nodep);
184 							}
185 							while (count < relative_index && nodep != NULL) {
186 								count++;
187 								nodep = nodep->next;
188 							}
189 							itemnode = nodep;
190 						} else {
191 							if (restart) {
192 								nodep = php_dom_first_child_of_container_node(basep);
193 							}
194 							itemnode = dom_get_elements_by_tag_name_ns_raw(basep, nodep, objmap->ns, objmap->local, objmap->local_lower, &count, relative_index);
195 						}
196 						cache_itemnode = true;
197 					}
198 				}
199 			}
200 		}
201 
202 		if (itemnode) {
203 			DOM_RET_OBJ(itemnode, objmap->baseobj);
204 			if (cache_itemnode) {
205 				/* Hold additional reference for the cache, must happen before releasing the cache
206 				 * because we might be the last reference holder.
207 				 * Instead of storing and copying zvals, we store the object pointer directly.
208 				 * This saves us some bytes because a pointer is smaller than a zval.
209 				 * This also means we have to manually refcount the objects here, and remove the reference count
210 				 * in reset_objmap_cache() and the destructor. */
211 				dom_object *cached_obj = Z_DOMOBJ_P(return_value);
212 				GC_ADDREF(&cached_obj->std);
213 				/* If the tag is stale, all cached data is useless. Otherwise only the cached object is useless. */
214 				if (php_dom_is_cache_tag_stale_from_node(&objmap->cache_tag, itemnode)) {
215 					php_dom_mark_cache_tag_up_to_date_from_node(&objmap->cache_tag, itemnode);
216 					reset_objmap_cache(objmap);
217 				} else {
218 					objmap_cache_release_cached_obj(objmap);
219 				}
220 				objmap->cached_obj_index = index;
221 				objmap->cached_obj = cached_obj;
222 			}
223 			return;
224 		}
225 	}
226 
227 	RETVAL_NULL();
228 }
229 
230 /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-844377136
231 Since:
232 */
PHP_METHOD(DOMNodeList,item)233 PHP_METHOD(DOMNodeList, item)
234 {
235 	zend_long index;
236 	ZEND_PARSE_PARAMETERS_START(1, 1)
237 		Z_PARAM_LONG(index)
238 	ZEND_PARSE_PARAMETERS_END();
239 
240 	zval *id = ZEND_THIS;
241 	dom_object *intern = Z_DOMOBJ_P(id);
242 	dom_nnodemap_object *objmap = intern->ptr;
243 	php_dom_nodelist_get_item_into_zval(objmap, index, return_value);
244 }
245 /* }}} end dom_nodelist_item */
246 
ZEND_METHOD(DOMNodeList,getIterator)247 ZEND_METHOD(DOMNodeList, getIterator)
248 {
249 	ZEND_PARSE_PARAMETERS_NONE();
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