summaryrefslogtreecommitdiff
path: root/src/rexmpp_xml_parser.c
blob: 3f485d7a1513fd9ab0582490bb5f74f53a6b2e72 (plain)
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/**
   @file rexmpp_xml_parser.c
   @brief XML parsing for rexmpp
   @author defanor <defanor@uberspace.net>
   @date 2023
   @copyright MIT license.
*/

#include <string.h>

#include "rexmpp.h"
#include "rexmpp_xml.h"
#include "rexmpp_xml_parser.h"
#include "config.h"

#if defined(USE_LIBXML2)

void rexmpp_xml_sax_characters (rexmpp_xml_parser_ctx_t ctx,
                                const char *ch,
                                int len)
{
  ctx->handlers->text(ctx->user_data, ch, len);
}

void rexmpp_xml_sax_elem_start (rexmpp_xml_parser_ctx_t ctx,
                                const char *localname,
                                const char *prefix,
                                const char *URI,
                                int nb_namespaces,
                                const char **namespaces,
                                int nb_attributes,
                                int nb_defaulted,
                                const char **attributes)
{
  (void)prefix;
  (void)nb_namespaces;
  (void)namespaces;
  (void)nb_defaulted;
  rexmpp_xml_attr_t *attrs = NULL;
  int i;
  for (i = nb_attributes - 1; i >= 0; i--) {
    size_t attr_len = attributes[i * 5 + 4] - attributes[i * 5 + 3];
    char *attr_val = malloc(attr_len + 1);
    if (attr_val != NULL) {
      attr_val[attr_len] = '\0';
      strncpy(attr_val, attributes[i * 5 + 3], attr_len);
      rexmpp_xml_attr_t *attr =
        rexmpp_xml_attr_new(attributes[i * 5], NULL, attr_val);
      free(attr_val);
      attr->next = attrs;
      attrs = attr;
    }
  }

  ctx->handlers->elem_start(ctx->user_data, localname, URI, attrs);
}

void rexmpp_xml_sax_elem_end (rexmpp_xml_parser_ctx_t ctx,
                              const char *localname,
                              const char *prefix,
                              const char *URI)
{
  (void)localname;
  (void)prefix;
  (void)URI;
  ctx->handlers->elem_end(ctx->user_data);
}

xmlSAXHandler rexmpp_xml_parser_sax = {
  .initialized = XML_SAX2_MAGIC,
  .characters = (charactersSAXFunc)rexmpp_xml_sax_characters,
  .startElementNs = (startElementNsSAX2Func)rexmpp_xml_sax_elem_start,
  .endElementNs = (endElementNsSAX2Func)rexmpp_xml_sax_elem_end,
};


/* rexmpp_xml_t *rexmpp_xml_from_libxml2 (xmlNodePtr from) { */
/*   if (from == NULL) { */
/*     return NULL; */
/*   } */

/*   rexmpp_xml_t *to = NULL; */
/*   if (from->type == XML_ELEMENT_NODE) { */
/*     to = malloc(sizeof(rexmpp_xml_t)); */

/*     /\* Type *\/ */
/*     to->type = REXMPP_XML_ELEMENT; */

/*     /\* Name and namespace *\/ */
/*     to->alt.elem.qname.name = strdup(from->name); */
/*     if (from->nsDef != NULL && from->nsDef->href != NULL) { */
/*       to->alt.elem.qname.namespace = strdup(from->nsDef->href); */
/*     } else { */
/*       to->alt.elem.qname.namespace = NULL; */
/*     } */

/*     /\* Attributes *\/ */
/*     to->alt.elem.attributes = NULL; */
/*     struct _xmlAttr *from_attr; */
/*     rexmpp_xml_attr_t **to_next_attr = &(to->alt.elem.attributes); */
/*     for (from_attr = from->properties; */
/*          from_attr != NULL; */
/*          from_attr = from_attr->next) */
/*       { */
/*         rexmpp_xml_attr_t *to_attr = */
/*           malloc(sizeof(rexmpp_xml_attr_t)); */
/*         to_attr->qname.name = strdup(from_attr->name); */
/*         to_attr->qname.namespace = NULL; */
/*         if (from_attr->ns != NULL && from_attr->ns->href != NULL) { */
/*           to_attr->qname.namespace = strdup(from_attr->ns->href); */
/*           to_attr->value = */
/*             xmlGetNsProp(from, to_attr->qname.name, to_attr->qname.namespace); */
/*         } else { */
/*           to_attr->value = xmlGetProp(from, to_attr->qname.name); */
/*         } */
/*         to_attr->next = NULL; */

