/thirdparty/libportfwd/third-party/miniupnpc-1.6/minixml.c

http://github.com/tomahawk-player/tomahawk · C · 216 lines · 169 code · 5 blank · 42 comment · 63 complexity · 6940ff768a85e337f025901c93749439 MD5 · raw file

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