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 ! */ 3/* Project : miniupnp 4 * webpage: http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ 5 * Author : Thomas Bernard 6 7Copyright (c) 2005-2014, Thomas BERNARD 8All rights reserved. 9 10Redistribution and use in source and binary forms, with or without 11modification, 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 21THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 22AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 25LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31POSSIBILITY OF SUCH DAMAGE. 32*/ 33#include <string.h> 34#include "minixml.h" 35 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. */ 39static int parseatt(struct xmlparser * p) 40{ 41 const char * attname; 42 int attnamelen; 43 const char * attvalue; 44 int attvaluelen; 45 while(p->xml < p->xmlend) 46 { 47 if(*p->xml=='/' || *p->xml=='>') 48 return 0; 49 if( !IS_WHITE_SPACE(*p->xml) ) 50 { 51 char sep; 52 attname = p->xml; 53 attnamelen = 0; 54 while(*p->xml!='=' && !IS_WHITE_SPACE(*p->xml) ) 55 { 56 attnamelen++; p->xml++; 57 if(p->xml >= p->xmlend) 58 return -1; 59 } 60 while(*(p->xml++) != '=') 61 { 62 if(p->xml >= p->xmlend) 63 return -1; 64 } 65 while(IS_WHITE_SPACE(*p->xml)) 66 { 67 p->xml++; 68 if(p->xml >= p->xmlend) 69 return -1; 70 } 71 sep = *p->xml; 72 if(sep=='\'' || sep=='\"') 73 { 74 p->xml++; 75 if(p->xml >= p->xmlend) 76 return -1; 77 attvalue = p->xml; 78 attvaluelen = 0; 79 while(*p->xml != sep) 80 { 81 attvaluelen++; p->xml++; 82 if(p->xml >= p->xmlend) 83 return -1; 84 } 85 } 86 else 87 { 88 attvalue = p->xml; 89 attvaluelen = 0; 90 while( !IS_WHITE_SPACE(*p->xml) 91 && *p->xml != '>' && *p->xml != '/') 92 { 93 attvaluelen++; p->xml++; 94 if(p->xml >= p->xmlend) 95 return -1; 96 } 97 } 98 /*printf("%.*s='%.*s'\n", 99 attnamelen, attname, attvaluelen, attvalue);*/ 100 if(p->attfunc) 101 p->attfunc(p->data, attname, attnamelen, attvalue, attvaluelen); 102 } 103 p->xml++; 104 } 105 return -1; 106} 107 108/* parseelt parse the xml stream and 109 * call the callback functions when needed... */ 110static void parseelt(struct xmlparser * p) 111{ 112 int i; 113 const char * elementname; 114 while(p->xml < (p->xmlend - 1)) 115 { 116 if((p->xml + 4) <= p->xmlend && (0 == memcmp(p->xml, "<!--", 4))) 117 { 118 p->xml += 3; 119 /* ignore comments */ 120 do 121 { 122 p->xml++; 123 if ((p->xml + 3) >= p->xmlend) 124 return; 125 } 126 while(memcmp(p->xml, "-->", 3) != 0); 127 p->xml += 3; 128 } 129 else if((p->xml)[0]=='<' && (p->xml)[1]!='?') 130 { 131 i = 0; elementname = ++p->xml; 132 while( !IS_WHITE_SPACE(*p->xml) 133 && (*p->xml!='>') && (*p->xml!='/') 134 ) 135 { 136 i++; p->xml++; 137 if (p->xml >= p->xmlend) 138 return; 139 /* to ignore namespace : */ 140 if(*p->xml==':') 141 { 142 i = 0; 143 elementname = ++p->xml; 144 } 145 } 146 if(i>0) 147 { 148 if(p->starteltfunc) 149 p->starteltfunc(p->data, elementname, i); 150 if(parseatt(p)) 151 return; 152 if(*p->xml!='/') 153 { 154 const char * data; 155 i = 0; data = ++p->xml; 156 if (p->xml >= p->xmlend) 157 return; 158 while( IS_WHITE_SPACE(*p->xml) ) 159 { 160 i++; p->xml++; 161 if (p->xml >= p->xmlend) 162 return; 163 } 164 if(memcmp(p->xml, "<![CDATA[", 9) == 0) 165 { 166 /* CDATA handling */ 167 p->xml += 9; 168 data = p->xml; 169 i = 0; 170 while(memcmp(p->xml, "]]>", 3) != 0) 171 { 172 i++; p->xml++; 173 if ((p->xml + 3) >= p->xmlend) 174 return; 175 } 176 if(i>0 && p->datafunc) 177 p->datafunc(p->data, data, i); 178 while(*p->xml!='<') 179 { 180 p->xml++; 181 if (p->xml >= p->xmlend) 182 return; 183 } 184 } 185 else 186 { 187 while(*p->xml!='<') 188 { 189 i++; p->xml++; 190 if ((p->xml + 1) >= p->xmlend) 191 return; 192 } 193 if(i>0 && p->datafunc && *(p->xml + 1) == '/') 194 p->datafunc(p->data, data, i); 195 } 196 } 197 } 198 else if(*p->xml == '/') 199 { 200 i = 0; elementname = ++p->xml; 201 if (p->xml >= p->xmlend) 202 return; 203 while((*p->xml != '>')) 204 { 205 i++; p->xml++; 206 if (p->xml >= p->xmlend) 207 return; 208 } 209 if(p->endeltfunc) 210 p->endeltfunc(p->data, elementname, i); 211 p->xml++; 212 } 213 } 214 else 215 { 216 p->xml++; 217 } 218 } 219} 220 221/* the parser must be initialized before calling this function */ 222void parsexml(struct xmlparser * parser) 223{ 224 parser->xml = parser->xmlstart; 225 parser->xmlend = parser->xmlstart + parser->xmlsize; 226 parseelt(parser); 227} 228 229 230