xref: /php-src/ext/dom/internal_helpers.h (revision 6980eba8)
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: Niels Dossche <nielsdos@php.net>                            |
14    +----------------------------------------------------------------------+
15 */
16 
17 #ifndef DOM_INTERNAL_HELPERS
18 #define DOM_INTERNAL_HELPERS
19 
20 /* We're using the type flags of the zval to store an extra flag. */
21 #define DOM_Z_OWNED(z, v)   ZVAL_PTR(z, (void *) (v))
22 #define DOM_Z_UNOWNED(z, v) ZVAL_INDIRECT(z, (void *) (v))
23 #define DOM_Z_IS_OWNED(z)   (Z_TYPE_P(z) == IS_PTR)
24 
25 #ifdef DOM_CE_H
26 
27 #define DOM_DEF_GET_CE_FUNC(name) \
28 static zend_always_inline zend_class_entry *dom_get_##name##_ce(bool modern) \
29 { \
30    return modern ? dom_modern_##name##_class_entry : dom_##name##_class_entry; \
31 }
32 
dom_get_html_document_ce(bool modern)33 static zend_always_inline zend_class_entry *dom_get_html_document_ce(bool modern)
34 {
35 	return modern ? dom_html_document_class_entry : dom_document_class_entry;
36 }
37 
dom_get_xml_document_ce(bool modern)38 static zend_always_inline zend_class_entry *dom_get_xml_document_ce(bool modern)
39 {
40 	return modern ? dom_xml_document_class_entry : dom_document_class_entry;
41 }
42 
dom_get_dtd_namednodemap_ce(bool modern)43 static zend_always_inline zend_class_entry *dom_get_dtd_namednodemap_ce(bool modern)
44 {
45 	return modern ? dom_modern_dtd_namednodemap_class_entry : dom_namednodemap_class_entry;
46 }
47 
48 DOM_DEF_GET_CE_FUNC(node)
DOM_DEF_GET_CE_FUNC(documenttype)49 DOM_DEF_GET_CE_FUNC(documenttype)
50 DOM_DEF_GET_CE_FUNC(attr)
51 DOM_DEF_GET_CE_FUNC(entity)
52 DOM_DEF_GET_CE_FUNC(entityreference)
53 DOM_DEF_GET_CE_FUNC(processinginstruction)
54 DOM_DEF_GET_CE_FUNC(comment)
55 DOM_DEF_GET_CE_FUNC(text)
56 DOM_DEF_GET_CE_FUNC(cdatasection)
57 DOM_DEF_GET_CE_FUNC(notation)
58 DOM_DEF_GET_CE_FUNC(documentfragment)
59 DOM_DEF_GET_CE_FUNC(namednodemap)
60 DOM_DEF_GET_CE_FUNC(nodelist)
61 DOM_DEF_GET_CE_FUNC(domimplementation)
62 
63 #endif
64 
65 static zend_always_inline size_t dom_minimum_modification_nr_since_parsing(php_libxml_ref_obj *doc_ptr)
66 {
67 	/* For old-style DOM, we need a "new DOMDocument" + a load, so the minimum modification nr is 2.
68 	 * For new-style DOM, we need only to call a named constructor, so the minimum modification nr is 1. */
69 	return doc_ptr->class_type == PHP_LIBXML_CLASS_MODERN ? 1 : 2;
70 }
71 
dom_mark_document_cache_as_modified_since_parsing(php_libxml_ref_obj * doc_ptr)72 static zend_always_inline void dom_mark_document_cache_as_modified_since_parsing(php_libxml_ref_obj *doc_ptr)
73 {
74 	if (doc_ptr) {
75 		doc_ptr->cache_tag.modification_nr = MAX(dom_minimum_modification_nr_since_parsing(doc_ptr) + 1,
76 												 doc_ptr->cache_tag.modification_nr);
77 	}
78 }
79 
80 /* Marks the ID cache as potentially stale */
dom_mark_ids_modified(php_libxml_ref_obj * doc_ptr)81 static zend_always_inline void dom_mark_ids_modified(php_libxml_ref_obj *doc_ptr)
82 {
83 	/* Although this is currently a wrapper function, it's best to abstract the actual implementation away. */
84 	dom_mark_document_cache_as_modified_since_parsing(doc_ptr);
85 }
86 
dom_is_document_cache_modified_since_parsing(php_libxml_ref_obj * doc_ptr)87 static zend_always_inline bool dom_is_document_cache_modified_since_parsing(php_libxml_ref_obj *doc_ptr)
88 {
89 	return !doc_ptr || doc_ptr->cache_tag.modification_nr > dom_minimum_modification_nr_since_parsing(doc_ptr);
90 }
91 
dom_mangle_pointer_for_key(const void * ptr)92 static zend_always_inline zend_long dom_mangle_pointer_for_key(const void *ptr)
93 {
94 	zend_ulong value = (zend_ulong) (uintptr_t) ptr;
95 	/* Rotate 3/4 bits for better hash distribution because the low 3/4 bits are normally 0. */
96 	const size_t rol_amount = (SIZEOF_ZEND_LONG == 8) ? 4 : 3;
97 	return (value >> rol_amount) | (value << (sizeof(value) * 8 - rol_amount));
98 }
99 
100 #endif
101