summaryrefslogtreecommitdiff
path: root/src/rexmpp_xml.c
blob: f38447052f12479eb521a4b1acd1aa9d6521865e (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/**
   @file rexmpp_xml.c
   @brief XML structures and functions for rexmpp
   @author defanor <defanor@uberspace.net>
   @date 2023
   @copyright MIT license.
*/

#include <string.h>
#include <libxml/tree.h>
#include <libxml/xmlsave.h>
#include "rexmpp.h"
#include "rexmpp_xml.h"

#ifndef USE_RUST
void rexmpp_xml_qname_free (rexmpp_xml_qname_t *qname) {
  if (qname->name != NULL) {
    free(qname->name);
    qname->name = NULL;
  }
  if (qname->namespace != NULL) {
    free(qname->namespace);
    qname->namespace = NULL;
  }
}

void rexmpp_xml_attribute_free (rexmpp_xml_attr_t *attr) {
  if (attr == NULL) {
    return;
  }
  rexmpp_xml_qname_free(&(attr->qname));
  if (attr->value != NULL) {
    free(attr->value);
    attr->value = NULL;
  }
  free(attr);
}

void rexmpp_xml_attribute_free_list (rexmpp_xml_attr_t *attr) {
  rexmpp_xml_attr_t *next = attr;
  while (attr != NULL) {
    next = attr->next;
    rexmpp_xml_attribute_free(attr);
    attr = next;
  }
}

void rexmpp_xml_free (rexmpp_xml_t *node) {
  if (node == NULL) {
    return;
  }
  if (node->type == REXMPP_XML_TEXT) {
    if (node->alt.text != NULL) {
      free(node->alt.text);
      node->alt.text = NULL;
    }
  } if (node->type == REXMPP_XML_ELEMENT) {
    rexmpp_xml_qname_free(&(node->alt.elem.qname));
    rexmpp_xml_attribute_free_list(node->alt.elem.attributes);
    rexmpp_xml_free_list(node->alt.elem.children);
  }
  free(node);
}

void rexmpp_xml_free_list (rexmpp_xml_t *node) {
  rexmpp_xml_t *next = node;
  while (node != NULL) {
    next = node->next;
    rexmpp_xml_free(node);
    node = next;
  }
}

rexmpp_xml_t *rexmpp_xml_clone (rexmpp_xml_t *node) {
  if (node == NULL) {
    return NULL;
  }

  if (node->type == REXMPP_XML_TEXT) {
    return rexmpp_xml_new_text(node->alt.text);
  } else if (node->type == REXMPP_XML_ELEMENT) {
    rexmpp_xml_t *ret =
      rexmpp_xml_new_elem(node->alt.elem.qname.name,
                          node->alt.elem.qname.namespace);
    rexmpp_xml_attr_t **next_attr = &(ret->alt.elem.attributes);
    rexmpp_xml_attr_t *old_attr;
    for (old_attr = node->alt.elem.attributes;
         old_attr != NULL;
         old_attr = old_attr->next)
      {
        rexmpp_xml_attr_t *new_attr =
          rexmpp_xml_attr_new(old_attr->qname.name,
                              old_attr->qname.namespace,
                              old_attr->value);
        *next_attr = new_attr;
        next_attr = &(new_attr->next);
      }

    ret->alt.elem.children =
      rexmpp_xml_clone_list(node->alt.elem.children);
    return ret;
  }
  return NULL;
}

rexmpp_xml_t *rexmpp_xml_clone_list (rexmpp_xml_t *node) {
  rexmpp_xml_t *first, *last;
  if (node == NULL) {
    return NULL;
  }
  first = rexmpp_xml_clone(node);
  for (last = first, node = node->next;
       node != NULL;
       last = last->next, node = node->next)
    {
      last->next = rexmpp_xml_clone(node);
    }
  return first;
}
#endif

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;
}

