/
parser_libxml2.c
298 lines (254 loc) · 8.1 KB
/
parser_libxml2.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/* parser.c
** strophe XMPP client library -- xml parser handlers and utility functions
**
** Copyright (C) 2005-2009 Collecta, Inc.
**
** This software is provided AS-IS with no warranty, either express
** or implied.
**
** This program is dual licensed under the MIT and GPLv3 licenses.
*/
/** @file
* XML parser handlers.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include "strophe.h"
#include "common.h"
#include "parser.h"
struct _parser_t {
xmpp_ctx_t *ctx;
xmlParserCtxtPtr xmlctx;
xmlSAXHandler handlers;
parser_start_callback startcb;
parser_end_callback endcb;
parser_stanza_callback stanzacb;
void *userdata;
int depth;
xmpp_stanza_t *stanza;
};
static void
_set_attributes(xmpp_stanza_t *stanza, int nattrs, const xmlChar **attrs)
{
int i, len;
char *value;
if (!attrs)
return;
/* SAX2 uses array of localname/prefix/uri/value_begin/value_end */
for (i = 0; i < nattrs * 5; i += 5) {
len = attrs[i + 4] - attrs[i + 3];
value = xmpp_alloc(stanza->ctx, len + 1);
if (value) {
memcpy(value, attrs[i + 3], len);
value[len] = '\0';
xmpp_stanza_set_attribute(stanza, (const char *)attrs[i], value);
xmpp_free(stanza->ctx, value);
}
}
}
/* SAX2 gives us the attrs in an incredibly inconvenient array,
* convert it to what the start callback is expecting */
static char **
_convert_attrs(parser_t *parser, int nattrs, const xmlChar **attrs)
{
int c, i, o, len;
char *value;
char **ret;
if (!attrs)
return NULL;
ret = xmpp_alloc(parser->ctx, (nattrs + 1) * 2 * sizeof(char *));
if (!ret)
return NULL;
memset(ret, 0, (nattrs + 1) * 2 * sizeof(char *));
for (c = 0; c < nattrs; c++) {
i = c * 5;
o = c * 2;
len = attrs[i + 4] - attrs[i + 3];
value = xmpp_alloc(parser->ctx, len + 1);
if (value) {
memcpy(value, attrs[i + 3], len);
value[len] = '\0';
ret[o] = xmpp_strdup(parser->ctx, (char *)attrs[i]);
ret[o + 1] = value;
}
}
return ret;
}
static void _free_cbattrs(parser_t *parser, char **attrs)
{
int i;
if (!attrs)
return;
for (i = 0; attrs[i]; i += 2) {
if (attrs[i])
xmpp_free(parser->ctx, attrs[i]);
if (attrs[i + 1])
xmpp_free(parser->ctx, attrs[i + 1]);
}
xmpp_free(parser->ctx, attrs);
}
static void _start_element(void *userdata,
const xmlChar *name,
const xmlChar *prefix,
const xmlChar *uri,
int nnamespaces,
const xmlChar **namespaces,
int nattrs,
int ndefaulted,
const xmlChar **attrs)
{
parser_t *parser = (parser_t *)userdata;
xmpp_stanza_t *child;
char **cbattrs;
UNUSED(prefix);
UNUSED(nnamespaces);
UNUSED(namespaces);
UNUSED(ndefaulted);
if (parser->depth == 0) {
/* notify the owner */
if (parser->startcb) {
cbattrs = _convert_attrs(parser, nattrs, attrs);
parser->startcb((char *)name, cbattrs, parser->userdata);
_free_cbattrs(parser, cbattrs);
}
} else {
/* build stanzas at depth 1 */
if (!parser->stanza && parser->depth != 1) {
/* something terrible happened */
/* FIXME: we should probably trigger a disconnect */
xmpp_error(parser->ctx, "parser", "oops, where did our stanza go?");
} else if (!parser->stanza) {
/* starting a new toplevel stanza */
parser->stanza = xmpp_stanza_new(parser->ctx);
if (!parser->stanza) {
/* FIXME: can't allocate, disconnect */
}
xmpp_stanza_set_name(parser->stanza, (char *)name);
_set_attributes(parser->stanza, nattrs, attrs);
if (uri)
xmpp_stanza_set_ns(parser->stanza, (char *)uri);
} else {
/* starting a child of conn->stanza */
child = xmpp_stanza_new(parser->ctx);
if (!child) {
/* FIXME: can't allocate, disconnect */
}
xmpp_stanza_set_name(child, (char *)name);
_set_attributes(child, nattrs, attrs);
if (uri)
xmpp_stanza_set_ns(child, (char *)uri);
/* add child to parent */
xmpp_stanza_add_child(parser->stanza, child);
/* the child is owned by the toplevel stanza now */
xmpp_stanza_release(child);
/* make child the current stanza */
parser->stanza = child;
}
}
parser->depth++;
}
static void _end_element(void *userdata,
const xmlChar *name,
const xmlChar *prefix,
const xmlChar *uri)
{
parser_t *parser = (parser_t *)userdata;
UNUSED(prefix);
UNUSED(uri);
parser->depth--;
if (parser->depth == 0) {
/* notify owner */
if (parser->endcb)
parser->endcb((char *)name, parser->userdata);
} else {
if (parser->stanza->parent) {
/* we're finishing a child stanza, so set current to the parent */
parser->stanza = parser->stanza->parent;
} else {
if (parser->stanzacb)
parser->stanzacb(parser->stanza, parser->userdata);
xmpp_stanza_release(parser->stanza);
parser->stanza = NULL;
}
}
}
static void _characters(void *userdata, const xmlChar *chr, int len)
{
parser_t *parser = (parser_t *)userdata;
xmpp_stanza_t *stanza;
/* skip unimportant whitespace, etc */
if (parser->depth < 2)
return;
/* create and populate stanza */
stanza = xmpp_stanza_new(parser->ctx);
if (!stanza) {
/* FIXME: allocation error, disconnect */
return;
}
xmpp_stanza_set_text_with_size(stanza, (char *)chr, len);
xmpp_stanza_add_child(parser->stanza, stanza);
xmpp_stanza_release(stanza);
}
/* create a new parser */
parser_t *parser_new(xmpp_ctx_t *ctx,
parser_start_callback startcb,
parser_end_callback endcb,
parser_stanza_callback stanzacb,
void *userdata)
{
parser_t *parser;
parser = xmpp_alloc(ctx, sizeof(parser_t));
if (parser != NULL) {
parser->ctx = ctx;
parser->xmlctx = NULL;
memset(&parser->handlers, 0, sizeof(xmlSAXHandler));
parser->handlers.initialized = XML_SAX2_MAGIC;
parser->handlers.startElementNs = _start_element;
parser->handlers.endElementNs = _end_element;
parser->handlers.characters = _characters;
parser->startcb = startcb;
parser->endcb = endcb;
parser->stanzacb = stanzacb;
parser->userdata = userdata;
parser->depth = 0;
parser->stanza = NULL;
parser_reset(parser);
}
return parser;
}
char *parser_attr_name(xmpp_ctx_t *ctx, char *nsname)
{
return xmpp_strdup(ctx, nsname);
}
/* free a parser */
void parser_free(parser_t *parser)
{
if (parser->xmlctx)
xmlFreeParserCtxt(parser->xmlctx);
if (parser->stanza)
xmpp_stanza_release(parser->stanza);
xmpp_free(parser->ctx, parser);
}
/* shuts down and restarts XML parser. true on success */
int parser_reset(parser_t *parser)
{
if (parser->xmlctx)
xmlFreeParserCtxt(parser->xmlctx);
if (parser->stanza)
xmpp_stanza_release(parser->stanza);
parser->stanza = NULL;
parser->depth = 0;
parser->xmlctx =
xmlCreatePushParserCtxt(&parser->handlers, parser, NULL, 0, NULL);
return parser->xmlctx ? 1 : 0;
}
/* feed a chunk of data to the parser */
int parser_feed(parser_t *parser, char *chunk, int len)
{
/* xmlParseChunk API returns 0 on success which is opposite logic to
the status returned by parser_feed */
return !xmlParseChunk(parser->xmlctx, chunk, len, 0);
}