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: Shane Caraveo <shane@php.net> |
14 | Wez Furlong <wez@thebrainroom.com> |
15 +----------------------------------------------------------------------+
16 */
17
18 #ifndef PHP_LIBXML_H
19 #define PHP_LIBXML_H
20
21 #ifdef HAVE_LIBXML
22 extern zend_module_entry libxml_module_entry;
23 #define libxml_module_ptr &libxml_module_entry
24
25 #include "php_version.h"
26 #define PHP_LIBXML_VERSION PHP_VERSION
27
28 #ifdef PHP_WIN32
29 # define PHP_LIBXML_API __declspec(dllexport)
30 #elif defined(__GNUC__) && __GNUC__ >= 4
31 # define PHP_LIBXML_API __attribute__ ((visibility("default")))
32 #else
33 # define PHP_LIBXML_API
34 #endif
35
36 #include "zend_smart_str.h"
37 #include <libxml/tree.h>
38 #include <libxml/parser.h>
39
40 #define LIBXML_SAVE_NOEMPTYTAG 1<<2
41
42 ZEND_BEGIN_MODULE_GLOBALS(libxml)
43 zval stream_context;
44 smart_str error_buffer;
45 zend_llist *error_list;
46 zend_fcall_info_cache entity_loader_callback;
47 bool entity_loader_disabled;
48 ZEND_END_MODULE_GLOBALS(libxml)
49
50 typedef struct _libxml_doc_props {
51 HashTable *classmap;
52 bool formatoutput;
53 bool validateonparse;
54 bool resolveexternals;
55 bool preservewhitespace;
56 bool substituteentities;
57 bool stricterror;
58 bool recover;
59 } libxml_doc_props;
60
61 typedef struct {
62 size_t modification_nr;
63 } php_libxml_cache_tag;
64
65 typedef struct _php_libxml_ref_obj {
66 void *ptr;
67 int refcount;
68 libxml_doc_props *doc_props;
69 php_libxml_cache_tag cache_tag;
70 } php_libxml_ref_obj;
71
72 typedef struct _php_libxml_node_ptr {
73 xmlNodePtr node;
74 int refcount;
75 void *_private;
76 } php_libxml_node_ptr;
77
78 typedef struct _php_libxml_node_object {
79 php_libxml_node_ptr *node;
80 php_libxml_ref_obj *document;
81 HashTable *properties;
82 zend_object std;
83 } php_libxml_node_object;
84
85
php_libxml_node_fetch_object(zend_object * obj)86 static inline php_libxml_node_object *php_libxml_node_fetch_object(zend_object *obj) {
87 return (php_libxml_node_object *)((char*)(obj) - obj->handlers->offset);
88 }
89
php_libxml_invalidate_node_list_cache(php_libxml_ref_obj * doc_ptr)90 static zend_always_inline void php_libxml_invalidate_node_list_cache(php_libxml_ref_obj *doc_ptr)
91 {
92 if (!doc_ptr) {
93 return;
94 }
95 #if SIZEOF_SIZE_T == 8
96 /* If one operation happens every nanosecond, then it would still require 584 years to overflow
97 * the counter. So we'll just assume this never happens. */
98 doc_ptr->cache_tag.modification_nr++;
99 #else
100 size_t new_modification_nr = doc_ptr->cache_tag.modification_nr + 1;
101 if (EXPECTED(new_modification_nr > 0)) { /* unsigned overflow; checking after addition results in one less instruction */
102 doc_ptr->cache_tag.modification_nr = new_modification_nr;
103 }
104 #endif
105 }
106
php_libxml_invalidate_node_list_cache_from_doc(xmlDocPtr docp)107 static zend_always_inline void php_libxml_invalidate_node_list_cache_from_doc(xmlDocPtr docp)
108 {
109 if (docp && docp->_private) { /* docp is NULL for detached nodes */
110 php_libxml_node_ptr *node_private = (php_libxml_node_ptr *) docp->_private;
111 php_libxml_node_object *object_private = (php_libxml_node_object *) node_private->_private;
112 if (object_private) {
113 php_libxml_invalidate_node_list_cache(object_private->document);
114 }
115 }
116 }
117
118 #define Z_LIBXML_NODE_P(zv) php_libxml_node_fetch_object(Z_OBJ_P((zv)))
119
120 typedef void * (*php_libxml_export_node) (zval *object);
121
122 PHP_LIBXML_API int php_libxml_increment_node_ptr(php_libxml_node_object *object, xmlNodePtr node, void *private_data);
123 PHP_LIBXML_API int php_libxml_decrement_node_ptr(php_libxml_node_object *object);
124 PHP_LIBXML_API int php_libxml_increment_doc_ref(php_libxml_node_object *object, xmlDocPtr docp);
125 PHP_LIBXML_API int php_libxml_decrement_doc_ref(php_libxml_node_object *object);
126 PHP_LIBXML_API xmlNodePtr php_libxml_import_node(zval *object);
127 PHP_LIBXML_API zval *php_libxml_register_export(zend_class_entry *ce, php_libxml_export_node export_function);
128 /* When an explicit freeing of node and children is required */
129 PHP_LIBXML_API void php_libxml_node_free_list(xmlNodePtr node);
130 PHP_LIBXML_API void php_libxml_node_free_resource(xmlNodePtr node);
131 /* When object dtor is called as node may still be referenced */
132 PHP_LIBXML_API void php_libxml_node_decrement_resource(php_libxml_node_object *object);
133 PHP_LIBXML_API void php_libxml_error_handler(void *ctx, const char *msg, ...);
134 PHP_LIBXML_API void php_libxml_ctx_warning(void *ctx, const char *msg, ...);
135 PHP_LIBXML_API void php_libxml_ctx_error(void *ctx, const char *msg, ...);
136 PHP_LIBXML_API int php_libxml_xmlCheckUTF8(const unsigned char *s);
137 PHP_LIBXML_API void php_libxml_switch_context(zval *context, zval *oldcontext);
138 PHP_LIBXML_API void php_libxml_issue_error(int level, const char *msg);
139 PHP_LIBXML_API bool php_libxml_disable_entity_loader(bool disable);
140 PHP_LIBXML_API void php_libxml_set_old_ns(xmlDocPtr doc, xmlNsPtr ns);
141
142 /* Init/shutdown functions*/
143 PHP_LIBXML_API void php_libxml_initialize(void);
144 PHP_LIBXML_API void php_libxml_shutdown(void);
145
146 #define LIBXML(v) ZEND_MODULE_GLOBALS_ACCESSOR(libxml, v)
147
148 #if defined(ZTS) && defined(COMPILE_DL_LIBXML)
ZEND_TSRMLS_CACHE_EXTERN()149 ZEND_TSRMLS_CACHE_EXTERN()
150 #endif
151
152 #if defined(__clang__)
153 # define PHP_LIBXML_IGNORE_DEPRECATIONS_START \
154 _Pragma("clang diagnostic push") \
155 _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
156 # define PHP_LIBXML_IGNORE_DEPRECATIONS_END \
157 _Pragma("clang diagnostic pop")
158 #elif defined(__GNUC__)
159 # define PHP_LIBXML_IGNORE_DEPRECATIONS_START \
160 _Pragma("GCC diagnostic push") \
161 _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
162 # define PHP_LIBXML_IGNORE_DEPRECATIONS_END \
163 _Pragma("GCC diagnostic pop")
164 #else
165 # define PHP_LIBXML_IGNORE_DEPRECATIONS_START
166 # define PHP_LIBXML_IGNORE_DEPRECATIONS_END
167 #endif
168
169 /* Other extension may override the global state options, these global options
170 * are copied initially to ctxt->options. Set the options to a known good value.
171 * See libxml2 globals.c and parserInternals.c.
172 * The unique_name argument allows multiple sanitizes and restores within the
173 * same function, even nested is necessary. */
174 #define PHP_LIBXML_SANITIZE_GLOBALS(unique_name) \
175 PHP_LIBXML_IGNORE_DEPRECATIONS_START \
176 int xml_old_loadsubset_##unique_name = xmlLoadExtDtdDefaultValue; \
177 xmlLoadExtDtdDefaultValue = 0; \
178 int xml_old_validate_##unique_name = xmlDoValidityCheckingDefaultValue; \
179 xmlDoValidityCheckingDefaultValue = 0; \
180 int xml_old_pedantic_##unique_name = xmlPedanticParserDefault(0); \
181 int xml_old_substitute_##unique_name = xmlSubstituteEntitiesDefault(0); \
182 int xml_old_linenrs_##unique_name = xmlLineNumbersDefault(0); \
183 int xml_old_blanks_##unique_name = xmlKeepBlanksDefault(1); \
184 PHP_LIBXML_IGNORE_DEPRECATIONS_END
185
186 #define PHP_LIBXML_RESTORE_GLOBALS(unique_name) \
187 PHP_LIBXML_IGNORE_DEPRECATIONS_START \
188 xmlLoadExtDtdDefaultValue = xml_old_loadsubset_##unique_name; \
189 xmlDoValidityCheckingDefaultValue = xml_old_validate_##unique_name; \
190 (void) xmlPedanticParserDefault(xml_old_pedantic_##unique_name); \
191 (void) xmlSubstituteEntitiesDefault(xml_old_substitute_##unique_name); \
192 (void) xmlLineNumbersDefault(xml_old_linenrs_##unique_name); \
193 (void) xmlKeepBlanksDefault(xml_old_blanks_##unique_name); \
194 PHP_LIBXML_IGNORE_DEPRECATIONS_END
195
196 /* Alternative for above, working directly on the context and not setting globals.
197 * Generally faster because no locking is involved, and this has the advantage that it sets the options to a known good value. */
198 static zend_always_inline void php_libxml_sanitize_parse_ctxt_options(xmlParserCtxtPtr ctxt)
199 {
200 PHP_LIBXML_IGNORE_DEPRECATIONS_START
201 ctxt->loadsubset = 0;
202 ctxt->validate = 0;
203 ctxt->pedantic = 0;
204 ctxt->replaceEntities = 0;
205 ctxt->linenumbers = 0;
206 ctxt->keepBlanks = 1;
207 ctxt->options = 0;
208 PHP_LIBXML_IGNORE_DEPRECATIONS_END
209 }
210
211 #else /* HAVE_LIBXML */
212 #define libxml_module_ptr NULL
213 #endif
214
215 #define phpext_libxml_ptr libxml_module_ptr
216
217 #endif /* PHP_LIBXML_H */
218