#ifndef USE_RUST
rexmpp_xml_t *rexmpp_xml_new_text (const char *str) {
  rexmpp_xml_t *node = malloc(sizeof(rexmpp_xml_t));
  node->type = REXMPP_XML_TEXT;
  node->alt.text = strdup(str);
  node->next = NULL;
  return node;
}

void rexmpp_xml_add_child (rexmpp_xml_t *node,
                           rexmpp_xml_t *child)
{
  rexmpp_xml_t **last_ptr = &(node->alt.elem.children);
  while (*last_ptr != NULL) {
    last_ptr = &((*last_ptr)->next);
  }
  *last_ptr = child;
}

int rexmpp_xml_add_text (rexmpp_xml_t *node,
                         const char *str)
{
  rexmpp_xml_t *text_node = rexmpp_xml_new_text(str);
  if (text_node != NULL) {
    rexmpp_xml_add_child(node, text_node);
    return 0;
  }
  return -1;
}

rexmpp_xml_t *rexmpp_xml_new_elem (const char *name,
                                   const char *namespace)
{
  rexmpp_xml_t *node = malloc(sizeof(rexmpp_xml_t));
  node->type = REXMPP_XML_ELEMENT;
  node->alt.elem.qname.name = strdup(name);
  if (namespace != NULL) {
    node->alt.elem.qname.namespace = strdup(namespace);
  } else {
    node->alt.elem.qname.namespace = NULL;
  }
  node->alt.elem.attributes = NULL;
  node->alt.elem.children = NULL;
  node->next = NULL;
  return node;
}

rexmpp_xml_attr_t *rexmpp_xml_attr_new (const char *name,
                                        const char *namespace,
                                        const char *value)
{
  rexmpp_xml_attr_t *attr = malloc(sizeof(rexmpp_xml_attr_t));
  attr->qname.name = strdup(name);
  if (namespace != NULL) {
    attr->qname.namespace = strdup(namespace);
  } else {
    attr->qname.namespace = NULL;
  }
  attr->value = strdup(value);
  attr->next = NULL;
  return attr;
}

int rexmpp_xml_add_attr_ns (rexmpp_xml_t *node,
                            const char *name,
                            const char *namespace,
                            const char *value)
{
  if (node == NULL || node->type != REXMPP_XML_ELEMENT) {
    return -1;
  }
  rexmpp_xml_attr_t *attr =
    rexmpp_xml_attr_new(name, namespace, value);
  attr->next = node->alt.elem.attributes;
  node->alt.elem.attributes = attr;
  return 0;
}

int rexmpp_xml_remove_attr_ns (rexmpp_xml_t *node,
                               const char *name,
                               const char *namespace) {
  if (node == NULL || node->type != REXMPP_XML_ELEMENT) {
    return -1;
  }

  rexmpp_xml_attr_t **attr, *next_attr;
  for (attr = &(node->alt.elem.attributes); *attr != NULL; attr = &((*attr)->next)) {
    if (rexmpp_xml_attr_match(*attr, namespace, name)) {
      next_attr = (*attr)->next;
      rexmpp_xml_attribute_free(*attr);
      *attr = next_attr;
      return 0;
    }
  }
  return 1;
}

int rexmpp_xml_add_attr (rexmpp_xml_t *node,
                         const char *name,
                         const char *value)
{
  return rexmpp_xml_add_attr_ns(node, name, NULL, value);
}

int rexmpp_xml_remove_attr (rexmpp_xml_t *node,
                            const char *name) {
  return rexmpp_xml_remove_attr_ns(node, name, NULL);
}
#endif

rexmpp_xml_t *
rexmpp_xml_add_id (rexmpp_t *s,
                   rexmpp_xml_t *node)
{
  char *buf = rexmpp_gen_id(s);
  if (buf == NULL) {
    return NULL;
  }
  rexmpp_xml_add_attr(node, "id", buf);
  free(buf);
  return node;
}

