/* $Id: minixml.c,v 1.12 2017/12/12 11:17:40 nanard Exp $ */1/* vim: tabstop=4 shiftwidth=4 noexpandtab2* minixml.c : the minimum size a xml parser can be ! */3/* Project : miniupnp4* webpage: http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/5* Author : Thomas Bernard67Copyright (c) 2005-2017, Thomas BERNARD8All rights reserved.910Redistribution and use in source and binary forms, with or without11modification, are permitted provided that the following conditions are met:1213* Redistributions of source code must retain the above copyright notice,14this list of conditions and the following disclaimer.15* Redistributions in binary form must reproduce the above copyright notice,16this list of conditions and the following disclaimer in the documentation17and/or other materials provided with the distribution.18* The name of the author may not be used to endorse or promote products19derived from this software without specific prior written permission.2021THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"22AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE25LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR26CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF27SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS28INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN29CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)30ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE31POSSIBILITY OF SUCH DAMAGE.32*/33#include <string.h>34#include "minixml.h"3536/* parseatt : used to parse the argument list37* return 0 (false) in case of success and -1 (true) if the end38* of the xmlbuffer is reached. */39static int parseatt(struct xmlparser * p)40{41const char * attname;42int attnamelen;43const char * attvalue;44int attvaluelen;45while(p->xml < p->xmlend)46{47if(*p->xml=='/' || *p->xml=='>')48return 0;49if( !IS_WHITE_SPACE(*p->xml) )50{51char sep;52attname = p->xml;53attnamelen = 0;54while(*p->xml!='=' && !IS_WHITE_SPACE(*p->xml) )55{56attnamelen++; p->xml++;57if(p->xml >= p->xmlend)58return -1;59}60while(*(p->xml++) != '=')61{62if(p->xml >= p->xmlend)63return -1;64}65while(IS_WHITE_SPACE(*p->xml))66{67p->xml++;68if(p->xml >= p->xmlend)69return -1;70}71sep = *p->xml;72if(sep=='\'' || sep=='\"')73{74p->xml++;75if(p->xml >= p->xmlend)76return -1;77attvalue = p->xml;78attvaluelen = 0;79while(*p->xml != sep)80{81attvaluelen++; p->xml++;82if(p->xml >= p->xmlend)83return -1;84}85}86else87{88attvalue = p->xml;89attvaluelen = 0;90while( !IS_WHITE_SPACE(*p->xml)91&& *p->xml != '>' && *p->xml != '/')92{93attvaluelen++; p->xml++;94if(p->xml >= p->xmlend)95return -1;96}97}98/*printf("%.*s='%.*s'\n",99attnamelen, attname, attvaluelen, attvalue);*/100if(p->attfunc)101p->attfunc(p->data, attname, attnamelen, attvalue, attvaluelen);102}103p->xml++;104}105return -1;106}107108/* parseelt parse the xml stream and109* call the callback functions when needed... */110static void parseelt(struct xmlparser * p)111{112int i;113const char * elementname;114while(p->xml < (p->xmlend - 1))115{116if((p->xml + 4) <= p->xmlend && (0 == memcmp(p->xml, "<!--", 4)))117{118p->xml += 3;119/* ignore comments */120do121{122p->xml++;123if ((p->xml + 3) >= p->xmlend)124return;125}126while(memcmp(p->xml, "-->", 3) != 0);127p->xml += 3;128}129else if((p->xml)[0]=='<' && (p->xml)[1]!='?')130{131i = 0; elementname = ++p->xml;132while( !IS_WHITE_SPACE(*p->xml)133&& (*p->xml!='>') && (*p->xml!='/')134)135{136i++; p->xml++;137if (p->xml >= p->xmlend)138return;139/* to ignore namespace : */140if(*p->xml==':')141{142i = 0;143elementname = ++p->xml;144}145}146if(i>0)147{148if(p->starteltfunc)149p->starteltfunc(p->data, elementname, i);150if(parseatt(p))151return;152if(*p->xml!='/')153{154const char * data;155i = 0; data = ++p->xml;156if (p->xml >= p->xmlend)157return;158while( IS_WHITE_SPACE(*p->xml) )159{160i++; p->xml++;161if (p->xml >= p->xmlend)162return;163}164/* CDATA are at least 9 + 3 characters long : <![CDATA[ ]]> */165if((p->xmlend >= (p->xml + (9 + 3))) && (memcmp(p->xml, "<![CDATA[", 9) == 0))166{167/* CDATA handling */168p->xml += 9;169data = p->xml;170i = 0;171while(memcmp(p->xml, "]]>", 3) != 0)172{173i++; p->xml++;174if ((p->xml + 3) >= p->xmlend)175return;176}177if(i>0 && p->datafunc)178p->datafunc(p->data, data, i);179while(*p->xml!='<')180{181p->xml++;182if (p->xml >= p->xmlend)183return;184}185}186else187{188while(*p->xml!='<')189{190i++; p->xml++;191if ((p->xml + 1) >= p->xmlend)192return;193}194if(i>0 && p->datafunc && *(p->xml + 1) == '/')195p->datafunc(p->data, data, i);196}197}198}199else if(*p->xml == '/')200{201i = 0; elementname = ++p->xml;202if (p->xml >= p->xmlend)203return;204while((*p->xml != '>'))205{206i++; p->xml++;207if (p->xml >= p->xmlend)208return;209}210if(p->endeltfunc)211p->endeltfunc(p->data, elementname, i);212p->xml++;213}214}215else216{217p->xml++;218}219}220}221222/* the parser must be initialized before calling this function */223void parsexml(struct xmlparser * parser)224{225parser->xml = parser->xmlstart;226parser->xmlend = parser->xmlstart + parser->xmlsize;227parseelt(parser);228}229230231232233