miniupnpd 1.9 (20160113)
[tomato.git] / release / src / router / miniupnpd / upnpreplyparse.c
blob5de5796a39547e267347a77d57ff044a1d02e381
1 /* $Id: upnpreplyparse.c,v 1.19 2015/07/15 10:29:11 nanard Exp $ */
2 /* MiniUPnP project
3 * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
4 * (c) 2006-2015 Thomas Bernard
5 * This software is subject to the conditions detailed
6 * in the LICENCE file provided within the distribution */
8 #include <stdlib.h>
9 #include <string.h>
10 #include <stdio.h>
12 #include "upnpreplyparse.h"
13 #include "minixml.h"
15 static void
16 NameValueParserStartElt(void * d, const char * name, int l)
18 struct NameValueParserData * data = (struct NameValueParserData *)d;
19 data->topelt = 1;
20 if(l>63)
21 l = 63;
22 memcpy(data->curelt, name, l);
23 data->curelt[l] = '\0';
24 data->cdata = NULL;
25 data->cdatalen = 0;
28 static void
29 NameValueParserEndElt(void * d, const char * name, int l)
31 struct NameValueParserData * data = (struct NameValueParserData *)d;
32 struct NameValue * nv;
33 (void)name;
34 (void)l;
35 if(!data->topelt)
36 return;
37 if(strcmp(data->curelt, "NewPortListing") != 0)
39 int l;
40 /* standard case. Limited to n chars strings */
41 l = data->cdatalen;
42 nv = malloc(sizeof(struct NameValue));
43 if(nv == NULL)
45 /* malloc error */
46 #ifdef DEBUG
47 fprintf(stderr, "%s: error allocating memory",
48 "NameValueParserEndElt");
49 #endif /* DEBUG */
50 return;
52 if(l>=(int)sizeof(nv->value))
53 l = sizeof(nv->value) - 1;
54 strncpy(nv->name, data->curelt, 64);
55 nv->name[63] = '\0';
56 if(data->cdata != NULL)
58 memcpy(nv->value, data->cdata, l);
59 nv->value[l] = '\0';
61 else
63 nv->value[0] = '\0';
65 nv->l_next = data->l_head; /* insert in list */
66 data->l_head = nv;
68 data->cdata = NULL;
69 data->cdatalen = 0;
70 data->topelt = 0;
73 static void
74 NameValueParserGetData(void * d, const char * datas, int l)
76 struct NameValueParserData * data = (struct NameValueParserData *)d;
77 if(strcmp(data->curelt, "NewPortListing") == 0)
79 /* specific case for NewPortListing which is a XML Document */
80 data->portListing = malloc(l + 1);
81 if(!data->portListing)
83 /* malloc error */
84 #ifdef DEBUG
85 fprintf(stderr, "%s: error allocating memory",
86 "NameValueParserGetData");
87 #endif /* DEBUG */
88 return;
90 memcpy(data->portListing, datas, l);
91 data->portListing[l] = '\0';
92 data->portListingLength = l;
94 else
96 /* standard case. */
97 data->cdata = datas;
98 data->cdatalen = l;
102 void
103 ParseNameValue(const char * buffer, int bufsize,
104 struct NameValueParserData * data)
106 struct xmlparser parser;
107 data->l_head = NULL;
108 data->portListing = NULL;
109 data->portListingLength = 0;
110 /* init xmlparser object */
111 parser.xmlstart = buffer;
112 parser.xmlsize = bufsize;
113 parser.data = data;
114 parser.starteltfunc = NameValueParserStartElt;
115 parser.endeltfunc = NameValueParserEndElt;
116 parser.datafunc = NameValueParserGetData;
117 parser.attfunc = 0;
118 parsexml(&parser);
121 void
122 ClearNameValueList(struct NameValueParserData * pdata)
124 struct NameValue * nv;
125 if(pdata->portListing)
127 free(pdata->portListing);
128 pdata->portListing = NULL;
129 pdata->portListingLength = 0;
131 while((nv = pdata->l_head) != NULL)
133 pdata->l_head = nv->l_next;
134 free(nv);
138 char *
139 GetValueFromNameValueList(struct NameValueParserData * pdata,
140 const char * Name)
142 struct NameValue * nv;
143 char * p = NULL;
144 for(nv = pdata->l_head;
145 (nv != NULL) && (p == NULL);
146 nv = nv->l_next)
148 if(strcmp(nv->name, Name) == 0)
149 p = nv->value;
151 return p;
154 #if 0
155 /* useless now that minixml ignores namespaces by itself */
156 char *
157 GetValueFromNameValueListIgnoreNS(struct NameValueParserData * pdata,
158 const char * Name)
160 struct NameValue * nv;
161 char * p = NULL;
162 char * pname;
163 for(nv = pdata->head.lh_first;
164 (nv != NULL) && (p == NULL);
165 nv = nv->entries.le_next)
167 pname = strrchr(nv->name, ':');
168 if(pname)
169 pname++;
170 else
171 pname = nv->name;
172 if(strcmp(pname, Name)==0)
173 p = nv->value;
175 return p;
177 #endif
179 /* debug all-in-one function
180 * do parsing then display to stdout */
181 #ifdef DEBUG
182 void
183 DisplayNameValueList(char * buffer, int bufsize)
185 struct NameValueParserData pdata;
186 struct NameValue * nv;
187 ParseNameValue(buffer, bufsize, &pdata);
188 for(nv = pdata.l_head;
189 nv != NULL;
190 nv = nv->l_next)
192 printf("%s = %s\n", nv->name, nv->value);
194 ClearNameValueList(&pdata);
196 #endif /* DEBUG */