char *rexmpp_xml_serialize (rexmpp_xml_t *node) {
  xmlNodePtr node_libxml2 = rexmpp_xml_to_libxml2(node);
  xmlBufferPtr buf = xmlBufferCreate();
  xmlSaveCtxtPtr ctx = xmlSaveToBuffer(buf, "utf-8", 0);
  xmlSaveTree(ctx, node_libxml2);
  xmlSaveFlush(ctx);
  xmlSaveClose(ctx);
  unsigned char *out = xmlBufferDetach(buf);
  xmlBufferFree(buf);
  xmlFreeNode(node_libxml2);
  return out;
}

xmlNodePtr rexmpp_xml_parse_libxml2 (const char *str, int str_len) {
  xmlNodePtr elem = NULL;
  xmlDocPtr doc = xmlReadMemory(str, str_len, "", "utf-8", XML_PARSE_NONET);
  if (doc != NULL) {
    elem = xmlCopyNode(xmlDocGetRootElement(doc), 1);
    xmlFreeDoc(doc);
  }
  return elem;
}

rexmpp_xml_t *rexmpp_xml_parse (const char *str, int str_len) {
  xmlNodePtr node_lxml2 = rexmpp_xml_parse_libxml2(str, str_len);
  if (node_lxml2 != NULL) {
    rexmpp_xml_t *node = rexmpp_xml_from_libxml2(node_lxml2);
    xmlFreeNode(node_lxml2);
    return node;
  }
  return NULL;
}

rexmpp_xml_t *rexmpp_xml_read_file (const char *path) {
  xmlDocPtr doc = xmlReadFile(path, "utf-8", XML_PARSE_NONET);
  xmlNodePtr lxml2 = xmlDocGetRootElement(doc);
  rexmpp_xml_t *ret = rexmpp_xml_from_libxml2(lxml2);
  xmlFreeDoc(doc);
  return ret;
}

int rexmpp_xml_write_file (const char *path, rexmpp_xml_t* node) {
  xmlDocPtr doc = xmlNewDoc("1.0");
  xmlNodePtr node_lxml2 = rexmpp_xml_to_libxml2(node);
  xmlDocSetRootElement(doc, node_lxml2);
  xmlSaveFileEnc(path, doc, "utf-8");
  xmlFreeDoc(doc);
  return 0;
}

#ifndef USE_RUST
unsigned int rexmpp_xml_siblings_count (rexmpp_xml_t *node) {
  unsigned int i = 0;
  for (i = 0; node != NULL; i++) {
    node = node->next;
  }
  return i;
}

int rexmpp_xml_match (rexmpp_xml_t *node,
                      const char *namespace,
                      const char *name)
{
  if (node == NULL) {
    return 0;
  }
  if (node->type != REXMPP_XML_ELEMENT) {
    return 0;
  }
  if (name != NULL) {
    if (strcmp(name, node->alt.elem.qname.name) != 0) {
      return 0;
    }
  }
  if (namespace != NULL) {
    if (node->alt.elem.qname.namespace == NULL &&
        strcmp(namespace, "jabber:client") != 0) {
      return 0;
    } else if (node->alt.elem.qname.namespace != NULL) {
      if (strcmp(namespace, node->alt.elem.qname.namespace) != 0) {
        return 0;
      }
    }
  }
  return 1;
}

int rexmpp_xml_attr_match (rexmpp_xml_attr_t *attr,
                           const char *namespace,
                           const char *name)
{
  if (attr == NULL) {
    return 0;
  }
  if (name != NULL) {
    if (strcmp(name, attr->qname.name) != 0) {
      return 0;
    }
  }
  if (namespace != NULL) {
    if (attr->qname.namespace == NULL &&
        strcmp(namespace, "jabber:client") != 0) {
      return 0;
    } else if (strcmp(namespace, attr->qname.namespace) != 0) {
      return 0;
    }
  }
  return 1;
}

