⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlio.c

📁 libxml,在UNIX/LINUX下非常重要的一个库,为XML相关应用提供方便.目前上载的是最新版本,若要取得最新版本,请参考里面的readme.
💻 C
📖 第 1 页 / 共 5 页
字号:
    if (ret < 0) xmlIOErr(0, "gzclose()");    return(ret);}#endif /* HAVE_ZLIB_H */#ifdef LIBXML_HTTP_ENABLED/************************************************************************ *									* *			I/O for HTTP file accesses			* *									* ************************************************************************/#ifdef LIBXML_OUTPUT_ENABLEDtypedef struct xmlIOHTTPWriteCtxt_{    int			compression;    char *		uri;    void *		doc_buff;} xmlIOHTTPWriteCtxt, *xmlIOHTTPWriteCtxtPtr;#ifdef HAVE_ZLIB_H#define DFLT_WBITS		( -15 )#define DFLT_MEM_LVL		( 8 )#define GZ_MAGIC1		( 0x1f )#define GZ_MAGIC2		( 0x8b )#define LXML_ZLIB_OS_CODE	( 0x03 )#define INIT_HTTP_BUFF_SIZE	( 32768 )#define DFLT_ZLIB_RATIO		( 5 )/***  Data structure and functions to work with sending compressed data**  via HTTP.*/typedef struct xmlZMemBuff_{   unsigned long	size;   unsigned long	crc;   unsigned char *	zbuff;   z_stream		zctrl;} xmlZMemBuff, *xmlZMemBuffPtr;/** * append_reverse_ulong * @buff:  Compressed memory buffer * @data:  Unsigned long to append * * Append a unsigned long in reverse byte order to the end of the * memory buffer. */static voidappend_reverse_ulong( xmlZMemBuff * buff, unsigned long data ) {    int		idx;    if ( buff == NULL )	return;    /*    **  This is plagiarized from putLong in gzio.c (zlib source) where    **  the number "4" is hardcoded.  If zlib is ever patched to     **  support 64 bit file sizes, this code would need to be patched    **  as well.    */    for ( idx = 0; idx < 4; idx++ ) {	*buff->zctrl.next_out = ( data & 0xff );	data >>= 8;	buff->zctrl.next_out++;    }    return;}/** * * xmlFreeZMemBuff * @buff:  The memory buffer context to clear * * Release all the resources associated with the compressed memory buffer. */static voidxmlFreeZMemBuff( xmlZMemBuffPtr buff ) {#ifdef DEBUG_HTTP    int z_err;#endif    if ( buff == NULL )	return;    xmlFree( buff->zbuff );#ifdef DEBUG_HTTP    z_err = deflateEnd( &buff->zctrl );    if ( z_err != Z_OK )	xmlGenericError( xmlGenericErrorContext,			"xmlFreeZMemBuff:  Error releasing zlib context:  %d\n",			z_err );#else    deflateEnd( &buff->zctrl );#endif    xmlFree( buff );    return;}/** * xmlCreateZMemBuff *@compression:	Compression value to use * * Create a memory buffer to hold the compressed XML document.  The * compressed document in memory will end up being identical to what * would be created if gzopen/gzwrite/gzclose were being used to  * write the document to disk.  The code for the header/trailer data to * the compression is plagiarized from the zlib source files. */static void *xmlCreateZMemBuff( int compression ) {    int			z_err;    int			hdr_lgth;    xmlZMemBuffPtr	buff = NULL;    if ( ( compression < 1 ) || ( compression > 9 ) )	return ( NULL );    /*  Create the control and data areas  */    buff = xmlMalloc( sizeof( xmlZMemBuff ) );    if ( buff == NULL ) {	xmlIOErrMemory("creating buffer context");	return ( NULL );    }    (void)memset( buff, 0, sizeof( xmlZMemBuff ) );    buff->size = INIT_HTTP_BUFF_SIZE;    buff->zbuff = xmlMalloc( buff->size );    if ( buff->zbuff == NULL ) {	xmlFreeZMemBuff( buff );	xmlIOErrMemory("creating buffer");	return ( NULL );    }    z_err = deflateInit2( &buff->zctrl, compression, Z_DEFLATED,			    DFLT_WBITS, DFLT_MEM_LVL, Z_DEFAULT_STRATEGY );    if ( z_err != Z_OK ) {	xmlChar msg[500];	xmlFreeZMemBuff( buff );	buff = NULL;	xmlStrPrintf(msg, 500,		    (const xmlChar *) "xmlCreateZMemBuff:  %s %d\n",		    "Error initializing compression context.  ZLIB error:",		    z_err );	xmlIOErr(XML_IO_WRITE, (const char *) msg);	return ( NULL );    }    /*  Set the header data.  The CRC will be needed for the trailer  */    buff->crc = crc32( 0L, Z_NULL, 0 );    hdr_lgth = snprintf( (char *)buff->zbuff, buff->size,			"%c%c%c%c%c%c%c%c%c%c",			GZ_MAGIC1, GZ_MAGIC2, Z_DEFLATED, 			0, 0, 0, 0, 0, 0, LXML_ZLIB_OS_CODE );    buff->zctrl.next_out  = buff->zbuff + hdr_lgth;    buff->zctrl.avail_out = buff->size - hdr_lgth;    return ( buff );}/** * xmlZMemBuffExtend * @buff:  Buffer used to compress and consolidate data. * @ext_amt:   Number of bytes to extend the buffer. * * Extend the internal buffer used to store the compressed data by the * specified amount. * * Returns 0 on success or -1 on failure to extend the buffer.  On failure * the original buffer still exists at the original size. */static intxmlZMemBuffExtend( xmlZMemBuffPtr buff, size_t ext_amt ) {    int			rc = -1;    size_t		new_size;    size_t		cur_used;    unsigned char *	tmp_ptr = NULL;    if ( buff == NULL )	return ( -1 );    else if ( ext_amt == 0 )	return ( 0 );    cur_used = buff->zctrl.next_out - buff->zbuff;    new_size = buff->size + ext_amt;#ifdef DEBUG_HTTP    if ( cur_used > new_size ) 	xmlGenericError( xmlGenericErrorContext,			"xmlZMemBuffExtend:  %s\n%s %d bytes.\n",			"Buffer overwrite detected during compressed memory",			"buffer extension.  Overflowed by", 			(cur_used - new_size ) );#endif    tmp_ptr = xmlRealloc( buff->zbuff, new_size );    if ( tmp_ptr != NULL ) {	rc = 0;	buff->size  = new_size;	buff->zbuff = tmp_ptr;	buff->zctrl.next_out  = tmp_ptr + cur_used;	buff->zctrl.avail_out = new_size - cur_used;    }    else {	xmlChar msg[500];	xmlStrPrintf(msg, 500,		    (const xmlChar *) "xmlZMemBuffExtend:  %s %lu bytes.\n",		    "Allocation failure extending output buffer to",		    new_size );	xmlIOErr(XML_IO_WRITE, (const char *) msg);    }    return ( rc );}/** * xmlZMemBuffAppend * @buff:  Buffer used to compress and consolidate data * @src:   Uncompressed source content to append to buffer * @len:   Length of source data to append to buffer * * Compress and append data to the internal buffer.  The data buffer * will be expanded if needed to store the additional data. * * Returns the number of bytes appended to the buffer or -1 on error. */static intxmlZMemBuffAppend( xmlZMemBuffPtr buff, const char * src, int len ) {    int		z_err;    size_t	min_accept;    if ( ( buff == NULL ) || ( src == NULL ) )	return ( -1 );    buff->zctrl.avail_in = len;    buff->zctrl.next_in  = (unsigned char *)src;    while ( buff->zctrl.avail_in > 0 ) {	/*	**  Extend the buffer prior to deflate call if a reasonable amount	**  of output buffer space is not available.	*/	min_accept = buff->zctrl.avail_in / DFLT_ZLIB_RATIO;	if ( buff->zctrl.avail_out <= min_accept ) {	    if ( xmlZMemBuffExtend( buff, buff->size ) == -1 )		return ( -1 );	}	z_err = deflate( &buff->zctrl, Z_NO_FLUSH );	if ( z_err != Z_OK ) {	    xmlChar msg[500];	    xmlStrPrintf(msg, 500,			(const xmlChar *) "xmlZMemBuffAppend:  %s %d %s - %d",			"Compression error while appending",			len, "bytes to buffer.  ZLIB error", z_err );	    xmlIOErr(XML_IO_WRITE, (const char *) msg);	    return ( -1 );	}    }    buff->crc = crc32( buff->crc, (unsigned char *)src, len );    return ( len );}/** * xmlZMemBuffGetContent * @buff:  Compressed memory content buffer * @data_ref:  Pointer reference to point to compressed content * * Flushes the compression buffers, appends gzip file trailers and * returns the compressed content and length of the compressed data. * NOTE:  The gzip trailer code here is plagiarized from zlib source. * * Returns the length of the compressed data or -1 on error. */static intxmlZMemBuffGetContent( xmlZMemBuffPtr buff, char ** data_ref ) {    int		zlgth = -1;    int		z_err;    if ( ( buff == NULL ) || ( data_ref == NULL ) )	return ( -1 );    /*  Need to loop until compression output buffers are flushed  */    do    {	z_err = deflate( &buff->zctrl, Z_FINISH );	if ( z_err == Z_OK ) {	    /*  In this case Z_OK means more buffer space needed  */	    if ( xmlZMemBuffExtend( buff, buff->size ) == -1 )		return ( -1 );	}    }    while ( z_err == Z_OK );    /*  If the compression state is not Z_STREAM_END, some error occurred  */    if ( z_err == Z_STREAM_END ) {	/*  Need to append the gzip data trailer  */	if ( buff->zctrl.avail_out < ( 2 * sizeof( unsigned long ) ) ) {	    if ( xmlZMemBuffExtend(buff, (2 * sizeof(unsigned long))) == -1 )		return ( -1 );	}	/*	**  For whatever reason, the CRC and length data are pushed out	**  in reverse byte order.  So a memcpy can't be used here.	*/	append_reverse_ulong( buff, buff->crc );	append_reverse_ulong( buff, buff->zctrl.total_in );	zlgth = buff->zctrl.next_out - buff->zbuff;	*data_ref = (char *)buff->zbuff;    }    else {	xmlChar msg[500];	xmlStrPrintf(msg, 500,		    (const xmlChar *) "xmlZMemBuffGetContent:  %s - %d\n",		    "Error flushing zlib buffers.  Error code", z_err );	xmlIOErr(XML_IO_WRITE, (const char *) msg);    }        return ( zlgth );}#endif /* LIBXML_OUTPUT_ENABLED */#endif  /*  HAVE_ZLIB_H  */#ifdef LIBXML_OUTPUT_ENABLED/** * xmlFreeHTTPWriteCtxt * @ctxt:  Context to cleanup * * Free allocated memory and reclaim system resources. * * No return value. */static voidxmlFreeHTTPWriteCtxt( xmlIOHTTPWriteCtxtPtr ctxt ){    if ( ctxt->uri != NULL )	xmlFree( ctxt->uri );    if ( ctxt->doc_buff != NULL ) {#ifdef HAVE_ZLIB_H	if ( ctxt->compression > 0 ) {	    xmlFreeZMemBuff( ctxt->doc_buff );	}	else#endif	{	    xmlOutputBufferClose( ctxt->doc_buff );	}    }    xmlFree( ctxt );    return;}#endif /* LIBXML_OUTPUT_ENABLED *//** * xmlIOHTTPMatch: * @filename:  the URI for matching * * check if the URI matches an HTTP one * * Returns 1 if matches, 0 otherwise */intxmlIOHTTPMatch (const char *filename) {    if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "http://", 7))	return(1);    return(0);}/** * xmlIOHTTPOpen: * @filename:  the URI for matching * * open an HTTP I/O channel * * Returns an I/O context or NULL in case of error */void *xmlIOHTTPOpen (const char *filename) {    return(xmlNanoHTTPOpen(filename, NULL));}#ifdef LIBXML_OUTPUT_ENABLED/** * xmlIOHTTPOpenW: * @post_uri:  The destination URI for the document * @compression:  The compression desired for the document. * * Open a temporary buffer to collect the document for a subsequent HTTP POST * request.  Non-static as is called from the output buffer creation routine. * * Returns an I/O context or NULL in case of error. */void *xmlIOHTTPOpenW(const char *post_uri, int compression){    xmlIOHTTPWriteCtxtPtr ctxt = NULL;    if (post_uri == NULL)        return (NULL);    ctxt = xmlMalloc(sizeof(xmlIOHTTPWriteCtxt));    if (ctxt == NULL) {	xmlIOErrMemory("creating HTTP output context");        return (NULL);    }    (void) memset(ctxt, 0, sizeof(xmlIOHTTPWriteCtxt));    ctxt->uri = (char *) xmlStrdup((const xmlChar *)post_uri);    if (ctxt->uri == NULL) {	xmlIOErrMemory("copying URI");        xmlFreeHTTPWriteCtxt(ctxt);        return (NULL);    }    /*     * **  Since the document length is required for an HTTP post,     * **  need to put the document into a buffer.  A memory buffer     * **  is being used to avoid pushing the data to disk and back.     */#ifdef HAVE_ZLIB_H    if ((compression > 0) && (compression <= 9)) {        ctxt->compression = compression;        ctxt->doc_buff = xmlCreateZMemBuff(compression);    } else#endif    {        /*  Any character conversions should have been done before this  */        ctxt->doc_buff = xmlAllocOutputBuffer(NULL);    }    if (ctxt->doc_buff == NULL) {        xmlFreeHTTPWriteCtxt(ctxt);        ctxt = NULL;    }    return (ctxt);}#endif /* LIBXML_OUTPUT_ENABLED */				#ifdef LIBXML_OUTPUT_ENABLED/** * xmlIOHTTPDfltOpenW * @post_uri:  The destination URI for this document. * * Calls xmlIOHTTPOpenW with no compression to set up for a subsequent * HTTP post command.  This function should generally not be used as * the open callback is short circuited in xmlOutputBufferCreateFile. * * Returns a pointer to the new IO context. */static void *xmlIOHTTPDfltOpenW( const char * post_uri ) {    return ( xmlIOHTTPOpenW( post_uri, 0 ) );}#endif /* LIBXML_OUTPUT_ENABLED *//** * xmlIOHTTPRead: * @context:  the I/O context * @buffer:  where to drop data * @len:  number of bytes to write * * Read @len bytes to @buffer from the I/O channel. * * Returns the number of bytes written */int xmlIOHTTPRead(void * context, char * buffer, int len) {    if ((buffer == NULL) || (len < 0)) return(-1);    return(xmlNanoHTTPRead(context, &buffer[0], len));}#ifdef LIBXML_OUTPUT_ENABLED/** * xmlIOHTTPWrite * @context:  previously opened writing context * @buffer:   data to output to temporary buffer * @len:      bytes to output * * Collect data from memory buffer into a temporary file for later * processing. *

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -