aboutsummaryrefslogtreecommitdiff
path: root/src/nat/miniupnp/upnpreplyparse.c
blob: 9aa895d1d7eec9b6a5109ab19870f3117840a2e5 (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
/* $Id: upnpreplyparse.c,v 1.10 2008/02/21 13:05:27 nanard Exp $ */
/* MiniUPnP project
 * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
 * (c) 2006 Thomas Bernard 
 * This software is subject to the conditions detailed
 * in the LICENCE file provided within the distribution */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "upnpreplyparse.h"
#include "minixml.h"

static void
NameValueParserStartElt (void *d, const char *name, int l)
{
  struct NameValueParserData *data = (struct NameValueParserData *) d;
  if (l > 63)
    l = 63;
  memcpy (data->curelt, name, l);
  data->curelt[l] = '\0';
}

static void
NameValueParserGetData (void *d, const char *datas, int l)
{
  struct NameValueParserData *data = (struct NameValueParserData *) d;
  struct NameValue *nv;
  nv = malloc (sizeof (struct NameValue));
  if (l > 63)
    l = 63;
  strncpy (nv->name, data->curelt, 64);
  nv->name[63] = '\0';
  memcpy (nv->value, datas, l);
  nv->value[l] = '\0';
  LIST_INSERT_HEAD (&(data->head), nv, entries);
}

void
ParseNameValue (const char *buffer, int bufsize,
                struct NameValueParserData *data)
{
  struct xmlparser parser;
  LIST_INIT (&(data->head));
  /* init xmlparser object */
  parser.xmlstart = buffer;
  parser.xmlsize = bufsize;
  parser.data = data;
  parser.starteltfunc = NameValueParserStartElt;
  parser.endeltfunc = 0;
  parser.datafunc = NameValueParserGetData;
  parser.attfunc = 0;
  parsexml (&parser);
}

void
ClearNameValueList (struct NameValueParserData *pdata)
{
  struct NameValue *nv;
  while ((nv = pdata->head.lh_first) != NULL)
    {
      LIST_REMOVE (nv, entries);
      free (nv);
    }
}

char *
GetValueFromNameValueList (struct NameValueParserData *pdata,
                           const char *Name)
{
  struct NameValue *nv;
  char *p = NULL;
  for (nv = pdata->head.lh_first;
       (nv != NULL) && (p == NULL); nv = nv->entries.le_next)
    {
      if (strcmp (nv->name, Name) == 0)
        p = nv->value;
    }
  return p;
}

#if 0
/* useless now that minixml ignores namespaces by itself */
char *
GetValueFromNameValueListIgnoreNS (struct NameValueParserData *pdata,
                                   const char *Name)
{
  struct NameValue *nv;
  char *p = NULL;
  char *pname;
  for (nv = pdata->head.lh_first;
       (nv != NULL) && (p == NULL); nv = nv->entries.le_next)
    {
      pname = strrchr (nv->name, ':');
      if (pname)
        pname++;
      else
        pname = nv->name;
      if (strcmp (pname, Name) == 0)
        p = nv->value;
    }
  return p;
}
#endif

/* debug all-in-one function 
 * do parsing then display to stdout */
#ifdef DEBUG
void
DisplayNameValueList (char *buffer, int bufsize)
{
  struct NameValueParserData pdata;
  struct NameValue *nv;
  ParseNameValue (buffer, bufsize, &pdata);
  for (nv = pdata.head.lh_first; nv != NULL; nv = nv->entries.le_next)
    {
      printf ("%s = %s\n", nv->name, nv->value);
    }
  ClearNameValueList (&pdata);
}
#endif