| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
- /* Copyright (C) 2001 by First Peer, Inc. All rights reserved.
- ** Copyright (C) 2002 Ximian, Inc.
- **
- ** Redistribution and use in source and binary forms, with or without
- ** modification, are permitted provided that the following conditions
- ** are met:
- ** 1. Redistributions of source code must retain the above copyright
- ** notice, this list of conditions and the following disclaimer.
- ** 2. Redistributions in binary form must reproduce the above copyright
- ** notice, this list of conditions and the following disclaimer in the
- ** documentation and/or other materials provided with the distribution.
- ** 3. The name of the author may not be used to endorse or promote products
- ** derived from this software without specific prior written permission.
- **
- ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- ** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- ** SUCH DAMAGE. */
- #include "xmlrpc_config.h"
- #include <stddef.h>
- #include <stdlib.h>
- #include <string.h>
- #include <libxml/parser.h>
- #include "xmlrpc.h"
- #include "xmlrpc_xmlparser.h"
- /* Define the contents of our internal structure. */
- struct _xml_element {
- struct _xml_element *_parent;
- char *_name;
- xmlrpc_mem_block _cdata; /* char */
- xmlrpc_mem_block _children; /* xml_element* */
- };
- #define XMLRPC_ASSERT_ELEM_OK(elem) \
- XMLRPC_ASSERT((elem) != NULL && (elem)->_name != XMLRPC_BAD_POINTER)
- /*=========================================================================
- ** xml_element_new
- **=========================================================================
- ** Create a new xml_element. This routine isn't exported, because the
- ** arguments are implementation-dependent.
- */
- static xml_element *xml_element_new (xmlrpc_env *env, char *name)
- {
- xml_element *retval;
- int name_valid, cdata_valid, children_valid;
- XMLRPC_ASSERT_ENV_OK(env);
- XMLRPC_ASSERT(name != NULL);
- /* Set up our error-handling preconditions. */
- retval = NULL;
- name_valid = cdata_valid = children_valid = 0;
- /* Allocate our xml_element structure. */
- retval = (xml_element*) malloc(sizeof(xml_element));
- XMLRPC_FAIL_IF_NULL(retval, env, XMLRPC_INTERNAL_ERROR,
- "Couldn't allocate memory for XML element");
- /* Set our parent field to NULL. */
- retval->_parent = NULL;
-
- /* Copy over the element name. */
- retval->_name = (char*) malloc(strlen(name) + 1);
- XMLRPC_FAIL_IF_NULL(retval->_name, env, XMLRPC_INTERNAL_ERROR,
- "Couldn't allocate memory for XML element");
- name_valid = 1;
- strcpy(retval->_name, name);
- /* Initialize a block to hold our CDATA. */
- XMLRPC_TYPED_MEM_BLOCK_INIT(char, env, &retval->_cdata, 0);
- XMLRPC_FAIL_IF_FAULT(env);
- cdata_valid = 1;
- /* Initialize a block to hold our child elements. */
- XMLRPC_TYPED_MEM_BLOCK_INIT(xml_element*, env, &retval->_children, 0);
- XMLRPC_FAIL_IF_FAULT(env);
- children_valid = 1;
- cleanup:
- if (env->fault_occurred) {
- if (retval) {
- if (name_valid)
- free(retval->_name);
- if (cdata_valid)
- xmlrpc_mem_block_clean(&retval->_cdata);
- if (children_valid)
- xmlrpc_mem_block_clean(&retval->_children);
- free(retval);
- }
- return NULL;
- } else {
- return retval;
- }
- }
- /*=========================================================================
- ** xml_element_free
- **=========================================================================
- ** Blow away an existing element & all of its child elements.
- */
- void xml_element_free (xml_element *elem)
- {
- xmlrpc_mem_block *children;
- int size, i;
- xml_element **contents;
- XMLRPC_ASSERT_ELEM_OK(elem);
- free(elem->_name);
- elem->_name = XMLRPC_BAD_POINTER;
- xmlrpc_mem_block_clean(&elem->_cdata);
- /* Deallocate all of our children recursively. */
- children = &elem->_children;
- contents = XMLRPC_TYPED_MEM_BLOCK_CONTENTS(xml_element*, children);
- size = XMLRPC_TYPED_MEM_BLOCK_SIZE(xml_element*, children);
- for (i = 0; i < size; i++)
- xml_element_free(contents[i]);
- xmlrpc_mem_block_clean(&elem->_children);
- free(elem);
- }
- /*=========================================================================
- ** Miscellaneous Accessors
- **=========================================================================
- ** Return the fields of the xml_element. See the header for more
- ** documentation on each function works.
- */
- char *xml_element_name (xml_element *elem)
- {
- XMLRPC_ASSERT_ELEM_OK(elem);
- return elem->_name;
- }
- /* The result of this function is NOT VALID until the end_element handler
- ** has been called! */
- size_t xml_element_cdata_size (xml_element *elem)
- {
- XMLRPC_ASSERT_ELEM_OK(elem);
- return XMLRPC_TYPED_MEM_BLOCK_SIZE(char, &elem->_cdata) - 1;
- }
- char *xml_element_cdata (xml_element *elem)
- {
- XMLRPC_ASSERT_ELEM_OK(elem);
- return XMLRPC_TYPED_MEM_BLOCK_CONTENTS(char, &elem->_cdata);
- }
- size_t xml_element_children_size (xml_element *elem)
- {
- XMLRPC_ASSERT_ELEM_OK(elem);
- return XMLRPC_TYPED_MEM_BLOCK_SIZE(xml_element*, &elem->_children);
- }
- xml_element **xml_element_children (xml_element *elem)
- {
- XMLRPC_ASSERT_ELEM_OK(elem);
- return XMLRPC_TYPED_MEM_BLOCK_CONTENTS(xml_element*, &elem->_children);
- }
- /*=========================================================================
- ** Internal xml_element Utility Functions
- **=========================================================================
- */
- static void xml_element_append_cdata (xmlrpc_env *env,
- xml_element *elem,
- char *cdata,
- size_t size)
- {
- XMLRPC_ASSERT_ENV_OK(env);
- XMLRPC_ASSERT_ELEM_OK(elem);
- XMLRPC_TYPED_MEM_BLOCK_APPEND(char, env, &elem->_cdata, cdata, size);
- }
- /* Whether or not this function succeeds, it takes ownership of the 'child'
- ** argument.
- ** WARNING - This is the exact opposite of the usual memory ownership
- ** rules for xmlrpc_value! So please pay attention. */
- static void xml_element_append_child (xmlrpc_env *env,
- xml_element *elem,
- xml_element *child)
- {
- XMLRPC_ASSERT_ENV_OK(env);
- XMLRPC_ASSERT_ELEM_OK(elem);
- XMLRPC_ASSERT_ELEM_OK(child);
- XMLRPC_ASSERT(child->_parent == NULL);
- XMLRPC_TYPED_MEM_BLOCK_APPEND(xml_element*, env, &elem->_children,
- &child, 1);
- if (!env->fault_occurred)
- child->_parent = elem;
- else
- xml_element_free(child);
- }
- /*=========================================================================
- ** Our parse context. We pass this around as libxml user data.
- **=========================================================================
- */
- typedef struct {
- xmlrpc_env *env;
- xml_element *root;
- xml_element *current;
- } parse_context;
- /*=========================================================================
- ** LibXML Event Handler Functions
- **=========================================================================
- */
- static void
- start_element (void *user_data, const xmlChar *name, const xmlChar **attrs)
- {
- parse_context *context;
- xml_element *elem, *new_current;
- XMLRPC_ASSERT(user_data != NULL && name != NULL);
- /* Get our context and see if an error has already occured. */
- context = (parse_context*) user_data;
- if (!context->env->fault_occurred) {
- /* Set up our error-handling preconditions. */
- elem = NULL;
- /* Build a new element. */
- elem = xml_element_new(context->env, (char *) name);
- XMLRPC_FAIL_IF_FAULT(context->env);
- /* Insert it in the appropriate place. */
- if (!context->root) {
- context->root = elem;
- context->current = elem;
- elem = NULL;
- } else {
- XMLRPC_ASSERT(context->current != NULL);
- /* (We need to watch our error handling invariants very carefully
- ** here. Read the docs for xml_element_append_child. */
- new_current = elem;
- xml_element_append_child(context->env, context->current, elem);
- elem = NULL;
- XMLRPC_FAIL_IF_FAULT(context->env);
- context->current = new_current;
- }
- cleanup:
- if (elem)
- xml_element_free(elem);
- }
- }
- static void
- end_element (void *user_data, const xmlChar *name)
- {
- parse_context *context;
- XMLRPC_ASSERT(user_data != NULL && name != NULL);
- /* Get our context and see if an error has already occured. */
- context = (parse_context*) user_data;
- if (!context->env->fault_occurred) {
- /* XXX - I think expat enforces these facts, but I want to be sure.
- ** If one of these assertion ever fails, it should be replaced by a
- ** non-assertion runtime error check. */
- XMLRPC_ASSERT(strcmp(name, context->current->_name) == 0);
- XMLRPC_ASSERT(context->current->_parent != NULL ||
- context->current == context->root);
- /* Add a trailing '\0' to our cdata. */
- xml_element_append_cdata(context->env, context->current, "\0", 1);
- XMLRPC_FAIL_IF_FAULT(context->env);
- /* Pop our "stack" of elements. */
- context->current = context->current->_parent;
- cleanup:
- return;
- }
- }
- static void character_data (void *user_data, const xmlChar *s, int len)
- {
- parse_context *context;
- XMLRPC_ASSERT(user_data != NULL && s != NULL && len >= 0);
- /* Get our context and see if an error has already occured. */
- context = (parse_context*) user_data;
- if (!context->env->fault_occurred) {
- XMLRPC_ASSERT(context->current != NULL);
-
- xml_element_append_cdata(context->env, context->current, (char *) s, len);
- XMLRPC_FAIL_IF_FAULT(context->env);
- cleanup:
- return;
- }
- }
- /*=========================================================================
- ** LibXML Driver
- **=========================================================================
- ** XXX - We should allow the user to specify the encoding of our xml_data.
- */
- static xmlSAXHandler sax_handler = {
- NULL, /* internalSubset */
- NULL, /* isStandalone */
- NULL, /* hasInternalSubset */
- NULL, /* hasExternalSubset */
- NULL, /* resolveEntity */
- NULL, /* getEntity */
- NULL, /* entityDecl */
- NULL, /* notationDecl */
- NULL, /* attributeDecl */
- NULL, /* elementDecl */
- NULL, /* unparsedEntityDecl */
- NULL, /* setDocumentLocator */
- NULL, /* startDocument */
- NULL, /* endDocument */
- start_element, /* startElement */
- end_element, /* endElement */
- NULL, /* reference */
- character_data, /* characters */
- NULL, /* ignorableWhitespace */
- NULL, /* processingInstruction */
- NULL, /* comment */
- NULL, /* warning */
- NULL, /* error */
- NULL, /* fatalError */
- };
- xml_element *xml_parse (xmlrpc_env *env, char *xml_data, int xml_len)
- {
- parse_context context;
- xmlParserCtxt *parser;
- int err;
- XMLRPC_ASSERT_ENV_OK(env);
- XMLRPC_ASSERT(xml_data != NULL && xml_len >= 0);
- /* Set up our error-handling preconditions. */
- parser = NULL;
- context.root = NULL;
-
- /* Set up the rest of our parse context. */
- context.env = env;
- context.current = NULL;
- /* Set up our XML parser. */
- parser = xmlCreatePushParserCtxt(&sax_handler, &context, NULL, 0, NULL);
- XMLRPC_FAIL_IF_NULL(parser, env, XMLRPC_INTERNAL_ERROR,
- "Could not create expat parser");
- /* Parse our data. */
- err = xmlParseChunk(parser, xml_data, xml_len, 1);
- if (err)
- XMLRPC_FAIL(env, XMLRPC_PARSE_ERROR, "XML parsing failed");
- XMLRPC_FAIL_IF_FAULT(env);
- /* Perform some sanity checks. */
- XMLRPC_ASSERT(context.root != NULL);
- XMLRPC_ASSERT(context.current == NULL);
- cleanup:
- if (parser)
- xmlFreeParserCtxt(parser);
- if (env->fault_occurred) {
- if (context.root)
- xml_element_free(context.root);
- return NULL;
- } else {
- return context.root;
- }
- }
|