1 | /* $Id: minixml.c,v 1.6 2007/05/15 18:14:08 nanard Exp $ */ |
---|
2 | /* minixml.c : the minimum size a xml parser can be ! */ |
---|
3 | /* Project : miniupnp |
---|
4 | * webpage: http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ |
---|
5 | * Author : Thomas Bernard |
---|
6 | |
---|
7 | Copyright (c) 2005-2007, Thomas BERNARD |
---|
8 | All rights reserved. |
---|
9 | |
---|
10 | Redistribution and use in source and binary forms, with or without |
---|
11 | modification, are permitted provided that the following conditions are met: |
---|
12 | |
---|
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. |
---|
20 | |
---|
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. |
---|
32 | */ |
---|
33 | #include "minixml.h" |
---|
34 | |
---|
35 | /* parseatt : used to parse the argument list |
---|
36 | * return 0 (false) in case of success and -1 (true) if the end |
---|
37 | * of the xmlbuffer is reached. */ |
---|
38 | static int parseatt(struct xmlparser * p) |
---|
39 | { |
---|
40 | const char * attname; |
---|
41 | int attnamelen; |
---|
42 | const char * attvalue; |
---|
43 | int attvaluelen; |
---|
44 | while(p->xml < p->xmlend) |
---|
45 | { |
---|
46 | if(*p->xml=='/' || *p->xml=='>') |
---|
47 | return 0; |
---|
48 | if( !IS_WHITE_SPACE(*p->xml) ) |
---|
49 | { |
---|
50 | char sep; |
---|
51 | attname = p->xml; |
---|
52 | attnamelen = 0; |
---|
53 | while(*p->xml!='=' && !IS_WHITE_SPACE(*p->xml) ) |
---|
54 | { |
---|
55 | attnamelen++; p->xml++; |
---|
56 | if(p->xml >= p->xmlend) |
---|
57 | return -1; |
---|
58 | } |
---|
59 | while(*(p->xml++) != '=') |
---|
60 | { |
---|
61 | if(p->xml >= p->xmlend) |
---|
62 | return -1; |
---|
63 | } |
---|
64 | while(IS_WHITE_SPACE(*p->xml)) |
---|
65 | { |
---|
66 | p->xml++; |
---|
67 | if(p->xml >= p->xmlend) |
---|
68 | return -1; |
---|
69 | } |
---|
70 | sep = *p->xml; |
---|
71 | if(sep=='\'' || sep=='\"') |
---|
72 | { |
---|
73 | p->xml++; |
---|
74 | if(p->xml >= p->xmlend) |
---|
75 | return -1; |
---|
76 | attvalue = p->xml; |
---|
77 | attvaluelen = 0; |
---|
78 | while(*p->xml != sep) |
---|
79 | { |
---|
80 | attvaluelen++; p->xml++; |
---|
81 | if(p->xml >= p->xmlend) |
---|
82 | return -1; |
---|
83 | } |
---|
84 | } |
---|
85 | else |
---|
86 | { |
---|
87 | attvalue = p->xml; |
---|
88 | attvaluelen = 0; |
---|
89 | while( !IS_WHITE_SPACE(*p->xml) |
---|
90 | && *p->xml != '>' && *p->xml != '/') |
---|
91 | { |
---|
92 | attvaluelen++; p->xml++; |
---|
93 | if(p->xml >= p->xmlend) |
---|
94 | return -1; |
---|
95 | } |
---|
96 | } |
---|
97 | /*printf("%.*s='%.*s'\n", |
---|
98 | attnamelen, attname, attvaluelen, attvalue);*/ |
---|
99 | if(p->attfunc) |
---|
100 | p->attfunc(p->data, attname, attnamelen, attvalue, attvaluelen); |
---|
101 | } |
---|
102 | p->xml++; |
---|
103 | } |
---|
104 | return -1; |
---|
105 | } |
---|
106 | |
---|
107 | /* parseelt parse the xml stream and |
---|
108 | * call the callback functions when needed... */ |
---|
109 | static void parseelt(struct xmlparser * p) |
---|
110 | { |
---|
111 | int i; |
---|
112 | const char * elementname; |
---|
113 | while(p->xml < (p->xmlend - 1)) |
---|
114 | { |
---|
115 | if((p->xml)[0]=='<' && (p->xml)[1]!='?') |
---|
116 | { |
---|
117 | i = 0; elementname = ++p->xml; |
---|
118 | while( !IS_WHITE_SPACE(*p->xml) |
---|
119 | && (*p->xml!='>') && (*p->xml!='/') |
---|
120 | ) |
---|
121 | { |
---|
122 | i++; p->xml++; |
---|
123 | if (p->xml >= p->xmlend) |
---|
124 | return; |
---|
125 | /* to ignore namespace : */ |
---|
126 | if(*p->xml==':') |
---|
127 | { |
---|
128 | i = 0; |
---|
129 | elementname = ++p->xml; |
---|
130 | } |
---|
131 | } |
---|
132 | if(i>0) |
---|
133 | { |
---|
134 | if(p->starteltfunc) |
---|
135 | p->starteltfunc(p->data, elementname, i); |
---|
136 | if(parseatt(p)) |
---|
137 | return; |
---|
138 | if(*p->xml!='/') |
---|
139 | { |
---|
140 | const char * data; |
---|
141 | i = 0; data = ++p->xml; |
---|
142 | if (p->xml >= p->xmlend) |
---|
143 | return; |
---|
144 | while( IS_WHITE_SPACE(*p->xml) ) |
---|
145 | { |
---|
146 | p->xml++; |
---|
147 | if (p->xml >= p->xmlend) |
---|
148 | return; |
---|
149 | } |
---|
150 | while(*p->xml!='<') |
---|
151 | { |
---|
152 | i++; p->xml++; |
---|
153 | if (p->xml >= p->xmlend) |
---|
154 | return; |
---|
155 | } |
---|
156 | if(i>0 && p->datafunc) |
---|
157 | p->datafunc(p->data, data, i); |
---|
158 | } |
---|
159 | } |
---|
160 | else if(*p->xml == '/') |
---|
161 | { |
---|
162 | i = 0; elementname = ++p->xml; |
---|
163 | if (p->xml >= p->xmlend) |
---|
164 | return; |
---|
165 | while((*p->xml != '>')) |
---|
166 | { |
---|
167 | i++; p->xml++; |
---|
168 | if (p->xml >= p->xmlend) |
---|
169 | return; |
---|
170 | } |
---|
171 | if(p->endeltfunc) |
---|
172 | p->endeltfunc(p->data, elementname, i); |
---|
173 | p->xml++; |
---|
174 | } |
---|
175 | } |
---|
176 | else |
---|
177 | { |
---|
178 | p->xml++; |
---|
179 | } |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | /* the parser must be initialized before calling this function */ |
---|
184 | void parsexml(struct xmlparser * parser) |
---|
185 | { |
---|
186 | parser->xml = parser->xmlstart; |
---|
187 | parser->xmlend = parser->xmlstart + parser->xmlsize; |
---|
188 | parseelt(parser); |
---|
189 | } |
---|
190 | |
---|
191 | |
---|