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 #if defined(__clang__)
123 # define PHP_LIBXML_IGNORE_DEPRECATIONS_START \
124 _Pragma("clang diagnostic push") \
125 _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
126 # define PHP_LIBXML_IGNORE_DEPRECATIONS_END \
127 _Pragma("clang diagnostic pop")
128 #elif defined(__GNUC__)
129 # define PHP_LIBXML_IGNORE_DEPRECATIONS_START \
130 _Pragma("GCC diagnostic push") \
131 _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
132 # define PHP_LIBXML_IGNORE_DEPRECATIONS_END \
133 _Pragma("GCC diagnostic pop")
134 #else
135 # define PHP_LIBXML_IGNORE_DEPRECATIONS_START
136 # define PHP_LIBXML_IGNORE_DEPRECATIONS_END
137 #endif
138
139 /* Other extension may override the global state options, these global options
140 * are copied initially to ctxt->options. Set the options to a known good value.
141 * See libxml2 globals.c and parserInternals.c.
142 * The unique_name argument allows multiple sanitizes and restores within the
143 * same function, even nested is necessary. */
144 #define PHP_LIBXML_SANITIZE_GLOBALS(unique_name) \
145 PHP_LIBXML_IGNORE_DEPRECATIONS_START \
146 int xml_old_loadsubset_##unique_name = xmlLoadExtDtdDefaultValue; \
147 xmlLoadExtDtdDefaultValue = 0; \
148 int xml_old_validate_##unique_name = xmlDoValidityCheckingDefaultValue; \
149 xmlDoValidityCheckingDefaultValue = 0; \
150 int xml_old_pedantic_##unique_name = xmlPedanticParserDefault(0); \
151 int xml_old_substitute_##unique_name = xmlSubstituteEntitiesDefault(0); \
152 int xml_old_linenrs_##unique_name = xmlLineNumbersDefault(0); \
153 int xml_old_blanks_##unique_name = xmlKeepBlanksDefault(1); \
154 PHP_LIBXML_IGNORE_DEPRECATIONS_END
155
156 #define PHP_LIBXML_RESTORE_GLOBALS(unique_name) \
157 PHP_LIBXML_IGNORE_DEPRECATIONS_START \
158 xmlLoadExtDtdDefaultValue = xml_old_loadsubset_##unique_name; \
159 xmlDoValidityCheckingDefaultValue = xml_old_validate_##unique_name; \
160 (void) xmlPedanticParserDefault(xml_old_pedantic_##unique_name); \
161 (void) xmlSubstituteEntitiesDefault(xml_old_substitute_##unique_name); \
162 (void) xmlLineNumbersDefault(xml_old_linenrs_##unique_name); \
163 (void) xmlKeepBlanksDefault(xml_old_blanks_##unique_name); \
164 PHP_LIBXML_IGNORE_DEPRECATIONS_END
165
166 /* Alternative for above, working directly on the context and not setting globals.
167 * Generally faster because no locking is involved, and this has the advantage that it sets the options to a known good value. */
168 static zend_always_inline void php_libxml_sanitize_parse_ctxt_options(xmlParserCtxtPtr ctxt)
169 {
170 PHP_LIBXML_IGNORE_DEPRECATIONS_START
171 ctxt->loadsubset = 0;
172 ctxt->validate = 0;
173 ctxt->pedantic = 0;
174 ctxt->replaceEntities = 0;
175 ctxt->linenumbers = 0;
176 ctxt->keepBlanks = 1;
177 ctxt->options = 0;
178 PHP_LIBXML_IGNORE_DEPRECATIONS_END
179 }
180
181 #else /* HAVE_LIBXML */
182 #define libxml_module_ptr NULL
183 #endif
184
185 #define phpext_libxml_ptr libxml_module_ptr
186
187 #endif /* PHP_LIBXML_H */
188