xref: /PHP-8.1/ext/libxml/php_libxml.h (revision 6a76e5d0)
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 	struct _php_libxml_entity_resolver {
47 		zval                    object;
48 		zend_fcall_info			fci;
49 		zend_fcall_info_cache	fcc;
50 	} entity_loader;
51 	bool entity_loader_disabled;
52 ZEND_END_MODULE_GLOBALS(libxml)
53 
54 typedef struct _libxml_doc_props {
55 	int formatoutput;
56 	int validateonparse;
57 	int resolveexternals;
58 	int preservewhitespace;
59 	int substituteentities;
60 	int stricterror;
61 	int recover;
62 	HashTable *classmap;
63 } libxml_doc_props;
64 
65 typedef struct _php_libxml_ref_obj {
66 	void *ptr;
67 	int   refcount;
68 	libxml_doc_props *doc_props;
69 } php_libxml_ref_obj;
70 
71 typedef struct _php_libxml_node_ptr {
72 	xmlNodePtr node;
73 	int	refcount;
74 	void *_private;
75 } php_libxml_node_ptr;
76 
77 typedef struct _php_libxml_node_object {
78 	php_libxml_node_ptr *node;
79 	php_libxml_ref_obj *document;
80 	HashTable *properties;
81 	zend_object  std;
82 } php_libxml_node_object;
83 
84 
php_libxml_node_fetch_object(zend_object * obj)85 static inline php_libxml_node_object *php_libxml_node_fetch_object(zend_object *obj) {
86 	return (php_libxml_node_object *)((char*)(obj) - obj->handlers->offset);
87 }
88 
89 #define Z_LIBXML_NODE_P(zv) php_libxml_node_fetch_object(Z_OBJ_P((zv)))
90 
91 typedef void * (*php_libxml_export_node) (zval *object);
92 
93 PHP_LIBXML_API int php_libxml_increment_node_ptr(php_libxml_node_object *object, xmlNodePtr node, void *private_data);
94 PHP_LIBXML_API int php_libxml_decrement_node_ptr(php_libxml_node_object *object);
95 PHP_LIBXML_API int php_libxml_increment_doc_ref(php_libxml_node_object *object, xmlDocPtr docp);
96 PHP_LIBXML_API int php_libxml_decrement_doc_ref(php_libxml_node_object *object);
97 PHP_LIBXML_API xmlNodePtr php_libxml_import_node(zval *object);
98 PHP_LIBXML_API zval *php_libxml_register_export(zend_class_entry *ce, php_libxml_export_node export_function);
99 /* When an explicit freeing of node and children is required */
100 PHP_LIBXML_API void php_libxml_node_free_list(xmlNodePtr node);
101 PHP_LIBXML_API void php_libxml_node_free_resource(xmlNodePtr node);
102 /* When object dtor is called as node may still be referenced */
103 PHP_LIBXML_API void php_libxml_node_decrement_resource(php_libxml_node_object *object);
104 PHP_LIBXML_API void php_libxml_error_handler(void *ctx, const char *msg, ...);
105 PHP_LIBXML_API void php_libxml_ctx_warning(void *ctx, const char *msg, ...);
106 PHP_LIBXML_API void php_libxml_ctx_error(void *ctx, const char *msg, ...);
107 PHP_LIBXML_API int php_libxml_xmlCheckUTF8(const unsigned char *s);
108 PHP_LIBXML_API void php_libxml_switch_context(zval *context, zval *oldcontext);
109 PHP_LIBXML_API void php_libxml_issue_error(int level, const char *msg);
110 PHP_LIBXML_API bool php_libxml_disable_entity_loader(bool disable);
111 
112 /* Init/shutdown functions*/
113 PHP_LIBXML_API void php_libxml_initialize(void);
114 PHP_LIBXML_API void php_libxml_shutdown(void);
115 
116 #define LIBXML(v) ZEND_MODULE_GLOBALS_ACCESSOR(libxml, v)
117 
118 #if defined(ZTS) && defined(COMPILE_DL_LIBXML)
ZEND_TSRMLS_CACHE_EXTERN()119 ZEND_TSRMLS_CACHE_EXTERN()
120 #endif
121 
122 /* Other extension may override the global state options, these global options
123  * are copied initially to ctxt->options. Set the options to a known good value.
124  * See libxml2 globals.c and parserInternals.c.
125  * The unique_name argument allows multiple sanitizes and restores within the
126  * same function, even nested is necessary. */
127 #define PHP_LIBXML_SANITIZE_GLOBALS(unique_name) \
128 	int xml_old_loadsubset_##unique_name = xmlLoadExtDtdDefaultValue; \
129 	xmlLoadExtDtdDefaultValue = 0; \
130 	int xml_old_validate_##unique_name = xmlDoValidityCheckingDefaultValue; \
131 	xmlDoValidityCheckingDefaultValue = 0; \
132 	int xml_old_pedantic_##unique_name = xmlPedanticParserDefault(0); \
133 	int xml_old_substitute_##unique_name = xmlSubstituteEntitiesDefault(0); \
134 	int xml_old_linenrs_##unique_name = xmlLineNumbersDefault(0); \
135 	int xml_old_blanks_##unique_name = xmlKeepBlanksDefault(1);
136 
137 #define PHP_LIBXML_RESTORE_GLOBALS(unique_name) \
138 	xmlLoadExtDtdDefaultValue = xml_old_loadsubset_##unique_name; \
139 	xmlDoValidityCheckingDefaultValue = xml_old_validate_##unique_name; \
140 	(void) xmlPedanticParserDefault(xml_old_pedantic_##unique_name); \
141 	(void) xmlSubstituteEntitiesDefault(xml_old_substitute_##unique_name); \
142 	(void) xmlLineNumbersDefault(xml_old_linenrs_##unique_name); \
143 	(void) xmlKeepBlanksDefault(xml_old_blanks_##unique_name);
144 
145 /* Alternative for above, working directly on the context and not setting globals.
146  * Generally faster because no locking is involved, and this has the advantage that it sets the options to a known good value. */
147 static zend_always_inline void php_libxml_sanitize_parse_ctxt_options(xmlParserCtxtPtr ctxt)
148 {
149 	ctxt->loadsubset = 0;
150 	ctxt->validate = 0;
151 	ctxt->pedantic = 0;
152 	ctxt->replaceEntities = 0;
153 	ctxt->linenumbers = 0;
154 	ctxt->keepBlanks = 1;
155 	ctxt->options = 0;
156 }
157 
158 #else /* HAVE_LIBXML */
159 #define libxml_module_ptr NULL
160 #endif
161 
162 #define phpext_libxml_ptr libxml_module_ptr
163 
164 #endif /* PHP_LIBXML_H */
165