/*         *to_next_attr = to_attr; */
/*         to_next_attr = &(to_attr->next); */
/*       } */

/*     /\* Children *\/ */
/*     to->alt.elem.children = NULL; */
/*     xmlNodePtr from_child; */
/*     rexmpp_xml_t **to_next_child = &(to->alt.elem.children); */
/*     for (from_child = from->children; */
/*          from_child != NULL; */
/*          from_child = from_child->next) */
/*       { */
/*         rexmpp_xml_t *next_child = rexmpp_xml_from_libxml2(from_child); */
/*         if (next_child != NULL) { */
/*           *to_next_child = next_child; */
/*           to_next_child = &(next_child->next); */
/*         } */
/*       } */

/*     /\* Next *\/ */
/*     to->next = NULL; */

/*   } else if (from->type == XML_TEXT_NODE) { */
/*     to = malloc(sizeof(rexmpp_xml_t)); */
/*     to->type = REXMPP_XML_TEXT; */
/*     to->alt.text = xmlNodeGetContent(from); */
/*     to->next = NULL; */
/*   } */
/*   return to; */
/* } */

/* rexmpp_xml_t *rexmpp_xml_from_libxml2_list (xmlNodePtr from) { */
/*   if (from == NULL) { */
/*     return NULL; */
/*   } */
/*   rexmpp_xml_t *to = rexmpp_xml_from_libxml2(from); */
/*   if (from->next != NULL) { */
/*     to->next = rexmpp_xml_from_libxml2_list(from->next); */
/*   } */
/*   return to; */
/* } */

/* xmlNodePtr rexmpp_xml_to_libxml2 (rexmpp_xml_t *from) { */
/*   if (from == NULL) { */
/*     return NULL; */
/*   } */

/*   if (from->type == REXMPP_XML_TEXT) { */
/*     xmlNodePtr to = xmlNewText(from->alt.text); */
/*     to->next = rexmpp_xml_to_libxml2(from->next); */
/*     return to; */
/*   } */

/*   /\* Name and namespace *\/ */
/*   xmlNodePtr to = xmlNewNode(NULL, from->alt.elem.qname.name); */
/*   if (from->alt.elem.qname.namespace != NULL) { */
/*     xmlNewNs(to, from->alt.elem.qname.namespace, NULL); */
/*   } */

/*   /\* Attributes *\/ */
/*   rexmpp_xml_attr_t *attr = from->alt.elem.attributes; */
/*   while (attr != NULL) { */
/*     /\* TODO: Would be nice to take namespaces into account, though */
/*        they are currently not used for attributes. *\/ */
/*     xmlNewProp(to, attr->qname.name, attr->value); */
/*     attr = attr->next; */
/*   } */

/*   /\* Children *\/ */
/*   rexmpp_xml_t *child = from->alt.elem.children; */
/*   while (child != NULL) { */
/*     xmlAddChild(to, rexmpp_xml_to_libxml2(child)); */
/*     child = child->next; */
/*   } */
/*   return to; */
/* } */

/* xmlNodePtr rexmpp_xml_to_libxml2_list (rexmpp_xml_t *from) { */
/*   xmlNodePtr to = rexmpp_xml_to_libxml2(from); */
/*   if (from->next != NULL) { */
/*     xmlAddNextSibling(to, rexmpp_xml_to_libxml2_list(from->next)); */
/*   } */
/*   return to; */
/* } */

#elif defined(USE_EXPAT)

