xref: /PHP-7.4/ext/xmlwriter/php_xmlwriter.c (revision fe1bfb78)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) The PHP Group                                          |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt.                                 |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Author: Rob Richards <rrichards@php.net>                             |
16   |         Pierre-A. Joye <pajoye@php.net>                              |
17   +----------------------------------------------------------------------+
18 */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 
25 #include "php.h"
26 #include "php_ini.h"
27 #include "ext/standard/info.h"
28 #include "php_xmlwriter.h"
29 #include "ext/standard/php_string.h"
30 
31 static PHP_FUNCTION(xmlwriter_set_indent);
32 static PHP_FUNCTION(xmlwriter_set_indent_string);
33 static PHP_FUNCTION(xmlwriter_start_attribute);
34 static PHP_FUNCTION(xmlwriter_end_attribute);
35 static PHP_FUNCTION(xmlwriter_write_attribute);
36 static PHP_FUNCTION(xmlwriter_start_attribute_ns);
37 static PHP_FUNCTION(xmlwriter_write_attribute_ns);
38 static PHP_FUNCTION(xmlwriter_start_element);
39 static PHP_FUNCTION(xmlwriter_end_element);
40 static PHP_FUNCTION(xmlwriter_full_end_element);
41 static PHP_FUNCTION(xmlwriter_start_element_ns);
42 static PHP_FUNCTION(xmlwriter_write_element);
43 static PHP_FUNCTION(xmlwriter_write_element_ns);
44 static PHP_FUNCTION(xmlwriter_start_pi);
45 static PHP_FUNCTION(xmlwriter_end_pi);
46 static PHP_FUNCTION(xmlwriter_write_pi);
47 static PHP_FUNCTION(xmlwriter_start_cdata);
48 static PHP_FUNCTION(xmlwriter_end_cdata);
49 static PHP_FUNCTION(xmlwriter_write_cdata);
50 static PHP_FUNCTION(xmlwriter_text);
51 static PHP_FUNCTION(xmlwriter_write_raw);
52 static PHP_FUNCTION(xmlwriter_start_document);
53 static PHP_FUNCTION(xmlwriter_end_document);
54 static PHP_FUNCTION(xmlwriter_start_comment);
55 static PHP_FUNCTION(xmlwriter_end_comment);
56 static PHP_FUNCTION(xmlwriter_write_comment);
57 static PHP_FUNCTION(xmlwriter_start_dtd);
58 static PHP_FUNCTION(xmlwriter_end_dtd);
59 static PHP_FUNCTION(xmlwriter_write_dtd);
60 static PHP_FUNCTION(xmlwriter_start_dtd_element);
61 static PHP_FUNCTION(xmlwriter_end_dtd_element);
62 static PHP_FUNCTION(xmlwriter_write_dtd_element);
63 static PHP_FUNCTION(xmlwriter_start_dtd_attlist);
64 static PHP_FUNCTION(xmlwriter_end_dtd_attlist);
65 static PHP_FUNCTION(xmlwriter_write_dtd_attlist);
66 static PHP_FUNCTION(xmlwriter_start_dtd_entity);
67 static PHP_FUNCTION(xmlwriter_end_dtd_entity);
68 static PHP_FUNCTION(xmlwriter_write_dtd_entity);
69 static PHP_FUNCTION(xmlwriter_open_uri);
70 static PHP_FUNCTION(xmlwriter_open_memory);
71 static PHP_FUNCTION(xmlwriter_output_memory);
72 static PHP_FUNCTION(xmlwriter_flush);
73 
74 static zend_class_entry *xmlwriter_class_entry_ce;
75 
76 static void xmlwriter_free_resource_ptr(xmlwriter_object *intern);
77 static void xmlwriter_dtor(zend_resource *rsrc);
78 
79 typedef int (*xmlwriter_read_one_char_t)(xmlTextWriterPtr writer, const xmlChar *content);
80 typedef int (*xmlwriter_read_int_t)(xmlTextWriterPtr writer);
81 
82 /* {{{ xmlwriter_object_free_storage */
xmlwriter_free_resource_ptr(xmlwriter_object * intern)83 static void xmlwriter_free_resource_ptr(xmlwriter_object *intern)
84 {
85 	if (intern) {
86 		if (intern->ptr) {
87 			xmlFreeTextWriter(intern->ptr);
88 			intern->ptr = NULL;
89 		}
90 		if (intern->output) {
91 			xmlBufferFree(intern->output);
92 			intern->output = NULL;
93 		}
94 		efree(intern);
95 	}
96 }
97 /* }}} */
98 
99 /* {{{ XMLWRITER_FROM_OBJECT */
100 #define XMLWRITER_FROM_OBJECT(intern, object) \
101 	{ \
102 		ze_xmlwriter_object *obj = Z_XMLWRITER_P(object); \
103 		intern = obj->xmlwriter_ptr; \
104 		if (!intern) { \
105 			php_error_docref(NULL, E_WARNING, "Invalid or uninitialized XMLWriter object"); \
106 			RETURN_FALSE; \
107 		} \
108 	}
109 /* }}} */
110 
111 static zend_object_handlers xmlwriter_object_handlers;
112 
113 /* {{{{ xmlwriter_object_dtor */
xmlwriter_object_dtor(zend_object * object)114 static void xmlwriter_object_dtor(zend_object *object)
115 {
116 	ze_xmlwriter_object *intern = php_xmlwriter_fetch_object(object);
117 
118 	/* freeing the resource here may leak, but otherwise we may use it after it has been freed */
119 	if (intern->xmlwriter_ptr) {
120 		xmlwriter_free_resource_ptr(intern->xmlwriter_ptr);
121 	}
122 	intern->xmlwriter_ptr = NULL;
123 	zend_objects_destroy_object(object);
124 }
125 /* }}} */
126 
127 /* {{{ xmlwriter_object_free_storage */
xmlwriter_object_free_storage(zend_object * object)128 static void xmlwriter_object_free_storage(zend_object *object)
129 {
130 	ze_xmlwriter_object *intern = php_xmlwriter_fetch_object(object);
131 
132 	zend_object_std_dtor(&intern->std);
133 }
134 /* }}} */
135 
136 
137 /* {{{ xmlwriter_object_new */
xmlwriter_object_new(zend_class_entry * class_type)138 static zend_object *xmlwriter_object_new(zend_class_entry *class_type)
139 {
140 	ze_xmlwriter_object *intern;
141 
142 	intern = zend_object_alloc(sizeof(ze_xmlwriter_object), class_type);
143 	zend_object_std_init(&intern->std, class_type);
144 	object_properties_init(&intern->std, class_type);
145 	intern->std.handlers = &xmlwriter_object_handlers;
146 
147 	return &intern->std;
148 }
149 /* }}} */
150 
151 #define XMLW_NAME_CHK(__err) \
152 	if (xmlValidateName((xmlChar *) name, 0) != 0) {	\
153 		php_error_docref(NULL, E_WARNING, "%s", __err);	\
154 		RETURN_FALSE;	\
155 	}	\
156 
157 /* {{{ arginfo */
158 ZEND_BEGIN_ARG_INFO(arginfo_xmlwriter_void, 0)
159 ZEND_END_ARG_INFO()
160 
161 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_resource, 0, 0, 1)
162 	ZEND_ARG_INFO(0, xmlwriter)
163 ZEND_END_ARG_INFO()
164 
165 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_open_uri, 0, 0, 1)
166 	ZEND_ARG_INFO(0, uri)
167 ZEND_END_ARG_INFO()
168 
169 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_set_indent, 0, 0, 2)
170 	ZEND_ARG_INFO(0, xmlwriter)
171 	ZEND_ARG_INFO(0, indent)
172 ZEND_END_ARG_INFO()
173 
174 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_set_indent, 0, 0, 1)
175 	ZEND_ARG_INFO(0, indent)
176 ZEND_END_ARG_INFO()
177 
178 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_set_indent_string, 0, 0, 2)
179 	ZEND_ARG_INFO(0, xmlwriter)
180 	ZEND_ARG_INFO(0, indentString)
181 ZEND_END_ARG_INFO()
182 
183 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_set_indent_string, 0, 0, 1)
184 	ZEND_ARG_INFO(0, indentString)
185 ZEND_END_ARG_INFO()
186 
187 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_start_attribute, 0, 0, 2)
188 	ZEND_ARG_INFO(0, xmlwriter)
189 	ZEND_ARG_INFO(0, name)
190 ZEND_END_ARG_INFO()
191 
192 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_start_attribute, 0, 0, 1)
193 	ZEND_ARG_INFO(0, name)
194 ZEND_END_ARG_INFO()
195 
196 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_start_attribute_ns, 0, 0, 4)
197 	ZEND_ARG_INFO(0, xmlwriter)
198 	ZEND_ARG_INFO(0, prefix)
199 	ZEND_ARG_INFO(0, name)
200 	ZEND_ARG_INFO(0, uri)
201 ZEND_END_ARG_INFO()
202 
203 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_start_attribute_ns, 0, 0, 3)
204 	ZEND_ARG_INFO(0, prefix)
205 	ZEND_ARG_INFO(0, name)
206 	ZEND_ARG_INFO(0, uri)
207 ZEND_END_ARG_INFO()
208 
209 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_write_attribute_ns, 0, 0, 5)
210 	ZEND_ARG_INFO(0, xmlwriter)
211 	ZEND_ARG_INFO(0, prefix)
212 	ZEND_ARG_INFO(0, name)
213 	ZEND_ARG_INFO(0, uri)
214 	ZEND_ARG_INFO(0, content)
215 ZEND_END_ARG_INFO()
216 
217 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_write_attribute_ns, 0, 0, 4)
218 	ZEND_ARG_INFO(0, prefix)
219 	ZEND_ARG_INFO(0, name)
220 	ZEND_ARG_INFO(0, uri)
221 	ZEND_ARG_INFO(0, content)
222 ZEND_END_ARG_INFO()
223 
224 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_write_attribute, 0, 0, 3)
225 	ZEND_ARG_INFO(0, xmlwriter)
226 	ZEND_ARG_INFO(0, name)
227 	ZEND_ARG_INFO(0, value)
228 ZEND_END_ARG_INFO()
229 
230 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_write_attribute, 0, 0, 2)
231 	ZEND_ARG_INFO(0, name)
232 	ZEND_ARG_INFO(0, value)
233 ZEND_END_ARG_INFO()
234 
235 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_start_element, 0, 0, 2)
236 	ZEND_ARG_INFO(0, xmlwriter)
237 	ZEND_ARG_INFO(0, name)
238 ZEND_END_ARG_INFO()
239 
240 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_start_element, 0, 0, 1)
241 	ZEND_ARG_INFO(0, name)
242 ZEND_END_ARG_INFO()
243 
244 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_start_element_ns, 0, 0, 4)
245 	ZEND_ARG_INFO(0, xmlwriter)
246 	ZEND_ARG_INFO(0, prefix)
247 	ZEND_ARG_INFO(0, name)
248 	ZEND_ARG_INFO(0, uri)
249 ZEND_END_ARG_INFO()
250 
251 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_start_element_ns, 0, 0, 3)
252 	ZEND_ARG_INFO(0, prefix)
253 	ZEND_ARG_INFO(0, name)
254 	ZEND_ARG_INFO(0, uri)
255 ZEND_END_ARG_INFO()
256 
257 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_write_element, 0, 0, 2)
258 	ZEND_ARG_INFO(0, xmlwriter)
259 	ZEND_ARG_INFO(0, name)
260 	ZEND_ARG_INFO(0, content)
261 ZEND_END_ARG_INFO()
262 
263 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_write_element, 0, 0, 1)
264 	ZEND_ARG_INFO(0, name)
265 	ZEND_ARG_INFO(0, content)
266 ZEND_END_ARG_INFO()
267 
268 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_write_element_ns, 0, 0, 4)
269 	ZEND_ARG_INFO(0, xmlwriter)
270 	ZEND_ARG_INFO(0, prefix)
271 	ZEND_ARG_INFO(0, name)
272 	ZEND_ARG_INFO(0, uri)
273 	ZEND_ARG_INFO(0, content)
274 ZEND_END_ARG_INFO()
275 
276 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_write_element_ns, 0, 0, 3)
277 	ZEND_ARG_INFO(0, prefix)
278 	ZEND_ARG_INFO(0, name)
279 	ZEND_ARG_INFO(0, uri)
280 	ZEND_ARG_INFO(0, content)
281 ZEND_END_ARG_INFO()
282 
283 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_start_pi, 0, 0, 2)
284 	ZEND_ARG_INFO(0, xmlwriter)
285 	ZEND_ARG_INFO(0, target)
286 ZEND_END_ARG_INFO()
287 
288 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_start_pi, 0, 0, 1)
289 	ZEND_ARG_INFO(0, target)
290 ZEND_END_ARG_INFO()
291 
292 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_write_pi, 0, 0, 3)
293 	ZEND_ARG_INFO(0, xmlwriter)
294 	ZEND_ARG_INFO(0, target)
295 	ZEND_ARG_INFO(0, content)
296 ZEND_END_ARG_INFO()
297 
298 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_write_pi, 0, 0, 2)
299 	ZEND_ARG_INFO(0, target)
300 	ZEND_ARG_INFO(0, content)
301 ZEND_END_ARG_INFO()
302 
303 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_write_cdata, 0, 0, 2)
304 	ZEND_ARG_INFO(0, xmlwriter)
305 	ZEND_ARG_INFO(0, content)
306 ZEND_END_ARG_INFO()
307 
308 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_write_cdata, 0, 0, 1)
309 	ZEND_ARG_INFO(0, content)
310 ZEND_END_ARG_INFO()
311 
312 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_text, 0, 0, 2)
313 	ZEND_ARG_INFO(0, xmlwriter)
314 	ZEND_ARG_INFO(0, content)
315 ZEND_END_ARG_INFO()
316 
317 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_text, 0, 0, 1)
318 	ZEND_ARG_INFO(0, content)
319 ZEND_END_ARG_INFO()
320 
321 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_write_raw, 0, 0, 2)
322 	ZEND_ARG_INFO(0, xmlwriter)
323 	ZEND_ARG_INFO(0, content)
324 ZEND_END_ARG_INFO()
325 
326 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_write_raw, 0, 0, 1)
327 	ZEND_ARG_INFO(0, content)
328 ZEND_END_ARG_INFO()
329 
330 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_start_document, 0, 0, 1)
331 	ZEND_ARG_INFO(0, xmlwriter)
332 	ZEND_ARG_INFO(0, version)
333 	ZEND_ARG_INFO(0, encoding)
334 	ZEND_ARG_INFO(0, standalone)
335 ZEND_END_ARG_INFO()
336 
337 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_start_document, 0, 0, 0)
338 	ZEND_ARG_INFO(0, version)
339 	ZEND_ARG_INFO(0, encoding)
340 	ZEND_ARG_INFO(0, standalone)
341 ZEND_END_ARG_INFO()
342 
343 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_write_comment, 0, 0, 2)
344 	ZEND_ARG_INFO(0, xmlwriter)
345 	ZEND_ARG_INFO(0, content)
346 ZEND_END_ARG_INFO()
347 
348 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_write_comment, 0, 0, 1)
349 	ZEND_ARG_INFO(0, content)
350 ZEND_END_ARG_INFO()
351 
352 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_start_dtd, 0, 0, 2)
353 	ZEND_ARG_INFO(0, xmlwriter)
354 	ZEND_ARG_INFO(0, qualifiedName)
355 	ZEND_ARG_INFO(0, publicId)
356 	ZEND_ARG_INFO(0, systemId)
357 ZEND_END_ARG_INFO()
358 
359 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_start_dtd, 0, 0, 1)
360 	ZEND_ARG_INFO(0, qualifiedName)
361 	ZEND_ARG_INFO(0, publicId)
362 	ZEND_ARG_INFO(0, systemId)
363 ZEND_END_ARG_INFO()
364 
365 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_write_dtd, 0, 0, 2)
366 	ZEND_ARG_INFO(0, xmlwriter)
367 	ZEND_ARG_INFO(0, name)
368 	ZEND_ARG_INFO(0, publicId)
369 	ZEND_ARG_INFO(0, systemId)
370 	ZEND_ARG_INFO(0, subset)
371 ZEND_END_ARG_INFO()
372 
373 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_write_dtd, 0, 0, 1)
374 	ZEND_ARG_INFO(0, name)
375 	ZEND_ARG_INFO(0, publicId)
376 	ZEND_ARG_INFO(0, systemId)
377 	ZEND_ARG_INFO(0, subset)
378 ZEND_END_ARG_INFO()
379 
380 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_start_dtd_element, 0, 0, 2)
381 	ZEND_ARG_INFO(0, xmlwriter)
382 	ZEND_ARG_INFO(0, qualifiedName)
383 ZEND_END_ARG_INFO()
384 
385 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_start_dtd_element, 0, 0, 1)
386 	ZEND_ARG_INFO(0, qualifiedName)
387 ZEND_END_ARG_INFO()
388 
389 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_write_dtd_element, 0, 0, 3)
390 	ZEND_ARG_INFO(0, xmlwriter)
391 	ZEND_ARG_INFO(0, name)
392 	ZEND_ARG_INFO(0, content)
393 ZEND_END_ARG_INFO()
394 
395 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_write_dtd_element, 0, 0, 2)
396 	ZEND_ARG_INFO(0, name)
397 	ZEND_ARG_INFO(0, content)
398 ZEND_END_ARG_INFO()
399 
400 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_start_dtd_attlist, 0, 0, 2)
401 	ZEND_ARG_INFO(0, xmlwriter)
402 	ZEND_ARG_INFO(0, name)
403 ZEND_END_ARG_INFO()
404 
405 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_start_dtd_attlist, 0, 0, 1)
406 	ZEND_ARG_INFO(0, name)
407 ZEND_END_ARG_INFO()
408 
409 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_write_dtd_attlist, 0, 0, 3)
410 	ZEND_ARG_INFO(0, xmlwriter)
411 	ZEND_ARG_INFO(0, name)
412 	ZEND_ARG_INFO(0, content)
413 ZEND_END_ARG_INFO()
414 
415 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_write_dtd_attlist, 0, 0, 2)
416 	ZEND_ARG_INFO(0, name)
417 	ZEND_ARG_INFO(0, content)
418 ZEND_END_ARG_INFO()
419 
420 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_start_dtd_entity, 0, 0, 3)
421 	ZEND_ARG_INFO(0, xmlwriter)
422 	ZEND_ARG_INFO(0, name)
423 	ZEND_ARG_INFO(0, isparam)
424 ZEND_END_ARG_INFO()
425 
426 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_start_dtd_entity, 0, 0, 2)
427 	ZEND_ARG_INFO(0, name)
428 	ZEND_ARG_INFO(0, isparam)
429 ZEND_END_ARG_INFO()
430 
431 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_write_dtd_entity, 0, 0, 3)
432 	ZEND_ARG_INFO(0, xmlwriter)
433 	ZEND_ARG_INFO(0, name)
434 	ZEND_ARG_INFO(0, content)
435 ZEND_END_ARG_INFO()
436 
437 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_write_dtd_entity, 0, 0, 2)
438 	ZEND_ARG_INFO(0, name)
439 	ZEND_ARG_INFO(0, content)
440 ZEND_END_ARG_INFO()
441 
442 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_output_memory, 0, 0, 1)
443 	ZEND_ARG_INFO(0, xmlwriter)
444 	ZEND_ARG_INFO(0, flush)
445 ZEND_END_ARG_INFO()
446 
447 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_output_memory, 0, 0, 0)
448 	ZEND_ARG_INFO(0, flush)
449 ZEND_END_ARG_INFO()
450 
451 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_flush, 0, 0, 1)
452 	ZEND_ARG_INFO(0, xmlwriter)
453 	ZEND_ARG_INFO(0, empty)
454 ZEND_END_ARG_INFO()
455 
456 ZEND_BEGIN_ARG_INFO_EX(arginfo_xmlwriter_method_flush, 0, 0, 0)
457 	ZEND_ARG_INFO(0, empty)
458 ZEND_END_ARG_INFO()
459 /* }}} */
460 
461 /* {{{ xmlwriter_functions */
462 static const zend_function_entry xmlwriter_functions[] = {
463 	PHP_FE(xmlwriter_open_uri,			arginfo_xmlwriter_open_uri)
464 	PHP_FE(xmlwriter_open_memory,		arginfo_xmlwriter_void)
465 	PHP_FE(xmlwriter_set_indent,		arginfo_xmlwriter_set_indent)
466 	PHP_FE(xmlwriter_set_indent_string, arginfo_xmlwriter_set_indent_string)
467 	PHP_FE(xmlwriter_start_comment,		arginfo_xmlwriter_resource)
468 	PHP_FE(xmlwriter_end_comment,		arginfo_xmlwriter_resource)
469 	PHP_FE(xmlwriter_start_attribute,	arginfo_xmlwriter_start_attribute)
470 	PHP_FE(xmlwriter_end_attribute,		arginfo_xmlwriter_resource)
471 	PHP_FE(xmlwriter_write_attribute,	arginfo_xmlwriter_write_attribute)
472 	PHP_FE(xmlwriter_start_attribute_ns,arginfo_xmlwriter_start_attribute_ns)
473 	PHP_FE(xmlwriter_write_attribute_ns,arginfo_xmlwriter_write_attribute_ns)
474 	PHP_FE(xmlwriter_start_element,		arginfo_xmlwriter_start_element)
475 	PHP_FE(xmlwriter_end_element,		arginfo_xmlwriter_resource)
476 	PHP_FE(xmlwriter_full_end_element,	arginfo_xmlwriter_resource)
477 	PHP_FE(xmlwriter_start_element_ns,	arginfo_xmlwriter_start_element_ns)
478 	PHP_FE(xmlwriter_write_element,		arginfo_xmlwriter_write_element)
479 	PHP_FE(xmlwriter_write_element_ns,	arginfo_xmlwriter_write_element_ns)
480 	PHP_FE(xmlwriter_start_pi,			arginfo_xmlwriter_start_pi)
481 	PHP_FE(xmlwriter_end_pi,			arginfo_xmlwriter_resource)
482 	PHP_FE(xmlwriter_write_pi,			arginfo_xmlwriter_write_pi)
483 	PHP_FE(xmlwriter_start_cdata,		arginfo_xmlwriter_resource)
484 	PHP_FE(xmlwriter_end_cdata,			arginfo_xmlwriter_resource)
485 	PHP_FE(xmlwriter_write_cdata,		arginfo_xmlwriter_write_cdata)
486 	PHP_FE(xmlwriter_text,				arginfo_xmlwriter_text)
487 	PHP_FE(xmlwriter_write_raw,			arginfo_xmlwriter_write_raw)
488 	PHP_FE(xmlwriter_start_document,	arginfo_xmlwriter_start_document)
489 	PHP_FE(xmlwriter_end_document,		arginfo_xmlwriter_resource)
490 	PHP_FE(xmlwriter_write_comment,		arginfo_xmlwriter_write_comment)
491 	PHP_FE(xmlwriter_start_dtd,			arginfo_xmlwriter_start_dtd)
492 	PHP_FE(xmlwriter_end_dtd,			arginfo_xmlwriter_resource)
493 	PHP_FE(xmlwriter_write_dtd,			arginfo_xmlwriter_write_dtd)
494 	PHP_FE(xmlwriter_start_dtd_element,	arginfo_xmlwriter_start_dtd_element)
495 	PHP_FE(xmlwriter_end_dtd_element,	arginfo_xmlwriter_resource)
496 	PHP_FE(xmlwriter_write_dtd_element,	arginfo_xmlwriter_write_dtd_element)
497 	PHP_FE(xmlwriter_start_dtd_attlist,	arginfo_xmlwriter_start_dtd_attlist)
498 	PHP_FE(xmlwriter_end_dtd_attlist,	arginfo_xmlwriter_resource)
499 	PHP_FE(xmlwriter_write_dtd_attlist,	arginfo_xmlwriter_write_dtd_attlist)
500 	PHP_FE(xmlwriter_start_dtd_entity,	arginfo_xmlwriter_start_dtd_entity)
501 	PHP_FE(xmlwriter_end_dtd_entity,	arginfo_xmlwriter_resource)
502 	PHP_FE(xmlwriter_write_dtd_entity,	arginfo_xmlwriter_write_dtd_entity)
503 	PHP_FE(xmlwriter_output_memory,		arginfo_xmlwriter_output_memory)
504 	PHP_FE(xmlwriter_flush,				arginfo_xmlwriter_flush)
505 	PHP_FE_END
506 };
507 /* }}} */
508 
509 /* {{{ xmlwriter_class_functions */
510 static const zend_function_entry xmlwriter_class_functions[] = {
511 	PHP_ME_MAPPING(openUri,		xmlwriter_open_uri,		arginfo_xmlwriter_open_uri, 0)
512 	PHP_ME_MAPPING(openMemory,	xmlwriter_open_memory, 	arginfo_xmlwriter_void, 0)
513 	PHP_ME_MAPPING(setIndent,	xmlwriter_set_indent,	arginfo_xmlwriter_method_set_indent, 0)
514 	PHP_ME_MAPPING(setIndentString,	xmlwriter_set_indent_string, arginfo_xmlwriter_method_set_indent_string, 0)
515 	PHP_ME_MAPPING(startComment,	xmlwriter_start_comment,	arginfo_xmlwriter_void, 0)
516 	PHP_ME_MAPPING(endComment,		xmlwriter_end_comment,		arginfo_xmlwriter_void, 0)
517 	PHP_ME_MAPPING(startAttribute,	xmlwriter_start_attribute,	arginfo_xmlwriter_method_start_attribute, 0)
518 	PHP_ME_MAPPING(endAttribute,	xmlwriter_end_attribute,	arginfo_xmlwriter_void, 0)
519 	PHP_ME_MAPPING(writeAttribute,	xmlwriter_write_attribute,	arginfo_xmlwriter_method_write_attribute, 0)
520 	PHP_ME_MAPPING(startAttributeNs,	xmlwriter_start_attribute_ns,arginfo_xmlwriter_method_start_attribute_ns, 0)
521 	PHP_ME_MAPPING(writeAttributeNs,	xmlwriter_write_attribute_ns,arginfo_xmlwriter_method_write_attribute_ns, 0)
522 	PHP_ME_MAPPING(startElement,	xmlwriter_start_element,	arginfo_xmlwriter_method_start_element, 0)
523 	PHP_ME_MAPPING(endElement,		xmlwriter_end_element,		arginfo_xmlwriter_void, 0)
524 	PHP_ME_MAPPING(fullEndElement,	xmlwriter_full_end_element,	arginfo_xmlwriter_void, 0)
525 	PHP_ME_MAPPING(startElementNs,	xmlwriter_start_element_ns,	arginfo_xmlwriter_method_start_element_ns, 0)
526 	PHP_ME_MAPPING(writeElement,	xmlwriter_write_element,	arginfo_xmlwriter_method_write_element, 0)
527 	PHP_ME_MAPPING(writeElementNs,	xmlwriter_write_element_ns,	arginfo_xmlwriter_method_write_element_ns, 0)
528 	PHP_ME_MAPPING(startPi,			xmlwriter_start_pi,			arginfo_xmlwriter_method_start_pi, 0)
529 	PHP_ME_MAPPING(endPi,			xmlwriter_end_pi,			arginfo_xmlwriter_void, 0)
530 	PHP_ME_MAPPING(writePi,			xmlwriter_write_pi,			arginfo_xmlwriter_method_write_pi, 0)
531 	PHP_ME_MAPPING(startCdata,		xmlwriter_start_cdata,		arginfo_xmlwriter_void, 0)
532 	PHP_ME_MAPPING(endCdata,		xmlwriter_end_cdata,		arginfo_xmlwriter_void, 0)
533 	PHP_ME_MAPPING(writeCdata,		xmlwriter_write_cdata,		arginfo_xmlwriter_method_write_cdata, 0)
534 	PHP_ME_MAPPING(text,			xmlwriter_text,				arginfo_xmlwriter_method_text, 0)
535 	PHP_ME_MAPPING(writeRaw,		xmlwriter_write_raw,		arginfo_xmlwriter_method_write_raw, 0)
536 	PHP_ME_MAPPING(startDocument,	xmlwriter_start_document,	arginfo_xmlwriter_method_start_document, 0)
537 	PHP_ME_MAPPING(endDocument,		xmlwriter_end_document,		arginfo_xmlwriter_void, 0)
538 	PHP_ME_MAPPING(writeComment,	xmlwriter_write_comment,	arginfo_xmlwriter_method_write_comment, 0)
539 	PHP_ME_MAPPING(startDtd,		xmlwriter_start_dtd,		arginfo_xmlwriter_method_start_dtd, 0)
540 	PHP_ME_MAPPING(endDtd,			xmlwriter_end_dtd,			arginfo_xmlwriter_void, 0)
541 	PHP_ME_MAPPING(writeDtd,		xmlwriter_write_dtd,		arginfo_xmlwriter_method_write_dtd, 0)
542 	PHP_ME_MAPPING(startDtdElement,	xmlwriter_start_dtd_element,arginfo_xmlwriter_method_start_dtd_element, 0)
543 	PHP_ME_MAPPING(endDtdElement,	xmlwriter_end_dtd_element,	arginfo_xmlwriter_void, 0)
544 	PHP_ME_MAPPING(writeDtdElement,	xmlwriter_write_dtd_element,	arginfo_xmlwriter_method_write_dtd_element, 0)
545 	PHP_ME_MAPPING(startDtdAttlist,	xmlwriter_start_dtd_attlist,	arginfo_xmlwriter_method_start_dtd_attlist, 0)
546 	PHP_ME_MAPPING(endDtdAttlist,	xmlwriter_end_dtd_attlist,	arginfo_xmlwriter_void, 0)
547 	PHP_ME_MAPPING(writeDtdAttlist,	xmlwriter_write_dtd_attlist,	arginfo_xmlwriter_method_write_dtd_attlist, 0)
548 	PHP_ME_MAPPING(startDtdEntity,	xmlwriter_start_dtd_entity,	arginfo_xmlwriter_method_start_dtd_entity, 0)
549 	PHP_ME_MAPPING(endDtdEntity,	xmlwriter_end_dtd_entity,	arginfo_xmlwriter_void, 0)
550 	PHP_ME_MAPPING(writeDtdEntity,	xmlwriter_write_dtd_entity,	arginfo_xmlwriter_method_write_dtd_entity, 0)
551 	PHP_ME_MAPPING(outputMemory,	xmlwriter_output_memory,	arginfo_xmlwriter_method_output_memory, 0)
552 	PHP_ME_MAPPING(flush,			xmlwriter_flush,			arginfo_xmlwriter_method_flush, 0)
553 	PHP_FE_END
554 };
555 /* }}} */
556 
557 /* {{{ function prototypes */
558 static PHP_MINIT_FUNCTION(xmlwriter);
559 static PHP_MSHUTDOWN_FUNCTION(xmlwriter);
560 static PHP_MINFO_FUNCTION(xmlwriter);
561 
562 static int le_xmlwriter;
563 /* }}} */
564 
565 /* _xmlwriter_get_valid_file_path should be made a
566 	common function in libxml extension as code is common to a few xml extensions */
567 /* {{{ _xmlwriter_get_valid_file_path */
_xmlwriter_get_valid_file_path(char * source,char * resolved_path,int resolved_path_len)568 static char *_xmlwriter_get_valid_file_path(char *source, char *resolved_path, int resolved_path_len ) {
569 	xmlURI *uri;
570 	xmlChar *escsource;
571 	char *file_dest;
572 	int isFileUri = 0;
573 
574 	uri = xmlCreateURI();
575 	escsource = xmlURIEscapeStr((xmlChar *)source, (xmlChar *) ":");
576 	xmlParseURIReference(uri, (char *)escsource);
577 	xmlFree(escsource);
578 
579 	if (uri->scheme != NULL) {
580 		/* absolute file uris - libxml only supports localhost or empty host */
581 		if (strncasecmp(source, "file:///", 8) == 0) {
582 			if (source[sizeof("file:///") - 1] == '\0') {
583 				xmlFreeURI(uri);
584 				return NULL;
585 			}
586 			isFileUri = 1;
587 #ifdef PHP_WIN32
588 			source += 8;
589 #else
590 			source += 7;
591 #endif
592 		} else if (strncasecmp(source, "file://localhost/",17) == 0) {
593 			if (source[sizeof("file://localhost/") - 1] == '\0') {
594 				xmlFreeURI(uri);
595 				return NULL;
596 			}
597 
598 			isFileUri = 1;
599 #ifdef PHP_WIN32
600 			source += 17;
601 #else
602 			source += 16;
603 #endif
604 		}
605 	}
606 
607 	if ((uri->scheme == NULL || isFileUri)) {
608 		char file_dirname[MAXPATHLEN];
609 		size_t dir_len;
610 
611 		if (!VCWD_REALPATH(source, resolved_path) && !expand_filepath(source, resolved_path)) {
612 			xmlFreeURI(uri);
613 			return NULL;
614 		}
615 
616 		memcpy(file_dirname, source, strlen(source));
617 		dir_len = php_dirname(file_dirname, strlen(source));
618 
619 		if (dir_len > 0) {
620 			zend_stat_t buf;
621 			if (php_sys_stat(file_dirname, &buf) != 0) {
622 				xmlFreeURI(uri);
623 				return NULL;
624 			}
625 		}
626 
627 		file_dest = resolved_path;
628 	} else {
629 		file_dest = source;
630 	}
631 
632 	xmlFreeURI(uri);
633 
634 	return file_dest;
635 }
636 /* }}} */
637 
638 /* {{{ xmlwriter_module_entry
639  */
640 zend_module_entry xmlwriter_module_entry = {
641 	STANDARD_MODULE_HEADER,
642 	"xmlwriter",
643 	xmlwriter_functions,
644 	PHP_MINIT(xmlwriter),
645 	PHP_MSHUTDOWN(xmlwriter),
646 	NULL,
647 	NULL,
648 	PHP_MINFO(xmlwriter),
649 	PHP_XMLWRITER_VERSION,
650 	STANDARD_MODULE_PROPERTIES
651 };
652 /* }}} */
653 
654 #ifdef COMPILE_DL_XMLWRITER
ZEND_GET_MODULE(xmlwriter)655 ZEND_GET_MODULE(xmlwriter)
656 #endif
657 
658 /* {{{ xmlwriter_objects_clone
659 static void xmlwriter_objects_clone(void *object, void **object_clone)
660 {
661 	TODO
662 }
663 }}} */
664 
665 /* {{{ xmlwriter_dtor */
666 static void xmlwriter_dtor(zend_resource *rsrc) {
667 	xmlwriter_object *intern;
668 
669 	intern = (xmlwriter_object *) rsrc->ptr;
670 	xmlwriter_free_resource_ptr(intern);
671 }
672 /* }}} */
673 
php_xmlwriter_string_arg(INTERNAL_FUNCTION_PARAMETERS,xmlwriter_read_one_char_t internal_function,char * err_string)674 static void php_xmlwriter_string_arg(INTERNAL_FUNCTION_PARAMETERS, xmlwriter_read_one_char_t internal_function, char *err_string)
675 {
676 	zval *pind;
677 	xmlwriter_object *intern;
678 	xmlTextWriterPtr ptr;
679 	char *name;
680 	size_t name_len;
681 	int retval;
682 
683 	zval *self = getThis();
684 
685 	if (self) {
686 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &name_len) == FAILURE) {
687 			return;
688 		}
689 		XMLWRITER_FROM_OBJECT(intern, self);
690 	} else {
691 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs", &pind, &name, &name_len) == FAILURE) {
692 			return;
693 		}
694 
695 		if ((intern = (xmlwriter_object *)zend_fetch_resource(Z_RES_P(pind), "XMLWriter", le_xmlwriter)) == NULL) {
696 			RETURN_FALSE;
697 		}
698 	}
699 
700 	if (err_string != NULL) {
701 		XMLW_NAME_CHK(err_string);
702 	}
703 
704 	ptr = intern->ptr;
705 
706 	if (ptr) {
707 		retval = internal_function(ptr, (xmlChar *) name);
708 		if (retval != -1) {
709 			RETURN_TRUE;
710 		}
711 	}
712 
713 	RETURN_FALSE;
714 }
715 
php_xmlwriter_end(INTERNAL_FUNCTION_PARAMETERS,xmlwriter_read_int_t internal_function)716 static void php_xmlwriter_end(INTERNAL_FUNCTION_PARAMETERS, xmlwriter_read_int_t internal_function)
717 {
718 	zval *pind;
719 	xmlwriter_object *intern;
720 	xmlTextWriterPtr ptr;
721 	int retval;
722 	zval *self = getThis();
723 
724 	if (self) {
725 		XMLWRITER_FROM_OBJECT(intern, self);
726 		if (zend_parse_parameters_none() == FAILURE) {
727 			return;
728 		}
729 	} else {
730 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pind) == FAILURE) {
731 			return;
732 		}
733 		if ((intern = (xmlwriter_object *)zend_fetch_resource(Z_RES_P(pind), "XMLWriter", le_xmlwriter)) == NULL) {
734 			RETURN_FALSE;
735 		}
736 	}
737 
738 	ptr = intern->ptr;
739 
740 	if (ptr) {
741 		retval = internal_function(ptr);
742 		if (retval != -1) {
743 			RETURN_TRUE;
744 		}
745 	}
746 
747 	RETURN_FALSE;
748 }
749 
750 /* {{{ proto bool xmlwriter_set_indent(resource xmlwriter, bool indent)
751 Toggle indentation on/off - returns FALSE on error */
PHP_FUNCTION(xmlwriter_set_indent)752 static PHP_FUNCTION(xmlwriter_set_indent)
753 {
754 	zval *pind;
755 	xmlwriter_object *intern;
756 	xmlTextWriterPtr ptr;
757 	int retval;
758 	zend_bool indent;
759 
760 	zval *self = getThis();
761 
762 	if (self) {
763 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "b", &indent) == FAILURE) {
764 			return;
765 		}
766 		XMLWRITER_FROM_OBJECT(intern, self);
767 	} else {
768 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "rb", &pind, &indent) == FAILURE) {
769 			return;
770 		}
771 		if ((intern = (xmlwriter_object *)zend_fetch_resource(Z_RES_P(pind), "XMLWriter", le_xmlwriter)) == NULL) {
772 			RETURN_FALSE;
773 		}
774 	}
775 
776 
777 	ptr = intern->ptr;
778 	if (ptr) {
779 		retval = xmlTextWriterSetIndent(ptr, indent);
780 		if (retval == 0) {
781 			RETURN_TRUE;
782 		}
783 	}
784 
785 	RETURN_FALSE;
786 }
787 /* }}} */
788 
789 /* {{{ proto bool xmlwriter_set_indent_string(resource xmlwriter, string indentString)
790 Set string used for indenting - returns FALSE on error */
PHP_FUNCTION(xmlwriter_set_indent_string)791 static PHP_FUNCTION(xmlwriter_set_indent_string)
792 {
793 	php_xmlwriter_string_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterSetIndentString, NULL);
794 }
795 /* }}} */
796 
797 /* {{{ proto bool xmlwriter_start_attribute(resource xmlwriter, string name)
798 Create start attribute - returns FALSE on error */
PHP_FUNCTION(xmlwriter_start_attribute)799 static PHP_FUNCTION(xmlwriter_start_attribute)
800 {
801 	php_xmlwriter_string_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterStartAttribute, "Invalid Attribute Name");
802 }
803 /* }}} */
804 
805 /* {{{ proto bool xmlwriter_end_attribute(resource xmlwriter)
806 End attribute - returns FALSE on error */
PHP_FUNCTION(xmlwriter_end_attribute)807 static PHP_FUNCTION(xmlwriter_end_attribute)
808 {
809 	php_xmlwriter_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterEndAttribute);
810 }
811 /* }}} */
812 
813 /* {{{ proto bool xmlwriter_start_attribute_ns(resource xmlwriter, string prefix, string name, string uri)
814 Create start namespaced attribute - returns FALSE on error */
PHP_FUNCTION(xmlwriter_start_attribute_ns)815 static PHP_FUNCTION(xmlwriter_start_attribute_ns)
816 {
817 	zval *pind;
818 	xmlwriter_object *intern;
819 	xmlTextWriterPtr ptr;
820 	char *name, *prefix, *uri;
821 	size_t name_len, prefix_len, uri_len;
822 	int retval;
823 	zval *self = getThis();
824 
825 	if (self) {
826 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss!",
827 			&prefix, &prefix_len, &name, &name_len, &uri, &uri_len) == FAILURE) {
828 			return;
829 		}
830 		XMLWRITER_FROM_OBJECT(intern, self);
831 	} else {
832 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "rsss!", &pind,
833 			&prefix, &prefix_len, &name, &name_len, &uri, &uri_len) == FAILURE) {
834 			return;
835 		}
836 		if ((intern = (xmlwriter_object *)zend_fetch_resource(Z_RES_P(pind), "XMLWriter", le_xmlwriter)) == NULL) {
837 			RETURN_FALSE;
838 		}
839 	}
840 
841 	XMLW_NAME_CHK("Invalid Attribute Name");
842 
843 	ptr = intern->ptr;
844 
845 	if (ptr) {
846 		retval = xmlTextWriterStartAttributeNS(ptr, (xmlChar *)prefix, (xmlChar *)name, (xmlChar *)uri);
847 		if (retval != -1) {
848 			RETURN_TRUE;
849 		}
850 	}
851 
852 	RETURN_FALSE;
853 }
854 /* }}} */
855 
856 /* {{{ proto bool xmlwriter_write_attribute(resource xmlwriter, string name, string content)
857 Write full attribute - returns FALSE on error */
PHP_FUNCTION(xmlwriter_write_attribute)858 static PHP_FUNCTION(xmlwriter_write_attribute)
859 {
860 	zval *pind;
861 	xmlwriter_object *intern;
862 	xmlTextWriterPtr ptr;
863 	char *name, *content;
864 	size_t name_len, content_len;
865 	int retval;
866 	zval *self = getThis();
867 
868 	if (self) {
869 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss",
870 			&name, &name_len, &content, &content_len) == FAILURE) {
871 			return;
872 		}
873 		XMLWRITER_FROM_OBJECT(intern, self);
874 	} else {
875 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "rss", &pind,
876 			&name, &name_len, &content, &content_len) == FAILURE) {
877 			return;
878 		}
879 		if ((intern = (xmlwriter_object *)zend_fetch_resource(Z_RES_P(pind), "XMLWriter", le_xmlwriter)) == NULL) {
880 			RETURN_FALSE;
881 		}
882 	}
883 
884 	XMLW_NAME_CHK("Invalid Attribute Name");
885 
886 	ptr = intern->ptr;
887 
888 	if (ptr) {
889 		retval = xmlTextWriterWriteAttribute(ptr, (xmlChar *)name, (xmlChar *)content);
890 		if (retval != -1) {
891 			RETURN_TRUE;
892 		}
893 	}
894 
895 	RETURN_FALSE;
896 }
897 /* }}} */
898 
899 /* {{{ proto bool xmlwriter_write_attribute_ns(resource xmlwriter, string prefix, string name, string uri, string content)
900 Write full namespaced attribute - returns FALSE on error */
PHP_FUNCTION(xmlwriter_write_attribute_ns)901 static PHP_FUNCTION(xmlwriter_write_attribute_ns)
902 {
903 	zval *pind;
904 	xmlwriter_object *intern;
905 	xmlTextWriterPtr ptr;
906 	char *name, *prefix, *uri, *content;
907 	size_t name_len, prefix_len, uri_len, content_len;
908 	int retval;
909 
910 	zval *self = getThis();
911 
912 	if (self) {
913 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss!s",
914 			&prefix, &prefix_len, &name, &name_len, &uri, &uri_len, &content, &content_len) == FAILURE) {
915 			return;
916 		}
917 		XMLWRITER_FROM_OBJECT(intern, self);
918 	} else {
919 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "rsss!s", &pind,
920 			&prefix, &prefix_len, &name, &name_len, &uri, &uri_len, &content, &content_len) == FAILURE) {
921 			return;
922 		}
923 		if ((intern = (xmlwriter_object *)zend_fetch_resource(Z_RES_P(pind), "XMLWriter", le_xmlwriter)) == NULL) {
924 			RETURN_FALSE;
925 		}
926 	}
927 
928 	XMLW_NAME_CHK("Invalid Attribute Name");
929 
930 	ptr = intern->ptr;
931 
932 	if (ptr) {
933 		retval = xmlTextWriterWriteAttributeNS(ptr, (xmlChar *)prefix, (xmlChar *)name, (xmlChar *)uri, (xmlChar *)content);
934 		if (retval != -1) {
935 			RETURN_TRUE;
936 		}
937 	}
938 
939 	RETURN_FALSE;
940 }
941 /* }}} */
942 
943 /* {{{ proto bool xmlwriter_start_element(resource xmlwriter, string name)
944 Create start element tag - returns FALSE on error */
PHP_FUNCTION(xmlwriter_start_element)945 static PHP_FUNCTION(xmlwriter_start_element)
946 {
947 	php_xmlwriter_string_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterStartElement, "Invalid Element Name");
948 }
949 /* }}} */
950 
951 /* {{{ proto bool xmlwriter_start_element_ns(resource xmlwriter, string prefix, string name, string uri)
952 Create start namespaced element tag - returns FALSE on error */
PHP_FUNCTION(xmlwriter_start_element_ns)953 static PHP_FUNCTION(xmlwriter_start_element_ns)
954 {
955 	zval *pind;
956 	xmlwriter_object *intern;
957 	xmlTextWriterPtr ptr;
958 	char *name, *prefix, *uri;
959 	size_t name_len, prefix_len, uri_len;
960 	int retval;
961 	zval *self = getThis();
962 
963 	if (self) {
964 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "s!ss!",
965 			&prefix, &prefix_len, &name, &name_len, &uri, &uri_len) == FAILURE) {
966 			return;
967 		}
968 		XMLWRITER_FROM_OBJECT(intern, self);
969 	} else {
970 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs!ss!", &pind,
971 			&prefix, &prefix_len, &name, &name_len, &uri, &uri_len) == FAILURE) {
972 			return;
973 		}
974 		if ((intern = (xmlwriter_object *)zend_fetch_resource(Z_RES_P(pind), "XMLWriter", le_xmlwriter)) == NULL) {
975 			RETURN_FALSE;
976 		}
977 	}
978 
979 	XMLW_NAME_CHK("Invalid Element Name");
980 
981 	ptr = intern->ptr;
982 
983 	if (ptr) {
984 		retval = xmlTextWriterStartElementNS(ptr, (xmlChar *)prefix, (xmlChar *)name, (xmlChar *)uri);
985 		if (retval != -1) {
986 			RETURN_TRUE;
987 		}
988 
989 	}
990 
991 	RETURN_FALSE;
992 }
993 /* }}} */
994 
995 /* {{{ proto bool xmlwriter_end_element(resource xmlwriter)
996 End current element - returns FALSE on error */
PHP_FUNCTION(xmlwriter_end_element)997 static PHP_FUNCTION(xmlwriter_end_element)
998 {
999 	php_xmlwriter_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterEndElement);
1000 }
1001 /* }}} */
1002 
1003 /* {{{ proto bool xmlwriter_full_end_element(resource xmlwriter)
1004 End current element - returns FALSE on error */
PHP_FUNCTION(xmlwriter_full_end_element)1005 static PHP_FUNCTION(xmlwriter_full_end_element)
1006 {
1007 	php_xmlwriter_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterFullEndElement);
1008 }
1009 /* }}} */
1010 
1011 /* {{{ proto bool xmlwriter_write_element(resource xmlwriter, string name[, string content])
1012 Write full element tag - returns FALSE on error */
PHP_FUNCTION(xmlwriter_write_element)1013 static PHP_FUNCTION(xmlwriter_write_element)
1014 {
1015 	zval *pind;
1016 	xmlwriter_object *intern;
1017 	xmlTextWriterPtr ptr;
1018 	char *name, *content = NULL;
1019 	size_t name_len, content_len;
1020 	int retval;
1021 	zval *self = getThis();
1022 
1023 	if (self) {
1024 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|s!",
1025 			&name, &name_len, &content, &content_len) == FAILURE) {
1026 			return;
1027 		}
1028 		XMLWRITER_FROM_OBJECT(intern, self);
1029 	} else {
1030 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs|s!", &pind,
1031 			&name, &name_len, &content, &content_len) == FAILURE) {
1032 			return;
1033 		}
1034 		if ((intern = (xmlwriter_object *)zend_fetch_resource(Z_RES_P(pind), "XMLWriter", le_xmlwriter)) == NULL) {
1035 			RETURN_FALSE;
1036 		}
1037 	}
1038 
1039 	XMLW_NAME_CHK("Invalid Element Name");
1040 
1041 	ptr = intern->ptr;
1042 
1043 	if (ptr) {
1044 		if (!content) {
1045 			retval = xmlTextWriterStartElement(ptr, (xmlChar *)name);
1046             if (retval == -1) {
1047                 RETURN_FALSE;
1048             }
1049 			xmlTextWriterEndElement(ptr);
1050             if (retval == -1) {
1051                 RETURN_FALSE;
1052             }
1053 		} else {
1054 			retval = xmlTextWriterWriteElement(ptr, (xmlChar *)name, (xmlChar *)content);
1055 		}
1056 		if (retval != -1) {
1057 			RETURN_TRUE;
1058 		}
1059 	}
1060 
1061 	RETURN_FALSE;
1062 }
1063 /* }}} */
1064 
1065 /* {{{ proto bool xmlwriter_write_element_ns(resource xmlwriter, string prefix, string name, string uri[, string content])
1066 Write full namesapced element tag - returns FALSE on error */
PHP_FUNCTION(xmlwriter_write_element_ns)1067 static PHP_FUNCTION(xmlwriter_write_element_ns)
1068 {
1069 	zval *pind;
1070 	xmlwriter_object *intern;
1071 	xmlTextWriterPtr ptr;
1072 	char *name, *prefix, *uri, *content = NULL;
1073 	size_t name_len, prefix_len, uri_len, content_len;
1074 	int retval;
1075 	zval *self = getThis();
1076 
1077 	if (self) {
1078 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "s!ss!|s!",
1079 			&prefix, &prefix_len, &name, &name_len, &uri, &uri_len, &content, &content_len) == FAILURE) {
1080 			return;
1081 		}
1082 		XMLWRITER_FROM_OBJECT(intern, self);
1083 	} else {
1084 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs!ss!|s!", &pind,
1085 			&prefix, &prefix_len, &name, &name_len, &uri, &uri_len, &content, &content_len) == FAILURE) {
1086 			return;
1087 		}
1088 		if ((intern = (xmlwriter_object *)zend_fetch_resource(Z_RES_P(pind), "XMLWriter", le_xmlwriter)) == NULL) {
1089 			RETURN_FALSE;
1090 		}
1091 	}
1092 
1093 	XMLW_NAME_CHK("Invalid Element Name");
1094 
1095 	ptr = intern->ptr;
1096 
1097 	if (ptr) {
1098 		if (!content) {
1099 			retval = xmlTextWriterStartElementNS(ptr,(xmlChar *)prefix, (xmlChar *)name, (xmlChar *)uri);
1100             if (retval == -1) {
1101                 RETURN_FALSE;
1102             }
1103 			retval = xmlTextWriterEndElement(ptr);
1104             if (retval == -1) {
1105                 RETURN_FALSE;
1106             }
1107 		} else {
1108 			retval = xmlTextWriterWriteElementNS(ptr, (xmlChar *)prefix, (xmlChar *)name, (xmlChar *)uri, (xmlChar *)content);
1109 		}
1110 		if (retval != -1) {
1111 			RETURN_TRUE;
1112 		}
1113 	}
1114 
1115 	RETURN_FALSE;
1116 }
1117 /* }}} */
1118 
1119 /* {{{ proto bool xmlwriter_start_pi(resource xmlwriter, string target)
1120 Create start PI tag - returns FALSE on error */
PHP_FUNCTION(xmlwriter_start_pi)1121 static PHP_FUNCTION(xmlwriter_start_pi)
1122 {
1123 	php_xmlwriter_string_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterStartPI, "Invalid PI Target");
1124 }
1125 /* }}} */
1126 
1127 /* {{{ proto bool xmlwriter_end_pi(resource xmlwriter)
1128 End current PI - returns FALSE on error */
PHP_FUNCTION(xmlwriter_end_pi)1129 static PHP_FUNCTION(xmlwriter_end_pi)
1130 {
1131 	php_xmlwriter_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterEndPI);
1132 }
1133 /* }}} */
1134 
1135 /* {{{ proto bool xmlwriter_write_pi(resource xmlwriter, string target, string content)
1136 Write full PI tag - returns FALSE on error */
PHP_FUNCTION(xmlwriter_write_pi)1137 static PHP_FUNCTION(xmlwriter_write_pi)
1138 {
1139 	zval *pind;
1140 	xmlwriter_object *intern;
1141 	xmlTextWriterPtr ptr;
1142 	char *name, *content;
1143 	size_t name_len, content_len;
1144 	int retval;
1145 
1146 	zval *self = getThis();
1147 
1148 	if (self) {
1149 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss",
1150 			&name, &name_len, &content, &content_len) == FAILURE) {
1151 			return;
1152 		}
1153 		XMLWRITER_FROM_OBJECT(intern, self);
1154 	} else {
1155 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "rss", &pind,
1156 			&name, &name_len, &content, &content_len) == FAILURE) {
1157 			return;
1158 		}
1159 		if ((intern = (xmlwriter_object *)zend_fetch_resource(Z_RES_P(pind), "XMLWriter", le_xmlwriter)) == NULL) {
1160 			RETURN_FALSE;
1161 		}
1162 	}
1163 
1164 	XMLW_NAME_CHK("Invalid PI Target");
1165 
1166 	ptr = intern->ptr;
1167 
1168 	if (ptr) {
1169 		retval = xmlTextWriterWritePI(ptr, (xmlChar *)name, (xmlChar *)content);
1170 		if (retval != -1) {
1171 			RETURN_TRUE;
1172 		}
1173 	}
1174 
1175 	RETURN_FALSE;
1176 }
1177 /* }}} */
1178 
1179 /* {{{ proto bool xmlwriter_start_cdata(resource xmlwriter)
1180 Create start CDATA tag - returns FALSE on error */
PHP_FUNCTION(xmlwriter_start_cdata)1181 static PHP_FUNCTION(xmlwriter_start_cdata)
1182 {
1183 	zval *pind;
1184 	xmlwriter_object *intern;
1185 	xmlTextWriterPtr ptr;
1186 	int retval;
1187 	zval *self = getThis();
1188 
1189 	if (self) {
1190 		if (zend_parse_parameters_none() == FAILURE) {
1191 			return;
1192 		}
1193 		XMLWRITER_FROM_OBJECT(intern, self);
1194 	} else {
1195 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pind) == FAILURE) {
1196 			return;
1197 		}
1198 		if ((intern = (xmlwriter_object *)zend_fetch_resource(Z_RES_P(pind), "XMLWriter", le_xmlwriter)) == NULL) {
1199 			RETURN_FALSE;
1200 		}
1201 	}
1202 
1203 	ptr = intern->ptr;
1204 
1205 	if (ptr) {
1206 		retval = xmlTextWriterStartCDATA(ptr);
1207 		if (retval != -1) {
1208 			RETURN_TRUE;
1209 		}
1210 	}
1211 
1212 	RETURN_FALSE;
1213 }
1214 /* }}} */
1215 
1216 /* {{{ proto bool xmlwriter_end_cdata(resource xmlwriter)
1217 End current CDATA - returns FALSE on error */
PHP_FUNCTION(xmlwriter_end_cdata)1218 static PHP_FUNCTION(xmlwriter_end_cdata)
1219 {
1220 	php_xmlwriter_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterEndCDATA);
1221 }
1222 /* }}} */
1223 
1224 /* {{{ proto bool xmlwriter_write_cdata(resource xmlwriter, string content)
1225 Write full CDATA tag - returns FALSE on error */
PHP_FUNCTION(xmlwriter_write_cdata)1226 static PHP_FUNCTION(xmlwriter_write_cdata)
1227 {
1228 	php_xmlwriter_string_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterWriteCDATA, NULL);
1229 }
1230 /* }}} */
1231 
1232 /* {{{ proto bool xmlwriter_write_raw(resource xmlwriter, string content)
1233 Write text - returns FALSE on error */
PHP_FUNCTION(xmlwriter_write_raw)1234 static PHP_FUNCTION(xmlwriter_write_raw)
1235 {
1236 	php_xmlwriter_string_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterWriteRaw, NULL);
1237 }
1238 /* }}} */
1239 
1240 /* {{{ proto bool xmlwriter_text(resource xmlwriter, string content)
1241 Write text - returns FALSE on error */
PHP_FUNCTION(xmlwriter_text)1242 static PHP_FUNCTION(xmlwriter_text)
1243 {
1244 	php_xmlwriter_string_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterWriteString, NULL);
1245 }
1246 /* }}} */
1247 
1248 /* {{{ proto bool xmlwriter_start_comment(resource xmlwriter)
1249 Create start comment - returns FALSE on error */
PHP_FUNCTION(xmlwriter_start_comment)1250 static PHP_FUNCTION(xmlwriter_start_comment)
1251 {
1252 	zval *pind;
1253 	xmlwriter_object *intern;
1254 	xmlTextWriterPtr ptr;
1255 	int retval;
1256 	zval *self = getThis();
1257 
1258 	if (self) {
1259 		if (zend_parse_parameters_none() == FAILURE) {
1260 			return;
1261 		}
1262 		XMLWRITER_FROM_OBJECT(intern, self);
1263 	} else {
1264 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pind) == FAILURE) {
1265 			return;
1266 		}
1267 		if ((intern = (xmlwriter_object *)zend_fetch_resource(Z_RES_P(pind), "XMLWriter", le_xmlwriter)) == NULL) {
1268 			RETURN_FALSE;
1269 		}
1270 	}
1271 
1272 	ptr = intern->ptr;
1273 
1274 	if (ptr) {
1275 		retval = xmlTextWriterStartComment(ptr);
1276 		if (retval != -1) {
1277 			RETURN_TRUE;
1278 		}
1279 	}
1280 
1281 	RETURN_FALSE;
1282 }
1283 /* }}} */
1284 
1285 /* {{{ proto bool xmlwriter_end_comment(resource xmlwriter)
1286 Create end comment - returns FALSE on error */
PHP_FUNCTION(xmlwriter_end_comment)1287 static PHP_FUNCTION(xmlwriter_end_comment)
1288 {
1289 	php_xmlwriter_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterEndComment);
1290 }
1291 /* }}} */
1292 
1293 /* {{{ proto bool xmlwriter_write_comment(resource xmlwriter, string content)
1294 Write full comment tag - returns FALSE on error */
PHP_FUNCTION(xmlwriter_write_comment)1295 static PHP_FUNCTION(xmlwriter_write_comment)
1296 {
1297 	php_xmlwriter_string_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterWriteComment, NULL);
1298 }
1299 /* }}} */
1300 
1301 /* {{{ proto bool xmlwriter_start_document(resource xmlwriter, string version, string encoding, string standalone)
1302 Create document tag - returns FALSE on error */
PHP_FUNCTION(xmlwriter_start_document)1303 static PHP_FUNCTION(xmlwriter_start_document)
1304 {
1305 	zval *pind;
1306 	xmlwriter_object *intern;
1307 	xmlTextWriterPtr ptr;
1308 	char *version = NULL, *enc = NULL, *alone = NULL;
1309 	size_t version_len, enc_len, alone_len;
1310 	int retval;
1311 
1312 	zval *self = getThis();
1313 
1314 	if (self) {
1315 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!s!s!", &version, &version_len, &enc, &enc_len, &alone, &alone_len) == FAILURE) {
1316 			return;
1317 		}
1318 		XMLWRITER_FROM_OBJECT(intern, self);
1319 	} else {
1320 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|s!s!s!", &pind, &version, &version_len, &enc, &enc_len, &alone, &alone_len) == FAILURE) {
1321 			return;
1322 		}
1323 		if ((intern = (xmlwriter_object *)zend_fetch_resource(Z_RES_P(pind), "XMLWriter", le_xmlwriter)) == NULL) {
1324 			RETURN_FALSE;
1325 		}
1326 	}
1327 
1328 	ptr = intern->ptr;
1329 
1330 	if (ptr) {
1331 		retval = xmlTextWriterStartDocument(ptr, version, enc, alone);
1332 		if (retval != -1) {
1333 			RETURN_TRUE;
1334 		}
1335 	}
1336 
1337 	RETURN_FALSE;
1338 }
1339 /* }}} */
1340 
1341 /* {{{ proto bool xmlwriter_end_document(resource xmlwriter)
1342 End current document - returns FALSE on error */
PHP_FUNCTION(xmlwriter_end_document)1343 static PHP_FUNCTION(xmlwriter_end_document)
1344 {
1345 	php_xmlwriter_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterEndDocument);
1346 }
1347 /* }}} */
1348 
1349 /* {{{ proto bool xmlwriter_start_dtd(resource xmlwriter, string name, string pubid, string sysid)
1350 Create start DTD tag - returns FALSE on error */
PHP_FUNCTION(xmlwriter_start_dtd)1351 static PHP_FUNCTION(xmlwriter_start_dtd)
1352 {
1353 	zval *pind;
1354 	xmlwriter_object *intern;
1355 	xmlTextWriterPtr ptr;
1356 	char *name, *pubid = NULL, *sysid = NULL;
1357 	size_t name_len, pubid_len, sysid_len;
1358 	int retval;
1359 	zval *self = getThis();
1360 
1361 	if (self) {
1362 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|s!s!", &name, &name_len, &pubid, &pubid_len, &sysid, &sysid_len) == FAILURE) {
1363 			return;
1364 		}
1365 
1366 		XMLWRITER_FROM_OBJECT(intern, self);
1367 	} else {
1368 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs|s!s!", &pind, &name, &name_len, &pubid, &pubid_len, &sysid, &sysid_len) == FAILURE) {
1369 			return;
1370 		}
1371 		if ((intern = (xmlwriter_object *)zend_fetch_resource(Z_RES_P(pind), "XMLWriter", le_xmlwriter)) == NULL) {
1372 			RETURN_FALSE;
1373 		}
1374 	}
1375 	ptr = intern->ptr;
1376 
1377 	if (ptr) {
1378 		retval = xmlTextWriterStartDTD(ptr, (xmlChar *)name, (xmlChar *)pubid, (xmlChar *)sysid);
1379 		if (retval != -1) {
1380 			RETURN_TRUE;
1381 		}
1382 	}
1383 
1384 	RETURN_FALSE;
1385 }
1386 /* }}} */
1387 
1388 /* {{{ proto bool xmlwriter_end_dtd(resource xmlwriter)
1389 End current DTD - returns FALSE on error */
PHP_FUNCTION(xmlwriter_end_dtd)1390 static PHP_FUNCTION(xmlwriter_end_dtd)
1391 {
1392 	php_xmlwriter_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterEndDTD);
1393 }
1394 /* }}} */
1395 
1396 /* {{{ proto bool xmlwriter_write_dtd(resource xmlwriter, string name, string pubid, string sysid, string subset)
1397 Write full DTD tag - returns FALSE on error */
PHP_FUNCTION(xmlwriter_write_dtd)1398 static PHP_FUNCTION(xmlwriter_write_dtd)
1399 {
1400 	zval *pind;
1401 	xmlwriter_object *intern;
1402 	xmlTextWriterPtr ptr;
1403 	char *name, *pubid = NULL, *sysid = NULL, *subset = NULL;
1404 	size_t name_len, pubid_len, sysid_len, subset_len;
1405 	int retval;
1406 	zval *self = getThis();
1407 
1408 	if (self) {
1409 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|s!s!s!", &name, &name_len, &pubid, &pubid_len, &sysid, &sysid_len, &subset, &subset_len) == FAILURE) {
1410 			return;
1411 		}
1412 
1413 		XMLWRITER_FROM_OBJECT(intern, self);
1414 	} else {
1415 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs|s!s!s!", &pind, &name, &name_len, &pubid, &pubid_len, &sysid, &sysid_len, &subset, &subset_len) == FAILURE) {
1416 			return;
1417 		}
1418 
1419 		if ((intern = (xmlwriter_object *)zend_fetch_resource(Z_RES_P(pind), "XMLWriter", le_xmlwriter)) == NULL) {
1420 			RETURN_FALSE;
1421 		}
1422 	}
1423 
1424 	ptr = intern->ptr;
1425 
1426 	if (ptr) {
1427 		retval = xmlTextWriterWriteDTD(ptr, (xmlChar *)name, (xmlChar *)pubid, (xmlChar *)sysid, (xmlChar *)subset);
1428 		if (retval != -1) {
1429 			RETURN_TRUE;
1430 		}
1431 	}
1432 
1433 	RETURN_FALSE;
1434 }
1435 /* }}} */
1436 
1437 /* {{{ proto bool xmlwriter_start_dtd_element(resource xmlwriter, string name)
1438 Create start DTD element - returns FALSE on error */
PHP_FUNCTION(xmlwriter_start_dtd_element)1439 static PHP_FUNCTION(xmlwriter_start_dtd_element)
1440 {
1441 	php_xmlwriter_string_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterStartDTDElement, "Invalid Element Name");
1442 }
1443 /* }}} */
1444 
1445 /* {{{ proto bool xmlwriter_end_dtd_element(resource xmlwriter)
1446 End current DTD element - returns FALSE on error */
PHP_FUNCTION(xmlwriter_end_dtd_element)1447 static PHP_FUNCTION(xmlwriter_end_dtd_element)
1448 {
1449 	php_xmlwriter_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterEndDTDElement);
1450 }
1451 /* }}} */
1452 
1453 /* {{{ proto bool xmlwriter_write_dtd_element(resource xmlwriter, string name, string content)
1454 Write full DTD element tag - returns FALSE on error */
PHP_FUNCTION(xmlwriter_write_dtd_element)1455 static PHP_FUNCTION(xmlwriter_write_dtd_element)
1456 {
1457 	zval *pind;
1458 	xmlwriter_object *intern;
1459 	xmlTextWriterPtr ptr;
1460 	char *name, *content;
1461 	size_t name_len, content_len;
1462 	int retval;
1463 	zval *self = getThis();
1464 
1465 	if (self) {
1466 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &name, &name_len, &content, &content_len) == FAILURE) {
1467 			return;
1468 		}
1469 		XMLWRITER_FROM_OBJECT(intern, self);
1470 	} else {
1471 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "rss", &pind,
1472 			&name, &name_len, &content, &content_len) == FAILURE) {
1473 			return;
1474 		}
1475 		if ((intern = (xmlwriter_object *)zend_fetch_resource(Z_RES_P(pind), "XMLWriter", le_xmlwriter)) == NULL) {
1476 			RETURN_FALSE;
1477 		}
1478 	}
1479 
1480 	XMLW_NAME_CHK("Invalid Element Name");
1481 
1482 	ptr = intern->ptr;
1483 
1484 	if (ptr) {
1485 		retval = xmlTextWriterWriteDTDElement(ptr, (xmlChar *)name, (xmlChar *)content);
1486 		if (retval != -1) {
1487 			RETURN_TRUE;
1488 		}
1489 	}
1490 
1491 	RETURN_FALSE;
1492 }
1493 /* }}} */
1494 
1495 /* {{{ proto bool xmlwriter_start_dtd_attlist(resource xmlwriter, string name)
1496 Create start DTD AttList - returns FALSE on error */
PHP_FUNCTION(xmlwriter_start_dtd_attlist)1497 static PHP_FUNCTION(xmlwriter_start_dtd_attlist)
1498 {
1499 	php_xmlwriter_string_arg(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterStartDTDAttlist, "Invalid Element Name");
1500 }
1501 /* }}} */
1502 
1503 /* {{{ proto bool xmlwriter_end_dtd_attlist(resource xmlwriter)
1504 End current DTD AttList - returns FALSE on error */
PHP_FUNCTION(xmlwriter_end_dtd_attlist)1505 static PHP_FUNCTION(xmlwriter_end_dtd_attlist)
1506 {
1507 	php_xmlwriter_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterEndDTDAttlist);
1508 }
1509 /* }}} */
1510 
1511 /* {{{ proto bool xmlwriter_write_dtd_attlist(resource xmlwriter, string name, string content)
1512 Write full DTD AttList tag - returns FALSE on error */
PHP_FUNCTION(xmlwriter_write_dtd_attlist)1513 static PHP_FUNCTION(xmlwriter_write_dtd_attlist)
1514 {
1515 	zval *pind;
1516 	xmlwriter_object *intern;
1517 	xmlTextWriterPtr ptr;
1518 	char *name, *content;
1519 	size_t name_len, content_len;
1520 	int retval;
1521 	zval *self = getThis();
1522 
1523 	if (self) {
1524 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss",
1525 			&name, &name_len, &content, &content_len) == FAILURE) {
1526 			return;
1527 		}
1528 		XMLWRITER_FROM_OBJECT(intern, self);
1529 	} else {
1530 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "rss", &pind,
1531 			&name, &name_len, &content, &content_len) == FAILURE) {
1532 			return;
1533 		}
1534 		if ((intern = (xmlwriter_object *)zend_fetch_resource(Z_RES_P(pind), "XMLWriter", le_xmlwriter)) == NULL) {
1535 			RETURN_FALSE;
1536 		}
1537 	}
1538 
1539 	XMLW_NAME_CHK("Invalid Element Name");
1540 
1541 	ptr = intern->ptr;
1542 
1543 	if (ptr) {
1544 		retval = xmlTextWriterWriteDTDAttlist(ptr, (xmlChar *)name, (xmlChar *)content);
1545 		if (retval != -1) {
1546 			RETURN_TRUE;
1547 		}
1548 	}
1549 
1550 	RETURN_FALSE;
1551 }
1552 /* }}} */
1553 
1554 /* {{{ proto bool xmlwriter_start_dtd_entity(resource xmlwriter, string name, bool isparam)
1555 Create start DTD Entity - returns FALSE on error */
PHP_FUNCTION(xmlwriter_start_dtd_entity)1556 static PHP_FUNCTION(xmlwriter_start_dtd_entity)
1557 {
1558 	zval *pind;
1559 	xmlwriter_object *intern;
1560 	xmlTextWriterPtr ptr;
1561 	char *name;
1562 	size_t name_len;
1563 	int retval;
1564 	zend_bool isparm;
1565 	zval *self = getThis();
1566 
1567 	if (self) {
1568 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "sb", &name, &name_len, &isparm) == FAILURE) {
1569 			return;
1570 		}
1571 		XMLWRITER_FROM_OBJECT(intern, self);
1572 	} else {
1573 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "rsb", &pind, &name, &name_len, &isparm) == FAILURE) {
1574 			return;
1575 		}
1576 		if ((intern = (xmlwriter_object *)zend_fetch_resource(Z_RES_P(pind), "XMLWriter", le_xmlwriter)) == NULL) {
1577 			RETURN_FALSE;
1578 		}
1579 	}
1580 
1581 	XMLW_NAME_CHK("Invalid Attribute Name");
1582 
1583 	ptr = intern->ptr;
1584 
1585 	if (ptr) {
1586 		retval = xmlTextWriterStartDTDEntity(ptr, isparm, (xmlChar *)name);
1587 		if (retval != -1) {
1588 			RETURN_TRUE;
1589 		}
1590 	}
1591 
1592 	RETURN_FALSE;
1593 }
1594 /* }}} */
1595 
1596 /* {{{ proto bool xmlwriter_end_dtd_entity(resource xmlwriter)
1597 End current DTD Entity - returns FALSE on error */
PHP_FUNCTION(xmlwriter_end_dtd_entity)1598 static PHP_FUNCTION(xmlwriter_end_dtd_entity)
1599 {
1600 	php_xmlwriter_end(INTERNAL_FUNCTION_PARAM_PASSTHRU, xmlTextWriterEndDTDEntity);
1601 }
1602 /* }}} */
1603 
1604 /* {{{ proto bool xmlwriter_write_dtd_entity(resource xmlwriter, string name, string content [, bool pe [, string pubid [, string sysid [, string ndataid]]]])
1605 Write full DTD Entity tag - returns FALSE on error */
PHP_FUNCTION(xmlwriter_write_dtd_entity)1606 static PHP_FUNCTION(xmlwriter_write_dtd_entity)
1607 {
1608 	zval *pind;
1609 	xmlwriter_object *intern;
1610 	xmlTextWriterPtr ptr;
1611 	char *name, *content;
1612 	size_t name_len, content_len;
1613 	int retval;
1614 	/* Optional parameters */
1615 	char *pubid = NULL, *sysid = NULL, *ndataid = NULL;
1616 	zend_bool pe = 0;
1617 	size_t pubid_len, sysid_len, ndataid_len;
1618 	zval *self = getThis();
1619 
1620 	if (self) {
1621 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|bsss",
1622 			&name, &name_len, &content, &content_len, &pe, &pubid, &pubid_len,
1623 			&sysid, &sysid_len, &ndataid, &ndataid_len) == FAILURE) {
1624 			return;
1625 		}
1626 		XMLWRITER_FROM_OBJECT(intern, self);
1627 	} else {
1628 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "rss|bsss", &pind,
1629 			&name, &name_len, &content, &content_len, &pe, &pubid, &pubid_len,
1630 			&sysid, &sysid_len, &ndataid, &ndataid_len) == FAILURE) {
1631 			return;
1632 		}
1633 		if ((intern = (xmlwriter_object *)zend_fetch_resource(Z_RES_P(pind), "XMLWriter", le_xmlwriter)) == NULL) {
1634 			RETURN_FALSE;
1635 		}
1636 	}
1637 
1638 	XMLW_NAME_CHK("Invalid Element Name");
1639 
1640 	ptr = intern->ptr;
1641 
1642 	if (ptr) {
1643 		retval = xmlTextWriterWriteDTDEntity(ptr, pe, (xmlChar *)name, (xmlChar *)pubid, (xmlChar *)sysid, (xmlChar *)ndataid, (xmlChar *)content);
1644 		if (retval != -1) {
1645 			RETURN_TRUE;
1646 		}
1647 	}
1648 
1649 	RETURN_FALSE;
1650 }
1651 /* }}} */
1652 
1653 /* {{{ proto resource xmlwriter_open_uri(string source)
1654 Create new xmlwriter using source uri for output */
PHP_FUNCTION(xmlwriter_open_uri)1655 static PHP_FUNCTION(xmlwriter_open_uri)
1656 {
1657 	char *valid_file = NULL;
1658 	xmlwriter_object *intern;
1659 	xmlTextWriterPtr ptr;
1660 	char *source;
1661 	char resolved_path[MAXPATHLEN + 1];
1662 	size_t source_len;
1663 	zval *self = getThis();
1664 	ze_xmlwriter_object *ze_obj = NULL;
1665 
1666 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &source, &source_len) == FAILURE) {
1667 		return;
1668 	}
1669 
1670 	if (self) {
1671 		/* We do not use XMLWRITER_FROM_OBJECT, xmlwriter init function here */
1672 		ze_obj = Z_XMLWRITER_P(self);
1673 	}
1674 
1675 	if (source_len == 0) {
1676 		php_error_docref(NULL, E_WARNING, "Empty string as source");
1677 		RETURN_FALSE;
1678 	}
1679 
1680 	valid_file = _xmlwriter_get_valid_file_path(source, resolved_path, MAXPATHLEN);
1681 	if (!valid_file) {
1682 		php_error_docref(NULL, E_WARNING, "Unable to resolve file path");
1683 		RETURN_FALSE;
1684 	}
1685 
1686 	ptr = xmlNewTextWriterFilename(valid_file, 0);
1687 
1688 	if (!ptr) {
1689 		RETURN_FALSE;
1690 	}
1691 
1692 	intern = emalloc(sizeof(xmlwriter_object));
1693 	intern->ptr = ptr;
1694 	intern->output = NULL;
1695 	if (self) {
1696 		if (ze_obj->xmlwriter_ptr) {
1697 			xmlwriter_free_resource_ptr(ze_obj->xmlwriter_ptr);
1698 		}
1699 		ze_obj->xmlwriter_ptr = intern;
1700 		RETURN_TRUE;
1701 	} else {
1702 		RETURN_RES(zend_register_resource(intern, le_xmlwriter));
1703 	}
1704 }
1705 /* }}} */
1706 
1707 /* {{{ proto resource xmlwriter_open_memory()
1708 Create new xmlwriter using memory for string output */
PHP_FUNCTION(xmlwriter_open_memory)1709 static PHP_FUNCTION(xmlwriter_open_memory)
1710 {
1711 	xmlwriter_object *intern;
1712 	xmlTextWriterPtr ptr;
1713 	xmlBufferPtr buffer;
1714 	zval *self = getThis();
1715 	ze_xmlwriter_object *ze_obj = NULL;
1716 
1717 	if (zend_parse_parameters_none() == FAILURE) {
1718 		return;
1719 	}
1720 
1721 	if (self) {
1722 		/* We do not use XMLWRITER_FROM_OBJECT, xmlwriter init function here */
1723 		ze_obj = Z_XMLWRITER_P(self);
1724 	}
1725 
1726 	buffer = xmlBufferCreate();
1727 
1728 	if (buffer == NULL) {
1729 		php_error_docref(NULL, E_WARNING, "Unable to create output buffer");
1730 		RETURN_FALSE;
1731 	}
1732 
1733 	ptr = xmlNewTextWriterMemory(buffer, 0);
1734 	if (! ptr) {
1735 		xmlBufferFree(buffer);
1736 		RETURN_FALSE;
1737 	}
1738 
1739 	intern = emalloc(sizeof(xmlwriter_object));
1740 	intern->ptr = ptr;
1741 	intern->output = buffer;
1742 	if (self) {
1743 		if (ze_obj->xmlwriter_ptr) {
1744 			xmlwriter_free_resource_ptr(ze_obj->xmlwriter_ptr);
1745 		}
1746 		ze_obj->xmlwriter_ptr = intern;
1747 		RETURN_TRUE;
1748 	} else {
1749 		RETURN_RES(zend_register_resource(intern, le_xmlwriter));
1750 	}
1751 
1752 }
1753 /* }}} */
1754 
1755 /* {{{ php_xmlwriter_flush */
php_xmlwriter_flush(INTERNAL_FUNCTION_PARAMETERS,int force_string)1756 static void php_xmlwriter_flush(INTERNAL_FUNCTION_PARAMETERS, int force_string) {
1757 	zval *pind;
1758 	xmlwriter_object *intern;
1759 	xmlTextWriterPtr ptr;
1760 	xmlBufferPtr buffer;
1761 	zend_bool empty = 1;
1762 	int output_bytes;
1763 	zval *self = getThis();
1764 
1765 	if (self) {
1766 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &empty) == FAILURE) {
1767 			return;
1768 		}
1769 		XMLWRITER_FROM_OBJECT(intern, self);
1770 	} else {
1771 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|b", &pind, &empty) == FAILURE) {
1772 			return;
1773 		}
1774 
1775 		if ((intern = (xmlwriter_object *)zend_fetch_resource(Z_RES_P(pind), "XMLWriter", le_xmlwriter)) == NULL) {
1776 			RETURN_FALSE;
1777 		}
1778 	}
1779 	ptr = intern->ptr;
1780 
1781 	if (ptr) {
1782 		buffer = intern->output;
1783 		if (force_string == 1 && buffer == NULL) {
1784 			RETURN_EMPTY_STRING();
1785 		}
1786 		output_bytes = xmlTextWriterFlush(ptr);
1787 		if (buffer) {
1788 			RETVAL_STRING((char *) buffer->content);
1789 			if (empty) {
1790 				xmlBufferEmpty(buffer);
1791 			}
1792 		} else {
1793 			RETVAL_LONG(output_bytes);
1794 		}
1795 		return;
1796 	}
1797 
1798 	RETURN_EMPTY_STRING();
1799 }
1800 /* }}} */
1801 
1802 /* {{{ proto string xmlwriter_output_memory(resource xmlwriter [,bool flush])
1803 Output current buffer as string */
PHP_FUNCTION(xmlwriter_output_memory)1804 static PHP_FUNCTION(xmlwriter_output_memory)
1805 {
1806 	php_xmlwriter_flush(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1807 }
1808 /* }}} */
1809 
1810 /* {{{ proto mixed xmlwriter_flush(resource xmlwriter [,bool empty])
1811 Output current buffer */
PHP_FUNCTION(xmlwriter_flush)1812 static PHP_FUNCTION(xmlwriter_flush)
1813 {
1814 	php_xmlwriter_flush(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1815 }
1816 /* }}} */
1817 
1818 /* {{{ PHP_MINIT_FUNCTION
1819  */
PHP_MINIT_FUNCTION(xmlwriter)1820 static PHP_MINIT_FUNCTION(xmlwriter)
1821 {
1822 	zend_class_entry ce;
1823 	le_xmlwriter = zend_register_list_destructors_ex(xmlwriter_dtor, NULL, "xmlwriter", module_number);
1824 
1825 	memcpy(&xmlwriter_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
1826 	xmlwriter_object_handlers.offset = XtOffsetOf(ze_xmlwriter_object, std);
1827 	xmlwriter_object_handlers.dtor_obj = xmlwriter_object_dtor;
1828 	xmlwriter_object_handlers.free_obj = xmlwriter_object_free_storage;
1829 	xmlwriter_object_handlers.clone_obj = NULL;
1830 	INIT_CLASS_ENTRY(ce, "XMLWriter", xmlwriter_class_functions);
1831 	ce.create_object = xmlwriter_object_new;
1832 	xmlwriter_class_entry_ce = zend_register_internal_class(&ce);
1833 
1834 	return SUCCESS;
1835 }
1836 /* }}} */
1837 
1838 /* {{{ PHP_MSHUTDOWN_FUNCTION
1839  */
PHP_MSHUTDOWN_FUNCTION(xmlwriter)1840 static PHP_MSHUTDOWN_FUNCTION(xmlwriter)
1841 {
1842 	return SUCCESS;
1843 }
1844 /* }}} */
1845 
1846 /* {{{ PHP_MINFO_FUNCTION
1847  */
PHP_MINFO_FUNCTION(xmlwriter)1848 static PHP_MINFO_FUNCTION(xmlwriter)
1849 {
1850 	php_info_print_table_start();
1851 	{
1852 		php_info_print_table_row(2, "XMLWriter", "enabled");
1853 	}
1854 	php_info_print_table_end();
1855 }
1856 /* }}} */
1857