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