void XMLCALL
rexmpp_xml_sax_elem_start (rexmpp_xml_parser_ctx_t ctx,
                           const char *el,
                           const char **attributes)
{
  char *buf = strdup(el);
  char *name = NULL, *namespace = buf;
  size_t i;
  for (i = 0; i < strlen(namespace); i++) {
    if (namespace[i] == '\xff') {
      name = namespace + i + 1;
      namespace[i] = '\0';
    }
  }
  if (name == NULL) {
    name = namespace;
    namespace = NULL;
  }
  rexmpp_xml_attr_t *attrs = NULL;
  for (i = 0; attributes[i] != NULL; i += 2) {
    rexmpp_xml_attr_t *attr =
      rexmpp_xml_attr_new(attributes[i], NULL, attributes[i + 1]);
    attr->next = attrs;
    attrs = attr;
  }

  ctx->handlers->elem_start(ctx->user_data, name, namespace, attrs);
  free(buf);
}

void XMLCALL
rexmpp_xml_sax_elem_end(rexmpp_xml_parser_ctx_t ctx,
                        const XML_Char *name)
{
  (void)name;
  ctx->handlers->elem_end(ctx->user_data);
}

void XMLCALL
rexmpp_xml_sax_characters (rexmpp_xml_parser_ctx_t ctx,
                           const XML_Char *ch,
                           int len)
{
  ctx->handlers->text(ctx->user_data, ch, len);
}

#endif



rexmpp_xml_parser_ctx_t
rexmpp_xml_parser_new (rexmpp_xml_parser_handlers_t handlers,
                       void *data)
{
  rexmpp_xml_parser_ctx_t ctx = malloc(sizeof(struct rexmpp_xml_parser_ctx));
  if (ctx == NULL) {
    return NULL;
  }
#if defined(USE_LIBXML2)
  xmlParserCtxtPtr p =
    xmlCreatePushParserCtxt(&rexmpp_xml_parser_sax, ctx, "", 0, NULL);
#elif defined(USE_EXPAT)
  XML_Parser p = XML_ParserCreateNS("utf-8", '\xff');
  XML_SetUserData(p, ctx);
  XML_SetStartElementHandler(p, (XML_StartElementHandler)
                             rexmpp_xml_sax_elem_start);
  XML_SetEndElementHandler(p, (XML_EndElementHandler)
                           rexmpp_xml_sax_elem_end);
  XML_SetCharacterDataHandler(p, (XML_CharacterDataHandler)
                              rexmpp_xml_sax_characters);
#endif
  if (p == NULL) {
    free(ctx);
    return NULL;
  }

  ctx->xml_parser = p;
  ctx->handlers = handlers;
  ctx->user_data = data;
  return ctx;
}

void rexmpp_xml_parser_free (rexmpp_xml_parser_ctx_t ctx) {
#if defined(USE_LIBXML2)
  xmlFreeParserCtxt(ctx->xml_parser);
#elif defined(USE_EXPAT)
  XML_ParserFree(ctx->xml_parser);
#endif
  free(ctx);
}

rexmpp_xml_parser_ctx_t rexmpp_xml_parser_reset (rexmpp_xml_parser_ctx_t ctx) {
#if defined(USE_LIBXML2)
  xmlCtxtResetPush(ctx->xml_parser, "", 0, "", "utf-8");
#elif defined(USE_EXPAT)
  XML_ParserReset(ctx->xml_parser, "utf-8");
  XML_SetUserData(ctx->xml_parser, ctx);
  XML_SetStartElementHandler(ctx->xml_parser, (XML_StartElementHandler)
                             rexmpp_xml_sax_elem_start);
  XML_SetEndElementHandler(ctx->xml_parser, (XML_EndElementHandler)
                           rexmpp_xml_sax_elem_end);
  XML_SetCharacterDataHandler(ctx->xml_parser, (XML_CharacterDataHandler)
                              rexmpp_xml_sax_characters);
#endif
  return ctx;
}

void
rexmpp_xml_parser_feed (rexmpp_xml_parser_ctx_t ctx,
                        const char *chunk,
                        size_t len,
                        int final)
{
#if defined(USE_LIBXML2)
  xmlParseChunk(ctx->xml_parser, chunk, len, final);
#elif defined(USE_EXPAT)
  XML_Parse(ctx->xml_parser, chunk, len, final);
#endif
}