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