minixml.c 5.7 KB

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