- Notifications
You must be signed in to change notification settings - Fork 7.8k
/
Copy pathphp_libxml.h
278 lines (241 loc) · 11.4 KB
/
php_libxml.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Shane Caraveo <shane@php.net> |
| Wez Furlong <wez@thebrainroom.com> |
+----------------------------------------------------------------------+
*/
#ifndefPHP_LIBXML_H
#definePHP_LIBXML_H
#ifdefHAVE_LIBXML
externzend_module_entrylibxml_module_entry;
#definelibxml_module_ptr &libxml_module_entry
#include"php_version.h"
#definePHP_LIBXML_VERSION PHP_VERSION
#ifdefPHP_WIN32
# definePHP_LIBXML_API __declspec(dllexport)
#elif defined(__GNUC__) &&__GNUC__ >= 4
# definePHP_LIBXML_API __attribute__ ((visibility("default")))
#else
# definePHP_LIBXML_API
#endif
#include"zend_smart_str.h"
#include<libxml/tree.h>
#include<libxml/parser.h>
#defineLIBXML_SAVE_NOEMPTYTAG 1<<2
#defineLIBXML_NS_TAG_HOOK 1
ZEND_BEGIN_MODULE_GLOBALS(libxml)
zvalstream_context;
smart_strerror_buffer;
zend_llist*error_list;
zend_fcall_info_cacheentity_loader_callback;
boolentity_loader_disabled;
ZEND_END_MODULE_GLOBALS(libxml)
typedefstruct_libxml_doc_props {
HashTable*classmap;
boolformatoutput;
boolvalidateonparse;
boolresolveexternals;
boolpreservewhitespace;
boolsubstituteentities;
boolstricterror;
boolrecover;
} libxml_doc_props;
/* Modification tracking: when the object changes, we increment its counter.
* When this counter no longer matches the counter at the time of caching,
* we know that the object has changed and we have to update the cache. */
typedefstruct {
size_tmodification_nr;
} php_libxml_cache_tag;
typedefstructphp_libxml_private_data_header {
void (*dtor)(structphp_libxml_private_data_header*);
void (*ns_hook)(structphp_libxml_private_data_header*, xmlNodePtr);
/* extra fields */
} php_libxml_private_data_header;
/**
* It's possible to set custom handlers for certain actions depending on the type of document.
* For example, there exist multiple ways to serialize an XML document,
* therefore this structure allows setting up a custom handler.
*/
typedefstructphp_libxml_document_handlers {
zend_string*(*dump_node_to_str)(xmlDocPtrdoc, xmlNodePtrnode, boolformat, constchar*encoding);
zend_string*(*dump_doc_to_str)(xmlDocPtrdoc, intoptions, constchar*encoding);
zend_long (*dump_node_to_file)(constchar*filename, xmlDocPtrdoc, xmlNodePtrnode, boolformat, constchar*encoding);
zend_long (*dump_doc_to_file)(constchar*filename, xmlDocPtrdoc, boolformat, constchar*encoding);
} php_libxml_document_handlers;
/**
* Multiple representations are possible of the same underlying node data.
* This is the case for example when a SimpleXML node is imported into DOM.
* It must not be possible to obtain both a legacy and a modern representation
* of the same node, as they have different assumptions. The class_type field
* allows us to pin the representation to one of the two. If it is unset, no
* representation has been forced upon the node yet, and thus no assumptions
* have yet been made. This is the case for example when a SimpleXML node is
* created by SimpleXML itself and never leaves SimpleXML.
*/
typedefenum_php_libxml_class_type {
PHP_LIBXML_CLASS_UNSET=0,
PHP_LIBXML_CLASS_LEGACY=1,
PHP_LIBXML_CLASS_MODERN=2,
} php_libxml_class_type;
typedefenumphp_libxml_quirks_mode {
PHP_LIBXML_NO_QUIRKS=0,
PHP_LIBXML_QUIRKS,
PHP_LIBXML_LIMITED_QUIRKS,
} php_libxml_quirks_mode;
typedefstruct_php_libxml_ref_obj {
void*ptr;
libxml_doc_props*doc_props;
php_libxml_cache_tagcache_tag;
php_libxml_private_data_header*private_data;
constphp_libxml_document_handlers*handlers;
unsigned intrefcount;
php_libxml_class_typeclass_type : 8;
php_libxml_quirks_modequirks_mode : 8;
} php_libxml_ref_obj;
typedefstruct_php_libxml_node_ptr {
xmlNodePtrnode;
unsigned intrefcount;
void*_private;
} php_libxml_node_ptr;
typedefstruct_php_libxml_node_object {
php_libxml_node_ptr*node;
php_libxml_ref_obj*document;
zend_objectstd;
} php_libxml_node_object;
staticinlinephp_libxml_node_object*php_libxml_node_fetch_object(zend_object*obj) {
return (php_libxml_node_object*)((char*)(obj) -obj->handlers->offset);
}
staticzend_always_inlinevoidphp_libxml_invalidate_cache_tag(php_libxml_cache_tag*cache_tag)
{
#ifSIZEOF_SIZE_T==8
/* If one operation happens every nanosecond, then it would still require 584 years to overflow
* the counter. So we'll just assume this never happens. */
cache_tag->modification_nr++;
#else
size_tnew_modification_nr=cache_tag->modification_nr+1;
if (EXPECTED(new_modification_nr>0)) { /* unsigned overflow; checking after addition results in one less instruction */
cache_tag->modification_nr=new_modification_nr;
}
#endif
}
staticzend_always_inlineboolphp_libxml_is_cache_tag_stale(constphp_libxml_cache_tag*object_tag, constphp_libxml_cache_tag*cache_tag)
{
ZEND_ASSERT(object_tag!=NULL);
ZEND_ASSERT(cache_tag!=NULL);
/* See overflow comment in php_libxml_invalidate_node_list_cache(). */
#ifSIZEOF_SIZE_T==8
returncache_tag->modification_nr!=object_tag->modification_nr;
#else
returncache_tag->modification_nr!=object_tag->modification_nr||UNEXPECTED(object_tag->modification_nr==SIZE_MAX);
#endif
}
staticzend_always_inlinevoidphp_libxml_invalidate_node_list_cache(php_libxml_ref_obj*doc_ptr)
{
if (doc_ptr) {
php_libxml_invalidate_cache_tag(&doc_ptr->cache_tag);
}
}
staticzend_always_inlinevoidphp_libxml_invalidate_node_list_cache_from_doc(xmlDocPtrdocp)
{
if (docp&&docp->_private) { /* docp is NULL for detached nodes */
php_libxml_node_ptr*node_private= (php_libxml_node_ptr*) docp->_private;
php_libxml_node_object*object_private= (php_libxml_node_object*) node_private->_private;
if (object_private) {
php_libxml_invalidate_node_list_cache(object_private->document);
}
}
}
#defineZ_LIBXML_NODE_P(zv) php_libxml_node_fetch_object(Z_OBJ_P((zv)))
typedefvoid* (*php_libxml_export_node) (zval*object);
typedefenum {
PHP_LIBXML_ERROR=0,
PHP_LIBXML_CTX_ERROR=1,
PHP_LIBXML_CTX_WARNING=2,
} php_libxml_error_level;
PHP_LIBXML_APIintphp_libxml_increment_node_ptr(php_libxml_node_object*object, xmlNodePtrnode, void*private_data);
PHP_LIBXML_APIintphp_libxml_decrement_node_ptr(php_libxml_node_object*object);
PHP_LIBXML_APIintphp_libxml_decrement_node_ptr_ref(php_libxml_node_ptr*ptr);
PHP_LIBXML_APIintphp_libxml_increment_doc_ref(php_libxml_node_object*object, xmlDocPtrdocp);
PHP_LIBXML_APIintphp_libxml_decrement_doc_ref_directly(php_libxml_ref_obj*document);
PHP_LIBXML_APIintphp_libxml_decrement_doc_ref(php_libxml_node_object*object);
PHP_LIBXML_APIxmlNodePtrphp_libxml_import_node(zval*object);
PHP_LIBXML_APIzval*php_libxml_register_export(zend_class_entry*ce, php_libxml_export_nodeexport_function);
/* When an explicit freeing of node and children is required */
PHP_LIBXML_APIvoidphp_libxml_node_free_list(xmlNodePtrnode);
PHP_LIBXML_APIvoidphp_libxml_node_free_resource(xmlNodePtrnode);
/* When object dtor is called as node may still be referenced */
PHP_LIBXML_APIvoidphp_libxml_node_decrement_resource(php_libxml_node_object*object);
PHP_LIBXML_APIvoidphp_libxml_error_handler(void*ctx, constchar*msg, ...);
PHP_LIBXML_APIvoidphp_libxml_ctx_warning(void*ctx, constchar*msg, ...);
PHP_LIBXML_APIvoidphp_libxml_pretend_ctx_error_ex(constchar*file, intline, intcolumn, constchar*msg,...);
PHP_LIBXML_APIvoidphp_libxml_ctx_error(void*ctx, constchar*msg, ...);
PHP_LIBXML_APIvoidphp_libxml_error_handler_va(php_libxml_error_levelerror_type, void*ctx, constchar*msg, va_listargs);
PHP_LIBXML_APIintphp_libxml_xmlCheckUTF8(constunsigned char*s);
PHP_LIBXML_APIvoidphp_libxml_switch_context(zval*context, zval*oldcontext);
PHP_LIBXML_APIvoidphp_libxml_issue_error(intlevel, constchar*msg);
PHP_LIBXML_APIboolphp_libxml_disable_entity_loader(booldisable);
PHP_LIBXML_APIvoidphp_libxml_set_old_ns(xmlDocPtrdoc, xmlNsPtrns);
PHP_LIBXML_APIphp_stream_context*php_libxml_get_stream_context(void);
PHP_LIBXML_APIboolphp_libxml_uses_internal_errors(void);
PHP_LIBXML_APIxmlChar*php_libxml_attr_value(constxmlAttr*attr, bool*free);
PHP_LIBXML_APIzend_string*php_libxml_sniff_charset_from_string(constchar*start, constchar*end);
PHP_LIBXML_APIzend_string*php_libxml_sniff_charset_from_stream(constphp_stream*s);
/* Init/shutdown functions*/
PHP_LIBXML_APIvoidphp_libxml_initialize(void);
PHP_LIBXML_APIvoidphp_libxml_shutdown(void);
#defineLIBXML(v) ZEND_MODULE_GLOBALS_ACCESSOR(libxml, v)
#if defined(ZTS) && defined(COMPILE_DL_LIBXML)
ZEND_TSRMLS_CACHE_EXTERN()
#endif
/* Other extension may override the global state options, these global options
* are copied initially to ctxt->options. Set the options to a known good value.
* See libxml2 globals.c and parserInternals.c.
* The unique_name argument allows multiple sanitizes and restores within the
* same function, even nested is necessary. */
# definePHP_LIBXML_SANITIZE_GLOBALS(unique_name) \
ZEND_DIAGNOSTIC_IGNORED_START("-Wdeprecated-declarations") \
int xml_old_loadsubset_##unique_name = xmlLoadExtDtdDefaultValue; \
xmlLoadExtDtdDefaultValue = 0; \
int xml_old_validate_##unique_name = xmlDoValidityCheckingDefaultValue; \
xmlDoValidityCheckingDefaultValue = 0; \
int xml_old_pedantic_##unique_name = xmlPedanticParserDefault(0); \
int xml_old_substitute_##unique_name = xmlSubstituteEntitiesDefault(0); \
int xml_old_linenrs_##unique_name = xmlLineNumbersDefault(0); \
int xml_old_blanks_##unique_name = xmlKeepBlanksDefault(1); \
ZEND_DIAGNOSTIC_IGNORED_END
# definePHP_LIBXML_RESTORE_GLOBALS(unique_name) \
ZEND_DIAGNOSTIC_IGNORED_START("-Wdeprecated-declarations") \
xmlLoadExtDtdDefaultValue = xml_old_loadsubset_##unique_name; \
xmlDoValidityCheckingDefaultValue = xml_old_validate_##unique_name; \
(void) xmlPedanticParserDefault(xml_old_pedantic_##unique_name); \
(void) xmlSubstituteEntitiesDefault(xml_old_substitute_##unique_name); \
(void) xmlLineNumbersDefault(xml_old_linenrs_##unique_name); \
(void) xmlKeepBlanksDefault(xml_old_blanks_##unique_name); \
ZEND_DIAGNOSTIC_IGNORED_END
/* Alternative for above, working directly on the context and not setting globals.
* Generally faster because no locking is involved, and this has the advantage that it sets the options to a known good value. */
staticzend_always_inlinevoidphp_libxml_sanitize_parse_ctxt_options(xmlParserCtxtPtrctxt)
{
ZEND_DIAGNOSTIC_IGNORED_START("-Wdeprecated-declarations") \
ctxt->loadsubset=0;
ctxt->validate=0;
ctxt->pedantic=0;
ctxt->replaceEntities=0;
ctxt->linenumbers=0;
ctxt->keepBlanks=1;
ctxt->options=0;
ZEND_DIAGNOSTIC_IGNORED_END
}
#endif
#definephpext_libxml_ptr libxml_module_ptr
#endif/* PHP_LIBXML_H */