diff --git a/ixml/inc/ixmldebug.h b/ixml/inc/ixmldebug.h index ac97680..59b57f2 100644 --- a/ixml/inc/ixmldebug.h +++ b/ixml/inc/ixmldebug.h @@ -40,8 +40,11 @@ void IxmlPrintf( ; #else /* DEBUG */ static UPNP_INLINE void IxmlPrintf( - const char* FmtStr, - ...) {} + const char *FmtStr, + ...) +{ + FmtStr = FmtStr; +} #endif /* DEBUG */ @@ -59,6 +62,8 @@ static UPNP_INLINE void printNodes( IXML_Node *tmpRoot, int depth) { + tmpRoot = tmpRoot; + depth = depth; } #endif diff --git a/ixml/src/document.c b/ixml/src/document.c index 4a6c191..eeec48f 100644 --- a/ixml/src/document.c +++ b/ixml/src/document.c @@ -139,7 +139,7 @@ int ixmlDocument_createElementEx( errCode = IXML_INSUFFICIENT_MEMORY; goto ErrorHandler; } - // set the node fields + /* set the node fields */ newElement->n.nodeType = eELEMENT_NODE; newElement->n.nodeName = strdup(tagName); if (newElement->n.nodeName == NULL) { @@ -229,7 +229,7 @@ int ixmlDocument_createTextNodeEx( rc = IXML_INSUFFICIENT_MEMORY; goto ErrorHandler; } - // initialize the node + /* initialize the node */ ixmlNode_init(returnNode); returnNode->nodeName = strdup(TEXTNODENAME); @@ -239,7 +239,7 @@ int ixmlDocument_createTextNodeEx( rc = IXML_INSUFFICIENT_MEMORY; goto ErrorHandler; } - // add in node value + /* add in node value */ if (data != NULL) { returnNode->nodeValue = strdup(data); if (returnNode->nodeValue == NULL) { @@ -295,7 +295,7 @@ int ixmlDocument_createAttributeEx( ixmlAttr_init(attrNode); attrNode->n.nodeType = eATTRIBUTE_NODE; - // set the node fields + /* set the node fields */ attrNode->n.nodeName = strdup(name); if (attrNode->n.nodeName == NULL) { ixmlAttr_free(attrNode); @@ -343,7 +343,7 @@ int ixmlDocument_createAttributeNSEx( if (errCode != IXML_SUCCESS) { goto ErrorHandler; } - // set the namespaceURI field + /* set the namespaceURI field */ attrNode->n.namespaceURI = strdup(namespaceURI); if (attrNode->n.namespaceURI == NULL) { ixmlAttr_free(attrNode); @@ -351,7 +351,7 @@ int ixmlDocument_createAttributeNSEx( errCode = IXML_INSUFFICIENT_MEMORY; goto ErrorHandler; } - // set the localName and prefix + /* set the localName and prefix */ errCode = ixmlNode_setNodeName((IXML_Node *)attrNode, qualifiedName); if (errCode != IXML_SUCCESS) { @@ -458,7 +458,7 @@ int ixmlDocument_createElementNSEx( line = __LINE__; goto ErrorHandler; } - // set the namespaceURI field + /* set the namespaceURI field */ newElement->n.namespaceURI = strdup(namespaceURI); if (newElement->n.namespaceURI == NULL) { line = __LINE__; @@ -467,7 +467,7 @@ int ixmlDocument_createElementNSEx( ret = IXML_INSUFFICIENT_MEMORY; goto ErrorHandler; } - // set the localName and prefix + /* set the localName and prefix */ ret = ixmlNode_setNodeName((IXML_Node *)newElement, qualifiedName); if (ret != IXML_SUCCESS) { line = __LINE__; diff --git a/ixml/src/element.c b/ixml/src/element.c index b554314..7055999 100644 --- a/ixml/src/element.c +++ b/ixml/src/element.c @@ -110,7 +110,7 @@ int ixmlElement_setAttribute( { IXML_Node *attrNode; IXML_Attr *newAttrNode; - short errCode = IXML_SUCCESS; + int errCode = IXML_SUCCESS; if (element == NULL || name == NULL || value == NULL) { errCode = IXML_INVALID_PARAMETER; @@ -208,7 +208,8 @@ IXML_Attr *ixmlElement_getAttributeNode(IXML_Element *element, const DOMString n attrNode = element->n.firstAttr; while (attrNode != NULL) { - if (strcmp(attrNode->nodeName, name) == 0) { // found it + if (strcmp(attrNode->nodeName, name) == 0) { + /* found it */ break; } else { attrNode = attrNode->nextSibling; @@ -555,8 +556,8 @@ IXML_Attr *ixmlElement_getAttributeNodeNS( while (attrNode != NULL) { if (strcmp(attrNode->localName, localName) == 0 && strcmp(attrNode->namespaceURI, namespaceURI) == 0) { - // found it - break; + /* found it */ + break; } else { attrNode = attrNode->nextSibling; } diff --git a/ixml/src/inc/ixmlmembuf.h b/ixml/src/inc/ixmlmembuf.h index 246f5ac..47fc281 100644 --- a/ixml/src/inc/ixmlmembuf.h +++ b/ixml/src/inc/ixmlmembuf.h @@ -166,7 +166,7 @@ int ixml_membuf_insert( size_t buf_len, /*! [in] The point of insertion relative to the beggining of the * ixml_membuf internal buffer. */ - int index); + size_t index); #endif /* IXML_MEMBUF_H */ diff --git a/ixml/src/ixml.c b/ixml/src/ixml.c index fa0e5aa..8e402b1 100644 --- a/ixml/src/ixml.c +++ b/ixml/src/ixml.c @@ -54,37 +54,29 @@ static void copy_with_escape( /*! [in] The string to copy from. */ const char *p) { - int i; - int plen; + size_t i; + size_t plen; - if (p == NULL) { + if (p == NULL) return; - } - - plen = strlen( p ); - - for (i = 0; i < plen; i++) { + plen = strlen(p); + for (i = 0; i < plen; ++i) { switch (p[i]) { case '<': ixml_membuf_append_str(buf, "<"); break; - case '>': ixml_membuf_append_str(buf, ">"); break; - case '&': ixml_membuf_append_str(buf, "&"); break; - case '\'': ixml_membuf_append_str(buf, "'"); break; - case '\"': ixml_membuf_append_str(buf, """); break; - default: ixml_membuf_append(buf, &p[i]); break; @@ -161,11 +153,11 @@ static void ixmlPrintDomTreeRecursive( } else { ixml_membuf_append_str(buf, ">"); } - // output the children + /* output the children */ ixmlPrintDomTreeRecursive( ixmlNode_getFirstChild(nodeptr), buf); - // Done with children. Output the end tag. + /* Done with children. Output the end tag. */ ixml_membuf_append_str(buf, ""); } - // output the children + /* output the children */ ixmlPrintDomTreeRecursive( ixmlNode_getFirstChild(nodeptr), buf); - // Done with children. Output the end tag. + /* Done with children. Output the end tag. */ ixml_membuf_append_str(buf, "\r\n"); @@ -314,10 +306,10 @@ static void ixmlDomTreetoString( ixml_membuf_append_str(buf, ">"); } - // output the children + /* output the children */ ixmlPrintDomTreeRecursive(ixmlNode_getFirstChild(nodeptr), buf); - // Done with children. Output the end tag. + /* Done with children. Output the end tag. */ ixml_membuf_append_str(buf, ""); diff --git a/ixml/src/ixmlmembuf.c b/ixml/src/ixmlmembuf.c index 42b67e3..a09f5e0 100644 --- a/ixml/src/ixmlmembuf.c +++ b/ixml/src/ixmlmembuf.c @@ -75,10 +75,10 @@ static int ixml_membuf_set_size( diff = new_length - m->length; alloc_len = MAXVAL(m->size_inc, diff) + m->capacity; } else { - // decrease length + /* decrease length */ assert(new_length <= m->length); - // if diff is 0..m->size_inc, don't free + /* if diff is 0..m->size_inc, don't free */ if ((m->capacity - new_length) <= m->size_inc) { return 0; } @@ -135,21 +135,21 @@ int ixml_membuf_assign( assert(m != NULL); - // set value to null + /* set value to null */ if (buf == NULL) { ixml_membuf_destroy(m); return IXML_SUCCESS; } - // alloc mem + /* alloc mem */ return_code = ixml_membuf_set_size(m, buf_len); if (return_code != 0) { return return_code; } - // copy + /* copy */ memcpy(m->buf, buf, buf_len); - // null-terminate + /* null-terminate */ m->buf[buf_len] = 0; m->length = buf_len; @@ -187,13 +187,13 @@ int ixml_membuf_insert( INOUT ixml_membuf *m, IN const void *buf, IN size_t buf_len, - int index) + size_t index) { int return_code = 0; assert(m != NULL); - if (index < 0 || index > (int)m->length) { + if (index > m->length) { return IXML_INDEX_SIZE_ERR; } diff --git a/ixml/src/ixmlparser.c b/ixml/src/ixmlparser.c index 739a49a..2a700ff 100644 --- a/ixml/src/ixmlparser.c +++ b/ixml/src/ixmlparser.c @@ -42,6 +42,7 @@ #include +#include /* for ptrdiff_t */ #include #include /* for free(), malloc() */ #include @@ -96,9 +97,9 @@ typedef char utf8char[8]; * See XML 1.0 (2nd Edition) for more details. */ static char_info_t Letter[] = { - {0x003A, 0x003A}, // character ":" + {0x003A, 0x003A}, /* character ":" */ {0x0041, 0x005A}, - {0x005F, 0x005F}, // character "_" + {0x005F, 0x005F}, /* character "_" */ {0x0061, 0x007A}, {0x00C0, 0x00D6}, {0x00D8, 0x00F6}, {0x00F8, 0x00FF}, {0x0100, 0x0131}, {0x0134, 0x013E}, {0x0141, 0x0148}, {0x014A, 0x017E}, {0x0180, 0x01C3}, {0x01CD, 0x01F0}, {0x01F4, 0x01F5}, {0x01FA, 0x0217}, @@ -244,221 +245,323 @@ static char_info_t NameChar[] = { #define NAMECHARTABLESIZE (sizeof(NameChar)/sizeof(NameChar[0])) -static void Parser_free( - Parser *myParser); -static int Parser_skipDocType( - char **pstr); -static int Parser_skipProlog( - Parser *xmlParser); -static int Parser_skipMisc( - Parser *xmlParser); +/*! + * \brief Frees one ElementStack item. + */ static void Parser_freeElementStackItem( - IXML_ElementStack *pItem); + /*! [in] The element stack item to free. */ + IXML_ElementStack *pItem) +{ + assert( pItem != NULL ); + if( pItem->element != NULL ) { + free( pItem->element ); + pItem->element = NULL; + } + if( pItem->namespaceUri != NULL ) { + free( pItem->namespaceUri ); + pItem->namespaceUri = NULL; + } + if( pItem->prefix != NULL ) { + free( pItem->prefix ); + pItem->prefix = NULL; + } +} + + +/*! + * \brief Frees namespaceURI item. + */ static void Parser_freeNsURI( - IXML_NamespaceURI *pNsURI); + /*! [in] The name space URI item to free. */ + IXML_NamespaceURI *pNsURI) +{ + assert( pNsURI != NULL ); + if( pNsURI->nsURI != NULL ) { + free( pNsURI->nsURI ); + } + if( pNsURI->prefix != NULL ) { + free( pNsURI->prefix ); + } +} -static int Parser_getNextNode( - Parser *myParser, - IXML_Node *newNode, - BOOL *isEnd); -static int Parser_getNextToken( - Parser *myParser); -static int Parser_xmlNamespace( - Parser *myParser, - IXML_Node *newNode); -static BOOL Parser_ElementPrefixDefined( - Parser *myParser, - IXML_Node *newNode, - char **nsURI); -static int Parser_setElementNamespace( - IXML_Element *newElement, - const char *nsURI); -static int Parser_parseDocument( - IXML_Document **retDoc, - Parser *domParser); -static BOOL Parser_hasDefaultNamespace( + +/*! + * \brief Frees all temporary memory allocated by xmlparser. + */ +static void Parser_free( + /*! [in] The XML parser. */ + Parser *xmlParser) +{ + IXML_ElementStack *pElement; + IXML_ElementStack *pNextElement; + IXML_NamespaceURI *pNsURI; + IXML_NamespaceURI *pNextNsURI; + + if( xmlParser == NULL ) { + return; + } + + if( xmlParser->dataBuffer != NULL ) { + free( xmlParser->dataBuffer ); + } + + ixml_membuf_destroy( &( xmlParser->tokenBuf ) ); + ixml_membuf_destroy( &( xmlParser->lastElem ) ); + + pElement = xmlParser->pCurElement; + while( pElement != NULL ) { + Parser_freeElementStackItem( pElement ); + + pNsURI = pElement->pNsURI; + while( pNsURI != NULL ) { + pNextNsURI = pNsURI->nextNsURI; + Parser_freeNsURI( pNsURI ); + free( pNsURI ); + pNsURI = pNextNsURI; + } + + pNextElement = pElement->nextElement; + free( pElement ); + pElement = pNextElement; + } + + free( xmlParser ); +} + + +/*! + * \brief Skips document type declaration + */ +static int Parser_skipDocType( + /*! [in,out] The pointer to the skipped point. */ + char **pstr) +{ + char *pCur = *pstr; + /* default there is no nested < */ + char *pNext = NULL; + int num = 1; + + assert( ( *pstr ) != NULL ); + if( *pstr == NULL ) { + return IXML_FAILED; + } + + while( ( pCur != NULL ) && ( num != 0 ) && ( *pCur != 0 ) ) { + if( *pCur == '<' ) { + num++; + } else if( *pCur == '>' ) { + num--; + } else if( *pCur == '"' ) { + pNext = strchr( pCur + 1, '"' ); + if( pNext == NULL ) { + return IXML_SYNTAX_ERR; + } + + pCur = pNext; + } + + pCur++; + } + + if( num == 0 ) { + *pstr = pCur; + return IXML_SUCCESS; + } else { + return IXML_SYNTAX_ERR; + } +} + + +/*! + * \brief Skips all characters in the string until it finds the skip key. + * Then it skips the skip key and returns. + */ +static int Parser_skipString( + /*! [in,out] The pointer to the skipped point. */ + char **pstrSrc, + /*! [in] The skip key. */ + const char *strSkipKey) +{ + if( !( *pstrSrc ) || !strSkipKey ) { + return IXML_FAILED; + } + + while( ( **pstrSrc ) + && strncmp( *pstrSrc, strSkipKey, + strlen( strSkipKey ) ) != 0 ) { + ( *pstrSrc )++; + } + + if( **pstrSrc == '\0' ) { + return IXML_SYNTAX_ERR; + } + *pstrSrc = *pstrSrc + strlen( strSkipKey ); + + return IXML_SUCCESS; +} + + +/*! + * \brief Skip white spaces. + */ +static void Parser_skipWhiteSpaces( + /*! [in] The XML parser. */ + Parser *xmlParser) +{ + while( ( *( xmlParser->curPtr ) != 0 ) && + ( strchr( WHITESPACE, *( xmlParser->curPtr ) ) != NULL ) ) { + xmlParser->curPtr++; + } +} + + +/*! + * \brief Skips XML declarations. + */ +static int Parser_skipXMLDecl( + /*! [in,out] The XML parser. */ + Parser *xmlParser) +{ + int rc = IXML_FAILED; + + assert( xmlParser ); + if( xmlParser == NULL ) { + return rc; + } + + rc = Parser_skipString( &( xmlParser->curPtr ), END_PI ); + Parser_skipWhiteSpaces( xmlParser ); + return rc; +} + + +/*! + * \brief Skips all characters in the string until it finds the skip key. + * Then it skips the skip key and returns. + */ +static int Parser_skipComment( + /*! [in,out] The pointer to the skipped point. */ + char **pstrSrc) +{ + char *pStrFound = NULL; + + assert( ( *pstrSrc ) != NULL ); + if( *pstrSrc == NULL ) { + return IXML_FAILED; + } + + pStrFound = strstr( *pstrSrc, END_COMMENT ); + if( ( pStrFound != NULL ) && ( pStrFound != *pstrSrc ) && + ( *( pStrFound - 1 ) != '-' ) ) { + *pstrSrc = pStrFound + strlen( END_COMMENT ); + } else { + return IXML_SYNTAX_ERR; + } + + return IXML_SUCCESS; +} + + +/*! + * \brief Skip comment, PI and white space. + */ +static int Parser_skipMisc( + /*! [in] The XML parser. */ + Parser *xmlParser) +{ + int rc = IXML_SUCCESS; + int done = FALSE; + + while( ( done == FALSE ) && ( rc == IXML_SUCCESS ) ) { + if( strncasecmp( xmlParser->curPtr, ( char * )BEGIN_COMMENT, strlen( BEGIN_COMMENT ) ) == 0 ) { + /*