aboutsummaryrefslogtreecommitdiff
path: root/src/upnp/upnp_xmlnode.c
blob: b37528f006f5aae1b1cf1efe3557c6fa7cca245c (plain) (blame)
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
/**
 * @file xmlnode.c XML DOM functions
 *
 * gaim
 *
 * Gaim is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/* A lot of this code at least resembles the code in libxode, but since
 * libxode uses memory pools that we simply have no need for, I decided to
 * write my own stuff.  Also, re-writing this lets me be as lightweight
 * as I want to be.  Thank you libxode for giving me a good starting point */

#include "platform.h"

#include "util.h"
#include "gnunet_util.h"
#include "xmlnode.h"

#include <libxml/parser.h>
#include <string.h>


#ifdef _WIN32
# define NEWLINE_S "\r\n"
#else
# define NEWLINE_S "\n"
#endif

#define TRUE GNUNET_YES
#define FALSE GNUNET_NO

#define g_return_if_fail(a) if(!(a)) return;
#define g_return_val_if_fail(a, val) if(!(a)) return (val);

/**
 * The valid types for an xmlnode
 */
typedef enum _XMLNodeType
{
  XMLNODE_TYPE_TAG,     /**< Just a tag */
  XMLNODE_TYPE_ATTRIB,          /**< Has attributes */
  XMLNODE_TYPE_DATA     /**< Has data */
} XMLNodeType;

typedef struct
{
  xmlnode *current;
  xmlnode **nodes;
  unsigned int pos;
  unsigned int size;
} XMLNodePool;

struct _xmlnode
{
  char *name;           /**< The name of the node. */
  char *xmlns;          /**< The namespace of the node */
  XMLNodeType type;     /**< The type of the node. */
  char *data;           /**< The data for the node. */
  size_t data_sz;               /**< The size of the data. */
  struct _xmlnode *parent;  /**< The parent node or @c NULL.*/
  struct _xmlnode *child;       /**< The child node or @c NULL.*/
  struct _xmlnode *lastchild;  /**< The last child node or @c NULL.*/
  struct _xmlnode *next;        /**< The next node or @c NULL. */
  XMLNodePool *pool;
  int free_pool;                /* set to GNUNET_YES for the root node, which must free the pool */
};


static void *
g_memdup (const void *data, size_t s)
{
  void *ret;

  ret = GNUNET_malloc (s);
  memcpy (ret, data, s);
  return ret;
}

static char *
g_string_append_len (char *prefix, const void *data, size_t s)
{
  char *ret;

  ret = g_strdup_printf ("%s%.*s", prefix, s, data);
  GNUNET_free (prefix);
  return ret;
}

static xmlnode *
new_node (const char *name, XMLNodeType type, void *user_data)
{
  xmlnode *node = GNUNET_malloc (sizeof (xmlnode));

  node->name = name == NULL ? NULL : GNUNET_strdup (name);
  node->type = type;
  node->pool = user_data;
  if (node->pool->size == node->pool->pos)
    GNUNET_array_grow (node->pool->nodes, node->pool->size,
                       node->pool->size * 2 + 64);
  node->pool->nodes[node->pool->pos++] = node;
  node->free_pool = 0;
  return node;
}

static xmlnode *
xmlnode_new (const char *name, void *user_data)
{
  g_return_val_if_fail (name != NULL, NULL);
  return new_node (name, XMLNODE_TYPE_TAG, user_data);
}

static void
xmlnode_insert_child (xmlnode * parent, xmlnode * child)
{
  g_return_if_fail (parent != NULL);
  g_return_if_fail (child != NULL);

  child->parent = parent;
  if (parent->lastchild)
    parent->lastchild->next = child;
  else
    parent->child = child;
  parent->lastchild = child;
}

static xmlnode *
xmlnode_new_child (xmlnode * parent, const char *name, void *user_data)
{
  xmlnode *node;

  g_return_val_if_fail (parent != NULL, NULL);
  g_return_val_if_fail (name != NULL, NULL);
  node = new_node (name, XMLNODE_TYPE_TAG, user_data);
  xmlnode_insert_child (parent, node);
  return node;
}

static void
xmlnode_insert_data (xmlnode * node,
                     const char *data, int size, void *user_data)
{
  xmlnode *child;
  size_t real_size;

  g_return_if_fail (node != NULL);
  g_return_if_fail (data != NULL);
  g_return_if_fail (size != 0);
  real_size = size == -1 ? strlen (data) : size;
  child = new_node (NULL, XMLNODE_TYPE_DATA, user_data);
  child->data = g_memdup (data, real_size);
  child->data_sz = real_size;
  xmlnode_insert_child (node, child);
}

