1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
/* Read an XML document from standard input and print
element declarations (if any) to standard output.
It must be used with Expat compiled for UTF-8 output.
__ __ _
___\ \/ /_ __ __ _| |_
/ _ \\ /| '_ \ / _` | __|
| __// \| |_) | (_| | |_
\___/_/\_\ .__/ \__,_|\__|
|_| XML parser
Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
Copyright (c) 2001-2003 Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
Copyright (c) 2004-2006 Karl Waclawek <karl@waclawek.net>
Copyright (c) 2005-2007 Steven Solie <steven@solie.ca>
Copyright (c) 2016-2024 Sebastian Pipping <sebastian@pipping.org>
Copyright (c) 2017 Rhodri James <rhodri@wildebeest.org.uk>
Copyright (c) 2019 Zhongyuan Zhou <zhouzhongyuan@huawei.com>
Copyright (c) 2024 Hanno Böck <hanno@gentoo.org>
Licensed under the MIT license:
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the
following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <expat.h>
#ifdef XML_LARGE_SIZE
# define XML_FMT_INT_MOD "ll"
#else
# define XML_FMT_INT_MOD "l"
#endif
#ifdef XML_UNICODE_WCHAR_T
# define XML_FMT_STR "ls"
#else
# define XML_FMT_STR "s"
#endif
// While traversing the XML_Content tree, we avoid recursion
// to not be vulnerable to a denial of service attack.
typedef struct StackStruct {
const XML_Content *model;
unsigned level;
struct StackStruct *prev;
} Stack;
static Stack *
stackPushMalloc(Stack *stackTop, const XML_Content *model, unsigned level) {
Stack *const newStackTop = malloc(sizeof(Stack));
if (! newStackTop) {
return NULL;
}
newStackTop->model = model;
newStackTop->level = level;
newStackTop->prev = stackTop;
return newStackTop;
}
static Stack *
stackPopFree(Stack *stackTop) {
Stack *const newStackTop = stackTop->prev;
free(stackTop);
return newStackTop;
}
static char *
contentTypeName(enum XML_Content_Type contentType) {
switch (contentType) {
case XML_CTYPE_EMPTY:
return "EMPTY";
case XML_CTYPE_ANY:
return "ANY";
case XML_CTYPE_MIXED:
return "MIXED";
case XML_CTYPE_NAME:
return "NAME";
case XML_CTYPE_CHOICE:
return "CHOICE";
case XML_CTYPE_SEQ:
return "SEQ";
default:
return "???";
}
}
static char *
contentQuantName(enum XML_Content_Quant contentQuant) {
switch (contentQuant) {
case XML_CQUANT_NONE:
return "NONE";
case XML_CQUANT_OPT:
return "OPT";
case XML_CQUANT_REP:
return "REP";
case XML_CQUANT_PLUS:
return "PLUS";
default:
return "???";
}
}
static void
dumpContentModelElement(const XML_Content *model, unsigned level,
const XML_Content *root) {
// Indent
unsigned u = 0;
for (; u < level; u++) {
printf(" ");
}
// Node
printf("[%u] type=%s(%u), quant=%s(%u)", (unsigned)(model - root),
contentTypeName(model->type), (unsigned int)model->type,
contentQuantName(model->quant), (unsigned int)model->quant);
if (model->name) {
printf(", name=\"%" XML_FMT_STR "\"", model->name);
} else {
printf(", name=NULL");
}
printf(", numchildren=%u", model->numchildren);
printf("\n");
}
static bool
dumpContentModel(const XML_Char *name, const XML_Content *root) {
printf("Element \"%" XML_FMT_STR "\":\n", name);
Stack *stackTop = stackPushMalloc(NULL, root, 1);
if (! stackTop) {
return false;
}
while (stackTop) {
const XML_Content *const model = stackTop->model;
const unsigned level = stackTop->level;
dumpContentModelElement(model, level, root);
stackTop = stackPopFree(stackTop);
for (size_t u = model->numchildren; u >= 1; u--) {
Stack *const newStackTop
= stackPushMalloc(stackTop, model->children + (u - 1), level + 1);
if (! newStackTop) {
// We ran out of memory, so let's free all memory allocated
// earlier in this function, to be leak-clean:
while (stackTop != NULL) {
stackTop = stackPopFree(stackTop);
}
return false;
}
stackTop = newStackTop;
}
}
printf("\n");
return true;
}
static void XMLCALL
handleElementDeclaration(void *userData, const XML_Char *name,
XML_Content *model) {
XML_Parser parser = (XML_Parser)userData;
const bool success = dumpContentModel(name, model);
XML_FreeContentModel(parser, model);
if (! success) {
XML_StopParser(parser, /* resumable= */ XML_FALSE);
}
}
int
main(void) {
XML_Parser parser = XML_ParserCreate(NULL);
int done;
if (! parser) {
fprintf(stderr, "Couldn't allocate memory for parser\n");
return 1;
}
XML_SetUserData(parser, parser);
XML_SetElementDeclHandler(parser, handleElementDeclaration);
do {
void *const buf = XML_GetBuffer(parser, BUFSIZ);
if (! buf) {
fprintf(stderr, "Couldn't allocate memory for buffer\n");
XML_ParserFree(parser);
return 1;
}
const size_t len = fread(buf, 1, BUFSIZ, stdin);
if (ferror(stdin)) {
fprintf(stderr, "Read error\n");
XML_ParserFree(parser);
return 1;
}
done = feof(stdin);
if (XML_ParseBuffer(parser, (int)len, done) == XML_STATUS_ERROR) {
enum XML_Error errorCode = XML_GetErrorCode(parser);
if (errorCode == XML_ERROR_ABORTED) {
errorCode = XML_ERROR_NO_MEMORY;
}
fprintf(stderr,
"Parse error at line %" XML_FMT_INT_MOD "u:\n%" XML_FMT_STR "\n",
XML_GetCurrentLineNumber(parser), XML_ErrorString(errorCode));
XML_ParserFree(parser);
return 1;
}
} while (! done);
XML_ParserFree(parser);
return 0;
}
|