Apache HTTPD
apr_xml.c
Go to the documentation of this file.
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "apr.h"
18#include "apr_strings.h"
19
20#define APR_WANT_STDIO /* for sprintf() */
21#define APR_WANT_STRFUNC
22#include "apr_want.h"
23
24#include "apr_xml.h"
25
26#include "apu_config.h"
27
28#if defined(HAVE_XMLPARSE_XMLPARSE_H)
29#include <xmlparse/xmlparse.h>
30#elif defined(HAVE_XMLTOK_XMLPARSE_H)
31#include <xmltok/xmlparse.h>
32#elif defined(HAVE_XML_XMLPARSE_H)
33#include <xml/xmlparse.h>
34#else
35#include <expat.h>
36#endif
37
38#define DEBUG_CR "\r\n"
39
40static const char APR_KW_xmlns[] = { 0x78, 0x6D, 0x6C, 0x6E, 0x73, '\0' };
41static const char APR_KW_xmlns_lang[] = { 0x78, 0x6D, 0x6C, 0x3A, 0x6C, 0x61, 0x6E, 0x67, '\0' };
42static const char APR_KW_DAV[] = { 0x44, 0x41, 0x56, 0x3A, '\0' };
43
44/* errors related to namespace processing */
45#define APR_XML_NS_ERROR_UNKNOWN_PREFIX (-1000)
46#define APR_XML_NS_ERROR_INVALID_DECL (-1001)
47
48/* test for a namespace prefix that begins with [Xx][Mm][Ll] */
49#define APR_XML_NS_IS_RESERVED(name) \
50 ( (name[0] == 0x58 || name[0] == 0x78) && \
51 (name[1] == 0x4D || name[1] == 0x6D) && \
52 (name[2] == 0x4C || name[2] == 0x6C) )
53
54
55/* the real (internal) definition of the parser context */
57 apr_xml_doc *doc; /* the doc we're parsing */
58 apr_pool_t *p; /* the pool we allocate from */
59 apr_xml_elem *cur_elem; /* current element */
60
61 int error; /* an error has occurred */
62#define APR_XML_ERROR_EXPAT 1
63#define APR_XML_ERROR_PARSE_DONE 2
64/* also: public APR_XML_NS_ERROR_* values (if any) */
65
66 XML_Parser xp; /* the actual (Expat) XML parser */
67 enum XML_Error xp_err; /* stored Expat error code */
68};
69
70/* struct for scoping namespace declarations */
71typedef struct apr_xml_ns_scope {
72 const char *prefix; /* prefix used for this ns */
73 int ns; /* index into namespace table */
74 int emptyURI; /* the namespace URI is the empty string */
75 struct apr_xml_ns_scope *next; /* next scoped namespace */
77
78
79/* return namespace table index for a given prefix */
80static int find_prefix(apr_xml_parser *parser, const char *prefix)
81{
83
84 /*
85 ** Walk up the tree, looking for a namespace scope that defines this
86 ** prefix.
87 */
88 for (; elem; elem = elem->parent) {
89 apr_xml_ns_scope *ns_scope;
90
91 for (ns_scope = elem->ns_scope; ns_scope; ns_scope = ns_scope->next) {
92 if (strcmp(prefix, ns_scope->prefix) == 0) {
93 if (ns_scope->emptyURI) {
94 /*
95 ** It is possible to set the default namespace to an
96 ** empty URI string; this resets the default namespace
97 ** to mean "no namespace." We just found the prefix
98 ** refers to an empty URI, so return "no namespace."
99 */
100 return APR_XML_NS_NONE;
101 }
102
103 return ns_scope->ns;
104 }
105 }
106 }
107
108 /*
109 * If the prefix is empty (""), this means that a prefix was not
110 * specified in the element/attribute. The search that was performed
111 * just above did not locate a default namespace URI (which is stored
112 * into ns_scope with an empty prefix). This means the element/attribute
113 * has "no namespace". We have a reserved value for this.
114 */
115 if (*prefix == '\0') {
116 return APR_XML_NS_NONE;
117 }
118
119 /* not found */
121}
122
123/* return original prefix given ns index */
124static const char * find_prefix_name(const apr_xml_elem *elem, int ns, int parent)
125{
126 /*
127 ** Walk up the tree, looking for a namespace scope that defines this
128 ** prefix.
129 */
130 for (; elem; elem = parent ? elem->parent : NULL) {
131 apr_xml_ns_scope *ns_scope = elem->ns_scope;
132
133 for (; ns_scope; ns_scope = ns_scope->next) {
134 if (ns_scope->ns == ns)
135 return ns_scope->prefix;
136 }
137 }
138 /* not found */
139 return "";
140}
141
142
143static void start_handler(void *userdata, const char *name, const char **attrs)
144{
145 apr_xml_parser *parser = userdata;
148 apr_xml_attr *prev;
149 char *colon;
150 const char *quoted;
151 char *elem_name;
152
153 /* punt once we find an error */
154 if (parser->error)
155 return;
156
157 elem = apr_pcalloc(parser->p, sizeof(*elem));
158
159 /* prep the element */
160 elem->name = elem_name = apr_pstrdup(parser->p, name);
161
162 /* fill in the attributes (note: ends up in reverse order) */
163 while (*attrs) {
164 attr = apr_palloc(parser->p, sizeof(*attr));
165 attr->name = apr_pstrdup(parser->p, *attrs++);
166 attr->value = apr_pstrdup(parser->p, *attrs++);
167 attr->next = elem->attr;
168 elem->attr = attr;
169 }
170
171 /* hook the element into the tree */
172 if (parser->cur_elem == NULL) {
173 /* no current element; this also becomes the root */
175 }
176 else {
177 /* this element appeared within the current elem */
178 elem->parent = parser->cur_elem;
179
180 /* set up the child/sibling links */
181 if (elem->parent->last_child == NULL) {
182 /* no first child either */
183 elem->parent->first_child = elem->parent->last_child = elem;
184 }
185 else {
186 /* hook onto the end of the parent's children */
187 elem->parent->last_child->next = elem;
188 elem->parent->last_child = elem;
189 }
190
191 /* this element is now the current element */
193 }
194
195 /* scan the attributes for namespace declarations */
196 for (prev = NULL, attr = elem->attr;
197 attr;
198 attr = attr->next) {
199 if (strncmp(attr->name, APR_KW_xmlns, 5) == 0) {
200 const char *prefix = &attr->name[5];
201 apr_xml_ns_scope *ns_scope;
202
203 /* test for xmlns:foo= form and xmlns= form */
204 if (*prefix == 0x3A) {
205 /* a namespace prefix declaration must have a
206 non-empty value. */
207 if (attr->value[0] == '\0') {
209 return;
210 }
211 ++prefix;
212 }
213 else if (*prefix != '\0') {
214 /* advance "prev" since "attr" is still present */
215 prev = attr;
216 continue;
217 }
218
219 /* quote the URI before we ever start working with it */
220 quoted = apr_xml_quote_string(parser->p, attr->value, 1);
221
222 /* build and insert the new scope */
223 ns_scope = apr_pcalloc(parser->p, sizeof(*ns_scope));
224 ns_scope->prefix = prefix;
226 ns_scope->emptyURI = *quoted == '\0';
227 ns_scope->next = elem->ns_scope;
228 elem->ns_scope = ns_scope;
229
230 /* remove this attribute from the element */
231 if (prev == NULL)
232 elem->attr = attr->next;
233 else
234 prev->next = attr->next;
235
236 /* Note: prev will not be advanced since we just removed "attr" */
237 }
238 else if (strcmp(attr->name, APR_KW_xmlns_lang) == 0) {
239 /* save away the language (in quoted form) */
240 elem->lang = apr_xml_quote_string(parser->p, attr->value, 1);
241
242 /* remove this attribute from the element */
243 if (prev == NULL)
244 elem->attr = attr->next;
245 else
246 prev->next = attr->next;
247
248 /* Note: prev will not be advanced since we just removed "attr" */
249 }
250 else {
251 /* advance "prev" since "attr" is still present */
252 prev = attr;
253 }
254 }
255
256 /*
257 ** If an xml:lang attribute didn't exist (lang==NULL), then copy the
258 ** language from the parent element (if present).
259 **
260 ** NOTE: elem_size() *depends* upon this pointer equality.
261 */
262 if (elem->lang == NULL && elem->parent != NULL)
263 elem->lang = elem->parent->lang;
264
265 /* adjust the element's namespace */
266 colon = strchr(elem_name, 0x3A);
267 if (colon == NULL) {
268 /*
269 * The element is using the default namespace, which will always
270 * be found. Either it will be "no namespace", or a default
271 * namespace URI has been specified at some point.
272 */
273 elem->ns = find_prefix(parser, "");
274 }
275 else if (APR_XML_NS_IS_RESERVED(elem->name)) {
276 elem->ns = APR_XML_NS_NONE;
277 }
278 else {
279 *colon = '\0';
280 elem->ns = find_prefix(parser, elem->name);
281 elem->name = colon + 1;
282
283 if (APR_XML_NS_IS_ERROR(elem->ns)) {
284 parser->error = elem->ns;
285 return;
286 }
287 }
288
289 /* adjust all remaining attributes' namespaces */
290 for (attr = elem->attr; attr; attr = attr->next) {
291 /*
292 * apr_xml_attr defines this as "const" but we dup'd it, so we
293 * know that we can change it. a bit hacky, but the existing
294 * structure def is best.
295 */
296 char *attr_name = (char *)attr->name;
297
298 colon = strchr(attr_name, 0x3A);
299 if (colon == NULL) {
300 /*
301 * Attributes do NOT use the default namespace. Therefore,
302 * we place them into the "no namespace" category.
303 */
304 attr->ns = APR_XML_NS_NONE;
305 }
306 else if (APR_XML_NS_IS_RESERVED(attr->name)) {
307 attr->ns = APR_XML_NS_NONE;
308 }
309 else {
310 *colon = '\0';
311 attr->ns = find_prefix(parser, attr->name);
312 attr->name = colon + 1;
313
314 if (APR_XML_NS_IS_ERROR(attr->ns)) {
315 parser->error = attr->ns;
316 return;
317 }
318 }
319 }
320}
321
322static void end_handler(void *userdata, const char *name)
323{
324 apr_xml_parser *parser = userdata;
325
326 /* punt once we find an error */
327 if (parser->error)
328 return;
329
330 /* pop up one level */
332}
333
334static void cdata_handler(void *userdata, const char *data, int len)
335{
336 apr_xml_parser *parser = userdata;
339 const char *s;
340
341 /* punt once we find an error */
342 if (parser->error)
343 return;
344
347
348 if (elem->last_child == NULL) {
349 /* no children yet. this cdata follows the start tag */
350 hdr = &elem->first_cdata;
351 }
352 else {
353 /* child elements exist. this cdata follows the last child. */
354 hdr = &elem->last_child->following_cdata;
355 }
356
358}
359
361{
363
365 parser->xp = NULL;
366
367 return APR_SUCCESS;
368}
369
370#if XML_MAJOR_VERSION > 1
371/* Stop the parser if an entity declaration is hit. */
372static void entity_declaration(void *userData, const XML_Char *entityName,
374 int value_length, const XML_Char *base,
375 const XML_Char *systemId, const XML_Char *publicId,
376 const XML_Char *notationName)
377{
378 apr_xml_parser *parser = userData;
379
381}
382#else
383/* A noop default_handler. */
384static void default_handler(void *userData, const XML_Char *s, int len)
385{
386}
387#endif
388
390{
392
393 parser->p = pool;
394 parser->doc = apr_pcalloc(pool, sizeof(*parser->doc));
395
396 parser->doc->namespaces = apr_array_make(pool, 5, sizeof(const char *));
397
398 /* ### is there a way to avoid hard-coding this? */
400
402 if (parser->xp == NULL) {
404 return NULL;
405 }
406
409
413
414 /* Prevent the "billion laughs" attack against expat by disabling
415 * internal entity expansion. With 2.x, forcibly stop the parser
416 * if an entity is declared - this is safer and a more obvious
417 * failure mode. With older versions, installing a noop
418 * DefaultHandler means that internal entities will be expanded as
419 * the empty string, which is also sufficient to prevent the
420 * attack. */
421#if XML_MAJOR_VERSION > 1
423#else
425#endif
426
427 return parser;
428}
429
431 const char *data, apr_size_t len,
432 int is_final)
433{
434 if (parser->xp == NULL) {
436 }
437 else {
438 int rv = XML_Parse(parser->xp, data, (int)len, is_final);
439
440 if (rv == 0) {
443 }
444 }
445
446 /* ### better error code? */
448}
449
451 const char *data,
453{
454 return do_parse(parser, data, len, 0 /* is_final */);
455}
456
459{
460 apr_status_t status = do_parse(parser, "", 0, 1 /* is_final */);
461
462 /* get rid of the parser */
464
465 if (status)
466 return status;
467
468 if (pdoc != NULL)
469 *pdoc = parser->doc;
470 return APR_SUCCESS;
471}
472
474 char *errbuf,
476{
477 int error = parser->error;
478 const char *msg;
479
480 /* clear our record of an error */
481 parser->error = 0;
482
483 switch (error) {
484 case 0:
485 msg = "No error.";
486 break;
487
489 msg = "An undefined namespace prefix was used.";
490 break;
491
493 msg = "A namespace prefix was defined with an empty URI.";
494 break;
495
498 "XML parser error code: %s (%d)",
500 return errbuf;
501
503 msg = "The parser is not active.";
504 break;
505
506 default:
507 msg = "There was an unknown error within the XML body.";
508 break;
509 }
510
511 (void) apr_cpystrn(errbuf, msg, errbufsize);
512 return errbuf;
513}
514
520{
521 apr_status_t rv;
522 char *buffer;
524
526 if (*parser == NULL) {
527 /* FIXME: returning an error code would be nice,
528 * but we dont get one ;( */
529 return APR_EGENERAL;
530 }
533
535
536 while (rv == APR_SUCCESS) {
538 if (rv != APR_SUCCESS) {
539 return rv;
540 }
541
544 }
545 if (rv != APR_EOF) {
546 return rv;
547 }
549 *parser = NULL;
550 return rv;
551}
552
554 const char *text)
555{
556 apr_text *t = apr_palloc(p, sizeof(*t));
557
558 t->text = text;
559 t->next = NULL;
560
561 if (hdr->first == NULL) {
562 /* no text elements yet */
563 hdr->first = hdr->last = t;
564 }
565 else {
566 /* append to the last text element */
567 hdr->last->next = t;
568 hdr->last = t;
569 }
570}
571
572
573/* ---------------------------------------------------------------
574**
575** XML UTILITY FUNCTIONS
576*/
577
578/*
579** apr_xml_quote_string: quote an XML string
580**
581** Replace '<', '>', and '&' with '&lt;', '&gt;', and '&amp;'.
582** If quotes is true, then replace '"' with '&quot;'.
583**
584** quotes is typically set to true for XML strings that will occur within
585** double quotes -- attribute values.
586*/
587APU_DECLARE(const char *) apr_xml_quote_string(apr_pool_t *p, const char *s,
588 int quotes)
589{
590 const char *scan;
591 apr_size_t len = 0;
592 apr_size_t extra = 0;
593 char *qstr;
594 char *qscan;
595 char c;
596
597 for (scan = s; (c = *scan) != '\0'; ++scan, ++len) {
598 if (c == '<' || c == '>')
599 extra += 3; /* &lt; or &gt; */
600 else if (c == '&')
601 extra += 4; /* &amp; */
602 else if (quotes && c == '"')
603 extra += 5; /* &quot; */
604 }
605
606 /* nothing to do? */
607 if (extra == 0)
608 return s;
609
610 qstr = apr_palloc(p, len + extra + 1);
611 for (scan = s, qscan = qstr; (c = *scan) != '\0'; ++scan) {
612 if (c == '<') {
613 *qscan++ = '&';
614 *qscan++ = 'l';
615 *qscan++ = 't';
616 *qscan++ = ';';
617 }
618 else if (c == '>') {
619 *qscan++ = '&';
620 *qscan++ = 'g';
621 *qscan++ = 't';
622 *qscan++ = ';';
623 }
624 else if (c == '&') {
625 *qscan++ = '&';
626 *qscan++ = 'a';
627 *qscan++ = 'm';
628 *qscan++ = 'p';
629 *qscan++ = ';';
630 }
631 else if (quotes && c == '"') {
632 *qscan++ = '&';
633 *qscan++ = 'q';
634 *qscan++ = 'u';
635 *qscan++ = 'o';
636 *qscan++ = 't';
637 *qscan++ = ';';
638 }
639 else {
640 *qscan++ = c;
641 }
642 }
643
644 *qscan = '\0';
645 return qstr;
646}
647
648/* how many characters for the given integer? */
649#define APR_XML_NS_LEN(ns) ((ns) < 10 ? 1 : (ns) < 100 ? 2 : (ns) < 1000 ? 3 : \
650 (ns) < 10000 ? 4 : (ns) < 100000 ? 5 : \
651 (ns) < 1000000 ? 6 : (ns) < 10000000 ? 7 : \
652 (ns) < 100000000 ? 8 : (ns) < 1000000000 ? 9 : 10)
653
655{
656 apr_size_t size = 0;
657
658 for (; t; t = t->next)
659 size += strlen(t->text);
660 return size;
661}
662
665{
667
670 const apr_xml_attr *attr;
671
672 size = 0;
673
675 int i;
676
677 /*
678 ** The outer element will contain xmlns:ns%d="%s" attributes
679 ** and an xml:lang attribute, if applicable.
680 */
681
682 for (i = namespaces->nelts; i--;) {
683 /* compute size of: ' xmlns:ns%d="%s"' */
684 size += (9 + APR_XML_NS_LEN(i) + 2 +
685 strlen(APR_XML_GET_URI_ITEM(namespaces, i)) + 1);
686 }
687
688 if (elem->lang != NULL) {
689 /* compute size of: ' xml:lang="%s"' */
690 size += 11 + strlen(elem->lang) + 1;
691 }
692 }
693 else if (style == APR_XML_X2T_PARSED) {
694 apr_xml_ns_scope *ns_scope = elem->ns_scope;
695
696 /* compute size of: ' xmlns:%s="%s"' */
697 for (; ns_scope; ns_scope = ns_scope->next) {
698 size += 10 + strlen(find_prefix_name(elem, ns_scope->ns, 0)) +
699 strlen(APR_XML_GET_URI_ITEM(namespaces, ns_scope->ns));
700 }
701
702 if (elem->lang != NULL) {
703 /* compute size of: ' xml:lang="%s"' */
704 size += 11 + strlen(elem->lang) + 1;
705 }
706 }
707
708 if (elem->ns == APR_XML_NS_NONE) {
709 /* compute size of: <%s> */
710 size += 1 + strlen(elem->name) + 1;
711 }
712 else if (style == APR_XML_X2T_PARSED) {
713 /* compute size of: <%s:%s> */
714 size += 3 + strlen(find_prefix_name(elem, elem->ns, 1)) + strlen(elem->name);
715 }
716 else {
717 int ns = ns_map ? ns_map[elem->ns] : elem->ns;
718
719 /* compute size of: <ns%d:%s> */
720 size += 3 + APR_XML_NS_LEN(ns) + 1 + strlen(elem->name) + 1;
721 }
722
724 /* insert a closing "/" */
725 size += 1;
726 }
727 else {
728 /*
729 * two of above plus "/":
730 * <ns%d:%s> ... </ns%d:%s>
731 * OR <%s> ... </%s>
732 */
733 size = 2 * size + 1;
734 }
735
736 for (attr = elem->attr; attr; attr = attr->next) {
737 if (attr->ns == APR_XML_NS_NONE) {
738 /* compute size of: ' %s="%s"' */
739 size += 1 + strlen(attr->name) + 2 + strlen(attr->value) + 1;
740 }
741 else if (style == APR_XML_X2T_PARSED) {
742 /* compute size of: ' %s:%s="%s"' */
743 size += 5 + strlen(find_prefix_name(elem, attr->ns, 1)) + strlen(attr->name) + strlen(attr->value);
744 }
745 else {
746 /* compute size of: ' ns%d:%s="%s"' */
747 int ns = ns_map ? ns_map[attr->ns] : attr->ns;
748 size += 3 + APR_XML_NS_LEN(ns) + 1 + strlen(attr->name) + 2 + strlen(attr->value) + 1;
749 }
750 }
751
752 /*
753 ** If the element has an xml:lang value that is *different* from
754 ** its parent, then add the thing in: ' xml:lang="%s"'.
755 **
756 ** NOTE: we take advantage of the pointer equality established by
757 ** the parsing for "inheriting" the xml:lang values from parents.
758 */
759 if (elem->lang != NULL &&
760 (elem->parent == NULL || elem->lang != elem->parent->lang)) {
761 size += 11 + strlen(elem->lang) + 1;
762 }
763 }
764 else if (style == APR_XML_X2T_LANG_INNER) {
765 /*
766 * This style prepends the xml:lang value plus a null terminator.
767 * If a lang value is not present, then we insert a null term.
768 */
769 size = elem->lang ? strlen(elem->lang) + 1 : 1;
770 }
771 else
772 size = 0;
773
774 size += text_size(elem->first_cdata.first);
775
776 for (elem = elem->first_child; elem; elem = elem->next) {
777 /* the size of the child element plus the CDATA that follows it */
779 text_size(elem->following_cdata.first));
780 }
781
782 return size;
783}
784
785static char *write_text(char *s, const apr_text *t)
786{
787 for (; t; t = t->next) {
788 apr_size_t len = strlen(t->text);
789 memcpy(s, t->text, len);
790 s += len;
791 }
792 return s;
793}
794
795static char *write_elem(char *s, const apr_xml_elem *elem, int style,
797{
798 const apr_xml_elem *child;
800 int ns;
801
805 const apr_xml_attr *attr;
806
807 if (elem->ns == APR_XML_NS_NONE)
808 len = sprintf(s, "<%s", elem->name);
809 else if (style == APR_XML_X2T_PARSED)
810 len = sprintf(s, "<%s:%s", find_prefix_name(elem, elem->ns, 1), elem->name);
811 else {
812 ns = ns_map ? ns_map[elem->ns] : elem->ns;
813 len = sprintf(s, "<ns%d:%s", ns, elem->name);
814 }
815 s += len;
816
817 for (attr = elem->attr; attr; attr = attr->next) {
818 if (attr->ns == APR_XML_NS_NONE)
819 len = sprintf(s, " %s=\"%s\"", attr->name, attr->value);
820 else if (style == APR_XML_X2T_PARSED)
821 len = sprintf(s, " %s:%s=\"%s\"",
822 find_prefix_name(elem, attr->ns, 1), attr->name, attr->value);
823 else {
824 ns = ns_map ? ns_map[attr->ns] : attr->ns;
825 len = sprintf(s, " ns%d:%s=\"%s\"", ns, attr->name, attr->value);
826 }
827 s += len;
828 }
829
830 /* add the xml:lang value if necessary */
831 if (elem->lang != NULL &&
833 elem->parent == NULL ||
834 elem->lang != elem->parent->lang)) {
835 len = sprintf(s, " xml:lang=\"%s\"", elem->lang);
836 s += len;
837 }
838
839 /* add namespace definitions, if required */
841 int i;
842
843 for (i = namespaces->nelts; i--;) {
844 len = sprintf(s, " xmlns:ns%d=\"%s\"", i,
846 s += len;
847 }
848 }
849 else if (style == APR_XML_X2T_PARSED) {
850 apr_xml_ns_scope *ns_scope = elem->ns_scope;
851
852 for (; ns_scope; ns_scope = ns_scope->next) {
853 const char *prefix = find_prefix_name(elem, ns_scope->ns, 0);
854
855 len = sprintf(s, " xmlns%s%s=\"%s\"",
856 *prefix ? ":" : "", *prefix ? prefix : "",
858 s += len;
859 }
860 }
861
862 /* no more to do. close it up and go. */
863 if (empty) {
864 *s++ = '/';
865 *s++ = '>';
866 return s;
867 }
868
869 /* just close it */
870 *s++ = '>';
871 }
872 else if (style == APR_XML_X2T_LANG_INNER) {
873 /* prepend the xml:lang value */
874 if (elem->lang != NULL) {
875 len = strlen(elem->lang);
876 memcpy(s, elem->lang, len);
877 s += len;
878 }
879 *s++ = '\0';
880 }
881
882 s = write_text(s, elem->first_cdata.first);
883
884 for (child = elem->first_child; child; child = child->next) {
885 s = write_elem(s, child,
887 NULL, ns_map);
889 }
890
892 if (elem->ns == APR_XML_NS_NONE)
893 len = sprintf(s, "</%s>", elem->name);
894 else if (style == APR_XML_X2T_PARSED)
895 len = sprintf(s, "</%s:%s>", find_prefix_name(elem, elem->ns, 1), elem->name);
896 else {
897 ns = ns_map ? ns_map[elem->ns] : elem->ns;
898 len = sprintf(s, "</ns%d:%s>", ns, elem->name);
899 }
900 s += len;
901 }
902
903 return s;
904}
905
907{
911
912 /* convert the element's text */
913 for (scan_txt = elem->first_cdata.first;
914 scan_txt != NULL;
915 scan_txt = scan_txt->next) {
916 scan_txt->text = apr_xml_quote_string(p, scan_txt->text, 0);
917 }
918 for (scan_txt = elem->following_cdata.first;
919 scan_txt != NULL;
920 scan_txt = scan_txt->next) {
921 scan_txt->text = apr_xml_quote_string(p, scan_txt->text, 0);
922 }
923
924 /* convert the attribute values */
925 for (scan_attr = elem->attr;
926 scan_attr != NULL;
927 scan_attr = scan_attr->next) {
928 scan_attr->value = apr_xml_quote_string(p, scan_attr->value, 1);
929 }
930
931 /* convert the child elements */
932 for (scan_elem = elem->first_child;
933 scan_elem != NULL;
934 scan_elem = scan_elem->next) {
936 }
937}
938
939/* convert an element to a text string */
942 int *ns_map, const char **pbuf,
944{
945 /* get the exact size, plus a null terminator */
947 char *s = apr_palloc(p, size);
948
950 s[size - 1] = '\0';
951
952 *pbuf = s;
953 if (psize)
954 *psize = size;
955}
956
958 const apr_xml_elem *elem)
959{
960 if (elem->ns == APR_XML_NS_NONE) {
961 /*
962 * The prefix (xml...) is already within the prop name, or
963 * the element simply has no prefix.
964 */
965 return apr_psprintf(p, "<%s/>" DEBUG_CR, elem->name);
966 }
967
968 return apr_psprintf(p, "<ns%d:%s/>" DEBUG_CR, elem->ns, elem->name);
969}
970
971/* return the URI's (existing) index, or insert it and return a new index */
973 const char *uri)
974{
975 int i;
976 const char **pelt;
977
978 /* never insert an empty URI; this index is always APR_XML_NS_NONE */
979 if (*uri == '\0')
980 return APR_XML_NS_NONE;
981
982 for (i = uri_array->nelts; i--;) {
984 return i;
985 }
986
988 *pelt = uri; /* assume uri is const or in a pool */
989 return uri_array->nelts - 1;
990}
991
992/* convert the element to EBCDIC */
993#if APR_CHARSET_EBCDIC
995 apr_xlate_t *convset)
996{
998 apr_xml_elem *ec;
999 apr_text *t;
1002
1003 inbytes_left = outbytes_left = strlen(e->name);
1004 status = apr_xlate_conv_buffer(convset, e->name, &inbytes_left, (char *) e->name, &outbytes_left);
1005 if (status) {
1006 return status;
1007 }
1008
1009 for (t = e->first_cdata.first; t != NULL; t = t->next) {
1010 inbytes_left = outbytes_left = strlen(t->text);
1011 status = apr_xlate_conv_buffer(convset, t->text, &inbytes_left, (char *) t->text, &outbytes_left);
1012 if (status) {
1013 return status;
1014 }
1015 }
1016
1017 for (t = e->following_cdata.first; t != NULL; t = t->next) {
1018 inbytes_left = outbytes_left = strlen(t->text);
1019 status = apr_xlate_conv_buffer(convset, t->text, &inbytes_left, (char *) t->text, &outbytes_left);
1020 if (status) {
1021 return status;
1022 }
1023 }
1024
1025 for (a = e->attr; a != NULL; a = a->next) {
1026 inbytes_left = outbytes_left = strlen(a->name);
1027 status = apr_xlate_conv_buffer(convset, a->name, &inbytes_left, (char *) a->name, &outbytes_left);
1028 if (status) {
1029 return status;
1030 }
1031 inbytes_left = outbytes_left = strlen(a->value);
1032 status = apr_xlate_conv_buffer(convset, a->value, &inbytes_left, (char *) a->value, &outbytes_left);
1033 if (status) {
1034 return status;
1035 }
1036 }
1037
1038 for (ec = e->first_child; ec != NULL; ec = ec->next) {
1039 status = apr_xml_parser_convert_elem(ec, convset);
1040 if (status) {
1041 return status;
1042 }
1043 }
1044 return APR_SUCCESS;
1045}
1046
1047/* convert the whole document to EBCDIC */
1050 apr_xlate_t *convset)
1051{
1053 /* Don't convert the namespaces: they are constant! */
1054 if (pdoc->namespaces != NULL) {
1055 int i;
1057 namespaces = apr_array_make(pool, pdoc->namespaces->nelts, sizeof(const char *));
1058 if (namespaces == NULL)
1059 return APR_ENOMEM;
1060 for (i = 0; i < pdoc->namespaces->nelts; i++) {
1062 char *ptr = (char *) APR_XML_GET_URI_ITEM(pdoc->namespaces, i);
1063 ptr = apr_pstrdup(pool, ptr);
1064 if ( ptr == NULL)
1065 return APR_ENOMEM;
1066 inbytes_left = outbytes_left = strlen(ptr);
1067 status = apr_xlate_conv_buffer(convset, ptr, &inbytes_left, ptr, &outbytes_left);
1068 if (status) {
1069 return status;
1070 }
1072 }
1074 }
1075 return apr_xml_parser_convert_elem(pdoc->root, convset);
1076}
1077#endif
const char apr_size_t len
Definition ap_regex.h:187
const ap_regex_t char * errbuf
Definition ap_regex.h:198
APR Strings library.
APR Standard Headers Support.
static int find_prefix(apr_xml_parser *parser, const char *prefix)
Definition apr_xml.c:80
static apr_size_t elem_size(const apr_xml_elem *elem, int style, apr_array_header_t *namespaces, int *ns_map)
Definition apr_xml.c:663
static apr_size_t text_size(const apr_text *t)
Definition apr_xml.c:654
static const char APR_KW_xmlns_lang[]
Definition apr_xml.c:41
#define APR_XML_ERROR_EXPAT
Definition apr_xml.c:62
static void start_handler(void *userdata, const char *name, const char **attrs)
Definition apr_xml.c:143
static apr_status_t do_parse(apr_xml_parser *parser, const char *data, apr_size_t len, int is_final)
Definition apr_xml.c:430
#define DEBUG_CR
Definition apr_xml.c:38
static char * write_elem(char *s, const apr_xml_elem *elem, int style, apr_array_header_t *namespaces, int *ns_map)
Definition apr_xml.c:795
static const char * find_prefix_name(const apr_xml_elem *elem, int ns, int parent)
Definition apr_xml.c:124
#define APR_XML_NS_ERROR_INVALID_DECL
Definition apr_xml.c:46
static void default_handler(void *userData, const XML_Char *s, int len)
Definition apr_xml.c:384
#define APR_XML_NS_LEN(ns)
Definition apr_xml.c:649
static apr_status_t cleanup_parser(void *ctx)
Definition apr_xml.c:360
#define APR_XML_NS_IS_RESERVED(name)
Definition apr_xml.c:49
static char * write_text(char *s, const apr_text *t)
Definition apr_xml.c:785
static void cdata_handler(void *userdata, const char *data, int len)
Definition apr_xml.c:334
static void end_handler(void *userdata, const char *name)
Definition apr_xml.c:322
#define APR_XML_ERROR_PARSE_DONE
Definition apr_xml.c:63
static const char APR_KW_DAV[]
Definition apr_xml.c:42
static const char APR_KW_xmlns[]
Definition apr_xml.c:40
#define APR_XML_NS_ERROR_UNKNOWN_PREFIX
Definition apr_xml.c:45
APR-UTIL XML Library.
const XML_LChar * XML_ErrorString(enum XML_Error code)
Definition xmlparse.c:2429
#define XML_FALSE
Definition expat.h:59
void XML_SetElementHandler(XML_Parser parser, XML_StartElementHandler start, XML_EndElementHandler end)
Definition xmlparse.c:1691
void XML_SetCharacterDataHandler(XML_Parser parser, XML_CharacterDataHandler handler)
Definition xmlparse.c:1712
XML_Error
Definition expat.h:83
void XML_SetEntityDeclHandler(XML_Parser parser, XML_EntityDeclHandler handler)
Definition xmlparse.c:1883
enum XML_Status XML_Parse(XML_Parser parser, const char *s, int len, int isFinal)
Definition xmlparse.c:1926
enum XML_Status XML_StopParser(XML_Parser parser, XML_Bool resumable)
Definition xmlparse.c:2234
enum XML_Error XML_GetErrorCode(XML_Parser parser)
Definition xmlparse.c:2318
XML_Parser XML_ParserCreate(const XML_Char *encoding)
Definition xmlparse.c:766
void XML_ParserFree(XML_Parser parser)
Definition xmlparse.c:1537
void XML_SetDefaultHandler(XML_Parser parser, XML_DefaultHandler handler)
Definition xmlparse.c:1756
void XML_SetUserData(XML_Parser parser, void *userData)
Definition xmlparse.c:1637
char XML_Char
ap_conf_vector_t * base
#define APR_EGENERAL
Definition apr_errno.h:313
#define APR_EOF
Definition apr_errno.h:461
#define APR_ENOMEM
Definition apr_errno.h:683
apr_bucket * e
apr_brigade_flush void * ctx
apr_bucket apr_bucket_brigade * a
int apr_off_t * length
apr_pool_t const char apr_dbd_t const char ** error
Definition apr_dbd.h:143
int char apr_size_t errbufsize
Definition apr_dbm.h:184
APU_DECLARE(void)
Computes SipHash-2-4, producing a 64bit (APR_SIPHASH_DSIZE) hash from a message and a 128bit (APR_SIP...
Definition apr_xml.c:553
const void apr_size_t int colon
Definition apr_escape.h:355
const char * uri
Definition apr_uri.h:159
const apr_xml_elem int apr_array_header_t int const char apr_size_t * psize
Definition apr_xml.h:289
#define APR_XML_NS_NONE
Definition apr_xml.h:134
#define APR_XML_X2T_PARSED
Definition apr_xml.h:296
const apr_xml_elem int apr_array_header_t int * ns_map
Definition apr_xml.h:288
#define APR_XML_X2T_LANG_INNER
Definition apr_xml.h:294
#define APR_XML_NS_IS_ERROR(e)
Definition apr_xml.h:138
apr_xml_parser apr_xml_doc ** ppdoc
Definition apr_xml.h:229
apr_text_header const char * text
Definition apr_xml.h:78
apr_xml_parser ** parser
Definition apr_xml.h:228
#define APR_XML_X2T_FULL_NS_LANG
Definition apr_xml.h:295
#define APR_XML_ELEM_IS_EMPTY(e)
Definition apr_xml.h:196
const apr_xml_elem int style
Definition apr_xml.h:287
const apr_xml_elem int apr_array_header_t int const char ** pbuf
Definition apr_xml.h:288
apr_xml_parser apr_xml_doc apr_file_t * xmlfd
Definition apr_xml.h:230
apr_text_header * hdr
Definition apr_xml.h:77
apr_xml_doc ** pdoc
Definition apr_xml.h:255
#define APR_XML_X2T_FULL
Definition apr_xml.h:292
apr_xml_parser apr_xml_doc apr_file_t apr_size_t buffer_length
Definition apr_xml.h:231
#define APR_XML_GET_URI_ITEM(ary, i)
Definition apr_xml.h:339
const apr_xml_elem int apr_array_header_t * namespaces
Definition apr_xml.h:287
const char int quotes
Definition apr_xml.h:318
struct apr_xlate_t apr_xlate_t
Definition apr_xlate.h:39
const char apr_size_t * inbytes_left
Definition apr_xlate.h:119
const char apr_size_t char apr_size_t * outbytes_left
Definition apr_xlate.h:121
apr_size_t size
const char int apr_pool_t * pool
Definition apr_cstr.h:84
const char * value
Definition apr_env.h:51
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
void * data
char * buffer
apr_vformatter_buff_t * c
Definition apr_lib.h:175
apr_interval_time_t t
apr_pool_t * parent
Definition apr_pools.h:197
#define apr_pcalloc(p, size)
Definition apr_pools.h:465
const char * s
Definition apr_strings.h:95
const char const char *const const char *const apr_procattr_t * attr
int int status
apr_pool_t * p
Definition md_event.c:32
return NULL
Definition mod_so.c:359
int i
Definition mod_so.c:347
char * name
struct apr_bucket *volatile next
apr_text * last
Definition apr_xml.h:68
apr_text * first
Definition apr_xml.h:66
struct apr_text * next
Definition apr_xml.h:57
struct apr_xml_attr * next
Definition apr_xml.h:158
apr_array_header_t * namespaces
Definition apr_xml.h:204
apr_xml_elem * root
Definition apr_xml.h:202
struct apr_xml_elem * parent
Definition apr_xml.h:176
struct apr_xml_elem * next
Definition apr_xml.h:178
apr_text_header following_cdata
Definition apr_xml.h:173
const char * prefix
Definition apr_xml.c:72
struct apr_xml_ns_scope * next
Definition apr_xml.c:75
apr_xml_elem * cur_elem
Definition apr_xml.c:59
apr_pool_t * p
Definition apr_xml.c:58
XML_Parser xp
Definition apr_xml.c:66
apr_xml_doc * doc
Definition apr_xml.c:57
enum XML_Error xp_err
Definition apr_xml.c:67
#define ns(x)
Definition xmltok.c:1644