int rexmpp_xml_is_stanza (rexmpp_xml_t *node) {
  return rexmpp_xml_match(node, "jabber:client", "message") ||
    rexmpp_xml_match(node, "jabber:client", "iq") ||
    rexmpp_xml_match(node, "jabber:client", "presence");
}

rexmpp_xml_t *rexmpp_xml_error (const char *type, const char *condition) {
  rexmpp_xml_t * error = rexmpp_xml_new_elem("error", NULL);
  rexmpp_xml_add_attr(error, "type", type);
  rexmpp_xml_t * cond =
    rexmpp_xml_new_elem(condition, "urn:ietf:params:xml:ns:xmpp-stanzas");
  rexmpp_xml_add_child(error, cond);
  return error;
}

rexmpp_xml_attr_t *
rexmpp_xml_find_attr (rexmpp_xml_t *node,
                      const char *name,
                      const char *namespace)
{
  if (node == NULL || node->type != REXMPP_XML_ELEMENT) {
    return NULL;
  }
  rexmpp_xml_attr_t *attr;
  for (attr = node->alt.elem.attributes; attr != NULL; attr = attr->next) {
    if (rexmpp_xml_attr_match(attr, namespace, name)) {
      return attr;
    }
  }
  return NULL;
}

const char *rexmpp_xml_find_attr_val_ns (rexmpp_xml_t *node,
                                         const char *name,
                                         const char *namespace) {
  rexmpp_xml_attr_t *attr = rexmpp_xml_find_attr(node, name, namespace);
  if (attr != NULL) {
    return attr->value;
  }
  return NULL;
}

const char *rexmpp_xml_find_attr_val (rexmpp_xml_t *node,
                                      const char *name) {
  return rexmpp_xml_find_attr_val_ns(node, name, NULL);
}

rexmpp_xml_t *
rexmpp_xml_find_child (rexmpp_xml_t *node,
                       const char *namespace,
                       const char *name)
{
  if (node == NULL || node->type != REXMPP_XML_ELEMENT) {
    return NULL;
  }
  rexmpp_xml_t *child;
  for (child = node->alt.elem.children; child != NULL; child = child->next) {
    if (rexmpp_xml_match(child, namespace, name)) {
      return child;
    }
  }
  return NULL;
}
#endif

int rexmpp_xml_eq (rexmpp_xml_t *n1, rexmpp_xml_t *n2) {
  /* Just serialize and compare strings for now: awkward, but
     simple. */
  char *n1str = rexmpp_xml_serialize(n1);
  char *n2str = rexmpp_xml_serialize(n2);
  int eq = (strcmp(n1str, n2str) == 0);
  free(n1str);
  free(n2str);
  return eq;
}

#ifndef USE_RUST
rexmpp_xml_t *rexmpp_xml_children (rexmpp_xml_t *node) {
  if (node != NULL && node->type == REXMPP_XML_ELEMENT) {
    return node->alt.elem.children;
  }
  return NULL;
}

rexmpp_xml_t *rexmpp_xml_first_elem_child (rexmpp_xml_t *node) {
  rexmpp_xml_t *child;
  for (child = rexmpp_xml_children(node); child != NULL; child = child->next) {
    if (child->type == REXMPP_XML_ELEMENT) {
      return child;
    }
  }
  return NULL;
}

rexmpp_xml_t *rexmpp_xml_next_elem_sibling (rexmpp_xml_t *node) {
  if (node == NULL) {
    return NULL;
  }
  rexmpp_xml_t *sibling;
  for (sibling = node->next; sibling != NULL; sibling = sibling->next) {
    if (sibling->type == REXMPP_XML_ELEMENT) {
      return sibling;
    }
  }
  return NULL;
}

char *rexmpp_xml_text (rexmpp_xml_t *node) {
  if (node != NULL && node->type == REXMPP_XML_TEXT) {
    return node->alt.text;
  }
  return NULL;
}

char *rexmpp_xml_text_child (rexmpp_xml_t *node) {
  return rexmpp_xml_text(rexmpp_xml_children(node));
}
#endif