xmlrpc_expat.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* Copyright (C) 2001 by First Peer, Inc. All rights reserved.
  2. **
  3. ** Redistribution and use in source and binary forms, with or without
  4. ** modification, are permitted provided that the following conditions
  5. ** are met:
  6. ** 1. Redistributions of source code must retain the above copyright
  7. ** notice, this list of conditions and the following disclaimer.
  8. ** 2. Redistributions in binary form must reproduce the above copyright
  9. ** notice, this list of conditions and the following disclaimer in the
  10. ** documentation and/or other materials provided with the distribution.
  11. ** 3. The name of the author may not be used to endorse or promote products
  12. ** derived from this software without specific prior written permission.
  13. **
  14. ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. ** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. ** SUCH DAMAGE. */
  25. #include "xmlrpc_config.h"
  26. #include <stddef.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <expat.h>
  30. #include "xmlrpc.h"
  31. #include "xmlrpc_int.h"
  32. #include "xmlrpc_xmlparser.h"
  33. /* Define the contents of our internal structure. */
  34. struct _xml_element {
  35. struct _xml_element *_parent;
  36. char *_name;
  37. xmlrpc_mem_block _cdata; /* char */
  38. xmlrpc_mem_block _children; /* xml_element* */
  39. };
  40. /* Check that we're using expat in UTF-8 mode, not wchar_t mode.
  41. ** If you need to use expat in wchar_t mode, write a subroutine to
  42. ** copy a wchar_t string to a char string & return an error for
  43. ** any non-ASCII characters. Then call this subroutine on all
  44. ** XML_Char strings passed to our event handlers before using the
  45. ** data. */
  46. /* #if sizeof(char) != sizeof(XML_Char)
  47. ** #error expat must define XML_Char to be a regular char.
  48. ** #endif
  49. */
  50. #define XMLRPC_ASSERT_ELEM_OK(elem) \
  51. XMLRPC_ASSERT((elem) != NULL && (elem)->_name != XMLRPC_BAD_POINTER)
  52. /*=========================================================================
  53. ** xml_element_new
  54. **=========================================================================
  55. ** Create a new xml_element. This routine isn't exported, because the
  56. ** arguments are implementation-dependent.
  57. */
  58. static xml_element *xml_element_new (xmlrpc_env *env, char *name)
  59. {
  60. xml_element *retval;
  61. int name_valid, cdata_valid, children_valid;
  62. XMLRPC_ASSERT_ENV_OK(env);
  63. XMLRPC_ASSERT(name != NULL);
  64. /* Set up our error-handling preconditions. */
  65. retval = NULL;
  66. name_valid = cdata_valid = children_valid = 0;
  67. /* Allocate our xml_element structure. */
  68. retval = (xml_element*) malloc(sizeof(xml_element));
  69. XMLRPC_FAIL_IF_NULL(retval, env, XMLRPC_INTERNAL_ERROR,
  70. "Couldn't allocate memory for XML element");
  71. /* Set our parent field to NULL. */
  72. retval->_parent = NULL;
  73. /* Copy over the element name. */
  74. retval->_name = (char*) malloc(strlen(name) + 1);
  75. XMLRPC_FAIL_IF_NULL(retval->_name, env, XMLRPC_INTERNAL_ERROR,
  76. "Couldn't allocate memory for XML element");
  77. name_valid = 1;
  78. strcpy(retval->_name, name);
  79. /* Initialize a block to hold our CDATA. */
  80. XMLRPC_TYPED_MEM_BLOCK_INIT(char, env, &retval->_cdata, 0);
  81. XMLRPC_FAIL_IF_FAULT(env);
  82. cdata_valid = 1;
  83. /* Initialize a block to hold our child elements. */
  84. XMLRPC_TYPED_MEM_BLOCK_INIT(xml_element*, env, &retval->_children, 0);
  85. XMLRPC_FAIL_IF_FAULT(env);
  86. children_valid = 1;
  87. cleanup:
  88. if (env->fault_occurred) {
  89. if (retval) {
  90. if (name_valid)
  91. free(retval->_name);
  92. if (cdata_valid)
  93. xmlrpc_mem_block_clean(&retval->_cdata);
  94. if (children_valid)
  95. xmlrpc_mem_block_clean(&retval->_children);
  96. free(retval);
  97. }
  98. return NULL;
  99. } else {
  100. return retval;
  101. }
  102. }
  103. /*=========================================================================
  104. ** xml_element_free
  105. **=========================================================================
  106. ** Blow away an existing element & all of its child elements.
  107. */
  108. void xml_element_free (xml_element *elem)
  109. {
  110. xmlrpc_mem_block *children;
  111. int size, i;
  112. xml_element **contents;
  113. XMLRPC_ASSERT_ELEM_OK(elem);
  114. free(elem->_name);
  115. elem->_name = XMLRPC_BAD_POINTER;
  116. xmlrpc_mem_block_clean(&elem->_cdata);
  117. /* Deallocate all of our children recursively. */
  118. children = &elem->_children;
  119. contents = XMLRPC_TYPED_MEM_BLOCK_CONTENTS(xml_element*, children);
  120. size = XMLRPC_TYPED_MEM_BLOCK_SIZE(xml_element*, children);
  121. for (i = 0; i < size; i++)
  122. xml_element_free(contents[i]);
  123. xmlrpc_mem_block_clean(&elem->_children);
  124. free(elem);
  125. }
  126. /*=========================================================================
  127. ** Miscellaneous Accessors
  128. **=========================================================================
  129. ** Return the fields of the xml_element. See the header for more
  130. ** documentation on each function works.
  131. */
  132. char *xml_element_name (xml_element *elem)
  133. {
  134. XMLRPC_ASSERT_ELEM_OK(elem);
  135. return elem->_name;
  136. }
  137. /* The result of this function is NOT VALID until the end_element handler
  138. ** has been called! */
  139. size_t xml_element_cdata_size (xml_element *elem)
  140. {
  141. XMLRPC_ASSERT_ELEM_OK(elem);
  142. return XMLRPC_TYPED_MEM_BLOCK_SIZE(char, &elem->_cdata) - 1;
  143. }
  144. char *xml_element_cdata (xml_element *elem)
  145. {
  146. XMLRPC_ASSERT_ELEM_OK(elem);
  147. return XMLRPC_TYPED_MEM_BLOCK_CONTENTS(char, &elem->_cdata);
  148. }
  149. size_t xml_element_children_size (xml_element *elem)
  150. {
  151. XMLRPC_ASSERT_ELEM_OK(elem);
  152. return XMLRPC_TYPED_MEM_BLOCK_SIZE(xml_element*, &elem->_children);
  153. }
  154. xml_element **xml_element_children (xml_element *elem)
  155. {
  156. XMLRPC_ASSERT_ELEM_OK(elem);
  157. return XMLRPC_TYPED_MEM_BLOCK_CONTENTS(xml_element*, &elem->_children);
  158. }
  159. /*=========================================================================
  160. ** Internal xml_element Utility Functions
  161. **=========================================================================
  162. */
  163. static void xml_element_append_cdata (xmlrpc_env *env,
  164. xml_element *elem,
  165. char *cdata,
  166. size_t size)
  167. {
  168. XMLRPC_ASSERT_ENV_OK(env);
  169. XMLRPC_ASSERT_ELEM_OK(elem);
  170. XMLRPC_TYPED_MEM_BLOCK_APPEND(char, env, &elem->_cdata, cdata, size);
  171. }
  172. /* Whether or not this function succeeds, it takes ownership of the 'child'
  173. ** argument.
  174. ** WARNING - This is the exact opposite of the usual memory ownership
  175. ** rules for xmlrpc_value! So please pay attention. */
  176. static void xml_element_append_child (xmlrpc_env *env,
  177. xml_element *elem,
  178. xml_element *child)
  179. {
  180. XMLRPC_ASSERT_ENV_OK(env);
  181. XMLRPC_ASSERT_ELEM_OK(elem);
  182. XMLRPC_ASSERT_ELEM_OK(child);
  183. XMLRPC_ASSERT(child->_parent == NULL);
  184. XMLRPC_TYPED_MEM_BLOCK_APPEND(xml_element*, env, &elem->_children,
  185. &child, 1);
  186. if (!env->fault_occurred)
  187. child->_parent = elem;
  188. else
  189. xml_element_free(child);
  190. }
  191. /*=========================================================================
  192. ** Our parse context. We pass this around as expat user data.
  193. **=========================================================================
  194. */
  195. typedef struct {
  196. xmlrpc_env *env;
  197. xml_element *root;
  198. xml_element *current;
  199. } parse_context;
  200. /*=========================================================================
  201. ** Expat Event Handler Functions
  202. **=========================================================================
  203. */
  204. static void
  205. start_element (void *user_data, XML_Char *name, XML_Char **atts ATTR_UNUSED)
  206. {
  207. parse_context *context;
  208. xml_element *elem, *new_current;
  209. XMLRPC_ASSERT(user_data != NULL && name != NULL);
  210. /* Get our context and see if an error has already occured. */
  211. context = (parse_context*) user_data;
  212. if (!context->env->fault_occurred) {
  213. /* Set up our error-handling preconditions. */
  214. elem = NULL;
  215. /* Build a new element. */
  216. elem = xml_element_new(context->env, name);
  217. XMLRPC_FAIL_IF_FAULT(context->env);
  218. /* Insert it in the appropriate place. */
  219. if (!context->root) {
  220. context->root = elem;
  221. context->current = elem;
  222. elem = NULL;
  223. } else {
  224. XMLRPC_ASSERT(context->current != NULL);
  225. /* (We need to watch our error handling invariants very carefully
  226. ** here. Read the docs for xml_element_append_child. */
  227. new_current = elem;
  228. xml_element_append_child(context->env, context->current, elem);
  229. elem = NULL;
  230. XMLRPC_FAIL_IF_FAULT(context->env);
  231. context->current = new_current;
  232. }
  233. cleanup:
  234. if (elem)
  235. xml_element_free(elem);
  236. }
  237. }
  238. static void end_element (void *user_data, XML_Char *name)
  239. {
  240. parse_context *context;
  241. XMLRPC_ASSERT(user_data != NULL && name != NULL);
  242. /* Get our context and see if an error has already occured. */
  243. context = (parse_context*) user_data;
  244. if (!context->env->fault_occurred) {
  245. /* XXX - I think expat enforces these facts, but I want to be sure.
  246. ** If one of these assertion ever fails, it should be replaced by a
  247. ** non-assertion runtime error check. */
  248. XMLRPC_ASSERT(strcmp(name, context->current->_name) == 0);
  249. XMLRPC_ASSERT(context->current->_parent != NULL ||
  250. context->current == context->root);
  251. /* Add a trailing '\0' to our cdata. */
  252. xml_element_append_cdata(context->env, context->current, "\0", 1);
  253. XMLRPC_FAIL_IF_FAULT(context->env);
  254. /* Pop our "stack" of elements. */
  255. context->current = context->current->_parent;
  256. cleanup:
  257. return;
  258. }
  259. }
  260. static void character_data (void *user_data, XML_Char *s, int len)
  261. {
  262. parse_context *context;
  263. XMLRPC_ASSERT(user_data != NULL && s != NULL && len >= 0);
  264. /* Get our context and see if an error has already occured. */
  265. context = (parse_context*) user_data;
  266. if (!context->env->fault_occurred) {
  267. XMLRPC_ASSERT(context->current != NULL);
  268. xml_element_append_cdata(context->env, context->current, s, len);
  269. XMLRPC_FAIL_IF_FAULT(context->env);
  270. cleanup:
  271. return;
  272. }
  273. }
  274. /*=========================================================================
  275. ** Expat Driver
  276. **=========================================================================
  277. ** XXX - We should allow the user to specify the encoding of our xml_data.
  278. */
  279. xml_element *xml_parse (xmlrpc_env *env, const char *xml_data, int xml_len)
  280. {
  281. parse_context context;
  282. XML_Parser parser;
  283. int ok;
  284. XMLRPC_ASSERT_ENV_OK(env);
  285. XMLRPC_ASSERT(xml_data != NULL && xml_len >= 0);
  286. /* Set up our error-handling preconditions. */
  287. parser = NULL;
  288. context.root = NULL;
  289. /* Set up the rest of our parse context. */
  290. context.env = env;
  291. context.current = NULL;
  292. /* Set up our XML parser. */
  293. parser = XML_ParserCreate(NULL);
  294. XMLRPC_FAIL_IF_NULL(parser, env, XMLRPC_INTERNAL_ERROR,
  295. "Could not create expat parser");
  296. XML_SetUserData(parser, &context);
  297. XML_SetElementHandler(parser,
  298. (XML_StartElementHandler) start_element,
  299. (XML_EndElementHandler) end_element);
  300. XML_SetCharacterDataHandler(parser,
  301. (XML_CharacterDataHandler) character_data);
  302. /* Parse our data. */
  303. ok = XML_Parse(parser, xml_data, xml_len, 1);
  304. if (!ok)
  305. XMLRPC_FAIL(env, XMLRPC_PARSE_ERROR,
  306. (char*) XML_ErrorString(XML_GetErrorCode(parser)));
  307. XMLRPC_FAIL_IF_FAULT(env);
  308. /* Perform some sanity checks. */
  309. XMLRPC_ASSERT(context.root != NULL);
  310. XMLRPC_ASSERT(context.current == NULL);
  311. cleanup:
  312. if (parser)
  313. XML_ParserFree(parser);
  314. if (env->fault_occurred) {
  315. if (context.root)
  316. xml_element_free(context.root);
  317. return NULL;
  318. } else {
  319. return context.root;
  320. }
  321. }