xref: /PHP-8.2/ext/libxml/php_libxml.h (revision 03547f68)
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 	libxml_doc_props *doc_props;
68 	php_libxml_cache_tag cache_tag;
69 	int refcount;
70 	bool is_modern_api_class;
71 } php_libxml_ref_obj;
72 
73 typedef struct _php_libxml_node_ptr {
74 	xmlNodePtr node;
75 	int	refcount;
76 	void *_private;
77 } php_libxml_node_ptr;
78 
79 typedef struct _php_libxml_node_object {
80 	php_libxml_node_ptr *node;
81 	php_libxml_ref_obj *document;
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_pretend_ctx_error_ex(const char *file, int line, int column, const char *msg,...);
136 PHP_LIBXML_API void php_libxml_ctx_error(void *ctx, const char *msg, ...);
137 PHP_LIBXML_API int php_libxml_xmlCheckUTF8(const unsigned char *s);
138 PHP_LIBXML_API void php_libxml_switch_context(zval *context, zval *oldcontext);
139 PHP_LIBXML_API void php_libxml_issue_error(int level, const char *msg);
140 PHP_LIBXML_API bool php_libxml_disable_entity_loader(bool disable);
141 PHP_LIBXML_API void php_libxml_set_old_ns(xmlDocPtr doc, xmlNsPtr ns);
142 PHP_LIBXML_API php_stream_context *php_libxml_get_stream_context(void);
143 
144 PHP_LIBXML_API zend_string *php_libxml_sniff_charset_from_string(const char *start, const char *end);
145 PHP_LIBXML_API zend_string *php_libxml_sniff_charset_from_stream(const php_stream *s);
146 
147 /* Init/shutdown functions*/
148 PHP_LIBXML_API void php_libxml_initialize(void);
149 PHP_LIBXML_API void php_libxml_shutdown(void);
150 
151 #define LIBXML(v) ZEND_MODULE_GLOBALS_ACCESSOR(libxml, v)
152 
153 #if defined(ZTS) && defined(COMPILE_DL_LIBXML)
ZEND_TSRMLS_CACHE_EXTERN()154 ZEND_TSRMLS_CACHE_EXTERN()
155 #endif
156 
157 /* Other extension may override the global state options, these global options
158  * are copied initially to ctxt->options. Set the options to a known good value.
159  * See libxml2 globals.c and parserInternals.c.
160  * The unique_name argument allows multiple sanitizes and restores within the
161  * same function, even nested is necessary. */
162 # define PHP_LIBXML_SANITIZE_GLOBALS(unique_name) \
163 	ZEND_DIAGNOSTIC_IGNORED_START("-Wdeprecated-declarations") \
164 	int xml_old_loadsubset_##unique_name = xmlLoadExtDtdDefaultValue; \
165 	xmlLoadExtDtdDefaultValue = 0; \
166 	int xml_old_validate_##unique_name = xmlDoValidityCheckingDefaultValue; \
167 	xmlDoValidityCheckingDefaultValue = 0; \
168 	int xml_old_pedantic_##unique_name = xmlPedanticParserDefault(0); \
169 	int xml_old_substitute_##unique_name = xmlSubstituteEntitiesDefault(0); \
170 	int xml_old_linenrs_##unique_name = xmlLineNumbersDefault(0); \
171 	int xml_old_blanks_##unique_name = xmlKeepBlanksDefault(1); \
172 	ZEND_DIAGNOSTIC_IGNORED_END
173 
174 # define PHP_LIBXML_RESTORE_GLOBALS(unique_name) \
175 	ZEND_DIAGNOSTIC_IGNORED_START("-Wdeprecated-declarations") \
176 	xmlLoadExtDtdDefaultValue = xml_old_loadsubset_##unique_name; \
177 	xmlDoValidityCheckingDefaultValue = xml_old_validate_##unique_name; \
178 	(void) xmlPedanticParserDefault(xml_old_pedantic_##unique_name); \
179 	(void) xmlSubstituteEntitiesDefault(xml_old_substitute_##unique_name); \
180 	(void) xmlLineNumbersDefault(xml_old_linenrs_##unique_name); \
181 	(void) xmlKeepBlanksDefault(xml_old_blanks_##unique_name); \
182 	ZEND_DIAGNOSTIC_IGNORED_END
183 
184 /* Alternative for above, working directly on the context and not setting globals.
185  * Generally faster because no locking is involved, and this has the advantage that it sets the options to a known good value. */
186 static zend_always_inline void php_libxml_sanitize_parse_ctxt_options(xmlParserCtxtPtr ctxt)
187 {
188 	ctxt->loadsubset = 0;
189 	ctxt->validate = 0;
190 	ctxt->pedantic = 0;
191 	ctxt->replaceEntities = 0;
192 	ctxt->linenumbers = 0;
193 	ctxt->keepBlanks = 1;
194 	ctxt->options = 0;
195 }
196 
197 #else /* HAVE_LIBXML */
198 #define libxml_module_ptr NULL
199 #endif
200 
201 #define phpext_libxml_ptr libxml_module_ptr
202 
203 #endif /* PHP_LIBXML_H */
204