static void
xmlnode_remove_attrib (xmlnode * node, const char *attr)
{
  xmlnode *attr_node, *sibling = NULL;

  g_return_if_fail (node != NULL);
  g_return_if_fail (attr != NULL);

  for (attr_node = node->child; attr_node; attr_node = attr_node->next)
    {
      if (attr_node->type == XMLNODE_TYPE_ATTRIB &&
          !strcmp (attr_node->name, attr))
        {
          if (node->child == attr_node)
            {
              node->child = attr_node->next;
            }
          else
            {
              sibling->next = attr_node->next;
            }
          if (node->lastchild == attr_node)
            {
              node->lastchild = sibling;
            }
          xmlnode_free (attr_node);
          return;
        }
      sibling = attr_node;
    }
}

static void
xmlnode_set_attrib (xmlnode * node,
                    const char *attr, const char *value, void *user_data)
{
  xmlnode *attrib_node;

  g_return_if_fail (node != NULL);
  g_return_if_fail (attr != NULL);
  g_return_if_fail (value != NULL);
  xmlnode_remove_attrib (node, attr);
  attrib_node = new_node (attr, XMLNODE_TYPE_ATTRIB, user_data);
  attrib_node->data = GNUNET_strdup (value);
  xmlnode_insert_child (node, attrib_node);
}

static void
xmlnode_set_namespace (xmlnode * node, const char *xmlns)
{
  g_return_if_fail (node != NULL);
  GNUNET_free_non_null (node->xmlns);
  node->xmlns = GNUNET_strdup (xmlns);
}

static const char *
xmlnode_get_namespace (xmlnode * node)
{
  g_return_val_if_fail (node != NULL, NULL);
  return node->xmlns;
}

static void
freePool (XMLNodePool * pool)
{
  unsigned int i;
  xmlnode *x;

  for (i = 0; i < pool->pos; i++)
    {
      x = pool->nodes[i];
      GNUNET_free_non_null (x->name);
      GNUNET_free_non_null (x->data);
      GNUNET_free_non_null (x->xmlns);
      GNUNET_free (x);
    }
  GNUNET_array_grow (pool->nodes, pool->size, 0);
  GNUNET_free (pool);
}

void
xmlnode_free (xmlnode * node)
{
  g_return_if_fail (node != NULL);
  if (node->free_pool != GNUNET_YES)
    return;
  freePool (node->pool);
}

static xmlnode *
xmlnode_get_child_with_namespace (const xmlnode * parent,
                                  const char *name, const char *ns)
{
  xmlnode *x;
  xmlnode *ret = NULL;
  char *parent_name;
  char *child_name;

  if (parent == NULL)
    return NULL;
  if (name == NULL)
    return NULL;

  parent_name = GNUNET_strdup (name);
  child_name = strstr (parent_name, "/");
  if (child_name != NULL)
    {
      child_name[0] = '\0';
      child_name++;
    }

  for (x = parent->child; x; x = x->next)
    {
      const char *xmlns = NULL;
      if (ns)
        xmlns = xmlnode_get_namespace (x);

      if (x->type == XMLNODE_TYPE_TAG && name
          && !strcmp (parent_name, x->name) && (!ns
                                                || (xmlns
                                                    && !strcmp (ns, xmlns))))
        {
          ret = x;
          break;
        }
    }

  if (child_name && ret)
    ret = xmlnode_get_child (ret, child_name);

  GNUNET_free (parent_name);
  return ret;
}

xmlnode *
xmlnode_get_child (const xmlnode * parent, const char *name)
{
  return xmlnode_get_child_with_namespace (parent, name, NULL);
}

char *
xmlnode_get_data (xmlnode * node)
{
  char *str = NULL;
  xmlnode *c;

  if (node == NULL)
    return NULL;
  for (c = node->child; c; c = c->next)
    {
      if (c->type == XMLNODE_TYPE_DATA)
        {
          if (!str)
            str = GNUNET_strdup ("");
          str = g_string_append_len (str, c->data, c->data_sz);
        }
    }
  if (str == NULL)
    return NULL;

  return str;
}

