xstream.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * $Id$
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Jabber
  19. * Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
  20. */
  21. /*! \file
  22. * \ingroup xmpp
  23. */
  24. #include "xode.h"
  25. /* xode_stream is a way to have a consistent method of handling incoming XML Stream based events... it doesn't handle the generation of an XML Stream, but provides some facilities to help do that */
  26. static void _xode_put_expatattribs(xode owner, const char** atts)
  27. {
  28. int i = 0;
  29. if (atts == NULL) return;
  30. while (atts[i] != '\0')
  31. {
  32. xode_put_attrib(owner, atts[i], atts[i+1]);
  33. i += 2;
  34. }
  35. }
  36. /******* internal expat callbacks *********/
  37. static void _xode_stream_startElement(xode_stream xs, const char* name, const char** atts)
  38. {
  39. xode_pool p;
  40. /* if xode_stream is bad, get outa here */
  41. if(xs->status > XODE_STREAM_NODE) return;
  42. if(xs->node == NULL)
  43. {
  44. p = xode_pool_heap(5*1024); /* 5k, typically 1-2k each plus copy of self and workspace */
  45. xs->node = xode_new_frompool(p,name);
  46. _xode_put_expatattribs(xs->node, atts);
  47. if(xs->status == XODE_STREAM_ROOT)
  48. {
  49. xs->status = XODE_STREAM_NODE; /* flag status that we're processing nodes now */
  50. (xs->f)(XODE_STREAM_ROOT, xs->node, xs->arg); /* send the root, f must free all nodes */
  51. xs->node = NULL;
  52. }
  53. }else{
  54. xs->node = xode_insert_tag(xs->node, name);
  55. _xode_put_expatattribs(xs->node, atts);
  56. }
  57. /* depth check */
  58. xs->depth++;
  59. if(xs->depth > XODE_STREAM_MAXDEPTH)
  60. xs->status = XODE_STREAM_ERROR;
  61. }
  62. static void _xode_stream_endElement(xode_stream xs, const char* name)
  63. {
  64. xode parent;
  65. /* if xode_stream is bad, get outa here */
  66. if(xs->status > XODE_STREAM_NODE) return;
  67. /* if it's already NULL we've received </stream>, tell the app and we're outta here */
  68. if(xs->node == NULL)
  69. {
  70. xs->status = XODE_STREAM_CLOSE;
  71. (xs->f)(XODE_STREAM_CLOSE, NULL, xs->arg);
  72. }else{
  73. parent = xode_get_parent(xs->node);
  74. /* we are the top-most node, feed to the app who is responsible to delete it */
  75. if(parent == NULL)
  76. (xs->f)(XODE_STREAM_NODE, xs->node, xs->arg);
  77. xs->node = parent;
  78. }
  79. xs->depth--;
  80. }
  81. static void _xode_stream_charData(xode_stream xs, const char *str, int len)
  82. {
  83. /* if xode_stream is bad, get outa here */
  84. if(xs->status > XODE_STREAM_NODE) return;
  85. if(xs->node == NULL)
  86. {
  87. /* we must be in the root of the stream where CDATA is irrelevant */
  88. return;
  89. }
  90. xode_insert_cdata(xs->node, str, len);
  91. }
  92. static void _xode_stream_cleanup(void *arg)
  93. {
  94. xode_stream xs = (xode_stream)arg;
  95. xode_free(xs->node); /* cleanup anything left over */
  96. XML_ParserFree(xs->parser);
  97. }
  98. /* creates a new xode_stream with given pool, xode_stream will be cleaned up w/ pool */
  99. xode_stream xode_stream_new(xode_pool p, xode_stream_onNode f, void *arg)
  100. {
  101. xode_stream newx;
  102. if(p == NULL || f == NULL)
  103. {
  104. fprintf(stderr,"Fatal Programming Error: xode_streamnew() was improperly called with NULL.\n");
  105. return NULL;
  106. }
  107. newx = xode_pool_malloco(p, sizeof(_xode_stream));
  108. newx->p = p;
  109. newx->f = f;
  110. newx->arg = arg;
  111. /* create expat parser and ensure cleanup */
  112. newx->parser = XML_ParserCreate(NULL);
  113. XML_SetUserData(newx->parser, (void *)newx);
  114. XML_SetElementHandler(newx->parser,
  115. (void (*)(void*, const char*, const char**))_xode_stream_startElement,
  116. (void (*)(void*, const char*))_xode_stream_endElement);
  117. XML_SetCharacterDataHandler(newx->parser,
  118. (void (*)(void*, const char*, int))_xode_stream_charData);
  119. xode_pool_cleanup(p, _xode_stream_cleanup, (void *)newx);
  120. return newx;
  121. }
  122. /* attempts to parse the buff onto this stream firing events to the handler, returns the last known status */
  123. int xode_stream_eat(xode_stream xs, char *buff, int len)
  124. {
  125. char *err;
  126. xode xerr;
  127. static char maxerr[] = "maximum node size reached";
  128. static char deeperr[] = "maximum node depth reached";
  129. if(xs == NULL)
  130. {
  131. fprintf(stderr,"Fatal Programming Error: xode_streameat() was improperly called with NULL.\n");
  132. return XODE_STREAM_ERROR;
  133. }
  134. if(len == 0 || buff == NULL)
  135. return xs->status;
  136. if(len == -1) /* easy for hand-fed eat calls */
  137. len = strlen(buff);
  138. if(!XML_Parse(xs->parser, buff, len, 0))
  139. {
  140. err = (char *)XML_ErrorString(XML_GetErrorCode(xs->parser));
  141. xs->status = XODE_STREAM_ERROR;
  142. }else if(xode_pool_size(xode_get_pool(xs->node)) > XODE_STREAM_MAXNODE || xs->cdata_len > XODE_STREAM_MAXNODE){
  143. err = maxerr;
  144. xs->status = XODE_STREAM_ERROR;
  145. }else if(xs->status == XODE_STREAM_ERROR){ /* set within expat handlers */
  146. err = deeperr;
  147. }else{
  148. err = deeperr;
  149. }
  150. /* fire parsing error event, make a node containing the error string */
  151. if(xs->status == XODE_STREAM_ERROR)
  152. {
  153. xerr = xode_new("error");
  154. xode_insert_cdata(xerr,err,-1);
  155. (xs->f)(XODE_STREAM_ERROR, xerr, xs->arg);
  156. }
  157. return xs->status;
  158. }