Index: include/reactos/libs/libxml/debugXML.h =================================================================== --- include/reactos/libs/libxml/debugXML.h (revision 58294) +++ include/reactos/libs/libxml/debugXML.h (working copy) @@ -63,7 +63,7 @@ /**************************************************************** * * - * Checking routines * + * Checking routines * * * ****************************************************************/ @@ -73,7 +73,7 @@ /**************************************************************** * * - * XML shell helpers * + * XML shell helpers * * * ****************************************************************/ Index: include/reactos/libs/libxml/dict.h =================================================================== --- include/reactos/libs/libxml/dict.h (revision 58294) +++ include/reactos/libs/libxml/dict.h (working copy) @@ -11,6 +11,7 @@ #ifndef __XML_DICT_H__ #define __XML_DICT_H__ +#include #include #include @@ -25,10 +26,20 @@ typedef xmlDict *xmlDictPtr; /* + * Initializer + */ +XMLPUBFUN int XMLCALL xmlInitializeDict(void); + +/* * Constructor and destructor. */ XMLPUBFUN xmlDictPtr XMLCALL xmlDictCreate (void); +XMLPUBFUN size_t XMLCALL + xmlDictSetLimit (xmlDictPtr dict, + size_t limit); +XMLPUBFUN size_t XMLCALL + xmlDictGetUsage (xmlDictPtr dict); XMLPUBFUN xmlDictPtr XMLCALL xmlDictCreateSub(xmlDictPtr sub); XMLPUBFUN int XMLCALL Index: include/reactos/libs/libxml/encoding.h =================================================================== --- include/reactos/libs/libxml/encoding.h (revision 58294) +++ include/reactos/libs/libxml/encoding.h (working copy) @@ -175,8 +175,8 @@ xmlFindCharEncodingHandler (const char *name); XMLPUBFUN xmlCharEncodingHandlerPtr XMLCALL xmlNewCharEncodingHandler (const char *name, - xmlCharEncodingInputFunc input, - xmlCharEncodingOutputFunc output); + xmlCharEncodingInputFunc input, + xmlCharEncodingOutputFunc output); /* * Interfaces for encoding names and aliases. Index: include/reactos/libs/libxml/hash.h =================================================================== --- include/reactos/libs/libxml/hash.h (revision 58294) +++ include/reactos/libs/libxml/hash.h (working copy) @@ -1,7 +1,7 @@ /* * Summary: Chained hash tables * Description: This module implements the hash table support used in - * various places in the library. + * various places in the library. * * Copy: See Copyright for the status of this software. * Index: include/reactos/libs/libxml/HTMLparser.h =================================================================== --- include/reactos/libs/libxml/HTMLparser.h (revision 58294) +++ include/reactos/libs/libxml/HTMLparser.h (working copy) @@ -184,7 +184,8 @@ HTML_PARSE_NOBLANKS = 1<<8, /* remove blank nodes */ HTML_PARSE_NONET = 1<<11,/* Forbid network access */ HTML_PARSE_NOIMPLIED= 1<<13,/* Do not add implied html/body... elements */ - HTML_PARSE_COMPACT = 1<<16 /* compact small text nodes */ + HTML_PARSE_COMPACT = 1<<16,/* compact small text nodes */ + HTML_PARSE_IGNORE_ENC=1<<21 /* ignore internal document encoding hint */ } htmlParserOption; XMLPUBFUN void XMLCALL Index: include/reactos/libs/libxml/nanoftp.h =================================================================== --- include/reactos/libs/libxml/nanoftp.h (revision 58293) +++ include/reactos/libs/libxml/nanoftp.h (working copy) @@ -31,6 +31,7 @@ * macro used to provide portability of code to windows sockets * the value to be used when the socket is not valid */ +#undef INVALID_SOCKET #define INVALID_SOCKET (-1) #endif Index: include/reactos/libs/libxml/parser.h =================================================================== --- include/reactos/libs/libxml/parser.h (revision 58294) +++ include/reactos/libs/libxml/parser.h (working copy) @@ -125,9 +125,9 @@ XML_PARSER_ENTITY_VALUE, /* within an entity value in a decl */ XML_PARSER_ATTRIBUTE_VALUE, /* within an attribute value */ XML_PARSER_SYSTEM_LITERAL, /* within a SYSTEM value */ - XML_PARSER_EPILOG, /* the Misc* after the last end tag */ + XML_PARSER_EPILOG, /* the Misc* after the last end tag */ XML_PARSER_IGNORE, /* within an IGNORED section */ - XML_PARSER_PUBLIC_LITERAL /* within a PUBLIC value */ + XML_PARSER_PUBLIC_LITERAL /* within a PUBLIC value */ } xmlParserInputState; /** @@ -308,6 +308,8 @@ int nodeInfoNr; /* Depth of the parsing stack */ int nodeInfoMax; /* Max depth of the parsing stack */ xmlParserNodeInfo *nodeInfoTab; /* array of nodeInfos */ + + int input_id; /* we need to label inputs */ }; /** @@ -891,12 +893,12 @@ XMLPUBFUN xmlDocPtr XMLCALL xmlSAXParseMemory (xmlSAXHandlerPtr sax, const char *buffer, - int size, + int size, int recovery); XMLPUBFUN xmlDocPtr XMLCALL xmlSAXParseMemoryWithData (xmlSAXHandlerPtr sax, const char *buffer, - int size, + int size, int recovery, void *data); XMLPUBFUN xmlDocPtr XMLCALL @@ -1105,8 +1107,10 @@ crash if you try to modify the tree) */ XML_PARSE_OLD10 = 1<<17,/* parse using XML-1.0 before update 5 */ XML_PARSE_NOBASEFIX = 1<<18,/* do not fixup XINCLUDE xml:base uris */ - XML_PARSE_HUGE = 1<<19, /* relax any hardcoded limit from the parser */ - XML_PARSE_OLDSAX = 1<<20 /* parse using SAX2 interface from before 2.7.0 */ + XML_PARSE_HUGE = 1<<19,/* relax any hardcoded limit from the parser */ + XML_PARSE_OLDSAX = 1<<20,/* parse using SAX2 interface before 2.7.0 */ + XML_PARSE_IGNORE_ENC= 1<<21,/* ignore internal document encoding hint */ + XML_PARSE_BIG_LINES = 1<<22 /* Store big lines numbers in text PSVI field */ } xmlParserOption; XMLPUBFUN void XMLCALL @@ -1223,6 +1227,7 @@ XML_WITH_DEBUG_RUN = 30, XML_WITH_ZLIB = 31, XML_WITH_ICU = 32, + XML_WITH_LZMA = 33, XML_WITH_NONE = 99999 /* just to be sure of allocation size */ } xmlFeature; Index: include/reactos/libs/libxml/parserInternals.h =================================================================== --- include/reactos/libs/libxml/parserInternals.h (revision 58294) +++ include/reactos/libs/libxml/parserInternals.h (working copy) @@ -1,5 +1,5 @@ /* - * Summary: internals routines exported by the parser. + * Summary: internals routines and limits exported by the parser. * Description: this module exports a number of internal parsing routines * they are not really all intended for applications but * can prove useful doing low level processing. @@ -36,10 +36,43 @@ * Maximum size allowed for a single text node when building a tree. * This is not a limitation of the parser but a safety boundary feature, * use XML_PARSE_HUGE option to override it. + * Introduced in 2.9.0 */ #define XML_MAX_TEXT_LENGTH 10000000 /** + * XML_MAX_NAME_LENGTH: + * + * Maximum size allowed for a markup identitier + * This is not a limitation of the parser but a safety boundary feature, + * use XML_PARSE_HUGE option to override it. + * Note that with the use of parsing dictionaries overriding the limit + * may result in more runtime memory usage in face of "unfriendly' content + * Introduced in 2.9.0 + */ +#define XML_MAX_NAME_LENGTH 50000 + +/** + * XML_MAX_DICTIONARY_LIMIT: + * + * Maximum size allowed by the parser for a dictionary by default + * This is not a limitation of the parser but a safety boundary feature, + * use XML_PARSE_HUGE option to override it. + * Introduced in 2.9.0 + */ +#define XML_MAX_DICTIONARY_LIMIT 10000000 + +/** + * XML_MAX_LOOKUP_LIMIT: + * + * Maximum size allowed by the parser for ahead lookup + * This is an upper boundary enforced by the parser to avoid bad + * behaviour on "unfriendly' content + * Introduced in 2.9.0 + */ +#define XML_MAX_LOOKUP_LIMIT 10000000 + +/** * XML_MAX_NAMELEN: * * Identifiers can be longer, but this will be more costly @@ -57,7 +90,7 @@ /************************************************************************ * * - * UNICODE version of the macros. * + * UNICODE version of the macros. * * * ************************************************************************/ /** @@ -251,7 +284,7 @@ * * Skips the end of line chars. */ -#define SKIP_EOL(p) \ +#define SKIP_EOL(p) \ if (*(p) == 0x13) { p++ ; if (*(p) == 0x10) p++; } \ if (*(p) == 0x10) { p++ ; if (*(p) == 0x13) p++; } @@ -483,7 +516,7 @@ * * Both general and parameter entities need to be substituted. */ -#define XML_SUBSTITUTE_BOTH 3 +#define XML_SUBSTITUTE_BOTH 3 XMLPUBFUN xmlChar * XMLCALL xmlStringDecodeEntities (xmlParserCtxtPtr ctxt, Index: include/reactos/libs/libxml/schematron.h =================================================================== --- include/reactos/libs/libxml/schematron.h (revision 58294) +++ include/reactos/libs/libxml/schematron.h (working copy) @@ -127,7 +127,7 @@ XMLPUBFUN xmlSchematronValidCtxtPtr XMLCALL xmlSchematronNewValidCtxt (xmlSchematronPtr schema, - int options); + int options); XMLPUBFUN void XMLCALL xmlSchematronFreeValidCtxt (xmlSchematronValidCtxtPtr ctxt); XMLPUBFUN int XMLCALL Index: include/reactos/libs/libxml/tree.h =================================================================== --- include/reactos/libs/libxml/tree.h (revision 58294) +++ include/reactos/libs/libxml/tree.h (working copy) @@ -13,6 +13,7 @@ #define __XML_TREE_H__ #include +#include #include #include @@ -74,13 +75,15 @@ XML_BUFFER_ALLOC_DOUBLEIT, /* double each time one need to grow */ XML_BUFFER_ALLOC_EXACT, /* grow only to the minimal size */ XML_BUFFER_ALLOC_IMMUTABLE, /* immutable buffer */ - XML_BUFFER_ALLOC_IO /* special allocation scheme used for I/O */ + XML_BUFFER_ALLOC_IO, /* special allocation scheme used for I/O */ + XML_BUFFER_ALLOC_HYBRID /* exact up to a threshold, and doubleit thereafter */ } xmlBufferAllocationScheme; /** * xmlBuffer: * - * A buffer structure. + * A buffer structure, this old construct is limited to 2GB and + * is being deprecated, use API with xmlBuf instead */ typedef struct _xmlBuffer xmlBuffer; typedef xmlBuffer *xmlBufferPtr; @@ -93,6 +96,41 @@ }; /** + * xmlBuf: + * + * A buffer structure, new one, the actual structure internals are not public + */ + +typedef struct _xmlBuf xmlBuf; + +/** + * xmlBufPtr: + * + * A pointer to a buffer structure, the actual structure internals are not + * public + */ + +typedef xmlBuf *xmlBufPtr; + +/* + * A few public routines for xmlBuf. As those are expected to be used + * mostly internally the bulk of the routines are internal in buf.h + */ +XMLPUBFUN xmlChar* XMLCALL xmlBufContent (const xmlBufPtr buf); +XMLPUBFUN xmlChar* XMLCALL xmlBufEnd (const xmlBufPtr buf); +XMLPUBFUN size_t XMLCALL xmlBufUse (xmlBufPtr buf); +XMLPUBFUN size_t XMLCALL xmlBufShrink (xmlBufPtr buf, size_t len); + +/* + * LIBXML2_NEW_BUFFER: + * + * Macro used to express that the API use the new buffers for + * xmlParserInputBuffer and xmlOutputBuffer. The change was + * introduced in 2.9.0. + */ +#define LIBXML2_NEW_BUFFER + +/** * XML_XML_NAMESPACE: * * This is the namespace for the special xml: prefix predefined in the @@ -647,7 +685,7 @@ int *len); /* - * Handling Buffers. + * Handling Buffers, the old ones see @xmlBuf for the new ones. */ XMLPUBFUN void XMLCALL @@ -694,6 +732,8 @@ xmlBufferEmpty (xmlBufferPtr buf); XMLPUBFUN const xmlChar* XMLCALL xmlBufferContent (const xmlBufferPtr buf); +XMLPUBFUN xmlChar* XMLCALL + xmlBufferDetach (xmlBufferPtr buf); XMLPUBFUN void XMLCALL xmlBufferSetAllocationScheme(xmlBufferPtr buf, xmlBufferAllocationScheme scheme); @@ -1026,9 +1066,14 @@ int len); XMLPUBFUN xmlChar * XMLCALL xmlNodeGetContent (xmlNodePtr cur); + XMLPUBFUN int XMLCALL xmlNodeBufGetContent (xmlBufferPtr buffer, xmlNodePtr cur); +XMLPUBFUN int XMLCALL + xmlBufGetNodeContent (xmlBufPtr buf, + xmlNodePtr cur); + XMLPUBFUN xmlChar * XMLCALL xmlNodeGetLang (xmlNodePtr cur); XMLPUBFUN int XMLCALL @@ -1136,6 +1181,12 @@ xmlSaveFormatFile (const char *filename, xmlDocPtr cur, int format); +XMLPUBFUN size_t XMLCALL + xmlBufNodeDump (xmlBufPtr buf, + xmlDocPtr doc, + xmlNodePtr cur, + int level, + int format); XMLPUBFUN int XMLCALL xmlNodeDump (xmlBufferPtr buf, xmlDocPtr doc, Index: include/reactos/libs/libxml/xmlerror.h =================================================================== --- include/reactos/libs/libxml/xmlerror.h (revision 58293) +++ include/reactos/libs/libxml/xmlerror.h (working copy) @@ -62,7 +62,9 @@ XML_FROM_WRITER, /* The xmlwriter module */ XML_FROM_MODULE, /* The dynamically loaded module module*/ XML_FROM_I18N, /* The module handling character conversion */ - XML_FROM_SCHEMATRONV /* The Schematron validator module */ + XML_FROM_SCHEMATRONV,/* The Schematron validator module */ + XML_FROM_BUFFER, /* The buffers module */ + XML_FROM_URI /* The URI module */ } xmlErrorDomain; /** @@ -205,6 +207,7 @@ XML_WAR_ENTITY_REDEFINED, /* 107 */ XML_ERR_UNKNOWN_VERSION, /* 108 */ XML_ERR_VERSION_MISMATCH, /* 109 */ + XML_ERR_NAME_TOO_LONG, /* 110 */ XML_NS_ERR_XML_NAMESPACE = 200, XML_NS_ERR_UNDEFINED_NAMESPACE, /* 201 */ XML_NS_ERR_QNAME, /* 202 */ @@ -825,11 +828,8 @@ XML_I18N_NO_HANDLER, /* 6001 */ XML_I18N_EXCESS_HANDLER, /* 6002 */ XML_I18N_CONV_FAILED, /* 6003 */ - XML_I18N_NO_OUTPUT /* 6004 */ -#if 0 - XML_CHECK_, /* 5033 */ - XML_CHECK_X /* 503 */ -#endif + XML_I18N_NO_OUTPUT, /* 6004 */ + XML_BUF_OVERFLOW = 7000 } xmlParserErrors; /** Index: include/reactos/libs/libxml/xmlexports.h =================================================================== --- include/reactos/libs/libxml/xmlexports.h (revision 58294) +++ include/reactos/libs/libxml/xmlexports.h (working copy) @@ -115,7 +115,7 @@ */ #if defined(IN_LIBXML) && !defined(LIBXML_STATIC) #define XMLPUBFUN __declspec(dllexport) - #define XMLPUBVAR __declspec(dllexport) + #define XMLPUBVAR __declspec(dllexport) extern #else #define XMLPUBFUN #if !defined(LIBXML_STATIC) Index: include/reactos/libs/libxml/xmlIO.h =================================================================== --- include/reactos/libs/libxml/xmlIO.h (revision 58294) +++ include/reactos/libs/libxml/xmlIO.h (working copy) @@ -129,8 +129,8 @@ xmlCharEncodingHandlerPtr encoder; /* I18N conversions to UTF-8 */ - xmlBufferPtr buffer; /* Local buffer encoded in UTF-8 */ - xmlBufferPtr raw; /* if encoder != NULL buffer for raw input */ + xmlBufPtr buffer; /* Local buffer encoded in UTF-8 */ + xmlBufPtr raw; /* if encoder != NULL buffer for raw input */ int compressed; /* -1=unknown, 0=not compressed, 1=compressed */ int error; unsigned long rawconsumed;/* amount consumed from raw */ @@ -145,8 +145,8 @@ xmlCharEncodingHandlerPtr encoder; /* I18N conversions to UTF-8 */ - xmlBufferPtr buffer; /* Local buffer encoded in UTF-8 or ISOLatin */ - xmlBufferPtr conv; /* if encoder != NULL buffer for output */ + xmlBufPtr buffer; /* Local buffer encoded in UTF-8 or ISOLatin */ + xmlBufPtr conv; /* if encoder != NULL buffer for output */ int written; /* total number of byte written */ int error; }; @@ -245,6 +245,12 @@ void *ioctx, xmlCharEncodingHandlerPtr encoder); +/* Couple of APIs to get the output without digging into the buffers */ +XMLPUBFUN const xmlChar * XMLCALL + xmlOutputBufferGetContent (xmlOutputBufferPtr out); +XMLPUBFUN size_t XMLCALL + xmlOutputBufferGetSize (xmlOutputBufferPtr out); + XMLPUBFUN int XMLCALL xmlOutputBufferWrite (xmlOutputBufferPtr out, int len, @@ -306,24 +312,24 @@ * Default 'file://' protocol callbacks */ XMLPUBFUN int XMLCALL - xmlFileMatch (const char *filename); + xmlFileMatch (const char *filename); XMLPUBFUN void * XMLCALL - xmlFileOpen (const char *filename); + xmlFileOpen (const char *filename); XMLPUBFUN int XMLCALL - xmlFileRead (void * context, + xmlFileRead (void * context, char * buffer, int len); XMLPUBFUN int XMLCALL - xmlFileClose (void * context); + xmlFileClose (void * context); /** * Default 'http://' protocol callbacks */ #ifdef LIBXML_HTTP_ENABLED XMLPUBFUN int XMLCALL - xmlIOHTTPMatch (const char *filename); + xmlIOHTTPMatch (const char *filename); XMLPUBFUN void * XMLCALL - xmlIOHTTPOpen (const char *filename); + xmlIOHTTPOpen (const char *filename); #ifdef LIBXML_OUTPUT_ENABLED XMLPUBFUN void * XMLCALL xmlIOHTTPOpenW (const char * post_uri, @@ -334,7 +340,7 @@ char * buffer, int len); XMLPUBFUN int XMLCALL - xmlIOHTTPClose (void * context); + xmlIOHTTPClose (void * context); #endif /* LIBXML_HTTP_ENABLED */ /** @@ -342,15 +348,15 @@ */ #ifdef LIBXML_FTP_ENABLED XMLPUBFUN int XMLCALL - xmlIOFTPMatch (const char *filename); + xmlIOFTPMatch (const char *filename); XMLPUBFUN void * XMLCALL - xmlIOFTPOpen (const char *filename); + xmlIOFTPOpen (const char *filename); XMLPUBFUN int XMLCALL xmlIOFTPRead (void * context, char * buffer, int len); XMLPUBFUN int XMLCALL - xmlIOFTPClose (void * context); + xmlIOFTPClose (void * context); #endif /* LIBXML_FTP_ENABLED */ #ifdef __cplusplus Index: include/reactos/libs/libxml/xmlreader.h =================================================================== --- include/reactos/libs/libxml/xmlreader.h (revision 58293) +++ include/reactos/libs/libxml/xmlreader.h (working copy) @@ -130,16 +130,16 @@ #ifdef LIBXML_WRITER_ENABLED XMLPUBFUN xmlChar * XMLCALL - xmlTextReaderReadInnerXml (xmlTextReaderPtr reader); + xmlTextReaderReadInnerXml(xmlTextReaderPtr reader); XMLPUBFUN xmlChar * XMLCALL - xmlTextReaderReadOuterXml (xmlTextReaderPtr reader); + xmlTextReaderReadOuterXml(xmlTextReaderPtr reader); #endif XMLPUBFUN xmlChar * XMLCALL - xmlTextReaderReadString (xmlTextReaderPtr reader); + xmlTextReaderReadString (xmlTextReaderPtr reader); XMLPUBFUN int XMLCALL - xmlTextReaderReadAttributeValue (xmlTextReaderPtr reader); + xmlTextReaderReadAttributeValue(xmlTextReaderPtr reader); /* * Attributes of the node @@ -285,6 +285,11 @@ xmlTextReaderRelaxNGValidate(xmlTextReaderPtr reader, const char *rng); XMLPUBFUN int XMLCALL + xmlTextReaderRelaxNGValidateCtxt(xmlTextReaderPtr reader, + xmlRelaxNGValidCtxtPtr ctxt, + int options); + +XMLPUBFUN int XMLCALL xmlTextReaderRelaxNGSetSchema(xmlTextReaderPtr reader, xmlRelaxNGPtr schema); XMLPUBFUN int XMLCALL @@ -392,27 +397,26 @@ * * Signature of an error callback from a reader parser */ -typedef void (XMLCALL *xmlTextReaderErrorFunc)(void *arg, - const char *msg, - xmlParserSeverities severity, - xmlTextReaderLocatorPtr locator); +typedef void (XMLCALL *xmlTextReaderErrorFunc)(void *arg, + const char *msg, + xmlParserSeverities severity, + xmlTextReaderLocatorPtr locator); XMLPUBFUN int XMLCALL - xmlTextReaderLocatorLineNumber(xmlTextReaderLocatorPtr locator); -/*int xmlTextReaderLocatorLinePosition(xmlTextReaderLocatorPtr locator);*/ + xmlTextReaderLocatorLineNumber(xmlTextReaderLocatorPtr locator); XMLPUBFUN xmlChar * XMLCALL - xmlTextReaderLocatorBaseURI (xmlTextReaderLocatorPtr locator); + xmlTextReaderLocatorBaseURI (xmlTextReaderLocatorPtr locator); XMLPUBFUN void XMLCALL - xmlTextReaderSetErrorHandler(xmlTextReaderPtr reader, - xmlTextReaderErrorFunc f, - void *arg); + xmlTextReaderSetErrorHandler(xmlTextReaderPtr reader, + xmlTextReaderErrorFunc f, + void *arg); XMLPUBFUN void XMLCALL - xmlTextReaderSetStructuredErrorHandler(xmlTextReaderPtr reader, - xmlStructuredErrorFunc f, - void *arg); + xmlTextReaderSetStructuredErrorHandler(xmlTextReaderPtr reader, + xmlStructuredErrorFunc f, + void *arg); XMLPUBFUN void XMLCALL - xmlTextReaderGetErrorHandler(xmlTextReaderPtr reader, - xmlTextReaderErrorFunc *f, - void **arg); + xmlTextReaderGetErrorHandler(xmlTextReaderPtr reader, + xmlTextReaderErrorFunc *f, + void **arg); #endif /* LIBXML_READER_ENABLED */ Index: include/reactos/libs/libxml/xmlschemas.h =================================================================== --- include/reactos/libs/libxml/xmlschemas.h (revision 58293) +++ include/reactos/libs/libxml/xmlschemas.h (working copy) @@ -92,7 +92,8 @@ * * Signature of an error callback from an XSD validation */ -typedef void (XMLCDECL *xmlSchemaValidityErrorFunc) (void *ctx, const char *msg, ...) LIBXML_ATTR_FORMAT(2,3); +typedef void (XMLCDECL *xmlSchemaValidityErrorFunc) + (void *ctx, const char *msg, ...) LIBXML_ATTR_FORMAT(2,3); /** * xmlSchemaValidityWarningFunc: @@ -102,7 +103,8 @@ * * Signature of a warning callback from an XSD validation */ -typedef void (XMLCDECL *xmlSchemaValidityWarningFunc) (void *ctx, const char *msg, ...) LIBXML_ATTR_FORMAT(2,3); +typedef void (XMLCDECL *xmlSchemaValidityWarningFunc) + (void *ctx, const char *msg, ...) LIBXML_ATTR_FORMAT(2,3); /** * A schemas validation context @@ -113,6 +115,22 @@ typedef struct _xmlSchemaValidCtxt xmlSchemaValidCtxt; typedef xmlSchemaValidCtxt *xmlSchemaValidCtxtPtr; +/** + * xmlSchemaValidityLocatorFunc: + * @ctx: user provided context + * @file: returned file information + * @line: returned line information + * + * A schemas validation locator, a callback called by the validator. + * This is used when file or node informations are not available + * to find out what file and line number are affected + * + * Returns: 0 in case of success and -1 in case of error + */ + +typedef int (XMLCDECL *xmlSchemaValidityLocatorFunc) (void *ctx, + const char **file, unsigned long *line); + /* * Interfaces for parsing. */ @@ -171,6 +189,9 @@ XMLPUBFUN int XMLCALL xmlSchemaSetValidOptions (xmlSchemaValidCtxtPtr ctxt, int options); +XMLPUBFUN void XMLCALL + xmlSchemaValidateSetFilename(xmlSchemaValidCtxtPtr vctxt, + const char *filename); XMLPUBFUN int XMLCALL xmlSchemaValidCtxtGetOptions(xmlSchemaValidCtxtPtr ctxt); @@ -210,6 +231,13 @@ void **user_data); XMLPUBFUN int XMLCALL xmlSchemaSAXUnplug (xmlSchemaSAXPlugPtr plug); + + +XMLPUBFUN void XMLCALL + xmlSchemaValidateSetLocator (xmlSchemaValidCtxtPtr vctxt, + xmlSchemaValidityLocatorFunc f, + void *ctxt); + #ifdef __cplusplus } #endif Index: include/reactos/libs/libxml/xmlschemastypes.h =================================================================== --- include/reactos/libs/libxml/xmlschemastypes.h (revision 58294) +++ include/reactos/libs/libxml/xmlschemastypes.h (working copy) @@ -31,7 +31,7 @@ } xmlSchemaWhitespaceValueType; XMLPUBFUN void XMLCALL - xmlSchemaInitTypes (void); + xmlSchemaInitTypes (void); XMLPUBFUN void XMLCALL xmlSchemaCleanupTypes (void); XMLPUBFUN xmlSchemaTypePtr XMLCALL Index: include/reactos/libs/libxml/xmlversion.h =================================================================== --- include/reactos/libs/libxml/xmlversion.h (revision 58294) +++ include/reactos/libs/libxml/xmlversion.h (working copy) @@ -29,28 +29,28 @@ * * the version string like "1.2.3" */ -#define LIBXML_DOTTED_VERSION "2.7.8" +#define LIBXML_DOTTED_VERSION "2.9.0" /** * LIBXML_VERSION: * * the version number: 1.2.3 value is 10203 */ -#define LIBXML_VERSION 20708 +#define LIBXML_VERSION 20900 /** * LIBXML_VERSION_STRING: * * the version number string, 1.2.3 value is "10203" */ -#define LIBXML_VERSION_STRING "20708" +#define LIBXML_VERSION_STRING "20900" /** * LIBXML_VERSION_EXTRA: * * extra version information, used to show a CVS compilation */ -#define LIBXML_VERSION_EXTRA "-GITv2.7.7-40-g82143f4" +#define LIBXML_VERSION_EXTRA "-GITv2.9.0-rc2-5-g7651606" /** * LIBXML_TEST_VERSION: @@ -58,7 +58,7 @@ * Macro to check that the libxml version in use is compatible with * the version the software has been compiled against */ -#define LIBXML_TEST_VERSION xmlCheckVersion(20708); +#define LIBXML_TEST_VERSION xmlCheckVersion(20900); #ifndef VMS #if 0 @@ -379,7 +379,7 @@ * * the string suffix used by dynamic modules (usually shared libraries) */ -#define LIBXML_MODULE_EXTENSION ".so" +#define LIBXML_MODULE_EXTENSION ".so" #endif /** @@ -391,6 +391,15 @@ #define LIBXML_ZLIB_ENABLED #endif +/** + * LIBXML_LZMA_ENABLED: + * + * Whether the Lzma support is compiled in + */ +#if 0 +#define LIBXML_LZMA_ENABLED +#endif + #ifdef __GNUC__ #ifdef HAVE_ANSIDECL_H #include @@ -403,7 +412,11 @@ */ #ifndef ATTRIBUTE_UNUSED -#define ATTRIBUTE_UNUSED __attribute__((unused)) +# if ((__GNUC__ > 2) || ((__GNUC__ == 2) && (__GNUC_MINOR__ >= 7))) +# define ATTRIBUTE_UNUSED __attribute__((unused)) +# else +# define ATTRIBUTE_UNUSED +# endif #endif /** Index: include/reactos/libs/libxml/xmlversion.h.in =================================================================== --- include/reactos/libs/libxml/xmlversion.h.in (revision 58294) +++ include/reactos/libs/libxml/xmlversion.h.in (working copy) @@ -379,7 +379,7 @@ * * the string suffix used by dynamic modules (usually shared libraries) */ -#define LIBXML_MODULE_EXTENSION "@MODULE_EXTENSION@" +#define LIBXML_MODULE_EXTENSION "@MODULE_EXTENSION@" #endif /** @@ -391,6 +391,15 @@ #define LIBXML_ZLIB_ENABLED #endif +/** + * LIBXML_LZMA_ENABLED: + * + * Whether the Lzma support is compiled in + */ +#if @WITH_LZMA@ +#define LIBXML_LZMA_ENABLED +#endif + #ifdef __GNUC__ #ifdef HAVE_ANSIDECL_H #include @@ -403,7 +412,11 @@ */ #ifndef ATTRIBUTE_UNUSED -#define ATTRIBUTE_UNUSED __attribute__((unused)) +# if ((__GNUC__ > 2) || ((__GNUC__ == 2) && (__GNUC_MINOR__ >= 7))) +# define ATTRIBUTE_UNUSED __attribute__((unused)) +# else +# define ATTRIBUTE_UNUSED +# endif #endif /** Index: include/reactos/libs/libxml/xmlwriter.h =================================================================== --- include/reactos/libs/libxml/xmlwriter.h (revision 58293) +++ include/reactos/libs/libxml/xmlwriter.h (working copy) @@ -1,4 +1,3 @@ - /* * Summary: text writing API for XML * Description: text writing API for XML @@ -471,6 +470,10 @@ xmlTextWriterSetIndentString(xmlTextWriterPtr writer, const xmlChar * str); + XMLPUBFUN int XMLCALL + xmlTextWriterSetQuoteChar(xmlTextWriterPtr writer, xmlChar quotechar); + + /* * misc */ Index: include/reactos/libs/libxml/xpath.h =================================================================== --- include/reactos/libs/libxml/xpath.h (revision 58294) +++ include/reactos/libs/libxml/xpath.h (working copy) @@ -68,7 +68,9 @@ XPATH_UNDEF_PREFIX_ERROR, XPATH_ENCODING_ERROR, XPATH_INVALID_CHAR_ERROR, - XPATH_INVALID_CTXT + XPATH_INVALID_CTXT, + XPATH_STACK_ERROR, + XPATH_FORBID_VARIABLE_ERROR } xmlXPathError; /* @@ -380,6 +382,8 @@ xmlXPathCompExprPtr comp; /* the precompiled expression */ int xptr; /* it this an XPointer expression */ xmlNodePtr ancestor; /* used for walking preceding axis */ + + int valueFrame; /* used to limit Pop on the stack */ }; /************************************************************************ @@ -417,7 +421,7 @@ * @index is out of range (0 to length-1) */ #define xmlXPathNodeSetItem(ns, index) \ - ((((ns) != NULL) && \ + ((((ns) != NULL) && \ ((index) >= 0) && ((index) < (ns)->nodeNr)) ? \ (ns)->nodeTab[(index)] \ : NULL) @@ -520,7 +524,7 @@ xmlXPathCompile (const xmlChar *str); XMLPUBFUN xmlXPathCompExprPtr XMLCALL xmlXPathCtxtCompile (xmlXPathContextPtr ctxt, - const xmlChar *str); + const xmlChar *str); XMLPUBFUN xmlXPathObjectPtr XMLCALL xmlXPathCompiledEval (xmlXPathCompExprPtr comp, xmlXPathContextPtr ctx); Index: include/reactos/libs/libxml/xpathInternals.h =================================================================== --- include/reactos/libs/libxml/xpathInternals.h (revision 58294) +++ include/reactos/libs/libxml/xpathInternals.h (working copy) @@ -103,13 +103,13 @@ XMLPUBFUN int XMLCALL xmlXPathPopBoolean (xmlXPathParserContextPtr ctxt); XMLPUBFUN double XMLCALL - xmlXPathPopNumber (xmlXPathParserContextPtr ctxt); + xmlXPathPopNumber (xmlXPathParserContextPtr ctxt); XMLPUBFUN xmlChar * XMLCALL - xmlXPathPopString (xmlXPathParserContextPtr ctxt); + xmlXPathPopString (xmlXPathParserContextPtr ctxt); XMLPUBFUN xmlNodeSetPtr XMLCALL - xmlXPathPopNodeSet (xmlXPathParserContextPtr ctxt); + xmlXPathPopNodeSet (xmlXPathParserContextPtr ctxt); XMLPUBFUN void * XMLCALL - xmlXPathPopExternal (xmlXPathParserContextPtr ctxt); + xmlXPathPopExternal (xmlXPathParserContextPtr ctxt); /** * xmlXPathReturnBoolean: @@ -296,7 +296,9 @@ #define CHECK_ARITY(x) \ if (ctxt == NULL) return; \ if (nargs != (x)) \ - XP_ERROR(XPATH_INVALID_ARITY); + XP_ERROR(XPATH_INVALID_ARITY); \ + if (ctxt->valueNr < ctxt->valueFrame + (x)) \ + XP_ERROR(XPATH_STACK_ERROR); /** * CAST_TO_STRING: @@ -471,7 +473,7 @@ */ XMLPUBFUN xmlXPathParserContextPtr XMLCALL xmlXPathNewParserContext (const xmlChar *str, - xmlXPathContextPtr ctxt); + xmlXPathContextPtr ctxt); XMLPUBFUN void XMLCALL xmlXPathFreeParserContext (xmlXPathParserContextPtr ctxt); @@ -480,7 +482,7 @@ valuePop (xmlXPathParserContextPtr ctxt); XMLPUBFUN int XMLCALL valuePush (xmlXPathParserContextPtr ctxt, - xmlXPathObjectPtr value); + xmlXPathObjectPtr value); XMLPUBFUN xmlXPathObjectPtr XMLCALL xmlXPathNewString (const xmlChar *val); @@ -498,13 +500,13 @@ xmlXPathNewNodeSet (xmlNodePtr val); XMLPUBFUN xmlXPathObjectPtr XMLCALL xmlXPathNewValueTree (xmlNodePtr val); -XMLPUBFUN void XMLCALL +XMLPUBFUN int XMLCALL xmlXPathNodeSetAdd (xmlNodeSetPtr cur, xmlNodePtr val); -XMLPUBFUN void XMLCALL +XMLPUBFUN int XMLCALL xmlXPathNodeSetAddUnique (xmlNodeSetPtr cur, xmlNodePtr val); -XMLPUBFUN void XMLCALL +XMLPUBFUN int XMLCALL xmlXPathNodeSetAddNs (xmlNodeSetPtr cur, xmlNodePtr node, xmlNsPtr ns); Index: include/reactos/libs/libxml/xpointer.h =================================================================== --- include/reactos/libs/libxml/xpointer.h (revision 58294) +++ include/reactos/libs/libxml/xpointer.h (working copy) @@ -101,7 +101,7 @@ xmlXPathContextPtr ctx); XMLPUBFUN void XMLCALL xmlXPtrRangeToFunction (xmlXPathParserContextPtr ctxt, - int nargs); + int nargs); XMLPUBFUN xmlNodePtr XMLCALL xmlXPtrBuildNodeList (xmlXPathObjectPtr obj); XMLPUBFUN void XMLCALL Index: lib/3rdparty/libxml2/acconfig.h =================================================================== --- lib/3rdparty/libxml2/acconfig.h (revision 58293) +++ lib/3rdparty/libxml2/acconfig.h (working copy) @@ -1,16 +0,0 @@ -#undef PACKAGE -#undef VERSION -#undef HAVE_LIBZ -#undef HAVE_LIBM -#undef HAVE_ISINF -#undef HAVE_ISNAN -#undef HAVE_LIBHISTORY -#undef HAVE_LIBREADLINE -#undef HAVE_LIBPTHREAD -#undef HAVE_PTHREAD_H - -/* Define if IPV6 support is there */ -#undef SUPPORT_IP6 - -/* Define if getaddrinfo is there */ -#undef HAVE_GETADDRINFO Index: lib/3rdparty/libxml2/buf.c =================================================================== --- lib/3rdparty/libxml2/buf.c (revision 0) +++ lib/3rdparty/libxml2/buf.c (working copy) @@ -0,0 +1,1301 @@ +/* + * buf.c: memory buffers for libxml2 + * + * new buffer structures and entry points to simplify the maintainance + * of libxml2 and ensure we keep good control over memory allocations + * and stay 64 bits clean. + * The new entry point use the xmlBufPtr opaque structure and + * xmlBuf...() counterparts to the old xmlBuf...() functions + * + * See Copyright for the status of this software. + * + * daniel@veillard.com + */ + +#define IN_LIBXML +#include "libxml.h" + +#include /* for memset() only ! */ +#include +#ifdef HAVE_CTYPE_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif + +#include +#include +#include +#include "buf.h" + +#define WITH_BUFFER_COMPAT + +/** + * xmlBuf: + * + * A buffer structure. The base of the structure is somehow compatible + * with struct _xmlBuffer to limit risks on application which accessed + * directly the input->buf->buffer structures. + */ + +struct _xmlBuf { + xmlChar *content; /* The buffer content UTF8 */ + unsigned int compat_use; /* for binary compatibility */ + unsigned int compat_size; /* for binary compatibility */ + xmlBufferAllocationScheme alloc; /* The realloc method */ + xmlChar *contentIO; /* in IO mode we may have a different base */ + size_t use; /* The buffer size used */ + size_t size; /* The buffer size */ + xmlBufferPtr buffer; /* wrapper for an old buffer */ + int error; /* an error code if a failure occured */ +}; + +#ifdef WITH_BUFFER_COMPAT +/* + * Macro for compatibility with xmlBuffer to be used after an xmlBuf + * is updated. This makes sure the compat fields are updated too. + */ +#define UPDATE_COMPAT(buf) \ + if (buf->size < INT_MAX) buf->compat_size = buf->size; \ + else buf->compat_size = INT_MAX; \ + if (buf->use < INT_MAX) buf->compat_use = buf->use; \ + else buf->compat_use = INT_MAX; + +/* + * Macro for compatibility with xmlBuffer to be used in all the xmlBuf + * entry points, it checks that the compat fields have not been modified + * by direct call to xmlBuffer function from code compiled before 2.9.0 . + */ +#define CHECK_COMPAT(buf) \ + if (buf->size != (size_t) buf->compat_size) \ + if (buf->compat_size < INT_MAX) \ + buf->size = buf->compat_size; \ + if (buf->use != (size_t) buf->compat_use) \ + if (buf->compat_use < INT_MAX) \ + buf->use = buf->compat_use; + +#else /* ! WITH_BUFFER_COMPAT */ +#define UPDATE_COMPAT(buf) +#define CHECK_COMPAT(buf) +#endif /* WITH_BUFFER_COMPAT */ + +/** + * xmlBufMemoryError: + * @extra: extra informations + * + * Handle an out of memory condition + * To be improved... + */ +static void +xmlBufMemoryError(xmlBufPtr buf, const char *extra) +{ + __xmlSimpleError(XML_FROM_BUFFER, XML_ERR_NO_MEMORY, NULL, NULL, extra); + if ((buf) && (buf->error == 0)) + buf->error = XML_ERR_NO_MEMORY; +} + +/** + * xmlBufOverflowError: + * @extra: extra informations + * + * Handle a buffer overflow error + * To be improved... + */ +static void +xmlBufOverflowError(xmlBufPtr buf, const char *extra) +{ + __xmlSimpleError(XML_FROM_BUFFER, XML_BUF_OVERFLOW, NULL, NULL, extra); + if ((buf) && (buf->error == 0)) + buf->error = XML_BUF_OVERFLOW; +} + + +/** + * xmlBufCreate: + * + * routine to create an XML buffer. + * returns the new structure. + */ +xmlBufPtr +xmlBufCreate(void) { + xmlBufPtr ret; + + ret = (xmlBufPtr) xmlMalloc(sizeof(xmlBuf)); + if (ret == NULL) { + xmlBufMemoryError(NULL, "creating buffer"); + return(NULL); + } + ret->compat_use = 0; + ret->use = 0; + ret->error = 0; + ret->buffer = NULL; + ret->size = xmlDefaultBufferSize; + ret->compat_size = xmlDefaultBufferSize; + ret->alloc = xmlBufferAllocScheme; + ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar)); + if (ret->content == NULL) { + xmlBufMemoryError(ret, "creating buffer"); + xmlFree(ret); + return(NULL); + } + ret->content[0] = 0; + ret->contentIO = NULL; + return(ret); +} + +/** + * xmlBufCreateSize: + * @size: initial size of buffer + * + * routine to create an XML buffer. + * returns the new structure. + */ +xmlBufPtr +xmlBufCreateSize(size_t size) { + xmlBufPtr ret; + + ret = (xmlBufPtr) xmlMalloc(sizeof(xmlBuf)); + if (ret == NULL) { + xmlBufMemoryError(NULL, "creating buffer"); + return(NULL); + } + ret->compat_use = 0; + ret->use = 0; + ret->error = 0; + ret->buffer = NULL; + ret->alloc = xmlBufferAllocScheme; + ret->size = (size ? size+2 : 0); /* +1 for ending null */ + ret->compat_size = (int) ret->size; + if (ret->size){ + ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar)); + if (ret->content == NULL) { + xmlBufMemoryError(ret, "creating buffer"); + xmlFree(ret); + return(NULL); + } + ret->content[0] = 0; + } else + ret->content = NULL; + ret->contentIO = NULL; + return(ret); +} + +/** + * xmlBufDetach: + * @buf: the buffer + * + * Remove the string contained in a buffer and give it back to the + * caller. The buffer is reset to an empty content. + * This doesn't work with immutable buffers as they can't be reset. + * + * Returns the previous string contained by the buffer. + */ +xmlChar * +xmlBufDetach(xmlBufPtr buf) { + xmlChar *ret; + + if (buf == NULL) + return(NULL); + if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) + return(NULL); + if (buf->buffer != NULL) + return(NULL); + if (buf->error) + return(NULL); + + ret = buf->content; + buf->content = NULL; + buf->size = 0; + buf->use = 0; + buf->compat_use = 0; + buf->compat_size = 0; + + return ret; +} + + +/** + * xmlBufCreateStatic: + * @mem: the memory area + * @size: the size in byte + * + * routine to create an XML buffer from an immutable memory area. + * The area won't be modified nor copied, and is expected to be + * present until the end of the buffer lifetime. + * + * returns the new structure. + */ +xmlBufPtr +xmlBufCreateStatic(void *mem, size_t size) { + xmlBufPtr ret; + + if ((mem == NULL) || (size == 0)) + return(NULL); + + ret = (xmlBufPtr) xmlMalloc(sizeof(xmlBuf)); + if (ret == NULL) { + xmlBufMemoryError(NULL, "creating buffer"); + return(NULL); + } + if (size < INT_MAX) { + ret->compat_use = size; + ret->compat_size = size; + } else { + ret->compat_use = INT_MAX; + ret->compat_size = INT_MAX; + } + ret->use = size; + ret->size = size; + ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE; + ret->content = (xmlChar *) mem; + ret->error = 0; + ret->buffer = NULL; + return(ret); +} + +/** + * xmlBufGetAllocationScheme: + * @buf: the buffer + * + * Get the buffer allocation scheme + * + * Returns the scheme or -1 in case of error + */ +int +xmlBufGetAllocationScheme(xmlBufPtr buf) { + if (buf == NULL) { +#ifdef DEBUG_BUFFER + xmlGenericError(xmlGenericErrorContext, + "xmlBufGetAllocationScheme: buf == NULL\n"); +#endif + return(-1); + } + return(buf->alloc); +} + +/** + * xmlBufSetAllocationScheme: + * @buf: the buffer to tune + * @scheme: allocation scheme to use + * + * Sets the allocation scheme for this buffer + * + * returns 0 in case of success and -1 in case of failure + */ +int +xmlBufSetAllocationScheme(xmlBufPtr buf, + xmlBufferAllocationScheme scheme) { + if ((buf == NULL) || (buf->error != 0)) { +#ifdef DEBUG_BUFFER + xmlGenericError(xmlGenericErrorContext, + "xmlBufSetAllocationScheme: buf == NULL or in error\n"); +#endif + return(-1); + } + if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) || + (buf->alloc == XML_BUFFER_ALLOC_IO)) + return(-1); + if ((scheme == XML_BUFFER_ALLOC_DOUBLEIT) || + (scheme == XML_BUFFER_ALLOC_EXACT) || + (scheme == XML_BUFFER_ALLOC_HYBRID) || + (scheme == XML_BUFFER_ALLOC_IMMUTABLE)) { + buf->alloc = scheme; + if (buf->buffer) + buf->buffer->alloc = scheme; + return(0); + } + /* + * Switching a buffer ALLOC_IO has the side effect of initializing + * the contentIO field with the current content + */ + if (scheme == XML_BUFFER_ALLOC_IO) { + buf->alloc = XML_BUFFER_ALLOC_IO; + buf->contentIO = buf->content; + } + return(-1); +} + +/** + * xmlBufFree: + * @buf: the buffer to free + * + * Frees an XML buffer. It frees both the content and the structure which + * encapsulate it. + */ +void +xmlBufFree(xmlBufPtr buf) { + if (buf == NULL) { +#ifdef DEBUG_BUFFER + xmlGenericError(xmlGenericErrorContext, + "xmlBufFree: buf == NULL\n"); +#endif + return; + } + + if ((buf->alloc == XML_BUFFER_ALLOC_IO) && + (buf->contentIO != NULL)) { + xmlFree(buf->contentIO); + } else if ((buf->content != NULL) && + (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) { + xmlFree(buf->content); + } + xmlFree(buf); +} + +/** + * xmlBufEmpty: + * @buf: the buffer + * + * empty a buffer. + */ +void +xmlBufEmpty(xmlBufPtr buf) { + if ((buf == NULL) || (buf->error != 0)) return; + if (buf->content == NULL) return; + CHECK_COMPAT(buf) + buf->use = 0; + if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) { + buf->content = BAD_CAST ""; + } else if ((buf->alloc == XML_BUFFER_ALLOC_IO) && + (buf->contentIO != NULL)) { + size_t start_buf = buf->content - buf->contentIO; + + buf->size += start_buf; + buf->content = buf->contentIO; + buf->content[0] = 0; + } else { + buf->content[0] = 0; + } + UPDATE_COMPAT(buf) +} + +/** + * xmlBufShrink: + * @buf: the buffer to dump + * @len: the number of xmlChar to remove + * + * Remove the beginning of an XML buffer. + * NOTE that this routine behaviour differs from xmlBufferShrink() + * as it will return 0 on error instead of -1 due to size_t being + * used as the return type. + * + * Returns the number of byte removed or 0 in case of failure + */ +size_t +xmlBufShrink(xmlBufPtr buf, size_t len) { + if ((buf == NULL) || (buf->error != 0)) return(0); + CHECK_COMPAT(buf) + if (len == 0) return(0); + if (len > buf->use) return(0); + + buf->use -= len; + if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) || + ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL))) { + /* + * we just move the content pointer, but also make sure + * the perceived buffer size has shrinked accordingly + */ + buf->content += len; + buf->size -= len; + + /* + * sometimes though it maybe be better to really shrink + * on IO buffers + */ + if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { + size_t start_buf = buf->content - buf->contentIO; + if (start_buf >= buf->size) { + memmove(buf->contentIO, &buf->content[0], buf->use); + buf->content = buf->contentIO; + buf->content[buf->use] = 0; + buf->size += start_buf; + } + } + } else { + memmove(buf->content, &buf->content[len], buf->use); + buf->content[buf->use] = 0; + } + UPDATE_COMPAT(buf) + return(len); +} + +/** + * xmlBufGrowInternal: + * @buf: the buffer + * @len: the minimum free size to allocate + * + * Grow the available space of an XML buffer, @len is the target value + * Error checking should be done on buf->error since using the return + * value doesn't work that well + * + * Returns 0 in case of error or the length made available otherwise + */ +static size_t +xmlBufGrowInternal(xmlBufPtr buf, size_t len) { + size_t size; + xmlChar *newbuf; + + if ((buf == NULL) || (buf->error != 0)) return(0); + CHECK_COMPAT(buf) + + if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0); + if (buf->use + len < buf->size) + return(buf->size - buf->use); + + /* + * Windows has a BIG problem on realloc timing, so we try to double + * the buffer size (if that's enough) (bug 146697) + * Apparently BSD too, and it's probably best for linux too + * On an embedded system this may be something to change + */ +#if 1 + if (buf->size > (size_t) len) + size = buf->size * 2; + else + size = buf->use + len + 100; +#else + size = buf->use + len + 100; +#endif + + if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { + size_t start_buf = buf->content - buf->contentIO; + + newbuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + size); + if (newbuf == NULL) { + xmlBufMemoryError(buf, "growing buffer"); + return(0); + } + buf->contentIO = newbuf; + buf->content = newbuf + start_buf; + } else { + newbuf = (xmlChar *) xmlRealloc(buf->content, size); + if (newbuf == NULL) { + xmlBufMemoryError(buf, "growing buffer"); + return(0); + } + buf->content = newbuf; + } + buf->size = size; + UPDATE_COMPAT(buf) + return(buf->size - buf->use); +} + +/** + * xmlBufGrow: + * @buf: the buffer + * @len: the minimum free size to allocate + * + * Grow the available space of an XML buffer, @len is the target value + * This is been kept compatible with xmlBufferGrow() as much as possible + * + * Returns -1 in case of error or the length made available otherwise + */ +int +xmlBufGrow(xmlBufPtr buf, int len) { + size_t ret; + + if ((buf == NULL) || (len < 0)) return(-1); + if (len == 0) + return(0); + ret = xmlBufGrowInternal(buf, len); + if (buf->error != 0) + return(-1); + return((int) ret); +} + +/** + * xmlBufInflate: + * @buf: the buffer + * @len: the minimum extra free size to allocate + * + * Grow the available space of an XML buffer, adding at least @len bytes + * + * Returns 0 if successful or -1 in case of error + */ +int +xmlBufInflate(xmlBufPtr buf, size_t len) { + if (buf == NULL) return(-1); + xmlBufGrowInternal(buf, len + buf->size); + if (buf->error) + return(-1); + return(0); +} + +/** + * xmlBufDump: + * @file: the file output + * @buf: the buffer to dump + * + * Dumps an XML buffer to a FILE *. + * Returns the number of #xmlChar written + */ +size_t +xmlBufDump(FILE *file, xmlBufPtr buf) { + size_t ret; + + if ((buf == NULL) || (buf->error != 0)) { +#ifdef DEBUG_BUFFER + xmlGenericError(xmlGenericErrorContext, + "xmlBufDump: buf == NULL or in error\n"); +#endif + return(0); + } + if (buf->content == NULL) { +#ifdef DEBUG_BUFFER + xmlGenericError(xmlGenericErrorContext, + "xmlBufDump: buf->content == NULL\n"); +#endif + return(0); + } + CHECK_COMPAT(buf) + if (file == NULL) + file = stdout; + ret = fwrite(buf->content, sizeof(xmlChar), buf->use, file); + return(ret); +} + +/** + * xmlBufContent: + * @buf: the buffer + * + * Function to extract the content of a buffer + * + * Returns the internal content + */ + +xmlChar * +xmlBufContent(const xmlBufPtr buf) +{ + if ((!buf) || (buf->error)) + return NULL; + + return(buf->content); +} + +/** + * xmlBufEnd: + * @buf: the buffer + * + * Function to extract the end of the content of a buffer + * + * Returns the end of the internal content or NULL in case of error + */ + +xmlChar * +xmlBufEnd(const xmlBufPtr buf) +{ + if ((!buf) || (buf->error)) + return NULL; + CHECK_COMPAT(buf) + + return(&buf->content[buf->use]); +} + +/** + * xmlBufAddLen: + * @buf: the buffer + * @len: the size which were added at the end + * + * Sometime data may be added at the end of the buffer without + * using the xmlBuf APIs that is used to expand the used space + * and set the zero terminating at the end of the buffer + * + * Returns -1 in case of error and 0 otherwise + */ +int +xmlBufAddLen(xmlBufPtr buf, size_t len) { + if ((buf == NULL) || (buf->error)) + return(-1); + CHECK_COMPAT(buf) + if (len > (buf->size - buf->use)) + return(-1); + buf->use += len; + UPDATE_COMPAT(buf) + if (buf->size > buf->use) + buf->content[buf->use] = 0; + else + return(-1); + return(0); +} + +/** + * xmlBufErase: + * @buf: the buffer + * @len: the size to erase at the end + * + * Sometime data need to be erased at the end of the buffer + * + * Returns -1 in case of error and 0 otherwise + */ +int +xmlBufErase(xmlBufPtr buf, size_t len) { + if ((buf == NULL) || (buf->error)) + return(-1); + CHECK_COMPAT(buf) + if (len > buf->use) + return(-1); + buf->use -= len; + buf->content[buf->use] = 0; + UPDATE_COMPAT(buf) + return(0); +} + +/** + * xmlBufLength: + * @buf: the buffer + * + * Function to get the length of a buffer + * + * Returns the length of data in the internal content + */ + +size_t +xmlBufLength(const xmlBufPtr buf) +{ + if ((!buf) || (buf->error)) + return 0; + CHECK_COMPAT(buf) + + return(buf->use); +} + +/** + * xmlBufUse: + * @buf: the buffer + * + * Function to get the length of a buffer + * + * Returns the length of data in the internal content + */ + +size_t +xmlBufUse(const xmlBufPtr buf) +{ + if ((!buf) || (buf->error)) + return 0; + CHECK_COMPAT(buf) + + return(buf->use); +} + +/** + * xmlBufAvail: + * @buf: the buffer + * + * Function to find how much free space is allocated but not + * used in the buffer. It does not account for the terminating zero + * usually needed + * + * Returns the amount or 0 if none or an error occured + */ + +size_t +xmlBufAvail(const xmlBufPtr buf) +{ + if ((!buf) || (buf->error)) + return 0; + CHECK_COMPAT(buf) + + return(buf->size - buf->use); +} + +/** + * xmlBufIsEmpty: + * @buf: the buffer + * + * Tell if a buffer is empty + * + * Returns 0 if no, 1 if yes and -1 in case of error + */ +int +xmlBufIsEmpty(const xmlBufPtr buf) +{ + if ((!buf) || (buf->error)) + return(-1); + CHECK_COMPAT(buf) + + return(buf->use == 0); +} + +/** + * xmlBufResize: + * @buf: the buffer to resize + * @size: the desired size + * + * Resize a buffer to accommodate minimum size of @size. + * + * Returns 0 in case of problems, 1 otherwise + */ +int +xmlBufResize(xmlBufPtr buf, size_t size) +{ + unsigned int newSize; + xmlChar* rebuf = NULL; + size_t start_buf; + + if ((buf == NULL) || (buf->error)) + return(0); + CHECK_COMPAT(buf) + + if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0); + + /* Don't resize if we don't have to */ + if (size < buf->size) + return 1; + + /* figure out new size */ + switch (buf->alloc){ + case XML_BUFFER_ALLOC_IO: + case XML_BUFFER_ALLOC_DOUBLEIT: + /*take care of empty case*/ + newSize = (buf->size ? buf->size*2 : size + 10); + while (size > newSize) { + if (newSize > UINT_MAX / 2) { + xmlBufMemoryError(buf, "growing buffer"); + return 0; + } + newSize *= 2; + } + break; + case XML_BUFFER_ALLOC_EXACT: + newSize = size+10; + break; + case XML_BUFFER_ALLOC_HYBRID: + if (buf->use < BASE_BUFFER_SIZE) + newSize = size; + else { + newSize = buf->size * 2; + while (size > newSize) { + if (newSize > UINT_MAX / 2) { + xmlBufMemoryError(buf, "growing buffer"); + return 0; + } + newSize *= 2; + } + } + break; + + default: + newSize = size+10; + break; + } + + if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { + start_buf = buf->content - buf->contentIO; + + if (start_buf > newSize) { + /* move data back to start */ + memmove(buf->contentIO, buf->content, buf->use); + buf->content = buf->contentIO; + buf->content[buf->use] = 0; + buf->size += start_buf; + } else { + rebuf = (xmlChar *) xmlRealloc(buf->contentIO, start_buf + newSize); + if (rebuf == NULL) { + xmlBufMemoryError(buf, "growing buffer"); + return 0; + } + buf->contentIO = rebuf; + buf->content = rebuf + start_buf; + } + } else { + if (buf->content == NULL) { + rebuf = (xmlChar *) xmlMallocAtomic(newSize); + } else if (buf->size - buf->use < 100) { + rebuf = (xmlChar *) xmlRealloc(buf->content, newSize); + } else { + /* + * if we are reallocating a buffer far from being full, it's + * better to make a new allocation and copy only the used range + * and free the old one. + */ + rebuf = (xmlChar *) xmlMallocAtomic(newSize); + if (rebuf != NULL) { + memcpy(rebuf, buf->content, buf->use); + xmlFree(buf->content); + rebuf[buf->use] = 0; + } + } + if (rebuf == NULL) { + xmlBufMemoryError(buf, "growing buffer"); + return 0; + } + buf->content = rebuf; + } + buf->size = newSize; + UPDATE_COMPAT(buf) + + return 1; +} + +/** + * xmlBufAdd: + * @buf: the buffer to dump + * @str: the #xmlChar string + * @len: the number of #xmlChar to add + * + * Add a string range to an XML buffer. if len == -1, the length of + * str is recomputed. + * + * Returns 0 successful, a positive error code number otherwise + * and -1 in case of internal or API error. + */ +int +xmlBufAdd(xmlBufPtr buf, const xmlChar *str, int len) { + unsigned int needSize; + + if ((str == NULL) || (buf == NULL) || (buf->error)) + return -1; + CHECK_COMPAT(buf) + + if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; + if (len < -1) { +#ifdef DEBUG_BUFFER + xmlGenericError(xmlGenericErrorContext, + "xmlBufAdd: len < 0\n"); +#endif + return -1; + } + if (len == 0) return 0; + + if (len < 0) + len = xmlStrlen(str); + + if (len < 0) return -1; + if (len == 0) return 0; + + needSize = buf->use + len + 2; + if (needSize > buf->size){ + if (!xmlBufResize(buf, needSize)){ + xmlBufMemoryError(buf, "growing buffer"); + return XML_ERR_NO_MEMORY; + } + } + + memmove(&buf->content[buf->use], str, len*sizeof(xmlChar)); + buf->use += len; + buf->content[buf->use] = 0; + UPDATE_COMPAT(buf) + return 0; +} + +/** + * xmlBufAddHead: + * @buf: the buffer + * @str: the #xmlChar string + * @len: the number of #xmlChar to add + * + * Add a string range to the beginning of an XML buffer. + * if len == -1, the length of @str is recomputed. + * + * Returns 0 successful, a positive error code number otherwise + * and -1 in case of internal or API error. + */ +int +xmlBufAddHead(xmlBufPtr buf, const xmlChar *str, int len) { + unsigned int needSize; + + if ((buf == NULL) || (buf->error)) + return(-1); + CHECK_COMPAT(buf) + if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; + if (str == NULL) { +#ifdef DEBUG_BUFFER + xmlGenericError(xmlGenericErrorContext, + "xmlBufAddHead: str == NULL\n"); +#endif + return -1; + } + if (len < -1) { +#ifdef DEBUG_BUFFER + xmlGenericError(xmlGenericErrorContext, + "xmlBufAddHead: len < 0\n"); +#endif + return -1; + } + if (len == 0) return 0; + + if (len < 0) + len = xmlStrlen(str); + + if (len <= 0) return -1; + + if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { + size_t start_buf = buf->content - buf->contentIO; + + if (start_buf > (unsigned int) len) { + /* + * We can add it in the space previously shrinked + */ + buf->content -= len; + memmove(&buf->content[0], str, len); + buf->use += len; + buf->size += len; + UPDATE_COMPAT(buf) + return(0); + } + } + needSize = buf->use + len + 2; + if (needSize > buf->size){ + if (!xmlBufResize(buf, needSize)){ + xmlBufMemoryError(buf, "growing buffer"); + return XML_ERR_NO_MEMORY; + } + } + + memmove(&buf->content[len], &buf->content[0], buf->use); + memmove(&buf->content[0], str, len); + buf->use += len; + buf->content[buf->use] = 0; + UPDATE_COMPAT(buf) + return 0; +} + +/** + * xmlBufCat: + * @buf: the buffer to add to + * @str: the #xmlChar string + * + * Append a zero terminated string to an XML buffer. + * + * Returns 0 successful, a positive error code number otherwise + * and -1 in case of internal or API error. + */ +int +xmlBufCat(xmlBufPtr buf, const xmlChar *str) { + if ((buf == NULL) || (buf->error)) + return(-1); + CHECK_COMPAT(buf) + if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; + if (str == NULL) return -1; + return xmlBufAdd(buf, str, -1); +} + +/** + * xmlBufCCat: + * @buf: the buffer to dump + * @str: the C char string + * + * Append a zero terminated C string to an XML buffer. + * + * Returns 0 successful, a positive error code number otherwise + * and -1 in case of internal or API error. + */ +int +xmlBufCCat(xmlBufPtr buf, const char *str) { + const char *cur; + + if ((buf == NULL) || (buf->error)) + return(-1); + CHECK_COMPAT(buf) + if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; + if (str == NULL) { +#ifdef DEBUG_BUFFER + xmlGenericError(xmlGenericErrorContext, + "xmlBufCCat: str == NULL\n"); +#endif + return -1; + } + for (cur = str;*cur != 0;cur++) { + if (buf->use + 10 >= buf->size) { + if (!xmlBufResize(buf, buf->use+10)){ + xmlBufMemoryError(buf, "growing buffer"); + return XML_ERR_NO_MEMORY; + } + } + buf->content[buf->use++] = *cur; + } + buf->content[buf->use] = 0; + UPDATE_COMPAT(buf) + return 0; +} + +/** + * xmlBufWriteCHAR: + * @buf: the XML buffer + * @string: the string to add + * + * routine which manages and grows an output buffer. This one adds + * xmlChars at the end of the buffer. + * + * Returns 0 if successful, a positive error code number otherwise + * and -1 in case of internal or API error. + */ +int +xmlBufWriteCHAR(xmlBufPtr buf, const xmlChar *string) { + if ((buf == NULL) || (buf->error)) + return(-1); + CHECK_COMPAT(buf) + if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) + return(-1); + return(xmlBufCat(buf, string)); +} + +/** + * xmlBufWriteChar: + * @buf: the XML buffer output + * @string: the string to add + * + * routine which manage and grows an output buffer. This one add + * C chars at the end of the array. + * + * Returns 0 if successful, a positive error code number otherwise + * and -1 in case of internal or API error. + */ +int +xmlBufWriteChar(xmlBufPtr buf, const char *string) { + if ((buf == NULL) || (buf->error)) + return(-1); + CHECK_COMPAT(buf) + if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) + return(-1); + return(xmlBufCCat(buf, string)); +} + + +/** + * xmlBufWriteQuotedString: + * @buf: the XML buffer output + * @string: the string to add + * + * routine which manage and grows an output buffer. This one writes + * a quoted or double quoted #xmlChar string, checking first if it holds + * quote or double-quotes internally + * + * Returns 0 if successful, a positive error code number otherwise + * and -1 in case of internal or API error. + */ +int +xmlBufWriteQuotedString(xmlBufPtr buf, const xmlChar *string) { + const xmlChar *cur, *base; + if ((buf == NULL) || (buf->error)) + return(-1); + CHECK_COMPAT(buf) + if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) + return(-1); + if (xmlStrchr(string, '\"')) { + if (xmlStrchr(string, '\'')) { +#ifdef DEBUG_BUFFER + xmlGenericError(xmlGenericErrorContext, + "xmlBufWriteQuotedString: string contains quote and double-quotes !\n"); +#endif + xmlBufCCat(buf, "\""); + base = cur = string; + while(*cur != 0){ + if(*cur == '"'){ + if (base != cur) + xmlBufAdd(buf, base, cur - base); + xmlBufAdd(buf, BAD_CAST """, 6); + cur++; + base = cur; + } + else { + cur++; + } + } + if (base != cur) + xmlBufAdd(buf, base, cur - base); + xmlBufCCat(buf, "\""); + } + else{ + xmlBufCCat(buf, "\'"); + xmlBufCat(buf, string); + xmlBufCCat(buf, "\'"); + } + } else { + xmlBufCCat(buf, "\""); + xmlBufCat(buf, string); + xmlBufCCat(buf, "\""); + } + return(0); +} + +/** + * xmlBufFromBuffer: + * @buffer: incoming old buffer to convert to a new one + * + * Helper routine to switch from the old buffer structures in use + * in various APIs. It creates a wrapper xmlBufPtr which will be + * used for internal processing until the xmlBufBackToBuffer() is + * issued. + * + * Returns a new xmlBufPtr unless the call failed and NULL is returned + */ +xmlBufPtr +xmlBufFromBuffer(xmlBufferPtr buffer) { + xmlBufPtr ret; + + if (buffer == NULL) + return(NULL); + + ret = (xmlBufPtr) xmlMalloc(sizeof(xmlBuf)); + if (ret == NULL) { + xmlBufMemoryError(NULL, "creating buffer"); + return(NULL); + } + ret->use = buffer->use; + ret->size = buffer->size; + ret->compat_use = buffer->use; + ret->compat_size = buffer->size; + ret->error = 0; + ret->buffer = buffer; + ret->alloc = buffer->alloc; + ret->content = buffer->content; + ret->contentIO = buffer->contentIO; + + return(ret); +} + +/** + * xmlBufBackToBuffer: + * @buf: new buffer wrapping the old one + * + * Function to be called once internal processing had been done to + * update back the buffer provided by the user. This can lead to + * a failure in case the size accumulated in the xmlBuf is larger + * than what an xmlBuffer can support on 64 bits (INT_MAX) + * The xmlBufPtr @buf wrapper is deallocated by this call in any case. + * + * Returns the old xmlBufferPtr unless the call failed and NULL is returned + */ +xmlBufferPtr +xmlBufBackToBuffer(xmlBufPtr buf) { + xmlBufferPtr ret; + + if ((buf == NULL) || (buf->error)) + return(NULL); + CHECK_COMPAT(buf) + if (buf->buffer == NULL) { + xmlBufFree(buf); + return(NULL); + } + + ret = buf->buffer; + /* + * What to do in case of error in the buffer ??? + */ + if (buf->use > INT_MAX) { + /* + * Worse case, we really allocated and used more than the + * maximum allowed memory for an xmlBuffer on this architecture. + * Keep the buffer but provide a truncated size value. + */ + xmlBufOverflowError(buf, "Used size too big for xmlBuffer"); + ret->use = INT_MAX; + ret->size = INT_MAX; + } else if (buf->size > INT_MAX) { + /* + * milder case, we allocated more than the maximum allowed memory + * for an xmlBuffer on this architecture, but used less than the + * limit. + * Keep the buffer but provide a truncated size value. + */ + xmlBufOverflowError(buf, "Allocated size too big for xmlBuffer"); + ret->size = INT_MAX; + } + ret->use = (int) buf->use; + ret->size = (int) buf->size; + ret->alloc = buf->alloc; + ret->content = buf->content; + ret->contentIO = buf->contentIO; + xmlFree(buf); + return(ret); +} + +/** + * xmlBufMergeBuffer: + * @buf: an xmlBufPtr + * @buffer: the buffer to consume into @buf + * + * The content of @buffer is appended to @buf and @buffer is freed + * + * Returns -1 in case of error, 0 otherwise, in any case @buffer is freed + */ +int +xmlBufMergeBuffer(xmlBufPtr buf, xmlBufferPtr buffer) { + int ret = 0; + + if ((buf == NULL) || (buf->error)) { + xmlBufferFree(buffer); + return(-1); + } + CHECK_COMPAT(buf) + if ((buffer != NULL) && (buffer->content != NULL) && + (buffer->use > 0)) { + ret = xmlBufAdd(buf, buffer->content, buffer->use); + } + xmlBufferFree(buffer); + return(ret); +} + +/** + * xmlBufResetInput: + * @buf: an xmlBufPtr + * @input: an xmlParserInputPtr + * + * Update the input to use the current set of pointers from the buffer. + * + * Returns -1 in case of error, 0 otherwise + */ +int +xmlBufResetInput(xmlBufPtr buf, xmlParserInputPtr input) { + if ((input == NULL) || (buf == NULL) || (buf->error)) + return(-1); + CHECK_COMPAT(buf) + input->base = input->cur = buf->content; + input->end = &buf->content[buf->use]; + return(0); +} + +/** + * xmlBufGetInputBase: + * @buf: an xmlBufPtr + * @input: an xmlParserInputPtr + * + * Get the base of the @input relative to the beginning of the buffer + * + * Returns the size_t corresponding to the displacement + */ +size_t +xmlBufGetInputBase(xmlBufPtr buf, xmlParserInputPtr input) { + size_t base; + + if ((input == NULL) || (buf == NULL) || (buf->error)) + return(-1); + CHECK_COMPAT(buf) + base = input->base - buf->content; + /* + * We could do some pointer arythmetic checks but that's probably + * sufficient. + */ + if (base > buf->size) { + xmlBufOverflowError(buf, "Input reference outside of the buffer"); + base = 0; + } + return(base); +} + +/** + * xmlBufSetInputBaseCur: + * @buf: an xmlBufPtr + * @input: an xmlParserInputPtr + * @base: the base value relative to the beginning of the buffer + * @cur: the cur value relative to the beginning of the buffer + * + * Update the input to use the base and cur relative to the buffer + * after a possible reallocation of its content + * + * Returns -1 in case of error, 0 otherwise + */ +int +xmlBufSetInputBaseCur(xmlBufPtr buf, xmlParserInputPtr input, + size_t base, size_t cur) { + if ((input == NULL) || (buf == NULL) || (buf->error)) + return(-1); + CHECK_COMPAT(buf) + input->base = &buf->content[base]; + input->cur = input->base + cur; + input->end = &buf->content[buf->use]; + return(0); +} Index: lib/3rdparty/libxml2/buf.h =================================================================== --- lib/3rdparty/libxml2/buf.h (revision 0) +++ lib/3rdparty/libxml2/buf.h (working copy) @@ -0,0 +1,72 @@ +/* + * Summary: Internal Interfaces for memory buffers in libxml2 + * Description: this module describes most of the new xmlBuf buffer + * entry points, those are private routines, with a + * few exceptions exported in tree.h. This was added + * in 2.9.0. + * + * Copy: See Copyright for the status of this software. + * + * Author: Daniel Veillard + */ + +#ifndef __XML_BUF_H__ +#define __XML_BUF_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +xmlBufPtr xmlBufCreate(void); +xmlBufPtr xmlBufCreateSize(size_t size); +xmlBufPtr xmlBufCreateStatic(void *mem, size_t size); + +int xmlBufSetAllocationScheme(xmlBufPtr buf, + xmlBufferAllocationScheme scheme); +int xmlBufGetAllocationScheme(xmlBufPtr buf); + +void xmlBufFree(xmlBufPtr buf); +void xmlBufEmpty(xmlBufPtr buf); + +/* size_t xmlBufShrink(xmlBufPtr buf, size_t len); */ +int xmlBufGrow(xmlBufPtr buf, int len); +int xmlBufInflate(xmlBufPtr buf, size_t len); +int xmlBufResize(xmlBufPtr buf, size_t len); + +int xmlBufAdd(xmlBufPtr buf, const xmlChar *str, int len); +int xmlBufAddHead(xmlBufPtr buf, const xmlChar *str, int len); +int xmlBufCat(xmlBufPtr buf, const xmlChar *str); +int xmlBufCCat(xmlBufPtr buf, const char *str); +int xmlBufWriteCHAR(xmlBufPtr buf, const xmlChar *string); +int xmlBufWriteChar(xmlBufPtr buf, const char *string); +int xmlBufWriteQuotedString(xmlBufPtr buf, const xmlChar *string); + +size_t xmlBufAvail(const xmlBufPtr buf); +size_t xmlBufLength(const xmlBufPtr buf); +/* size_t xmlBufUse(const xmlBufPtr buf); */ +int xmlBufIsEmpty(const xmlBufPtr buf); +int xmlBufAddLen(xmlBufPtr buf, size_t len); +int xmlBufErase(xmlBufPtr buf, size_t len); + +/* const xmlChar * xmlBufContent(const xmlBufPtr buf); */ +/* const xmlChar * xmlBufEnd(const xmlBufPtr buf); */ + +xmlChar * xmlBufDetach(xmlBufPtr buf); + +size_t xmlBufDump(FILE *file, xmlBufPtr buf); + +xmlBufPtr xmlBufFromBuffer(xmlBufferPtr buffer); +xmlBufferPtr xmlBufBackToBuffer(xmlBufPtr buf); +int xmlBufMergeBuffer(xmlBufPtr buf, xmlBufferPtr buffer); + +int xmlBufResetInput(xmlBufPtr buf, xmlParserInputPtr input); +size_t xmlBufGetInputBase(xmlBufPtr buf, xmlParserInputPtr input); +int xmlBufSetInputBaseCur(xmlBufPtr buf, xmlParserInputPtr input, + size_t base, size_t cur); +#ifdef __cplusplus +} +#endif +#endif /* __XML_BUF_H__ */ + Index: lib/3rdparty/libxml2/c14n.c =================================================================== --- lib/3rdparty/libxml2/c14n.c (revision 58294) +++ lib/3rdparty/libxml2/c14n.c (working copy) @@ -27,6 +27,8 @@ #include #include +#include "buf.h" + /************************************************************************ * * * Some declaration better left private ATM * @@ -2033,9 +2035,9 @@ return (-1); } - ret = buf->buffer->use; + ret = xmlBufUse(buf->buffer); if (ret > 0) { - *doc_txt_ptr = xmlStrndup(buf->buffer->content, ret); + *doc_txt_ptr = xmlStrndup(xmlBufContent(buf->buffer), ret); } (void) xmlOutputBufferClose(buf); Index: lib/3rdparty/libxml2/catalog.c =================================================================== --- lib/3rdparty/libxml2/catalog.c (revision 58294) +++ lib/3rdparty/libxml2/catalog.c (working copy) @@ -41,6 +41,8 @@ #include #include +#include "buf.h" + #define MAX_DELEGATE 50 #define MAX_CATAL_DEPTH 50 @@ -912,10 +914,7 @@ inputStream->filename = (char *) xmlCanonicPath((const xmlChar *)filename); inputStream->buf = buf; - inputStream->base = inputStream->buf->buffer->content; - inputStream->cur = inputStream->buf->buffer->content; - inputStream->end = - &inputStream->buf->buffer->content[inputStream->buf->buffer->use]; + xmlBufResetInput(buf->buffer, inputStream); inputPush(ctxt, inputStream); if ((ctxt->directory == NULL) && (directory == NULL)) @@ -1407,8 +1406,6 @@ return(-1); if (catal->URL == NULL) return(-1); - if (catal->children != NULL) - return(-1); /* * lock the whole catalog for modification Index: lib/3rdparty/libxml2/CMakeLists.txt =================================================================== --- lib/3rdparty/libxml2/CMakeLists.txt (revision 58293) +++ lib/3rdparty/libxml2/CMakeLists.txt (working copy) @@ -13,6 +13,7 @@ -D_DLL -D__USE_CRTIMP) list(APPEND SOURCE + buf.c c14n.c catalog.c chvalid.c Index: lib/3rdparty/libxml2/config.h =================================================================== --- lib/3rdparty/libxml2/config.h (revision 58293) +++ lib/3rdparty/libxml2/config.h (working copy) @@ -1,24 +1,8 @@ -/* config.h. Generated by configure. */ +/* config.h. Generated from config.h.in by configure. */ /* config.h.in. Generated from configure.in by autoheader. */ -#define PACKAGE "libxml2" -#define VERSION "2.7.7" -/* #undef HAVE_LIBZ */ -/* #undef HAVE_LIBM */ -/* #undef HAVE_ISINF */ -#define HAVE_ISNAN -/* #undef HAVE_LIBHISTORY */ -/* #undef HAVE_LIBREADLINE */ -/* #undef HAVE_LIBPTHREAD */ -/* #undef HAVE_PTHREAD_H */ -/* Define if IPV6 support is there */ -/* #undef SUPPORT_IP6 */ - -/* Define if getaddrinfo is there */ -/* #undef HAVE_GETADDRINFO */ - /* Define to 1 if you have the header file. */ -/* #undef HAVE_ANSICDECL_H */ +/* #undef HAVE_ANSIDECL_H */ /* Define to 1 if you have the header file. */ /* #undef HAVE_ARPA_INET_H */ @@ -86,11 +70,14 @@ /* Define to 1 if you have the header file. */ #define HAVE_INTTYPES_H 1 +/* Define to 1 if you have the `isascii' function. */ +/* #undef HAVE_ISASCII */ + /* Define if isinf is there */ /* #undef HAVE_ISINF */ /* Define if isnan is there */ -#define HAVE_ISNAN +#define HAVE_ISNAN /**/ /* Define to 1 if you have the `isnand' function. */ /* #undef HAVE_ISNAND */ @@ -98,24 +85,15 @@ /* Define if history library is there (-lhistory) */ /* #undef HAVE_LIBHISTORY */ -/* Define to 1 if you have the `inet' library (-linet). */ -/* #undef HAVE_LIBINET */ +/* Have compression library */ +/* #undef HAVE_LIBLZMA */ -/* Define to 1 if you have the `net' library (-lnet). */ -/* #undef HAVE_LIBNET */ - -/* Define to 1 if you have the `nsl' library (-lnsl). */ -/* #undef HAVE_LIBNSL */ - /* Define if pthread library is there (-lpthread) */ /* #undef HAVE_LIBPTHREAD */ /* Define if readline library is there (-lreadline) */ /* #undef HAVE_LIBREADLINE */ -/* Define to 1 if you have the `socket' library (-lsocket). */ -/* #undef HAVE_LIBSOCKET */ - /* Have compression library */ /* #undef HAVE_LIBZ */ @@ -125,6 +103,9 @@ /* Define to 1 if you have the `localtime' function. */ #define HAVE_LOCALTIME 1 +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LZMA_H */ + /* Define to 1 if you have the header file. */ #define HAVE_MALLOC_H 1 @@ -134,6 +115,17 @@ /* Define to 1 if you have the header file. */ #define HAVE_MEMORY_H 1 +/* Define to 1 if you have the `mmap' function. */ +/* #undef HAVE_MMAP */ + +/* Define to 1 if you have the `munmap' function. */ +/* #undef HAVE_MUNMAP */ + +/* mmap() is no good without munmap() */ +#if defined(HAVE_MMAP) && !defined(HAVE_MUNMAP) +# undef /**/ HAVE_MMAP +#endif + /* Define to 1 if you have the header file. */ /* #undef HAVE_NAN_H */ @@ -146,12 +138,24 @@ /* Define to 1 if you have the header file. */ /* #undef HAVE_NETINET_IN_H */ +/* Define to 1 if you have the header file. */ +/* #undef HAVE_POLL_H */ + /* Define to 1 if you have the `printf' function. */ #define HAVE_PRINTF 1 /* Define if is there */ /* #undef HAVE_PTHREAD_H */ +/* Define to 1 if you have the `putenv' function. */ +/* #undef HAVE_PUTENV */ + +/* Define to 1 if you have the `rand' function. */ +#define HAVE_RAND 1 + +/* Define to 1 if you have the `rand_r' function. */ +/* #undef HAVE_RAND_R */ + /* Define to 1 if you have the header file. */ /* #undef HAVE_RESOLV_H */ @@ -170,6 +174,9 @@ /* Define to 1 if you have the `sprintf' function. */ #define HAVE_SPRINTF 1 +/* Define to 1 if you have the `srand' function. */ +#define HAVE_SRAND 1 + /* Define to 1 if you have the `sscanf' function. */ #define HAVE_SSCANF 1 @@ -232,12 +239,18 @@ /* Define to 1 if you have the header file. */ #define HAVE_SYS_TYPES_H 1 +/* Define to 1 if you have the `time' function. */ +#define HAVE_TIME 1 + /* Define to 1 if you have the header file. */ #define HAVE_TIME_H 1 /* Define to 1 if you have the header file. */ #define HAVE_UNISTD_H 1 +/* Whether va_copy() is available */ +#define HAVE_VA_COPY 1 + /* Define to 1 if you have the `vfprintf' function. */ #define HAVE_VFPRINTF 1 @@ -253,6 +266,16 @@ /* Define to 1 if you have the `_stat' function. */ #define HAVE__STAT 1 +/* Whether __va_copy() is available */ +/* #undef HAVE___VA_COPY */ + +/* Define as const if the declaration of iconv() needs const. */ +/* #undef ICONV_CONST */ + +/* Define to the sub-directory in which libtool stores uninstalled libraries. + */ +/* #undef LT_OBJDIR */ + /* Name of package */ #define PACKAGE "libxml2" @@ -268,12 +291,12 @@ /* Define to the one symbol short name of this package. */ #define PACKAGE_TARNAME "" +/* Define to the home page for this package. */ +#define PACKAGE_URL "" + /* Define to the version of this package. */ #define PACKAGE_VERSION "" -/* Define to 1 if the C compiler supports function prototypes. */ -#define PROTOTYPES 1 - /* Define to 1 if you have the ANSI C header files. */ #define STDC_HEADERS 1 @@ -281,26 +304,26 @@ /* #undef SUPPORT_IP6 */ /* Version number of package */ -#define VERSION "2.7.7" +#define VERSION "2.9.0" /* Determine what socket length (socklen_t) data type is */ #define XML_SOCKLEN_T int +/* Define for Solaris 2.5.1 so the uint32_t typedef from , + , or is not used. If the typedef were allowed, the + #define below would cause a syntax error. */ +/* #undef _UINT32_T */ + /* Using the Win32 Socket implementation */ //#define _WINSOCKAPI_ 1 -/* Define like PROTOTYPES; this can be used by system headers. */ -#define __PROTOTYPES 1 -/* Win32 Std C name mangling work-around */ -#define snprintf _snprintf - /* ss_family is not defined here, use __ss_family instead */ /* #undef ss_family */ -#if !defined(__MINGW32__) || defined(__NO_ISOCEXT) -#define vsnprintf _vsnprintf -#endif +/* Define to the type of an unsigned integer type of width exactly 32 bits if + such a type exists and the standard includes do not define it. */ +/* #undef uint32_t */ #if defined(_MSC_VER) #define GetModuleHandleA GetModuleHandleA_ Index: lib/3rdparty/libxml2/config.h.in =================================================================== --- lib/3rdparty/libxml2/config.h.in (revision 58293) +++ lib/3rdparty/libxml2/config.h.in (working copy) @@ -1,21 +1,5 @@ /* config.h.in. Generated from configure.in by autoheader. */ -#undef PACKAGE -#undef VERSION -#undef HAVE_LIBZ -#undef HAVE_LIBM -#undef HAVE_ISINF -#undef HAVE_ISNAN -#undef HAVE_LIBHISTORY -#undef HAVE_LIBREADLINE -#undef HAVE_LIBPTHREAD -#undef HAVE_PTHREAD_H -/* Define if IPV6 support is there */ -#undef SUPPORT_IP6 - -/* Define if getaddrinfo is there */ -#undef HAVE_GETADDRINFO - /* Define to 1 if you have the header file. */ #undef HAVE_ANSIDECL_H @@ -85,8 +69,8 @@ /* Define to 1 if you have the header file. */ #undef HAVE_INTTYPES_H -/* Define to 1 if you have the header file. */ -#undef HAVE_INTTYPES_H_H +/* Define to 1 if you have the `isascii' function. */ +#undef HAVE_ISASCII /* Define if isinf is there */ #undef HAVE_ISINF @@ -100,6 +84,9 @@ /* Define if history library is there (-lhistory) */ #undef HAVE_LIBHISTORY +/* Have compression library */ +#undef HAVE_LIBLZMA + /* Define if pthread library is there (-lpthread) */ #undef HAVE_LIBPTHREAD @@ -115,6 +102,9 @@ /* Define to 1 if you have the `localtime' function. */ #undef HAVE_LOCALTIME +/* Define to 1 if you have the header file. */ +#undef HAVE_LZMA_H + /* Define to 1 if you have the header file. */ #undef HAVE_MALLOC_H @@ -124,6 +114,17 @@ /* Define to 1 if you have the header file. */ #undef HAVE_MEMORY_H +/* Define to 1 if you have the `mmap' function. */ +#undef HAVE_MMAP + +/* Define to 1 if you have the `munmap' function. */ +#undef HAVE_MUNMAP + +/* mmap() is no good without munmap() */ +#if defined(HAVE_MMAP) && !defined(HAVE_MUNMAP) +# undef /**/ HAVE_MMAP +#endif + /* Define to 1 if you have the header file. */ #undef HAVE_NAN_H @@ -145,6 +146,15 @@ /* Define if is there */ #undef HAVE_PTHREAD_H +/* Define to 1 if you have the `putenv' function. */ +#undef HAVE_PUTENV + +/* Define to 1 if you have the `rand' function. */ +#undef HAVE_RAND + +/* Define to 1 if you have the `rand_r' function. */ +#undef HAVE_RAND_R + /* Define to 1 if you have the header file. */ #undef HAVE_RESOLV_H @@ -163,6 +173,9 @@ /* Define to 1 if you have the `sprintf' function. */ #undef HAVE_SPRINTF +/* Define to 1 if you have the `srand' function. */ +#undef HAVE_SRAND + /* Define to 1 if you have the `sscanf' function. */ #undef HAVE_SSCANF @@ -225,6 +238,9 @@ /* Define to 1 if you have the header file. */ #undef HAVE_SYS_TYPES_H +/* Define to 1 if you have the `time' function. */ +#undef HAVE_TIME + /* Define to 1 if you have the header file. */ #undef HAVE_TIME_H @@ -280,9 +296,6 @@ /* Define to the version of this package. */ #undef PACKAGE_VERSION -/* Define to 1 if the C compiler supports function prototypes. */ -#undef PROTOTYPES - /* Define to 1 if you have the ANSI C header files. */ #undef STDC_HEADERS @@ -295,17 +308,17 @@ /* Determine what socket length (socklen_t) data type is */ #undef XML_SOCKLEN_T +/* Define for Solaris 2.5.1 so the uint32_t typedef from , + , or is not used. If the typedef were allowed, the + #define below would cause a syntax error. */ +#undef _UINT32_T + /* Using the Win32 Socket implementation */ #undef _WINSOCKAPI_ -/* Define like PROTOTYPES; this can be used by system headers. */ -#undef __PROTOTYPES - -/* Win32 Std C name mangling work-around */ -#undef snprintf - /* ss_family is not defined here, use __ss_family instead */ #undef ss_family -/* Win32 Std C name mangling work-around */ -#undef vsnprintf +/* Define to the type of an unsigned integer type of width exactly 32 bits if + such a type exists and the standard includes do not define it. */ +#undef uint32_t Index: lib/3rdparty/libxml2/debugXML.c =================================================================== --- lib/3rdparty/libxml2/debugXML.c (revision 58294) +++ lib/3rdparty/libxml2/debugXML.c (working copy) @@ -1724,8 +1724,11 @@ switch (node->type) { case XML_ELEMENT_NODE: - if (node->name != NULL) + if (node->name != NULL) { + if ((node->ns != NULL) && (node->ns->prefix != NULL)) + fprintf(output, "%s:", node->ns->prefix); fprintf(output, "%s", (const char *) node->name); + } break; case XML_ATTRIBUTE_NODE: if (node->name != NULL) @@ -2696,6 +2699,8 @@ } else if (node->type == XML_ELEMENT_NODE) { for (i = 0; i < indent; i++) fprintf(ctxt->output, " "); + if ((node->ns) && (node->ns->prefix)) + fprintf(ctxt->output, "%s:", node->ns->prefix); fprintf(ctxt->output, "%s\n", node->name); } else { } @@ -2837,6 +2842,10 @@ while (1) { if (ctxt->node == (xmlNodePtr) ctxt->doc) snprintf(prompt, sizeof(prompt), "%s > ", "/"); + else if ((ctxt->node != NULL) && (ctxt->node->name) && + (ctxt->node->ns) && (ctxt->node->ns->prefix)) + snprintf(prompt, sizeof(prompt), "%s:%s > ", + (ctxt->node->ns->prefix), ctxt->node->name); else if ((ctxt->node != NULL) && (ctxt->node->name)) snprintf(prompt, sizeof(prompt), "%s > ", ctxt->node->name); else @@ -2911,6 +2920,7 @@ fprintf(ctxt->output, "\t the default namespace if any uses 'defaultns' prefix\n"); #endif /* LIBXML_XPATH_ENABLED */ fprintf(ctxt->output, "\tpwd display current working directory\n"); + fprintf(ctxt->output, "\twhereis display absolute path of [path] or current working directory\n"); fprintf(ctxt->output, "\tquit leave shell\n"); #ifdef LIBXML_OUTPUT_ENABLED fprintf(ctxt->output, "\tsave [name] save this document to name or the original name\n"); @@ -2960,7 +2970,79 @@ if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL)) fprintf(ctxt->output, "%s\n", dir); } else if (!strcmp(command, "du")) { - xmlShellDu(ctxt, NULL, ctxt->node, NULL); + if (arg[0] == 0) { + xmlShellDu(ctxt, NULL, ctxt->node, NULL); + } else { + ctxt->pctxt->node = ctxt->node; +#ifdef LIBXML_XPATH_ENABLED + ctxt->pctxt->node = ctxt->node; + list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt); +#else + list = NULL; +#endif /* LIBXML_XPATH_ENABLED */ + if (list != NULL) { + switch (list->type) { + case XPATH_UNDEFINED: + xmlGenericError(xmlGenericErrorContext, + "%s: no such node\n", arg); + break; + case XPATH_NODESET:{ + int indx; + + if (list->nodesetval == NULL) + break; + + for (indx = 0; + indx < list->nodesetval->nodeNr; + indx++) + xmlShellDu(ctxt, NULL, + list->nodesetval-> + nodeTab[indx], NULL); + break; + } + case XPATH_BOOLEAN: + xmlGenericError(xmlGenericErrorContext, + "%s is a Boolean\n", arg); + break; + case XPATH_NUMBER: + xmlGenericError(xmlGenericErrorContext, + "%s is a number\n", arg); + break; + case XPATH_STRING: + xmlGenericError(xmlGenericErrorContext, + "%s is a string\n", arg); + break; + case XPATH_POINT: + xmlGenericError(xmlGenericErrorContext, + "%s is a point\n", arg); + break; + case XPATH_RANGE: + xmlGenericError(xmlGenericErrorContext, + "%s is a range\n", arg); + break; + case XPATH_LOCATIONSET: + xmlGenericError(xmlGenericErrorContext, + "%s is a range\n", arg); + break; + case XPATH_USERS: + xmlGenericError(xmlGenericErrorContext, + "%s is user-defined\n", arg); + break; + case XPATH_XSLT_TREE: + xmlGenericError(xmlGenericErrorContext, + "%s is an XSLT value tree\n", + arg); + break; + } +#ifdef LIBXML_XPATH_ENABLED + xmlXPathFreeObject(list); +#endif + } else { + xmlGenericError(xmlGenericErrorContext, + "%s: no such node\n", arg); + } + ctxt->pctxt->node = NULL; + } } else if (!strcmp(command, "base")) { xmlShellBase(ctxt, NULL, ctxt->node, NULL); } else if (!strcmp(command, "set")) { @@ -3078,6 +3160,83 @@ } ctxt->pctxt->node = NULL; } + } else if (!strcmp(command, "whereis")) { + char dir[500]; + + if (arg[0] == 0) { + if (!xmlShellPwd(ctxt, dir, ctxt->node, NULL)) + fprintf(ctxt->output, "%s\n", dir); + } else { + ctxt->pctxt->node = ctxt->node; +#ifdef LIBXML_XPATH_ENABLED + list = xmlXPathEval((xmlChar *) arg, ctxt->pctxt); +#else + list = NULL; +#endif /* LIBXML_XPATH_ENABLED */ + if (list != NULL) { + switch (list->type) { + case XPATH_UNDEFINED: + xmlGenericError(xmlGenericErrorContext, + "%s: no such node\n", arg); + break; + case XPATH_NODESET:{ + int indx; + + if (list->nodesetval == NULL) + break; + + for (indx = 0; + indx < list->nodesetval->nodeNr; + indx++) { + if (!xmlShellPwd(ctxt, dir, list->nodesetval-> + nodeTab[indx], NULL)) + fprintf(ctxt->output, "%s\n", dir); + } + break; + } + case XPATH_BOOLEAN: + xmlGenericError(xmlGenericErrorContext, + "%s is a Boolean\n", arg); + break; + case XPATH_NUMBER: + xmlGenericError(xmlGenericErrorContext, + "%s is a number\n", arg); + break; + case XPATH_STRING: + xmlGenericError(xmlGenericErrorContext, + "%s is a string\n", arg); + break; + case XPATH_POINT: + xmlGenericError(xmlGenericErrorContext, + "%s is a point\n", arg); + break; + case XPATH_RANGE: + xmlGenericError(xmlGenericErrorContext, + "%s is a range\n", arg); + break; + case XPATH_LOCATIONSET: + xmlGenericError(xmlGenericErrorContext, + "%s is a range\n", arg); + break; + case XPATH_USERS: + xmlGenericError(xmlGenericErrorContext, + "%s is user-defined\n", arg); + break; + case XPATH_XSLT_TREE: + xmlGenericError(xmlGenericErrorContext, + "%s is an XSLT value tree\n", + arg); + break; + } +#ifdef LIBXML_XPATH_ENABLED + xmlXPathFreeObject(list); +#endif + } else { + xmlGenericError(xmlGenericErrorContext, + "%s: no such node\n", arg); + } + ctxt->pctxt->node = NULL; + } } else if (!strcmp(command, "cd")) { if (arg[0] == 0) { ctxt->node = (xmlNodePtr) ctxt->doc; Index: lib/3rdparty/libxml2/dict.c =================================================================== --- lib/3rdparty/libxml2/dict.c (revision 58294) +++ lib/3rdparty/libxml2/dict.c (working copy) @@ -2,7 +2,7 @@ * dict.c: dictionary of reusable strings, just used to avoid allocation * and freeing operations. * - * Copyright (C) 2003 Daniel Veillard. + * Copyright (C) 2003-2012 Daniel Veillard. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -19,6 +19,29 @@ #define IN_LIBXML #include "libxml.h" +#include +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_TIME_H +#include +#endif + +/* + * Following http://www.ocert.org/advisories/ocert-2011-003.html + * it seems that having hash randomization might be a good idea + * when using XML with untrusted data + * Note1: that it works correctly only if compiled with WITH_BIG_KEY + * which is the default. + * Note2: the fast function used for a small dict won't protect very + * well but since the attack is based on growing a very big hash + * list we will use the BigKey algo as soon as the hash size grows + * over MIN_DICT_SIZE so this actually works + */ +#if defined(HAVE_RAND) && defined(HAVE_SRAND) && defined(HAVE_TIME) +#define DICT_RANDOMIZATION +#endif + #include #ifdef HAVE_STDINT_H #include @@ -44,23 +67,23 @@ #define WITH_BIG_KEY #ifdef WITH_BIG_KEY -#define xmlDictComputeKey(dict, name, len) \ - (((dict)->size == MIN_DICT_SIZE) ? \ - xmlDictComputeFastKey(name, len) : \ - xmlDictComputeBigKey(name, len)) +#define xmlDictComputeKey(dict, name, len) \ + (((dict)->size == MIN_DICT_SIZE) ? \ + xmlDictComputeFastKey(name, len, (dict)->seed) : \ + xmlDictComputeBigKey(name, len, (dict)->seed)) -#define xmlDictComputeQKey(dict, prefix, plen, name, len) \ - (((prefix) == NULL) ? \ - (xmlDictComputeKey(dict, name, len)) : \ - (((dict)->size == MIN_DICT_SIZE) ? \ - xmlDictComputeFastQKey(prefix, plen, name, len) : \ - xmlDictComputeBigQKey(prefix, plen, name, len))) +#define xmlDictComputeQKey(dict, prefix, plen, name, len) \ + (((prefix) == NULL) ? \ + (xmlDictComputeKey(dict, name, len)) : \ + (((dict)->size == MIN_DICT_SIZE) ? \ + xmlDictComputeFastQKey(prefix, plen, name, len, (dict)->seed) : \ + xmlDictComputeBigQKey(prefix, plen, name, len, (dict)->seed))) #else /* !WITH_BIG_KEY */ -#define xmlDictComputeKey(dict, name, len) \ - xmlDictComputeFastKey(name, len) -#define xmlDictComputeQKey(dict, prefix, plen, name, len) \ - xmlDictComputeFastQKey(prefix, plen, name, len) +#define xmlDictComputeKey(dict, name, len) \ + xmlDictComputeFastKey(name, len, (dict)->seed) +#define xmlDictComputeQKey(dict, prefix, plen, name, len) \ + xmlDictComputeFastQKey(prefix, plen, name, len, (dict)->seed) #endif /* WITH_BIG_KEY */ /* @@ -71,7 +94,7 @@ struct _xmlDictEntry { struct _xmlDictEntry *next; const xmlChar *name; - int len; + unsigned int len; int valid; unsigned long okey; }; @@ -82,8 +105,8 @@ xmlDictStringsPtr next; xmlChar *free; xmlChar *end; - int size; - int nbStrings; + size_t size; + size_t nbStrings; xmlChar array[1]; }; /* @@ -93,11 +116,15 @@ int ref_counter; struct _xmlDictEntry *dict; - int size; - int nbElems; + size_t size; + unsigned int nbElems; xmlDictStringsPtr strings; struct _xmlDict *subdict; + /* used for randomization */ + int seed; + /* used to impose a limit on size */ + size_t limit; }; /* @@ -111,28 +138,69 @@ */ static int xmlDictInitialized = 0; +#ifdef DICT_RANDOMIZATION +#ifdef HAVE_RAND_R +/* + * Internal data for random function, protected by xmlDictMutex + */ +unsigned int rand_seed = 0; +#endif +#endif + /** * xmlInitializeDict: * * Do the dictionary mutex initialization. * this function is not thread safe, initialization should * preferably be done once at startup + * + * Returns 0 if initialization was already done, and 1 if that + * call led to the initialization */ -static int xmlInitializeDict(void) { +int xmlInitializeDict(void) { if (xmlDictInitialized) return(1); if ((xmlDictMutex = xmlNewRMutex()) == NULL) return(0); + xmlRMutexLock(xmlDictMutex); +#ifdef DICT_RANDOMIZATION +#ifdef HAVE_RAND_R + rand_seed = time(NULL); + rand_r(& rand_seed); +#else + srand(time(NULL)); +#endif +#endif xmlDictInitialized = 1; + xmlRMutexUnlock(xmlDictMutex); return(1); } +#ifdef DICT_RANDOMIZATION +int __xmlRandom(void) { + int ret; + + if (xmlDictInitialized == 0) + xmlInitializeDict(); + + xmlRMutexLock(xmlDictMutex); +#ifdef HAVE_RAND_R + ret = rand_r(& rand_seed); +#else + ret = rand(); +#endif + xmlRMutexUnlock(xmlDictMutex); + return(ret); +} +#endif + /** * xmlDictCleanup: * - * Free the dictionary mutex. + * Free the dictionary mutex. Do not call unless sure the library + * is not in use anymore ! */ void xmlDictCleanup(void) { @@ -148,17 +216,18 @@ * xmlDictAddString: * @dict: the dictionnary * @name: the name of the userdata - * @len: the length of the name, if -1 it is recomputed + * @len: the length of the name * * Add the string to the array[s] * * Returns the pointer of the local string, or NULL in case of error. */ static const xmlChar * -xmlDictAddString(xmlDictPtr dict, const xmlChar *name, int namelen) { +xmlDictAddString(xmlDictPtr dict, const xmlChar *name, unsigned int namelen) { xmlDictStringsPtr pool; const xmlChar *ret; - int size = 0; /* + sizeof(_xmlDictStrings) == 1024 */ + size_t size = 0; /* + sizeof(_xmlDictStrings) == 1024 */ + size_t limit = 0; #ifdef DICT_DEBUG_PATTERNS fprintf(stderr, "-"); @@ -168,12 +237,17 @@ if (pool->end - pool->free > namelen) goto found_pool; if (pool->size > size) size = pool->size; + limit += pool->size; pool = pool->next; } /* * Not found, need to allocate */ if (pool == NULL) { + if ((dict->limit > 0) && (limit > dict->limit)) { + return(NULL); + } + if (size == 0) size = 1000; else size *= 4; /* exponential growth */ if (size < 4 * namelen) @@ -206,19 +280,20 @@ * @prefix: the prefix of the userdata * @plen: the prefix length * @name: the name of the userdata - * @len: the length of the name, if -1 it is recomputed + * @len: the length of the name * * Add the QName to the array[s] * * Returns the pointer of the local string, or NULL in case of error. */ static const xmlChar * -xmlDictAddQString(xmlDictPtr dict, const xmlChar *prefix, int plen, - const xmlChar *name, int namelen) +xmlDictAddQString(xmlDictPtr dict, const xmlChar *prefix, unsigned int plen, + const xmlChar *name, unsigned int namelen) { xmlDictStringsPtr pool; const xmlChar *ret; - int size = 0; /* + sizeof(_xmlDictStrings) == 1024 */ + size_t size = 0; /* + sizeof(_xmlDictStrings) == 1024 */ + size_t limit = 0; if (prefix == NULL) return(xmlDictAddString(dict, name, namelen)); @@ -230,12 +305,17 @@ if (pool->end - pool->free > namelen + plen + 1) goto found_pool; if (pool->size > size) size = pool->size; + limit += pool->size; pool = pool->next; } /* * Not found, need to allocate */ if (pool == NULL) { + if ((dict->limit > 0) && (limit > dict->limit)) { + return(NULL); + } + if (size == 0) size = 1000; else size *= 4; /* exponential growth */ if (size < 4 * (namelen + plen + 1)) @@ -277,13 +357,13 @@ */ static uint32_t -xmlDictComputeBigKey(const xmlChar* data, int namelen) { +xmlDictComputeBigKey(const xmlChar* data, int namelen, int seed) { uint32_t hash; int i; if (namelen <= 0 || data == NULL) return(0); - hash = 0; + hash = seed; for (i = 0;i < namelen; i++) { hash += data[i]; @@ -310,12 +390,12 @@ */ static unsigned long xmlDictComputeBigQKey(const xmlChar *prefix, int plen, - const xmlChar *name, int len) + const xmlChar *name, int len, int seed) { uint32_t hash; int i; - hash = 0; + hash = seed; for (i = 0;i < plen; i++) { hash += prefix[i]; @@ -346,8 +426,8 @@ * for low hash table fill. */ static unsigned long -xmlDictComputeFastKey(const xmlChar *name, int namelen) { - unsigned long value = 0L; +xmlDictComputeFastKey(const xmlChar *name, int namelen, int seed) { + unsigned long value = seed; if (name == NULL) return(0); value = *name; @@ -381,9 +461,9 @@ */ static unsigned long xmlDictComputeFastQKey(const xmlChar *prefix, int plen, - const xmlChar *name, int len) + const xmlChar *name, int len, int seed) { - unsigned long value = 0L; + unsigned long value = (unsigned long) seed; if (plen == 0) value += 30 * (unsigned long) ':'; @@ -452,6 +532,7 @@ dict = xmlMalloc(sizeof(xmlDict)); if (dict) { dict->ref_counter = 1; + dict->limit = 0; dict->size = MIN_DICT_SIZE; dict->nbElems = 0; @@ -460,6 +541,11 @@ dict->subdict = NULL; if (dict->dict) { memset(dict->dict, 0, MIN_DICT_SIZE * sizeof(xmlDictEntry)); +#ifdef DICT_RANDOMIZATION + dict->seed = __xmlRandom(); +#else + dict->seed = 0; +#endif return(dict); } xmlFree(dict); @@ -486,6 +572,7 @@ #ifdef DICT_DEBUG_PATTERNS fprintf(stderr, "R"); #endif + dict->seed = sub->seed; dict->subdict = sub; xmlDictReference(dict->subdict); } @@ -523,9 +610,9 @@ * Returns 0 in case of success, -1 in case of failure */ static int -xmlDictGrow(xmlDictPtr dict, int size) { +xmlDictGrow(xmlDictPtr dict, size_t size) { unsigned long key, okey; - int oldsize, i; + size_t oldsize, i; xmlDictEntryPtr iter, next; struct _xmlDictEntry *olddict; #ifdef DEBUG_GROW @@ -642,7 +729,7 @@ #ifdef DEBUG_GROW xmlGenericError(xmlGenericErrorContext, - "xmlDictGrow : from %d to %d, %d elems\n", oldsize, size, nbElem); + "xmlDictGrow : from %lu to %lu, %u elems\n", oldsize, size, nbElem); #endif return(ret); @@ -657,7 +744,7 @@ */ void xmlDictFree(xmlDictPtr dict) { - int i; + size_t i; xmlDictEntryPtr iter; xmlDictEntryPtr next; int inside_dict = 0; @@ -726,17 +813,24 @@ xmlDictEntryPtr entry; xmlDictEntryPtr insert; const xmlChar *ret; + unsigned int l; if ((dict == NULL) || (name == NULL)) return(NULL); if (len < 0) - len = strlen((const char *) name); + l = strlen((const char *) name); + else + l = len; + if (((dict->limit > 0) && (l >= dict->limit)) || + (l > INT_MAX / 2)) + return(NULL); + /* * Check for duplicate and insertion location. */ - okey = xmlDictComputeKey(dict, name, len); + okey = xmlDictComputeKey(dict, name, l); key = okey % dict->size; if (dict->dict[key].valid == 0) { insert = NULL; @@ -744,25 +838,25 @@ for (insert = &(dict->dict[key]); insert->next != NULL; insert = insert->next) { #ifdef __GNUC__ - if ((insert->okey == okey) && (insert->len == len)) { - if (!memcmp(insert->name, name, len)) + if ((insert->okey == okey) && (insert->len == l)) { + if (!memcmp(insert->name, name, l)) return(insert->name); } #else - if ((insert->okey == okey) && (insert->len == len) && - (!xmlStrncmp(insert->name, name, len))) + if ((insert->okey == okey) && (insert->len == l) && + (!xmlStrncmp(insert->name, name, l))) return(insert->name); #endif nbi++; } #ifdef __GNUC__ - if ((insert->okey == okey) && (insert->len == len)) { - if (!memcmp(insert->name, name, len)) + if ((insert->okey == okey) && (insert->len == l)) { + if (!memcmp(insert->name, name, l)) return(insert->name); } #else - if ((insert->okey == okey) && (insert->len == len) && - (!xmlStrncmp(insert->name, name, len))) + if ((insert->okey == okey) && (insert->len == l) && + (!xmlStrncmp(insert->name, name, l))) return(insert->name); #endif } @@ -775,7 +869,7 @@ (dict->subdict->size != MIN_DICT_SIZE)) || ((dict->size != MIN_DICT_SIZE) && (dict->subdict->size == MIN_DICT_SIZE))) - skey = xmlDictComputeKey(dict->subdict, name, len); + skey = xmlDictComputeKey(dict->subdict, name, l); else skey = okey; @@ -786,32 +880,32 @@ for (tmp = &(dict->subdict->dict[key]); tmp->next != NULL; tmp = tmp->next) { #ifdef __GNUC__ - if ((tmp->okey == skey) && (tmp->len == len)) { - if (!memcmp(tmp->name, name, len)) + if ((tmp->okey == skey) && (tmp->len == l)) { + if (!memcmp(tmp->name, name, l)) return(tmp->name); } #else - if ((tmp->okey == skey) && (tmp->len == len) && - (!xmlStrncmp(tmp->name, name, len))) + if ((tmp->okey == skey) && (tmp->len == l) && + (!xmlStrncmp(tmp->name, name, l))) return(tmp->name); #endif nbi++; } #ifdef __GNUC__ - if ((tmp->okey == skey) && (tmp->len == len)) { - if (!memcmp(tmp->name, name, len)) + if ((tmp->okey == skey) && (tmp->len == l)) { + if (!memcmp(tmp->name, name, l)) return(tmp->name); } #else - if ((tmp->okey == skey) && (tmp->len == len) && - (!xmlStrncmp(tmp->name, name, len))) + if ((tmp->okey == skey) && (tmp->len == l) && + (!xmlStrncmp(tmp->name, name, l))) return(tmp->name); #endif } key = okey % dict->size; } - ret = xmlDictAddString(dict, name, len); + ret = xmlDictAddString(dict, name, l); if (ret == NULL) return(NULL); if (insert == NULL) { @@ -822,7 +916,7 @@ return(NULL); } entry->name = ret; - entry->len = len; + entry->len = l; entry->next = NULL; entry->valid = 1; entry->okey = okey; @@ -857,17 +951,23 @@ xmlDictExists(xmlDictPtr dict, const xmlChar *name, int len) { unsigned long key, okey, nbi = 0; xmlDictEntryPtr insert; + unsigned int l; if ((dict == NULL) || (name == NULL)) return(NULL); if (len < 0) - len = strlen((const char *) name); + l = strlen((const char *) name); + else + l = len; + if (((dict->limit > 0) && (l >= dict->limit)) || + (l > INT_MAX / 2)) + return(NULL); /* * Check for duplicate and insertion location. */ - okey = xmlDictComputeKey(dict, name, len); + okey = xmlDictComputeKey(dict, name, l); key = okey % dict->size; if (dict->dict[key].valid == 0) { insert = NULL; @@ -875,25 +975,25 @@ for (insert = &(dict->dict[key]); insert->next != NULL; insert = insert->next) { #ifdef __GNUC__ - if ((insert->okey == okey) && (insert->len == len)) { - if (!memcmp(insert->name, name, len)) + if ((insert->okey == okey) && (insert->len == l)) { + if (!memcmp(insert->name, name, l)) return(insert->name); } #else - if ((insert->okey == okey) && (insert->len == len) && - (!xmlStrncmp(insert->name, name, len))) + if ((insert->okey == okey) && (insert->len == l) && + (!xmlStrncmp(insert->name, name, l))) return(insert->name); #endif nbi++; } #ifdef __GNUC__ - if ((insert->okey == okey) && (insert->len == len)) { - if (!memcmp(insert->name, name, len)) + if ((insert->okey == okey) && (insert->len == l)) { + if (!memcmp(insert->name, name, l)) return(insert->name); } #else - if ((insert->okey == okey) && (insert->len == len) && - (!xmlStrncmp(insert->name, name, len))) + if ((insert->okey == okey) && (insert->len == l) && + (!xmlStrncmp(insert->name, name, l))) return(insert->name); #endif } @@ -906,7 +1006,7 @@ (dict->subdict->size != MIN_DICT_SIZE)) || ((dict->size != MIN_DICT_SIZE) && (dict->subdict->size == MIN_DICT_SIZE))) - skey = xmlDictComputeKey(dict->subdict, name, len); + skey = xmlDictComputeKey(dict->subdict, name, l); else skey = okey; @@ -917,25 +1017,25 @@ for (tmp = &(dict->subdict->dict[key]); tmp->next != NULL; tmp = tmp->next) { #ifdef __GNUC__ - if ((tmp->okey == skey) && (tmp->len == len)) { - if (!memcmp(tmp->name, name, len)) + if ((tmp->okey == skey) && (tmp->len == l)) { + if (!memcmp(tmp->name, name, l)) return(tmp->name); } #else - if ((tmp->okey == skey) && (tmp->len == len) && - (!xmlStrncmp(tmp->name, name, len))) + if ((tmp->okey == skey) && (tmp->len == l) && + (!xmlStrncmp(tmp->name, name, l))) return(tmp->name); #endif nbi++; } #ifdef __GNUC__ - if ((tmp->okey == skey) && (tmp->len == len)) { - if (!memcmp(tmp->name, name, len)) + if ((tmp->okey == skey) && (tmp->len == l)) { + if (!memcmp(tmp->name, name, l)) return(tmp->name); } #else - if ((tmp->okey == skey) && (tmp->len == len) && - (!xmlStrncmp(tmp->name, name, len))) + if ((tmp->okey == skey) && (tmp->len == l) && + (!xmlStrncmp(tmp->name, name, l))) return(tmp->name); #endif } @@ -961,7 +1061,7 @@ xmlDictEntryPtr entry; xmlDictEntryPtr insert; const xmlChar *ret; - int len, plen, l; + unsigned int len, plen, l; if ((dict == NULL) || (name == NULL)) return(NULL); @@ -1095,6 +1195,50 @@ return(dict->nbElems); } +/** + * xmlDictSetLimit: + * @dict: the dictionnary + * @limit: the limit in bytes + * + * Set a size limit for the dictionary + * Added in 2.9.0 + * + * Returns the previous limit of the dictionary or 0 + */ +size_t +xmlDictSetLimit(xmlDictPtr dict, size_t limit) { + size_t ret; + if (dict == NULL) + return(0); + ret = dict->limit; + dict->limit = limit; + return(ret); +} + +/** + * xmlDictGetUsage: + * @dict: the dictionnary + * + * Get how much memory is used by a dictionary for strings + * Added in 2.9.0 + * + * Returns the amount of strings allocated + */ +size_t +xmlDictGetUsage(xmlDictPtr dict) { + xmlDictStringsPtr pool; + size_t limit = 0; + + if (dict == NULL) + return(0); + pool = dict->strings; + while (pool != NULL) { + limit += pool->size; + pool = pool->next; + } + return(limit); +} + #define bottom_dict #include "elfgcchack.h" Index: lib/3rdparty/libxml2/enc.h =================================================================== --- lib/3rdparty/libxml2/enc.h (revision 0) +++ lib/3rdparty/libxml2/enc.h (working copy) @@ -0,0 +1,32 @@ +/* + * Summary: Internal Interfaces for encoding in libxml2 + * Description: this module describes a few interfaces which were + * addded along with the API changes in 2.9.0 + * those are private routines at this point + * + * Copy: See Copyright for the status of this software. + * + * Author: Daniel Veillard + */ + +#ifndef __XML_ENC_H__ +#define __XML_ENC_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +int xmlCharEncFirstLineInt(xmlCharEncodingHandler *handler, xmlBufferPtr out, + xmlBufferPtr in, int len); +int xmlCharEncFirstLineInput(xmlParserInputBufferPtr input, int len); +int xmlCharEncInput(xmlParserInputBufferPtr input); +int xmlCharEncOutput(xmlOutputBufferPtr output, int init); + +#ifdef __cplusplus +} +#endif +#endif /* __XML_ENC_H__ */ + + Index: lib/3rdparty/libxml2/encoding.c =================================================================== --- lib/3rdparty/libxml2/encoding.c (revision 58294) +++ lib/3rdparty/libxml2/encoding.c (working copy) @@ -24,6 +24,7 @@ #include "libxml.h" #include +#include #ifdef HAVE_CTYPE_H #include @@ -44,6 +45,9 @@ #include #include +#include "buf.h" +#include "enc.h" + static xmlCharEncodingHandlerPtr xmlUTF16LEHandler = NULL; static xmlCharEncodingHandlerPtr xmlUTF16BEHandler = NULL; @@ -1897,9 +1901,6 @@ * The real API used by libxml for on-the-fly conversion * * * ************************************************************************/ -int -xmlCharEncFirstLineInt(xmlCharEncodingHandler *handler, xmlBufferPtr out, - xmlBufferPtr in, int len); /** * xmlCharEncFirstLineInt: @@ -1928,7 +1929,7 @@ if (in == NULL) return(-1); /* calculate space available */ - written = out->size - out->use; + written = out->size - out->use - 1; /* count '\0' */ toconv = in->use; /* * echo '' | wc -c => 38 @@ -1946,7 +1947,7 @@ toconv = 180; } if (toconv * 2 >= written) { - xmlBufferGrow(out, toconv); + xmlBufferGrow(out, toconv * 2); written = out->size - out->use - 1; } @@ -2029,6 +2030,251 @@ } /** + * xmlCharEncFirstLineInput: + * @input: a parser input buffer + * @len: number of bytes to convert for the first line, or -1 + * + * Front-end for the encoding handler input function, but handle only + * the very first line. Point is that this is based on autodetection + * of the encoding and once that first line is converted we may find + * out that a different decoder is needed to process the input. + * + * Returns the number of byte written if success, or + * -1 general error + * -2 if the transcoding fails (for *in is not valid utf8 string or + * the result of transformation can't fit into the encoding we want), or + */ +int +xmlCharEncFirstLineInput(xmlParserInputBufferPtr input, int len) +{ + int ret = -2; + size_t written; + size_t toconv; + int c_in; + int c_out; + xmlBufPtr in; + xmlBufPtr out; + + if ((input == NULL) || (input->encoder == NULL) || + (input->buffer == NULL) || (input->raw == NULL)) + return (-1); + out = input->buffer; + in = input->raw; + + toconv = xmlBufUse(in); + if (toconv == 0) + return (0); + written = xmlBufAvail(out) - 1; /* count '\0' */ + /* + * echo '' | wc -c => 38 + * 45 chars should be sufficient to reach the end of the encoding + * declaration without going too far inside the document content. + * on UTF-16 this means 90bytes, on UCS4 this means 180 + * The actual value depending on guessed encoding is passed as @len + * if provided + */ + if (len >= 0) { + if (toconv > (unsigned int) len) + toconv = len; + } else { + if (toconv > 180) + toconv = 180; + } + if (toconv * 2 >= written) { + xmlBufGrow(out, toconv * 2); + written = xmlBufAvail(out) - 1; + } + if (written > 360) + written = 360; + + c_in = toconv; + c_out = written; + if (input->encoder->input != NULL) { + ret = input->encoder->input(xmlBufEnd(out), &c_out, + xmlBufContent(in), &c_in); + xmlBufShrink(in, c_in); + xmlBufAddLen(out, c_out); + } +#ifdef LIBXML_ICONV_ENABLED + else if (input->encoder->iconv_in != NULL) { + ret = xmlIconvWrapper(input->encoder->iconv_in, xmlBufEnd(out), + &c_out, xmlBufContent(in), &c_in); + xmlBufShrink(in, c_in); + xmlBufAddLen(out, c_out); + if (ret == -1) + ret = -3; + } +#endif /* LIBXML_ICONV_ENABLED */ +#ifdef LIBXML_ICU_ENABLED + else if (input->encoder->uconv_in != NULL) { + ret = xmlUconvWrapper(input->encoder->uconv_in, 1, xmlBufEnd(out), + &c_out, xmlBufContent(in), &c_in); + xmlBufShrink(in, c_in); + xmlBufAddLen(out, c_out); + if (ret == -1) + ret = -3; + } +#endif /* LIBXML_ICU_ENABLED */ + switch (ret) { + case 0: +#ifdef DEBUG_ENCODING + xmlGenericError(xmlGenericErrorContext, + "converted %d bytes to %d bytes of input\n", + c_in, c_out); +#endif + break; + case -1: +#ifdef DEBUG_ENCODING + xmlGenericError(xmlGenericErrorContext, + "converted %d bytes to %d bytes of input, %d left\n", + c_in, c_out, (int)xmlBufUse(in)); +#endif + break; + case -3: +#ifdef DEBUG_ENCODING + xmlGenericError(xmlGenericErrorContext, + "converted %d bytes to %d bytes of input, %d left\n", + c_in, c_out, (int)xmlBufUse(in)); +#endif + break; + case -2: { + char buf[50]; + const xmlChar *content = xmlBufContent(in); + + snprintf(&buf[0], 49, "0x%02X 0x%02X 0x%02X 0x%02X", + content[0], content[1], + content[2], content[3]); + buf[49] = 0; + xmlEncodingErr(XML_I18N_CONV_FAILED, + "input conversion failed due to input error, bytes %s\n", + buf); + } + } + /* + * Ignore when input buffer is not on a boundary + */ + if (ret == -3) ret = 0; + if (ret == -1) ret = 0; + return(ret); +} + +/** + * xmlCharEncInput: + * @input: a parser input buffer + * + * Generic front-end for the encoding handler on parser input + * + * Returns the number of byte written if success, or + * -1 general error + * -2 if the transcoding fails (for *in is not valid utf8 string or + * the result of transformation can't fit into the encoding we want), or + */ +int +xmlCharEncInput(xmlParserInputBufferPtr input) +{ + int ret = -2; + size_t written; + size_t toconv; + int c_in; + int c_out; + xmlBufPtr in; + xmlBufPtr out; + + if ((input == NULL) || (input->encoder == NULL) || + (input->buffer == NULL) || (input->raw == NULL)) + return (-1); + out = input->buffer; + in = input->raw; + + toconv = xmlBufUse(in); + if (toconv == 0) + return (0); + if (toconv > 64 * 1024) + toconv = 64 * 1024; + written = xmlBufAvail(out); + if (written > 0) + written--; /* count '\0' */ + if (toconv * 2 >= written) { + xmlBufGrow(out, toconv * 2); + written = xmlBufAvail(out); + if (written > 0) + written--; /* count '\0' */ + } + if (written > 128 * 1024) + written = 128 * 1024; + + c_in = toconv; + c_out = written; + if (input->encoder->input != NULL) { + ret = input->encoder->input(xmlBufEnd(out), &c_out, + xmlBufContent(in), &c_in); + xmlBufShrink(in, c_in); + xmlBufAddLen(out, c_out); + } +#ifdef LIBXML_ICONV_ENABLED + else if (input->encoder->iconv_in != NULL) { + ret = xmlIconvWrapper(input->encoder->iconv_in, xmlBufEnd(out), + &c_out, xmlBufContent(in), &c_in); + xmlBufShrink(in, c_in); + xmlBufAddLen(out, c_out); + if (ret == -1) + ret = -3; + } +#endif /* LIBXML_ICONV_ENABLED */ +#ifdef LIBXML_ICU_ENABLED + else if (input->encoder->uconv_in != NULL) { + ret = xmlUconvWrapper(input->encoder->uconv_in, 1, xmlBufEnd(out), + &c_out, xmlBufContent(in), &c_in); + xmlBufShrink(in, c_in); + xmlBufAddLen(out, c_out); + if (ret == -1) + ret = -3; + } +#endif /* LIBXML_ICU_ENABLED */ + switch (ret) { + case 0: +#ifdef DEBUG_ENCODING + xmlGenericError(xmlGenericErrorContext, + "converted %d bytes to %d bytes of input\n", + c_in, c_out); +#endif + break; + case -1: +#ifdef DEBUG_ENCODING + xmlGenericError(xmlGenericErrorContext, + "converted %d bytes to %d bytes of input, %d left\n", + c_in, c_out, (int)xmlBufUse(in)); +#endif + break; + case -3: +#ifdef DEBUG_ENCODING + xmlGenericError(xmlGenericErrorContext, + "converted %d bytes to %d bytes of input, %d left\n", + c_in, c_out, (int)xmlBufUse(in)); +#endif + break; + case -2: { + char buf[50]; + const xmlChar *content = xmlBufContent(in); + + snprintf(&buf[0], 49, "0x%02X 0x%02X 0x%02X 0x%02X", + content[0], content[1], + content[2], content[3]); + buf[49] = 0; + xmlEncodingErr(XML_I18N_CONV_FAILED, + "input conversion failed due to input error, bytes %s\n", + buf); + } + } + /* + * Ignore when input buffer is not on a boundary + */ + if (ret == -3) + ret = 0; + return (c_out? c_out : ret); +} + +/** * xmlCharEncInFunc: * @handler: char encoding transformation data structure * @out: an xmlBuffer for the output. @@ -2059,7 +2305,7 @@ toconv = in->use; if (toconv == 0) return (0); - written = out->size - out->use; + written = out->size - out->use -1; /* count '\0' */ if (toconv * 2 >= written) { xmlBufferGrow(out, out->size + toconv * 2); written = out->size - out->use - 1; @@ -2136,6 +2382,235 @@ } /** + * xmlCharEncOutput: + * @output: a parser output buffer + * @init: is this an initialization call without data + * + * Generic front-end for the encoding handler on parser output + * a first call with @init == 1 has to be made first to initiate the + * output in case of non-stateless encoding needing to initiate their + * state or the output (like the BOM in UTF16). + * In case of UTF8 sequence conversion errors for the given encoder, + * the content will be automatically remapped to a CharRef sequence. + * + * Returns the number of byte written if success, or + * -1 general error + * -2 if the transcoding fails (for *in is not valid utf8 string or + * the result of transformation can't fit into the encoding we want), or + */ +int +xmlCharEncOutput(xmlOutputBufferPtr output, int init) +{ + int ret = -2; + size_t written; + size_t writtentot = 0; + size_t toconv; + int c_in; + int c_out; + xmlBufPtr in; + xmlBufPtr out; + int charref_len = 0; + + if ((output == NULL) || (output->encoder == NULL) || + (output->buffer == NULL) || (output->conv == NULL)) + return (-1); + out = output->conv; + in = output->buffer; + +retry: + + written = xmlBufAvail(out); + if (written > 0) + written--; /* count '\0' */ + + /* + * First specific handling of the initialization call + */ + if (init) { + c_in = 0; + c_out = written; + if (output->encoder->output != NULL) { + ret = output->encoder->output(xmlBufEnd(out), &c_out, + NULL, &c_in); + if (ret > 0) /* Gennady: check return value */ + xmlBufAddLen(out, c_out); + } +#ifdef LIBXML_ICONV_ENABLED + else if (output->encoder->iconv_out != NULL) { + ret = xmlIconvWrapper(output->encoder->iconv_out, xmlBufEnd(out), + &c_out, NULL, &c_in); + xmlBufAddLen(out, c_out); + } +#endif /* LIBXML_ICONV_ENABLED */ +#ifdef LIBXML_ICU_ENABLED + else if (output->encoder->uconv_out != NULL) { + ret = xmlUconvWrapper(output->encoder->uconv_out, 0, xmlBufEnd(out), + &c_out, NULL, &c_in); + xmlBufAddLen(out, c_out); + } +#endif /* LIBXML_ICU_ENABLED */ +#ifdef DEBUG_ENCODING + xmlGenericError(xmlGenericErrorContext, + "initialized encoder\n"); +#endif + return(0); + } + + /* + * Conversion itself. + */ + toconv = xmlBufUse(in); + if (toconv == 0) + return (0); + if (toconv > 64 * 1024) + toconv = 64 * 1024; + if (toconv * 4 >= written) { + xmlBufGrow(out, toconv * 4); + written = xmlBufAvail(out) - 1; + } + if (written > 256 * 1024) + written = 256 * 1024; + + c_in = toconv; + c_out = written; + if (output->encoder->output != NULL) { + ret = output->encoder->output(xmlBufEnd(out), &c_out, + xmlBufContent(in), &c_in); + if (c_out > 0) { + xmlBufShrink(in, c_in); + xmlBufAddLen(out, c_out); + writtentot += c_out; + } + } +#ifdef LIBXML_ICONV_ENABLED + else if (output->encoder->iconv_out != NULL) { + ret = xmlIconvWrapper(output->encoder->iconv_out, xmlBufEnd(out), + &c_out, xmlBufContent(in), &c_in); + xmlBufShrink(in, c_in); + xmlBufAddLen(out, c_out); + writtentot += c_out; + if (ret == -1) { + if (c_out > 0) { + /* + * Can be a limitation of iconv + */ + charref_len = 0; + goto retry; + } + ret = -3; + } + } +#endif /* LIBXML_ICONV_ENABLED */ +#ifdef LIBXML_ICU_ENABLED + else if (output->encoder->uconv_out != NULL) { + ret = xmlUconvWrapper(output->encoder->uconv_out, 0, xmlBufEnd(out), + &c_out, xmlBufContent(in), &c_in); + xmlBufShrink(in, c_in); + xmlBufAddLen(out, c_out); + writtentot += c_out; + if (ret == -1) { + if (c_out > 0) { + /* + * Can be a limitation of uconv + */ + charref_len = 0; + goto retry; + } + ret = -3; + } + } +#endif /* LIBXML_ICU_ENABLED */ + else { + xmlEncodingErr(XML_I18N_NO_OUTPUT, + "xmlCharEncOutFunc: no output function !\n", NULL); + return(-1); + } + + if (ret >= 0) output += ret; + + /* + * Attempt to handle error cases + */ + switch (ret) { + case 0: +#ifdef DEBUG_ENCODING + xmlGenericError(xmlGenericErrorContext, + "converted %d bytes to %d bytes of output\n", + c_in, c_out); +#endif + break; + case -1: +#ifdef DEBUG_ENCODING + xmlGenericError(xmlGenericErrorContext, + "output conversion failed by lack of space\n"); +#endif + break; + case -3: +#ifdef DEBUG_ENCODING + xmlGenericError(xmlGenericErrorContext,"converted %d bytes to %d bytes of output %d left\n", + c_in, c_out, (int) xmlBufUse(in)); +#endif + break; + case -2: { + int len = (int) xmlBufUse(in); + xmlChar *content = xmlBufContent(in); + int cur; + + cur = xmlGetUTF8Char(content, &len); + if ((charref_len != 0) && (c_out < charref_len)) { + /* + * We attempted to insert a character reference and failed. + * Undo what was written and skip the remaining charref. + */ + xmlBufErase(out, c_out); + writtentot -= c_out; + xmlBufShrink(in, charref_len - c_out); + charref_len = 0; + + ret = -1; + break; + } else if (cur > 0) { + xmlChar charref[20]; + +#ifdef DEBUG_ENCODING + xmlGenericError(xmlGenericErrorContext, + "handling output conversion error\n"); + xmlGenericError(xmlGenericErrorContext, + "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n", + content[0], content[1], + content[2], content[3]); +#endif + /* + * Removes the UTF8 sequence, and replace it by a charref + * and continue the transcoding phase, hoping the error + * did not mangle the encoder state. + */ + charref_len = snprintf((char *) &charref[0], sizeof(charref), + "&#%d;", cur); + xmlBufShrink(in, len); + xmlBufAddHead(in, charref, -1); + + goto retry; + } else { + char buf[50]; + + snprintf(&buf[0], 49, "0x%02X 0x%02X 0x%02X 0x%02X", + content[0], content[1], + content[2], content[3]); + buf[49] = 0; + xmlEncodingErr(XML_I18N_CONV_FAILED, + "output conversion failed due to conv error, bytes %s\n", + buf); + if (xmlBufGetAllocationScheme(in) != XML_BUFFER_ALLOC_IMMUTABLE) + content[0] = ' '; + } + break; + } + } + return(ret); +} + +/** * xmlCharEncOutFunc: * @handler: char enconding transformation data structure * @out: an xmlBuffer for the output. @@ -2161,6 +2636,7 @@ int writtentot = 0; int toconv; int output = 0; + int charref_len = 0; if (handler == NULL) return(-1); if (out == NULL) return(-1); @@ -2242,6 +2718,7 @@ /* * Can be a limitation of iconv */ + charref_len = 0; goto retry; } ret = -3; @@ -2262,6 +2739,7 @@ /* * Can be a limitation of iconv */ + charref_len = 0; goto retry; } ret = -3; @@ -2305,7 +2783,19 @@ int cur; cur = xmlGetUTF8Char(utf, &len); - if (cur > 0) { + if ((charref_len != 0) && (written < charref_len)) { + /* + * We attempted to insert a character reference and failed. + * Undo what was written and skip the remaining charref. + */ + out->use -= written; + writtentot -= written; + xmlBufferShrink(in, charref_len - written); + charref_len = 0; + + ret = -1; + break; + } else if (cur > 0) { xmlChar charref[20]; #ifdef DEBUG_ENCODING @@ -2321,7 +2811,8 @@ * and continue the transcoding phase, hoping the error * did not mangle the encoder state. */ - snprintf((char *) &charref[0], sizeof(charref), "&#%d;", cur); + charref_len = snprintf((char *) &charref[0], sizeof(charref), + "&#%d;", cur); xmlBufferShrink(in, len); xmlBufferAddHead(in, charref, -1); Index: lib/3rdparty/libxml2/entities.c =================================================================== --- lib/3rdparty/libxml2/entities.c (revision 58294) +++ lib/3rdparty/libxml2/entities.c (working copy) @@ -22,6 +22,8 @@ #include #include +#include "save.h" + /* * The XML predefined entities. */ @@ -528,20 +530,20 @@ * Macro used to grow the current buffer. */ #define growBufferReentrant() { \ - buffer_size *= 2; \ - buffer = (xmlChar *) \ - xmlRealloc(buffer, buffer_size * sizeof(xmlChar)); \ - if (buffer == NULL) { \ - xmlEntitiesErrMemory("xmlEncodeEntitiesReentrant: realloc failed");\ - return(NULL); \ - } \ + xmlChar *tmp; \ + size_t new_size = buffer_size * 2; \ + if (new_size < buffer_size) goto mem_error; \ + tmp = (xmlChar *) xmlRealloc(buffer, new_size); \ + if (tmp == NULL) goto mem_error; \ + buffer = tmp; \ + buffer_size = new_size; \ } - /** - * xmlEncodeEntitiesReentrant: + * xmlEncodeEntitiesInternal: * @doc: the document containing the string * @input: A string to convert to XML. + * @attr: are we handling an atrbute value * * Do a global encoding of a string, replacing the predefined entities * and non ASCII values with their entities and CharRef counterparts. @@ -550,12 +552,12 @@ * * Returns A newly allocated string with the substitution done. */ -xmlChar * -xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlChar *input) { +static xmlChar * +xmlEncodeEntitiesInternal(xmlDocPtr doc, const xmlChar *input, int attr) { const xmlChar *cur = input; xmlChar *buffer = NULL; xmlChar *out = NULL; - int buffer_size = 0; + size_t buffer_size = 0; int html = 0; if (input == NULL) return(NULL); @@ -568,14 +570,14 @@ buffer_size = 1000; buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar)); if (buffer == NULL) { - xmlEntitiesErrMemory("xmlEncodeEntitiesReentrant: malloc failed"); + xmlEntitiesErrMemory("xmlEncodeEntities: malloc failed"); return(NULL); } out = buffer; while (*cur != '\0') { - if (out - buffer > buffer_size - 100) { - int indx = out - buffer; + size_t indx = out - buffer; + if (indx + 100 > buffer_size) { growBufferReentrant(); out = &buffer[indx]; @@ -585,6 +587,27 @@ * By default one have to encode at least '<', '>', '"' and '&' ! */ if (*cur == '<') { + const xmlChar *end; + + /* + * Special handling of server side include in HTML attributes + */ + if (html && attr && + (cur[1] == '!') && (cur[2] == '-') && (cur[3] == '-') && + ((end = xmlStrstr(cur, BAD_CAST "-->")) != NULL)) { + while (cur != end) { + *out++ = *cur++; + indx = out - buffer; + if (indx + 100 > buffer_size) { + growBufferReentrant(); + out = &buffer[indx]; + } + } + *out++ = *cur++; + *out++ = *cur++; + *out++ = *cur++; + continue; + } *out++ = '&'; *out++ = 'l'; *out++ = 't'; @@ -595,6 +618,23 @@ *out++ = 't'; *out++ = ';'; } else if (*cur == '&') { + /* + * Special handling of &{...} construct from HTML 4, see + * http://www.w3.org/TR/html401/appendix/notes.html#h-B.7.1 + */ + if (html && attr && (cur[1] == '{') && + (strchr((const char *) cur, '}'))) { + while (*cur != '}') { + *out++ = *cur++; + indx = out - buffer; + if (indx + 100 > buffer_size) { + growBufferReentrant(); + out = &buffer[indx]; + } + } + *out++ = *cur++; + continue; + } *out++ = '&'; *out++ = 'a'; *out++ = 'm'; @@ -627,7 +667,7 @@ if (*cur < 0xC0) { xmlEntitiesErr(XML_CHECK_NOT_UTF8, - "xmlEncodeEntitiesReentrant : input not UTF-8"); + "xmlEncodeEntities: input not UTF-8"); if (doc != NULL) doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1"); snprintf(buf, sizeof(buf), "&#%d;", *cur); @@ -660,7 +700,7 @@ } if ((l == 1) || (!IS_CHAR(val))) { xmlEntitiesErr(XML_ERR_INVALID_CHAR, - "xmlEncodeEntitiesReentrant : char out of range\n"); + "xmlEncodeEntities: char out of range\n"); if (doc != NULL) doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1"); snprintf(buf, sizeof(buf), "&#%d;", *cur); @@ -692,9 +732,47 @@ } *out = 0; return(buffer); + +mem_error: + xmlEntitiesErrMemory("xmlEncodeEntities: realloc failed"); + xmlFree(buffer); + return(NULL); } /** + * xmlEncodeAttributeEntities: + * @doc: the document containing the string + * @input: A string to convert to XML. + * + * Do a global encoding of a string, replacing the predefined entities + * and non ASCII values with their entities and CharRef counterparts for + * attribute values. + * + * Returns A newly allocated string with the substitution done. + */ +xmlChar * +xmlEncodeAttributeEntities(xmlDocPtr doc, const xmlChar *input) { + return xmlEncodeEntitiesInternal(doc, input, 1); +} + +/** + * xmlEncodeEntitiesReentrant: + * @doc: the document containing the string + * @input: A string to convert to XML. + * + * Do a global encoding of a string, replacing the predefined entities + * and non ASCII values with their entities and CharRef counterparts. + * Contrary to xmlEncodeEntities, this routine is reentrant, and result + * must be deallocated. + * + * Returns A newly allocated string with the substitution done. + */ +xmlChar * +xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlChar *input) { + return xmlEncodeEntitiesInternal(doc, input, 0); +} + +/** * xmlEncodeSpecialChars: * @doc: the document containing the string * @input: A string to convert to XML. @@ -709,7 +787,7 @@ const xmlChar *cur = input; xmlChar *buffer = NULL; xmlChar *out = NULL; - int buffer_size = 0; + size_t buffer_size = 0; if (input == NULL) return(NULL); /* @@ -724,8 +802,8 @@ out = buffer; while (*cur != '\0') { - if (out - buffer > buffer_size - 10) { - int indx = out - buffer; + size_t indx = out - buffer; + if (indx + 10 > buffer_size) { growBufferReentrant(); out = &buffer[indx]; @@ -774,6 +852,11 @@ } *out = 0; return(buffer); + +mem_error: + xmlEntitiesErrMemory("xmlEncodeSpecialChars: realloc failed"); + xmlFree(buffer); + return(NULL); } /** Index: lib/3rdparty/libxml2/error.c =================================================================== --- lib/3rdparty/libxml2/error.c (revision 58294) +++ lib/3rdparty/libxml2/error.c (working copy) @@ -292,7 +292,10 @@ } else { if (file != NULL) channel(data, "%s:%d: ", file, line); - else if ((line != 0) && (domain == XML_FROM_PARSER)) + else if ((line != 0) && + ((domain == XML_FROM_PARSER) || (domain == XML_FROM_SCHEMASV)|| + (domain == XML_FROM_SCHEMASP)||(domain == XML_FROM_DTD) || + (domain == XML_FROM_RELAXNGP)||(domain == XML_FROM_RELAXNGV))) channel(data, "Entity: line %d: ", line); } if (name != NULL) { @@ -360,6 +363,15 @@ case XML_FROM_I18N: channel(data, "encoding "); break; + case XML_FROM_SCHEMATRONV: + channel(data, "schematron "); + break; + case XML_FROM_BUFFER: + channel(data, "internal buffer "); + break; + case XML_FROM_URI: + channel(data, "URI "); + break; default: break; } @@ -452,6 +464,8 @@ xmlErrorPtr to = &xmlLastError; xmlNodePtr baseptr = NULL; + if (code == XML_ERR_OK) + return; if ((xmlGetWarningsDefaultValue == 0) && (level == XML_ERR_WARNING)) return; if ((domain == XML_FROM_PARSER) || (domain == XML_FROM_HTML) || @@ -459,8 +473,11 @@ (domain == XML_FROM_IO) || (domain == XML_FROM_VALID)) { ctxt = (xmlParserCtxtPtr) ctx; if ((schannel == NULL) && (ctxt != NULL) && (ctxt->sax != NULL) && - (ctxt->sax->initialized == XML_SAX2_MAGIC)) + (ctxt->sax->initialized == XML_SAX2_MAGIC) && + (ctxt->sax->serror != NULL)) { schannel = ctxt->sax->serror; + data = ctxt->userData; + } } /* * Check if structured error handler set @@ -473,16 +490,6 @@ if (schannel != NULL) data = xmlStructuredErrorContext; } - if ((domain == XML_FROM_VALID) && - ((channel == xmlParserValidityError) || - (channel == xmlParserValidityWarning))) { - ctxt = (xmlParserCtxtPtr) ctx; - if ((schannel == NULL) && (ctxt != NULL) && (ctxt->sax != NULL) && - (ctxt->sax->initialized == XML_SAX2_MAGIC)) - schannel = ctxt->sax->serror; - } - if (code == XML_ERR_OK) - return; /* * Formatting the message */ @@ -526,6 +533,8 @@ if ((node != NULL) && (node->type == XML_ELEMENT_NODE)) line = node->line; + if ((line == 0) || (line == 65535)) + line = xmlGetLineNo(node); } /* @@ -589,6 +598,11 @@ if (to != &xmlLastError) xmlCopyError(to,&xmlLastError); + if (schannel != NULL) { + schannel(data, to); + return; + } + /* * Find the callback channel if channel param is NULL */ @@ -600,20 +614,13 @@ channel = ctxt->sax->error; data = ctxt->userData; } else if (channel == NULL) { - if ((schannel == NULL) && (xmlStructuredError != NULL)) { - schannel = xmlStructuredError; - data = xmlStructuredErrorContext; + channel = xmlGenericError; + if (ctxt != NULL) { + data = ctxt; } else { - channel = xmlGenericError; - if (!data) { - data = xmlGenericErrorContext; - } + data = xmlGenericErrorContext; } } - if (schannel != NULL) { - schannel(data, to); - return; - } if (channel == NULL) return; Index: lib/3rdparty/libxml2/globals.c =================================================================== --- lib/3rdparty/libxml2/globals.c (revision 58294) +++ lib/3rdparty/libxml2/globals.c (working copy) @@ -509,7 +509,7 @@ #if defined(LIBXML_DOCB_ENABLED) && defined(LIBXML_LEGACY_ENABLED) && defined(LIBXML_SAX1_ENABLED) initdocbDefaultSAXHandler(&gs->docbDefaultSAXHandler); #endif -#if defined(LIBXML_HTML_ENABLED) && defined(LIBXML_LEGACY_ENABLED) +#if defined(LIBXML_HTML_ENABLED) && defined(LIBXML_LEGACY_ENABLED) && defined(LIBXML_SAX1_ENABLED) inithtmlDefaultSAXHandler(&gs->htmlDefaultSAXHandler); #endif Index: lib/3rdparty/libxml2/hash.c =================================================================== --- lib/3rdparty/libxml2/hash.c (revision 58294) +++ lib/3rdparty/libxml2/hash.c (working copy) @@ -3,7 +3,7 @@ * * Reference: Your favorite introductory book on algorithms * - * Copyright (C) 2000 Bjorn Reese and Daniel Veillard. + * Copyright (C) 2000,2012 Bjorn Reese and Daniel Veillard. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -21,6 +21,22 @@ #include "libxml.h" #include +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_TIME_H +#include +#endif + +/* + * Following http://www.ocert.org/advisories/ocert-2011-003.html + * it seems that having hash randomization might be a good idea + * when using XML with untrusted data + */ +#if defined(HAVE_RAND) && defined(HAVE_SRAND) && defined(HAVE_TIME) +#define HASH_RANDOMIZATION +#endif + #include #include #include @@ -53,6 +69,9 @@ int size; int nbElems; xmlDictPtr dict; +#ifdef HASH_RANDOMIZATION + int random_seed; +#endif }; /* @@ -65,6 +84,9 @@ unsigned long value = 0L; char ch; +#ifdef HASH_RANDOMIZATION + value = table->random_seed; +#endif if (name != NULL) { value += 30 * (*name); while ((ch = *name++) != 0) { @@ -92,6 +114,9 @@ unsigned long value = 0L; char ch; +#ifdef HASH_RANDOMIZATION + value = table->random_seed; +#endif if (prefix != NULL) value += 30 * (*prefix); else @@ -156,6 +181,9 @@ table->table = xmlMalloc(size * sizeof(xmlHashEntry)); if (table->table) { memset(table->table, 0, size * sizeof(xmlHashEntry)); +#ifdef HASH_RANDOMIZATION + table->random_seed = __xmlRandom(); +#endif return(table); } xmlFree(table); Index: lib/3rdparty/libxml2/HTMLparser.c =================================================================== --- lib/3rdparty/libxml2/HTMLparser.c (revision 58294) +++ lib/3rdparty/libxml2/HTMLparser.c (working copy) @@ -44,6 +44,9 @@ #include #include +#include "buf.h" +#include "enc.h" + #define HTML_MAX_NAMELEN 1000 #define HTML_PARSER_BIG_BUFFER_SIZE 1000 #define HTML_PARSER_BUFFER_SIZE 100 @@ -727,7 +730,7 @@ static const char* const name_attr[] = { "name", NULL } ; static const char* const action_attr[] = { "action", NULL } ; static const char* const blockli_elt[] = { BLOCK, "li", NULL } ; -static const char* const meta_attrs[] = { I18N, "http-equiv", "name", "scheme", NULL } ; +static const char* const meta_attrs[] = { I18N, "http-equiv", "name", "scheme", "charset", NULL } ; static const char* const content_attr[] = { "content", NULL } ; static const char* const type_attr[] = { "type", NULL } ; static const char* const noframes_content[] = { "body", FLOW MODIFIER, NULL } ; @@ -1080,9 +1083,9 @@ "menu", "p", "head", "ul", NULL, "p", "p", "head", "h1", "h2", "h3", "h4", "h5", "h6", FONTSTYLE, NULL, "div", "p", "head", NULL, -"noscript", "p", "head", NULL, +"noscript", "p", NULL, "center", "font", "b", "i", "p", "head", NULL, -"a", "a", NULL, +"a", "a", "head", NULL, "caption", "p", NULL, "colgroup", "caption", "colgroup", "col", "p", NULL, "col", "caption", "col", "p", NULL, @@ -1100,6 +1103,43 @@ "option", "option", NULL, "fieldset", "legend", "p", "head", "h1", "h2", "h3", "h4", "h5", "h6", "pre", "listing", "xmp", "a", NULL, +/* most tags in in FONTSTYLE, PHRASE and SPECIAL should close */ +"tt", "head", NULL, +"i", "head", NULL, +"b", "head", NULL, +"u", "head", NULL, +"s", "head", NULL, +"strike", "head", NULL, +"big", "head", NULL, +"small", "head", NULL, + +"em", "head", NULL, +"strong", "head", NULL, +"dfn", "head", NULL, +"code", "head", NULL, +"samp", "head", NULL, +"kbd", "head", NULL, +"var", "head", NULL, +"cite", "head", NULL, +"abbr", "head", NULL, +"acronym", "head", NULL, + +/* "a" */ +"img", "head", NULL, +/* "applet" */ +/* "embed" */ +/* "object" */ +"font", "head", NULL, +/* "basefont" */ +"br", "head", NULL, +/* "script" */ +"map", "head", NULL, +"q", "head", NULL, +"sub", "head", NULL, +"sup", "head", NULL, +"span", "head", NULL, +"bdo", "head", NULL, +"iframe", "head", NULL, NULL }; @@ -2941,9 +2981,14 @@ */ if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { if (areBlanks(ctxt, buf, nbchar)) { - if (ctxt->sax->ignorableWhitespace != NULL) - ctxt->sax->ignorableWhitespace(ctxt->userData, - buf, nbchar); + if (ctxt->keepBlanks) { + if (ctxt->sax->characters != NULL) + ctxt->sax->characters(ctxt->userData, buf, nbchar); + } else { + if (ctxt->sax->ignorableWhitespace != NULL) + ctxt->sax->ignorableWhitespace(ctxt->userData, + buf, nbchar); + } } else { htmlCheckParagraph(ctxt); if (ctxt->sax->characters != NULL) @@ -2974,8 +3019,14 @@ */ if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { if (areBlanks(ctxt, buf, nbchar)) { - if (ctxt->sax->ignorableWhitespace != NULL) - ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar); + if (ctxt->keepBlanks) { + if (ctxt->sax->characters != NULL) + ctxt->sax->characters(ctxt->userData, buf, nbchar); + } else { + if (ctxt->sax->ignorableWhitespace != NULL) + ctxt->sax->ignorableWhitespace(ctxt->userData, + buf, nbchar); + } } else { htmlCheckParagraph(ctxt); if (ctxt->sax->characters != NULL) @@ -3435,35 +3486,27 @@ } /** - * htmlCheckEncoding: + * htmlCheckEncodingDirect: * @ctxt: an HTML parser context * @attvalue: the attribute value * - * Checks an http-equiv attribute from a Meta tag to detect + * Checks an attribute value to detect * the encoding * If a new encoding is detected the parser is switched to decode * it and pass UTF8 */ static void -htmlCheckEncoding(htmlParserCtxtPtr ctxt, const xmlChar *attvalue) { - const xmlChar *encoding; +htmlCheckEncodingDirect(htmlParserCtxtPtr ctxt, const xmlChar *encoding) { - if ((ctxt == NULL) || (attvalue == NULL)) + if ((ctxt == NULL) || (encoding == NULL) || + (ctxt->options & HTML_PARSE_IGNORE_ENC)) return; /* do not change encoding */ if (ctxt->input->encoding != NULL) return; - encoding = xmlStrcasestr(attvalue, BAD_CAST"charset="); if (encoding != NULL) { - encoding += 8; - } else { - encoding = xmlStrcasestr(attvalue, BAD_CAST"charset ="); - if (encoding != NULL) - encoding += 9; - } - if (encoding != NULL) { xmlCharEncoding enc; xmlCharEncodingHandlerPtr handler; @@ -3500,7 +3543,9 @@ xmlSwitchToEncoding(ctxt, handler); ctxt->charset = XML_CHAR_ENCODING_UTF8; } else { - ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING; + htmlParseErr(ctxt, XML_ERR_UNSUPPORTED_ENCODING, + "htmlCheckEncoding: unknown encoding %s\n", + encoding, NULL); } } @@ -3515,24 +3560,51 @@ * convert as much as possible to the parser reading buffer. */ processed = ctxt->input->cur - ctxt->input->base; - xmlBufferShrink(ctxt->input->buf->buffer, processed); - nbchars = xmlCharEncInFunc(ctxt->input->buf->encoder, - ctxt->input->buf->buffer, - ctxt->input->buf->raw); + xmlBufShrink(ctxt->input->buf->buffer, processed); + nbchars = xmlCharEncInput(ctxt->input->buf); if (nbchars < 0) { htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING, "htmlCheckEncoding: encoder error\n", NULL, NULL); } - ctxt->input->base = - ctxt->input->cur = ctxt->input->buf->buffer->content; - ctxt->input->end = - &ctxt->input->base[ctxt->input->buf->buffer->use]; + xmlBufResetInput(ctxt->input->buf->buffer, ctxt->input); } } } /** + * htmlCheckEncoding: + * @ctxt: an HTML parser context + * @attvalue: the attribute value + * + * Checks an http-equiv attribute from a Meta tag to detect + * the encoding + * If a new encoding is detected the parser is switched to decode + * it and pass UTF8 + */ +static void +htmlCheckEncoding(htmlParserCtxtPtr ctxt, const xmlChar *attvalue) { + const xmlChar *encoding; + + if (!attvalue) + return; + + encoding = xmlStrcasestr(attvalue, BAD_CAST"charset"); + if (encoding != NULL) { + encoding += 7; + } + /* + * skip blank + */ + if (encoding && IS_BLANK_CH(*encoding)) + encoding = xmlStrcasestr(attvalue, BAD_CAST"="); + if (encoding && *encoding == '=') { + encoding ++; + htmlCheckEncodingDirect(ctxt, encoding); + } +} + +/** * htmlCheckMeta: * @ctxt: an HTML parser context * @atts: the attributes values @@ -3556,6 +3628,8 @@ if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"http-equiv")) && (!xmlStrcasecmp(value, BAD_CAST"Content-Type"))) http = 1; + else if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"charset"))) + htmlCheckEncodingDirect(ctxt, value); else if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"content"))) content = value; att = atts[i++]; @@ -3885,6 +3959,7 @@ if ((oldname != NULL) && (xmlStrEqual(oldname, name))) { if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL)) ctxt->sax->endElement(ctxt->userData, name); + htmlNodeInfoPop(ctxt); htmlnamePop(ctxt); ret = 1; } else { @@ -4877,9 +4952,7 @@ input->filename = NULL; input->buf = buf; - input->base = input->buf->buffer->content; - input->cur = input->buf->buffer->content; - input->end = &input->buf->buffer->content[input->buf->buffer->use]; + xmlBufResetInput(buf->buffer, input); inputPush(ctxt, input); return(ctxt); @@ -4996,8 +5069,8 @@ buf = in->base; len = in->length; } else { - buf = in->buf->buffer->content; - len = in->buf->buffer->use; + buf = xmlBufContent(in->buf->buffer); + len = xmlBufUse(in->buf->buffer); } /* take into account the sequence length */ @@ -5123,8 +5196,8 @@ buf = in->base; len = in->length; } else { - buf = in->buf->buffer->content; - len = in->buf->buffer->use; + buf = xmlBufContent(in->buf->buffer); + len = xmlBufUse(in->buf->buffer); } for (; base < len; base++) { @@ -5173,6 +5246,8 @@ int avail = 0; xmlChar cur, next; + htmlParserNodeInfo node_info; + #ifdef DEBUG_PUSH switch (ctxt->instate) { case XML_PARSER_EOF: @@ -5233,7 +5308,7 @@ if (in->buf == NULL) avail = in->length - (in->cur - in->base); else - avail = in->buf->buffer->use - (in->cur - in->base); + avail = xmlBufUse(in->buf->buffer) - (in->cur - in->base); if ((avail == 0) && (terminate)) { htmlAutoCloseOnEnd(ctxt); if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) { @@ -5269,7 +5344,7 @@ if (in->buf == NULL) avail = in->length - (in->cur - in->base); else - avail = in->buf->buffer->use - (in->cur - in->base); + avail = xmlBufUse(in->buf->buffer) - (in->cur - in->base); } if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) ctxt->sax->setDocumentLocator(ctxt->userData, @@ -5311,11 +5386,24 @@ if (in->buf == NULL) avail = in->length - (in->cur - in->base); else - avail = in->buf->buffer->use - (in->cur - in->base); - if (avail < 2) + avail = xmlBufUse(in->buf->buffer) - (in->cur - in->base); + /* + * no chars in buffer + */ + if (avail < 1) goto done; + /* + * not enouth chars in buffer + */ + if (avail < 2) { + if (!terminate) + goto done; + else + next = ' '; + } else { + next = in->cur[1]; + } cur = in->cur[0]; - next = in->cur[1]; if ((cur == '<') && (next == '!') && (in->cur[2] == '-') && (in->cur[3] == '-')) { if ((!terminate) && @@ -5371,7 +5459,7 @@ if (in->buf == NULL) avail = in->length - (in->cur - in->base); else - avail = in->buf->buffer->use - (in->cur - in->base); + avail = xmlBufUse(in->buf->buffer) - (in->cur - in->base); if (avail < 2) goto done; cur = in->cur[0]; @@ -5412,7 +5500,7 @@ if (in->buf == NULL) avail = in->length - (in->cur - in->base); else - avail = in->buf->buffer->use - (in->cur - in->base); + avail = xmlBufUse(in->buf->buffer) - (in->cur - in->base); if (avail < 1) goto done; cur = in->cur[0]; @@ -5465,8 +5553,22 @@ int failed; const htmlElemDesc * info; - if (avail < 2) + /* + * no chars in buffer + */ + if (avail < 1) goto done; + /* + * not enouth chars in buffer + */ + if (avail < 2) { + if (!terminate) + goto done; + else + next = ' '; + } else { + next = in->cur[1]; + } cur = in->cur[0]; if (cur != '<') { ctxt->instate = XML_PARSER_CONTENT; @@ -5476,7 +5578,7 @@ #endif break; } - if (in->cur[1] == '/') { + if (next == '/') { ctxt->instate = XML_PARSER_END_TAG; ctxt->checkIndex = 0; #ifdef DEBUG_PUSH @@ -5489,6 +5591,14 @@ (htmlParseLookupSequence(ctxt, '>', 0, 0, 0, 1) < 0)) goto done; + /* Capture start position */ + if (ctxt->record_info) { + node_info.begin_pos = ctxt->input->consumed + + (CUR_PTR - ctxt->input->base); + node_info.begin_line = ctxt->input->line; + } + + failed = htmlParseStartTag(ctxt); name = ctxt->name; if ((failed == -1) || @@ -5538,6 +5648,9 @@ htmlnamePop(ctxt); } + if (ctxt->record_info) + htmlNodeInfoPush(ctxt, &node_info); + ctxt->instate = XML_PARSER_CONTENT; #ifdef DEBUG_PUSH xmlGenericError(xmlGenericErrorContext, @@ -5554,6 +5667,10 @@ ctxt->sax->endElement(ctxt->userData, name); htmlnamePop(ctxt); } + + if (ctxt->record_info) + htmlNodeInfoPush(ctxt, &node_info); + ctxt->instate = XML_PARSER_CONTENT; #ifdef DEBUG_PUSH xmlGenericError(xmlGenericErrorContext, @@ -5581,9 +5698,15 @@ if ((cur != '<') && (cur != '&')) { if (ctxt->sax != NULL) { if (IS_BLANK_CH(cur)) { - if (ctxt->sax->ignorableWhitespace != NULL) - ctxt->sax->ignorableWhitespace( - ctxt->userData, &cur, 1); + if (ctxt->keepBlanks) { + if (ctxt->sax->characters != NULL) + ctxt->sax->characters( + ctxt->userData, &cur, 1); + } else { + if (ctxt->sax->ignorableWhitespace != NULL) + ctxt->sax->ignorableWhitespace( + ctxt->userData, &cur, 1); + } } else { htmlCheckParagraph(ctxt); if (ctxt->sax->characters != NULL) @@ -5906,8 +6029,8 @@ } if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) { - int base = ctxt->input->base - ctxt->input->buf->buffer->content; - int cur = ctxt->input->cur - ctxt->input->base; + size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, ctxt->input); + size_t cur = ctxt->input->cur - ctxt->input->base; int res; res = xmlParserInputBufferPush(ctxt->input->buf, size, chunk); @@ -5916,10 +6039,7 @@ ctxt->disableSAX = 1; return (XML_PARSER_EOF); } - ctxt->input->base = ctxt->input->buf->buffer->content + base; - ctxt->input->cur = ctxt->input->base + cur; - ctxt->input->end = - &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use]; + xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, base, cur); #ifdef DEBUG_PUSH xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size); #endif @@ -5935,7 +6055,7 @@ (in->raw != NULL)) { int nbchars; - nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw); + nbchars = xmlCharEncInput(in); if (nbchars < 0) { htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING, "encoder error\n", NULL, NULL); @@ -6034,24 +6154,18 @@ inputStream->filename = (char *) xmlCanonicPath((const xmlChar *) filename); inputStream->buf = buf; - inputStream->base = inputStream->buf->buffer->content; - inputStream->cur = inputStream->buf->buffer->content; - inputStream->end = - &inputStream->buf->buffer->content[inputStream->buf->buffer->use]; + xmlBufResetInput(buf->buffer, inputStream); inputPush(ctxt, inputStream); if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && (ctxt->input->buf != NULL)) { - int base = ctxt->input->base - ctxt->input->buf->buffer->content; - int cur = ctxt->input->cur - ctxt->input->base; + size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, ctxt->input); + size_t cur = ctxt->input->cur - ctxt->input->base; xmlParserInputBufferPush(ctxt->input->buf, size, chunk); - ctxt->input->base = ctxt->input->buf->buffer->content + base; - ctxt->input->cur = ctxt->input->base + cur; - ctxt->input->end = - &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use]; + xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, base, cur); #ifdef DEBUG_PUSH xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size); #endif @@ -6537,6 +6651,14 @@ ctxt->options |= HTML_PARSE_NODEFDTD; options -= HTML_PARSE_NODEFDTD; } + if (options & HTML_PARSE_IGNORE_ENC) { + ctxt->options |= HTML_PARSE_IGNORE_ENC; + options -= HTML_PARSE_IGNORE_ENC; + } + if (options & HTML_PARSE_NOIMPLIED) { + ctxt->options |= HTML_PARSE_NOIMPLIED; + options -= HTML_PARSE_NOIMPLIED; + } ctxt->dictNames = 0; return (options); } @@ -6730,8 +6852,11 @@ input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, XML_CHAR_ENCODING_NONE); - if (input == NULL) + if (input == NULL) { + if (ioclose != NULL) + ioclose(ioctx); return (NULL); + } ctxt = htmlNewParserCtxt(); if (ctxt == NULL) { xmlFreeParserInputBuffer(input); @@ -6930,8 +7055,11 @@ input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, XML_CHAR_ENCODING_NONE); - if (input == NULL) + if (input == NULL) { + if (ioclose != NULL) + ioclose(ioctx); return (NULL); + } stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); if (stream == NULL) { xmlFreeParserInputBuffer(input); Index: lib/3rdparty/libxml2/HTMLtree.c =================================================================== --- lib/3rdparty/libxml2/HTMLtree.c (revision 58294) +++ lib/3rdparty/libxml2/HTMLtree.c (working copy) @@ -30,6 +30,8 @@ #include #include +#include "buf.h" + /************************************************************************ * * * Getting/Setting encoding meta tags * @@ -164,6 +166,7 @@ const xmlChar *content = NULL; char newcontent[100]; + newcontent[0] = 0; if (doc == NULL) return(-1); @@ -278,8 +281,13 @@ xmlNewProp(meta, BAD_CAST"content", BAD_CAST newcontent); } } else { + /* remove the meta tag if NULL is passed */ + if (encoding == NULL) { + xmlUnlinkNode(meta); + xmlFreeNode(meta); + } /* change the document only if there is a real encoding change */ - if (xmlStrcasestr(content, encoding) == NULL) { + else if (xmlStrcasestr(content, encoding) == NULL) { xmlSetProp(meta, BAD_CAST"content", BAD_CAST newcontent); } } @@ -385,13 +393,9 @@ * * ************************************************************************/ -static int -htmlNodeDumpFormat(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, - int format); - /** - * htmlNodeDumpFormat: - * @buf: the HTML buffer output + * htmlBufNodeDumpFormat: + * @buf: the xmlBufPtr output * @doc: the document * @cur: the current node * @format: should formatting spaces been added @@ -400,10 +404,10 @@ * * Returns the number of byte written or -1 in case of error */ -static int -htmlNodeDumpFormat(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, +static size_t +htmlBufNodeDumpFormat(xmlBufPtr buf, xmlDocPtr doc, xmlNodePtr cur, int format) { - unsigned int use; + size_t use; int ret; xmlOutputBufferPtr outbuf; @@ -426,10 +430,10 @@ outbuf->context = NULL; outbuf->written = 0; - use = buf->use; + use = xmlBufUse(buf); htmlNodeDumpFormatOutput(outbuf, doc, cur, NULL, format); xmlFree(outbuf); - ret = buf->use - use; + ret = xmlBufUse(buf) - use; return (ret); } @@ -446,9 +450,24 @@ */ int htmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur) { + xmlBufPtr buffer; + size_t ret; + + if ((buf == NULL) || (cur == NULL)) + return(-1); + xmlInitParser(); + buffer = xmlBufFromBuffer(buf); + if (buffer == NULL) + return(-1); - return(htmlNodeDumpFormat(buf, doc, cur, 1)); + ret = htmlBufNodeDumpFormat(buffer, doc, cur, 1); + + xmlBufBackToBuffer(buffer); + + if (ret > INT_MAX) + return(-1); + return((int) ret); } /** @@ -481,7 +500,7 @@ if (enc != XML_CHAR_ENCODING_UTF8) { handler = xmlFindCharEncodingHandler(encoding); if (handler == NULL) - return(-1); + htmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding); } } @@ -562,11 +581,9 @@ } handler = xmlFindCharEncodingHandler(encoding); - if (handler == NULL) { - *mem = NULL; - *size = 0; - return; - } + if (handler == NULL) + htmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding); + } else { handler = xmlFindCharEncodingHandler(encoding); } @@ -587,15 +604,15 @@ return; } - htmlDocContentDumpFormatOutput(buf, cur, NULL, format); + htmlDocContentDumpFormatOutput(buf, cur, NULL, format); xmlOutputBufferFlush(buf); if (buf->conv != NULL) { - *size = buf->conv->use; - *mem = xmlStrndup(buf->conv->content, *size); + *size = xmlBufUse(buf->conv); + *mem = xmlStrndup(xmlBufContent(buf->conv), *size); } else { - *size = buf->buffer->use; - *mem = xmlStrndup(buf->buffer->content, *size); + *size = xmlBufUse(buf->buffer); + *mem = xmlStrndup(xmlBufContent(buf->buffer), *size); } (void)xmlOutputBufferClose(buf); } @@ -646,14 +663,14 @@ xmlOutputBufferWriteString(buf, (const char *)cur->name); if (cur->ExternalID != NULL) { xmlOutputBufferWriteString(buf, " PUBLIC "); - xmlBufferWriteQuotedString(buf->buffer, cur->ExternalID); + xmlBufWriteQuotedString(buf->buffer, cur->ExternalID); if (cur->SystemID != NULL) { xmlOutputBufferWriteString(buf, " "); - xmlBufferWriteQuotedString(buf->buffer, cur->SystemID); + xmlBufWriteQuotedString(buf->buffer, cur->SystemID); } } else if (cur->SystemID != NULL) { xmlOutputBufferWriteString(buf, " SYSTEM "); - xmlBufferWriteQuotedString(buf->buffer, cur->SystemID); + xmlBufWriteQuotedString(buf->buffer, cur->SystemID); } xmlOutputBufferWriteString(buf, ">\n"); } @@ -673,9 +690,10 @@ xmlChar *value; /* - * TODO: The html output method should not escape a & character - * occurring in an attribute value immediately followed by - * a { character (see Section B.7.1 of the HTML 4.0 Recommendation). + * The html output method should not escape a & character + * occurring in an attribute value immediately followed by + * a { character (see Section B.7.1 of the HTML 4.0 Recommendation). + * This is implemented in xmlEncodeEntitiesReentrant */ if (cur == NULL) { @@ -703,15 +721,19 @@ while (IS_BLANK_CH(*tmp)) tmp++; - escaped = xmlURIEscapeStr(tmp, BAD_CAST"@/:=?;#%&,+"); + /* + * the < and > have already been escaped at the entity level + * And doing so here breaks server side includes + */ + escaped = xmlURIEscapeStr(tmp, BAD_CAST"@/:=?;#%&,+<>"); if (escaped != NULL) { - xmlBufferWriteQuotedString(buf->buffer, escaped); + xmlBufWriteQuotedString(buf->buffer, escaped); xmlFree(escaped); } else { - xmlBufferWriteQuotedString(buf->buffer, value); + xmlBufWriteQuotedString(buf->buffer, value); } } else { - xmlBufferWriteQuotedString(buf->buffer, value); + xmlBufWriteQuotedString(buf->buffer, value); } xmlFree(value); } else { @@ -1061,7 +1083,7 @@ handler = xmlFindCharEncodingHandler(encoding); if (handler == NULL) - return(-1); + htmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding); } else { handler = xmlFindCharEncodingHandler(encoding); } @@ -1120,7 +1142,7 @@ handler = xmlFindCharEncodingHandler(encoding); if (handler == NULL) - return(-1); + htmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding); } } @@ -1181,7 +1203,7 @@ handler = xmlFindCharEncodingHandler(encoding); if (handler == NULL) - return(-1); + htmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, NULL, encoding); } htmlSetMetaEncoding(cur, (const xmlChar *) encoding); } else { Index: lib/3rdparty/libxml2/include/wsockcompat.h =================================================================== --- lib/3rdparty/libxml2/include/wsockcompat.h (revision 58294) +++ lib/3rdparty/libxml2/include/wsockcompat.h (working copy) @@ -27,7 +27,7 @@ #endif #endif -#ifdef __MINGW32__ +#if defined( __MINGW32__ ) || defined( _MSC_VER ) /* Include here to ensure that it doesn't get included later * (e.g. by iconv.h) and overwrites the definition of EWOULDBLOCK. */ #include @@ -39,6 +39,9 @@ #endif #define EWOULDBLOCK WSAEWOULDBLOCK +#define ESHUTDOWN WSAESHUTDOWN + +#if (!defined(_MSC_VER) || (_MSC_VER < 1600)) || defined(__REACTOS__) #define EINPROGRESS WSAEINPROGRESS #define EALREADY WSAEALREADY #define ENOTSOCK WSAENOTSOCK @@ -61,7 +64,6 @@ #define ENOBUFS WSAENOBUFS #define EISCONN WSAEISCONN #define ENOTCONN WSAENOTCONN -#define ESHUTDOWN WSAESHUTDOWN #define ETOOMANYREFS WSAETOOMANYREFS #define ETIMEDOUT WSAETIMEDOUT #define ECONNREFUSED WSAECONNREFUSED @@ -73,11 +75,12 @@ #define EDQUOT WSAEDQUOT #define ESTALE WSAESTALE #define EREMOTE WSAEREMOTE -/* These cause conflicts with the codes from errno.h. Since they are - not used in the relevant code (nanoftp, nanohttp), we can leave +/* These cause conflicts with the codes from errno.h. Since they are + not used in the relevant code (nanoftp, nanohttp), we can leave them disabled. #define ENAMETOOLONG WSAENAMETOOLONG #define ENOTEMPTY WSAENOTEMPTY */ +#endif /* _MSC_VER */ #endif /* __XML_WSOCKCOMPAT_H__ */ Index: lib/3rdparty/libxml2/libxml.h =================================================================== --- lib/3rdparty/libxml2/libxml.h (revision 58293) +++ lib/3rdparty/libxml2/libxml.h (working copy) @@ -29,6 +29,11 @@ #include #include #else +/* + * Currently supported platforms use either autoconf or + * copy to config.h own "preset" configuration file. + * As result ifdef HAVE_CONFIG_H is omited here. + */ #include "config.h" #include #endif @@ -79,6 +84,13 @@ void __xmlGlobalInitMutexUnlock(void); void __xmlGlobalInitMutexDestroy(void); +#if defined(HAVE_RAND) && defined(HAVE_SRAND) && defined(HAVE_TIME) +/* + * internal thread safe random function + */ +int __xmlRandom(void); +#endif + #ifdef IN_LIBXML #ifdef __GNUC__ #ifdef PIC @@ -90,4 +102,7 @@ #endif #endif #endif +#if !defined(PIC) && !defined(NOLIBTOOL) && !defined(LIBXML_STATIC) +# define LIBXML_STATIC +#endif #endif /* ! __XML_LIBXML_H__ */ Index: lib/3rdparty/libxml2/nanoftp.c =================================================================== --- lib/3rdparty/libxml2/nanoftp.c (revision 58294) +++ lib/3rdparty/libxml2/nanoftp.c (working copy) @@ -947,7 +947,8 @@ ((struct sockaddr_in *)&ctxt->ftpAddr)->sin_family = AF_INET; memcpy (&((struct sockaddr_in *)&ctxt->ftpAddr)->sin_addr, hp->h_addr_list[0], hp->h_length); - ((struct sockaddr_in *)&ctxt->ftpAddr)->sin_port = (u_short)htons ((unsigned short)port); + ((struct sockaddr_in *)&ctxt->ftpAddr)->sin_port = + (unsigned short)htons ((unsigned short)port); ctxt->controlFd = socket (AF_INET, SOCK_STREAM, 0); addrlen = sizeof (struct sockaddr_in); } Index: lib/3rdparty/libxml2/nanohttp.c =================================================================== --- lib/3rdparty/libxml2/nanohttp.c (revision 58294) +++ lib/3rdparty/libxml2/nanohttp.c (working copy) @@ -64,9 +64,6 @@ #ifdef HAVE_STRINGS_H #include #endif -#ifdef SUPPORT_IP6 -#include -#endif #ifdef HAVE_ZLIB_H #include #endif @@ -1140,10 +1137,12 @@ "Non-recoverable errors: FORMERR, REFUSED, or NOTIMP."; break; +#ifdef NO_ADDRESS case NO_ADDRESS: h_err_txt = "Valid name, no data record of requested type."; break; +#endif default: h_err_txt = "No error text defined."; @@ -1166,7 +1165,7 @@ memcpy (&ia, h->h_addr_list[i], h->h_length); sockin.sin_family = h->h_addrtype; sockin.sin_addr = ia; - sockin.sin_port = (u_short)htons ((unsigned short)port); + sockin.sin_port = (unsigned short)htons ((unsigned short)port); addr = (struct sockaddr *) &sockin; #ifdef SUPPORT_IP6 } else if (have_ipv6 () && (h->h_addrtype == AF_INET6)) { @@ -1615,6 +1614,7 @@ char *buf = NULL; int fd; int len; + int ret = 0; if (filename == NULL) return(-1); ctxt = xmlNanoHTTPOpen(URL, contentType); @@ -1636,12 +1636,14 @@ xmlNanoHTTPFetchContent( ctxt, &buf, &len ); if ( len > 0 ) { - write(fd, buf, len); + if (write(fd, buf, len) == -1) { + ret = -1; + } } xmlNanoHTTPClose(ctxt); close(fd); - return(0); + return(ret); } #ifdef LIBXML_OUTPUT_ENABLED @@ -1660,6 +1662,7 @@ char *buf = NULL; int fd; int len; + int ret = 0; if ((ctxt == NULL) || (filename == NULL)) return(-1); @@ -1675,12 +1678,14 @@ xmlNanoHTTPFetchContent( ctxt, &buf, &len ); if ( len > 0 ) { - write(fd, buf, len); + if (write(fd, buf, len) == -1) { + ret = -1; + } } xmlNanoHTTPClose(ctxt); close(fd); - return(0); + return(ret); } #endif /* LIBXML_OUTPUT_ENABLED */ Index: lib/3rdparty/libxml2/parser.c =================================================================== --- lib/3rdparty/libxml2/parser.c (revision 58294) +++ lib/3rdparty/libxml2/parser.c (working copy) @@ -40,6 +40,7 @@ #endif #include +#include #include #include #include @@ -79,7 +80,13 @@ #ifdef HAVE_ZLIB_H #include #endif +#ifdef HAVE_LZMA_H +#include +#endif +#include "buf.h" +#include "enc.h" + static void xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *info); @@ -114,10 +121,10 @@ * parser option. */ static int -xmlParserEntityCheck(xmlParserCtxtPtr ctxt, unsigned long size, +xmlParserEntityCheck(xmlParserCtxtPtr ctxt, size_t size, xmlEntityPtr ent) { - unsigned long consumed = 0; + size_t consumed = 0; if ((ctxt == NULL) || (ctxt->options & XML_PARSE_HUGE)) return (0); @@ -191,12 +198,24 @@ #define XML_PARSER_BUFFER_SIZE 100 #define SAX_COMPAT_MODE BAD_CAST "SAX compatibility mode document" +/** + * XML_PARSER_CHUNK_SIZE + * + * When calling GROW that's the minimal amount of data + * the parser expected to have received. It is not a hard + * limit but an optimization when reading strings like Names + * It is not strictly needed as long as inputs available characters + * are followed by 0, which should be provided by the I/O level + */ +#define XML_PARSER_CHUNK_SIZE 100 + /* * List of XML prefixed PI allowed by W3C specs */ static const char *xmlW3CPIs[] = { "xml-stylesheet", + "xml-model", NULL }; @@ -281,193 +300,201 @@ xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *info) { const char *errmsg; + char errstr[129] = ""; if ((ctxt != NULL) && (ctxt->disableSAX != 0) && (ctxt->instate == XML_PARSER_EOF)) return; switch (error) { case XML_ERR_INVALID_HEX_CHARREF: - errmsg = "CharRef: invalid hexadecimal value\n"; + errmsg = "CharRef: invalid hexadecimal value"; break; case XML_ERR_INVALID_DEC_CHARREF: - errmsg = "CharRef: invalid decimal value\n"; + errmsg = "CharRef: invalid decimal value"; break; case XML_ERR_INVALID_CHARREF: - errmsg = "CharRef: invalid value\n"; + errmsg = "CharRef: invalid value"; break; case XML_ERR_INTERNAL_ERROR: errmsg = "internal error"; break; case XML_ERR_PEREF_AT_EOF: - errmsg = "PEReference at end of document\n"; + errmsg = "PEReference at end of document"; break; case XML_ERR_PEREF_IN_PROLOG: - errmsg = "PEReference in prolog\n"; + errmsg = "PEReference in prolog"; break; case XML_ERR_PEREF_IN_EPILOG: - errmsg = "PEReference in epilog\n"; + errmsg = "PEReference in epilog"; break; case XML_ERR_PEREF_NO_NAME: - errmsg = "PEReference: no name\n"; + errmsg = "PEReference: no name"; break; case XML_ERR_PEREF_SEMICOL_MISSING: - errmsg = "PEReference: expecting ';'\n"; + errmsg = "PEReference: expecting ';'"; break; case XML_ERR_ENTITY_LOOP: - errmsg = "Detected an entity reference loop\n"; + errmsg = "Detected an entity reference loop"; break; case XML_ERR_ENTITY_NOT_STARTED: - errmsg = "EntityValue: \" or ' expected\n"; + errmsg = "EntityValue: \" or ' expected"; break; case XML_ERR_ENTITY_PE_INTERNAL: - errmsg = "PEReferences forbidden in internal subset\n"; + errmsg = "PEReferences forbidden in internal subset"; break; case XML_ERR_ENTITY_NOT_FINISHED: - errmsg = "EntityValue: \" or ' expected\n"; + errmsg = "EntityValue: \" or ' expected"; break; case XML_ERR_ATTRIBUTE_NOT_STARTED: - errmsg = "AttValue: \" or ' expected\n"; + errmsg = "AttValue: \" or ' expected"; break; case XML_ERR_LT_IN_ATTRIBUTE: - errmsg = "Unescaped '<' not allowed in attributes values\n"; + errmsg = "Unescaped '<' not allowed in attributes values"; break; case XML_ERR_LITERAL_NOT_STARTED: - errmsg = "SystemLiteral \" or ' expected\n"; + errmsg = "SystemLiteral \" or ' expected"; break; case XML_ERR_LITERAL_NOT_FINISHED: - errmsg = "Unfinished System or Public ID \" or ' expected\n"; + errmsg = "Unfinished System or Public ID \" or ' expected"; break; case XML_ERR_MISPLACED_CDATA_END: - errmsg = "Sequence ']]>' not allowed in content\n"; + errmsg = "Sequence ']]>' not allowed in content"; break; case XML_ERR_URI_REQUIRED: - errmsg = "SYSTEM or PUBLIC, the URI is missing\n"; + errmsg = "SYSTEM or PUBLIC, the URI is missing"; break; case XML_ERR_PUBID_REQUIRED: - errmsg = "PUBLIC, the Public Identifier is missing\n"; + errmsg = "PUBLIC, the Public Identifier is missing"; break; case XML_ERR_HYPHEN_IN_COMMENT: - errmsg = "Comment must not contain '--' (double-hyphen)\n"; + errmsg = "Comment must not contain '--' (double-hyphen)"; break; case XML_ERR_PI_NOT_STARTED: - errmsg = "xmlParsePI : no target name\n"; + errmsg = "xmlParsePI : no target name"; break; case XML_ERR_RESERVED_XML_NAME: - errmsg = "Invalid PI name\n"; + errmsg = "Invalid PI name"; break; case XML_ERR_NOTATION_NOT_STARTED: - errmsg = "NOTATION: Name expected here\n"; + errmsg = "NOTATION: Name expected here"; break; case XML_ERR_NOTATION_NOT_FINISHED: - errmsg = "'>' required to close NOTATION declaration\n"; + errmsg = "'>' required to close NOTATION declaration"; break; case XML_ERR_VALUE_REQUIRED: - errmsg = "Entity value required\n"; + errmsg = "Entity value required"; break; case XML_ERR_URI_FRAGMENT: errmsg = "Fragment not allowed"; break; case XML_ERR_ATTLIST_NOT_STARTED: - errmsg = "'(' required to start ATTLIST enumeration\n"; + errmsg = "'(' required to start ATTLIST enumeration"; break; case XML_ERR_NMTOKEN_REQUIRED: - errmsg = "NmToken expected in ATTLIST enumeration\n"; + errmsg = "NmToken expected in ATTLIST enumeration"; break; case XML_ERR_ATTLIST_NOT_FINISHED: - errmsg = "')' required to finish ATTLIST enumeration\n"; + errmsg = "')' required to finish ATTLIST enumeration"; break; case XML_ERR_MIXED_NOT_STARTED: - errmsg = "MixedContentDecl : '|' or ')*' expected\n"; + errmsg = "MixedContentDecl : '|' or ')*' expected"; break; case XML_ERR_PCDATA_REQUIRED: - errmsg = "MixedContentDecl : '#PCDATA' expected\n"; + errmsg = "MixedContentDecl : '#PCDATA' expected"; break; case XML_ERR_ELEMCONTENT_NOT_STARTED: - errmsg = "ContentDecl : Name or '(' expected\n"; + errmsg = "ContentDecl : Name or '(' expected"; break; case XML_ERR_ELEMCONTENT_NOT_FINISHED: - errmsg = "ContentDecl : ',' '|' or ')' expected\n"; + errmsg = "ContentDecl : ',' '|' or ')' expected"; break; case XML_ERR_PEREF_IN_INT_SUBSET: errmsg = - "PEReference: forbidden within markup decl in internal subset\n"; + "PEReference: forbidden within markup decl in internal subset"; break; case XML_ERR_GT_REQUIRED: - errmsg = "expected '>'\n"; + errmsg = "expected '>'"; break; case XML_ERR_CONDSEC_INVALID: - errmsg = "XML conditional section '[' expected\n"; + errmsg = "XML conditional section '[' expected"; break; case XML_ERR_EXT_SUBSET_NOT_FINISHED: - errmsg = "Content error in the external subset\n"; + errmsg = "Content error in the external subset"; break; case XML_ERR_CONDSEC_INVALID_KEYWORD: errmsg = - "conditional section INCLUDE or IGNORE keyword expected\n"; + "conditional section INCLUDE or IGNORE keyword expected"; break; case XML_ERR_CONDSEC_NOT_FINISHED: - errmsg = "XML conditional section not closed\n"; + errmsg = "XML conditional section not closed"; break; case XML_ERR_XMLDECL_NOT_STARTED: - errmsg = "Text declaration '' expected\n"; + errmsg = "parsing XML declaration: '?>' expected"; break; case XML_ERR_EXT_ENTITY_STANDALONE: - errmsg = "external parsed entities cannot be standalone\n"; + errmsg = "external parsed entities cannot be standalone"; break; case XML_ERR_ENTITYREF_SEMICOL_MISSING: - errmsg = "EntityRef: expecting ';'\n"; + errmsg = "EntityRef: expecting ';'"; break; case XML_ERR_DOCTYPE_NOT_FINISHED: - errmsg = "DOCTYPE improperly terminated\n"; + errmsg = "DOCTYPE improperly terminated"; break; case XML_ERR_LTSLASH_REQUIRED: - errmsg = "EndTag: 'errNo = error; __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error, - XML_ERR_FATAL, NULL, 0, info, NULL, NULL, 0, 0, errmsg, + XML_ERR_FATAL, NULL, 0, info, NULL, NULL, 0, 0, &errstr[0], info); if (ctxt != NULL) { ctxt->wellFormed = 0; @@ -731,7 +758,7 @@ * @info1: extra information string * @info2: extra information string * - * Handle a fatal parser error, i.e. violating Well-Formedness constraints + * Handle a namespace warning error */ static void xmlNsWarn(xmlParserCtxtPtr ctxt, xmlParserErrors error, @@ -954,6 +981,12 @@ #else return(0); #endif + case XML_WITH_LZMA: +#ifdef LIBXML_LZMA_ENABLED + return(1); +#else + return(0); +#endif case XML_WITH_ICU: #ifdef LIBXML_ICU_ENABLED return(1); @@ -1819,15 +1852,14 @@ if (ctxt->nameNr >= ctxt->nameMax) { const xmlChar * *tmp; - ctxt->nameMax *= 2; tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab, - ctxt->nameMax * + ctxt->nameMax * 2 * sizeof(ctxt->nameTab[0])); if (tmp == NULL) { - ctxt->nameMax /= 2; goto mem_error; } ctxt->nameTab = tmp; + ctxt->nameMax *= 2; } ctxt->nameTab[ctxt->nameNr] = value; ctxt->name = value; @@ -1991,6 +2023,12 @@ xmlGROW (ctxt); static void xmlGROW (xmlParserCtxtPtr ctxt) { + if ((((ctxt->input->end - ctxt->input->cur) > XML_MAX_LOOKUP_LIMIT) || + ((ctxt->input->cur - ctxt->input->base) > XML_MAX_LOOKUP_LIMIT)) && + ((ctxt->options & XML_PARSE_HUGE) == 0)) { + xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, "Huge input lookup"); + ctxt->instate = XML_PARSER_EOF; + } xmlParserInputGrow(ctxt->input, INPUT_CHUNK); if ((ctxt->input->cur != NULL) && (*ctxt->input->cur == 0) && (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) @@ -2135,6 +2173,8 @@ "Pushing input %d : %.30s\n", ctxt->inputNr+1, input->cur); } ret = inputPush(ctxt, input); + if (ctxt->instate == XML_PARSER_EOF) + return(-1); GROW; return(ret); } @@ -2171,6 +2211,8 @@ if (count++ > 20) { count = 0; GROW; + if (ctxt->instate == XML_PARSER_EOF) + return(0); } if ((RAW >= '0') && (RAW <= '9')) val = val * 16 + (CUR - '0'); @@ -2202,6 +2244,8 @@ if (count++ > 20) { count = 0; GROW; + if (ctxt->instate == XML_PARSER_EOF) + return(0); } if ((RAW >= '0') && (RAW <= '9')) val = val * 10 + (CUR - '0'); @@ -2550,6 +2594,8 @@ * the amount of data in the buffer. */ GROW + if (ctxt->instate == XML_PARSER_EOF) + return; if ((ctxt->input->end - ctxt->input->cur)>=4) { start[0] = RAW; start[1] = NXT(1); @@ -2580,15 +2626,17 @@ /* * Macro used to grow the current buffer. + * buffer##_size is expected to be a size_t + * mem_error: is expected to handle memory allocation failures */ #define growBuffer(buffer, n) { \ xmlChar *tmp; \ - buffer##_size *= 2; \ - buffer##_size += n; \ - tmp = (xmlChar *) \ - xmlRealloc(buffer, buffer##_size * sizeof(xmlChar)); \ + size_t new_size = buffer##_size * 2 + n; \ + if (new_size < buffer##_size) goto mem_error; \ + tmp = (xmlChar *) xmlRealloc(buffer, new_size); \ if (tmp == NULL) goto mem_error; \ buffer = tmp; \ + buffer##_size = new_size; \ } /** @@ -2614,14 +2662,14 @@ xmlStringLenDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int len, int what, xmlChar end, xmlChar end2, xmlChar end3) { xmlChar *buffer = NULL; - int buffer_size = 0; + size_t buffer_size = 0; + size_t nbchars = 0; xmlChar *current = NULL; xmlChar *rep = NULL; const xmlChar *last; xmlEntityPtr ent; int c,l; - int nbchars = 0; if ((ctxt == NULL) || (str == NULL) || (len < 0)) return(NULL); @@ -2638,7 +2686,7 @@ * allocate a translation buffer. */ buffer_size = XML_PARSER_BIG_BUFFER_SIZE; - buffer = (xmlChar *) xmlMallocAtomic(buffer_size * sizeof(xmlChar)); + buffer = (xmlChar *) xmlMallocAtomic(buffer_size); if (buffer == NULL) goto mem_error; /* @@ -2658,7 +2706,7 @@ if (val != 0) { COPY_BUF(0,buffer,nbchars,val); } - if (nbchars > buffer_size - XML_PARSER_BUFFER_SIZE) { + if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { growBuffer(buffer, XML_PARSER_BUFFER_SIZE); } } else if ((c == '&') && (what & XML_SUBSTITUTE_REF)) { @@ -2676,7 +2724,7 @@ (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { if (ent->content != NULL) { COPY_BUF(0,buffer,nbchars,ent->content[0]); - if (nbchars > buffer_size - XML_PARSER_BUFFER_SIZE) { + if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { growBuffer(buffer, XML_PARSER_BUFFER_SIZE); } } else { @@ -2693,8 +2741,7 @@ current = rep; while (*current != 0) { /* non input consuming loop */ buffer[nbchars++] = *current++; - if (nbchars > - buffer_size - XML_PARSER_BUFFER_SIZE) { + if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { if (xmlParserEntityCheck(ctxt, nbchars, ent)) goto int_error; growBuffer(buffer, XML_PARSER_BUFFER_SIZE); @@ -2708,8 +2755,8 @@ const xmlChar *cur = ent->name; buffer[nbchars++] = '&'; - if (nbchars > buffer_size - i - XML_PARSER_BUFFER_SIZE) { - growBuffer(buffer, XML_PARSER_BUFFER_SIZE); + if (nbchars + i + XML_PARSER_BUFFER_SIZE > buffer_size) { + growBuffer(buffer, i + XML_PARSER_BUFFER_SIZE); } for (;i > 0;i--) buffer[nbchars++] = *cur++; @@ -2736,8 +2783,7 @@ current = rep; while (*current != 0) { /* non input consuming loop */ buffer[nbchars++] = *current++; - if (nbchars > - buffer_size - XML_PARSER_BUFFER_SIZE) { + if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { if (xmlParserEntityCheck(ctxt, nbchars, ent)) goto int_error; growBuffer(buffer, XML_PARSER_BUFFER_SIZE); @@ -2750,8 +2796,8 @@ } else { COPY_BUF(l,buffer,nbchars,c); str += l; - if (nbchars > buffer_size - XML_PARSER_BUFFER_SIZE) { - growBuffer(buffer, XML_PARSER_BUFFER_SIZE); + if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { + growBuffer(buffer, XML_PARSER_BUFFER_SIZE); } } if (str < last) @@ -3168,6 +3214,8 @@ * Handler for more complex cases */ GROW; + if (ctxt->instate == XML_PARSER_EOF) + return(NULL); c = CUR_CHAR(l); if ((ctxt->options & XML_PARSE_OLD10) == 0) { /* @@ -3216,9 +3264,11 @@ ((c >= 0xFDF0) && (c <= 0xFFFD)) || ((c >= 0x10000) && (c <= 0xEFFFF)) )) { - if (count++ > 100) { + if (count++ > XML_PARSER_CHUNK_SIZE) { count = 0; GROW; + if (ctxt->instate == XML_PARSER_EOF) + return(NULL); } len += l; NEXTL(l); @@ -3240,15 +3290,29 @@ (c == '_') || (c == ':') || (IS_COMBINING(c)) || (IS_EXTENDER(c)))) { - if (count++ > 100) { + if (count++ > XML_PARSER_CHUNK_SIZE) { count = 0; GROW; + if (ctxt->instate == XML_PARSER_EOF) + return(NULL); } len += l; NEXTL(l); c = CUR_CHAR(l); + if (c == 0) { + count = 0; + GROW; + if (ctxt->instate == XML_PARSER_EOF) + return(NULL); + c = CUR_CHAR(l); + } } } + if ((len > XML_MAX_NAME_LENGTH) && + ((ctxt->options & XML_PARSE_HUGE) == 0)) { + xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Name"); + return(NULL); + } if ((*ctxt->input->cur == '\n') && (ctxt->input->cur[-1] == '\r')) return(xmlDictLookup(ctxt->dict, ctxt->input->cur - (len + 1), len)); return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len)); @@ -3298,6 +3362,11 @@ in++; if ((*in > 0) && (*in < 0x80)) { count = in - ctxt->input->cur; + if ((count > XML_MAX_NAME_LENGTH) && + ((ctxt->options & XML_PARSE_HUGE) == 0)) { + xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Name"); + return(NULL); + } ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count); ctxt->input->cur = in; ctxt->nbChars += count; @@ -3333,14 +3402,33 @@ while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ (xmlIsNameChar(ctxt, c) && (c != ':'))) { - if (count++ > 100) { + if (count++ > XML_PARSER_CHUNK_SIZE) { + if ((len > XML_MAX_NAME_LENGTH) && + ((ctxt->options & XML_PARSE_HUGE) == 0)) { + xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName"); + return(NULL); + } count = 0; GROW; + if (ctxt->instate == XML_PARSER_EOF) + return(NULL); } len += l; NEXTL(l); c = CUR_CHAR(l); + if (c == 0) { + count = 0; + GROW; + if (ctxt->instate == XML_PARSER_EOF) + return(NULL); + c = CUR_CHAR(l); + } } + if ((len > XML_MAX_NAME_LENGTH) && + ((ctxt->options & XML_PARSE_HUGE) == 0)) { + xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName"); + return(NULL); + } return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len)); } @@ -3385,6 +3473,11 @@ in++; if ((*in > 0) && (*in < 0x80)) { count = in - ctxt->input->cur; + if ((count > XML_MAX_NAME_LENGTH) && + ((ctxt->options & XML_PARSE_HUGE) == 0)) { + xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName"); + return(NULL); + } ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count); ctxt->input->cur = in; ctxt->nbChars += count; @@ -3416,6 +3509,8 @@ const xmlChar *ret; GROW; + if (ctxt->instate == XML_PARSER_EOF) + return(NULL); in = ctxt->input->cur; while (*in != 0 && *in == *cmp) { @@ -3495,6 +3590,13 @@ while (xmlIsNameChar(ctxt, c)) { if (len + 10 > max) { xmlChar *tmp; + + if ((len > XML_MAX_NAME_LENGTH) && + ((ctxt->options & XML_PARSE_HUGE) == 0)) { + xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName"); + xmlFree(buffer); + return(NULL); + } max *= 2; tmp = (xmlChar *) xmlRealloc(buffer, max * sizeof(xmlChar)); @@ -3514,6 +3616,11 @@ return(buffer); } } + if ((len > XML_MAX_NAME_LENGTH) && + ((ctxt->options & XML_PARSE_HUGE) == 0)) { + xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName"); + return(NULL); + } *str = cur; return(xmlStrndup(buf, len)); } @@ -3543,16 +3650,25 @@ #endif GROW; + if (ctxt->instate == XML_PARSER_EOF) + return(NULL); c = CUR_CHAR(l); while (xmlIsNameChar(ctxt, c)) { - if (count++ > 100) { + if (count++ > XML_PARSER_CHUNK_SIZE) { count = 0; GROW; } COPY_BUF(l,buf,len,c); NEXTL(l); c = CUR_CHAR(l); + if (c == 0) { + count = 0; + GROW; + if (ctxt->instate == XML_PARSER_EOF) + return(NULL); + c = CUR_CHAR(l); + } if (len >= XML_MAX_NAMELEN) { /* * Okay someone managed to make a huge token, so he's ready to pay @@ -3568,13 +3684,23 @@ } memcpy(buffer, buf, len); while (xmlIsNameChar(ctxt, c)) { - if (count++ > 100) { + if (count++ > XML_PARSER_CHUNK_SIZE) { count = 0; GROW; + if (ctxt->instate == XML_PARSER_EOF) { + xmlFree(buffer); + return(NULL); + } } if (len + 10 > max) { xmlChar *tmp; + if ((max > XML_MAX_NAME_LENGTH) && + ((ctxt->options & XML_PARSE_HUGE) == 0)) { + xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NmToken"); + xmlFree(buffer); + return(NULL); + } max *= 2; tmp = (xmlChar *) xmlRealloc(buffer, max * sizeof(xmlChar)); @@ -3595,6 +3721,11 @@ } if (len == 0) return(NULL); + if ((len > XML_MAX_NAME_LENGTH) && + ((ctxt->options & XML_PARSE_HUGE) == 0)) { + xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NmToken"); + return(NULL); + } return(xmlStrndup(buf, len)); } @@ -3641,6 +3772,10 @@ ctxt->instate = XML_PARSER_ENTITY_VALUE; input = ctxt->input; GROW; + if (ctxt->instate == XML_PARSER_EOF) { + xmlFree(buf); + return(NULL); + } NEXT; c = CUR_CHAR(l); /* @@ -3652,8 +3787,8 @@ * In practice it means we stop the loop only when back at parsing * the initial entity and the quote is found */ - while ((IS_CHAR(c)) && ((c != stop) || /* checked */ - (ctxt->input != input))) { + while (((IS_CHAR(c)) && ((c != stop) || /* checked */ + (ctxt->input != input))) && (ctxt->instate != XML_PARSER_EOF)) { if (len + 5 >= size) { xmlChar *tmp; @@ -3682,6 +3817,10 @@ } } buf[len] = 0; + if (ctxt->instate == XML_PARSER_EOF) { + xmlFree(buf); + return(NULL); + } /* * Raise problem w.r.t. '&' and '%' being used in non-entities @@ -3755,8 +3894,8 @@ xmlChar limit = 0; xmlChar *buf = NULL; xmlChar *rep = NULL; - int len = 0; - int buf_size = 0; + size_t len = 0; + size_t buf_size = 0; int c, l, in_space = 0; xmlChar *current = NULL; xmlEntityPtr ent; @@ -3778,15 +3917,26 @@ * allocate a translation buffer. */ buf_size = XML_PARSER_BUFFER_SIZE; - buf = (xmlChar *) xmlMallocAtomic(buf_size * sizeof(xmlChar)); + buf = (xmlChar *) xmlMallocAtomic(buf_size); if (buf == NULL) goto mem_error; /* * OK loop until we reach one of the ending char or a size limit. */ c = CUR_CHAR(l); - while ((NXT(0) != limit) && /* checked */ - (IS_CHAR(c)) && (c != '<')) { + while (((NXT(0) != limit) && /* checked */ + (IS_CHAR(c)) && (c != '<')) && + (ctxt->instate != XML_PARSER_EOF)) { + /* + * Impose a reasonable limit on attribute size, unless XML_PARSE_HUGE + * special option is given + */ + if ((len > XML_MAX_TEXT_LENGTH) && + ((ctxt->options & XML_PARSE_HUGE) == 0)) { + xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, + "AttValue lenght too long\n"); + goto mem_error; + } if (c == 0) break; if (c == '&') { in_space = 0; @@ -3795,7 +3945,7 @@ if (val == '&') { if (ctxt->replaceEntities) { - if (len > buf_size - 10) { + if (len + 10 > buf_size) { growBuffer(buf, 10); } buf[len++] = '&'; @@ -3804,7 +3954,7 @@ * The reparsing will be done in xmlStringGetNodeList() * called by the attribute() function in SAX.c */ - if (len > buf_size - 10) { + if (len + 10 > buf_size) { growBuffer(buf, 10); } buf[len++] = '&'; @@ -3814,7 +3964,7 @@ buf[len++] = ';'; } } else if (val != 0) { - if (len > buf_size - 10) { + if (len + 10 > buf_size) { growBuffer(buf, 10); } len += xmlCopyChar(0, &buf[len], val); @@ -3826,7 +3976,7 @@ ctxt->nbentities += ent->owner; if ((ent != NULL) && (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { - if (len > buf_size - 10) { + if (len + 10 > buf_size) { growBuffer(buf, 10); } if ((ctxt->replaceEntities == 0) && @@ -3854,7 +4004,7 @@ current++; } else buf[len++] = *current++; - if (len > buf_size - 10) { + if (len + 10 > buf_size) { growBuffer(buf, 10); } } @@ -3862,7 +4012,7 @@ rep = NULL; } } else { - if (len > buf_size - 10) { + if (len + 10 > buf_size) { growBuffer(buf, 10); } if (ent->content != NULL) @@ -3890,7 +4040,7 @@ * Just output the reference */ buf[len++] = '&'; - while (len > buf_size - i - 10) { + while (len + i + 10 > buf_size) { growBuffer(buf, i + 10); } for (;i > 0;i--) @@ -3903,7 +4053,7 @@ if ((len != 0) || (!normalize)) { if ((!normalize) || (!in_space)) { COPY_BUF(l,buf,len,0x20); - while (len > buf_size - 10) { + while (len + 10 > buf_size) { growBuffer(buf, 10); } } @@ -3912,7 +4062,7 @@ } else { in_space = 0; COPY_BUF(l,buf,len,c); - if (len > buf_size - 10) { + if (len + 10 > buf_size) { growBuffer(buf, 10); } } @@ -3921,6 +4071,9 @@ GROW; c = CUR_CHAR(l); } + if (ctxt->instate == XML_PARSER_EOF) + goto error; + if ((in_space) && (normalize)) { while (buf[len - 1] == 0x20) len--; } @@ -3937,11 +4090,23 @@ } } else NEXT; - if (attlen != NULL) *attlen = len; + + /* + * There we potentially risk an overflow, don't allow attribute value of + * lenght more than INT_MAX it is a very reasonnable assumption ! + */ + if (len >= INT_MAX) { + xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, + "AttValue lenght too long\n"); + goto mem_error; + } + + if (attlen != NULL) *attlen = (int) len; return(buf); mem_error: xmlErrMemory(ctxt, NULL); +error: if (buf != NULL) xmlFree(buf); if (rep != NULL) @@ -4033,6 +4198,13 @@ if (len + 5 >= size) { xmlChar *tmp; + if ((size > XML_MAX_NAME_LENGTH) && + ((ctxt->options & XML_PARSE_HUGE) == 0)) { + xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "SystemLiteral"); + xmlFree(buf); + ctxt->instate = (xmlParserInputState) state; + return(NULL); + } size *= 2; tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); if (tmp == NULL) { @@ -4047,6 +4219,10 @@ if (count > 50) { GROW; count = 0; + if (ctxt->instate == XML_PARSER_EOF) { + xmlFree(buf); + return(NULL); + } } COPY_BUF(l,buf,len,cur); NEXTL(l); @@ -4110,6 +4286,12 @@ if (len + 1 >= size) { xmlChar *tmp; + if ((size > XML_MAX_NAME_LENGTH) && + ((ctxt->options & XML_PARSE_HUGE) == 0)) { + xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Public ID"); + xmlFree(buf); + return(NULL); + } size *= 2; tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); if (tmp == NULL) { @@ -4124,6 +4306,10 @@ if (count > 50) { GROW; count = 0; + if (ctxt->instate == XML_PARSER_EOF) { + xmlFree(buf); + return(NULL); + } } NEXT; cur = CUR; @@ -4330,6 +4516,8 @@ } SHRINK; GROW; + if (ctxt->instate == XML_PARSER_EOF) + return; in = ctxt->input->cur; } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09)); nbchar = 0; @@ -4398,6 +4586,8 @@ if (count > 50) { GROW; count = 0; + if (ctxt->instate == XML_PARSER_EOF) + return; } NEXTL(l); cur = CUR_CHAR(l); @@ -4527,11 +4717,12 @@ * [15] Comment ::= '' */ static void -xmlParseCommentComplex(xmlParserCtxtPtr ctxt, xmlChar *buf, int len, int size) { +xmlParseCommentComplex(xmlParserCtxtPtr ctxt, xmlChar *buf, + size_t len, size_t size) { int q, ql; int r, rl; int cur, l; - int count = 0; + size_t count = 0; int inputid; inputid = ctxt->input->id; @@ -4577,16 +4768,26 @@ if ((r == '-') && (q == '-')) { xmlFatalErr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, NULL); } + if ((len > XML_MAX_TEXT_LENGTH) && + ((ctxt->options & XML_PARSE_HUGE) == 0)) { + xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, + "Comment too big found", NULL); + xmlFree (buf); + return; + } if (len + 5 >= size) { xmlChar *new_buf; - size *= 2; - new_buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); + size_t new_size; + + new_size = size * 2; + new_buf = (xmlChar *) xmlRealloc(buf, new_size); if (new_buf == NULL) { xmlFree (buf); xmlErrMemory(ctxt, NULL); return; } buf = new_buf; + size = new_size; } COPY_BUF(ql,buf,len,q); q = r; @@ -4598,6 +4799,10 @@ if (count > 50) { GROW; count = 0; + if (ctxt->instate == XML_PARSER_EOF) { + xmlFree(buf); + return; + } } NEXTL(l); cur = CUR_CHAR(l); @@ -4647,11 +4852,12 @@ void xmlParseComment(xmlParserCtxtPtr ctxt) { xmlChar *buf = NULL; - int size = XML_PARSER_BUFFER_SIZE; - int len = 0; + size_t size = XML_PARSER_BUFFER_SIZE; + size_t len = 0; xmlParserInputState state; const xmlChar *in; - int nbchar = 0, ccol; + size_t nbchar = 0; + int ccol; int inputid; /* @@ -4731,6 +4937,13 @@ buf[len] = 0; } } + if ((len > XML_MAX_TEXT_LENGTH) && + ((ctxt->options & XML_PARSE_HUGE) == 0)) { + xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, + "Comment too big found", NULL); + xmlFree (buf); + return; + } ctxt->input->cur = in; if (*in == 0xA) { in++; @@ -4748,6 +4961,10 @@ } SHRINK; GROW; + if (ctxt->instate == XML_PARSER_EOF) { + xmlFree(buf); + return; + } in = ctxt->input->cur; if (*in == '-') { if (in[1] == '-') { @@ -4769,13 +4986,14 @@ ctxt->instate = state; return; } - if (buf != NULL) - xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, - "Comment not terminated \n" }, + { "Huge PI node", "huge:piNode", "" }, +}; + +static const char *current; +static int rlen; +static unsigned int currentTest = 0; +static int instate = 0; + +/** + * hugeMatch: + * @URI: an URI to test + * + * Check for an huge: query + * + * Returns 1 if yes and 0 if another Input module should be used + */ +static int +hugeMatch(const char * URI) { + if ((URI != NULL) && (!strncmp(URI, "huge:", 5))) + return(1); + return(0); +} + +/** + * hugeOpen: + * @URI: an URI to test + * + * Return a pointer to the huge: query handler, in this example simply + * the current pointer... + * + * Returns an Input context or NULL in case or error + */ +static void * +hugeOpen(const char * URI) { + if ((URI == NULL) || (strncmp(URI, "huge:", 5))) + return(NULL); + + for (currentTest = 0;currentTest < sizeof(hugeTests)/sizeof(hugeTests[0]); + currentTest++) + if (!strcmp(hugeTests[currentTest].name, URI)) + goto found; + + return(NULL); + +found: + rlen = strlen(hugeTests[currentTest].start); + current = hugeTests[currentTest].start; + instate = 0; + return((void *) current); +} + +/** + * hugeClose: + * @context: the read context + * + * Close the huge: query handler + * + * Returns 0 or -1 in case of error + */ +static int +hugeClose(void * context) { + if (context == NULL) return(-1); + fprintf(stderr, "\n"); + return(0); +} + +#define CHUNK 4096 + +char filling[CHUNK + 1]; + +static void fillFilling(void) { + int i; + + for (i = 0;i < CHUNK;i++) { + filling[i] = 'a'; + } + filling[CHUNK] = 0; +} + +size_t maxlen = 64 * 1024 * 1024; +size_t curlen = 0; +size_t dotlen; + +/** + * hugeRead: + * @context: the read context + * @buffer: where to store data + * @len: number of bytes to read + * + * Implement an huge: query read. + * + * Returns the number of bytes read or -1 in case of error + */ +static int +hugeRead(void *context, char *buffer, int len) +{ + if ((context == NULL) || (buffer == NULL) || (len < 0)) + return (-1); + + if (instate == 0) { + if (len >= rlen) { + len = rlen; + rlen = 0; + memcpy(buffer, current, len); + instate = 1; + curlen = 0; + dotlen = maxlen / 10; + } else { + memcpy(buffer, current, len); + rlen -= len; + current += len; + } + } else if (instate == 2) { + if (len >= rlen) { + len = rlen; + rlen = 0; + memcpy(buffer, current, len); + instate = 3; + curlen = 0; + } else { + memcpy(buffer, current, len); + rlen -= len; + current += len; + } + } else if (instate == 1) { + if (len > CHUNK) len = CHUNK; + memcpy(buffer, &filling[0], len); + curlen += len; + if (curlen >= maxlen) { + rlen = strlen(hugeTests[currentTest].end); + current = hugeTests[currentTest].end; + instate = 2; + } else { + if (curlen > dotlen) { + fprintf(stderr, "."); + dotlen += maxlen / 10; + } + } + } else + len = 0; + return (len); +} + +/************************************************************************ + * * + * Crazy document generator * + * * + ************************************************************************/ + +unsigned int crazy_indx = 0; + +const char *crazy = "\ +\ +\ +\ +\ +\ +\ +]>\ +\ +\ +\ +\ +\ +foo\ +\ +\ +\ +"; + +/** + * crazyMatch: + * @URI: an URI to test + * + * Check for a crazy: query + * + * Returns 1 if yes and 0 if another Input module should be used + */ +static int +crazyMatch(const char * URI) { + if ((URI != NULL) && (!strncmp(URI, "crazy:", 6))) + return(1); + return(0); +} + +/** + * crazyOpen: + * @URI: an URI to test + * + * Return a pointer to the crazy: query handler, in this example simply + * the current pointer... + * + * Returns an Input context or NULL in case or error + */ +static void * +crazyOpen(const char * URI) { + if ((URI == NULL) || (strncmp(URI, "crazy:", 6))) + return(NULL); + + if (crazy_indx > strlen(crazy)) + return(NULL); + reset_timout(); + rlen = crazy_indx; + current = &crazy[0]; + instate = 0; + return((void *) current); +} + +/** + * crazyClose: + * @context: the read context + * + * Close the crazy: query handler + * + * Returns 0 or -1 in case of error + */ +static int +crazyClose(void * context) { + if (context == NULL) return(-1); + return(0); +} + + +/** + * crazyRead: + * @context: the read context + * @buffer: where to store data + * @len: number of bytes to read + * + * Implement an crazy: query read. + * + * Returns the number of bytes read or -1 in case of error + */ +static int +crazyRead(void *context, char *buffer, int len) +{ + if ((context == NULL) || (buffer == NULL) || (len < 0)) + return (-1); + + if ((check_time() <= 0) && (instate == 1)) { + fprintf(stderr, "\ntimeout in crazy(%d)\n", crazy_indx); + rlen = strlen(crazy) - crazy_indx; + current = &crazy[crazy_indx]; + instate = 2; + } + if (instate == 0) { + if (len >= rlen) { + len = rlen; + rlen = 0; + memcpy(buffer, current, len); + instate = 1; + curlen = 0; + } else { + memcpy(buffer, current, len); + rlen -= len; + current += len; + } + } else if (instate == 2) { + if (len >= rlen) { + len = rlen; + rlen = 0; + memcpy(buffer, current, len); + instate = 3; + curlen = 0; + } else { + memcpy(buffer, current, len); + rlen -= len; + current += len; + } + } else if (instate == 1) { + if (len > CHUNK) len = CHUNK; + memcpy(buffer, &filling[0], len); + curlen += len; + if (curlen >= maxlen) { + rlen = strlen(crazy) - crazy_indx; + current = &crazy[crazy_indx]; + instate = 2; + } + } else + len = 0; + return (len); +} +/************************************************************************ + * * + * Libxml2 specific routines * + * * + ************************************************************************/ + +static int nb_tests = 0; +static int nb_errors = 0; +static int nb_leaks = 0; +static int extraMemoryFromResolver = 0; + +/* + * We need to trap calls to the resolver to not account memory for the catalog + * which is shared to the current running test. We also don't want to have + * network downloads modifying tests. + */ +static xmlParserInputPtr +testExternalEntityLoader(const char *URL, const char *ID, + xmlParserCtxtPtr ctxt) { + xmlParserInputPtr ret; + int memused = xmlMemUsed(); + + ret = xmlNoNetExternalEntityLoader(URL, ID, ctxt); + extraMemoryFromResolver += xmlMemUsed() - memused; + + return(ret); +} + +/* + * Trapping the error messages at the generic level to grab the equivalent of + * stderr messages on CLI tools. + */ +static char testErrors[32769]; +static int testErrorsSize = 0; + +static void XMLCDECL +channel(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...) { + va_list args; + int res; + + if (testErrorsSize >= 32768) + return; + va_start(args, msg); + res = vsnprintf(&testErrors[testErrorsSize], + 32768 - testErrorsSize, + msg, args); + va_end(args); + if (testErrorsSize + res >= 32768) { + /* buffer is full */ + testErrorsSize = 32768; + testErrors[testErrorsSize] = 0; + } else { + testErrorsSize += res; + } + testErrors[testErrorsSize] = 0; +} + +/** + * xmlParserPrintFileContext: + * @input: an xmlParserInputPtr input + * + * Displays current context within the input content for error tracking + */ + +static void +xmlParserPrintFileContextInternal(xmlParserInputPtr input , + xmlGenericErrorFunc chanl, void *data ) { + const xmlChar *cur, *base; + unsigned int n, col; /* GCC warns if signed, because compared with sizeof() */ + xmlChar content[81]; /* space for 80 chars + line terminator */ + xmlChar *ctnt; + + if (input == NULL) return; + cur = input->cur; + base = input->base; + /* skip backwards over any end-of-lines */ + while ((cur > base) && ((*(cur) == '\n') || (*(cur) == '\r'))) { + cur--; + } + n = 0; + /* search backwards for beginning-of-line (to max buff size) */ + while ((n++ < (sizeof(content)-1)) && (cur > base) && + (*(cur) != '\n') && (*(cur) != '\r')) + cur--; + if ((*(cur) == '\n') || (*(cur) == '\r')) cur++; + /* calculate the error position in terms of the current position */ + col = input->cur - cur; + /* search forward for end-of-line (to max buff size) */ + n = 0; + ctnt = content; + /* copy selected text to our buffer */ + while ((*cur != 0) && (*(cur) != '\n') && + (*(cur) != '\r') && (n < sizeof(content)-1)) { + *ctnt++ = *cur++; + n++; + } + *ctnt = 0; + /* print out the selected text */ + chanl(data ,"%s\n", content); + /* create blank line with problem pointer */ + n = 0; + ctnt = content; + /* (leave buffer space for pointer + line terminator) */ + while ((nfile; + line = err->line; + code = err->code; + domain = err->domain; + level = err->level; + node = err->node; + if ((domain == XML_FROM_PARSER) || (domain == XML_FROM_HTML) || + (domain == XML_FROM_DTD) || (domain == XML_FROM_NAMESPACE) || + (domain == XML_FROM_IO) || (domain == XML_FROM_VALID)) { + ctxt = err->ctxt; + } + str = err->message; + + if (code == XML_ERR_OK) + return; + + if ((node != NULL) && (node->type == XML_ELEMENT_NODE)) + name = node->name; + + /* + * Maintain the compatibility with the legacy error handling + */ + if (ctxt != NULL) { + input = ctxt->input; + if ((input != NULL) && (input->filename == NULL) && + (ctxt->inputNr > 1)) { + cur = input; + input = ctxt->inputTab[ctxt->inputNr - 2]; + } + if (input != NULL) { + if (input->filename) + channel(data, "%s:%d: ", input->filename, input->line); + else if ((line != 0) && (domain == XML_FROM_PARSER)) + channel(data, "Entity: line %d: ", input->line); + } + } else { + if (file != NULL) + channel(data, "%s:%d: ", file, line); + else if ((line != 0) && (domain == XML_FROM_PARSER)) + channel(data, "Entity: line %d: ", line); + } + if (name != NULL) { + channel(data, "element %s: ", name); + } + if (code == XML_ERR_OK) + return; + switch (domain) { + case XML_FROM_PARSER: + channel(data, "parser "); + break; + case XML_FROM_NAMESPACE: + channel(data, "namespace "); + break; + case XML_FROM_DTD: + case XML_FROM_VALID: + channel(data, "validity "); + break; + case XML_FROM_HTML: + channel(data, "HTML parser "); + break; + case XML_FROM_MEMORY: + channel(data, "memory "); + break; + case XML_FROM_OUTPUT: + channel(data, "output "); + break; + case XML_FROM_IO: + channel(data, "I/O "); + break; + case XML_FROM_XINCLUDE: + channel(data, "XInclude "); + break; + case XML_FROM_XPATH: + channel(data, "XPath "); + break; + case XML_FROM_XPOINTER: + channel(data, "parser "); + break; + case XML_FROM_REGEXP: + channel(data, "regexp "); + break; + case XML_FROM_MODULE: + channel(data, "module "); + break; + case XML_FROM_SCHEMASV: + channel(data, "Schemas validity "); + break; + case XML_FROM_SCHEMASP: + channel(data, "Schemas parser "); + break; + case XML_FROM_RELAXNGP: + channel(data, "Relax-NG parser "); + break; + case XML_FROM_RELAXNGV: + channel(data, "Relax-NG validity "); + break; + case XML_FROM_CATALOG: + channel(data, "Catalog "); + break; + case XML_FROM_C14N: + channel(data, "C14N "); + break; + case XML_FROM_XSLT: + channel(data, "XSLT "); + break; + default: + break; + } + if (code == XML_ERR_OK) + return; + switch (level) { + case XML_ERR_NONE: + channel(data, ": "); + break; + case XML_ERR_WARNING: + channel(data, "warning : "); + break; + case XML_ERR_ERROR: + channel(data, "error : "); + break; + case XML_ERR_FATAL: + channel(data, "error : "); + break; + } + if (code == XML_ERR_OK) + return; + if (str != NULL) { + int len; + len = xmlStrlen((const xmlChar *)str); + if ((len > 0) && (str[len - 1] != '\n')) + channel(data, "%s\n", str); + else + channel(data, "%s", str); + } else { + channel(data, "%s\n", "out of memory error"); + } + if (code == XML_ERR_OK) + return; + + if (ctxt != NULL) { + xmlParserPrintFileContextInternal(input, channel, data); + if (cur != NULL) { + if (cur->filename) + channel(data, "%s:%d: \n", cur->filename, cur->line); + else if ((line != 0) && (domain == XML_FROM_PARSER)) + channel(data, "Entity: line %d: \n", cur->line); + xmlParserPrintFileContextInternal(cur, channel, data); + } + } + if ((domain == XML_FROM_XPATH) && (err->str1 != NULL) && + (err->int1 < 100) && + (err->int1 < xmlStrlen((const xmlChar *)err->str1))) { + xmlChar buf[150]; + int i; + + channel(data, "%s\n", err->str1); + for (i=0;i < err->int1;i++) + buf[i] = ' '; + buf[i++] = '^'; + buf[i] = 0; + channel(data, "%s\n", buf); + } +} + +static void +initializeLibxml2(void) { + xmlGetWarningsDefaultValue = 0; + xmlPedanticParserDefault(0); + + xmlMemSetup(xmlMemFree, xmlMemMalloc, xmlMemRealloc, xmlMemoryStrdup); + xmlInitParser(); + xmlSetExternalEntityLoader(testExternalEntityLoader); + xmlSetStructuredErrorFunc(NULL, testStructuredErrorHandler); + /* + * register the new I/O handlers + */ + if (xmlRegisterInputCallbacks(hugeMatch, hugeOpen, + hugeRead, hugeClose) < 0) { + fprintf(stderr, "failed to register Huge handlers\n"); + exit(1); + } + if (xmlRegisterInputCallbacks(crazyMatch, crazyOpen, + crazyRead, crazyClose) < 0) { + fprintf(stderr, "failed to register Crazy handlers\n"); + exit(1); + } +} + +/************************************************************************ + * * + * SAX empty callbacks * + * * + ************************************************************************/ + +unsigned long callbacks = 0; + +/** + * isStandaloneCallback: + * @ctxt: An XML parser context + * + * Is this document tagged standalone ? + * + * Returns 1 if true + */ +static int +isStandaloneCallback(void *ctx ATTRIBUTE_UNUSED) +{ + callbacks++; + return (0); +} + +/** + * hasInternalSubsetCallback: + * @ctxt: An XML parser context + * + * Does this document has an internal subset + * + * Returns 1 if true + */ +static int +hasInternalSubsetCallback(void *ctx ATTRIBUTE_UNUSED) +{ + callbacks++; + return (0); +} + +/** + * hasExternalSubsetCallback: + * @ctxt: An XML parser context + * + * Does this document has an external subset + * + * Returns 1 if true + */ +static int +hasExternalSubsetCallback(void *ctx ATTRIBUTE_UNUSED) +{ + callbacks++; + return (0); +} + +/** + * internalSubsetCallback: + * @ctxt: An XML parser context + * + * Does this document has an internal subset + */ +static void +internalSubsetCallback(void *ctx ATTRIBUTE_UNUSED, + const xmlChar * name ATTRIBUTE_UNUSED, + const xmlChar * ExternalID ATTRIBUTE_UNUSED, + const xmlChar * SystemID ATTRIBUTE_UNUSED) +{ + callbacks++; + return; +} + +/** + * externalSubsetCallback: + * @ctxt: An XML parser context + * + * Does this document has an external subset + */ +static void +externalSubsetCallback(void *ctx ATTRIBUTE_UNUSED, + const xmlChar * name ATTRIBUTE_UNUSED, + const xmlChar * ExternalID ATTRIBUTE_UNUSED, + const xmlChar * SystemID ATTRIBUTE_UNUSED) +{ + callbacks++; + return; +} + +/** + * resolveEntityCallback: + * @ctxt: An XML parser context + * @publicId: The public ID of the entity + * @systemId: The system ID of the entity + * + * Special entity resolver, better left to the parser, it has + * more context than the application layer. + * The default behaviour is to NOT resolve the entities, in that case + * the ENTITY_REF nodes are built in the structure (and the parameter + * values). + * + * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour. + */ +static xmlParserInputPtr +resolveEntityCallback(void *ctx ATTRIBUTE_UNUSED, + const xmlChar * publicId ATTRIBUTE_UNUSED, + const xmlChar * systemId ATTRIBUTE_UNUSED) +{ + callbacks++; + return (NULL); +} + +/** + * getEntityCallback: + * @ctxt: An XML parser context + * @name: The entity name + * + * Get an entity by name + * + * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour. + */ +static xmlEntityPtr +getEntityCallback(void *ctx ATTRIBUTE_UNUSED, + const xmlChar * name ATTRIBUTE_UNUSED) +{ + callbacks++; + return (NULL); +} + +/** + * getParameterEntityCallback: + * @ctxt: An XML parser context + * @name: The entity name + * + * Get a parameter entity by name + * + * Returns the xmlParserInputPtr + */ +static xmlEntityPtr +getParameterEntityCallback(void *ctx ATTRIBUTE_UNUSED, + const xmlChar * name ATTRIBUTE_UNUSED) +{ + callbacks++; + return (NULL); +} + + +/** + * entityDeclCallback: + * @ctxt: An XML parser context + * @name: the entity name + * @type: the entity type + * @publicId: The public ID of the entity + * @systemId: The system ID of the entity + * @content: the entity value (without processing). + * + * An entity definition has been parsed + */ +static void +entityDeclCallback(void *ctx ATTRIBUTE_UNUSED, + const xmlChar * name ATTRIBUTE_UNUSED, + int type ATTRIBUTE_UNUSED, + const xmlChar * publicId ATTRIBUTE_UNUSED, + const xmlChar * systemId ATTRIBUTE_UNUSED, + xmlChar * content ATTRIBUTE_UNUSED) +{ + callbacks++; + return; +} + +/** + * attributeDeclCallback: + * @ctxt: An XML parser context + * @name: the attribute name + * @type: the attribute type + * + * An attribute definition has been parsed + */ +static void +attributeDeclCallback(void *ctx ATTRIBUTE_UNUSED, + const xmlChar * elem ATTRIBUTE_UNUSED, + const xmlChar * name ATTRIBUTE_UNUSED, + int type ATTRIBUTE_UNUSED, int def ATTRIBUTE_UNUSED, + const xmlChar * defaultValue ATTRIBUTE_UNUSED, + xmlEnumerationPtr tree ATTRIBUTE_UNUSED) +{ + callbacks++; + return; +} + +/** + * elementDeclCallback: + * @ctxt: An XML parser context + * @name: the element name + * @type: the element type + * @content: the element value (without processing). + * + * An element definition has been parsed + */ +static void +elementDeclCallback(void *ctx ATTRIBUTE_UNUSED, + const xmlChar * name ATTRIBUTE_UNUSED, + int type ATTRIBUTE_UNUSED, + xmlElementContentPtr content ATTRIBUTE_UNUSED) +{ + callbacks++; + return; +} + +/** + * notationDeclCallback: + * @ctxt: An XML parser context + * @name: The name of the notation + * @publicId: The public ID of the entity + * @systemId: The system ID of the entity + * + * What to do when a notation declaration has been parsed. + */ +static void +notationDeclCallback(void *ctx ATTRIBUTE_UNUSED, + const xmlChar * name ATTRIBUTE_UNUSED, + const xmlChar * publicId ATTRIBUTE_UNUSED, + const xmlChar * systemId ATTRIBUTE_UNUSED) +{ + callbacks++; + return; +} + +/** + * unparsedEntityDeclCallback: + * @ctxt: An XML parser context + * @name: The name of the entity + * @publicId: The public ID of the entity + * @systemId: The system ID of the entity + * @notationName: the name of the notation + * + * What to do when an unparsed entity declaration is parsed + */ +static void +unparsedEntityDeclCallback(void *ctx ATTRIBUTE_UNUSED, + const xmlChar * name ATTRIBUTE_UNUSED, + const xmlChar * publicId ATTRIBUTE_UNUSED, + const xmlChar * systemId ATTRIBUTE_UNUSED, + const xmlChar * notationName ATTRIBUTE_UNUSED) +{ + callbacks++; + return; +} + +/** + * setDocumentLocatorCallback: + * @ctxt: An XML parser context + * @loc: A SAX Locator + * + * Receive the document locator at startup, actually xmlDefaultSAXLocator + * Everything is available on the context, so this is useless in our case. + */ +static void +setDocumentLocatorCallback(void *ctx ATTRIBUTE_UNUSED, + xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED) +{ + callbacks++; + return; +} + +/** + * startDocumentCallback: + * @ctxt: An XML parser context + * + * called when the document start being processed. + */ +static void +startDocumentCallback(void *ctx ATTRIBUTE_UNUSED) +{ + callbacks++; + return; +} + +/** + * endDocumentCallback: + * @ctxt: An XML parser context + * + * called when the document end has been detected. + */ +static void +endDocumentCallback(void *ctx ATTRIBUTE_UNUSED) +{ + callbacks++; + return; +} + +#if 0 +/** + * startElementCallback: + * @ctxt: An XML parser context + * @name: The element name + * + * called when an opening tag has been processed. + */ +static void +startElementCallback(void *ctx ATTRIBUTE_UNUSED, + const xmlChar * name ATTRIBUTE_UNUSED, + const xmlChar ** atts ATTRIBUTE_UNUSED) +{ + callbacks++; + return; +} + +/** + * endElementCallback: + * @ctxt: An XML parser context + * @name: The element name + * + * called when the end of an element has been detected. + */ +static void +endElementCallback(void *ctx ATTRIBUTE_UNUSED, + const xmlChar * name ATTRIBUTE_UNUSED) +{ + callbacks++; + return; +} +#endif + +/** + * charactersCallback: + * @ctxt: An XML parser context + * @ch: a xmlChar string + * @len: the number of xmlChar + * + * receiving some chars from the parser. + * Question: how much at a time ??? + */ +static void +charactersCallback(void *ctx ATTRIBUTE_UNUSED, + const xmlChar * ch ATTRIBUTE_UNUSED, + int len ATTRIBUTE_UNUSED) +{ + callbacks++; + return; +} + +/** + * referenceCallback: + * @ctxt: An XML parser context + * @name: The entity name + * + * called when an entity reference is detected. + */ +static void +referenceCallback(void *ctx ATTRIBUTE_UNUSED, + const xmlChar * name ATTRIBUTE_UNUSED) +{ + callbacks++; + return; +} + +/** + * ignorableWhitespaceCallback: + * @ctxt: An XML parser context + * @ch: a xmlChar string + * @start: the first char in the string + * @len: the number of xmlChar + * + * receiving some ignorable whitespaces from the parser. + * Question: how much at a time ??? + */ +static void +ignorableWhitespaceCallback(void *ctx ATTRIBUTE_UNUSED, + const xmlChar * ch ATTRIBUTE_UNUSED, + int len ATTRIBUTE_UNUSED) +{ + callbacks++; + return; +} + +/** + * processingInstructionCallback: + * @ctxt: An XML parser context + * @target: the target name + * @data: the PI data's + * @len: the number of xmlChar + * + * A processing instruction has been parsed. + */ +static void +processingInstructionCallback(void *ctx ATTRIBUTE_UNUSED, + const xmlChar * target ATTRIBUTE_UNUSED, + const xmlChar * data ATTRIBUTE_UNUSED) +{ + callbacks++; + return; +} + +/** + * cdataBlockCallback: + * @ctx: the user data (XML parser context) + * @value: The pcdata content + * @len: the block length + * + * called when a pcdata block has been parsed + */ +static void +cdataBlockCallback(void *ctx ATTRIBUTE_UNUSED, + const xmlChar * value ATTRIBUTE_UNUSED, + int len ATTRIBUTE_UNUSED) +{ + callbacks++; + return; +} + +/** + * commentCallback: + * @ctxt: An XML parser context + * @value: the comment content + * + * A comment has been parsed. + */ +static void +commentCallback(void *ctx ATTRIBUTE_UNUSED, + const xmlChar * value ATTRIBUTE_UNUSED) +{ + callbacks++; + return; +} + +/** + * warningCallback: + * @ctxt: An XML parser context + * @msg: the message to display/transmit + * @...: extra parameters for the message display + * + * Display and format a warning messages, gives file, line, position and + * extra parameters. + */ +static void XMLCDECL +warningCallback(void *ctx ATTRIBUTE_UNUSED, + const char *msg ATTRIBUTE_UNUSED, ...) +{ + callbacks++; + return; +} + +/** + * errorCallback: + * @ctxt: An XML parser context + * @msg: the message to display/transmit + * @...: extra parameters for the message display + * + * Display and format a error messages, gives file, line, position and + * extra parameters. + */ +static void XMLCDECL +errorCallback(void *ctx ATTRIBUTE_UNUSED, const char *msg ATTRIBUTE_UNUSED, + ...) +{ + callbacks++; + return; +} + +/** + * fatalErrorCallback: + * @ctxt: An XML parser context + * @msg: the message to display/transmit + * @...: extra parameters for the message display + * + * Display and format a fatalError messages, gives file, line, position and + * extra parameters. + */ +static void XMLCDECL +fatalErrorCallback(void *ctx ATTRIBUTE_UNUSED, + const char *msg ATTRIBUTE_UNUSED, ...) +{ + return; +} + + +/* + * SAX2 specific callbacks + */ + +/** + * startElementNsCallback: + * @ctxt: An XML parser context + * @name: The element name + * + * called when an opening tag has been processed. + */ +static void +startElementNsCallback(void *ctx ATTRIBUTE_UNUSED, + const xmlChar * localname ATTRIBUTE_UNUSED, + const xmlChar * prefix ATTRIBUTE_UNUSED, + const xmlChar * URI ATTRIBUTE_UNUSED, + int nb_namespaces ATTRIBUTE_UNUSED, + const xmlChar ** namespaces ATTRIBUTE_UNUSED, + int nb_attributes ATTRIBUTE_UNUSED, + int nb_defaulted ATTRIBUTE_UNUSED, + const xmlChar ** attributes ATTRIBUTE_UNUSED) +{ + callbacks++; + return; +} + +/** + * endElementCallback: + * @ctxt: An XML parser context + * @name: The element name + * + * called when the end of an element has been detected. + */ +static void +endElementNsCallback(void *ctx ATTRIBUTE_UNUSED, + const xmlChar * localname ATTRIBUTE_UNUSED, + const xmlChar * prefix ATTRIBUTE_UNUSED, + const xmlChar * URI ATTRIBUTE_UNUSED) +{ + callbacks++; + return; +} + +static xmlSAXHandler callbackSAX2HandlerStruct = { + internalSubsetCallback, + isStandaloneCallback, + hasInternalSubsetCallback, + hasExternalSubsetCallback, + resolveEntityCallback, + getEntityCallback, + entityDeclCallback, + notationDeclCallback, + attributeDeclCallback, + elementDeclCallback, + unparsedEntityDeclCallback, + setDocumentLocatorCallback, + startDocumentCallback, + endDocumentCallback, + NULL, + NULL, + referenceCallback, + charactersCallback, + ignorableWhitespaceCallback, + processingInstructionCallback, + commentCallback, + warningCallback, + errorCallback, + fatalErrorCallback, + getParameterEntityCallback, + cdataBlockCallback, + externalSubsetCallback, + XML_SAX2_MAGIC, + NULL, + startElementNsCallback, + endElementNsCallback, + NULL +}; + +static xmlSAXHandlerPtr callbackSAX2Handler = &callbackSAX2HandlerStruct; + +/************************************************************************ + * * + * The tests front-ends * + * * + ************************************************************************/ + +/** + * readerTest: + * @filename: the file to parse + * @max_size: size of the limit to test + * @options: parsing options + * @fail: should a failure be reported + * + * Parse a memory generated file using SAX + * + * Returns 0 in case of success, an error code otherwise + */ +static int +saxTest(const char *filename, size_t limit, int options, int fail) { + int res = 0; + xmlParserCtxtPtr ctxt; + xmlDocPtr doc; + xmlSAXHandlerPtr old_sax; + + nb_tests++; + + maxlen = limit; + ctxt = xmlNewParserCtxt(); + if (ctxt == NULL) { + fprintf(stderr, "Failed to create parser context\n"); + return(1); + } + old_sax = ctxt->sax; + ctxt->sax = callbackSAX2Handler; + ctxt->userData = NULL; + doc = xmlCtxtReadFile(ctxt, filename, NULL, options); + + if (doc != NULL) { + fprintf(stderr, "SAX parsing generated a document !\n"); + xmlFreeDoc(doc); + res = 0; + } else if (ctxt->wellFormed == 0) { + if (fail) + res = 0; + else { + fprintf(stderr, "Failed to parse '%s' %lu\n", filename, limit); + res = 1; + } + } else { + if (fail) { + fprintf(stderr, "Failed to get failure for '%s' %lu\n", + filename, limit); + res = 1; + } else + res = 0; + } + ctxt->sax = old_sax; + xmlFreeParserCtxt(ctxt); + + return(res); +} +#ifdef LIBXML_READER_ENABLED +/** + * readerTest: + * @filename: the file to parse + * @max_size: size of the limit to test + * @options: parsing options + * @fail: should a failure be reported + * + * Parse a memory generated file using the xmlReader + * + * Returns 0 in case of success, an error code otherwise + */ +static int +readerTest(const char *filename, size_t limit, int options, int fail) { + xmlTextReaderPtr reader; + int res = 0; + int ret; + + nb_tests++; + + maxlen = limit; + reader = xmlReaderForFile(filename , NULL, options); + if (reader == NULL) { + fprintf(stderr, "Failed to open '%s' test\n", filename); + return(1); + } + ret = xmlTextReaderRead(reader); + while (ret == 1) { + ret = xmlTextReaderRead(reader); + } + if (ret != 0) { + if (fail) + res = 0; + else { + if (strncmp(filename, "crazy:", 6) == 0) + fprintf(stderr, "Failed to parse '%s' %u\n", + filename, crazy_indx); + else + fprintf(stderr, "Failed to parse '%s' %lu\n", + filename, limit); + res = 1; + } + } else { + if (fail) { + if (strncmp(filename, "crazy:", 6) == 0) + fprintf(stderr, "Failed to get failure for '%s' %u\n", + filename, crazy_indx); + else + fprintf(stderr, "Failed to get failure for '%s' %lu\n", + filename, limit); + res = 1; + } else + res = 0; + } + if (timeout) + res = 1; + xmlFreeTextReader(reader); + + return(res); +} +#endif + +/************************************************************************ + * * + * Tests descriptions * + * * + ************************************************************************/ + +typedef int (*functest) (const char *filename, size_t limit, int options, + int fail); + +typedef struct limitDesc limitDesc; +typedef limitDesc *limitDescPtr; +struct limitDesc { + const char *name; /* the huge generator name */ + size_t limit; /* the limit to test */ + int options; /* extra parser options */ + int fail; /* whether the test should fail */ +}; + +static limitDesc limitDescriptions[] = { + /* max lenght of a text node in content */ + {"huge:textNode", XML_MAX_TEXT_LENGTH - CHUNK, 0, 0}, + {"huge:textNode", XML_MAX_TEXT_LENGTH + CHUNK, 0, 1}, + {"huge:textNode", XML_MAX_TEXT_LENGTH + CHUNK, XML_PARSE_HUGE, 0}, + /* max lenght of a text node in content */ + {"huge:attrNode", XML_MAX_TEXT_LENGTH - CHUNK, 0, 0}, + {"huge:attrNode", XML_MAX_TEXT_LENGTH + CHUNK, 0, 1}, + {"huge:attrNode", XML_MAX_TEXT_LENGTH + CHUNK, XML_PARSE_HUGE, 0}, + /* max lenght of a comment node */ + {"huge:commentNode", XML_MAX_TEXT_LENGTH - CHUNK, 0, 0}, + {"huge:commentNode", XML_MAX_TEXT_LENGTH + CHUNK, 0, 1}, + {"huge:commentNode", XML_MAX_TEXT_LENGTH + CHUNK, XML_PARSE_HUGE, 0}, + /* max lenght of a PI node */ + {"huge:piNode", XML_MAX_TEXT_LENGTH - CHUNK, 0, 0}, + {"huge:piNode", XML_MAX_TEXT_LENGTH + CHUNK, 0, 1}, + {"huge:piNode", XML_MAX_TEXT_LENGTH + CHUNK, XML_PARSE_HUGE, 0}, +}; + +typedef struct testDesc testDesc; +typedef testDesc *testDescPtr; +struct testDesc { + const char *desc; /* descripton of the test */ + functest func; /* function implementing the test */ +}; + +static +testDesc testDescriptions[] = { + { "Parsing of huge files with the sax parser", saxTest}, +/* { "Parsing of huge files with the tree parser", treeTest}, */ +#ifdef LIBXML_READER_ENABLED + { "Parsing of huge files with the reader", readerTest}, +#endif + {NULL, NULL} +}; + +typedef struct testException testException; +typedef testException *testExceptionPtr; +struct testException { + unsigned int test; /* the parser test number */ + unsigned int limit; /* the limit test number */ + int fail; /* new fail value or -1*/ + size_t size; /* new limit value or 0 */ +}; + +static +testException testExceptions[] = { + /* the SAX parser doesn't hit a limit of XML_MAX_TEXT_LENGTH text nodes */ + { 0, 1, 0, 0}, +}; + +static int +launchTests(testDescPtr tst, unsigned int test) { + int res = 0, err = 0; + unsigned int i, j; + size_t limit; + int fail; + + if (tst == NULL) return(-1); + + for (i = 0;i < sizeof(limitDescriptions)/sizeof(limitDescriptions[0]);i++) { + limit = limitDescriptions[i].limit; + fail = limitDescriptions[i].fail; + /* + * Handle exceptions if any + */ + for (j = 0;j < sizeof(testExceptions)/sizeof(testExceptions[0]);j++) { + if ((testExceptions[j].test == test) && + (testExceptions[j].limit == i)) { + if (testExceptions[j].fail != -1) + fail = testExceptions[j].fail; + if (testExceptions[j].size != 0) + limit = testExceptions[j].size; + break; + } + } + res = tst->func(limitDescriptions[i].name, limit, + limitDescriptions[i].options, fail); + if (res != 0) { + nb_errors++; + err++; + } + } + return(err); +} + + +static int +runtest(unsigned int i) { + int ret = 0, res; + int old_errors, old_tests, old_leaks; + + old_errors = nb_errors; + old_tests = nb_tests; + old_leaks = nb_leaks; + if ((tests_quiet == 0) && (testDescriptions[i].desc != NULL)) + printf("## %s\n", testDescriptions[i].desc); + res = launchTests(&testDescriptions[i], i); + if (res != 0) + ret++; + if (verbose) { + if ((nb_errors == old_errors) && (nb_leaks == old_leaks)) + printf("Ran %d tests, no errors\n", nb_tests - old_tests); + else + printf("Ran %d tests, %d errors, %d leaks\n", + nb_tests - old_tests, + nb_errors - old_errors, + nb_leaks - old_leaks); + } + return(ret); +} + +static int +launchCrazySAX(unsigned int test, int fail) { + int res = 0, err = 0; + + crazy_indx = test; + + res = saxTest("crazy::test", XML_MAX_LOOKUP_LIMIT - CHUNK, 0, fail); + if (res != 0) { + nb_errors++; + err++; + } + if (tests_quiet == 0) + fprintf(stderr, "%c", crazy[test]); + + return(err); +} + +static int +launchCrazy(unsigned int test, int fail) { + int res = 0, err = 0; + + crazy_indx = test; + + res = readerTest("crazy::test", XML_MAX_LOOKUP_LIMIT - CHUNK, 0, fail); + if (res != 0) { + nb_errors++; + err++; + } + if (tests_quiet == 0) + fprintf(stderr, "%c", crazy[test]); + + return(err); +} + +static int get_crazy_fail(int test) { + /* + * adding 1000000 of character 'a' leads to parser failure mostly + * everywhere except in those special spots. Need to be updated + * each time crazy is updated + */ + int fail = 1; + if ((test == 44) || /* PI in Misc */ + ((test >= 50) && (test <= 55)) || /* Comment in Misc */ + (test == 79) || /* PI in DTD */ + ((test >= 85) && (test <= 90)) || /* Comment in DTD */ + (test == 154) || /* PI in Misc */ + ((test >= 160) && (test <= 165)) || /* Comment in Misc */ + ((test >= 178) && (test <= 181)) || /* attribute value */ + (test == 183) || /* Text */ + (test == 189) || /* PI in Content */ + (test == 191) || /* Text */ + ((test >= 195) && (test <= 200)) || /* Comment in Content */ + ((test >= 203) && (test <= 206)) || /* Text */ + (test == 215) || (test == 216) || /* in CDATA */ + (test == 219) || /* Text */ + (test == 231) || /* PI in Misc */ + ((test >= 237) && (test <= 242))) /* Comment in Misc */ + fail = 0; + return(fail); +} + +static int +runcrazy(void) { + int ret = 0, res = 0; + int old_errors, old_tests, old_leaks; + unsigned int i; + + old_errors = nb_errors; + old_tests = nb_tests; + old_leaks = nb_leaks; + if (tests_quiet == 0) { + printf("## Crazy tests on reader\n"); + } + for (i = 0;i < strlen(crazy);i++) { + res += launchCrazy(i, get_crazy_fail(i)); + if (res != 0) + ret++; + } + if (tests_quiet == 0) { + printf("\n## Crazy tests on SAX\n"); + } + for (i = 0;i < strlen(crazy);i++) { + res += launchCrazySAX(i, get_crazy_fail(i)); + if (res != 0) + ret++; + } + if (tests_quiet == 0) + fprintf(stderr, "\n"); + if (verbose) { + if ((nb_errors == old_errors) && (nb_leaks == old_leaks)) + printf("Ran %d tests, no errors\n", nb_tests - old_tests); + else + printf("Ran %d tests, %d errors, %d leaks\n", + nb_tests - old_tests, + nb_errors - old_errors, + nb_leaks - old_leaks); + } + return(ret); +} + + +int +main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) { + int i, a, ret = 0; + int subset = 0; + + fillFilling(); + initializeLibxml2(); + + for (a = 1; a < argc;a++) { + if (!strcmp(argv[a], "-v")) + verbose = 1; + else if (!strcmp(argv[a], "-quiet")) + tests_quiet = 1; + else if (!strcmp(argv[a], "-crazy")) + subset = 1; + } + if (subset == 0) { + for (i = 0; testDescriptions[i].func != NULL; i++) { + ret += runtest(i); + } + } + ret += runcrazy(); + if ((nb_errors == 0) && (nb_leaks == 0)) { + ret = 0; + printf("Total %d tests, no errors\n", + nb_tests); + } else { + ret = 1; + printf("Total %d tests, %d errors, %d leaks\n", + nb_tests, nb_errors, nb_leaks); + } + xmlCleanupParser(); + xmlMemoryDump(); + + return(ret); +} Index: lib/3rdparty/libxml2/testrecurse.c =================================================================== --- lib/3rdparty/libxml2/testrecurse.c (revision 58293) +++ lib/3rdparty/libxml2/testrecurse.c (working copy) @@ -10,11 +10,8 @@ * daniel@veillard.com */ -#ifdef HAVE_CONFIG_H #include "libxml.h" -#else #include -#endif #if !defined(_WIN32) || defined(__CYGWIN__) #include @@ -148,8 +145,7 @@ free(pglob->gl_pathv[i]); } } -#define vsnprintf _vsnprintf -#define snprintf _snprintf + #else #include #endif @@ -684,12 +680,10 @@ xmlDocPtr doc; xmlParserCtxtPtr ctxt; int res = 0; - int mem; nb_tests++; ctxt = xmlNewParserCtxt(); - mem = xmlMemUsed(); /* * base of the test, parse with the old API */ @@ -725,12 +719,10 @@ xmlDocPtr doc; xmlParserCtxtPtr ctxt; int res = 0; - int mem; nb_tests++; ctxt = xmlNewParserCtxt(); - mem = xmlMemUsed(); /* * base of the test, parse with the old API */ Index: lib/3rdparty/libxml2/testRelax.c =================================================================== --- lib/3rdparty/libxml2/testRelax.c (revision 58294) +++ lib/3rdparty/libxml2/testRelax.c (working copy) @@ -49,7 +49,7 @@ #endif static int noout = 0; static int tree = 0; -#ifdef HAVE_SYS_MMAN_H +#ifdef HAVE_MMAP static int memory = 0; #endif @@ -65,7 +65,7 @@ debug++; else #endif -#ifdef HAVE_SYS_MMAN_H +#ifdef HAVE_MMAP if ((!strcmp(argv[i], "-memory")) || (!strcmp(argv[i], "--memory"))) { memory++; } else @@ -84,7 +84,7 @@ if (schema == NULL) { xmlRelaxNGParserCtxtPtr ctxt; -#ifdef HAVE_SYS_MMAN_H +#ifdef HAVE_MMAP if (memory) { int fd; struct stat info; @@ -174,7 +174,7 @@ #endif printf("\t--noout : do not print the result\n"); printf("\t--tree : print the intermediate Relax-NG document tree\n"); -#ifdef HAVE_SYS_MMAN_H +#ifdef HAVE_MMAP printf("\t--memory : test the schemas in memory parsing\n"); #endif } Index: lib/3rdparty/libxml2/testSchemas.c =================================================================== --- lib/3rdparty/libxml2/testSchemas.c (revision 58294) +++ lib/3rdparty/libxml2/testSchemas.c (working copy) @@ -49,7 +49,7 @@ static int debug = 0; #endif static int noout = 0; -#ifdef HAVE_SYS_MMAN_H +#ifdef HAVE_MMAP static int memory = 0; #endif @@ -65,7 +65,7 @@ debug++; else #endif -#ifdef HAVE_SYS_MMAN_H +#ifdef HAVE_MMAP if ((!strcmp(argv[i], "-memory")) || (!strcmp(argv[i], "--memory"))) { memory++; } else @@ -80,7 +80,7 @@ if (schema == NULL) { xmlSchemaParserCtxtPtr ctxt; -#ifdef HAVE_SYS_MMAN_H +#ifdef HAVE_MMAP if (memory) { int fd; struct stat info; @@ -164,7 +164,7 @@ printf("\t--debug : dump a debug tree of the in-memory document\n"); #endif printf("\t--noout : do not print the result\n"); -#ifdef HAVE_SYS_MMAN_H +#ifdef HAVE_MMAP printf("\t--memory : test the schemas in memory parsing\n"); #endif } Index: lib/3rdparty/libxml2/testThreads.c =================================================================== --- lib/3rdparty/libxml2/testThreads.c (revision 58293) +++ lib/3rdparty/libxml2/testThreads.c (working copy) @@ -105,10 +105,8 @@ for (repeat = 0;repeat < 500;repeat++) { xmlLoadCatalog(catalog); - for (i = 0; i < num_threads; i++) { - results[i] = NULL; - tid[i] = (pthread_t) -1; - } + memset(results, 0, sizeof(*results)*num_threads); + memset(tid, 0xff, sizeof(*tid)*num_threads); for (i = 0; i < num_threads; i++) { ret = pthread_create(&tid[i], NULL, thread_specific_data, Index: lib/3rdparty/libxml2/testXPath.c =================================================================== --- lib/3rdparty/libxml2/testXPath.c (revision 58294) +++ lib/3rdparty/libxml2/testXPath.c (working copy) @@ -172,10 +172,12 @@ xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS; xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS; xmlSubstituteEntitiesDefaultValue = 1; +#ifdef LIBXML_SAX1_ENABLED if (nocdata != 0) { xmlDefaultSAXHandlerInit(); xmlDefaultSAXHandler.cdataBlock = NULL; } +#endif if (document == NULL) { if (filename == NULL) document = xmlReadDoc(buffer,NULL,NULL,XML_PARSE_COMPACT); Index: lib/3rdparty/libxml2/threads.c =================================================================== --- lib/3rdparty/libxml2/threads.c (revision 58293) +++ lib/3rdparty/libxml2/threads.c (working copy) @@ -439,7 +439,7 @@ /* Make sure the global init lock is initialized and then lock it. */ #ifdef HAVE_PTHREAD_H /* The mutex is statically initialized, so we just lock it. */ - if (pthread_mutex_lock) + if (pthread_mutex_lock != NULL) pthread_mutex_lock(&global_init_lock); #elif defined HAVE_WIN32_THREADS LPCRITICAL_SECTION cs; @@ -509,7 +509,7 @@ __xmlGlobalInitMutexUnlock(void) { #ifdef HAVE_PTHREAD_H - if (pthread_mutex_unlock) + if (pthread_mutex_unlock != NULL) pthread_mutex_unlock(&global_init_lock); #elif defined HAVE_WIN32_THREADS if (global_init_lock != NULL) { @@ -915,6 +915,7 @@ #ifdef HAVE_PTHREAD_H if ((libxml_is_threaded) && (pthread_key_delete != NULL)) pthread_key_delete(globalkey); + once_control = PTHREAD_ONCE_INIT; #elif defined(HAVE_WIN32_THREADS) && !defined(HAVE_COMPILER_TLS) && (!defined(LIBXML_STATIC) || defined(LIBXML_STATIC_FOR_DLL)) if (globalkey != TLS_OUT_OF_INDEXES) { xmlGlobalStateCleanupHelperParams *p; Index: lib/3rdparty/libxml2/timsort.h =================================================================== --- lib/3rdparty/libxml2/timsort.h (revision 0) +++ lib/3rdparty/libxml2/timsort.h (working copy) @@ -0,0 +1,506 @@ +/* + * taken from https://github.com/swenson/sort + * Kept as is for the moment to be able to apply upstream patches for that + * code, currently used only to speed up XPath node sorting, see xpath.c + */ + +/* + * All code in this header, unless otherwise specified, is hereby licensed under the MIT Public License: + +Copyright (c) 2010 Christopher Swenson + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#include +#include +#include +#ifdef HAVE_STDINT_H +#include +#else +#ifdef HAVE_INTTYPES_H +#include +#elif defined(WIN32) +typedef __int64 int64_t; +typedef unsigned __int64 uint64_t; +#endif +#endif + +#ifndef MAX +#define MAX(x,y) (((x) > (y) ? (x) : (y))) +#endif +#ifndef MIN +#define MIN(x,y) (((x) < (y) ? (x) : (y))) +#endif + +int compute_minrun(const uint64_t); + +#ifndef CLZ +#ifdef __GNUC__ +#define CLZ __builtin_clzll +#else + +int clzll(uint64_t); + +/* adapted from Hacker's Delight */ +int clzll(uint64_t x) /* {{{ */ +{ + int n; + + if (x == 0) return(64); + n = 0; + if (x <= 0x00000000FFFFFFFFL) {n = n + 32; x = x << 32;} + if (x <= 0x0000FFFFFFFFFFFFL) {n = n + 16; x = x << 16;} + if (x <= 0x00FFFFFFFFFFFFFFL) {n = n + 8; x = x << 8;} + if (x <= 0x0FFFFFFFFFFFFFFFL) {n = n + 4; x = x << 4;} + if (x <= 0x3FFFFFFFFFFFFFFFL) {n = n + 2; x = x << 2;} + if (x <= 0x7FFFFFFFFFFFFFFFL) {n = n + 1;} + return n; +} +/* }}} */ + +#define CLZ clzll +#endif +#endif + +int compute_minrun(const uint64_t size) /* {{{ */ +{ + const int top_bit = 64 - CLZ(size); + const int shift = MAX(top_bit, 6) - 6; + const int minrun = size >> shift; + const uint64_t mask = (1ULL << shift) - 1; + if (mask & size) return minrun + 1; + return minrun; +} +/* }}} */ + +#ifndef SORT_NAME +#error "Must declare SORT_NAME" +#endif + +#ifndef SORT_TYPE +#error "Must declare SORT_TYPE" +#endif + +#ifndef SORT_CMP +#define SORT_CMP(x, y) ((x) < (y) ? -1 : ((x) == (y) ? 0 : 1)) +#endif + + +#define SORT_SWAP(x,y) {SORT_TYPE __SORT_SWAP_t = (x); (x) = (y); (y) = __SORT_SWAP_t;} + +#define SORT_CONCAT(x, y) x ## _ ## y +#define SORT_MAKE_STR1(x, y) SORT_CONCAT(x,y) +#define SORT_MAKE_STR(x) SORT_MAKE_STR1(SORT_NAME,x) + +#define BINARY_INSERTION_FIND SORT_MAKE_STR(binary_insertion_find) +#define BINARY_INSERTION_SORT_START SORT_MAKE_STR(binary_insertion_sort_start) +#define BINARY_INSERTION_SORT SORT_MAKE_STR(binary_insertion_sort) +#define REVERSE_ELEMENTS SORT_MAKE_STR(reverse_elements) +#define COUNT_RUN SORT_MAKE_STR(count_run) +#define CHECK_INVARIANT SORT_MAKE_STR(check_invariant) +#define TIM_SORT SORT_MAKE_STR(tim_sort) +#define TIM_SORT_RESIZE SORT_MAKE_STR(tim_sort_resize) +#define TIM_SORT_MERGE SORT_MAKE_STR(tim_sort_merge) +#define TIM_SORT_COLLAPSE SORT_MAKE_STR(tim_sort_collapse) + +#define TIM_SORT_RUN_T SORT_MAKE_STR(tim_sort_run_t) +#define TEMP_STORAGE_T SORT_MAKE_STR(temp_storage_t) + +typedef struct { + int64_t start; + int64_t length; +} TIM_SORT_RUN_T; + +void BINARY_INSERTION_SORT(SORT_TYPE *dst, const size_t size); +void TIM_SORT(SORT_TYPE *dst, const size_t size); + +/* Function used to do a binary search for binary insertion sort */ +static int64_t BINARY_INSERTION_FIND(SORT_TYPE *dst, const SORT_TYPE x, const size_t size) +{ + int64_t l, c, r; + SORT_TYPE lx; + SORT_TYPE cx; + l = 0; + r = size - 1; + c = r >> 1; + lx = dst[l]; + + /* check for beginning conditions */ + if (SORT_CMP(x, lx) < 0) + return 0; + else if (SORT_CMP(x, lx) == 0) + { + int64_t i = 1; + while (SORT_CMP(x, dst[i]) == 0) i++; + return i; + } + + cx = dst[c]; + while (1) + { + const int val = SORT_CMP(x, cx); + if (val < 0) + { + if (c - l <= 1) return c; + r = c; + } + else if (val > 0) + { + if (r - c <= 1) return c + 1; + l = c; + lx = cx; + } + else + { + do + { + cx = dst[++c]; + } while (SORT_CMP(x, cx) == 0); + return c; + } + c = l + ((r - l) >> 1); + cx = dst[c]; + } +} + +/* Binary insertion sort, but knowing that the first "start" entries are sorted. Used in timsort. */ +static void BINARY_INSERTION_SORT_START(SORT_TYPE *dst, const size_t start, const size_t size) +{ + int64_t i; + for (i = start; i < (int64_t) size; i++) + { + int64_t j; + SORT_TYPE x; + int64_t location; + /* If this entry is already correct, just move along */ + if (SORT_CMP(dst[i - 1], dst[i]) <= 0) continue; + + /* Else we need to find the right place, shift everything over, and squeeze in */ + x = dst[i]; + location = BINARY_INSERTION_FIND(dst, x, i); + for (j = i - 1; j >= location; j--) + { + dst[j + 1] = dst[j]; + } + dst[location] = x; + } +} + +/* Binary insertion sort */ +void BINARY_INSERTION_SORT(SORT_TYPE *dst, const size_t size) +{ + BINARY_INSERTION_SORT_START(dst, 1, size); +} + +/* timsort implementation, based on timsort.txt */ + +static void REVERSE_ELEMENTS(SORT_TYPE *dst, int64_t start, int64_t end) +{ + while (1) + { + if (start >= end) return; + SORT_SWAP(dst[start], dst[end]); + start++; + end--; + } +} + +static int64_t COUNT_RUN(SORT_TYPE *dst, const int64_t start, const size_t size) +{ + int64_t curr; + if (size - start == 1) return 1; + if (start >= (int64_t) size - 2) + { + if (SORT_CMP(dst[size - 2], dst[size - 1]) > 0) + SORT_SWAP(dst[size - 2], dst[size - 1]); + return 2; + } + + curr = start + 2; + + if (SORT_CMP(dst[start], dst[start + 1]) <= 0) + { + /* increasing run */ + while (1) + { + if (curr == (int64_t) size - 1) break; + if (SORT_CMP(dst[curr - 1], dst[curr]) > 0) break; + curr++; + } + return curr - start; + } + else + { + /* decreasing run */ + while (1) + { + if (curr == (int64_t) size - 1) break; + if (SORT_CMP(dst[curr - 1], dst[curr]) <= 0) break; + curr++; + } + /* reverse in-place */ + REVERSE_ELEMENTS(dst, start, curr - 1); + return curr - start; + } +} + +#define PUSH_NEXT() do {\ +len = COUNT_RUN(dst, curr, size);\ +run = minrun;\ +if (run < minrun) run = minrun;\ +if (run > (int64_t) size - curr) run = size - curr;\ +if (run > len)\ +{\ + BINARY_INSERTION_SORT_START(&dst[curr], len, run);\ + len = run;\ +}\ +{\ +run_stack[stack_curr].start = curr;\ +run_stack[stack_curr].length = len;\ +stack_curr++;\ +}\ +curr += len;\ +if (curr == (int64_t) size)\ +{\ + /* finish up */ \ + while (stack_curr > 1) \ + { \ + TIM_SORT_MERGE(dst, run_stack, stack_curr, store); \ + run_stack[stack_curr - 2].length += run_stack[stack_curr - 1].length; \ + stack_curr--; \ + } \ + if (store->storage != NULL)\ + {\ + free(store->storage);\ + store->storage = NULL;\ + }\ + return;\ +}\ +}\ +while (0) + +static int CHECK_INVARIANT(TIM_SORT_RUN_T *stack, const int stack_curr) +{ + int64_t A, B, C; + if (stack_curr < 2) return 1; + if (stack_curr == 2) + { + const int64_t A1 = stack[stack_curr - 2].length; + const int64_t B1 = stack[stack_curr - 1].length; + if (A1 <= B1) return 0; + return 1; + } + A = stack[stack_curr - 3].length; + B = stack[stack_curr - 2].length; + C = stack[stack_curr - 1].length; + if ((A <= B + C) || (B <= C)) return 0; + return 1; +} + +typedef struct { + size_t alloc; + SORT_TYPE *storage; +} TEMP_STORAGE_T; + + +static void TIM_SORT_RESIZE(TEMP_STORAGE_T *store, const size_t new_size) +{ + if (store->alloc < new_size) + { + SORT_TYPE *tempstore = (SORT_TYPE *)realloc(store->storage, new_size * sizeof(SORT_TYPE)); + if (tempstore == NULL) + { + fprintf(stderr, "Error allocating temporary storage for tim sort: need %lu bytes", sizeof(SORT_TYPE) * new_size); + exit(1); + } + store->storage = tempstore; + store->alloc = new_size; + } +} + +static void TIM_SORT_MERGE(SORT_TYPE *dst, const TIM_SORT_RUN_T *stack, const int stack_curr, TEMP_STORAGE_T *store) +{ + const int64_t A = stack[stack_curr - 2].length; + const int64_t B = stack[stack_curr - 1].length; + const int64_t curr = stack[stack_curr - 2].start; + SORT_TYPE *storage; + int64_t i, j, k; + + TIM_SORT_RESIZE(store, MIN(A, B)); + storage = store->storage; + + /* left merge */ + if (A < B) + { + memcpy(storage, &dst[curr], A * sizeof(SORT_TYPE)); + i = 0; + j = curr + A; + + for (k = curr; k < curr + A + B; k++) + { + if ((i < A) && (j < curr + A + B)) + { + if (SORT_CMP(storage[i], dst[j]) <= 0) + dst[k] = storage[i++]; + else + dst[k] = dst[j++]; + } + else if (i < A) + { + dst[k] = storage[i++]; + } + else + dst[k] = dst[j++]; + } + } + /* right merge */ + else + { + memcpy(storage, &dst[curr + A], B * sizeof(SORT_TYPE)); + i = B - 1; + j = curr + A - 1; + + for (k = curr + A + B - 1; k >= curr; k--) + { + if ((i >= 0) && (j >= curr)) + { + if (SORT_CMP(dst[j], storage[i]) > 0) + dst[k] = dst[j--]; + else + dst[k] = storage[i--]; + } + else if (i >= 0) + dst[k] = storage[i--]; + else + dst[k] = dst[j--]; + } + } +} + +static int TIM_SORT_COLLAPSE(SORT_TYPE *dst, TIM_SORT_RUN_T *stack, int stack_curr, TEMP_STORAGE_T *store, const size_t size) +{ + while (1) + { + int64_t A, B, C; + /* if the stack only has one thing on it, we are done with the collapse */ + if (stack_curr <= 1) break; + /* if this is the last merge, just do it */ + if ((stack_curr == 2) && + (stack[0].length + stack[1].length == (int64_t) size)) + { + TIM_SORT_MERGE(dst, stack, stack_curr, store); + stack[0].length += stack[1].length; + stack_curr--; + break; + } + /* check if the invariant is off for a stack of 2 elements */ + else if ((stack_curr == 2) && (stack[0].length <= stack[1].length)) + { + TIM_SORT_MERGE(dst, stack, stack_curr, store); + stack[0].length += stack[1].length; + stack_curr--; + break; + } + else if (stack_curr == 2) + break; + + A = stack[stack_curr - 3].length; + B = stack[stack_curr - 2].length; + C = stack[stack_curr - 1].length; + + /* check first invariant */ + if (A <= B + C) + { + if (A < C) + { + TIM_SORT_MERGE(dst, stack, stack_curr - 1, store); + stack[stack_curr - 3].length += stack[stack_curr - 2].length; + stack[stack_curr - 2] = stack[stack_curr - 1]; + stack_curr--; + } + else + { + TIM_SORT_MERGE(dst, stack, stack_curr, store); + stack[stack_curr - 2].length += stack[stack_curr - 1].length; + stack_curr--; + } + } + /* check second invariant */ + else if (B <= C) + { + TIM_SORT_MERGE(dst, stack, stack_curr, store); + stack[stack_curr - 2].length += stack[stack_curr - 1].length; + stack_curr--; + } + else + break; + } + return stack_curr; +} + +void TIM_SORT(SORT_TYPE *dst, const size_t size) +{ + int minrun; + TEMP_STORAGE_T _store, *store; + TIM_SORT_RUN_T run_stack[128]; + int stack_curr = 0; + int64_t len, run; + int64_t curr = 0; + + if (size < 64) + { + BINARY_INSERTION_SORT(dst, size); + return; + } + + /* compute the minimum run length */ + minrun = compute_minrun(size); + + /* temporary storage for merges */ + store = &_store; + store->alloc = 0; + store->storage = NULL; + + PUSH_NEXT(); + PUSH_NEXT(); + PUSH_NEXT(); + + while (1) + { + if (!CHECK_INVARIANT(run_stack, stack_curr)) + { + stack_curr = TIM_SORT_COLLAPSE(dst, run_stack, stack_curr, store, size); + continue; + } + PUSH_NEXT(); + } +} + +#undef SORT_CONCAT +#undef SORT_MAKE_STR1 +#undef SORT_MAKE_STR +#undef SORT_NAME +#undef SORT_TYPE +#undef SORT_CMP +#undef TEMP_STORAGE_T +#undef TIM_SORT_RUN_T +#undef PUSH_NEXT +#undef SORT_SWAP +#undef SORT_CONCAT +#undef SORT_MAKE_STR1 +#undef SORT_MAKE_STR +#undef BINARY_INSERTION_FIND +#undef BINARY_INSERTION_SORT_START +#undef BINARY_INSERTION_SORT +#undef REVERSE_ELEMENTS +#undef COUNT_RUN +#undef TIM_SORT +#undef TIM_SORT_RESIZE +#undef TIM_SORT_COLLAPSE +#undef TIM_SORT_RUN_T +#undef TEMP_STORAGE_T Index: lib/3rdparty/libxml2/tree.c =================================================================== --- lib/3rdparty/libxml2/tree.c (revision 58293) +++ lib/3rdparty/libxml2/tree.c (working copy) @@ -41,6 +41,9 @@ #include #endif +#include "buf.h" +#include "save.h" + int __xmlRegisterCallbacks = 0; /************************************************************************ @@ -682,7 +685,8 @@ void xmlSetBufferAllocationScheme(xmlBufferAllocationScheme scheme) { if ((scheme == XML_BUFFER_ALLOC_EXACT) || - (scheme == XML_BUFFER_ALLOC_DOUBLEIT)) + (scheme == XML_BUFFER_ALLOC_DOUBLEIT) || + (scheme == XML_BUFFER_ALLOC_HYBRID)) xmlBufferAllocScheme = scheme; } @@ -693,6 +697,9 @@ * XML_BUFFER_ALLOC_EXACT - use exact sizes, keeps memory usage down * XML_BUFFER_ALLOC_DOUBLEIT - double buffer when extra needed, * improves performance + * XML_BUFFER_ALLOC_HYBRID - use exact sizes on small strings to keep memory usage tight + * in normal usage, and doubleit on large strings to avoid + * pathological performance. * * Returns the current allocation scheme */ @@ -1261,9 +1268,14 @@ const xmlChar *cur = value, *end = cur + len; const xmlChar *q; xmlEntityPtr ent; + xmlBufPtr buf; if (value == NULL) return(NULL); + buf = xmlBufCreateSize(0); + if (buf == NULL) return(NULL); + xmlBufSetAllocationScheme(buf, XML_BUFFER_ALLOC_HYBRID); + q = cur; while ((cur < end) && (*cur != 0)) { if (cur[0] == '&') { @@ -1274,19 +1286,8 @@ * Save the current text. */ if (cur != q) { - if ((last != NULL) && (last->type == XML_TEXT_NODE)) { - xmlNodeAddContentLen(last, q, cur - q); - } else { - node = xmlNewDocTextLen(doc, q, cur - q); - if (node == NULL) return(ret); - if (last == NULL) - last = ret = node; - else { - last->next = node; - node->prev = last; - last = node; - } - } + if (xmlBufAdd(buf, q, cur - q)) + goto out; } q = cur; if ((cur + 2 < end) && (cur[1] == '#') && (cur[2] == 'x')) { @@ -1351,7 +1352,7 @@ if ((cur >= end) || (*cur == 0)) { xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc, (const char *) q); - return(ret); + goto out; } if (cur != q) { /* @@ -1361,23 +1362,36 @@ ent = xmlGetDocEntity(doc, val); if ((ent != NULL) && (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { - if (last == NULL) { - node = xmlNewDocText(doc, ent->content); - last = ret = node; - } else if (last->type != XML_TEXT_NODE) { - node = xmlNewDocText(doc, ent->content); - last = xmlAddNextSibling(last, node); - } else - xmlNodeAddContent(last, ent->content); + if (xmlBufCat(buf, ent->content)) + goto out; + } else { /* + * Flush buffer so far + */ + if (!xmlBufIsEmpty(buf)) { + node = xmlNewDocText(doc, NULL); + if (node == NULL) { + if (val != NULL) xmlFree(val); + goto out; + } + node->content = xmlBufDetach(buf); + + if (last == NULL) { + last = ret = node; + } else { + last = xmlAddNextSibling(last, node); + } + } + + /* * Create a new REFERENCE_REF node */ node = xmlNewReference(doc, val); if (node == NULL) { if (val != NULL) xmlFree(val); - return(ret); + goto out; } else if ((ent != NULL) && (ent->children == NULL)) { xmlNodePtr temp; @@ -1404,40 +1418,44 @@ q = cur; } if (charval != 0) { - xmlChar buf[10]; + xmlChar buffer[10]; int l; - l = xmlCopyCharMultiByte(buf, charval); - buf[l] = 0; - node = xmlNewDocText(doc, buf); - if (node != NULL) { - if (last == NULL) { - last = ret = node; - } else { - last = xmlAddNextSibling(last, node); - } - } + l = xmlCopyCharMultiByte(buffer, charval); + buffer[l] = 0; + + if (xmlBufCat(buf, buffer)) + goto out; charval = 0; } } else cur++; } - if ((cur != q) || (ret == NULL)) { + + if (cur != q) { /* * Handle the last piece of text. */ - if ((last != NULL) && (last->type == XML_TEXT_NODE)) { - xmlNodeAddContentLen(last, q, cur - q); + if (xmlBufAdd(buf, q, cur - q)) + goto out; + } + + if (!xmlBufIsEmpty(buf)) { + node = xmlNewDocText(doc, NULL); + if (node == NULL) goto out; + node->content = xmlBufDetach(buf); + + if (last == NULL) { + last = ret = node; } else { - node = xmlNewDocTextLen(doc, q, cur - q); - if (node == NULL) return(ret); - if (last == NULL) { - ret = node; - } else { - xmlAddNextSibling(last, node); - } + last = xmlAddNextSibling(last, node); } + } else if (ret == NULL) { + ret = xmlNewDocText(doc, BAD_CAST ""); } + +out: + xmlBufFree(buf); return(ret); } @@ -1458,9 +1476,14 @@ const xmlChar *cur = value; const xmlChar *q; xmlEntityPtr ent; + xmlBufPtr buf; if (value == NULL) return(NULL); + buf = xmlBufCreateSize(0); + if (buf == NULL) return(NULL); + xmlBufSetAllocationScheme(buf, XML_BUFFER_ALLOC_HYBRID); + q = cur; while (*cur != 0) { if (cur[0] == '&') { @@ -1471,19 +1494,8 @@ * Save the current text. */ if (cur != q) { - if ((last != NULL) && (last->type == XML_TEXT_NODE)) { - xmlNodeAddContentLen(last, q, cur - q); - } else { - node = xmlNewDocTextLen(doc, q, cur - q); - if (node == NULL) return(ret); - if (last == NULL) - last = ret = node; - else { - last->next = node; - node->prev = last; - last = node; - } - } + if (xmlBufAdd(buf, q, cur - q)) + goto out; } q = cur; if ((cur[1] == '#') && (cur[2] == 'x')) { @@ -1536,7 +1548,7 @@ if (*cur == 0) { xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc, (const char *) q); - return(ret); + goto out; } if (cur != q) { /* @@ -1546,23 +1558,32 @@ ent = xmlGetDocEntity(doc, val); if ((ent != NULL) && (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { - if (last == NULL) { - node = xmlNewDocText(doc, ent->content); - last = ret = node; - } else if (last->type != XML_TEXT_NODE) { - node = xmlNewDocText(doc, ent->content); - last = xmlAddNextSibling(last, node); - } else - xmlNodeAddContent(last, ent->content); + if (xmlBufCat(buf, ent->content)) + goto out; + } else { /* + * Flush buffer so far + */ + if (!xmlBufIsEmpty(buf)) { + node = xmlNewDocText(doc, NULL); + node->content = xmlBufDetach(buf); + + if (last == NULL) { + last = ret = node; + } else { + last = xmlAddNextSibling(last, node); + } + } + + /* * Create a new REFERENCE_REF node */ node = xmlNewReference(doc, val); if (node == NULL) { if (val != NULL) xmlFree(val); - return(ret); + goto out; } else if ((ent != NULL) && (ent->children == NULL)) { xmlNodePtr temp; @@ -1588,19 +1609,15 @@ q = cur; } if (charval != 0) { - xmlChar buf[10]; + xmlChar buffer[10]; int len; - len = xmlCopyCharMultiByte(buf, charval); - buf[len] = 0; - node = xmlNewDocText(doc, buf); - if (node != NULL) { - if (last == NULL) { - last = ret = node; - } else { - last = xmlAddNextSibling(last, node); - } - } + len = xmlCopyCharMultiByte(buffer, charval); + buffer[len] = 0; + + if (xmlBufCat(buf, buffer)) + goto out; + charval = 0; } } else cur++; @@ -1609,18 +1626,22 @@ /* * Handle the last piece of text. */ - if ((last != NULL) && (last->type == XML_TEXT_NODE)) { - xmlNodeAddContentLen(last, q, cur - q); + xmlBufAdd(buf, q, cur - q); + } + + if (!xmlBufIsEmpty(buf)) { + node = xmlNewDocText(doc, NULL); + node->content = xmlBufDetach(buf); + + if (last == NULL) { + last = ret = node; } else { - node = xmlNewDocTextLen(doc, q, cur - q); - if (node == NULL) return(ret); - if (last == NULL) { - last = ret = node; - } else { - last = xmlAddNextSibling(last, node); - } + last = xmlAddNextSibling(last, node); } } + +out: + xmlBufFree(buf); return(ret); } @@ -1641,9 +1662,14 @@ xmlNodePtr node = list; xmlChar *ret = NULL; xmlEntityPtr ent; + int attr; if (list == NULL) return (NULL); + if ((list->parent != NULL) && (list->parent->type == XML_ATTRIBUTE_NODE)) + attr = 1; + else + attr = 0; while (node != NULL) { if ((node->type == XML_TEXT_NODE) || @@ -1653,7 +1679,10 @@ } else { xmlChar *buffer; - buffer = xmlEncodeEntitiesReentrant(doc, node->content); + if (attr) + buffer = xmlEncodeAttributeEntities(doc, node->content); + else + buffer = xmlEncodeEntitiesReentrant(doc, node->content); if (buffer != NULL) { ret = xmlStrcat(ret, buffer); xmlFree(buffer); @@ -2759,7 +2788,7 @@ xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) { xmlAttrPtr prop; - if (tree == NULL) + if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL)) return; if (tree->doc != doc) { if(tree->type == XML_ELEMENT_NODE) { @@ -2787,7 +2816,7 @@ xmlSetListDoc(xmlNodePtr list, xmlDocPtr doc) { xmlNodePtr cur; - if (list == NULL) + if ((list == NULL) || (list->type == XML_NAMESPACE_DECL)) return; cur = list; while (cur != NULL) { @@ -2894,7 +2923,9 @@ xmlAddPropSibling(xmlNodePtr prev, xmlNodePtr cur, xmlNodePtr prop) { xmlAttrPtr attr; - if (cur->type != XML_ATTRIBUTE_NODE) + if ((cur == NULL) || (cur->type != XML_ATTRIBUTE_NODE) || + (prop == NULL) || (prop->type != XML_ATTRIBUTE_NODE) || + ((prev != NULL) && (prev->type != XML_ATTRIBUTE_NODE))) return(NULL); /* check if an attribute with the same name exists */ @@ -2942,14 +2973,14 @@ */ xmlNodePtr xmlAddNextSibling(xmlNodePtr cur, xmlNodePtr elem) { - if (cur == NULL) { + if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) { #ifdef DEBUG_TREE xmlGenericError(xmlGenericErrorContext, "xmlAddNextSibling : cur == NULL\n"); #endif return(NULL); } - if (elem == NULL) { + if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) { #ifdef DEBUG_TREE xmlGenericError(xmlGenericErrorContext, "xmlAddNextSibling : elem == NULL\n"); @@ -3020,14 +3051,14 @@ */ xmlNodePtr xmlAddPrevSibling(xmlNodePtr cur, xmlNodePtr elem) { - if (cur == NULL) { + if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) { #ifdef DEBUG_TREE xmlGenericError(xmlGenericErrorContext, "xmlAddPrevSibling : cur == NULL\n"); #endif return(NULL); } - if (elem == NULL) { + if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) { #ifdef DEBUG_TREE xmlGenericError(xmlGenericErrorContext, "xmlAddPrevSibling : elem == NULL\n"); @@ -3098,7 +3129,7 @@ xmlAddSibling(xmlNodePtr cur, xmlNodePtr elem) { xmlNodePtr parent; - if (cur == NULL) { + if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) { #ifdef DEBUG_TREE xmlGenericError(xmlGenericErrorContext, "xmlAddSibling : cur == NULL\n"); @@ -3106,7 +3137,7 @@ return(NULL); } - if (elem == NULL) { + if ((elem == NULL) || (elem->type == XML_NAMESPACE_DECL)) { #ifdef DEBUG_TREE xmlGenericError(xmlGenericErrorContext, "xmlAddSibling : elem == NULL\n"); @@ -3174,7 +3205,7 @@ xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) { xmlNodePtr prev; - if (parent == NULL) { + if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) { #ifdef DEBUG_TREE xmlGenericError(xmlGenericErrorContext, "xmlAddChildList : parent == NULL\n"); @@ -3182,7 +3213,7 @@ return(NULL); } - if (cur == NULL) { + if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) { #ifdef DEBUG_TREE xmlGenericError(xmlGenericErrorContext, "xmlAddChildList : child == NULL\n"); @@ -3260,7 +3291,7 @@ xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) { xmlNodePtr prev; - if (parent == NULL) { + if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) { #ifdef DEBUG_TREE xmlGenericError(xmlGenericErrorContext, "xmlAddChild : parent == NULL\n"); @@ -3268,7 +3299,7 @@ return(NULL); } - if (cur == NULL) { + if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) { #ifdef DEBUG_TREE xmlGenericError(xmlGenericErrorContext, "xmlAddChild : child == NULL\n"); @@ -3382,7 +3413,7 @@ */ xmlNodePtr xmlGetLastChild(xmlNodePtr parent) { - if (parent == NULL) { + if ((parent == NULL) || (parent->type == XML_NAMESPACE_DECL)) { #ifdef DEBUG_TREE xmlGenericError(xmlGenericErrorContext, "xmlGetLastChild : parent == NULL\n"); @@ -3732,6 +3763,10 @@ * @cur: the node * * Unlink a node from it's current context, the node is not freed + * If one need to free the node, use xmlFreeNode() routine after the + * unlink to discard it. + * Note that namespace nodes can't be unlinked as they do not have + * pointer to their parent. */ void xmlUnlinkNode(xmlNodePtr cur) { @@ -3742,6 +3777,8 @@ #endif return; } + if (cur->type == XML_NAMESPACE_DECL) + return; if (cur->type == XML_DTD_NODE) { xmlDocPtr doc; doc = cur->doc; @@ -3810,14 +3847,15 @@ xmlNodePtr xmlReplaceNode(xmlNodePtr old, xmlNodePtr cur) { if (old == cur) return(NULL); - if ((old == NULL) || (old->parent == NULL)) { + if ((old == NULL) || (old->type == XML_NAMESPACE_DECL) || + (old->parent == NULL)) { #ifdef DEBUG_TREE xmlGenericError(xmlGenericErrorContext, "xmlReplaceNode : old == NULL or without parent\n"); #endif return(NULL); } - if (cur == NULL) { + if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) { xmlUnlinkNode(old); return(old); } @@ -3931,6 +3969,8 @@ xmlAttrPtr ret; if (cur == NULL) return(NULL); + if ((target != NULL) && (target->type != XML_ELEMENT_NODE)) + return(NULL); if (target != NULL) ret = xmlNewDocProp(target->doc, cur->name, NULL); else if (doc != NULL) @@ -4050,6 +4090,8 @@ xmlAttrPtr ret = NULL; xmlAttrPtr p = NULL,q; + if ((target != NULL) && (target->type != XML_ELEMENT_NODE)) + return(NULL); while (cur != NULL) { q = xmlCopyProp(target, cur); if (q == NULL) @@ -4495,39 +4537,71 @@ ************************************************************************/ /** - * xmlGetLineNo: + * xmlGetLineNoInternal: * @node: valid node + * @depth: used to limit any risk of recursion * - * Get line number of @node. This requires activation of this option - * before invoking the parser by calling xmlLineNumbersDefault(1) + * Get line number of @node. + * Try to override the limitation of lines being store in 16 bits ints * * Returns the line number if successful, -1 otherwise */ -long -xmlGetLineNo(xmlNodePtr node) +static long +xmlGetLineNoInternal(xmlNodePtr node, int depth) { long result = -1; + if (depth >= 5) + return(-1); + if (!node) return result; if ((node->type == XML_ELEMENT_NODE) || (node->type == XML_TEXT_NODE) || (node->type == XML_COMMENT_NODE) || - (node->type == XML_PI_NODE)) - result = (long) node->line; - else if ((node->prev != NULL) && + (node->type == XML_PI_NODE)) { + if (node->line == 65535) { + if ((node->type == XML_TEXT_NODE) && (node->psvi != NULL)) + result = (long) node->psvi; + else if ((node->type == XML_ELEMENT_NODE) && + (node->children != NULL)) + result = xmlGetLineNoInternal(node->children, depth + 1); + else if (node->next != NULL) + result = xmlGetLineNoInternal(node->next, depth + 1); + else if (node->prev != NULL) + result = xmlGetLineNoInternal(node->prev, depth + 1); + } + if ((result == -1) || (result == 65535)) + result = (long) node->line; + } else if ((node->prev != NULL) && ((node->prev->type == XML_ELEMENT_NODE) || (node->prev->type == XML_TEXT_NODE) || (node->prev->type == XML_COMMENT_NODE) || (node->prev->type == XML_PI_NODE))) - result = xmlGetLineNo(node->prev); + result = xmlGetLineNoInternal(node->prev, depth + 1); else if ((node->parent != NULL) && (node->parent->type == XML_ELEMENT_NODE)) - result = xmlGetLineNo(node->parent); + result = xmlGetLineNoInternal(node->parent, depth + 1); return result; } +/** + * xmlGetLineNo: + * @node: valid node + * + * Get line number of @node. + * Try to override the limitation of lines being store in 16 bits ints + * if XML_PARSE_BIG_LINES parser option was used + * + * Returns the line number if successful, -1 otherwise + */ +long +xmlGetLineNo(xmlNodePtr node) +{ + return(xmlGetLineNoInternal(node, 0)); +} + #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED) /** * xmlGetNodePath: @@ -4550,7 +4624,7 @@ char nametemp[100]; int occur = 0, generic; - if (node == NULL) + if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) return (NULL); buf_len = 500; @@ -4815,7 +4889,7 @@ xmlNodePtr old = NULL; if (doc == NULL) return(NULL); - if (root == NULL) + if ((root == NULL) || (root->type == XML_NAMESPACE_DECL)) return(NULL); xmlUnlinkNode(root); xmlSetTreeDoc(root, doc); @@ -4902,6 +4976,8 @@ xmlNodeGetLang(xmlNodePtr cur) { xmlChar *lang; + if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) + return(NULL); while (cur != NULL) { lang = xmlGetNsProp(cur, BAD_CAST "lang", XML_XML_NAMESPACE); if (lang != NULL) @@ -4981,6 +5057,8 @@ xmlNodeGetSpacePreserve(xmlNodePtr cur) { xmlChar *space; + if ((cur == NULL) || (cur->type != XML_ELEMENT_NODE)) + return(-1); while (cur != NULL) { space = xmlGetNsProp(cur, BAD_CAST "space", XML_XML_NAMESPACE); if (space != NULL) { @@ -5147,6 +5225,8 @@ if ((cur == NULL) && (doc == NULL)) return(NULL); + if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL)) + return(NULL); if (doc == NULL) doc = cur->doc; if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) { cur = doc->children; @@ -5226,11 +5306,39 @@ int xmlNodeBufGetContent(xmlBufferPtr buffer, xmlNodePtr cur) { + xmlBufPtr buf; + int ret; + if ((cur == NULL) || (buffer == NULL)) return(-1); + buf = xmlBufFromBuffer(buffer); + ret = xmlBufGetNodeContent(buf, cur); + buffer = xmlBufBackToBuffer(buf); + if ((ret < 0) || (buffer == NULL)) + return(-1); + return(0); +} + +/** + * xmlBufGetNodeContent: + * @buf: a buffer xmlBufPtr + * @cur: the node being read + * + * Read the value of a node @cur, this can be either the text carried + * directly by this node if it's a TEXT node or the aggregate string + * of the values carried by this node child's (TEXT and ENTITY_REF). + * Entity references are substituted. + * Fills up the buffer @buffer with this value + * + * Returns 0 in case of success and -1 in case of error. + */ +int +xmlBufGetNodeContent(xmlBufPtr buf, xmlNodePtr cur) +{ + if ((cur == NULL) || (buf == NULL)) return(-1); switch (cur->type) { case XML_CDATA_SECTION_NODE: case XML_TEXT_NODE: - xmlBufferCat(buffer, cur->content); + xmlBufCat(buf, cur->content); break; case XML_DOCUMENT_FRAG_NODE: case XML_ELEMENT_NODE:{ @@ -5241,10 +5349,10 @@ case XML_CDATA_SECTION_NODE: case XML_TEXT_NODE: if (tmp->content != NULL) - xmlBufferCat(buffer, tmp->content); + xmlBufCat(buf, tmp->content); break; case XML_ENTITY_REF_NODE: - xmlNodeBufGetContent(buffer, tmp); + xmlBufGetNodeContent(buf, tmp); break; default: break; @@ -5288,16 +5396,16 @@ while (tmp != NULL) { if (tmp->type == XML_TEXT_NODE) - xmlBufferCat(buffer, tmp->content); + xmlBufCat(buf, tmp->content); else - xmlNodeBufGetContent(buffer, tmp); + xmlBufGetNodeContent(buf, tmp); tmp = tmp->next; } break; } case XML_COMMENT_NODE: case XML_PI_NODE: - xmlBufferCat(buffer, cur->content); + xmlBufCat(buf, cur->content); break; case XML_ENTITY_REF_NODE:{ xmlEntityPtr ent; @@ -5315,7 +5423,7 @@ * xmlNodeGetContent() which handles all possible node types */ tmp = ent->children; while (tmp) { - xmlNodeBufGetContent(buffer, tmp); + xmlBufGetNodeContent(buf, tmp); tmp = tmp->next; } break; @@ -5337,13 +5445,13 @@ if ((cur->type == XML_ELEMENT_NODE) || (cur->type == XML_TEXT_NODE) || (cur->type == XML_CDATA_SECTION_NODE)) { - xmlNodeBufGetContent(buffer, cur); + xmlBufGetNodeContent(buf, cur); } cur = cur->next; } break; case XML_NAMESPACE_DECL: - xmlBufferCat(buffer, ((xmlNsPtr) cur)->href); + xmlBufCat(buf, ((xmlNsPtr) cur)->href); break; case XML_ELEMENT_DECL: case XML_ATTRIBUTE_DECL: @@ -5352,6 +5460,7 @@ } return(0); } + /** * xmlNodeGetContent: * @cur: the node being read @@ -5371,16 +5480,15 @@ switch (cur->type) { case XML_DOCUMENT_FRAG_NODE: case XML_ELEMENT_NODE:{ - xmlBufferPtr buffer; + xmlBufPtr buf; xmlChar *ret; - buffer = xmlBufferCreateSize(64); - if (buffer == NULL) + buf = xmlBufCreateSize(64); + if (buf == NULL) return (NULL); - xmlNodeBufGetContent(buffer, cur); - ret = buffer->content; - buffer->content = NULL; - xmlBufferFree(buffer); + xmlBufGetNodeContent(buf, cur); + ret = xmlBufDetach(buf); + xmlBufFree(buf); return (ret); } case XML_ATTRIBUTE_NODE: @@ -5392,7 +5500,7 @@ return (NULL); case XML_ENTITY_REF_NODE:{ xmlEntityPtr ent; - xmlBufferPtr buffer; + xmlBufPtr buf; xmlChar *ret; /* lookup entity declaration */ @@ -5400,15 +5508,14 @@ if (ent == NULL) return (NULL); - buffer = xmlBufferCreate(); - if (buffer == NULL) + buf = xmlBufCreate(); + if (buf == NULL) return (NULL); - xmlNodeBufGetContent(buffer, cur); + xmlBufGetNodeContent(buf, cur); - ret = buffer->content; - buffer->content = NULL; - xmlBufferFree(buffer); + ret = xmlBufDetach(buf); + xmlBufFree(buf); return (ret); } case XML_ENTITY_NODE: @@ -5423,18 +5530,17 @@ case XML_DOCB_DOCUMENT_NODE: #endif case XML_HTML_DOCUMENT_NODE: { - xmlBufferPtr buffer; + xmlBufPtr buf; xmlChar *ret; - buffer = xmlBufferCreate(); - if (buffer == NULL) + buf = xmlBufCreate(); + if (buf == NULL) return (NULL); - xmlNodeBufGetContent(buffer, (xmlNodePtr) cur); + xmlBufGetNodeContent(buf, (xmlNodePtr) cur); - ret = buffer->content; - buffer->content = NULL; - xmlBufferFree(buffer); + ret = xmlBufDetach(buf); + xmlBufFree(buf); return (ret); } case XML_NAMESPACE_DECL: { @@ -5755,6 +5861,9 @@ int maxns = 10; int i; + if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) + return(NULL); + while (node != NULL) { if (node->type == XML_ELEMENT_NODE) { cur = node->nsDef; @@ -5853,7 +5962,7 @@ xmlNsPtr cur; xmlNodePtr orig = node; - if (node == NULL) return(NULL); + if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) return(NULL); if ((nameSpace != NULL) && (xmlStrEqual(nameSpace, (const xmlChar *)"xml"))) { if ((doc == NULL) && (node->type == XML_ELEMENT_NODE)) { @@ -5983,7 +6092,7 @@ xmlNodePtr orig = node; int is_attr; - if ((node == NULL) || (href == NULL)) + if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) || (href == NULL)) return (NULL); if (xmlStrEqual(href, XML_XML_NAMESPACE)) { /* @@ -6074,7 +6183,7 @@ xmlChar prefix[50]; int counter = 1; - if (tree == NULL) { + if ((tree == NULL) || (tree->type != XML_ELEMENT_NODE)) { #ifdef DEBUG_TREE xmlGenericError(xmlGenericErrorContext, "xmlNewReconciliedNs : tree == NULL\n"); @@ -6914,6 +7023,34 @@ } /** + * xmlBufferDetach: + * @buf: the buffer + * + * Remove the string contained in a buffer and gie it back to the + * caller. The buffer is reset to an empty content. + * This doesn't work with immutable buffers as they can't be reset. + * + * Returns the previous string contained by the buffer. + */ +xmlChar * +xmlBufferDetach(xmlBufferPtr buf) { + xmlChar *ret; + + if (buf == NULL) + return(NULL); + if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) + return(NULL); + + ret = buf->content; + buf->content = NULL; + buf->size = 0; + buf->use = 0; + + return ret; +} + + +/** * xmlBufferCreateStatic: * @mem: the memory area * @size: the size in byte @@ -6964,6 +7101,7 @@ (buf->alloc == XML_BUFFER_ALLOC_IO)) return; if ((scheme == XML_BUFFER_ALLOC_DOUBLEIT) || (scheme == XML_BUFFER_ALLOC_EXACT) || + (scheme == XML_BUFFER_ALLOC_HYBRID) || (scheme == XML_BUFFER_ALLOC_IMMUTABLE)) buf->alloc = scheme; } @@ -7231,6 +7369,21 @@ case XML_BUFFER_ALLOC_EXACT: newSize = size+10; break; + case XML_BUFFER_ALLOC_HYBRID: + if (buf->use < BASE_BUFFER_SIZE) + newSize = size; + else { + newSize = buf->size * 2; + while (size > newSize) { + if (newSize > UINT_MAX / 2) { + xmlTreeErrMemory("growing buffer"); + return 0; + } + newSize *= 2; + } + } + break; + default: newSize = size+10; break; @@ -7887,6 +8040,8 @@ if ((map == NULL) || (*map != NULL)) return (-1); + if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) + return (-1); /* * Get in-scope ns-decls of @parent. */ @@ -8154,6 +8309,8 @@ if ((doc == NULL) || (nsName == NULL) || (retNs == NULL)) return (-1); + if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) + return(-1); *retNs = NULL; if (xmlStrEqual(nsName, XML_XML_NAMESPACE)) { @@ -8252,8 +8409,8 @@ xmlNodePtr cur; xmlNsPtr ns; - if ((doc == NULL) || (node == NULL)) - return (-1); + if ((doc == NULL) || (node == NULL) || (node->type == XML_NAMESPACE_DECL)) + return(-1); if (retNs) *retNs = NULL; @@ -8321,6 +8478,9 @@ char buf[50]; const xmlChar *pref; int counter = 0; + + if ((doc == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE)) + return(NULL); /* * Create a ns-decl on @anchor. */ @@ -8837,6 +8997,9 @@ parnsdone = 0; cur = node; + if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL)) + goto internal_error; + while (cur != NULL) { /* * Paranoid source-doc sanity check. @@ -9216,6 +9379,9 @@ *resNode = NULL; cur = node; + if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL)) + return(-1); + while (cur != NULL) { if (cur->doc != sourceDoc) { /* @@ -9733,6 +9899,8 @@ if (attr->children == NULL) return (0); cur = attr->children; + if ((cur != NULL) && (cur->type == XML_NAMESPACE_DECL)) + goto internal_error; while (cur != NULL) { cur->doc = destDoc; switch (cur->type) { @@ -9817,7 +9985,8 @@ xmlNodePtr destParent, int options) { - if ((node == NULL) || (destDoc == NULL) || + if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) || + (destDoc == NULL) || ((destParent != NULL) && (destParent->doc != destDoc))) return(-1); /* Index: lib/3rdparty/libxml2/trio.c =================================================================== --- lib/3rdparty/libxml2/trio.c (revision 58294) +++ lib/3rdparty/libxml2/trio.c (working copy) @@ -307,6 +307,10 @@ #define NAN_LOWER "nan" #define NAN_UPPER "NAN" +#if !defined(HAVE_ISASCII) && !defined(isascii) +# define isascii(x) ((unsigned int)(x) < 128) +#endif + /* Various constants */ enum { TYPE_PRINT = 1, Index: lib/3rdparty/libxml2/trio.h =================================================================== --- lib/3rdparty/libxml2/trio.h (revision 58293) +++ lib/3rdparty/libxml2/trio.h (working copy) @@ -28,7 +28,7 @@ * Use autoconf defines if present. Packages using trio must define * HAVE_CONFIG_H as a compiler option themselves. */ -#if defined(HAVE_CONFIG_H) +#if defined(TRIO_HAVE_CONFIG_H) # include "config.h" #endif Index: lib/3rdparty/libxml2/uri.c =================================================================== --- lib/3rdparty/libxml2/uri.c (revision 58294) +++ lib/3rdparty/libxml2/uri.c (working copy) @@ -18,6 +18,37 @@ #include #include +/** + * MAX_URI_LENGTH: + * + * The definition of the URI regexp in the above RFC has no size limit + * In practice they are usually relativey short except for the + * data URI scheme as defined in RFC 2397. Even for data URI the usual + * maximum size before hitting random practical limits is around 64 KB + * and 4KB is usually a maximum admitted limit for proper operations. + * The value below is more a security limit than anything else and + * really should never be hit by 'normal' operations + * Set to 1 MByte in 2012, this is only enforced on output + */ +#define MAX_URI_LENGTH 1024 * 1024 + +static void +xmlURIErrMemory(const char *extra) +{ + if (extra) + __xmlRaiseError(NULL, NULL, NULL, + NULL, NULL, XML_FROM_URI, + XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, + extra, NULL, NULL, 0, 0, + "Memory allocation failed : %s\n", extra); + else + __xmlRaiseError(NULL, NULL, NULL, + NULL, NULL, XML_FROM_URI, + XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, + NULL, NULL, NULL, 0, 0, + "Memory allocation failed\n"); +} + static void xmlCleanURI(xmlURIPtr uri); /* @@ -980,8 +1011,7 @@ ret = (xmlURIPtr) xmlMalloc(sizeof(xmlURI)); if (ret == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlCreateURI: out of memory\n"); + xmlURIErrMemory("creating URI structure\n"); return(NULL); } memset(ret, 0, sizeof(xmlURI)); @@ -989,6 +1019,31 @@ } /** + * xmlSaveUriRealloc: + * + * Function to handle properly a reallocation when saving an URI + * Also imposes some limit on the length of an URI string output + */ +static xmlChar * +xmlSaveUriRealloc(xmlChar *ret, int *max) { + xmlChar *temp; + int tmp; + + if (*max > MAX_URI_LENGTH) { + xmlURIErrMemory("reaching arbitrary MAX_URI_LENGTH limit\n"); + return(NULL); + } + tmp = *max * 2; + temp = (xmlChar *) xmlRealloc(ret, (tmp + 1)); + if (temp == NULL) { + xmlURIErrMemory("saving URI\n"); + return(NULL); + } + *max = tmp; + return(temp); +} + +/** * xmlSaveUri: * @uri: pointer to an xmlURI * @@ -1010,8 +1065,7 @@ max = 80; ret = (xmlChar *) xmlMallocAtomic((max + 1) * sizeof(xmlChar)); if (ret == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlSaveUri: out of memory\n"); + xmlURIErrMemory("saving URI\n"); return(NULL); } len = 0; @@ -1020,28 +1074,16 @@ p = uri->scheme; while (*p != 0) { if (len >= max) { - max *= 2; - temp = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar)); - if (temp == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlSaveUri: out of memory\n"); - xmlFree(ret); - return(NULL); - } + temp = xmlSaveUriRealloc(ret, &max); + if (temp == NULL) goto mem_error; ret = temp; } ret[len++] = *p++; } if (len >= max) { - max *= 2; - temp = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar)); - if (temp == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlSaveUri: out of memory\n"); - xmlFree(ret); - return(NULL); - } - ret = temp; + temp = xmlSaveUriRealloc(ret, &max); + if (temp == NULL) goto mem_error; + ret = temp; } ret[len++] = ':'; } @@ -1049,15 +1091,9 @@ p = uri->opaque; while (*p != 0) { if (len + 3 >= max) { - max *= 2; - temp = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar)); - if (temp == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlSaveUri: out of memory\n"); - xmlFree(ret); - return(NULL); - } - ret = temp; + temp = xmlSaveUriRealloc(ret, &max); + if (temp == NULL) goto mem_error; + ret = temp; } if (IS_RESERVED(*(p)) || IS_UNRESERVED(*(p))) ret[len++] = *p++; @@ -1072,15 +1108,9 @@ } else { if (uri->server != NULL) { if (len + 3 >= max) { - max *= 2; - temp = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar)); - if (temp == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlSaveUri: out of memory\n"); - xmlFree(ret); - return(NULL); - } - ret = temp; + temp = xmlSaveUriRealloc(ret, &max); + if (temp == NULL) goto mem_error; + ret = temp; } ret[len++] = '/'; ret[len++] = '/'; @@ -1088,16 +1118,9 @@ p = uri->user; while (*p != 0) { if (len + 3 >= max) { - max *= 2; - temp = (xmlChar *) xmlRealloc(ret, - (max + 1) * sizeof(xmlChar)); - if (temp == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlSaveUri: out of memory\n"); - xmlFree(ret); - return(NULL); - } - ret = temp; + temp = xmlSaveUriRealloc(ret, &max); + if (temp == NULL) goto mem_error; + ret = temp; } if ((IS_UNRESERVED(*(p))) || ((*(p) == ';')) || ((*(p) == ':')) || @@ -1114,78 +1137,43 @@ } } if (len + 3 >= max) { - max *= 2; - temp = (xmlChar *) xmlRealloc(ret, - (max + 1) * sizeof(xmlChar)); - if (temp == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlSaveUri: out of memory\n"); - xmlFree(ret); - return(NULL); - } - ret = temp; + temp = xmlSaveUriRealloc(ret, &max); + if (temp == NULL) goto mem_error; + ret = temp; } ret[len++] = '@'; } p = uri->server; while (*p != 0) { if (len >= max) { - max *= 2; - temp = (xmlChar *) xmlRealloc(ret, - (max + 1) * sizeof(xmlChar)); - if (temp == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlSaveUri: out of memory\n"); - xmlFree(ret); - return(NULL); - } - ret = temp; + temp = xmlSaveUriRealloc(ret, &max); + if (temp == NULL) goto mem_error; + ret = temp; } ret[len++] = *p++; } if (uri->port > 0) { if (len + 10 >= max) { - max *= 2; - temp = (xmlChar *) xmlRealloc(ret, - (max + 1) * sizeof(xmlChar)); - if (temp == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlSaveUri: out of memory\n"); - xmlFree(ret); - return(NULL); - } - ret = temp; + temp = xmlSaveUriRealloc(ret, &max); + if (temp == NULL) goto mem_error; + ret = temp; } len += snprintf((char *) &ret[len], max - len, ":%d", uri->port); } } else if (uri->authority != NULL) { if (len + 3 >= max) { - max *= 2; - temp = (xmlChar *) xmlRealloc(ret, - (max + 1) * sizeof(xmlChar)); - if (temp == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlSaveUri: out of memory\n"); - xmlFree(ret); - return(NULL); - } - ret = temp; + temp = xmlSaveUriRealloc(ret, &max); + if (temp == NULL) goto mem_error; + ret = temp; } ret[len++] = '/'; ret[len++] = '/'; p = uri->authority; while (*p != 0) { if (len + 3 >= max) { - max *= 2; - temp = (xmlChar *) xmlRealloc(ret, - (max + 1) * sizeof(xmlChar)); - if (temp == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlSaveUri: out of memory\n"); - xmlFree(ret); - return(NULL); - } - ret = temp; + temp = xmlSaveUriRealloc(ret, &max); + if (temp == NULL) goto mem_error; + ret = temp; } if ((IS_UNRESERVED(*(p))) || ((*(p) == '$')) || ((*(p) == ',')) || ((*(p) == ';')) || @@ -1202,16 +1190,9 @@ } } else if (uri->scheme != NULL) { if (len + 3 >= max) { - max *= 2; - temp = (xmlChar *) xmlRealloc(ret, - (max + 1) * sizeof(xmlChar)); - if (temp == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlSaveUri: out of memory\n"); - xmlFree(ret); - return(NULL); - } - ret = temp; + temp = xmlSaveUriRealloc(ret, &max); + if (temp == NULL) goto mem_error; + ret = temp; } ret[len++] = '/'; ret[len++] = '/'; @@ -1229,14 +1210,9 @@ (p[2] == ':') && (xmlStrEqual(BAD_CAST uri->scheme, BAD_CAST "file"))) { if (len + 3 >= max) { - max *= 2; - ret = (xmlChar *) xmlRealloc(ret, - (max + 1) * sizeof(xmlChar)); - if (ret == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlSaveUri: out of memory\n"); - return(NULL); - } + temp = xmlSaveUriRealloc(ret, &max); + if (temp == NULL) goto mem_error; + ret = temp; } ret[len++] = *p++; ret[len++] = *p++; @@ -1244,16 +1220,9 @@ } while (*p != 0) { if (len + 3 >= max) { - max *= 2; - temp = (xmlChar *) xmlRealloc(ret, - (max + 1) * sizeof(xmlChar)); - if (temp == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlSaveUri: out of memory\n"); - xmlFree(ret); - return(NULL); - } - ret = temp; + temp = xmlSaveUriRealloc(ret, &max); + if (temp == NULL) goto mem_error; + ret = temp; } if ((IS_UNRESERVED(*(p))) || ((*(p) == '/')) || ((*(p) == ';')) || ((*(p) == '@')) || ((*(p) == '&')) || @@ -1271,61 +1240,33 @@ } if (uri->query_raw != NULL) { if (len + 1 >= max) { - max *= 2; - temp = (xmlChar *) xmlRealloc(ret, - (max + 1) * sizeof(xmlChar)); - if (temp == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlSaveUri: out of memory\n"); - xmlFree(ret); - return(NULL); - } - ret = temp; + temp = xmlSaveUriRealloc(ret, &max); + if (temp == NULL) goto mem_error; + ret = temp; } ret[len++] = '?'; p = uri->query_raw; while (*p != 0) { if (len + 1 >= max) { - max *= 2; - temp = (xmlChar *) xmlRealloc(ret, - (max + 1) * sizeof(xmlChar)); - if (temp == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlSaveUri: out of memory\n"); - xmlFree(ret); - return(NULL); - } - ret = temp; + temp = xmlSaveUriRealloc(ret, &max); + if (temp == NULL) goto mem_error; + ret = temp; } ret[len++] = *p++; } } else if (uri->query != NULL) { if (len + 3 >= max) { - max *= 2; - temp = (xmlChar *) xmlRealloc(ret, - (max + 1) * sizeof(xmlChar)); - if (temp == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlSaveUri: out of memory\n"); - xmlFree(ret); - return(NULL); - } - ret = temp; + temp = xmlSaveUriRealloc(ret, &max); + if (temp == NULL) goto mem_error; + ret = temp; } ret[len++] = '?'; p = uri->query; while (*p != 0) { if (len + 3 >= max) { - max *= 2; - temp = (xmlChar *) xmlRealloc(ret, - (max + 1) * sizeof(xmlChar)); - if (temp == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlSaveUri: out of memory\n"); - xmlFree(ret); - return(NULL); - } - ret = temp; + temp = xmlSaveUriRealloc(ret, &max); + if (temp == NULL) goto mem_error; + ret = temp; } if ((IS_UNRESERVED(*(p))) || (IS_RESERVED(*(p)))) ret[len++] = *p++; @@ -1341,31 +1282,17 @@ } if (uri->fragment != NULL) { if (len + 3 >= max) { - max *= 2; - temp = (xmlChar *) xmlRealloc(ret, - (max + 1) * sizeof(xmlChar)); - if (temp == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlSaveUri: out of memory\n"); - xmlFree(ret); - return(NULL); - } - ret = temp; + temp = xmlSaveUriRealloc(ret, &max); + if (temp == NULL) goto mem_error; + ret = temp; } ret[len++] = '#'; p = uri->fragment; while (*p != 0) { if (len + 3 >= max) { - max *= 2; - temp = (xmlChar *) xmlRealloc(ret, - (max + 1) * sizeof(xmlChar)); - if (temp == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlSaveUri: out of memory\n"); - xmlFree(ret); - return(NULL); - } - ret = temp; + temp = xmlSaveUriRealloc(ret, &max); + if (temp == NULL) goto mem_error; + ret = temp; } if ((IS_UNRESERVED(*(p))) || (IS_RESERVED(*(p)))) ret[len++] = *p++; @@ -1379,18 +1306,16 @@ } } if (len >= max) { - max *= 2; - temp = (xmlChar *) xmlRealloc(ret, (max + 1) * sizeof(xmlChar)); - if (temp == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlSaveUri: out of memory\n"); - xmlFree(ret); - return(NULL); - } - ret = temp; + temp = xmlSaveUriRealloc(ret, &max); + if (temp == NULL) goto mem_error; + ret = temp; } ret[len] = 0; return(ret); + +mem_error: + xmlFree(ret); + return(NULL); } /** @@ -1604,10 +1529,11 @@ break; } /* Valgrind complained, strcpy(cur, segp + 3); */ - /* string will overlap, do not use strcpy */ - tmp = cur; - segp += 3; - while ((*tmp++ = *segp++) != 0); + /* string will overlap, do not use strcpy */ + tmp = cur; + segp += 3; + while ((*tmp++ = *segp++) != 0) + ; /* If there are no previous segments, then keep going from here. */ segp = cur; @@ -1692,8 +1618,7 @@ if (target == NULL) { ret = (char *) xmlMallocAtomic(len + 1); if (ret == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlURIUnescapeString: out of memory\n"); + xmlURIErrMemory("unescaping URI value\n"); return(NULL); } } else @@ -1743,9 +1668,8 @@ xmlChar *ret, ch; xmlChar *temp; const xmlChar *in; + int len, out; - unsigned int len, out; - if (str == NULL) return(NULL); if (str[0] == 0) @@ -1756,19 +1680,16 @@ len += 20; ret = (xmlChar *) xmlMallocAtomic(len); if (ret == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlURIEscapeStr: out of memory\n"); + xmlURIErrMemory("escaping URI value\n"); return(NULL); } in = (const xmlChar *) str; out = 0; while(*in != 0) { if (len - out <= 3) { - len += 20; - temp = (xmlChar *) xmlRealloc(ret, len); + temp = xmlSaveUriRealloc(ret, &len); if (temp == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlURIEscapeStr: out of memory\n"); + xmlURIErrMemory("escaping URI value\n"); xmlFree(ret); return(NULL); } @@ -1823,10 +1744,9 @@ int ret2; #define NULLCHK(p) if(!p) { \ - xmlGenericError(xmlGenericErrorContext, \ - "xmlURIEscape: out of memory\n"); \ - xmlFreeURI(uri); \ - return NULL; } \ + xmlURIErrMemory("escaping URI value\n"); \ + xmlFreeURI(uri); \ + return NULL; } \ if (str == NULL) return (NULL); @@ -2133,8 +2053,7 @@ len += strlen(bas->path); res->path = (char *) xmlMallocAtomic(len); if (res->path == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlBuildURI: out of memory\n"); + xmlURIErrMemory("resolving URI against base\n"); goto done; } res->path[0] = 0; @@ -2395,8 +2314,7 @@ */ val = (xmlChar *) xmlMalloc (len + 3 * nbslash); if (val == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlBuildRelativeURI: out of memory\n"); + xmlURIErrMemory("building relative URI\n"); goto done; } vptr = val; @@ -2479,7 +2397,20 @@ if (path == NULL) return(NULL); - /* sanitize filename starting with // so it can be used as URI */ +#if defined(_WIN32) + /* + * We must not change the backslashes to slashes if the the path + * starts with \\?\ + * Those paths can be up to 32k characters long. + * Was added specifically for OpenOffice, those paths can't be converted + * to URIs anyway. + */ + if ((path[0] == '\\') && (path[1] == '\\') && (path[2] == '?') && + (path[3] == '\\') ) + return xmlStrdup((const xmlChar *) path); +#endif + + /* sanitize filename starting with // so it can be used as URI */ if ((path[0] == '/') && (path[1] == '/') && (path[2] != '/')) path++; Index: lib/3rdparty/libxml2/valid.c =================================================================== --- lib/3rdparty/libxml2/valid.c (revision 58294) +++ lib/3rdparty/libxml2/valid.c (working copy) @@ -5236,7 +5236,7 @@ xmlElementContentPtr cont; const xmlChar *name; - if (elemDecl == NULL) + if ((elemDecl == NULL) || (parent == NULL)) return(-1); cont = elemDecl->content; name = elemDecl->name; @@ -5517,7 +5517,8 @@ int ret = 1; xmlNodePtr cur, child; - if ((ctxt == NULL) || (doc == NULL) || (elem == NULL)) + if ((ctxt == NULL) || (doc == NULL) || (elem == NULL) || + (elem->type != XML_ELEMENT_NODE)) return(0); child = elem->children; @@ -6379,7 +6380,8 @@ * they don't really mean anything validation wise. */ if ((elem->type == XML_XINCLUDE_START) || - (elem->type == XML_XINCLUDE_END)) + (elem->type == XML_XINCLUDE_END) || + (elem->type == XML_NAMESPACE_DECL)) return(1); CHECK_DTD; @@ -6559,6 +6561,7 @@ int xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) { xmlRefTablePtr table; + unsigned int save; if (ctxt == NULL) return(0); @@ -6568,6 +6571,10 @@ return(0); } + /* trick to get correct line id report */ + save = ctxt->finishDtd; + ctxt->finishDtd = 0; + /* * Check all the NOTATION/NOTATIONS attributes */ @@ -6581,6 +6588,8 @@ ctxt->doc = doc; ctxt->valid = 1; xmlHashScan(table, (xmlHashScanner) xmlValidateCheckRefCallback, ctxt); + + ctxt->finishDtd = save; return(ctxt->valid); } Index: lib/3rdparty/libxml2/xinclude.c =================================================================== --- lib/3rdparty/libxml2/xinclude.c (revision 58294) +++ lib/3rdparty/libxml2/xinclude.c (working copy) @@ -26,6 +26,7 @@ #ifdef LIBXML_XINCLUDE_ENABLED #include +#include "buf.h" #define XINCLUDE_MAX_DEPTH 40 @@ -874,8 +875,8 @@ static xmlNodePtr xmlXIncludeGetNthChild(xmlNodePtr cur, int no) { int i; - if (cur == NULL) - return(cur); + if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) + return(NULL); cur = cur->children; for (i = 0;i <= no;cur = cur->next) { if (cur == NULL) @@ -922,11 +923,13 @@ return(NULL); start = (xmlNodePtr) range->user; - if (start == NULL) + if ((start == NULL) || (start->type == XML_NAMESPACE_DECL)) return(NULL); end = range->user2; if (end == NULL) return(xmlDocCopyNode(start, target, 1)); + if (end->type == XML_NAMESPACE_DECL) + return(NULL); cur = start; index1 = range->index; @@ -1796,6 +1799,9 @@ int i; xmlChar *encoding = NULL; xmlCharEncoding enc = (xmlCharEncoding) 0; + xmlParserCtxtPtr pctxt; + xmlParserInputPtr inputStream; + int xinclude_multibyte_fallback_used = 0; /* * Check the URL and remove any fragment identifier @@ -1870,43 +1876,65 @@ /* * Load it. */ - buf = xmlParserInputBufferCreateFilename((const char *)URL, enc); + pctxt = xmlNewParserCtxt(); + inputStream = xmlLoadExternalEntity((const char*)URL, NULL, pctxt); + if(inputStream == NULL) { + xmlFreeParserCtxt(pctxt); + xmlFree(URL); + return(-1); + } + buf = inputStream->buf; if (buf == NULL) { + xmlFreeInputStream (inputStream); + xmlFreeParserCtxt(pctxt); xmlFree(URL); return(-1); } + if (buf->encoder) + xmlCharEncCloseFunc(buf->encoder); + buf->encoder = xmlGetCharEncodingHandler(enc); node = xmlNewText(NULL); /* * Scan all chars from the resource and add the to the node */ +xinclude_multibyte_fallback: while (xmlParserInputBufferRead(buf, 128) > 0) { int len; const xmlChar *content; - content = xmlBufferContent(buf->buffer); - len = xmlBufferLength(buf->buffer); + content = xmlBufContent(buf->buffer); + len = xmlBufLength(buf->buffer); for (i = 0;i < len;) { int cur; int l; cur = xmlStringCurrentChar(NULL, &content[i], &l); if (!IS_CHAR(cur)) { - xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, - XML_XINCLUDE_INVALID_CHAR, - "%s contains invalid char\n", URL); - xmlFreeParserInputBuffer(buf); - xmlFree(URL); - return(-1); + /* Handle splitted multibyte char at buffer boundary */ + if (((len - i) < 4) && (!xinclude_multibyte_fallback_used)) { + xinclude_multibyte_fallback_used = 1; + xmlBufShrink(buf->buffer, i); + goto xinclude_multibyte_fallback; + } else { + xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, + XML_XINCLUDE_INVALID_CHAR, + "%s contains invalid char\n", URL); + xmlFreeParserInputBuffer(buf); + xmlFree(URL); + return(-1); + } } else { + xinclude_multibyte_fallback_used = 0; xmlNodeAddContentLen(node, &content[i], l); } i += l; } - xmlBufferShrink(buf->buffer, len); + xmlBufShrink(buf->buffer, len); } - xmlFreeParserInputBuffer(buf); + xmlFreeParserCtxt(pctxt); xmlXIncludeAddTxt(ctxt, node, URL); + xmlFreeInputStream(inputStream); loaded: /* @@ -1933,7 +1961,8 @@ xmlXIncludeCtxtPtr newctxt; int ret = 0; - if ((fallback == NULL) || (ctxt == NULL)) + if ((fallback == NULL) || (fallback->type == XML_NAMESPACE_DECL) || + (ctxt == NULL)) return(-1); if (fallback->children != NULL) { /* @@ -2159,7 +2188,7 @@ if ((nr < 0) || (nr >= ctxt->incNr)) return(-1); cur = ctxt->incTab[nr]->ref; - if (cur == NULL) + if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) return(-1); /* @@ -2334,7 +2363,7 @@ int ret = 0; int i, start; - if ((doc == NULL) || (tree == NULL)) + if ((doc == NULL) || (tree == NULL) || (tree->type == XML_NAMESPACE_DECL)) return(-1); if (ctxt == NULL) return(-1); @@ -2448,7 +2477,8 @@ xmlXIncludeCtxtPtr ctxt; int ret = 0; - if ((tree == NULL) || (tree->doc == NULL)) + if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL) || + (tree->doc == NULL)) return(-1); ctxt = xmlXIncludeNewContext(tree->doc); @@ -2533,7 +2563,8 @@ xmlXIncludeCtxtPtr ctxt; int ret = 0; - if ((tree == NULL) || (tree->doc == NULL)) + if ((tree == NULL) || (tree->type == XML_NAMESPACE_DECL) || + (tree->doc == NULL)) return(-1); ctxt = xmlXIncludeNewContext(tree->doc); if (ctxt == NULL) @@ -2577,7 +2608,8 @@ xmlXIncludeProcessNode(xmlXIncludeCtxtPtr ctxt, xmlNodePtr node) { int ret = 0; - if ((node == NULL) || (node->doc == NULL) || (ctxt == NULL)) + if ((node == NULL) || (node->type == XML_NAMESPACE_DECL) || + (node->doc == NULL) || (ctxt == NULL)) return(-1); ret = xmlXIncludeDoProcess(ctxt, node->doc, node); if ((ret >= 0) && (ctxt->nbErrors > 0)) Index: lib/3rdparty/libxml2/xmlIO.c =================================================================== --- lib/3rdparty/libxml2/xmlIO.c (revision 58294) +++ lib/3rdparty/libxml2/xmlIO.c (working copy) @@ -35,6 +35,9 @@ #ifdef HAVE_ZLIB_H #include #endif +#ifdef HAVE_LZMA_H +#include +#endif #if defined(WIN32) || defined(_WIN32) //#include @@ -94,6 +97,9 @@ #endif #include +#include "buf.h" +#include "enc.h" + /* #define VERBOSE_FAILURE */ /* #define DEBUG_EXTERNAL_ENTITIES */ /* #define DEBUG_INPUT */ @@ -766,13 +772,21 @@ xmlCheckFilename (const char *path) { #ifdef HAVE_STAT - struct stat stat_buffer; + struct stat stat_buffer; #endif - if (path == NULL) - return(0); + if (path == NULL) + return(0); #ifdef HAVE_STAT #if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__) + /* + * On Windows stat and wstat do not work with long pathname, + * which start with '\\?\' + */ + if ((path[0] == '\\') && (path[1] == '\\') && (path[2] == '?') && + (path[3] == '\\') ) + return 1; + if (xmlWrapStat(path, &stat_buffer) == -1) return 0; #else @@ -1307,6 +1321,125 @@ } #endif /* HAVE_ZLIB_H */ +#ifdef HAVE_LZMA_H +/************************************************************************ + * * + * I/O for compressed file accesses * + * * + ************************************************************************/ +#include "xzlib.h" +/** + * xmlXzfileMatch: + * @filename: the URI for matching + * + * input from compressed file test + * + * Returns 1 if matches, 0 otherwise + */ +static int +xmlXzfileMatch (const char *filename ATTRIBUTE_UNUSED) { + return(1); +} + +/** + * xmlXzFileOpen_real: + * @filename: the URI for matching + * + * input from compressed file open + * if @filename is " " then the standard input is used + * + * Returns an I/O context or NULL in case of error + */ +static void * +xmlXzfileOpen_real (const char *filename) { + const char *path = NULL; + xzFile fd; + + if (!strcmp(filename, "-")) { + fd = __libxml2_xzdopen(dup(0), "rb"); + return((void *) fd); + } + + if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file://localhost/", 17)) { + path = &filename[16]; + } else if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file:///", 8)) { + path = &filename[7]; + } else if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file:/", 6)) { + /* lots of generators seems to lazy to read RFC 1738 */ + path = &filename[5]; + } else + path = filename; + + if (path == NULL) + return(NULL); + if (!xmlCheckFilename(path)) + return(NULL); + + fd = __libxml2_xzopen(path, "rb"); + return((void *) fd); +} + +/** + * xmlXzfileOpen: + * @filename: the URI for matching + * + * Wrapper around xmlXzfileOpen_real that try it with an unescaped + * version of @filename, if this fails fallback to @filename + * + * Returns a handler or NULL in case or failure + */ +static void * +xmlXzfileOpen (const char *filename) { + char *unescaped; + void *retval; + + retval = xmlXzfileOpen_real(filename); + if (retval == NULL) { + unescaped = xmlURIUnescapeString(filename, 0, NULL); + if (unescaped != NULL) { + retval = xmlXzfileOpen_real(unescaped); + } + xmlFree(unescaped); + } + + return retval; +} + +/** + * xmlXzfileRead: + * @context: the I/O context + * @buffer: where to drop data + * @len: number of bytes to write + * + * Read @len bytes to @buffer from the compressed I/O channel. + * + * Returns the number of bytes written + */ +static int +xmlXzfileRead (void * context, char * buffer, int len) { + int ret; + + ret = __libxml2_xzread((xzFile) context, &buffer[0], len); + if (ret < 0) xmlIOErr(0, "xzread()"); + return(ret); +} + +/** + * xmlXzfileClose: + * @context: the I/O context + * + * Close a compressed I/O channel + */ +static int +xmlXzfileClose (void * context) { + int ret; + + ret = (__libxml2_xzclose((xzFile) context) == LZMA_OK ) ? 0 : -1; + if (ret < 0) xmlIOErr(0, "xzclose()"); + return(ret); +} +#endif /* HAVE_LZMA_H */ + #ifdef LIBXML_HTTP_ENABLED /************************************************************************ * * @@ -1915,8 +2048,8 @@ /* Pull the data out of the memory output buffer */ xmlOutputBufferPtr dctxt = ctxt->doc_buff; - http_content = (char *)dctxt->buffer->content; - content_lgth = dctxt->buffer->use; + http_content = (char *) xmlBufContent(dctxt->buffer); + content_lgth = xmlBufUse(dctxt->buffer); } if ( http_content == NULL ) { @@ -2182,6 +2315,10 @@ xmlRegisterInputCallbacks(xmlGzfileMatch, xmlGzfileOpen, xmlGzfileRead, xmlGzfileClose); #endif /* HAVE_ZLIB_H */ +#ifdef HAVE_LZMA_H + xmlRegisterInputCallbacks(xmlXzfileMatch, xmlXzfileOpen, + xmlXzfileRead, xmlXzfileClose); +#endif /* HAVE_ZLIB_H */ #ifdef LIBXML_HTTP_ENABLED xmlRegisterInputCallbacks(xmlIOHTTPMatch, xmlIOHTTPOpen, @@ -2279,15 +2416,15 @@ return(NULL); } memset(ret, 0, (size_t) sizeof(xmlParserInputBuffer)); - ret->buffer = xmlBufferCreateSize(2 * xmlDefaultBufferSize); + ret->buffer = xmlBufCreateSize(2 * xmlDefaultBufferSize); if (ret->buffer == NULL) { xmlFree(ret); return(NULL); } - ret->buffer->alloc = XML_BUFFER_ALLOC_DOUBLEIT; + xmlBufSetAllocationScheme(ret->buffer, XML_BUFFER_ALLOC_DOUBLEIT); ret->encoder = xmlGetCharEncodingHandler(enc); if (ret->encoder != NULL) - ret->raw = xmlBufferCreateSize(2 * xmlDefaultBufferSize); + ret->raw = xmlBufCreateSize(2 * xmlDefaultBufferSize); else ret->raw = NULL; ret->readcallback = NULL; @@ -2318,19 +2455,19 @@ return(NULL); } memset(ret, 0, (size_t) sizeof(xmlOutputBuffer)); - ret->buffer = xmlBufferCreate(); + ret->buffer = xmlBufCreate(); if (ret->buffer == NULL) { xmlFree(ret); return(NULL); } /* try to avoid a performance problem with Windows realloc() */ - if (ret->buffer->alloc == XML_BUFFER_ALLOC_EXACT) - ret->buffer->alloc = XML_BUFFER_ALLOC_DOUBLEIT; + if (xmlBufGetAllocationScheme(ret->buffer) == XML_BUFFER_ALLOC_EXACT) + xmlBufSetAllocationScheme(ret->buffer, XML_BUFFER_ALLOC_DOUBLEIT); ret->encoder = encoder; if (encoder != NULL) { - ret->conv = xmlBufferCreateSize(4000); + ret->conv = xmlBufCreateSize(4000); if (ret->conv == NULL) { xmlFree(ret); return(NULL); @@ -2339,7 +2476,7 @@ /* * This call is designed to initiate the encoder state */ - xmlCharEncOutFunc(encoder, ret->conv, NULL); + xmlCharEncOutput(ret, 1); } else ret->conv = NULL; ret->writecallback = NULL; @@ -2368,7 +2505,7 @@ return(NULL); } memset(ret, 0, (size_t) sizeof(xmlOutputBuffer)); - ret->buffer = xmlBufferCreate(); + ret->buffer = xmlBufCreate(); if (ret->buffer == NULL) { xmlFree(ret); return(NULL); @@ -2377,15 +2514,12 @@ /* * For conversion buffers we use the special IO handling - * We don't do that from the exported API to avoid confusing - * user's code. */ - ret->buffer->alloc = XML_BUFFER_ALLOC_IO; - ret->buffer->contentIO = ret->buffer->content; + xmlBufSetAllocationScheme(ret->buffer, XML_BUFFER_ALLOC_IO); ret->encoder = encoder; if (encoder != NULL) { - ret->conv = xmlBufferCreateSize(4000); + ret->conv = xmlBufCreateSize(4000); if (ret->conv == NULL) { xmlFree(ret); return(NULL); @@ -2394,7 +2528,7 @@ /* * This call is designed to initiate the encoder state */ - xmlCharEncOutFunc(encoder, ret->conv, NULL); + xmlCharEncOutput(ret, 1); } else ret->conv = NULL; ret->writecallback = NULL; @@ -2418,7 +2552,7 @@ if (in == NULL) return; if (in->raw) { - xmlBufferFree(in->raw); + xmlBufFree(in->raw); in->raw = NULL; } if (in->encoder != NULL) { @@ -2428,7 +2562,7 @@ in->closecallback(in->context); } if (in->buffer != NULL) { - xmlBufferFree(in->buffer); + xmlBufFree(in->buffer); in->buffer = NULL; } @@ -2460,14 +2594,14 @@ } written = out->written; if (out->conv) { - xmlBufferFree(out->conv); + xmlBufFree(out->conv); out->conv = NULL; } if (out->encoder != NULL) { xmlCharEncCloseFunc(out->encoder); } if (out->buffer != NULL) { - xmlBufferFree(out->buffer); + xmlBufFree(out->buffer); out->buffer = NULL; } @@ -2797,6 +2931,39 @@ return(ret); } +/** + * xmlOutputBufferGetContent: + * @out: an xmlOutputBufferPtr + * + * Gives a pointer to the data currently held in the output buffer + * + * Returns a pointer to the data or NULL in case of error + */ +const xmlChar * +xmlOutputBufferGetContent(xmlOutputBufferPtr out) { + if ((out == NULL) || (out->buffer == NULL)) + return(NULL); + + return(xmlBufContent(out->buffer)); +} + +/** + * xmlOutputBufferGetSize: + * @out: an xmlOutputBufferPtr + * + * Gives the length of the data currently held in the output buffer + * + * Returns 0 in case or error or no data is held, the size otherwise + */ +size_t +xmlOutputBufferGetSize(xmlOutputBufferPtr out) { + if ((out == NULL) || (out->buffer == NULL)) + return(0); + + return(xmlBufUse(out->buffer)); +} + + #endif /* LIBXML_OUTPUT_ENABLED */ /** @@ -2849,7 +3016,7 @@ ret->context = (void *) mem; ret->readcallback = (xmlInputReadCallback) xmlNop; ret->closecallback = NULL; - errcode = xmlBufferAdd(ret->buffer, (const xmlChar *) mem, size); + errcode = xmlBufAdd(ret->buffer, (const xmlChar *) mem, size); if (errcode != 0) { xmlFree(ret); return(NULL); @@ -2886,14 +3053,14 @@ return(NULL); } memset(ret, 0, (size_t) sizeof(xmlParserInputBuffer)); - ret->buffer = xmlBufferCreateStatic((void *)mem, (size_t) size); + ret->buffer = xmlBufCreateStatic((void *)mem, (size_t) size); if (ret->buffer == NULL) { xmlFree(ret); return(NULL); } ret->encoder = xmlGetCharEncodingHandler(enc); if (ret->encoder != NULL) - ret->raw = xmlBufferCreateSize(2 * xmlDefaultBufferSize); + ret->raw = xmlBufCreateSize(2 * xmlDefaultBufferSize); else ret->raw = NULL; ret->compressed = -1; @@ -3062,33 +3229,33 @@ * Store the data in the incoming raw buffer */ if (in->raw == NULL) { - in->raw = xmlBufferCreate(); + in->raw = xmlBufCreate(); } - ret = xmlBufferAdd(in->raw, (const xmlChar *) buf, len); + ret = xmlBufAdd(in->raw, (const xmlChar *) buf, len); if (ret != 0) return(-1); /* * convert as much as possible to the parser reading buffer. */ - use = in->raw->use; - nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw); + use = xmlBufUse(in->raw); + nbchars = xmlCharEncInput(in); if (nbchars < 0) { xmlIOErr(XML_IO_ENCODER, NULL); in->error = XML_IO_ENCODER; return(-1); } - in->rawconsumed += (use - in->raw->use); + in->rawconsumed += (use - xmlBufUse(in->raw)); } else { nbchars = len; - ret = xmlBufferAdd(in->buffer, (xmlChar *) buf, nbchars); + ret = xmlBufAdd(in->buffer, (xmlChar *) buf, nbchars); if (ret != 0) return(-1); } #ifdef DEBUG_INPUT xmlGenericError(xmlGenericErrorContext, "I/O: pushed %d chars, buffer %d/%d\n", - nbchars, in->buffer->use, in->buffer->size); + nbchars, xmlBufUse(in->buffer), xmlBufLength(in->buffer)); #endif return(nbchars); } @@ -3126,29 +3293,23 @@ char *buffer = NULL; int res = 0; int nbchars = 0; - int buffree; - unsigned int needSize; if ((in == NULL) || (in->error)) return(-1); if ((len <= MINLEN) && (len != 4)) len = MINLEN; - buffree = in->buffer->size - in->buffer->use; - if (buffree <= 0) { + if (xmlBufAvail(in->buffer) <= 0) { xmlIOErr(XML_IO_BUFFER_FULL, NULL); in->error = XML_IO_BUFFER_FULL; return(-1); } - needSize = in->buffer->use + len + 1; - if (needSize > in->buffer->size){ - if (!xmlBufferResize(in->buffer, needSize)){ - xmlIOErrMemory("growing input buffer"); - in->error = XML_ERR_NO_MEMORY; - return(-1); - } + if (xmlBufGrow(in->buffer, len + 1) < 0) { + xmlIOErrMemory("growing input buffer"); + in->error = XML_ERR_NO_MEMORY; + return(-1); } - buffer = (char *)&in->buffer->content[in->buffer->use]; + buffer = (char *)xmlBufEnd(in->buffer); /* * Call the read method for this I/O type. @@ -3173,32 +3334,31 @@ * Store the data in the incoming raw buffer */ if (in->raw == NULL) { - in->raw = xmlBufferCreate(); + in->raw = xmlBufCreate(); } - res = xmlBufferAdd(in->raw, (const xmlChar *) buffer, len); + res = xmlBufAdd(in->raw, (const xmlChar *) buffer, len); if (res != 0) return(-1); /* * convert as much as possible to the parser reading buffer. */ - use = in->raw->use; - nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw); + use = xmlBufUse(in->raw); + nbchars = xmlCharEncInput(in); if (nbchars < 0) { xmlIOErr(XML_IO_ENCODER, NULL); in->error = XML_IO_ENCODER; return(-1); } - in->rawconsumed += (use - in->raw->use); + in->rawconsumed += (use - xmlBufUse(in->raw)); } else { nbchars = len; - in->buffer->use += nbchars; - buffer[nbchars] = 0; + xmlBufAddLen(in->buffer, nbchars); } #ifdef DEBUG_INPUT xmlGenericError(xmlGenericErrorContext, - "I/O: read %d chars, buffer %d/%d\n", - nbchars, in->buffer->use, in->buffer->size); + "I/O: read %d chars, buffer %d\n", + nbchars, xmlBufUse(in->buffer)); #endif return(nbchars); } @@ -3220,8 +3380,7 @@ if ((in == NULL) || (in->error)) return(-1); if (in->readcallback != NULL) return(xmlParserInputBufferGrow(in, len)); - else if ((in->buffer != NULL) && - (in->buffer->alloc == XML_BUFFER_ALLOC_IMMUTABLE)) + else if (xmlBufGetAllocationScheme(in->buffer) == XML_BUFFER_ALLOC_IMMUTABLE) return(0); else return(-1); @@ -3266,30 +3425,30 @@ * Store the data in the incoming raw buffer */ if (out->conv == NULL) { - out->conv = xmlBufferCreate(); + out->conv = xmlBufCreate(); } - ret = xmlBufferAdd(out->buffer, (const xmlChar *) buf, chunk); + ret = xmlBufAdd(out->buffer, (const xmlChar *) buf, chunk); if (ret != 0) return(-1); - if ((out->buffer->use < MINLEN) && (chunk == len)) + if ((xmlBufUse(out->buffer) < MINLEN) && (chunk == len)) goto done; /* * convert as much as possible to the parser reading buffer. */ - ret = xmlCharEncOutFunc(out->encoder, out->conv, out->buffer); + ret = xmlCharEncOutput(out, 0); if ((ret < 0) && (ret != -3)) { xmlIOErr(XML_IO_ENCODER, NULL); out->error = XML_IO_ENCODER; return(-1); } - nbchars = out->conv->use; + nbchars = xmlBufUse(out->conv); } else { - ret = xmlBufferAdd(out->buffer, (const xmlChar *) buf, chunk); + ret = xmlBufAdd(out->buffer, (const xmlChar *) buf, chunk); if (ret != 0) return(-1); - nbchars = out->buffer->use; + nbchars = xmlBufUse(out->buffer); } buf += chunk; len -= chunk; @@ -3303,14 +3462,14 @@ */ if (out->encoder != NULL) { ret = out->writecallback(out->context, - (const char *)out->conv->content, nbchars); + (const char *)xmlBufContent(out->conv), nbchars); if (ret >= 0) - xmlBufferShrink(out->conv, ret); + xmlBufShrink(out->conv, ret); } else { ret = out->writecallback(out->context, - (const char *)out->buffer->content, nbchars); + (const char *)xmlBufContent(out->buffer), nbchars); if (ret >= 0) - xmlBufferShrink(out->buffer, ret); + xmlBufShrink(out->buffer, ret); } if (ret < 0) { xmlIOErr(XML_IO_WRITE, NULL); @@ -3418,7 +3577,8 @@ if ((out == NULL) || (out->error) || (str == NULL) || (out->buffer == NULL) || - (out->buffer->alloc == XML_BUFFER_ALLOC_IMMUTABLE)) return(-1); + (xmlBufGetAllocationScheme(out->buffer) == XML_BUFFER_ALLOC_IMMUTABLE)) + return(-1); len = strlen((const char *)str); if (len < 0) return(0); if (out->error) return(-1); @@ -3431,14 +3591,14 @@ * how many bytes to consume and how many bytes to store. */ cons = len; - chunk = (out->buffer->size - out->buffer->use) - 1; + chunk = xmlBufAvail(out->buffer) - 1; /* * make sure we have enough room to save first, if this is * not the case force a flush, but make sure we stay in the loop */ if (chunk < 40) { - if (xmlBufferGrow(out->buffer, out->buffer->size + 100) < 0) + if (xmlBufGrow(out->buffer, 100) < 0) return(-1); oldwritten = -1; continue; @@ -3452,36 +3612,33 @@ * Store the data in the incoming raw buffer */ if (out->conv == NULL) { - out->conv = xmlBufferCreate(); + out->conv = xmlBufCreate(); } - ret = escaping(out->buffer->content + out->buffer->use , + ret = escaping(xmlBufEnd(out->buffer) , &chunk, str, &cons); if ((ret < 0) || (chunk == 0)) /* chunk==0 => nothing done */ return(-1); - out->buffer->use += chunk; - out->buffer->content[out->buffer->use] = 0; + xmlBufAddLen(out->buffer, chunk); - if ((out->buffer->use < MINLEN) && (cons == len)) + if ((xmlBufUse(out->buffer) < MINLEN) && (cons == len)) goto done; /* * convert as much as possible to the output buffer. */ - ret = xmlCharEncOutFunc(out->encoder, out->conv, out->buffer); + ret = xmlCharEncOutput(out, 0); if ((ret < 0) && (ret != -3)) { xmlIOErr(XML_IO_ENCODER, NULL); out->error = XML_IO_ENCODER; return(-1); } - nbchars = out->conv->use; + nbchars = xmlBufUse(out->conv); } else { - ret = escaping(out->buffer->content + out->buffer->use , - &chunk, str, &cons); + ret = escaping(xmlBufEnd(out->buffer), &chunk, str, &cons); if ((ret < 0) || (chunk == 0)) /* chunk==0 => nothing done */ return(-1); - out->buffer->use += chunk; - out->buffer->content[out->buffer->use] = 0; - nbchars = out->buffer->use; + xmlBufAddLen(out->buffer, chunk); + nbchars = xmlBufUse(out->buffer); } str += cons; len -= cons; @@ -3495,14 +3652,14 @@ */ if (out->encoder != NULL) { ret = out->writecallback(out->context, - (const char *)out->conv->content, nbchars); + (const char *)xmlBufContent(out->conv), nbchars); if (ret >= 0) - xmlBufferShrink(out->conv, ret); + xmlBufShrink(out->conv, ret); } else { ret = out->writecallback(out->context, - (const char *)out->buffer->content, nbchars); + (const char *)xmlBufContent(out->buffer), nbchars); if (ret >= 0) - xmlBufferShrink(out->buffer, ret); + xmlBufShrink(out->buffer, ret); } if (ret < 0) { xmlIOErr(XML_IO_WRITE, NULL); @@ -3510,8 +3667,8 @@ return(ret); } out->written += ret; - } else if (out->buffer->size - out->buffer->use < MINLEN) { - xmlBufferResize(out->buffer, out->buffer->size + MINLEN); + } else if (xmlBufAvail(out->buffer) < MINLEN) { + xmlBufGrow(out->buffer, MINLEN); } written += nbchars; } while ((len > 0) && (oldwritten != written)); @@ -3571,7 +3728,7 @@ /* * convert as much as possible to the parser reading buffer. */ - nbchars = xmlCharEncOutFunc(out->encoder, out->conv, out->buffer); + nbchars = xmlCharEncOutput(out, 0); if (nbchars < 0) { xmlIOErr(XML_IO_ENCODER, NULL); out->error = XML_IO_ENCODER; @@ -3585,14 +3742,16 @@ if ((out->conv != NULL) && (out->encoder != NULL) && (out->writecallback != NULL)) { ret = out->writecallback(out->context, - (const char *)out->conv->content, out->conv->use); + (const char *)xmlBufContent(out->conv), + xmlBufUse(out->conv)); if (ret >= 0) - xmlBufferShrink(out->conv, ret); + xmlBufShrink(out->conv, ret); } else if (out->writecallback != NULL) { ret = out->writecallback(out->context, - (const char *)out->buffer->content, out->buffer->use); + (const char *)xmlBufContent(out->buffer), + xmlBufUse(out->buffer)); if (ret >= 0) - xmlBufferShrink(out->buffer, ret); + xmlBufShrink(out->buffer, ret); } if (ret < 0) { xmlIOErr(XML_IO_FLUSH, NULL); Index: lib/3rdparty/libxml2/xmllint.c =================================================================== --- lib/3rdparty/libxml2/xmllint.c (revision 58293) +++ lib/3rdparty/libxml2/xmllint.c (working copy) @@ -130,6 +130,7 @@ #endif /* LIBXML_TREE_ENABLED */ static int recovery = 0; static int noent = 0; +static int noenc = 0; static int noblanks = 0; static int noout = 0; static int nowrap = 0; @@ -168,7 +169,7 @@ #ifdef LIBXML_PUSH_ENABLED static int push = 0; #endif /* LIBXML_PUSH_ENABLED */ -#ifdef HAVE_SYS_MMAN_H +#ifdef HAVE_MMAP static int memory = 0; #endif static int testIO = 0; @@ -208,7 +209,7 @@ #ifdef LIBXML_XPATH_ENABLED static const char *xpathquery = NULL; #endif -static int options = XML_PARSE_COMPACT; +static int options = XML_PARSE_COMPACT | XML_PARSE_BIG_LINES; static int sax = 0; static int oldxml10 = 0; @@ -519,10 +520,11 @@ * We cannot do anything because we don't have a timing function */ #ifdef HAVE_STDARG_H + va_list ap; va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap); - fprintf(stderr, " was not timed\n", msec); + fprintf(stderr, " was not timed\n"); #else /* We don't have gettimeofday, time or stdarg.h, what crazy world is * this ?! @@ -1667,6 +1669,7 @@ (xmlSchemaValidityErrorFunc) fprintf, (xmlSchemaValidityWarningFunc) fprintf, stderr); + xmlSchemaValidateSetFilename(vctxt, filename); ret = xmlSchemaValidateStream(vctxt, buf, 0, handler, (void *)user_data); @@ -1821,7 +1824,7 @@ static void streamFile(char *filename) { xmlTextReaderPtr reader; int ret; -#ifdef HAVE_SYS_MMAN_H +#ifdef HAVE_MMAP int fd = -1; struct stat info; const char *base = NULL; @@ -1972,7 +1975,7 @@ patstream = NULL; } #endif -#ifdef HAVE_SYS_MMAN_H +#ifdef HAVE_MMAP if (memory) { xmlFreeParserInputBuffer(input); munmap((char *) base, info.st_size); @@ -2072,7 +2075,7 @@ #ifdef LIBXML_OUTPUT_ENABLED xmlSaveCtxtPtr ctxt; - if (cur->nodesetval->nodeNr <= 0) { + if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr <= 0)) { fprintf(stderr, "XPath set is empty\n"); progresult = XMLLINT_ERR_XPATH; break; @@ -2137,7 +2140,7 @@ progresult = XMLLINT_ERR_MEM; return; } - ctxt->node = xmlDocGetRootElement(doc); + ctxt->node = (xmlNodePtr) doc; res = xmlXPathEval(BAD_CAST query, ctxt); xmlXPathFreeContext(ctxt); @@ -2210,7 +2213,7 @@ } } #endif /* LIBXML_PUSH_ENABLED */ -#ifdef HAVE_SYS_MMAN_H +#ifdef HAVE_MMAP else if ((html) && (memory)) { int fd; struct stat info; @@ -2325,7 +2328,7 @@ if (rectxt == NULL) xmlFreeParserCtxt(ctxt); } -#ifdef HAVE_SYS_MMAN_H +#ifdef HAVE_MMAP } else if (memory) { int fd; struct stat info; @@ -2550,7 +2553,9 @@ size = xmlC14NDocDumpMemory(doc, NULL, XML_C14N_1_0, NULL, 1, &result); if (size >= 0) { - write(1, result, size); + if (write(1, result, size) == -1) { + fprintf(stderr, "Can't write data\n"); + } xmlFree(result); } else { fprintf(stderr, "Failed to canonicalize\n"); @@ -2562,7 +2567,9 @@ size = xmlC14NDocDumpMemory(doc, NULL, XML_C14N_1_1, NULL, 1, &result); if (size >= 0) { - write(1, result, size); + if (write(1, result, size) == -1) { + fprintf(stderr, "Can't write data\n"); + } xmlFree(result); } else { fprintf(stderr, "Failed to canonicalize\n"); @@ -2575,7 +2582,9 @@ size = xmlC14NDocDumpMemory(doc, NULL, XML_C14N_EXCLUSIVE_1_0, NULL, 1, &result); if (size >= 0) { - write(1, result, size); + if (write(1, result, size) == -1) { + fprintf(stderr, "Can't write data\n"); + } xmlFree(result); } else { fprintf(stderr, "Failed to canonicalize\n"); @@ -2583,7 +2592,7 @@ } } else #endif -#ifdef HAVE_SYS_MMAN_H +#ifdef HAVE_MMAP if (memory) { xmlChar *result; int len; @@ -2604,12 +2613,14 @@ fprintf(stderr, "Failed to save\n"); progresult = XMLLINT_ERR_OUT; } else { - write(1, result, len); + if (write(1, result, len) == -1) { + fprintf(stderr, "Can't write data\n"); + } xmlFree(result); } } else -#endif /* HAVE_SYS_MMAN_H */ +#endif /* HAVE_MMAP */ if (compress) { xmlSaveFile(output ? output : "-", doc); } else if (oldout) { @@ -2949,6 +2960,7 @@ if (xmlHasFeature(XML_WITH_DEBUG_MEM)) fprintf(stderr, "MemDebug "); if (xmlHasFeature(XML_WITH_DEBUG_RUN)) fprintf(stderr, "RunDebug "); if (xmlHasFeature(XML_WITH_ZLIB)) fprintf(stderr, "Zlib "); + if (xmlHasFeature(XML_WITH_LZMA)) fprintf(stderr, "Lzma "); fprintf(stderr, "\n"); } @@ -2975,6 +2987,7 @@ printf("\t--recover : output what was parsable on broken XML documents\n"); printf("\t--huge : remove any internal arbitrary parser limits\n"); printf("\t--noent : substitute entity references by their value\n"); + printf("\t--noenc : ignore any encoding specified inside the document\n"); printf("\t--noout : don't output the result tree\n"); printf("\t--path 'paths': provide a set of paths for resources\n"); printf("\t--load-trace : print trace of all external entites loaded\n"); @@ -3005,7 +3018,7 @@ #ifdef LIBXML_PUSH_ENABLED printf("\t--push : use the push mode of the parser\n"); #endif /* LIBXML_PUSH_ENABLED */ -#ifdef HAVE_SYS_MMAN_H +#ifdef HAVE_MMAP printf("\t--memory : parse from memory\n"); #endif printf("\t--maxmem nbbytes : limits memory allocation to nbbytes bytes\n"); @@ -3129,6 +3142,10 @@ (!strcmp(argv[i], "--noent"))) { noent++; options |= XML_PARSE_NOENT; + } else if ((!strcmp(argv[i], "-noenc")) || + (!strcmp(argv[i], "--noenc"))) { + noenc++; + options |= XML_PARSE_IGNORE_ENC; } else if ((!strcmp(argv[i], "-nsclean")) || (!strcmp(argv[i], "--nsclean"))) { options |= XML_PARSE_NSCLEAN; @@ -3231,7 +3248,7 @@ (!strcmp(argv[i], "--push"))) push++; #endif /* LIBXML_PUSH_ENABLED */ -#ifdef HAVE_SYS_MMAN_H +#ifdef HAVE_MMAP else if ((!strcmp(argv[i], "-memory")) || (!strcmp(argv[i], "--memory"))) memory++; @@ -3323,8 +3340,9 @@ } else if ((!strcmp(argv[i], "-noblanks")) || (!strcmp(argv[i], "--noblanks"))) { - noblanks++; - xmlKeepBlanksDefault(0); + noblanks++; + xmlKeepBlanksDefault(0); + options |= XML_PARSE_NOBLANKS; } else if ((!strcmp(argv[i], "-maxmem")) || (!strcmp(argv[i], "--maxmem"))) { @@ -3349,11 +3367,11 @@ i++; #ifdef LIBXML_OUTPUT_ENABLED format = atoi(argv[i]); -#endif /* LIBXML_OUTPUT_ENABLED */ if (format == 1) { noblanks++; xmlKeepBlanksDefault(0); } +#endif /* LIBXML_OUTPUT_ENABLED */ } #ifdef LIBXML_READER_ENABLED else if ((!strcmp(argv[i], "-stream")) || Index: lib/3rdparty/libxml2/xmlmemory.c =================================================================== --- lib/3rdparty/libxml2/xmlmemory.c (revision 58294) +++ lib/3rdparty/libxml2/xmlmemory.c (working copy) @@ -205,7 +205,8 @@ if (xmlMemTraceBlockAt == ret) { xmlGenericError(xmlGenericErrorContext, - "%p : Malloc(%ld) Ok\n", xmlMemTraceBlockAt, size); + "%p : Malloc(%lu) Ok\n", xmlMemTraceBlockAt, + (long unsigned)size); xmlMallocBreakpoint(); } @@ -273,7 +274,8 @@ if (xmlMemTraceBlockAt == ret) { xmlGenericError(xmlGenericErrorContext, - "%p : Malloc(%ld) Ok\n", xmlMemTraceBlockAt, size); + "%p : Malloc(%lu) Ok\n", xmlMemTraceBlockAt, + (long unsigned)size); xmlMallocBreakpoint(); } @@ -348,8 +350,9 @@ } if (xmlMemTraceBlockAt == ptr) { xmlGenericError(xmlGenericErrorContext, - "%p : Realloced(%ld -> %ld) Ok\n", - xmlMemTraceBlockAt, p->mh_size, size); + "%p : Realloced(%lu -> %lu) Ok\n", + xmlMemTraceBlockAt, (long unsigned)p->mh_size, + (long unsigned)size); xmlMallocBreakpoint(); } p->mh_tag = MEMTAG; Index: lib/3rdparty/libxml2/xmlmodule.c =================================================================== --- lib/3rdparty/libxml2/xmlmodule.c (revision 58294) +++ lib/3rdparty/libxml2/xmlmodule.c (working copy) @@ -61,6 +61,10 @@ * @options: a set of xmlModuleOption * * Opens a module/shared library given its name or path + * NOTE: that due to portability issues, behaviour can only be + * guaranteed with @name using ASCII. We canot guarantee that + * an UTF-8 string would work, which is why name is a const char * + * and not a const xmlChar * . * TODO: options are not yet implemented. * * Returns a handle for the module or NULL in case of error @@ -99,6 +103,10 @@ * @symbol: the resulting symbol address * * Lookup for a symbol address in the given module + * NOTE: that due to portability issues, behaviour can only be + * guaranteed with @name using ASCII. We canot guarantee that + * an UTF-8 string would work, which is why name is a const char * + * and not a const xmlChar * . * * Returns 0 if the symbol was found, or -1 in case of error */ @@ -300,7 +308,7 @@ static void * xmlModulePlatformOpen(const char *name) { - return LoadLibrary(name); + return LoadLibraryA(name); } /* @@ -326,7 +334,14 @@ static int xmlModulePlatformSymbol(void *handle, const char *name, void **symbol) { +#ifdef _WIN32_WCE + /* + * GetProcAddressA seems only available on WinCE + */ + *symbol = GetProcAddressA(handle, name); +#else *symbol = GetProcAddress(handle, name); +#endif return (NULL == *symbol) ? -1 : 0; } Index: lib/3rdparty/libxml2/xmlreader.c =================================================================== --- lib/3rdparty/libxml2/xmlreader.c (revision 58294) +++ lib/3rdparty/libxml2/xmlreader.c (working copy) @@ -44,6 +44,8 @@ #include #endif +#include "buf.h" + #define MAX_ERR_MSG_SIZE 64000 /* @@ -135,7 +137,7 @@ int depth; /* depth of the current node */ xmlNodePtr faketext;/* fake xmlNs chld */ int preserve;/* preserve the resulting document */ - xmlBufferPtr buffer; /* used to return const xmlChar * */ + xmlBufPtr buffer; /* used to return const xmlChar * */ xmlDictPtr dict; /* the context dictionnary */ /* entity stack when traversing entities content */ @@ -152,6 +154,7 @@ /* Handling of RelaxNG validation */ xmlRelaxNGPtr rngSchemas; /* The Relax NG schemas */ xmlRelaxNGValidCtxtPtr rngValidCtxt;/* The Relax NG validation context */ + int rngPreserveCtxt; /* 1 if the context was provided by the user */ int rngValidErrors;/* The number of errors detected */ xmlNodePtr rngFullNode; /* the node if RNG not progressive */ /* Handling of Schemas validation */ @@ -806,9 +809,10 @@ */ static int xmlTextReaderPushData(xmlTextReaderPtr reader) { - xmlBufferPtr inbuf; + xmlBufPtr inbuf; int val, s; xmlTextReaderState oldstate; + int alloc; if ((reader->input == NULL) || (reader->input->buffer == NULL)) return(-1); @@ -816,17 +820,18 @@ oldstate = reader->state; reader->state = XML_TEXTREADER_NONE; inbuf = reader->input->buffer; + alloc = xmlBufGetAllocationScheme(inbuf); while (reader->state == XML_TEXTREADER_NONE) { - if (inbuf->use < reader->cur + CHUNK_SIZE) { + if (xmlBufUse(inbuf) < reader->cur + CHUNK_SIZE) { /* * Refill the buffer unless we are at the end of the stream */ if (reader->mode != XML_TEXTREADER_MODE_EOF) { val = xmlParserInputBufferRead(reader->input, 4096); if ((val == 0) && - (inbuf->alloc == XML_BUFFER_ALLOC_IMMUTABLE)) { - if (inbuf->use == reader->cur) { + (alloc == XML_BUFFER_ALLOC_IMMUTABLE)) { + if (xmlBufUse(inbuf) == reader->cur) { reader->mode = XML_TEXTREADER_MODE_EOF; reader->state = oldstate; } @@ -849,21 +854,23 @@ * parse by block of CHUNK_SIZE bytes, various tests show that * it's the best tradeoff at least on a 1.2GH Duron */ - if (inbuf->use >= reader->cur + CHUNK_SIZE) { + if (xmlBufUse(inbuf) >= reader->cur + CHUNK_SIZE) { val = xmlParseChunk(reader->ctxt, - (const char *) &inbuf->content[reader->cur], - CHUNK_SIZE, 0); + (const char *) xmlBufContent(inbuf) + reader->cur, + CHUNK_SIZE, 0); reader->cur += CHUNK_SIZE; - if ((val != 0) || (reader->ctxt->wellFormed == 0)) - return(-1); + if (val != 0) + reader->ctxt->wellFormed = 0; + if (reader->ctxt->wellFormed == 0) + break; } else { - s = inbuf->use - reader->cur; + s = xmlBufUse(inbuf) - reader->cur; val = xmlParseChunk(reader->ctxt, - (const char *) &inbuf->content[reader->cur], - s, 0); + (const char *) xmlBufContent(inbuf) + reader->cur, + s, 0); reader->cur += s; - if ((val != 0) || (reader->ctxt->wellFormed == 0)) - return(-1); + if (val != 0) + reader->ctxt->wellFormed = 0; break; } } @@ -872,10 +879,10 @@ * Discard the consumed input when needed and possible */ if (reader->mode == XML_TEXTREADER_MODE_INTERACTIVE) { - if (inbuf->alloc != XML_BUFFER_ALLOC_IMMUTABLE) { + if (alloc != XML_BUFFER_ALLOC_IMMUTABLE) { if ((reader->cur >= 4096) && - (inbuf->use - reader->cur <= CHUNK_SIZE)) { - val = xmlBufferShrink(inbuf, reader->cur); + (xmlBufUse(inbuf) - reader->cur <= CHUNK_SIZE)) { + val = xmlBufShrink(inbuf, reader->cur); if (val >= 0) { reader->cur -= val; } @@ -889,17 +896,26 @@ */ else if (reader->mode == XML_TEXTREADER_MODE_EOF) { if (reader->state != XML_TEXTREADER_DONE) { - s = inbuf->use - reader->cur; + s = xmlBufUse(inbuf) - reader->cur; val = xmlParseChunk(reader->ctxt, - (const char *) &inbuf->content[reader->cur], - s, 1); - reader->cur = inbuf->use; + (const char *) xmlBufContent(inbuf) + reader->cur, + s, 1); + reader->cur = xmlBufUse(inbuf); reader->state = XML_TEXTREADER_DONE; - if ((val != 0) || (reader->ctxt->wellFormed == 0)) - return(-1); + if (val != 0) { + if (reader->ctxt->wellFormed) + reader->ctxt->wellFormed = 0; + else + return(-1); + } } } reader->state = oldstate; + if (reader->ctxt->wellFormed == 0) { + reader->mode = XML_TEXTREADER_MODE_EOF; + return(-1); + } + return(0); } @@ -1212,6 +1228,9 @@ xmlBufferPtr buffer; xmlChar *ret; + if ((node == NULL) || (node->type == XML_NAMESPACE_DECL)) + return(NULL); + buffer = xmlBufferCreate(); if (buffer == NULL) return NULL; @@ -1264,8 +1283,6 @@ return(xmlTextReaderReadTree(reader)); if (reader->ctxt == NULL) return(-1); - if (reader->ctxt->wellFormed != 1) - return(-1); #ifdef DEBUG_READER fprintf(stderr, "\nREAD "); @@ -2055,7 +2072,7 @@ ret->entMax = 0; ret->entNr = 0; ret->input = input; - ret->buffer = xmlBufferCreateSize(100); + ret->buffer = xmlBufCreateSize(100); if (ret->buffer == NULL) { xmlFree(ret); xmlGenericError(xmlGenericErrorContext, @@ -2064,7 +2081,7 @@ } ret->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler)); if (ret->sax == NULL) { - xmlBufferFree(ret->buffer); + xmlBufFree(ret->buffer); xmlFree(ret); xmlGenericError(xmlGenericErrorContext, "xmlNewTextReader : malloc failed\n"); @@ -2097,12 +2114,13 @@ ret->mode = XML_TEXTREADER_MODE_INITIAL; ret->node = NULL; ret->curnode = NULL; - if (ret->input->buffer->use < 4) { + if (xmlBufUse(ret->input->buffer) < 4) { xmlParserInputBufferRead(input, 4); } - if (ret->input->buffer->use >= 4) { + if (xmlBufUse(ret->input->buffer) >= 4) { ret->ctxt = xmlCreatePushParserCtxt(ret->sax, NULL, - (const char *) ret->input->buffer->content, 4, URI); + (const char *) xmlBufContent(ret->input->buffer), + 4, URI); ret->base = 0; ret->cur = 4; } else { @@ -2114,7 +2132,7 @@ if (ret->ctxt == NULL) { xmlGenericError(xmlGenericErrorContext, "xmlNewTextReader : malloc failed\n"); - xmlBufferFree(ret->buffer); + xmlBufFree(ret->buffer); xmlFree(ret->sax); xmlFree(ret); return(NULL); @@ -2187,7 +2205,8 @@ reader->rngSchemas = NULL; } if (reader->rngValidCtxt != NULL) { - xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); + if (! reader->rngPreserveCtxt) + xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); reader->rngValidCtxt = NULL; } if (reader->xsdPlug != NULL) { @@ -2243,7 +2262,7 @@ if ((reader->input != NULL) && (reader->allocs & XML_TEXTREADER_INPUT)) xmlFreeParserInputBuffer(reader->input); if (reader->buffer != NULL) - xmlBufferFree(reader->buffer); + xmlBufFree(reader->buffer); if (reader->entTab != NULL) xmlFree(reader->entTab); if (reader->dict != NULL) @@ -3591,16 +3610,17 @@ (attr->children->next == NULL)) return(attr->children->content); else { - if (reader->buffer == NULL) - reader->buffer = xmlBufferCreateSize(100); if (reader->buffer == NULL) { - xmlGenericError(xmlGenericErrorContext, - "xmlTextReaderSetup : malloc failed\n"); - return (NULL); - } - reader->buffer->use = 0; - xmlNodeBufGetContent(reader->buffer, node); - return(reader->buffer->content); + reader->buffer = xmlBufCreateSize(100); + if (reader->buffer == NULL) { + xmlGenericError(xmlGenericErrorContext, + "xmlTextReaderSetup : malloc failed\n"); + return (NULL); + } + } else + xmlBufEmpty(reader->buffer); + xmlBufGetNodeContent(reader->buffer, node); + return(xmlBufContent(reader->buffer)); } break; } @@ -4095,9 +4115,11 @@ reader->rngSchemas = NULL; } if (reader->rngValidCtxt != NULL) { - xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); + if (! reader->rngPreserveCtxt) + xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); reader->rngValidCtxt = NULL; } + reader->rngPreserveCtxt = 0; return(0); } if (reader->mode != XML_TEXTREADER_MODE_INITIAL) @@ -4107,9 +4129,11 @@ reader->rngSchemas = NULL; } if (reader->rngValidCtxt != NULL) { - xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); + if (! reader->rngPreserveCtxt) + xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); reader->rngValidCtxt = NULL; } + reader->rngPreserveCtxt = 0; reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(schema); if (reader->rngValidCtxt == NULL) return(-1); @@ -4131,6 +4155,60 @@ } /** + * xmlTextReaderLocator: + * @ctx: the xmlTextReaderPtr used + * @file: returned file information + * @line: returned line information + * + * Internal locator function for the readers + * + * Returns 0 in case the Schema validation could be (des)activated and + * -1 in case of error. + */ +static int +xmlTextReaderLocator(void *ctx, const char **file, unsigned long *line) { + xmlTextReaderPtr reader; + + if ((ctx == NULL) || ((file == NULL) && (line == NULL))) + return(-1); + + if (file != NULL) + *file = NULL; + if (line != NULL) + *line = 0; + + reader = (xmlTextReaderPtr) ctx; + if ((reader->ctxt != NULL) && (reader->ctxt->input != NULL)) { + if (file != NULL) + *file = reader->ctxt->input->filename; + if (line != NULL) + *line = reader->ctxt->input->line; + return(0); + } + if (reader->node != NULL) { + long res; + int ret = 0; + + if (line != NULL) { + res = xmlGetLineNo(reader->node); + if (res > 0) + *line = (unsigned long) res; + else + ret = -1; + } + if (file != NULL) { + xmlDocPtr doc = reader->node->doc; + if ((doc != NULL) && (doc->URL != NULL)) + *file = (const char *) doc->URL; + else + ret = -1; + } + return(ret); + } + return(-1); +} + +/** * xmlTextReaderSetSchema: * @reader: the xmlTextReaderPtr used * @schema: a precompiled Schema schema @@ -4197,6 +4275,10 @@ reader->xsdValidCtxt = NULL; return(-1); } + xmlSchemaValidateSetLocator(reader->xsdValidCtxt, + xmlTextReaderLocator, + (void *) reader); + if (reader->errorFunc != NULL) { xmlSchemaSetValidErrors(reader->xsdValidCtxt, xmlTextReaderValidityErrorRelay, @@ -4214,67 +4296,91 @@ } /** - * xmlTextReaderRelaxNGValidate: + * xmlTextReaderRelaxNGValidateInternal: * @reader: the xmlTextReaderPtr used * @rng: the path to a RelaxNG schema or NULL + * @ctxt: the RelaxNG schema validation context or NULL + * @options: options (not yet used) * * Use RelaxNG to validate the document as it is processed. * Activation is only possible before the first Read(). - * if @rng is NULL, then RelaxNG validation is deactivated. + * If both @rng and @ctxt are NULL, then RelaxNG validation is deactivated. * * Returns 0 in case the RelaxNG validation could be (de)activated and - * -1 in case of error. + * -1 in case of error. */ -int -xmlTextReaderRelaxNGValidate(xmlTextReaderPtr reader, const char *rng) { - xmlRelaxNGParserCtxtPtr ctxt; - +static int +xmlTextReaderRelaxNGValidateInternal(xmlTextReaderPtr reader, + const char *rng, + xmlRelaxNGValidCtxtPtr ctxt, + int options ATTRIBUTE_UNUSED) +{ if (reader == NULL) - return(-1); + return(-1); - if (rng == NULL) { - if (reader->rngValidCtxt != NULL) { + if ((rng != NULL) && (ctxt != NULL)) + return (-1); + + if (((rng != NULL) || (ctxt != NULL)) && + ((reader->mode != XML_TEXTREADER_MODE_INITIAL) || + (reader->ctxt == NULL))) + return(-1); + + /* Cleanup previous validation stuff. */ + if (reader->rngValidCtxt != NULL) { + if ( !reader->rngPreserveCtxt) xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); - reader->rngValidCtxt = NULL; - } - if (reader->rngSchemas != NULL) { - xmlRelaxNGFree(reader->rngSchemas); - reader->rngSchemas = NULL; - } - return(0); + reader->rngValidCtxt = NULL; } - if (reader->mode != XML_TEXTREADER_MODE_INITIAL) - return(-1); + reader->rngPreserveCtxt = 0; if (reader->rngSchemas != NULL) { xmlRelaxNGFree(reader->rngSchemas); reader->rngSchemas = NULL; } - if (reader->rngValidCtxt != NULL) { - xmlRelaxNGFreeValidCtxt(reader->rngValidCtxt); - reader->rngValidCtxt = NULL; + + if ((rng == NULL) && (ctxt == NULL)) { + /* We just want to deactivate the validation, so get out. */ + return(0); } - ctxt = xmlRelaxNGNewParserCtxt(rng); - if (reader->errorFunc != NULL) { - xmlRelaxNGSetParserErrors(ctxt, - xmlTextReaderValidityErrorRelay, - xmlTextReaderValidityWarningRelay, - reader); + + + if (rng != NULL) { + xmlRelaxNGParserCtxtPtr pctxt; + /* Parse the schema and create validation environment. */ + + pctxt = xmlRelaxNGNewParserCtxt(rng); + if (reader->errorFunc != NULL) { + xmlRelaxNGSetParserErrors(pctxt, + xmlTextReaderValidityErrorRelay, + xmlTextReaderValidityWarningRelay, + reader); + } + if (reader->sErrorFunc != NULL) { + xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt, + xmlTextReaderValidityStructuredRelay, + reader); + } + reader->rngSchemas = xmlRelaxNGParse(pctxt); + xmlRelaxNGFreeParserCtxt(pctxt); + if (reader->rngSchemas == NULL) + return(-1); + reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(reader->rngSchemas); + if (reader->rngValidCtxt == NULL) { + xmlRelaxNGFree(reader->rngSchemas); + reader->rngSchemas = NULL; + return(-1); + } + } else { + /* Use the given validation context. */ + reader->rngValidCtxt = ctxt; + reader->rngPreserveCtxt = 1; } - if (reader->sErrorFunc != NULL) { - xmlRelaxNGSetValidStructuredErrors(reader->rngValidCtxt, - xmlTextReaderValidityStructuredRelay, - reader); - } - reader->rngSchemas = xmlRelaxNGParse(ctxt); - xmlRelaxNGFreeParserCtxt(ctxt); - if (reader->rngSchemas == NULL) - return(-1); - reader->rngValidCtxt = xmlRelaxNGNewValidCtxt(reader->rngSchemas); - if (reader->rngValidCtxt == NULL) { - xmlRelaxNGFree(reader->rngSchemas); - reader->rngSchemas = NULL; - return(-1); - } + /* + * Redirect the validation context's error channels to use + * the reader channels. + * TODO: In case the user provides the validation context we + * could make this redirection optional. + */ if (reader->errorFunc != NULL) { xmlRelaxNGSetValidErrors(reader->rngValidCtxt, xmlTextReaderValidityErrorRelay, @@ -4387,6 +4493,9 @@ return(-1); } } + xmlSchemaValidateSetLocator(reader->xsdValidCtxt, + xmlTextReaderLocator, + (void *) reader); /* * Redirect the validation context's error channels to use * the reader channels. @@ -4447,6 +4556,46 @@ { return(xmlTextReaderSchemaValidateInternal(reader, xsd, NULL, 0)); } + +/** + * xmlTextReaderRelaxNGValidateCtxt: + * @reader: the xmlTextReaderPtr used + * @ctxt: the RelaxNG schema validation context or NULL + * @options: options (not used yet) + * + * Use RelaxNG schema context to validate the document as it is processed. + * Activation is only possible before the first Read(). + * If @ctxt is NULL, then RelaxNG schema validation is deactivated. + * + * Returns 0 in case the schemas validation could be (de)activated and + * -1 in case of error. + */ +int +xmlTextReaderRelaxNGValidateCtxt(xmlTextReaderPtr reader, + xmlRelaxNGValidCtxtPtr ctxt, + int options) +{ + return(xmlTextReaderRelaxNGValidateInternal(reader, NULL, ctxt, options)); +} + +/** + * xmlTextReaderRelaxNGValidate: + * @reader: the xmlTextReaderPtr used + * @rng: the path to a RelaxNG schema or NULL + * + * Use RelaxNG schema to validate the document as it is processed. + * Activation is only possible before the first Read(). + * If @rng is NULL, then RelaxNG schema validation is deactivated. + * + * Returns 0 in case the schemas validation could be (de)activated and + * -1 in case of error. + */ +int +xmlTextReaderRelaxNGValidate(xmlTextReaderPtr reader, const char *rng) +{ + return(xmlTextReaderRelaxNGValidateInternal(reader, rng, NULL, 0)); +} + #endif /** @@ -4964,7 +5113,7 @@ reader->allocs |= XML_TEXTREADER_INPUT; } if (reader->buffer == NULL) - reader->buffer = xmlBufferCreateSize(100); + reader->buffer = xmlBufCreateSize(100); if (reader->buffer == NULL) { xmlGenericError(xmlGenericErrorContext, "xmlTextReaderSetup : malloc failed\n"); @@ -5005,13 +5154,14 @@ reader->node = NULL; reader->curnode = NULL; if (input != NULL) { - if (reader->input->buffer->use < 4) { + if (xmlBufUse(reader->input->buffer) < 4) { xmlParserInputBufferRead(input, 4); } if (reader->ctxt == NULL) { - if (reader->input->buffer->use >= 4) { + if (xmlBufUse(reader->input->buffer) >= 4) { reader->ctxt = xmlCreatePushParserCtxt(reader->sax, NULL, - (const char *) reader->input->buffer->content, 4, URL); + (const char *) xmlBufContent(reader->input->buffer), + 4, URL); reader->base = 0; reader->cur = 4; } else { @@ -5040,10 +5190,7 @@ inputStream->filename = (char *) xmlCanonicPath((const xmlChar *) URL); inputStream->buf = buf; - inputStream->base = inputStream->buf->buffer->content; - inputStream->cur = inputStream->buf->buffer->content; - inputStream->end = - &inputStream->buf->buffer->content[inputStream->buf->buffer->use]; + xmlBufResetInput(buf->buffer, inputStream); inputPush(reader->ctxt, inputStream); reader->cur = 0; @@ -5331,8 +5478,11 @@ input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, XML_CHAR_ENCODING_NONE); - if (input == NULL) + if (input == NULL) { + if (ioclose != NULL) + ioclose(ioctx); return (NULL); + } reader = xmlNewTextReader(input, URL); if (reader == NULL) { xmlFreeParserInputBuffer(input); @@ -5549,10 +5699,14 @@ input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, XML_CHAR_ENCODING_NONE); - if (input == NULL) + if (input == NULL) { + if (ioclose != NULL) + ioclose(ioctx); return (-1); + } return (xmlTextReaderSetup(reader, input, URL, encoding, options)); } + /************************************************************************ * * * Utilities * Index: lib/3rdparty/libxml2/xmlregexp.c =================================================================== --- lib/3rdparty/libxml2/xmlregexp.c (revision 58294) +++ lib/3rdparty/libxml2/xmlregexp.c (working copy) @@ -44,6 +44,9 @@ #define MAX_PUSH 10000000 +#ifdef ERROR +#undef ERROR +#endif #define ERROR(str) \ ctxt->error = XML_REGEXP_COMPILE_ERROR; \ xmlRegexpErrCompile(ctxt, str); @@ -219,6 +222,7 @@ struct _xmlAutomataState { xmlRegStateType type; xmlRegMarkedType mark; + xmlRegMarkedType markd; xmlRegMarkedType reached; int no; int maxTrans; @@ -2586,6 +2590,8 @@ if (state == NULL) return(ret); + if (state->markd == XML_REGEXP_MARK_VISITED) + return(ret); if (ctxt->flags & AM_AUTOMATA_RNG) deep = 0; @@ -2603,8 +2609,10 @@ if (t1->atom == NULL) { if (t1->to < 0) continue; + state->markd = XML_REGEXP_MARK_VISITED; res = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to], to, atom); + state->markd = 0; if (res == 0) { ret = 0; /* t1->nd = 1; */ @@ -3199,7 +3207,7 @@ memset(exec->counts, 0, comp->nbCounters * sizeof(int)); } else exec->counts = NULL; - while ((exec->status == 0) && + while ((exec->status == 0) && (exec->state != NULL) && ((exec->inputString[exec->index] != 0) || ((exec->state != NULL) && (exec->state->type != XML_REGEXP_FINAL_STATE)))) { @@ -3453,6 +3461,8 @@ } xmlFree(exec->rollbacks); } + if (exec->state == NULL) + return(-1); if (exec->counts != NULL) xmlFree(exec->counts); if (exec->status == 0) @@ -5370,6 +5380,10 @@ end = ctxt->state; while ((CUR == '|') && (ctxt->error == 0)) { NEXT; + if (CUR == 0) { + ERROR("expecting a branch after |") + return; + } ctxt->state = start; ctxt->end = NULL; xmlFAParseBranch(ctxt, end); Index: lib/3rdparty/libxml2/xmlsave.c =================================================================== --- lib/3rdparty/libxml2/xmlsave.c (revision 58294) +++ lib/3rdparty/libxml2/xmlsave.c (working copy) @@ -19,6 +19,10 @@ #include +#include "buf.h" +#include "enc.h" +#include "save.h" + /************************************************************************ * * * XHTML detection * @@ -248,7 +252,7 @@ /* * We assume we have UTF-8 input. */ - if (outend - out < 10) break; + if (outend - out < 11) break; if (*in < 0xC0) { xmlSaveErr(XML_SAVE_NOT_UTF8, NULL, NULL); @@ -436,14 +440,14 @@ while (children != NULL) { switch (children->type) { case XML_TEXT_NODE: - xmlAttrSerializeTxtContent(buf->buffer, attr->doc, - attr, children->content); + xmlBufAttrSerializeTxtContent(buf->buffer, attr->doc, + attr, children->content); break; case XML_ENTITY_REF_NODE: - xmlBufferAdd(buf->buffer, BAD_CAST "&", 1); - xmlBufferAdd(buf->buffer, children->name, + xmlBufAdd(buf->buffer, BAD_CAST "&", 1); + xmlBufAdd(buf->buffer, children->name, xmlStrlen(children->name)); - xmlBufferAdd(buf->buffer, BAD_CAST ";", 1); + xmlBufAdd(buf->buffer, BAD_CAST ";", 1); break; default: /* should not happen unless we have a badly built tree */ @@ -453,6 +457,96 @@ } } +/** + * xmlBufDumpNotationTable: + * @buf: an xmlBufPtr output + * @table: A notation table + * + * This will dump the content of the notation table as an XML DTD definition + */ +void +xmlBufDumpNotationTable(xmlBufPtr buf, xmlNotationTablePtr table) { + xmlBufferPtr buffer; + + buffer = xmlBufferCreate(); + if (buffer == NULL) { + /* + * TODO set the error in buf + */ + return; + } + xmlDumpNotationTable(buffer, table); + xmlBufMergeBuffer(buf, buffer); +} + +/** + * xmlBufDumpElementDecl: + * @buf: an xmlBufPtr output + * @elem: An element table + * + * This will dump the content of the element declaration as an XML + * DTD definition + */ +void +xmlBufDumpElementDecl(xmlBufPtr buf, xmlElementPtr elem) { + xmlBufferPtr buffer; + + buffer = xmlBufferCreate(); + if (buffer == NULL) { + /* + * TODO set the error in buf + */ + return; + } + xmlDumpElementDecl(buffer, elem); + xmlBufMergeBuffer(buf, buffer); +} + +/** + * xmlBufDumpAttributeDecl: + * @buf: an xmlBufPtr output + * @attr: An attribute declaration + * + * This will dump the content of the attribute declaration as an XML + * DTD definition + */ +void +xmlBufDumpAttributeDecl(xmlBufPtr buf, xmlAttributePtr attr) { + xmlBufferPtr buffer; + + buffer = xmlBufferCreate(); + if (buffer == NULL) { + /* + * TODO set the error in buf + */ + return; + } + xmlDumpAttributeDecl(buffer, attr); + xmlBufMergeBuffer(buf, buffer); +} + +/** + * xmlBufDumpEntityDecl: + * @buf: an xmlBufPtr output + * @ent: An entity table + * + * This will dump the content of the entity table as an XML DTD definition + */ +void +xmlBufDumpEntityDecl(xmlBufPtr buf, xmlEntityPtr ent) { + xmlBufferPtr buffer; + + buffer = xmlBufferCreate(); + if (buffer == NULL) { + /* + * TODO set the error in buf + */ + return; + } + xmlDumpEntityDecl(buffer, ent); + xmlBufMergeBuffer(buf, buffer); +} + /************************************************************************ * * * Dumping XML tree content to an I/O output buffer * @@ -469,7 +563,7 @@ (const char *)encoding); return(-1); } - buf->conv = xmlBufferCreate(); + buf->conv = xmlBufCreate(); if (buf->conv == NULL) { xmlCharEncCloseFunc(buf->encoder); xmlSaveErrMemory("creating encoding buffer"); @@ -478,7 +572,7 @@ /* * initialize the state, e.g. if outputting a BOM */ - xmlCharEncOutFunc(buf->encoder, buf->conv, NULL); + xmlCharEncOutput(buf, 1); } return(0); } @@ -487,7 +581,7 @@ xmlOutputBufferPtr buf = ctxt->buf; xmlOutputBufferFlush(buf); xmlCharEncCloseFunc(buf->encoder); - xmlBufferFree(buf->conv); + xmlBufFree(buf->conv); buf->encoder = NULL; buf->conv = NULL; return(0); @@ -553,7 +647,7 @@ } else xmlOutputBufferWrite(buf, 5, "xmlns"); xmlOutputBufferWrite(buf, 1, "="); - xmlBufferWriteQuotedString(buf->buffer, cur->href); + xmlBufWriteQuotedString(buf->buffer, cur->href); } } @@ -623,12 +717,12 @@ xmlOutputBufferWriteString(buf, (const char *)dtd->name); if (dtd->ExternalID != NULL) { xmlOutputBufferWrite(buf, 8, " PUBLIC "); - xmlBufferWriteQuotedString(buf->buffer, dtd->ExternalID); + xmlBufWriteQuotedString(buf->buffer, dtd->ExternalID); xmlOutputBufferWrite(buf, 1, " "); - xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID); + xmlBufWriteQuotedString(buf->buffer, dtd->SystemID); } else if (dtd->SystemID != NULL) { xmlOutputBufferWrite(buf, 8, " SYSTEM "); - xmlBufferWriteQuotedString(buf->buffer, dtd->SystemID); + xmlBufWriteQuotedString(buf->buffer, dtd->SystemID); } if ((dtd->entities == NULL) && (dtd->elements == NULL) && (dtd->attributes == NULL) && (dtd->notations == NULL) && @@ -643,7 +737,8 @@ */ if ((dtd->notations != NULL) && ((dtd->doc == NULL) || (dtd->doc->intSubset == dtd))) { - xmlDumpNotationTable(buf->buffer, (xmlNotationTablePtr) dtd->notations); + xmlBufDumpNotationTable(buf->buffer, + (xmlNotationTablePtr) dtd->notations); } format = ctxt->format; level = ctxt->level; @@ -841,15 +936,15 @@ return; } if (cur->type == XML_ELEMENT_DECL) { - xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur); + xmlBufDumpElementDecl(buf->buffer, (xmlElementPtr) cur); return; } if (cur->type == XML_ATTRIBUTE_DECL) { - xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur); + xmlBufDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur); return; } if (cur->type == XML_ENTITY_DECL) { - xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur); + xmlBufDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur); return; } if (cur->type == XML_TEXT_NODE) { @@ -1097,12 +1192,12 @@ if ((ctxt->options & XML_SAVE_NO_DECL) == 0) { xmlOutputBufferWrite(buf, 14, "version != NULL) - xmlBufferWriteQuotedString(buf->buffer, cur->version); + xmlBufWriteQuotedString(buf->buffer, cur->version); else xmlOutputBufferWrite(buf, 5, "\"1.0\""); if (encoding != NULL) { xmlOutputBufferWrite(buf, 10, " encoding="); - xmlBufferWriteQuotedString(buf->buffer, (xmlChar *) encoding); + xmlBufWriteQuotedString(buf->buffer, (xmlChar *) encoding); } switch (cur->standalone) { case 0: @@ -1370,6 +1465,10 @@ return; if (cur->type == XML_XINCLUDE_END) return; + if (cur->type == XML_NAMESPACE_DECL) { + xmlNsDumpOutputCtxt(ctxt, (xmlNsPtr) cur); + return; + } if (cur->type == XML_DTD_NODE) { xmlDtdDumpOutput(ctxt, (xmlDtdPtr) cur); return; @@ -1380,15 +1479,15 @@ } buf = ctxt->buf; if (cur->type == XML_ELEMENT_DECL) { - xmlDumpElementDecl(buf->buffer, (xmlElementPtr) cur); + xmlBufDumpElementDecl(buf->buffer, (xmlElementPtr) cur); return; } if (cur->type == XML_ATTRIBUTE_DECL) { - xmlDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur); + xmlBufDumpAttributeDecl(buf->buffer, (xmlAttributePtr) cur); return; } if (cur->type == XML_ENTITY_DECL) { - xmlDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur); + xmlBufDumpEntityDecl(buf->buffer, (xmlEntityPtr) cur); return; } if (cur->type == XML_TEXT_NODE) { @@ -1936,18 +2035,19 @@ * Public entry points based on buffers * * * ************************************************************************/ + /** - * xmlAttrSerializeTxtContent: - * @buf: the XML buffer output + * xmlBufAttrSerializeTxtContent: + * @buf: and xmlBufPtr output * @doc: the document * @attr: the attribute node * @string: the text content * - * Serialize text attribute values to an xml simple buffer + * Serialize text attribute values to an xmlBufPtr */ void -xmlAttrSerializeTxtContent(xmlBufferPtr buf, xmlDocPtr doc, - xmlAttrPtr attr, const xmlChar * string) +xmlBufAttrSerializeTxtContent(xmlBufPtr buf, xmlDocPtr doc, + xmlAttrPtr attr, const xmlChar * string) { xmlChar *base, *cur; @@ -1957,44 +2057,44 @@ while (*cur != 0) { if (*cur == '\n') { if (base != cur) - xmlBufferAdd(buf, base, cur - base); - xmlBufferAdd(buf, BAD_CAST " ", 5); + xmlBufAdd(buf, base, cur - base); + xmlBufAdd(buf, BAD_CAST " ", 5); cur++; base = cur; } else if (*cur == '\r') { if (base != cur) - xmlBufferAdd(buf, base, cur - base); - xmlBufferAdd(buf, BAD_CAST " ", 5); + xmlBufAdd(buf, base, cur - base); + xmlBufAdd(buf, BAD_CAST " ", 5); cur++; base = cur; } else if (*cur == '\t') { if (base != cur) - xmlBufferAdd(buf, base, cur - base); - xmlBufferAdd(buf, BAD_CAST " ", 4); + xmlBufAdd(buf, base, cur - base); + xmlBufAdd(buf, BAD_CAST " ", 4); cur++; base = cur; } else if (*cur == '"') { if (base != cur) - xmlBufferAdd(buf, base, cur - base); - xmlBufferAdd(buf, BAD_CAST """, 6); + xmlBufAdd(buf, base, cur - base); + xmlBufAdd(buf, BAD_CAST """, 6); cur++; base = cur; } else if (*cur == '<') { if (base != cur) - xmlBufferAdd(buf, base, cur - base); - xmlBufferAdd(buf, BAD_CAST "<", 4); + xmlBufAdd(buf, base, cur - base); + xmlBufAdd(buf, BAD_CAST "<", 4); cur++; base = cur; } else if (*cur == '>') { if (base != cur) - xmlBufferAdd(buf, base, cur - base); - xmlBufferAdd(buf, BAD_CAST ">", 4); + xmlBufAdd(buf, base, cur - base); + xmlBufAdd(buf, BAD_CAST ">", 4); cur++; base = cur; } else if (*cur == '&') { if (base != cur) - xmlBufferAdd(buf, base, cur - base); - xmlBufferAdd(buf, BAD_CAST "&", 5); + xmlBufAdd(buf, base, cur - base); + xmlBufAdd(buf, BAD_CAST "&", 5); cur++; base = cur; } else if ((*cur >= 0x80) && ((doc == NULL) || @@ -2002,17 +2102,17 @@ /* * We assume we have UTF-8 content. */ - unsigned char tmp[10]; + unsigned char tmp[12]; int val = 0, l = 1; if (base != cur) - xmlBufferAdd(buf, base, cur - base); + xmlBufAdd(buf, base, cur - base); if (*cur < 0xC0) { xmlSaveErr(XML_SAVE_NOT_UTF8, (xmlNodePtr) attr, NULL); if (doc != NULL) doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1"); xmlSerializeHexCharRef(tmp, *cur); - xmlBufferAdd(buf, (xmlChar *) tmp, -1); + xmlBufAdd(buf, (xmlChar *) tmp, -1); cur++; base = cur; continue; @@ -2044,7 +2144,7 @@ doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1"); xmlSerializeHexCharRef(tmp, *cur); - xmlBufferAdd(buf, (xmlChar *) tmp, -1); + xmlBufAdd(buf, (xmlChar *) tmp, -1); cur++; base = cur; continue; @@ -2054,7 +2154,7 @@ * as a char ref */ xmlSerializeHexCharRef(tmp, val); - xmlBufferAdd(buf, (xmlChar *) tmp, -1); + xmlBufAdd(buf, (xmlChar *) tmp, -1); cur += l; base = cur; } else { @@ -2062,10 +2162,34 @@ } } if (base != cur) - xmlBufferAdd(buf, base, cur - base); + xmlBufAdd(buf, base, cur - base); } /** + * xmlAttrSerializeTxtContent: + * @buf: the XML buffer output + * @doc: the document + * @attr: the attribute node + * @string: the text content + * + * Serialize text attribute values to an xml simple buffer + */ +void +xmlAttrSerializeTxtContent(xmlBufferPtr buf, xmlDocPtr doc, + xmlAttrPtr attr, const xmlChar * string) +{ + xmlBufPtr buffer; + + if ((buf == NULL) || (string == NULL)) + return; + buffer = xmlBufFromBuffer(buf); + if (buffer == NULL) + return; + xmlBufAttrSerializeTxtContent(buffer, doc, attr, string); + xmlBufBackToBuffer(buffer); +} + +/** * xmlNodeDump: * @buf: the XML buffer output * @doc: the document @@ -2076,6 +2200,8 @@ * Dump an XML node, recursive behaviour,children are printed too. * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 * or xmlKeepBlanksDefault(0) was called + * Since this is using xmlBuffer structures it is limited to 2GB and somehow + * deprecated, use xmlBufNodeDump() instead. * * Returns the number of bytes written to the buffer or -1 in case of error */ @@ -2083,8 +2209,43 @@ xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, int format) { - unsigned int use; + xmlBufPtr buffer; int ret; + + if ((buf == NULL) || (cur == NULL)) + return(-1); + buffer = xmlBufFromBuffer(buf); + if (buffer == NULL) + return(-1); + ret = xmlBufNodeDump(buffer, doc, cur, level, format); + xmlBufBackToBuffer(buffer); + if (ret > INT_MAX) + return(-1); + return((int) ret); +} + +/** + * xmlBufNodeDump: + * @buf: the XML buffer output + * @doc: the document + * @cur: the current node + * @level: the imbrication level for indenting + * @format: is formatting allowed + * + * Dump an XML node, recursive behaviour,children are printed too. + * Note that @format = 1 provide node indenting only if xmlIndentTreeOutput = 1 + * or xmlKeepBlanksDefault(0) was called + * + * Returns the number of bytes written to the buffer, in case of error 0 + * is returned or @buf stores the error + */ + +size_t +xmlBufNodeDump(xmlBufPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, + int format) +{ + size_t use; + int ret; xmlOutputBufferPtr outbuf; xmlInitParser(); @@ -2116,10 +2277,10 @@ outbuf->context = NULL; outbuf->written = 0; - use = buf->use; + use = xmlBufUse(buf); xmlNodeDumpOutput(outbuf, doc, cur, level, format, NULL); xmlFree(outbuf); - ret = buf->use - use; + ret = xmlBufUse(buf) - use; return (ret); } @@ -2299,11 +2460,11 @@ xmlDocContentDumpOutput(&ctxt, out_doc); xmlOutputBufferFlush(out_buff); if (out_buff->conv != NULL) { - *doc_txt_len = out_buff->conv->use; - *doc_txt_ptr = xmlStrndup(out_buff->conv->content, *doc_txt_len); + *doc_txt_len = xmlBufUse(out_buff->conv); + *doc_txt_ptr = xmlStrndup(xmlBufContent(out_buff->conv), *doc_txt_len); } else { - *doc_txt_len = out_buff->buffer->use; - *doc_txt_ptr = xmlStrndup(out_buff->buffer->content, *doc_txt_len); + *doc_txt_len = xmlBufUse(out_buff->buffer); + *doc_txt_ptr = xmlStrndup(xmlBufContent(out_buff->buffer),*doc_txt_len); } (void)xmlOutputBufferClose(out_buff); Index: lib/3rdparty/libxml2/xmlschemas.c =================================================================== --- lib/3rdparty/libxml2/xmlschemas.c (revision 58294) +++ lib/3rdparty/libxml2/xmlschemas.c (working copy) @@ -975,6 +975,7 @@ xmlSAXHandlerPtr sax; xmlParserCtxtPtr parserCtxt; void *user_data; /* TODO: What is this for? */ + char *filename; int err; int nberrors; @@ -1028,6 +1029,10 @@ int hasKeyrefs; int createIDCNodeTables; int psviExposeIDCNodeTables; + + /* Locator for error reporting in streaming mode */ + xmlSchemaValidityLocatorFunc locFunc; + void *locCtxt; }; /** @@ -2078,6 +2083,20 @@ (vctxt->parserCtxt->input != NULL)) file = vctxt->parserCtxt->input->filename; } + if (vctxt->locFunc != NULL) { + if ((file == NULL) || (line == 0)) { + unsigned long l; + const char *f; + vctxt->locFunc(vctxt->locCtxt, &f, &l); + if (file == NULL) + file = f; + if (line == 0) + line = (int) l; + } + } + if ((file == NULL) && (vctxt->filename != NULL)) + file = vctxt->filename; + __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASV, error, errorLevel, file, line, @@ -12938,6 +12957,15 @@ if (tmp2 != 1) ret = 0; sub = sub->next; } + + /* + * epsilon needed to block previous trans from + * being allowed to enter back from another + * construct + */ + pctxt->state = xmlAutomataNewEpsilon(pctxt->am, + pctxt->state, NULL); + if (particle->minOccurs == 0) { xmlAutomataNewEpsilon(pctxt->am, oldstate, pctxt->state); @@ -13946,7 +13974,7 @@ */ if ((sub->negNsSet != NULL) && (super->negNsSet != NULL) && - (sub->negNsSet->value == sub->negNsSet->value)) + (sub->negNsSet->value == super->negNsSet->value)) return (0); /* * 3.1 sub must be a set whose members are either namespace names or �absent�. @@ -15156,9 +15184,10 @@ FREE_AND_NULL(str) return (XML_SCHEMAP_ST_PROPS_CORRECT_1); } - if ( (WXS_IS_LIST(type) || WXS_IS_UNION(type)) && - (WXS_IS_RESTRICTION(type) == 0) && - (! WXS_IS_ANY_SIMPLE_TYPE(baseType))) { + if ((WXS_IS_LIST(type) || WXS_IS_UNION(type)) && + (WXS_IS_RESTRICTION(type) == 0) && + ((! WXS_IS_ANY_SIMPLE_TYPE(baseType)) && + (baseType->type != XML_SCHEMA_TYPE_SIMPLE))) { xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_ST_PROPS_CORRECT_1, WXS_BASIC_CAST type, NULL, @@ -18475,8 +18504,8 @@ particle->children->children = (xmlSchemaTreeItemPtr) xmlSchemaAddParticle(pctxt, type->node, - ((xmlSchemaParticlePtr) type->subtypes)->minOccurs, - ((xmlSchemaParticlePtr) type->subtypes)->maxOccurs); + ((xmlSchemaParticlePtr) baseType->subtypes)->minOccurs, + ((xmlSchemaParticlePtr) baseType->subtypes)->maxOccurs); if (particle->children->children == NULL) goto exit_failure; particle = (xmlSchemaParticlePtr) @@ -21936,7 +21965,7 @@ /************************************************************************ * * - * Validation of identity-constraints (IDC) * + * Validation of identity-constraints (IDC) * * * ************************************************************************/ @@ -27449,8 +27478,28 @@ } /** + * xmlSchemaValidateSetFilename: + * @vctxt: the schema validation context + * @filename: the file name + * + * Workaround to provide file error reporting information when this is + * not provided by current APIs + */ +void +xmlSchemaValidateSetFilename(xmlSchemaValidCtxtPtr vctxt, const char *filename) { + if (vctxt == NULL) + return; + if (vctxt->filename != NULL) + xmlFree(vctxt->filename); + if (filename != NULL) + vctxt->filename = (char *) xmlStrdup((const xmlChar *) filename); + else + vctxt->filename = NULL; +} + +/** * xmlSchemaClearValidCtxt: - * @ctxt: the schema validation context + * @vctxt: the schema validation context * * Free the resources associated to the schema validation context; * leaves some fields alive intended for reuse of the context. @@ -27551,6 +27600,11 @@ * where the user provides the dict? */ vctxt->dict = xmlDictCreate(); + + if (vctxt->filename != NULL) { + xmlFree(vctxt->filename); + vctxt->filename = NULL; + } } /** @@ -27636,6 +27690,8 @@ xmlSchemaItemListFree(ctxt->nodeQNames); if (ctxt->dict != NULL) xmlDictFree(ctxt->dict); + if (ctxt->filename != NULL) + xmlFree(ctxt->filename); xmlFree(ctxt); } @@ -28630,6 +28686,63 @@ } /** + * xmlSchemaValidateSetLocator: + * @vctxt: a schema validation context + * @f: the locator function pointer + * @ctxt: the locator context + * + * Allows to set a locator function to the validation context, + * which will be used to provide file and line information since + * those are not provided as part of the SAX validation flow + * Setting @f to NULL disable the locator. + */ + +void +xmlSchemaValidateSetLocator(xmlSchemaValidCtxtPtr vctxt, + xmlSchemaValidityLocatorFunc f, + void *ctxt) +{ + if (vctxt == NULL) return; + vctxt->locFunc = f; + vctxt->locCtxt = ctxt; +} + +/** + * xmlSchemaValidateStreamLocator: + * @ctx: the xmlTextReaderPtr used + * @file: returned file information + * @line: returned line information + * + * Internal locator function for the readers + * + * Returns 0 in case the Schema validation could be (des)activated and + * -1 in case of error. + */ +static int +xmlSchemaValidateStreamLocator(void *ctx, const char **file, + unsigned long *line) { + xmlParserCtxtPtr ctxt; + + if ((ctx == NULL) || ((file == NULL) && (line == NULL))) + return(-1); + + if (file != NULL) + *file = NULL; + if (line != NULL) + *line = 0; + + ctxt = (xmlParserCtxtPtr) ctx; + if (ctxt->input != NULL) { + if (file != NULL) + *file = ctxt->input->filename; + if (line != NULL) + *line = ctxt->input->line; + return(0); + } + return(-1); +} + +/** * xmlSchemaValidateStream: * @ctxt: a schema validation context * @input: the input to use for reading the data @@ -28672,6 +28785,7 @@ xmlCtxtUseOptions(pctxt, options); #endif pctxt->linenumbers = 1; + xmlSchemaValidateSetLocator(ctxt, xmlSchemaValidateStreamLocator, pctxt); inputStream = xmlNewIOInputStream(pctxt, input, enc);; if (inputStream == NULL) { Index: lib/3rdparty/libxml2/xmlschemastypes.c =================================================================== --- lib/3rdparty/libxml2/xmlschemastypes.c (revision 58294) +++ lib/3rdparty/libxml2/xmlschemastypes.c (working copy) @@ -3569,8 +3569,8 @@ /* seconds */ sec = x->value.dur.sec - y->value.dur.sec; - carry = (long)sec / SECS_PER_DAY; - sec -= (double)(carry * SECS_PER_DAY); + carry = (long)(sec / SECS_PER_DAY); + sec -= ((double)carry) * SECS_PER_DAY; /* days */ day = x->value.dur.day - y->value.dur.day + carry; @@ -5122,7 +5122,7 @@ */ static int xmlSchemaValidateLengthFacetInternal(xmlSchemaFacetPtr facet, - xmlSchemaTypeType valType, + xmlSchemaValType valType, const xmlChar *value, xmlSchemaValPtr val, unsigned long *length, Index: lib/3rdparty/libxml2/xmlwriter.c =================================================================== --- lib/3rdparty/libxml2/xmlwriter.c (revision 58294) +++ lib/3rdparty/libxml2/xmlwriter.c (working copy) @@ -21,6 +21,10 @@ #include +#include "buf.h" +#include "enc.h" +#include "save.h" + #define B64LINELEN 72 #define B64CRLF "\r\n" @@ -548,9 +552,9 @@ writer->out->encoder = encoder; if (encoder != NULL) { if (writer->out->conv == NULL) { - writer->out->conv = xmlBufferCreateSize(4000); + writer->out->conv = xmlBufCreateSize(4000); } - xmlCharEncOutFunc(encoder, writer->out->conv, NULL); + xmlCharEncOutput(writer->out, 1); if ((writer->doc != NULL) && (writer->doc->encoding == NULL)) writer->doc->encoding = xmlStrdup((xmlChar *)writer->out->encoder->name); } else @@ -1501,8 +1505,8 @@ break; case XML_TEXTWRITER_ATTRIBUTE: buf = NULL; - xmlAttrSerializeTxtContent(writer->out->buffer, writer->doc, - NULL, content); + xmlBufAttrSerializeTxtContent(writer->out->buffer, + writer->doc, NULL, content); break; default: break; @@ -4606,6 +4610,26 @@ } /** + * xmlTextWriterSetQuoteChar: + * @writer: the xmlTextWriterPtr + * @quotechar: the quote character + * + * Set the character used for quoting attributes. + * + * Returns -1 on error or 0 otherwise. + */ +int +xmlTextWriterSetQuoteChar(xmlTextWriterPtr writer, xmlChar quotechar) +{ + if ((writer == NULL) || ((quotechar != '\'') && (quotechar != '"'))) + return -1; + + writer->qchar = quotechar; + + return 0; +} + +/** * xmlTextWriterWriteIndent: * @writer: the xmlTextWriterPtr * Index: lib/3rdparty/libxml2/xpath.c =================================================================== --- lib/3rdparty/libxml2/xpath.c (revision 58294) +++ lib/3rdparty/libxml2/xpath.c (working copy) @@ -55,6 +55,8 @@ #include #endif +#include "buf.h" + #ifdef LIBXML_PATTERN_ENABLED #define XPATH_STREAMING #endif @@ -64,6 +66,15 @@ "Unimplemented block at %s:%d\n", \ __FILE__, __LINE__); +/** + * WITH_TIM_SORT: + * + * Use the Timsort algorithm provided in timsort.h to sort + * nodeset as this is a great improvement over the old Shell sort + * used in xmlXPathNodeSetSort() + */ +#define WITH_TIM_SORT + /* * XP_OPTIMIZED_NON_ELEM_COMPARISON: * If defined, this will use xmlXPathCmpNodesExt() instead of @@ -90,12 +101,76 @@ /* #define XP_DEBUG_OBJ_USAGE */ /* + * XPATH_MAX_STEPS: + * when compiling an XPath expression we arbitrary limit the maximum + * number of step operation in the compiled expression. 1000000 is + * an insanely large value which should never be reached under normal + * circumstances + */ +#define XPATH_MAX_STEPS 1000000 + +/* + * XPATH_MAX_STACK_DEPTH: + * when evaluating an XPath expression we arbitrary limit the maximum + * number of object allowed to be pushed on the stack. 1000000 is + * an insanely large value which should never be reached under normal + * circumstances + */ +#define XPATH_MAX_STACK_DEPTH 1000000 + +/* + * XPATH_MAX_NODESET_LENGTH: + * when evaluating an XPath expression nodesets are created and we + * arbitrary limit the maximum length of those node set. 10000000 is + * an insanely large value which should never be reached under normal + * circumstances, one would first need to construct an in memory tree + * with more than 10 millions nodes. + */ +#define XPATH_MAX_NODESET_LENGTH 10000000 + +/* * TODO: * There are a few spots where some tests are done which depend upon ascii * data. These should be enhanced for full UTF8 support (see particularly * any use of the macros IS_ASCII_CHARACTER and IS_ASCII_DIGIT) */ +/* + * Wrapper for the Timsort argorithm from timsort.h + */ +#ifdef WITH_TIM_SORT +#define SORT_NAME libxml_domnode +#define SORT_TYPE xmlNodePtr +/** + * wrap_cmp: + * @x: a node + * @y: another node + * + * Comparison function for the Timsort implementation + * + * Returns -2 in case of error -1 if first point < second point, 0 if + * it's the same node, +1 otherwise + */ +static +int wrap_cmp( xmlNodePtr x, xmlNodePtr y ); +#ifdef XP_OPTIMIZED_NON_ELEM_COMPARISON + static int xmlXPathCmpNodesExt(xmlNodePtr, xmlNodePtr); + static int wrap_cmp( xmlNodePtr x, xmlNodePtr y ) + { + int res = xmlXPathCmpNodesExt(x, y); + return res == -2 ? res : -res; + } +#else + static int wrap_cmp( xmlNodePtr x, xmlNodePtr y ) + { + int res = xmlXPathCmpNodes(x, y); + return res == -2 ? res : -res; + } +#endif +#define SORT_CMP(x, y) (wrap_cmp(x, y)) +#include "timsort.h" +#endif /* WITH_TIM_SORT */ + #if defined(LIBXML_XPATH_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED) /************************************************************************ @@ -252,6 +327,8 @@ "Encoding error\n", "Char out of XML range\n", "Invalid or incomplete context\n", + "Stack usage errror\n", + "Forbidden variable\n", "?? Unknown error ??\n" /* Must be last in the list! */ }; #define MAXERRNO ((int)(sizeof(xmlXPathErrorMessages) / \ @@ -419,8 +496,7 @@ if (list->items == NULL) { if (initialSize <= 0) initialSize = 1; - list->items = (void **) xmlMalloc( - initialSize * sizeof(void *)); + list->items = (void **) xmlMalloc(initialSize * sizeof(void *)); if (list->items == NULL) { xmlXPathErrMemory(NULL, "xmlPointerListCreate: allocating item\n"); @@ -429,12 +505,17 @@ list->number = 0; list->size = initialSize; } else if (list->size <= list->number) { + if (list->size > 50000000) { + xmlXPathErrMemory(NULL, + "xmlPointerListAddSize: re-allocating item\n"); + return(-1); + } list->size *= 2; list->items = (void **) xmlRealloc(list->items, list->size * sizeof(void *)); if (list->items == NULL) { xmlXPathErrMemory(NULL, - "xmlPointerListCreate: re-allocating item\n"); + "xmlPointerListAddSize: re-allocating item\n"); list->size = 0; return(-1); } @@ -552,8 +633,6 @@ NODE_TYPE_PI = XML_PI_NODE } xmlXPathTypeVal; -#define XP_REWRITE_DOS_CHILD_ELEM 1 - typedef struct _xmlXPathStepOp xmlXPathStepOp; typedef xmlXPathStepOp *xmlXPathStepOpPtr; struct _xmlXPathStepOp { @@ -567,7 +646,6 @@ void *value5; void *cache; void *cacheURI; - int rewriteType; }; struct _xmlXPathCompExpr { @@ -722,6 +800,10 @@ if (comp->nbStep >= comp->maxStep) { xmlXPathStepOp *real; + if (comp->maxStep >= XPATH_MAX_STEPS) { + xmlXPathErrMemory(NULL, "adding step\n"); + return(-1); + } comp->maxStep *= 2; real = (xmlXPathStepOp *) xmlRealloc(comp->steps, comp->maxStep * sizeof(xmlXPathStepOp)); @@ -733,7 +815,6 @@ comp->steps = real; } comp->last = comp->nbStep; - comp->steps[comp->nbStep].rewriteType = 0; comp->steps[comp->nbStep].ch1 = ch1; comp->steps[comp->nbStep].ch2 = ch2; comp->steps[comp->nbStep].op = op; @@ -2051,6 +2132,11 @@ ret->type = XPATH_NODESET; ret->boolval = 0; ret->nodesetval = xmlXPathNodeSetCreate(val); + if (ret->nodesetval == NULL) { + ctxt->lastError.domain = XML_FROM_XPATH; + ctxt->lastError.code = XML_ERR_NO_MEMORY; + return(NULL); + } #ifdef XP_DEBUG_OBJ_USAGE xmlXPathDebugObjUsageRequested(ctxt, XPATH_NODESET); #endif @@ -2398,6 +2484,42 @@ ************************************************************************/ /** + * xmlXPathSetFrame: + * @ctxt: an XPath parser context + * + * Set the callee evaluation frame + * + * Returns the previous frame value to be restored once done + */ +static int +xmlXPathSetFrame(xmlXPathParserContextPtr ctxt) { + int ret; + + if (ctxt == NULL) + return(0); + ret = ctxt->valueFrame; + ctxt->valueFrame = ctxt->valueNr; + return(ret); +} + +/** + * xmlXPathPopFrame: + * @ctxt: an XPath parser context + * @frame: the previous frame value + * + * Remove the callee evaluation frame + */ +static void +xmlXPathPopFrame(xmlXPathParserContextPtr ctxt, int frame) { + if (ctxt == NULL) + return; + if (ctxt->valueNr < ctxt->valueFrame) { + xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_STACK_ERROR); + } + ctxt->valueFrame = frame; +} + +/** * valuePop: * @ctxt: an XPath evaluation context * @@ -2412,6 +2534,12 @@ if ((ctxt == NULL) || (ctxt->valueNr <= 0)) return (NULL); + + if (ctxt->valueNr <= ctxt->valueFrame) { + xmlXPatherror(ctxt, __FILE__, __LINE__, XPATH_STACK_ERROR); + return (NULL); + } + ctxt->valueNr--; if (ctxt->valueNr > 0) ctxt->value = ctxt->valueTab[ctxt->valueNr - 1]; @@ -2437,11 +2565,17 @@ if (ctxt->valueNr >= ctxt->valueMax) { xmlXPathObjectPtr *tmp; + if (ctxt->valueMax >= XPATH_MAX_STACK_DEPTH) { + xmlXPathErrMemory(NULL, "XPath stack depth limit reached\n"); + ctxt->error = XPATH_MEMORY_ERROR; + return (0); + } tmp = (xmlXPathObjectPtr *) xmlRealloc(ctxt->valueTab, 2 * ctxt->valueMax * sizeof(ctxt->valueTab[0])); if (tmp == NULL) { - xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); + xmlXPathErrMemory(NULL, "pushing value\n"); + ctxt->error = XPATH_MEMORY_ERROR; return (0); } ctxt->valueMax *= 2; @@ -3276,13 +3410,19 @@ */ void xmlXPathNodeSetSort(xmlNodeSetPtr set) { +#ifndef WITH_TIM_SORT int i, j, incr, len; xmlNodePtr tmp; +#endif if (set == NULL) return; - /* Use Shell's sort to sort the node-set */ +#ifndef WITH_TIM_SORT + /* + * Use the old Shell's sort implementation to sort the node-set + * Timsort ought to be quite faster + */ len = set->nodeNr; for (incr = len / 2; incr > 0; incr /= 2) { for (i = incr; i < len; i++) { @@ -3305,6 +3445,9 @@ } } } +#else /* WITH_TIM_SORT */ + libxml_domnode_tim_sort(set->nodeTab, set->nodeNr); +#endif /* WITH_TIM_SORT */ } #define XML_NODESET_DEFAULT 10 @@ -3483,8 +3626,10 @@ * @ns: a the namespace node * * add a new namespace node to an existing NodeSet + * + * Returns 0 in case of success and -1 in case of error */ -void +int xmlXPathNodeSetAddNs(xmlNodeSetPtr cur, xmlNodePtr node, xmlNsPtr ns) { int i; @@ -3492,7 +3637,7 @@ if ((cur == NULL) || (ns == NULL) || (node == NULL) || (ns->type != XML_NAMESPACE_DECL) || (node->type != XML_ELEMENT_NODE)) - return; + return(-1); /* @@ with_ns to check whether namespace nodes should be looked at @@ */ /* @@ -3503,7 +3648,7 @@ (cur->nodeTab[i]->type == XML_NAMESPACE_DECL) && (((xmlNsPtr)cur->nodeTab[i])->next == (xmlNsPtr) node) && (xmlStrEqual(ns->prefix, ((xmlNsPtr)cur->nodeTab[i])->prefix))) - return; + return(0); } /* @@ -3514,7 +3659,7 @@ sizeof(xmlNodePtr)); if (cur->nodeTab == NULL) { xmlXPathErrMemory(NULL, "growing nodeset\n"); - return; + return(-1); } memset(cur->nodeTab, 0 , XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); @@ -3522,16 +3667,21 @@ } else if (cur->nodeNr == cur->nodeMax) { xmlNodePtr *temp; - cur->nodeMax *= 2; - temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * + if (cur->nodeMax >= XPATH_MAX_NODESET_LENGTH) { + xmlXPathErrMemory(NULL, "growing nodeset hit limit\n"); + return(-1); + } + temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * 2 * sizeof(xmlNodePtr)); if (temp == NULL) { xmlXPathErrMemory(NULL, "growing nodeset\n"); - return; + return(-1); } + cur->nodeMax *= 2; cur->nodeTab = temp; } cur->nodeTab[cur->nodeNr++] = xmlXPathNodeSetDupNs(node, ns); + return(0); } /** @@ -3540,24 +3690,21 @@ * @val: a new xmlNodePtr * * add a new xmlNodePtr to an existing NodeSet + * + * Returns 0 in case of success, and -1 in case of error */ -void +int xmlXPathNodeSetAdd(xmlNodeSetPtr cur, xmlNodePtr val) { int i; - if ((cur == NULL) || (val == NULL)) return; + if ((cur == NULL) || (val == NULL)) return(-1); -#if 0 - if ((val->type == XML_ELEMENT_NODE) && (val->name[0] == ' ')) - return; /* an XSLT fake node */ -#endif - /* @@ with_ns to check whether namespace nodes should be looked at @@ */ /* * prevent duplcates */ for (i = 0;i < cur->nodeNr;i++) - if (cur->nodeTab[i] == val) return; + if (cur->nodeTab[i] == val) return(0); /* * grow the nodeTab if needed @@ -3567,7 +3714,7 @@ sizeof(xmlNodePtr)); if (cur->nodeTab == NULL) { xmlXPathErrMemory(NULL, "growing nodeset\n"); - return; + return(-1); } memset(cur->nodeTab, 0 , XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); @@ -3575,13 +3722,17 @@ } else if (cur->nodeNr == cur->nodeMax) { xmlNodePtr *temp; - cur->nodeMax *= 2; - temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * + if (cur->nodeMax >= XPATH_MAX_NODESET_LENGTH) { + xmlXPathErrMemory(NULL, "growing nodeset hit limit\n"); + return(-1); + } + temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * 2 * sizeof(xmlNodePtr)); if (temp == NULL) { xmlXPathErrMemory(NULL, "growing nodeset\n"); - return; + return(-1); } + cur->nodeMax *= 2; cur->nodeTab = temp; } if (val->type == XML_NAMESPACE_DECL) { @@ -3591,6 +3742,7 @@ xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); } else cur->nodeTab[cur->nodeNr++] = val; + return(0); } /** @@ -3600,16 +3752,13 @@ * * add a new xmlNodePtr to an existing NodeSet, optimized version * when we are sure the node is not already in the set. + * + * Returns 0 in case of success and -1 in case of failure */ -void +int xmlXPathNodeSetAddUnique(xmlNodeSetPtr cur, xmlNodePtr val) { - if ((cur == NULL) || (val == NULL)) return; + if ((cur == NULL) || (val == NULL)) return(-1); -#if 0 - if ((val->type == XML_ELEMENT_NODE) && (val->name[0] == ' ')) - return; /* an XSLT fake node */ -#endif - /* @@ with_ns to check whether namespace nodes should be looked at @@ */ /* * grow the nodeTab if needed @@ -3619,7 +3768,7 @@ sizeof(xmlNodePtr)); if (cur->nodeTab == NULL) { xmlXPathErrMemory(NULL, "growing nodeset\n"); - return; + return(-1); } memset(cur->nodeTab, 0 , XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); @@ -3627,14 +3776,18 @@ } else if (cur->nodeNr == cur->nodeMax) { xmlNodePtr *temp; - cur->nodeMax *= 2; - temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * + if (cur->nodeMax >= XPATH_MAX_NODESET_LENGTH) { + xmlXPathErrMemory(NULL, "growing nodeset hit limit\n"); + return(-1); + } + temp = (xmlNodePtr *) xmlRealloc(cur->nodeTab, cur->nodeMax * 2 * sizeof(xmlNodePtr)); if (temp == NULL) { xmlXPathErrMemory(NULL, "growing nodeset\n"); - return; + return(-1); } cur->nodeTab = temp; + cur->nodeMax *= 2; } if (val->type == XML_NAMESPACE_DECL) { xmlNsPtr ns = (xmlNsPtr) val; @@ -3643,6 +3796,7 @@ xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); } else cur->nodeTab[cur->nodeNr++] = val; + return(0); } /** @@ -3738,14 +3892,18 @@ } else if (val1->nodeNr == val1->nodeMax) { xmlNodePtr *temp; - val1->nodeMax *= 2; - temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax * + if (val1->nodeMax >= XPATH_MAX_NODESET_LENGTH) { + xmlXPathErrMemory(NULL, "merging nodeset hit limit\n"); + return(NULL); + } + temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax * 2 * sizeof(xmlNodePtr)); if (temp == NULL) { xmlXPathErrMemory(NULL, "merging nodeset\n"); return(NULL); } val1->nodeTab = temp; + val1->nodeMax *= 2; } if (n2->type == XML_NAMESPACE_DECL) { xmlNsPtr ns = (xmlNsPtr) n2; @@ -3759,69 +3917,7 @@ return(val1); } -#if 0 /* xmlXPathNodeSetMergeUnique() is currently not used anymore */ -/** - * xmlXPathNodeSetMergeUnique: - * @val1: the first NodeSet or NULL - * @val2: the second NodeSet - * - * Merges two nodesets, all nodes from @val2 are added to @val1 - * if @val1 is NULL, a new set is created and copied from @val2 - * - * Returns @val1 once extended or NULL in case of error. - */ -static xmlNodeSetPtr -xmlXPathNodeSetMergeUnique(xmlNodeSetPtr val1, xmlNodeSetPtr val2) { - int i; - if (val2 == NULL) return(val1); - if (val1 == NULL) { - val1 = xmlXPathNodeSetCreate(NULL); - } - if (val1 == NULL) - return (NULL); - - /* @@ with_ns to check whether namespace nodes should be looked at @@ */ - - for (i = 0;i < val2->nodeNr;i++) { - /* - * grow the nodeTab if needed - */ - if (val1->nodeMax == 0) { - val1->nodeTab = (xmlNodePtr *) xmlMalloc(XML_NODESET_DEFAULT * - sizeof(xmlNodePtr)); - if (val1->nodeTab == NULL) { - xmlXPathErrMemory(NULL, "merging nodeset\n"); - return(NULL); - } - memset(val1->nodeTab, 0 , - XML_NODESET_DEFAULT * (size_t) sizeof(xmlNodePtr)); - val1->nodeMax = XML_NODESET_DEFAULT; - } else if (val1->nodeNr == val1->nodeMax) { - xmlNodePtr *temp; - - val1->nodeMax *= 2; - temp = (xmlNodePtr *) xmlRealloc(val1->nodeTab, val1->nodeMax * - sizeof(xmlNodePtr)); - if (temp == NULL) { - xmlXPathErrMemory(NULL, "merging nodeset\n"); - return(NULL); - } - val1->nodeTab = temp; - } - if (val2->nodeTab[i]->type == XML_NAMESPACE_DECL) { - xmlNsPtr ns = (xmlNsPtr) val2->nodeTab[i]; - - val1->nodeTab[val1->nodeNr++] = - xmlXPathNodeSetDupNs((xmlNodePtr) ns->next, ns); - } else - val1->nodeTab[val1->nodeNr++] = val2->nodeTab[i]; - } - - return(val1); -} -#endif /* xmlXPathNodeSetMergeUnique() is currently not used anymore */ - /** * xmlXPathNodeSetMergeAndClear: * @set1: the first NodeSet or NULL @@ -3907,14 +4003,18 @@ } else if (set1->nodeNr >= set1->nodeMax) { xmlNodePtr *temp; - set1->nodeMax *= 2; + if (set1->nodeMax >= XPATH_MAX_NODESET_LENGTH) { + xmlXPathErrMemory(NULL, "merging nodeset hit limit\n"); + return(NULL); + } temp = (xmlNodePtr *) xmlRealloc( - set1->nodeTab, set1->nodeMax * sizeof(xmlNodePtr)); + set1->nodeTab, set1->nodeMax * 2 * sizeof(xmlNodePtr)); if (temp == NULL) { xmlXPathErrMemory(NULL, "merging nodeset\n"); return(NULL); } set1->nodeTab = temp; + set1->nodeMax *= 2; } if (n2->type == XML_NAMESPACE_DECL) { xmlNsPtr ns = (xmlNsPtr) n2; @@ -3991,14 +4091,18 @@ } else if (set1->nodeNr >= set1->nodeMax) { xmlNodePtr *temp; - set1->nodeMax *= 2; + if (set1->nodeMax >= XPATH_MAX_NODESET_LENGTH) { + xmlXPathErrMemory(NULL, "merging nodeset hit limit\n"); + return(NULL); + } temp = (xmlNodePtr *) xmlRealloc( - set1->nodeTab, set1->nodeMax * sizeof(xmlNodePtr)); + set1->nodeTab, set1->nodeMax * 2 * sizeof(xmlNodePtr)); if (temp == NULL) { xmlXPathErrMemory(NULL, "merging nodeset\n"); return(NULL); } set1->nodeTab = temp; + set1->nodeMax *= 2; } set1->nodeTab[set1->nodeNr++] = n2; } @@ -4289,9 +4393,12 @@ ret = xmlXPathNewNodeSet(NULL); else { ret = xmlXPathNewNodeSet(val->nodeTab[0]); - if (ret) - for (i = 1; i < val->nodeNr; ++i) - xmlXPathNodeSetAddUnique(ret->nodesetval, val->nodeTab[i]); + if (ret) { + for (i = 1; i < val->nodeNr; ++i) { + if (xmlXPathNodeSetAddUnique(ret->nodesetval, val->nodeTab[i]) + < 0) break; + } + } } return (ret); @@ -4367,8 +4474,10 @@ for (i = 0; i < l1; i++) { cur = xmlXPathNodeSetItem(nodes1, i); - if (!xmlXPathNodeSetContains(nodes2, cur)) - xmlXPathNodeSetAddUnique(ret, cur); + if (!xmlXPathNodeSetContains(nodes2, cur)) { + if (xmlXPathNodeSetAddUnique(ret, cur) < 0) + break; + } } return(ret); } @@ -4401,8 +4510,10 @@ for (i = 0; i < l1; i++) { cur = xmlXPathNodeSetItem(nodes1, i); - if (xmlXPathNodeSetContains(nodes2, cur)) - xmlXPathNodeSetAddUnique(ret, cur); + if (xmlXPathNodeSetContains(nodes2, cur)) { + if (xmlXPathNodeSetAddUnique(ret, cur) < 0) + break; + } } return(ret); } @@ -4438,7 +4549,8 @@ strval = xmlXPathCastNodeToString(cur); if (xmlHashLookup(hash, strval) == NULL) { xmlHashAddEntry(hash, strval, strval); - xmlXPathNodeSetAddUnique(ret, cur); + if (xmlXPathNodeSetAddUnique(ret, cur) < 0) + break; } else { xmlFree(strval); } @@ -4530,7 +4642,8 @@ cur = xmlXPathNodeSetItem(nodes, i); if (cur == node) break; - xmlXPathNodeSetAddUnique(ret, cur); + if (xmlXPathNodeSetAddUnique(ret, cur) < 0) + break; } return(ret); } @@ -4634,7 +4747,8 @@ cur = xmlXPathNodeSetItem(nodes, i); if (cur == node) break; - xmlXPathNodeSetAddUnique(ret, cur); + if (xmlXPathNodeSetAddUnique(ret, cur) < 0) + break; } xmlXPathNodeSetSort(ret); /* bug 413451 */ return(ret); @@ -6154,6 +6268,7 @@ ret->valueNr = 0; ret->valueMax = 10; ret->value = NULL; + ret->valueFrame = 0; ret->context = ctxt; ret->comp = comp; @@ -7656,6 +7771,7 @@ return(NULL); } +#if 0 /** * xmlXPathNextDescendantOrSelfElemParent: * @ctxt: the XPath Parser context @@ -7731,6 +7847,7 @@ } return(NULL); } +#endif /** * xmlXPathNextDescendant: @@ -7758,6 +7875,8 @@ return(ctxt->context->node->children); } + if (cur->type == XML_NAMESPACE_DECL) + return(NULL); if (cur->children != NULL) { /* * Do not descend on entities declarations @@ -8140,6 +8259,10 @@ static int xmlXPathIsAncestor(xmlNodePtr ancestor, xmlNodePtr node) { if ((ancestor == NULL) || (node == NULL)) return(0); + if (node->type == XML_NAMESPACE_DECL) + return(0); + if (ancestor->type == XML_NAMESPACE_DECL) + return(0); /* nodes need to be in the same document */ if (ancestor->doc != node->doc) return(0); /* avoid searching if ancestor or node is the root node */ @@ -8177,7 +8300,7 @@ if (cur->type == XML_ATTRIBUTE_NODE) return(cur->parent); } - if (cur == NULL) + if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) return (NULL); if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) cur = cur->prev; @@ -8224,6 +8347,8 @@ return (NULL); ctxt->ancestor = cur->parent; } + if (cur->type == XML_NAMESPACE_DECL) + return(NULL); if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE)) cur = cur->prev; while (cur->prev == NULL) { @@ -8431,7 +8556,7 @@ int i = 0; tmp = cur->nodesetval->nodeTab[0]; - if (tmp != NULL) { + if ((tmp != NULL) && (tmp->type != XML_NAMESPACE_DECL)) { tmp = tmp->children; while (tmp != NULL) { tmp = tmp->next; @@ -9101,7 +9226,7 @@ xmlXPathSubstringBeforeFunction(xmlXPathParserContextPtr ctxt, int nargs) { xmlXPathObjectPtr str; xmlXPathObjectPtr find; - xmlBufferPtr target; + xmlBufPtr target; const xmlChar *point; int offset; @@ -9111,16 +9236,16 @@ CAST_TO_STRING; str = valuePop(ctxt); - target = xmlBufferCreate(); + target = xmlBufCreate(); if (target) { point = xmlStrstr(str->stringval, find->stringval); if (point) { offset = (int)(point - str->stringval); - xmlBufferAdd(target, str->stringval, offset); + xmlBufAdd(target, str->stringval, offset); } valuePush(ctxt, xmlXPathCacheNewString(ctxt->context, - xmlBufferContent(target))); - xmlBufferFree(target); + xmlBufContent(target))); + xmlBufFree(target); } xmlXPathReleaseObject(ctxt->context, str); xmlXPathReleaseObject(ctxt->context, find); @@ -9144,7 +9269,7 @@ xmlXPathSubstringAfterFunction(xmlXPathParserContextPtr ctxt, int nargs) { xmlXPathObjectPtr str; xmlXPathObjectPtr find; - xmlBufferPtr target; + xmlBufPtr target; const xmlChar *point; int offset; @@ -9154,17 +9279,17 @@ CAST_TO_STRING; str = valuePop(ctxt); - target = xmlBufferCreate(); + target = xmlBufCreate(); if (target) { point = xmlStrstr(str->stringval, find->stringval); if (point) { offset = (int)(point - str->stringval) + xmlStrlen(find->stringval); - xmlBufferAdd(target, &str->stringval[offset], + xmlBufAdd(target, &str->stringval[offset], xmlStrlen(str->stringval) - offset); } valuePush(ctxt, xmlXPathCacheNewString(ctxt->context, - xmlBufferContent(target))); - xmlBufferFree(target); + xmlBufContent(target))); + xmlBufFree(target); } xmlXPathReleaseObject(ctxt->context, str); xmlXPathReleaseObject(ctxt->context, find); @@ -9188,7 +9313,7 @@ xmlXPathNormalizeFunction(xmlXPathParserContextPtr ctxt, int nargs) { xmlXPathObjectPtr obj = NULL; xmlChar *source = NULL; - xmlBufferPtr target; + xmlBufPtr target; xmlChar blank; if (ctxt == NULL) return; @@ -9206,7 +9331,7 @@ obj = valuePop(ctxt); source = obj->stringval; - target = xmlBufferCreate(); + target = xmlBufCreate(); if (target && source) { /* Skip leading whitespaces */ @@ -9220,16 +9345,16 @@ blank = 0x20; } else { if (blank) { - xmlBufferAdd(target, &blank, 1); + xmlBufAdd(target, &blank, 1); blank = 0; } - xmlBufferAdd(target, source, 1); + xmlBufAdd(target, source, 1); } source++; } valuePush(ctxt, xmlXPathCacheNewString(ctxt->context, - xmlBufferContent(target))); - xmlBufferFree(target); + xmlBufContent(target))); + xmlBufFree(target); } xmlXPathReleaseObject(ctxt->context, obj); } @@ -9260,7 +9385,7 @@ xmlXPathObjectPtr str; xmlXPathObjectPtr from; xmlXPathObjectPtr to; - xmlBufferPtr target; + xmlBufPtr target; int offset, max; xmlChar ch; const xmlChar *point; @@ -9275,7 +9400,7 @@ CAST_TO_STRING; str = valuePop(ctxt); - target = xmlBufferCreate(); + target = xmlBufCreate(); if (target) { max = xmlUTF8Strlen(to->stringval); for (cptr = str->stringval; (ch=*cptr); ) { @@ -9284,10 +9409,10 @@ if (offset < max) { point = xmlUTF8Strpos(to->stringval, offset); if (point) - xmlBufferAdd(target, point, xmlUTF8Strsize(point, 1)); + xmlBufAdd(target, point, xmlUTF8Strsize(point, 1)); } } else - xmlBufferAdd(target, cptr, xmlUTF8Strsize(cptr, 1)); + xmlBufAdd(target, cptr, xmlUTF8Strsize(cptr, 1)); /* Step to next character in input */ cptr++; @@ -9296,6 +9421,7 @@ if ( (ch & 0xc0) != 0xc0 ) { xmlGenericError(xmlGenericErrorContext, "xmlXPathTranslateFunction: Invalid UTF8 string\n"); + /* not asserting an XPath error is probably better */ break; } /* then skip over remaining bytes for this char */ @@ -9303,6 +9429,7 @@ if ( (*cptr++ & 0xc0) != 0x80 ) { xmlGenericError(xmlGenericErrorContext, "xmlXPathTranslateFunction: Invalid UTF8 string\n"); + /* not asserting an XPath error is probably better */ break; } if (ch & 0x80) /* must have had error encountered */ @@ -9311,8 +9438,8 @@ } } valuePush(ctxt, xmlXPathCacheNewString(ctxt->context, - xmlBufferContent(target))); - xmlBufferFree(target); + xmlBufContent(target))); + xmlBufFree(target); xmlXPathReleaseObject(ctxt->context, str); xmlXPathReleaseObject(ctxt->context, from); xmlXPathReleaseObject(ctxt->context, to); @@ -9819,7 +9946,7 @@ xmlXPathParseName(xmlXPathParserContextPtr ctxt) { const xmlChar *in; xmlChar *ret; - int count = 0; + size_t count = 0; if ((ctxt == NULL) || (ctxt->cur == NULL)) return(NULL); /* @@ -9838,6 +9965,10 @@ in++; if ((*in > 0) && (*in < 0x80)) { count = in - ctxt->cur; + if (count > XML_MAX_NAME_LENGTH) { + ctxt->cur = in; + XP_ERRORNULL(XPATH_EXPR_ERROR); + } ret = xmlStrndup(ctxt->cur, count); ctxt->cur = in; return(ret); @@ -9881,6 +10012,9 @@ xmlChar *buffer; int max = len * 2; + if (len > XML_MAX_NAME_LENGTH) { + XP_ERRORNULL(XPATH_EXPR_ERROR); + } buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); if (buffer == NULL) { XP_ERRORNULL(XPATH_MEMORY_ERROR); @@ -9892,6 +10026,9 @@ (IS_COMBINING(c)) || (IS_EXTENDER(c))) { if (len + 10 > max) { + if (max > XML_MAX_NAME_LENGTH) { + XP_ERRORNULL(XPATH_EXPR_ERROR); + } max *= 2; buffer = (xmlChar *) xmlRealloc(buffer, max * sizeof(xmlChar)); @@ -10044,7 +10181,6 @@ xmlXPathCompNumber(xmlXPathParserContextPtr ctxt) { double ret = 0.0; - double mult = 1; int ok = 0; int exponent = 0; int is_exponent_negative = 0; @@ -10245,7 +10381,7 @@ name, prefix); SKIP_BLANKS; if ((ctxt->context != NULL) && (ctxt->context->flags & XML_XPATH_NOVAR)) { - XP_ERROR(XPATH_UNDEF_VARIABLE_ERROR); + XP_ERROR(XPATH_FORBID_VARIABLE_ERROR); } } @@ -11609,9 +11745,13 @@ */ if (contextObj == NULL) contextObj = xmlXPathCacheNewNodeSet(xpctxt, contextNode); - else - xmlXPathNodeSetAddUnique(contextObj->nodesetval, - contextNode); + else { + if (xmlXPathNodeSetAddUnique(contextObj->nodesetval, + contextNode) < 0) { + ctxt->error = XPATH_MEMORY_ERROR; + goto evaluation_exit; + } + } valuePush(ctxt, contextObj); @@ -11712,6 +11852,7 @@ xmlXPathObjectPtr contextObj = NULL, exprRes = NULL; xmlNodePtr oldContextNode, contextNode = NULL; xmlXPathContextPtr xpctxt = ctxt->context; + int frame; #ifdef LIBXML_XPTR_ENABLED /* @@ -11731,6 +11872,8 @@ */ exprOp = &ctxt->comp->steps[op->ch2]; for (i = 0; i < set->nodeNr; i++) { + xmlXPathObjectPtr tmp; + if (set->nodeTab[i] == NULL) continue; @@ -11754,22 +11897,33 @@ */ if (contextObj == NULL) contextObj = xmlXPathCacheNewNodeSet(xpctxt, contextNode); - else - xmlXPathNodeSetAddUnique(contextObj->nodesetval, - contextNode); + else { + if (xmlXPathNodeSetAddUnique(contextObj->nodesetval, + contextNode) < 0) { + ctxt->error = XPATH_MEMORY_ERROR; + goto evaluation_exit; + } + } + frame = xmlXPathSetFrame(ctxt); valuePush(ctxt, contextObj); res = xmlXPathCompOpEvalToBoolean(ctxt, exprOp, 1); + tmp = valuePop(ctxt); + xmlXPathPopFrame(ctxt, frame); if ((ctxt->error != XPATH_EXPRESSION_OK) || (res == -1)) { - xmlXPathObjectPtr tmp; - /* pop the result */ - tmp = valuePop(ctxt); - xmlXPathReleaseObject(xpctxt, tmp); - /* then pop off contextObj, which will be freed later */ - valuePop(ctxt); + while (tmp != contextObj) { + /* + * Free up the result + * then pop off contextObj, which will be freed later + */ + xmlXPathReleaseObject(xpctxt, tmp); + tmp = valuePop(ctxt); + } goto evaluation_error; } + /* push the result back onto the stack */ + valuePush(ctxt, tmp); if (res) pos++; @@ -11921,22 +12075,25 @@ #define XP_TEST_HIT \ if (hasAxisRange != 0) { \ if (++pos == maxPos) { \ - addNode(seq, cur); \ - goto axis_range_end; } \ + if (addNode(seq, cur) < 0) \ + ctxt->error = XPATH_MEMORY_ERROR; \ + goto axis_range_end; } \ } else { \ - addNode(seq, cur); \ + if (addNode(seq, cur) < 0) \ + ctxt->error = XPATH_MEMORY_ERROR; \ if (breakOnFirstHit) goto first_hit; } #define XP_TEST_HIT_NS \ if (hasAxisRange != 0) { \ if (++pos == maxPos) { \ hasNsNodes = 1; \ - xmlXPathNodeSetAddNs(seq, xpctxt->node, (xmlNsPtr) cur); \ + if (xmlXPathNodeSetAddNs(seq, xpctxt->node, (xmlNsPtr) cur) < 0) \ + ctxt->error = XPATH_MEMORY_ERROR; \ goto axis_range_end; } \ } else { \ hasNsNodes = 1; \ - xmlXPathNodeSetAddNs(seq, \ - xpctxt->node, (xmlNsPtr) cur); \ + if (xmlXPathNodeSetAddNs(seq, xpctxt->node, (xmlNsPtr) cur) < 0) \ + ctxt->error = XPATH_MEMORY_ERROR; \ if (breakOnFirstHit) goto first_hit; } xmlXPathAxisVal axis = (xmlXPathAxisVal) op->value; @@ -11956,8 +12113,6 @@ xmlNodeSetPtr contextSeq; int contextIdx; xmlNodePtr contextNode; - /* The context node for a compound traversal */ - xmlNodePtr outerContextNode; /* The final resulting node set wrt to all context nodes */ xmlNodeSetPtr outSeq; /* @@ -11973,9 +12128,7 @@ int breakOnFirstHit; xmlXPathTraversalFunction next = NULL; - /* compound axis traversal */ - xmlXPathTraversalFunctionExt outerNext = NULL; - void (*addNode) (xmlNodeSetPtr, xmlNodePtr); + int (*addNode) (xmlNodeSetPtr, xmlNodePtr); xmlXPathNodeSetMergeFunction mergeAndClear; xmlNodePtr oldContextNode; xmlXPathContextPtr xpctxt = ctxt->context; @@ -12024,13 +12177,6 @@ break; case AXIS_CHILD: last = NULL; - if (op->rewriteType == XP_REWRITE_DOS_CHILD_ELEM) { - /* - * This iterator will give us only nodes which can - * hold element nodes. - */ - outerNext = xmlXPathNextDescendantOrSelfElemParent; - } if (((test == NODE_TEST_NAME) || (test == NODE_TEST_ALL)) && (type == NODE_TYPE_NODE)) { @@ -12160,32 +12306,13 @@ addNode = xmlXPathNodeSetAddUnique; outSeq = NULL; seq = NULL; - outerContextNode = NULL; contextNode = NULL; contextIdx = 0; - while ((contextIdx < contextSeq->nodeNr) || (contextNode != NULL)) { - if (outerNext != NULL) { - /* - * This is a compound traversal. - */ - if (contextNode == NULL) { - /* - * Set the context for the outer traversal. - */ - outerContextNode = contextSeq->nodeTab[contextIdx++]; - contextNode = outerNext(NULL, outerContextNode); - } else - contextNode = outerNext(contextNode, outerContextNode); - if (contextNode == NULL) - continue; - /* - * Set the context for the main traversal. - */ - xpctxt->node = contextNode; - } else - xpctxt->node = contextSeq->nodeTab[contextIdx++]; + while (((contextIdx < contextSeq->nodeNr) || (contextNode != NULL)) && + (ctxt->error == XPATH_EXPRESSION_OK)) { + xpctxt->node = contextSeq->nodeTab[contextIdx++]; if (seq == NULL) { seq = xmlXPathNodeSetCreate(NULL); @@ -12383,7 +12510,7 @@ } break; } /* switch(test) */ - } while (cur != NULL); + } while ((cur != NULL) && (ctxt->error == XPATH_EXPRESSION_OK)); goto apply_predicates; @@ -12424,6 +12551,9 @@ #endif apply_predicates: /* --------------------------------------------------- */ + if (ctxt->error != XPATH_EXPRESSION_OK) + goto error; + /* * Apply predicates. */ @@ -12932,8 +13062,10 @@ tmp = xmlXPathCacheNewNodeSet(ctxt->context, ctxt->context->node); } else { - xmlXPathNodeSetAddUnique(tmp->nodesetval, - ctxt->context->node); + if (xmlXPathNodeSetAddUnique(tmp->nodesetval, + ctxt->context->node) < 0) { + ctxt->error = XPATH_MEMORY_ERROR; + } } valuePush(ctxt, tmp); if (op->ch2 != -1) @@ -13046,8 +13178,10 @@ tmp = xmlXPathCacheNewNodeSet(ctxt->context, ctxt->context->node); } else { - xmlXPathNodeSetAddUnique(tmp->nodesetval, - ctxt->context->node); + if (xmlXPathNodeSetAddUnique(tmp->nodesetval, + ctxt->context->node) < 0) { + ctxt->error = XPATH_MEMORY_ERROR; + } } valuePush(ctxt, tmp); ctxt->context->contextSize = oldset->nodeNr; @@ -13065,7 +13199,8 @@ */ res = valuePop(ctxt); if (xmlXPathEvaluatePredicateResult(ctxt, res)) { - xmlXPathNodeSetAdd(newset, oldset->nodeTab[i]); + if (xmlXPathNodeSetAdd(newset, oldset->nodeTab[i]) < 0) + ctxt->error = XPATH_MEMORY_ERROR; } /* * Cleanup @@ -13357,6 +13492,7 @@ xmlGenericError(xmlGenericErrorContext, "xmlXPathCompOpEval: variable %s bound to undefined prefix %s\n", (char *) op->value4, (char *)op->value5); + ctxt->error = XPATH_UNDEF_PREFIX_ERROR; return (total); } val = xmlXPathVariableLookupNS(ctxt->context, @@ -13373,7 +13509,9 @@ xmlXPathFunction func; const xmlChar *oldFunc, *oldFuncURI; int i; + int frame; + frame = xmlXPathSetFrame(ctxt); if (op->ch1 != -1) total += xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]); @@ -13381,15 +13519,18 @@ xmlGenericError(xmlGenericErrorContext, "xmlXPathCompOpEval: parameter error\n"); ctxt->error = XPATH_INVALID_OPERAND; + xmlXPathPopFrame(ctxt, frame); return (total); } - for (i = 0; i < op->value; i++) + for (i = 0; i < op->value; i++) { if (ctxt->valueTab[(ctxt->valueNr - 1) - i] == NULL) { xmlGenericError(xmlGenericErrorContext, "xmlXPathCompOpEval: parameter error\n"); ctxt->error = XPATH_INVALID_OPERAND; + xmlXPathPopFrame(ctxt, frame); return (total); } + } if (op->cache != NULL) XML_CAST_FPTR(func) = op->cache; else { @@ -13405,6 +13546,8 @@ xmlGenericError(xmlGenericErrorContext, "xmlXPathCompOpEval: function %s bound to undefined prefix %s\n", (char *)op->value4, (char *)op->value5); + xmlXPathPopFrame(ctxt, frame); + ctxt->error = XPATH_UNDEF_PREFIX_ERROR; return (total); } func = xmlXPathFunctionLookupNS(ctxt->context, @@ -13426,6 +13569,7 @@ func(ctxt, op->value); ctxt->context->function = oldFunc; ctxt->context->functionURI = oldFuncURI; + xmlXPathPopFrame(ctxt, frame); return (total); } case XPATH_OP_ARG: @@ -13735,8 +13879,10 @@ tmp = xmlXPathCacheNewNodeSet(ctxt->context, ctxt->context->node); } else { - xmlXPathNodeSetAddUnique(tmp->nodesetval, - ctxt->context->node); + if (xmlXPathNodeSetAddUnique(tmp->nodesetval, + ctxt->context->node) < 0) { + ctxt->error = XPATH_MEMORY_ERROR; + } } valuePush(ctxt, tmp); ctxt->context->contextSize = oldset->nodeNr; @@ -13766,7 +13912,9 @@ */ res = valuePop(ctxt); if (xmlXPathEvaluatePredicateResult(ctxt, res)) { - xmlXPathNodeSetAdd(newset, oldset->nodeTab[i]); + if (xmlXPathNodeSetAdd(newset, oldset->nodeTab[i]) + < 0) + ctxt->error = XPATH_MEMORY_ERROR; } /* @@ -13982,6 +14130,7 @@ } xmlGenericError(xmlGenericErrorContext, "XPath: unknown precompiled operation %d\n", op->op); + ctxt->error = XPATH_INVALID_OPERAND; return (total); } @@ -14128,7 +14277,7 @@ if (toBool) return(1); xmlXPathNodeSetAddUnique((*resultSeq)->nodesetval, - (xmlNodePtr) ctxt->doc); + (xmlNodePtr) ctxt->doc); } else { /* Select "self::node()" */ if (toBool) @@ -14221,7 +14370,11 @@ } else if (ret == 1) { if (toBool) goto return_1; - xmlXPathNodeSetAddUnique((*resultSeq)->nodesetval, cur); + if (xmlXPathNodeSetAddUnique((*resultSeq)->nodesetval, cur) + < 0) { + ctxt->lastError.domain = XML_FROM_XPATH; + ctxt->lastError.code = XML_ERR_NO_MEMORY; + } } if ((cur->children == NULL) || (depth >= max_depth)) { ret = xmlStreamPop(patstream); @@ -14237,6 +14390,7 @@ } scan_children: + if (cur->type == XML_NAMESPACE_DECL) break; if ((cur->children != NULL) && (depth < max_depth)) { /* * Do not descend on entities declarations @@ -14329,6 +14483,7 @@ ctxt->valueNr = 0; ctxt->valueMax = 10; ctxt->value = NULL; + ctxt->valueFrame = 0; } #ifdef XPATH_STREAMING if (ctxt->comp->stream) { @@ -14556,57 +14711,63 @@ } #endif /* XPATH_STREAMING */ -static int -xmlXPathCanRewriteDosExpression(xmlChar *expr) -{ - if (expr == NULL) - return(0); - do { - if ((*expr == '/') && (*(++expr) == '/')) - return(1); - } while (*expr++); - return(0); -} static void -xmlXPathRewriteDOSExpression(xmlXPathCompExprPtr comp, xmlXPathStepOpPtr op) +xmlXPathOptimizeExpression(xmlXPathCompExprPtr comp, xmlXPathStepOpPtr op) { /* * Try to rewrite "descendant-or-self::node()/foo" to an optimized * internal representation. */ - if (op->ch1 != -1) { - if ((op->op == XPATH_OP_COLLECT /* 11 */) && - ((xmlXPathAxisVal) op->value == AXIS_CHILD /* 4 */) && - ((xmlXPathTestVal) op->value2 == NODE_TEST_NAME /* 5 */) && - ((xmlXPathTypeVal) op->value3 == NODE_TYPE_NODE /* 0 */)) - { - /* - * This is a "child::foo" - */ - xmlXPathStepOpPtr prevop = &comp->steps[op->ch1]; - if ((prevop->op == XPATH_OP_COLLECT /* 11 */) && - (prevop->ch1 != -1) && - ((xmlXPathAxisVal) prevop->value == - AXIS_DESCENDANT_OR_SELF) && - (prevop->ch2 == -1) && - ((xmlXPathTestVal) prevop->value2 == NODE_TEST_TYPE) && - ((xmlXPathTypeVal) prevop->value3 == NODE_TYPE_NODE) && - (comp->steps[prevop->ch1].op == XPATH_OP_ROOT)) - { - /* - * This is a "/descendant-or-self::node()" without predicates. - * Eliminate it. - */ - op->ch1 = prevop->ch1; - op->rewriteType = XP_REWRITE_DOS_CHILD_ELEM; - } + if ((op->ch1 != -1) && + (op->op == XPATH_OP_COLLECT /* 11 */)) + { + xmlXPathStepOpPtr prevop = &comp->steps[op->ch1]; + + if ((prevop->op == XPATH_OP_COLLECT /* 11 */) && + ((xmlXPathAxisVal) prevop->value == + AXIS_DESCENDANT_OR_SELF) && + (prevop->ch2 == -1) && + ((xmlXPathTestVal) prevop->value2 == NODE_TEST_TYPE) && + ((xmlXPathTypeVal) prevop->value3 == NODE_TYPE_NODE)) + { + /* + * This is a "descendant-or-self::node()" without predicates. + * Try to eliminate it. + */ + + switch ((xmlXPathAxisVal) op->value) { + case AXIS_CHILD: + case AXIS_DESCENDANT: + /* + * Convert "descendant-or-self::node()/child::" or + * "descendant-or-self::node()/descendant::" to + * "descendant::" + */ + op->ch1 = prevop->ch1; + op->value = AXIS_DESCENDANT; + break; + case AXIS_SELF: + case AXIS_DESCENDANT_OR_SELF: + /* + * Convert "descendant-or-self::node()/self::" or + * "descendant-or-self::node()/descendant-or-self::" to + * to "descendant-or-self::" + */ + op->ch1 = prevop->ch1; + op->value = AXIS_DESCENDANT_OR_SELF; + break; + default: + break; + } } - if (op->ch1 != -1) - xmlXPathRewriteDOSExpression(comp, &comp->steps[op->ch1]); } + + /* Recurse */ + if (op->ch1 != -1) + xmlXPathOptimizeExpression(comp, &comp->steps[op->ch1]); if (op->ch2 != -1) - xmlXPathRewriteDOSExpression(comp, &comp->steps[op->ch2]); + xmlXPathOptimizeExpression(comp, &comp->steps[op->ch2]); } /** @@ -14664,12 +14825,8 @@ comp->string = xmlStrdup(str); comp->nb = 0; #endif - if ((comp->expr != NULL) && - (comp->nbStep > 2) && - (comp->last >= 0) && - (xmlXPathCanRewriteDosExpression(comp->expr) == 1)) - { - xmlXPathRewriteDOSExpression(comp, &comp->steps[comp->last]); + if ((comp->nbStep > 1) && (comp->last >= 0)) { + xmlXPathOptimizeExpression(comp, &comp->steps[comp->last]); } } return(comp); @@ -14846,17 +15003,12 @@ #endif { xmlXPathCompileExpr(ctxt, 1); - /* - * In this scenario the expression string will sit in ctxt->base. - */ if ((ctxt->error == XPATH_EXPRESSION_OK) && (ctxt->comp != NULL) && - (ctxt->base != NULL) && - (ctxt->comp->nbStep > 2) && - (ctxt->comp->last >= 0) && - (xmlXPathCanRewriteDosExpression((xmlChar *) ctxt->base) == 1)) + (ctxt->comp->nbStep > 1) && + (ctxt->comp->last >= 0)) { - xmlXPathRewriteDOSExpression(ctxt->comp, + xmlXPathOptimizeExpression(ctxt->comp, &ctxt->comp->steps[ctxt->comp->last]); } } @@ -15030,7 +15182,7 @@ xmlXPathEscapeUriFunction(xmlXPathParserContextPtr ctxt, int nargs) { xmlXPathObjectPtr str; int escape_reserved; - xmlBufferPtr target; + xmlBufPtr target; xmlChar *cptr; xmlChar escape[4]; @@ -15041,7 +15193,7 @@ CAST_TO_STRING; str = valuePop(ctxt); - target = xmlBufferCreate(); + target = xmlBufCreate(); escape[0] = '%'; escape[3] = 0; @@ -15066,7 +15218,7 @@ *cptr == ':' || *cptr == '@' || *cptr == '&' || *cptr == '=' || *cptr == '+' || *cptr == '$' || *cptr == ','))) { - xmlBufferAdd(target, cptr, 1); + xmlBufAdd(target, cptr, 1); } else { if ((*cptr >> 4) < 10) escape[1] = '0' + (*cptr >> 4); @@ -15077,13 +15229,13 @@ else escape[2] = 'A' - 10 + (*cptr & 0xF); - xmlBufferAdd(target, &escape[0], 3); + xmlBufAdd(target, &escape[0], 3); } } } valuePush(ctxt, xmlXPathCacheNewString(ctxt->context, - xmlBufferContent(target))); - xmlBufferFree(target); + xmlBufContent(target))); + xmlBufFree(target); xmlXPathReleaseObject(ctxt->context, str); } Index: lib/3rdparty/libxml2/xpointer.c =================================================================== --- lib/3rdparty/libxml2/xpointer.c (revision 58294) +++ lib/3rdparty/libxml2/xpointer.c (working copy) @@ -134,7 +134,7 @@ static int xmlXPtrGetArity(xmlNodePtr cur) { int i; - if (cur == NULL) + if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) return(-1); cur = cur->children; for (i = 0;cur != NULL;cur = cur->next) { @@ -157,7 +157,7 @@ static int xmlXPtrGetIndex(xmlNodePtr cur) { int i; - if (cur == NULL) + if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) return(-1); for (i = 1;cur != NULL;cur = cur->prev) { if ((cur->type == XML_ELEMENT_NODE) || @@ -179,7 +179,7 @@ static xmlNodePtr xmlXPtrGetNthChild(xmlNodePtr cur, int no) { int i; - if (cur == NULL) + if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) return(cur); cur = cur->children; for (i = 0;i <= no;cur = cur->next) { @@ -1007,21 +1007,14 @@ NEXT; break; } - *cur++ = CUR; } else if (CUR == '(') { level++; - *cur++ = CUR; } else if (CUR == '^') { - NEXT; - if ((CUR == ')') || (CUR == '(') || (CUR == '^')) { - *cur++ = CUR; - } else { - *cur++ = '^'; - *cur++ = CUR; - } - } else { - *cur++ = CUR; + if ((NXT(1) == ')') || (NXT(1) == '(') || (NXT(1) == '^')) { + NEXT; + } } + *cur++ = CUR; NEXT; } *cur = 0; @@ -1269,6 +1262,7 @@ ctxt->valueNr = 0; ctxt->valueMax = 10; ctxt->value = NULL; + ctxt->valueFrame = 0; } SKIP_BLANKS; if (CUR == '/') { @@ -1450,11 +1444,13 @@ return(NULL); start = (xmlNodePtr) range->user; - if (start == NULL) + if ((start == NULL) || (start->type == XML_NAMESPACE_DECL)) return(NULL); end = range->user2; if (end == NULL) return(xmlCopyNode(start, 1)); + if (end->type == XML_NAMESPACE_DECL) + return(NULL); cur = start; index1 = range->index; @@ -2310,7 +2306,7 @@ xmlNodePtr xmlXPtrAdvanceNode(xmlNodePtr cur, int *level) { next: - if (cur == NULL) + if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) return(NULL); if (cur->children != NULL) { cur = cur->children ; @@ -2368,7 +2364,7 @@ if ((node == NULL) || (indx == NULL)) return(-1); cur = *node; - if (cur == NULL) + if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) return(-1); pos = *indx; @@ -2459,9 +2455,10 @@ if (string == NULL) return(-1); - if (start == NULL) + if ((start == NULL) || (start->type == XML_NAMESPACE_DECL)) return(-1); - if ((end == NULL) || (endindex == NULL)) + if ((end == NULL) || (*end == NULL) || + ((*end)->type == XML_NAMESPACE_DECL) || (endindex == NULL)) return(-1); cur = start; if (cur == NULL) @@ -2544,13 +2541,12 @@ if (string == NULL) return(-1); - if ((start == NULL) || (startindex == NULL)) + if ((start == NULL) || (*start == NULL) || + ((*start)->type == XML_NAMESPACE_DECL) || (startindex == NULL)) return(-1); if ((end == NULL) || (endindex == NULL)) return(-1); cur = *start; - if (cur == NULL) - return(-1); pos = *startindex - 1; first = string[0]; @@ -2624,14 +2620,12 @@ xmlNodePtr cur; int pos, len = 0; - if ((node == NULL) || (indx == NULL)) + if ((node == NULL) || (*node == NULL) || + ((*node)->type == XML_NAMESPACE_DECL) || (indx == NULL)) return(-1); cur = *node; pos = *indx; - if (cur == NULL) - return(-1); - if ((cur->type == XML_ELEMENT_NODE) || (cur->type == XML_DOCUMENT_NODE) || (cur->type == XML_HTML_DOCUMENT_NODE)) { Index: lib/3rdparty/libxml2/xzlib.c =================================================================== --- lib/3rdparty/libxml2/xzlib.c (revision 0) +++ lib/3rdparty/libxml2/xzlib.c (working copy) @@ -0,0 +1,755 @@ +/** + * xzlib.c: front end for the transparent suport of lzma compression + * at the I/O layer, based on an example file from lzma project + * + * See Copyright for the status of this software. + * + * Anders F Bjorklund + */ +#define IN_LIBXML +#include "libxml.h" +#ifdef HAVE_LZMA_H + +#include +#ifdef HAVE_ERRNO_H +#include +#endif + + +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef HAVE_SYS_STAT_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_ZLIB_H +#include +#endif +#include + +#include "xzlib.h" +#include + +/* values for xz_state how */ +#define LOOK 0 /* look for a gzip/lzma header */ +#define COPY 1 /* copy input directly */ +#define GZIP 2 /* decompress a gzip stream */ +#define LZMA 3 /* decompress a lzma stream */ + +/* internal lzma file state data structure */ +typedef struct { + int mode; /* see lzma modes above */ + int fd; /* file descriptor */ + char *path; /* path or fd for error messages */ + uint64_t pos; /* current position in uncompressed data */ + unsigned int size; /* buffer size, zero if not allocated yet */ + unsigned int want; /* requested buffer size, default is BUFSIZ */ + unsigned char *in; /* input buffer */ + unsigned char *out; /* output buffer (double-sized when reading) */ + unsigned char *next; /* next output data to deliver or write */ + unsigned int have; /* amount of output data unused at next */ + int eof; /* true if end of input file reached */ + uint64_t start; /* where the lzma data started, for rewinding */ + uint64_t raw; /* where the raw data started, for seeking */ + int how; /* 0: get header, 1: copy, 2: decompress */ + int direct; /* true if last read direct, false if lzma */ + /* seek request */ + uint64_t skip; /* amount to skip (already rewound if backwards) */ + int seek; /* true if seek request pending */ + /* error information */ + int err; /* error code */ + char *msg; /* error message */ + /* lzma stream */ + int init; /* is the iniflate stream initialized */ + lzma_stream strm; /* stream structure in-place (not a pointer) */ + char padding1[32]; /* padding allowing to cope with possible + extensions of above structure without + too much side effect */ +#ifdef HAVE_ZLIB_H + /* zlib inflate or deflate stream */ + z_stream zstrm; /* stream structure in-place (not a pointer) */ +#endif + char padding2[32]; /* padding allowing to cope with possible + extensions of above structure without + too much side effect */ +} xz_state, *xz_statep; + +static void +xz_error(xz_statep state, int err, const char *msg) +{ + /* free previously allocated message and clear */ + if (state->msg != NULL) { + if (state->err != LZMA_MEM_ERROR) + xmlFree(state->msg); + state->msg = NULL; + } + + /* set error code, and if no message, then done */ + state->err = err; + if (msg == NULL) + return; + + /* for an out of memory error, save as static string */ + if (err == LZMA_MEM_ERROR) { + state->msg = (char *) msg; + return; + } + + /* construct error message with path */ + if ((state->msg = + xmlMalloc(strlen(state->path) + strlen(msg) + 3)) == NULL) { + state->err = LZMA_MEM_ERROR; + state->msg = (char *) "out of memory"; + return; + } + strcpy(state->msg, state->path); + strcat(state->msg, ": "); + strcat(state->msg, msg); + return; +} + +static void +xz_reset(xz_statep state) +{ + state->have = 0; /* no output data available */ + state->eof = 0; /* not at end of file */ + state->how = LOOK; /* look for gzip header */ + state->direct = 1; /* default for empty file */ + state->seek = 0; /* no seek request pending */ + xz_error(state, LZMA_OK, NULL); /* clear error */ + state->pos = 0; /* no uncompressed data yet */ + state->strm.avail_in = 0; /* no input data yet */ +#ifdef HAVE_ZLIB_H + state->zstrm.avail_in = 0; /* no input data yet */ +#endif +} + +static xzFile +xz_open(const char *path, int fd, const char *mode ATTRIBUTE_UNUSED) +{ + xz_statep state; + + /* allocate xzFile structure to return */ + state = xmlMalloc(sizeof(xz_state)); + if (state == NULL) + return NULL; + state->size = 0; /* no buffers allocated yet */ + state->want = BUFSIZ; /* requested buffer size */ + state->msg = NULL; /* no error message yet */ + state->init = 0; /* initialization of zlib data */ + + /* save the path name for error messages */ + state->path = xmlMalloc(strlen(path) + 1); + if (state->path == NULL) { + xmlFree(state); + return NULL; + } + strcpy(state->path, path); + + /* open the file with the appropriate mode (or just use fd) */ + state->fd = fd != -1 ? fd : open(path, +#ifdef O_LARGEFILE + O_LARGEFILE | +#endif +#ifdef O_BINARY + O_BINARY | +#endif + O_RDONLY, 0666); + if (state->fd == -1) { + xmlFree(state->path); + xmlFree(state); + return NULL; + } + + /* save the current position for rewinding (only if reading) */ + state->start = lseek(state->fd, 0, SEEK_CUR); + if (state->start == (uint64_t) - 1) + state->start = 0; + + /* initialize stream */ + xz_reset(state); + + /* return stream */ + return (xzFile) state; +} + +xzFile +__libxml2_xzopen(const char *path, const char *mode) +{ + return xz_open(path, -1, mode); +} + +xzFile +__libxml2_xzdopen(int fd, const char *mode) +{ + char *path; /* identifier for error messages */ + xzFile xz; + + if (fd == -1 || (path = xmlMalloc(7 + 3 * sizeof(int))) == NULL) + return NULL; + sprintf(path, "", fd); /* for debugging */ + xz = xz_open(path, fd, mode); + xmlFree(path); + return xz; +} + +static int +xz_load(xz_statep state, unsigned char *buf, unsigned int len, + unsigned int *have) +{ + int ret; + + *have = 0; + do { + ret = read(state->fd, buf + *have, len - *have); + if (ret <= 0) + break; + *have += ret; + } while (*have < len); + if (ret < 0) { + xz_error(state, -1, strerror(errno)); + return -1; + } + if (ret == 0) + state->eof = 1; + return 0; +} + +static int +xz_avail(xz_statep state) +{ + lzma_stream *strm = &(state->strm); + + if (state->err != LZMA_OK) + return -1; + if (state->eof == 0) { + /* avail_in is size_t, which is not necessary sizeof(unsigned) */ + unsigned tmp = strm->avail_in; + + if (xz_load(state, state->in, state->size, &tmp) == -1) { + strm->avail_in = tmp; + return -1; + } + strm->avail_in = tmp; + strm->next_in = state->in; + } + return 0; +} + +static int +is_format_xz(xz_statep state) +{ + lzma_stream *strm = &(state->strm); + + return strm->avail_in >= 6 && memcmp(state->in, "\3757zXZ", 6) == 0; +} + +static int +is_format_lzma(xz_statep state) +{ + lzma_stream *strm = &(state->strm); + + lzma_filter filter; + lzma_options_lzma *opt; + uint32_t dict_size; + uint64_t uncompressed_size; + size_t i; + + if (strm->avail_in < 13) + return 0; + + filter.id = LZMA_FILTER_LZMA1; + if (lzma_properties_decode(&filter, NULL, state->in, 5) != LZMA_OK) + return 0; + + opt = filter.options; + dict_size = opt->dict_size; + free(opt); /* we can't use xmlFree on a string returned by zlib */ + + /* A hack to ditch tons of false positives: We allow only dictionary + * sizes that are 2^n or 2^n + 2^(n-1) or UINT32_MAX. LZMA_Alone + * created only files with 2^n, but accepts any dictionary size. + * If someone complains, this will be reconsidered. + */ + if (dict_size != UINT32_MAX) { + uint32_t d = dict_size - 1; + + d |= d >> 2; + d |= d >> 3; + d |= d >> 4; + d |= d >> 8; + d |= d >> 16; + ++d; + if (d != dict_size || dict_size == 0) + return 0; + } + + /* Another hack to ditch false positives: Assume that if the + * uncompressed size is known, it must be less than 256 GiB. + * Again, if someone complains, this will be reconsidered. + */ + uncompressed_size = 0; + for (i = 0; i < 8; ++i) + uncompressed_size |= (uint64_t) (state->in[5 + i]) << (i * 8); + + if (uncompressed_size != UINT64_MAX + && uncompressed_size > (UINT64_C(1) << 38)) + return 0; + + return 1; +} + +#ifdef HAVE_ZLIB_H + +/* Get next byte from input, or -1 if end or error. */ +#define NEXT() ((strm->avail_in == 0 && xz_avail(state) == -1) ? -1 : \ + (strm->avail_in == 0 ? -1 : \ + (strm->avail_in--, *(strm->next_in)++))) + +/* Get a four-byte little-endian integer and return 0 on success and the value + in *ret. Otherwise -1 is returned and *ret is not modified. */ +static int +gz_next4(xz_statep state, unsigned long *ret) +{ + int ch; + unsigned long val; + z_streamp strm = &(state->zstrm); + + val = NEXT(); + val += (unsigned) NEXT() << 8; + val += (unsigned long) NEXT() << 16; + ch = NEXT(); + if (ch == -1) + return -1; + val += (unsigned long) ch << 24; + *ret = val; + return 0; +} +#endif + +static int +xz_head(xz_statep state) +{ + lzma_stream *strm = &(state->strm); + lzma_stream init = LZMA_STREAM_INIT; + int flags; + unsigned len; + + /* allocate read buffers and inflate memory */ + if (state->size == 0) { + /* allocate buffers */ + state->in = xmlMalloc(state->want); + state->out = xmlMalloc(state->want << 1); + if (state->in == NULL || state->out == NULL) { + if (state->out != NULL) + xmlFree(state->out); + if (state->in != NULL) + xmlFree(state->in); + xz_error(state, LZMA_MEM_ERROR, "out of memory"); + return -1; + } + state->size = state->want; + + /* allocate decoder memory */ + state->strm = init; + state->strm.avail_in = 0; + state->strm.next_in = NULL; + if (lzma_auto_decoder(&state->strm, UINT64_MAX, 0) != LZMA_OK) { + xmlFree(state->out); + xmlFree(state->in); + state->size = 0; + xz_error(state, LZMA_MEM_ERROR, "out of memory"); + return -1; + } +#ifdef HAVE_ZLIB_H + /* allocate inflate memory */ + state->zstrm.zalloc = Z_NULL; + state->zstrm.zfree = Z_NULL; + state->zstrm.opaque = Z_NULL; + state->zstrm.avail_in = 0; + state->zstrm.next_in = Z_NULL; + if (state->init == 0) { + if (inflateInit2(&(state->zstrm), -15) != Z_OK) {/* raw inflate */ + xmlFree(state->out); + xmlFree(state->in); + state->size = 0; + xz_error(state, LZMA_MEM_ERROR, "out of memory"); + return -1; + } + state->init = 1; + } +#endif + } + + /* get some data in the input buffer */ + if (strm->avail_in == 0) { + if (xz_avail(state) == -1) + return -1; + if (strm->avail_in == 0) + return 0; + } + + /* look for the xz magic header bytes */ + if (is_format_xz(state) || is_format_lzma(state)) { + state->how = LZMA; + state->direct = 0; + return 0; + } +#ifdef HAVE_ZLIB_H + /* look for the gzip magic header bytes 31 and 139 */ + if (strm->next_in[0] == 31) { + strm->avail_in--; + strm->next_in++; + if (strm->avail_in == 0 && xz_avail(state) == -1) + return -1; + if (strm->avail_in && strm->next_in[0] == 139) { + /* we have a gzip header, woo hoo! */ + strm->avail_in--; + strm->next_in++; + + /* skip rest of header */ + if (NEXT() != 8) { /* compression method */ + xz_error(state, LZMA_DATA_ERROR, + "unknown compression method"); + return -1; + } + flags = NEXT(); + if (flags & 0xe0) { /* reserved flag bits */ + xz_error(state, LZMA_DATA_ERROR, + "unknown header flags set"); + return -1; + } + NEXT(); /* modification time */ + NEXT(); + NEXT(); + NEXT(); + NEXT(); /* extra flags */ + NEXT(); /* operating system */ + if (flags & 4) { /* extra field */ + len = (unsigned) NEXT(); + len += (unsigned) NEXT() << 8; + while (len--) + if (NEXT() < 0) + break; + } + if (flags & 8) /* file name */ + while (NEXT() > 0) ; + if (flags & 16) /* comment */ + while (NEXT() > 0) ; + if (flags & 2) { /* header crc */ + NEXT(); + NEXT(); + } + /* an unexpected end of file is not checked for here -- it will be + * noticed on the first request for uncompressed data */ + + /* set up for decompression */ + inflateReset(&state->zstrm); + state->zstrm.adler = crc32(0L, Z_NULL, 0); + state->how = GZIP; + state->direct = 0; + return 0; + } else { + /* not a gzip file -- save first byte (31) and fall to raw i/o */ + state->out[0] = 31; + state->have = 1; + } + } +#endif + + /* doing raw i/o, save start of raw data for seeking, copy any leftover + * input to output -- this assumes that the output buffer is larger than + * the input buffer, which also assures space for gzungetc() */ + state->raw = state->pos; + state->next = state->out; + if (strm->avail_in) { + memcpy(state->next + state->have, strm->next_in, strm->avail_in); + state->have += strm->avail_in; + strm->avail_in = 0; + } + state->how = COPY; + state->direct = 1; + return 0; +} + +static int +xz_decomp(xz_statep state) +{ + int ret; + unsigned had; + unsigned long crc, len; + lzma_stream *strm = &(state->strm); + + lzma_action action = LZMA_RUN; + + /* fill output buffer up to end of deflate stream */ + had = strm->avail_out; + do { + /* get more input for inflate() */ + if (strm->avail_in == 0 && xz_avail(state) == -1) + return -1; + if (strm->avail_in == 0) { + xz_error(state, LZMA_DATA_ERROR, "unexpected end of file"); + return -1; + } + if (state->eof) + action = LZMA_FINISH; + + /* decompress and handle errors */ +#ifdef HAVE_ZLIB_H + if (state->how == GZIP) { + state->zstrm.avail_in = (uInt) state->strm.avail_in; + state->zstrm.next_in = (Bytef *) state->strm.next_in; + state->zstrm.avail_out = (uInt) state->strm.avail_out; + state->zstrm.next_out = (Bytef *) state->strm.next_out; + ret = inflate(&state->zstrm, Z_NO_FLUSH); + if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) { + xz_error(state, Z_STREAM_ERROR, + "internal error: inflate stream corrupt"); + return -1; + } + if (ret == Z_MEM_ERROR) + ret = LZMA_MEM_ERROR; + if (ret == Z_DATA_ERROR) + ret = LZMA_DATA_ERROR; + if (ret == Z_STREAM_END) + ret = LZMA_STREAM_END; + state->strm.avail_in = state->zstrm.avail_in; + state->strm.next_in = state->zstrm.next_in; + state->strm.avail_out = state->zstrm.avail_out; + state->strm.next_out = state->zstrm.next_out; + } else /* state->how == LZMA */ +#endif + ret = lzma_code(strm, action); + if (ret == LZMA_MEM_ERROR) { + xz_error(state, LZMA_MEM_ERROR, "out of memory"); + return -1; + } + if (ret == LZMA_DATA_ERROR) { + xz_error(state, LZMA_DATA_ERROR, "compressed data error"); + return -1; + } + } while (strm->avail_out && ret != LZMA_STREAM_END); + + /* update available output and crc check value */ + state->have = had - strm->avail_out; + state->next = strm->next_out - state->have; +#ifdef HAVE_ZLIB_H + state->zstrm.adler = + crc32(state->zstrm.adler, state->next, state->have); +#endif + + if (ret == LZMA_STREAM_END) { +#ifdef HAVE_ZLIB_H + if (state->how == GZIP) { + if (gz_next4(state, &crc) == -1 || gz_next4(state, &len) == -1) { + xz_error(state, LZMA_DATA_ERROR, "unexpected end of file"); + return -1; + } + if (crc != state->zstrm.adler) { + xz_error(state, LZMA_DATA_ERROR, "incorrect data check"); + return -1; + } + if (len != (state->zstrm.total_out & 0xffffffffL)) { + xz_error(state, LZMA_DATA_ERROR, "incorrect length check"); + return -1; + } + state->strm.avail_in = 0; + state->strm.next_in = NULL; + state->strm.avail_out = 0; + state->strm.next_out = NULL; + } else +#endif + if (strm->avail_in != 0 || !state->eof) { + xz_error(state, LZMA_DATA_ERROR, "trailing garbage"); + return -1; + } + state->how = LOOK; /* ready for next stream, once have is 0 (leave + * state->direct unchanged to remember how) */ + } + + /* good decompression */ + return 0; +} + +static int +xz_make(xz_statep state) +{ + lzma_stream *strm = &(state->strm); + + if (state->how == LOOK) { /* look for lzma / gzip header */ + if (xz_head(state) == -1) + return -1; + if (state->have) /* got some data from xz_head() */ + return 0; + } + if (state->how == COPY) { /* straight copy */ + if (xz_load(state, state->out, state->size << 1, &(state->have)) == + -1) + return -1; + state->next = state->out; + } else if (state->how == LZMA || state->how == GZIP) { /* decompress */ + strm->avail_out = state->size << 1; + strm->next_out = state->out; + if (xz_decomp(state) == -1) + return -1; + } + return 0; +} + +static int +xz_skip(xz_statep state, uint64_t len) +{ + unsigned n; + + /* skip over len bytes or reach end-of-file, whichever comes first */ + while (len) + /* skip over whatever is in output buffer */ + if (state->have) { + n = (uint64_t) state->have > len ? + (unsigned) len : state->have; + state->have -= n; + state->next += n; + state->pos += n; + len -= n; + } + + /* output buffer empty -- return if we're at the end of the input */ + else if (state->eof && state->strm.avail_in == 0) + break; + + /* need more data to skip -- load up output buffer */ + else { + /* get more output, looking for header if required */ + if (xz_make(state) == -1) + return -1; + } + return 0; +} + +int +__libxml2_xzread(xzFile file, void *buf, unsigned len) +{ + unsigned got, n; + xz_statep state; + lzma_stream *strm; + + /* get internal structure */ + if (file == NULL) + return -1; + state = (xz_statep) file; + strm = &(state->strm); + + /* check that we're reading and that there's no error */ + if (state->err != LZMA_OK) + return -1; + + /* since an int is returned, make sure len fits in one, otherwise return + * with an error (this avoids the flaw in the interface) */ + if ((int) len < 0) { + xz_error(state, LZMA_BUF_ERROR, + "requested length does not fit in int"); + return -1; + } + + /* if len is zero, avoid unnecessary operations */ + if (len == 0) + return 0; + + /* process a skip request */ + if (state->seek) { + state->seek = 0; + if (xz_skip(state, state->skip) == -1) + return -1; + } + + /* get len bytes to buf, or less than len if at the end */ + got = 0; + do { + /* first just try copying data from the output buffer */ + if (state->have) { + n = state->have > len ? len : state->have; + memcpy(buf, state->next, n); + state->next += n; + state->have -= n; + } + + /* output buffer empty -- return if we're at the end of the input */ + else if (state->eof && strm->avail_in == 0) + break; + + /* need output data -- for small len or new stream load up our output + * buffer */ + else if (state->how == LOOK || len < (state->size << 1)) { + /* get more output, looking for header if required */ + if (xz_make(state) == -1) + return -1; + continue; /* no progress yet -- go back to memcpy() above */ + /* the copy above assures that we will leave with space in the + * output buffer, allowing at least one gzungetc() to succeed */ + } + + /* large len -- read directly into user buffer */ + else if (state->how == COPY) { /* read directly */ + if (xz_load(state, buf, len, &n) == -1) + return -1; + } + + /* large len -- decompress directly into user buffer */ + else { /* state->how == LZMA */ + strm->avail_out = len; + strm->next_out = buf; + if (xz_decomp(state) == -1) + return -1; + n = state->have; + state->have = 0; + } + + /* update progress */ + len -= n; + buf = (char *) buf + n; + got += n; + state->pos += n; + } while (len); + + /* return number of bytes read into user buffer (will fit in int) */ + return (int) got; +} + +int +__libxml2_xzclose(xzFile file) +{ + int ret; + xz_statep state; + + /* get internal structure */ + if (file == NULL) + return LZMA_DATA_ERROR; + state = (xz_statep) file; + + /* free memory and close file */ + if (state->size) { + lzma_end(&(state->strm)); +#ifdef HAVE_ZLIB_H + if (state->init == 1) + inflateEnd(&(state->zstrm)); + state->init = 0; +#endif + xmlFree(state->out); + xmlFree(state->in); + } + xmlFree(state->path); + ret = close(state->fd); + xmlFree(state); + return ret ? ret : LZMA_OK; +} +#endif /* HAVE_LZMA_H */ Index: lib/3rdparty/libxml2/xzlib.h =================================================================== --- lib/3rdparty/libxml2/xzlib.h (revision 0) +++ lib/3rdparty/libxml2/xzlib.h (working copy) @@ -0,0 +1,18 @@ +/** + * xzlib.h: header for the front end for the transparent suport of lzma + * compression at the I/O layer + * + * See Copyright for the status of this software. + * + * Anders F Bjorklund + */ + +#ifndef LIBXML2_XZLIB_H +#define LIBXML2_XZLIB_H +typedef void *xzFile; /* opaque lzma file descriptor */ + +xzFile __libxml2_xzopen(const char *path, const char *mode); +xzFile __libxml2_xzdopen(int fd, const char *mode); +int __libxml2_xzread(xzFile file, void *buf, unsigned len); +int __libxml2_xzclose(xzFile file); +#endif /* LIBXML2_XZLIB_H */