static void
xmlnode_parser_element_start_libxml (void *user_data,
                                     const xmlChar * element_name,
                                     const xmlChar * prefix,
                                     const xmlChar * xmlns,
                                     int nb_namespaces,
                                     const xmlChar ** namespaces,
                                     int nb_attributes,
                                     int nb_defaulted,
                                     const xmlChar ** attributes)
{
  XMLNodePool *xpd = user_data;
  xmlnode *node;
  int i;

  if (!element_name)
    return;
  if (xpd->current)
    node =
      xmlnode_new_child (xpd->current, (const char *) element_name,
                         user_data);
  else
    node = xmlnode_new ((const char *) element_name, user_data);

  xmlnode_set_namespace (node, (const char *) xmlns);

  for (i = 0; i < nb_attributes * 5; i += 5)
    {
      char *txt;
      int attrib_len = attributes[i + 4] - attributes[i + 3];
      char *attrib = GNUNET_malloc (attrib_len + 1);
      memcpy (attrib, attributes[i + 3], attrib_len);
      attrib[attrib_len] = '\0';
      txt = attrib;
      attrib = gaim_unescape_html (txt);
      GNUNET_free (txt);
      xmlnode_set_attrib (node, (const char *) attributes[i], attrib,
                          user_data);
      GNUNET_free (attrib);
    }
  xpd->current = node;
}

static void
xmlnode_parser_element_end_libxml (void *user_data,
                                   const xmlChar * element_name,
                                   const xmlChar * prefix,
                                   const xmlChar * xmlns)
{
  XMLNodePool *xpd = user_data;

  if (!element_name || !xpd->current)
    return;
  if (xpd->current->parent)
    {
      if (!xmlStrcmp ((xmlChar *) xpd->current->name, element_name))
        xpd->current = xpd->current->parent;
    }
}

static void
xmlnode_parser_element_text_libxml (void *user_data,
                                    const xmlChar * text, int text_len)
{
  XMLNodePool *xpd = user_data;

  if (!xpd->current || !text || !text_len)
    return;
  xmlnode_insert_data (xpd->current,
                       (const char *) text, text_len, user_data);
}

static xmlSAXHandler xmlnode_parser_libxml = {
  .internalSubset = NULL,
  .isStandalone = NULL,
  .hasInternalSubset = NULL,
  .hasExternalSubset = NULL,
  .resolveEntity = NULL,
  .getEntity = NULL,
  .entityDecl = NULL,
  .notationDecl = NULL,
  .attributeDecl = NULL,
  .elementDecl = NULL,
  .unparsedEntityDecl = NULL,
  .setDocumentLocator = NULL,
  .startDocument = NULL,
  .endDocument = NULL,
  .startElement = NULL,
  .endElement = NULL,
  .reference = NULL,
  .characters = xmlnode_parser_element_text_libxml,
  .ignorableWhitespace = NULL,
  .processingInstruction = NULL,
  .comment = NULL,
  .warning = NULL,
  .error = NULL,
  .fatalError = NULL,
  .getParameterEntity = NULL,
  .cdataBlock = NULL,
  .externalSubset = NULL,
  .initialized = XML_SAX2_MAGIC,
  ._private = NULL,
  .startElementNs = xmlnode_parser_element_start_libxml,
  .endElementNs = xmlnode_parser_element_end_libxml,
  .serror = NULL
};

xmlnode *
xmlnode_from_str (const char *str, int size)
{
  XMLNodePool *xpd;
  xmlnode *ret;
  size_t real_size;

  g_return_val_if_fail (str != NULL, NULL);

  real_size = size < 0 ? strlen (str) : size;
  xpd = GNUNET_malloc (sizeof (XMLNodePool));
  memset (xpd, 0, sizeof (XMLNodePool));
  if (xmlSAXUserParseMemory (&xmlnode_parser_libxml, xpd, str, real_size) < 0)
    {
      freePool (xpd);
      return NULL;
    }
  ret = xpd->current;
  ret->free_pool = GNUNET_YES;
  return ret;
}

xmlnode *
xmlnode_get_next_twin (xmlnode * node)
{
  xmlnode *sibling;
  const char *ns = xmlnode_get_namespace (node);

  g_return_val_if_fail (node != NULL, NULL);
  g_return_val_if_fail (node->type == XMLNODE_TYPE_TAG, NULL);

  for (sibling = node->next; sibling; sibling = sibling->next)
    {
      const char *xmlns = NULL;
      if (ns)
        xmlns = xmlnode_get_namespace (sibling);

      if (sibling->type == XMLNODE_TYPE_TAG
          && !strcmp (node->name, sibling->name) && (!ns
                                                     || (xmlns
                                                         && !strcmp (ns,
                                                                     xmlns))))
        return sibling;
    }
  return NULL;
}