1 /* $Id: minixml.c,v 1.11 2014/02/03 15:54:12 nanard Exp $ */
2 /* minixml.c : the minimum size a xml parser can be ! */
4 * webpage: http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
5 * Author : Thomas Bernard
7 Copyright (c) 2005-2014, Thomas BERNARD
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
13 * Redistributions of source code must retain the above copyright notice,
14 this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
18 * The name of the author may not be used to endorse or promote products
19 derived from this software without specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
36 /* parseatt : used to parse the argument list
37 * return 0 (false) in case of success and -1 (true) if the end
38 * of the xmlbuffer is reached. */
39 static int parseatt(struct xmlparser
* p
)
43 const char * attvalue
;
45 while(p
->xml
< p
->xmlend
)
47 if(*p
->xml
=='/' || *p
->xml
=='>')
49 if( !IS_WHITE_SPACE(*p
->xml
) )
54 while(*p
->xml
!='=' && !IS_WHITE_SPACE(*p
->xml
) )
56 attnamelen
++; p
->xml
++;
57 if(p
->xml
>= p
->xmlend
)
60 while(*(p
->xml
++) != '=')
62 if(p
->xml
>= p
->xmlend
)
65 while(IS_WHITE_SPACE(*p
->xml
))
68 if(p
->xml
>= p
->xmlend
)
72 if(sep
=='\'' || sep
=='\"')
75 if(p
->xml
>= p
->xmlend
)
81 attvaluelen
++; p
->xml
++;
82 if(p
->xml
>= p
->xmlend
)
90 while( !IS_WHITE_SPACE(*p
->xml
)
91 && *p
->xml
!= '>' && *p
->xml
!= '/')
93 attvaluelen
++; p
->xml
++;
94 if(p
->xml
>= p
->xmlend
)
98 /*printf("%.*s='%.*s'\n",
99 attnamelen, attname, attvaluelen, attvalue);*/
101 p
->attfunc(p
->data
, attname
, attnamelen
, attvalue
, attvaluelen
);
108 /* parseelt parse the xml stream and
109 * call the callback functions when needed... */
110 static void parseelt(struct xmlparser
* p
)
113 const char * elementname
;
114 while(p
->xml
< (p
->xmlend
- 1))
116 if((p
->xml
+ 4) <= p
->xmlend
&& (0 == memcmp(p
->xml
, "<!--", 4)))
119 /* ignore comments */
123 if ((p
->xml
+ 3) >= p
->xmlend
)
126 while(memcmp(p
->xml
, "-->", 3) != 0);
129 else if((p
->xml
)[0]=='<' && (p
->xml
)[1]!='?')
131 i
= 0; elementname
= ++p
->xml
;
132 while( !IS_WHITE_SPACE(*p
->xml
)
133 && (*p
->xml
!='>') && (*p
->xml
!='/')
137 if (p
->xml
>= p
->xmlend
)
139 /* to ignore namespace : */
143 elementname
= ++p
->xml
;
149 p
->starteltfunc(p
->data
, elementname
, i
);
155 i
= 0; data
= ++p
->xml
;
156 if (p
->xml
>= p
->xmlend
)
158 while( IS_WHITE_SPACE(*p
->xml
) )
161 if (p
->xml
>= p
->xmlend
)
164 if(memcmp(p
->xml
, "<![CDATA[", 9) == 0)
170 while(memcmp(p
->xml
, "]]>", 3) != 0)
173 if ((p
->xml
+ 3) >= p
->xmlend
)
176 if(i
>0 && p
->datafunc
)
177 p
->datafunc(p
->data
, data
, i
);
181 if (p
->xml
>= p
->xmlend
)
190 if ((p
->xml
+ 1) >= p
->xmlend
)
193 if(i
>0 && p
->datafunc
&& *(p
->xml
+ 1) == '/')
194 p
->datafunc(p
->data
, data
, i
);
198 else if(*p
->xml
== '/')
200 i
= 0; elementname
= ++p
->xml
;
201 if (p
->xml
>= p
->xmlend
)
203 while((*p
->xml
!= '>'))
206 if (p
->xml
>= p
->xmlend
)
210 p
->endeltfunc(p
->data
, elementname
, i
);
221 /* the parser must be initialized before calling this function */
222 void parsexml(struct xmlparser
* parser
)
224 parser
->xml
= parser
->xmlstart
;
225 parser
->xmlend
= parser
->xmlstart
+ parser
->xmlsize
;