forked from xdebug/xdebug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xdebug_xml.c
214 lines (185 loc) · 5.56 KB
/
xdebug_xml.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
+----------------------------------------------------------------------+
| Xdebug |
+----------------------------------------------------------------------+
| Copyright (c) 2002-2018 Derick Rethans |
+----------------------------------------------------------------------+
| This source file is subject to version 1.01 of the Xdebug license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| https://xdebug.org/license.php |
| If you did not receive a copy of the Xdebug license and are unable |
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Derick Rethans <[email protected]> |
+----------------------------------------------------------------------+
*/
#include <stdlib.h>
#include <stdio.h>
#include "xdebug_mm.h"
#include "xdebug_str.h"
#include "xdebug_var.h"
#include "xdebug_xml.h"
#include "xdebug_compat.h"
static void xdebug_xml_return_attribute(xdebug_xml_attribute* attr, xdebug_str* output)
{
char *tmp;
size_t newlen;
xdebug_str_addl(output, " ", 1, 0);
/* attribute name */
tmp = xdebug_xmlize(attr->name, attr->name_len, &newlen);
xdebug_str_addl(output, tmp, newlen, 0);
efree(tmp);
/* attribute value */
xdebug_str_addl(output, "=\"", 2, 0);
if (attr->value) {
tmp = xdebug_xmlize(attr->value, attr->value_len, &newlen);
xdebug_str_add(output, tmp, 0);
efree(tmp);
}
xdebug_str_addl(output, "\"", 1, 0);
if (attr->next) {
xdebug_xml_return_attribute(attr->next, output);
}
}
static void xdebug_xml_return_text_node(xdebug_xml_text_node* node, xdebug_str* output)
{
xdebug_str_addl(output, "<![CDATA[", 9, 0);
if (node->encode) {
/* if cdata tags are in the text, then we must base64 encode */
int new_len = 0;
char *encoded_text;
encoded_text = (char*) xdebug_base64_encode((unsigned char*) node->text, node->text_len, &new_len);
xdebug_str_add(output, encoded_text, 0);
efree(encoded_text);
} else {
xdebug_str_add(output, node->text, 0);
}
xdebug_str_addl(output, "]]>", 3, 0);
}
void xdebug_xml_return_node(xdebug_xml_node* node, struct xdebug_str *output)
{
xdebug_str_addl(output, "<", 1, 0);
xdebug_str_add(output, node->tag, 0);
if (node->text && node->text->encode) {
xdebug_xml_add_attribute_ex(node, "encoding", "base64", 0, 0);
}
if (node->attribute) {
xdebug_xml_return_attribute(node->attribute, output);
}
xdebug_str_addl(output, ">", 1, 0);
if (node->child) {
xdebug_xml_return_node(node->child, output);
}
if (node->text) {
xdebug_xml_return_text_node(node->text, output);
}
xdebug_str_addl(output, "</", 2, 0);
xdebug_str_add(output, node->tag, 0);
xdebug_str_addl(output, ">", 1, 0);
if (node->next) {
xdebug_xml_return_node(node->next, output);
}
}
xdebug_xml_node *xdebug_xml_node_init_ex(const char *tag, int free_tag)
{
xdebug_xml_node *xml = xdmalloc(sizeof (xdebug_xml_node));
xml->tag = (char*) tag;
xml->text = NULL;
xml->child = NULL;
xml->attribute = NULL;
xml->next = NULL;
xml->free_tag = free_tag;
return xml;
}
void xdebug_xml_add_attribute_exl(xdebug_xml_node* xml, const char *attribute, size_t attribute_len, const char *value, size_t value_len, int free_name, int free_value)
{
xdebug_xml_attribute *attr = xdmalloc(sizeof (xdebug_xml_attribute));
xdebug_xml_attribute **ptr;
/* Init structure */
attr->name = (char*) attribute;
attr->value = (char*) value;
attr->name_len = attribute_len;
attr->value_len = value_len;
attr->next = NULL;
attr->free_name = free_name;
attr->free_value = free_value;
/* Find last attribute in node */
ptr = &xml->attribute;
while (*ptr != NULL) {
ptr = &(*ptr)->next;
}
*ptr = attr;
}
void xdebug_xml_add_child(xdebug_xml_node *xml, xdebug_xml_node *child)
{
xdebug_xml_node **ptr;
ptr = &xml->child;
while (*ptr != NULL) {
ptr = &((*ptr)->next);
}
*ptr = child;
}
static void xdebug_xml_text_node_dtor(xdebug_xml_text_node* node)
{
if (node->free_value && node->text) {
xdfree(node->text);
}
xdfree(node);
}
void xdebug_xml_add_text(xdebug_xml_node *xml, char *text)
{
xdebug_xml_add_text_ex(xml, text, strlen(text), 1, 0);
}
void xdebug_xml_add_text_encode(xdebug_xml_node *xml, char *text)
{
xdebug_xml_add_text_ex(xml, text, strlen(text), 1, 1);
}
void xdebug_xml_add_text_ex(xdebug_xml_node *xml, char *text, int length, int free_text, int encode)
{
xdebug_xml_text_node *node = xdmalloc(sizeof (xdebug_xml_text_node));
node->free_value = free_text;
node->encode = encode;
if (xml->text) {
xdebug_xml_text_node_dtor(xml->text);
}
node->text = text;
node->text_len = length;
xml->text = node;
if (!encode && strstr(node->text, "]]>")) {
node->encode = 1;
}
}
static void xdebug_xml_attribute_dtor(xdebug_xml_attribute *attr)
{
if (attr->next) {
xdebug_xml_attribute_dtor(attr->next);
}
if (attr->free_name) {
xdfree(attr->name);
}
if (attr->free_value) {
xdfree(attr->value);
}
xdfree(attr);
}
void xdebug_xml_node_dtor(xdebug_xml_node* xml)
{
if (xml->next) {
xdebug_xml_node_dtor(xml->next);
}
if (xml->child) {
xdebug_xml_node_dtor(xml->child);
}
if (xml->attribute) {
xdebug_xml_attribute_dtor(xml->attribute);
}
if (xml->free_tag) {
xdfree(xml->tag);
}
if (xml->text) {
xdebug_xml_text_node_dtor(xml->text);
}
xdfree(xml);
}