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 #ifndef PHP_XML_COMMON_H
19 #define PHP_XML_COMMON_H
20
21 #include "ext/libxml/php_libxml.h"
22
23 typedef libxml_doc_props *dom_doc_propsptr;
24
25 typedef struct _dom_object {
26 void *ptr;
27 php_libxml_ref_obj *document;
28 HashTable *prop_handler;
29 zend_object std;
30 } dom_object;
31
php_dom_obj_from_obj(zend_object * obj)32 static inline dom_object *php_dom_obj_from_obj(zend_object *obj) {
33 return (dom_object*)((char*)(obj) - XtOffsetOf(dom_object, std));
34 }
35
36 #define Z_DOMOBJ_P(zv) php_dom_obj_from_obj(Z_OBJ_P((zv)))
37
38 #ifdef PHP_WIN32
39 # ifdef DOM_EXPORTS
40 # define PHP_DOM_EXPORT __declspec(dllexport)
41 # elif !defined(DOM_LOCAL_DEFINES) /* Allow to counteract LNK4049 warning. */
42 # define PHP_DOM_EXPORT __declspec(dllimport)
43 # else
44 # define PHP_DOM_EXPORT
45 # endif /* DOM_EXPORTS */
46 #elif defined(__GNUC__) && __GNUC__ >= 4
47 # define PHP_DOM_EXPORT __attribute__ ((visibility("default")))
48 #elif defined(PHPAPI)
49 # define PHP_DOM_EXPORT PHPAPI
50 #else
51 # define PHP_DOM_EXPORT
52 #endif
53
54 PHP_DOM_EXPORT extern zend_class_entry *dom_node_class_entry;
55 PHP_DOM_EXPORT dom_object *php_dom_object_get_data(xmlNodePtr obj);
56 PHP_DOM_EXPORT bool php_dom_create_object(xmlNodePtr obj, zval* return_value, dom_object *domobj);
57 PHP_DOM_EXPORT xmlNodePtr dom_object_get_node(dom_object *obj);
58
59 #define NODE_GET_OBJ(__ptr, __id, __prtype, __intern) { \
60 __intern = Z_LIBXML_NODE_P(__id); \
61 if (UNEXPECTED(__intern->node == NULL)) { \
62 php_error_docref(NULL, E_WARNING, "Couldn't fetch %s", \
63 ZSTR_VAL(__intern->std.ce->name));\
64 RETURN_NULL();\
65 } \
66 __ptr = (__prtype)__intern->node->node; \
67 }
68
69 #define DOC_GET_OBJ(__ptr, __id, __prtype, __intern) { \
70 __intern = Z_LIBXML_NODE_P(__id); \
71 if (EXPECTED(__intern->document != NULL)) { \
72 __ptr = (__prtype)__intern->document->ptr); \
73 } \
74 }
75
76 #define DOM_RET_OBJ(obj, domobject) \
77 php_dom_create_object(obj, return_value, domobject)
78
79 #define DOM_GET_THIS_OBJ(__ptr, __id, __prtype, __intern) \
80 __id = ZEND_THIS; \
81 DOM_GET_OBJ(__ptr, __id, __prtype, __intern);
82
83 struct php_dom_private_data;
84 typedef struct php_dom_private_data php_dom_private_data;
85
php_dom_get_private_data(dom_object * intern)86 static zend_always_inline php_dom_private_data *php_dom_get_private_data(dom_object *intern)
87 {
88 ZEND_ASSERT(intern->document != NULL);
89 return (php_dom_private_data *) intern->document->private_data;
90 }
91
php_dom_next_in_tree_order(const xmlNode * nodep,const xmlNode * basep)92 static zend_always_inline xmlNodePtr php_dom_next_in_tree_order(const xmlNode *nodep, const xmlNode *basep)
93 {
94 if (nodep->type == XML_ELEMENT_NODE && nodep->children) {
95 return nodep->children;
96 }
97
98 if (nodep->next) {
99 return nodep->next;
100 } else {
101 /* Go upwards, until we find a parent node with a next sibling, or until we hit the base. */
102 do {
103 nodep = nodep->parent;
104 if (nodep == basep) {
105 return NULL;
106 }
107 /* This shouldn't happen, unless there's an invalidation bug somewhere. */
108 if (UNEXPECTED(nodep == NULL)) {
109 zend_throw_error(NULL, "Current node in traversal is not in the document. Please report this as a bug in php-src.");
110 return NULL;
111 }
112 } while (nodep->next == NULL);
113 return nodep->next;
114 }
115 }
116
php_dom_follow_spec_doc_ref(const php_libxml_ref_obj * document)117 static zend_always_inline bool php_dom_follow_spec_doc_ref(const php_libxml_ref_obj *document)
118 {
119 return document != NULL && document->class_type == PHP_LIBXML_CLASS_MODERN;
120 }
121
php_dom_follow_spec_intern(const dom_object * intern)122 static zend_always_inline bool php_dom_follow_spec_intern(const dom_object *intern)
123 {
124 ZEND_ASSERT(intern != NULL);
125 return php_dom_follow_spec_doc_ref(intern->document);
126 }
127
128 #endif
129