minixml.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. Copyright (c) 2005-2007, 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 "minixml.h"
  30. /* parseatt : used to parse the argument list
  31. * return 0 (false) in case of success and -1 (true) if the end
  32. * of the xmlbuffer is reached. */
  33. int parseatt(struct xmlparser * p)
  34. {
  35. const char * attname;
  36. int attnamelen;
  37. const char * attvalue;
  38. int attvaluelen;
  39. while(p->xml < p->xmlend)
  40. {
  41. if(*p->xml=='/' || *p->xml=='>')
  42. return 0;
  43. if( !IS_WHITE_SPACE(*p->xml) )
  44. {
  45. char sep;
  46. attname = p->xml;
  47. attnamelen = 0;
  48. while(*p->xml!='=' && !IS_WHITE_SPACE(*p->xml) )
  49. {
  50. attnamelen++; p->xml++;
  51. if(p->xml >= p->xmlend)
  52. return -1;
  53. }
  54. while(*(p->xml++) != '=')
  55. {
  56. if(p->xml >= p->xmlend)
  57. return -1;
  58. }
  59. while(IS_WHITE_SPACE(*p->xml))
  60. {
  61. p->xml++;
  62. if(p->xml >= p->xmlend)
  63. return -1;
  64. }
  65. sep = *p->xml;
  66. if(sep=='\'' || sep=='\"')
  67. {
  68. p->xml++;
  69. if(p->xml >= p->xmlend)
  70. return -1;
  71. attvalue = p->xml;
  72. attvaluelen = 0;
  73. while(*p->xml != sep)
  74. {
  75. attvaluelen++; p->xml++;
  76. if(p->xml >= p->xmlend)
  77. return -1;
  78. }
  79. }
  80. else
  81. {
  82. attvalue = p->xml;
  83. attvaluelen = 0;
  84. while( !IS_WHITE_SPACE(*p->xml)
  85. && *p->xml != '>' && *p->xml != '/')
  86. {
  87. attvaluelen++; p->xml++;
  88. if(p->xml >= p->xmlend)
  89. return -1;
  90. }
  91. }
  92. /*printf("%.*s='%.*s'\n",
  93. attnamelen, attname, attvaluelen, attvalue);*/
  94. if(p->attfunc)
  95. p->attfunc(p->data, attname, attnamelen, attvalue, attvaluelen);
  96. }
  97. p->xml++;
  98. }
  99. return -1;
  100. }
  101. /* parseelt parse the xml stream and
  102. * call the callback functions when needed... */
  103. void parseelt(struct xmlparser * p)
  104. {
  105. int i;
  106. const char * elementname;
  107. while(p->xml < (p->xmlend - 1))
  108. {
  109. if((p->xml)[0]=='<' && (p->xml)[1]!='?')
  110. {
  111. i = 0; elementname = ++p->xml;
  112. while( !IS_WHITE_SPACE(*p->xml)
  113. && (*p->xml!='>') && (*p->xml!='/')
  114. )
  115. {
  116. i++; p->xml++;
  117. if (p->xml >= p->xmlend)
  118. return;
  119. /* to ignore namespace : */
  120. if(*p->xml==':')
  121. {
  122. i = 0;
  123. elementname = ++p->xml;
  124. }
  125. }
  126. if(i>0)
  127. {
  128. if(p->starteltfunc)
  129. p->starteltfunc(p->data, elementname, i);
  130. if(parseatt(p))
  131. return;
  132. if(*p->xml!='/')
  133. {
  134. const char * data;
  135. i = 0; data = ++p->xml;
  136. if (p->xml >= p->xmlend)
  137. return;
  138. while( IS_WHITE_SPACE(*p->xml) )
  139. {
  140. p->xml++;
  141. if (p->xml >= p->xmlend)
  142. return;
  143. }
  144. while(*p->xml!='<')
  145. {
  146. i++; p->xml++;
  147. if (p->xml >= p->xmlend)
  148. return;
  149. }
  150. if(i>0 && p->datafunc)
  151. p->datafunc(p->data, data, i);
  152. }
  153. }
  154. else if(*p->xml == '/')
  155. {
  156. i = 0; elementname = ++p->xml;
  157. if (p->xml >= p->xmlend)
  158. return;
  159. while((*p->xml != '>'))
  160. {
  161. i++; p->xml++;
  162. if (p->xml >= p->xmlend)
  163. return;
  164. }
  165. if(p->endeltfunc)
  166. p->endeltfunc(p->data, elementname, i);
  167. p->xml++;
  168. }
  169. }
  170. else
  171. {
  172. p->xml++;
  173. }
  174. }
  175. }
  176. /* the parser must be initialized before calling this function */
  177. void parsexml(struct xmlparser * parser)
  178. {
  179. parser->xml = parser->xmlstart;
  180. parser->xmlend = parser->xmlstart + parser->xmlsize;
  181. parseelt(parser);
  182. }