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

📄 entities.c

📁 SIP 1.5.0源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * [2] Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] *                  | [#x10000-#x10FFFF] * any Unicode character, excluding the surrogate blocks, FFFE, and FFFF. */#define IS_CHAR(c)							\    (((c) == 0x09) || ((c) == 0x0a) || ((c) == 0x0d) ||			\     (((c) >= 0x20) && ((c) != 0xFFFE) && ((c) != 0xFFFF)))/* * A buffer used for converting entities to their equivalent and back. */static int buffer_size = 0;static xmlChar *buffer = NULL;int growBuffer(void) {    buffer_size *= 2;    buffer = (xmlChar *) xmlRealloc(buffer, buffer_size * sizeof(xmlChar));    if (buffer == NULL) {        perror("realloc failed");	return(-1);    }    return(0);}/** * xmlEncodeEntities: * @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. * * TODO: remove xmlEncodeEntities, once we are not afraid of breaking binary *       compatibility * * People must migrate their code to xmlEncodeEntitiesReentrant ! * This routine will issue a warning when encountered. *  * Returns A newly allocated string with the substitution done. */const xmlChar *xmlEncodeEntities(xmlDocPtr doc, const xmlChar *input) {    const xmlChar *cur = input;    xmlChar *out = buffer;    static int warning = 1;    int html = 0;    if (warning) {    fprintf(stderr, "Deprecated API xmlEncodeEntities() used\n");    fprintf(stderr, "   change code to use xmlEncodeEntitiesReentrant()\n");    warning = 0;    }    if (input == NULL) return(NULL);    if (doc != NULL)        html = (doc->type == XML_HTML_DOCUMENT_NODE);    if (buffer == NULL) {        buffer_size = 1000;        buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));	if (buffer == NULL) {	    perror("malloc failed");            return(NULL);	}	out = buffer;    }    while (*cur != '\0') {        if (out - buffer > buffer_size - 100) {	    int index = out - buffer;	    growBuffer();	    out = &buffer[index];	}	/*	 * By default one have to encode at least '<', '>', '"' and '&' !	 */	if (*cur == '<') {	    *out++ = '&';	    *out++ = 'l';	    *out++ = 't';	    *out++ = ';';	} else if (*cur == '>') {	    *out++ = '&';	    *out++ = 'g';	    *out++ = 't';	    *out++ = ';';	} else if (*cur == '&') {	    *out++ = '&';	    *out++ = 'a';	    *out++ = 'm';	    *out++ = 'p';	    *out++ = ';';	} else if (*cur == '"') {	    *out++ = '&';	    *out++ = 'q';	    *out++ = 'u';	    *out++ = 'o';	    *out++ = 't';	    *out++ = ';';	} else if ((*cur == '\'') && (!html)) {	    *out++ = '&';	    *out++ = 'a';	    *out++ = 'p';	    *out++ = 'o';	    *out++ = 's';	    *out++ = ';';	} else if (((*cur >= 0x20) && (*cur < 0x80)) ||	    (*cur == '\n') || (*cur == '\r') || (*cur == '\t')) {	    /*	     * default case, just copy !	     */	    *out++ = *cur;#ifndef USE_UTF_8	} else if ((sizeof(xmlChar) == 1) && (*cur >= 0x80)) {	    char buf[10], *ptr;#ifdef HAVE_SNPRINTF	    snprintf(buf, 9, "&#%d;", *cur);#else	    sprintf(buf, "&#%d;", *cur);#endif            ptr = buf;	    while (*ptr != 0) *out++ = *ptr++;#endif	} else if (IS_CHAR(*cur)) {	    char buf[10], *ptr;#ifdef HAVE_SNPRINTF	    snprintf(buf, 9, "&#%d;", *cur);#else	    sprintf(buf, "&#%d;", *cur);#endif            ptr = buf;	    while (*ptr != 0) *out++ = *ptr++;	}#if 0	else {	    /*	     * default case, this is not a valid char !	     * Skip it...	     */	    fprintf(stderr, "xmlEncodeEntities: invalid char %d\n", (int) *cur);	}#endif	cur++;    }    *out++ = 0;    return(buffer);}/* * Macro used to grow the current buffer. */#define growBufferReentrant() {						\    buffer_size *= 2;							\    buffer = (xmlChar *)						\    		xmlRealloc(buffer, buffer_size * sizeof(xmlChar));	\    if (buffer == NULL) {						\	perror("realloc failed");					\	return(NULL);							\    }									\}/** * 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. * * TODO !!!! Once moved to UTF-8 internal encoding, the encoding of non-ascii *           get erroneous. * * Returns A newly allocated string with the substitution done. */xmlChar *xmlEncodeEntitiesReentrant(xmlDocPtr doc, const xmlChar *input) {    const xmlChar *cur = input;    xmlChar *buffer = NULL;    xmlChar *out = NULL;    int buffer_size = 0;    int html = 0;    if (input == NULL) return(NULL);    if (doc != NULL)        html = (doc->type == XML_HTML_DOCUMENT_NODE);    /*     * allocate an translation buffer.     */    buffer_size = 1000;    buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));    if (buffer == NULL) {	perror("malloc failed");	return(NULL);    }    out = buffer;    while (*cur != '\0') {        if (out - buffer > buffer_size - 100) {	    int index = out - buffer;	    growBufferReentrant();	    out = &buffer[index];	}	/*	 * By default one have to encode at least '<', '>', '"' and '&' !	 */	if (*cur == '<') {	    *out++ = '&';	    *out++ = 'l';	    *out++ = 't';	    *out++ = ';';	} else if (*cur == '>') {	    *out++ = '&';	    *out++ = 'g';	    *out++ = 't';	    *out++ = ';';	} else if (*cur == '&') {	    *out++ = '&';	    *out++ = 'a';	    *out++ = 'm';	    *out++ = 'p';	    *out++ = ';';	} else if (*cur == '"') {	    *out++ = '&';	    *out++ = 'q';	    *out++ = 'u';	    *out++ = 'o';	    *out++ = 't';	    *out++ = ';';#if 0	} else if ((*cur == '\'') && (!html)) {	    *out++ = '&';	    *out++ = 'a';	    *out++ = 'p';	    *out++ = 'o';	    *out++ = 's';	    *out++ = ';';#endif	} else if (((*cur >= 0x20) && (*cur < 0x80)) ||	    (*cur == '\n') || (*cur == '\r') || (*cur == '\t')) {	    /*	     * default case, just copy !	     */	    *out++ = *cur;	} else if (*cur >= 0x80) {	    if (html) {		char buf[15], *ptr;		/*		 * TODO: improve by searching in html40EntitiesTable		 */#ifdef HAVE_SNPRINTF		snprintf(buf, 9, "&#%d;", *cur);#else		sprintf(buf, "&#%d;", *cur);#endif		ptr = buf;		while (*ptr != 0) *out++ = *ptr++;	    } else if (doc->encoding != NULL) {		/*		 * TODO !!!		 */		*out++ = *cur;	    } else {		/*		 * We assume we have UTF-8 input.		 */		char buf[10], *ptr;		int val = 0, l = 1;		if (*cur < 0xC0) {		    fprintf(stderr,			    "xmlEncodeEntitiesReentrant : input not UTF-8\n");		    doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");#ifdef HAVE_SNPRINTF		    snprintf(buf, 9, "&#%d;", *cur);#else		    sprintf(buf, "&#%d;", *cur);#endif		    ptr = buf;		    while (*ptr != 0) *out++ = *ptr++;		    continue;		} else if (*cur < 0xE0) {                    val = (cur[0]) & 0x1F;		    val <<= 6;		    val |= (cur[1]) & 0x3F;		    l = 2;		} else if (*cur < 0xF0) {                    val = (cur[0]) & 0x0F;		    val <<= 6;		    val |= (cur[1]) & 0x3F;		    val <<= 6;		    val |= (cur[2]) & 0x3F;		    l = 3;		} else if (*cur < 0xF8) {                    val = (cur[0]) & 0x07;		    val <<= 6;		    val |= (cur[1]) & 0x3F;		    val <<= 6;		    val |= (cur[2]) & 0x3F;		    val <<= 6;		    val |= (cur[3]) & 0x3F;		    l = 4;		}		if ((l == 1) || (!IS_CHAR(val))) {		    fprintf(stderr,			"xmlEncodeEntitiesReentrant : char out of range\n");		    doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");#ifdef HAVE_SNPRINTF		    snprintf(buf, 9, "&#%d;", *cur);#else		    sprintf(buf, "&#%d;", *cur);#endif		    ptr = buf;		    while (*ptr != 0) *out++ = *ptr++;		    cur++;		    continue;		}		/*		 * We could do multiple things here. Just save as a char ref		 */#ifdef HAVE_SNPRINTF		snprintf(buf, 14, "&#x%X;", val);#else		sprintf(buf, "&#x%X;", val);#endif		buf[14] = 0;		ptr = buf;		while (*ptr != 0) *out++ = *ptr++;		cur += l;		continue;	    }	} else if (IS_CHAR(*cur)) {	    char buf[10], *ptr;#ifdef HAVE_SNPRINTF	    snprintf(buf, 9, "&#%d;", *cur);#else	    sprintf(buf, "&#%d;", *cur);#endif            ptr = buf;	    while (*ptr != 0) *out++ = *ptr++;	}#if 0	else {	    /*	     * default case, this is not a valid char !	     * Skip it...	     */	    fprintf(stderr, "xmlEncodeEntities: invalid char %d\n", (int) *cur);	}#endif	cur++;    }    *out++ = 0;    return(buffer);}/** * xmlCreateEntitiesTable: * * create and initialize an empty entities hash table. * * Returns the xmlEntitiesTablePtr just created or NULL in case of error. */xmlEntitiesTablePtrxmlCreateEntitiesTable(void) {    xmlEntitiesTablePtr ret;    ret = (xmlEntitiesTablePtr)          xmlMalloc(sizeof(xmlEntitiesTable));    if (ret == NULL) {        fprintf(stderr, "xmlCreateEntitiesTable : xmlMalloc(%ld) failed\n",	        (long)sizeof(xmlEntitiesTable));        return(NULL);    }    ret->max_entities = XML_MIN_ENTITIES_TABLE;    ret->nb_entities = 0;    ret->table = (xmlEntityPtr *)          xmlMalloc(ret->max_entities * sizeof(xmlEntityPtr));    if (ret == NULL) {        fprintf(stderr, "xmlCreateEntitiesTable : xmlMalloc(%ld) failed\n",	        ret->max_entities * (long)sizeof(xmlEntityPtr));	xmlFree(ret);        return(NULL);    }    return(ret);}/** * xmlFreeEntitiesTable: * @table:  An entity table * * Deallocate the memory used by an entities hash table. */voidxmlFreeEntitiesTable(xmlEntitiesTablePtr table) {    int i;    if (table == NULL) return;    for (i = 0;i < table->nb_entities;i++) {        xmlFreeEntity(table->table[i]);    }    xmlFree(table->table);    xmlFree(table);}/** * xmlCopyEntitiesTable: * @table:  An entity table * * Build a copy of an entity table. *  * Returns the new xmlEntitiesTablePtr or NULL in case of error. */xmlEntitiesTablePtrxmlCopyEntitiesTable(xmlEntitiesTablePtr table) {    xmlEntitiesTablePtr ret;    xmlEntityPtr cur, ent;    int i;    ret = (xmlEntitiesTablePtr) xmlMalloc(sizeof(xmlEntitiesTable));    if (ret == NULL) {        fprintf(stderr, "xmlCopyEntitiesTable: out of memory !\n");	return(NULL);    }    ret->table = (xmlEntityPtr *) xmlMalloc(table->max_entities *                                            sizeof(xmlEntityPtr));    if (ret->table == NULL) {        fprintf(stderr, "xmlCopyEntitiesTable: out of memory !\n");	xmlFree(ret);	return(NULL);    }    ret->max_entities = table->max_entities;    ret->nb_entities = table->nb_entities;    for (i = 0;i < ret->nb_entities;i++) {	cur = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity));	if (cur == NULL) {	    fprintf(stderr, "xmlCopyEntityTable: out of memory !\n");	    xmlFree(ret);	    xmlFree(ret->table);	    return(NULL);	}	memset(cur, 0, sizeof(xmlEntity));	cur->type = XML_ELEMENT_DECL;	ret->table[i] = cur;	ent = table->table[i];	cur->etype = ent->etype;	if (ent->name != NULL)	    cur->name = xmlStrdup(ent->name);	if (ent->ExternalID != NULL)	    cur->ExternalID = xmlStrdup(ent->ExternalID);	if (ent->SystemID != NULL)	    cur->SystemID = xmlStrdup(ent->SystemID);	if (ent->content != NULL)	    cur->content = xmlStrdup(ent->content);	if (ent->orig != NULL)	    cur->orig = xmlStrdup(ent->orig);    }    return(ret);}/** * xmlDumpEntityDecl: * @buf:  An XML buffer. * @ent:  An entity table * * This will dump the content of the entity table as an XML DTD definition */voidxmlDumpEntityDecl(xmlBufferPtr buf, xmlEntityPtr ent) {    switch (ent->etype) {	case XML_INTERNAL_GENERAL_ENTITY:	    xmlBufferWriteChar(buf, "<!ENTITY ");	    xmlBufferWriteCHAR(buf, ent->name);	    xmlBufferWriteChar(buf, " ");	    if (ent->orig != NULL)		xmlBufferWriteQuotedString(buf, ent->orig);	    else		xmlBufferWriteQuotedString(buf, ent->content);	    xmlBufferWriteChar(buf, ">\n");	    break;	case XML_EXTERNAL_GENERAL_PARSED_ENTITY:	    xmlBufferWriteChar(buf, "<!ENTITY ");	    xmlBufferWriteCHAR(buf, ent->name);	    if (ent->ExternalID != NULL) {		 xmlBufferWriteChar(buf, " PUBLIC ");		 xmlBufferWriteQuotedString(buf, ent->ExternalID);		 xmlBufferWriteChar(buf, " ");		 xmlBufferWriteQuotedString(buf, ent->SystemID);	    } else {		 xmlBufferWriteChar(buf, " SYSTEM ");		 xmlBufferWriteQuotedString(buf, ent->SystemID);	    }	    xmlBufferWriteChar(buf, ">\n");	    break;	case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:	    xmlBufferWriteChar(buf, "<!ENTITY ");	    xmlBufferWriteCHAR(buf, ent->name);	    if (ent->ExternalID != NULL) {		 xmlBufferWriteChar(buf, " PUBLIC ");		 xmlBufferWriteQuotedString(buf, ent->ExternalID);		 xmlBufferWriteChar(buf, " ");		 xmlBufferWriteQuotedString(buf, ent->SystemID);	    } else {		 xmlBufferWriteChar(buf, " SYSTEM ");		 xmlBufferWriteQuotedString(buf, ent->SystemID);	    }	    if (ent->content != NULL) { /* Should be true ! */		xmlBufferWriteChar(buf, " NDATA ");		if (ent->orig != NULL)		    xmlBufferWriteCHAR(buf, ent->orig);		else		    xmlBufferWriteCHAR(buf, ent->content);	    }	    xmlBufferWriteChar(buf, ">\n");	    break;	case XML_INTERNAL_PARAMETER_ENTITY:	    xmlBufferWriteChar(buf, "<!ENTITY % ");	    xmlBufferWriteCHAR(buf, ent->name);	    xmlBufferWriteChar(buf, " ");	    if (ent->orig == NULL)		xmlBufferWriteQuotedString(buf, ent->content);	    else		xmlBufferWriteQuotedString(buf, ent->orig);	    xmlBufferWriteChar(buf, ">\n");	    break;	case XML_EXTERNAL_PARAMETER_ENTITY:	    xmlBufferWriteChar(buf, "<!ENTITY % ");	    xmlBufferWriteCHAR(buf, ent->name);	    if (ent->ExternalID != NULL) {		 xmlBufferWriteChar(buf, " PUBLIC ");		 xmlBufferWriteQuotedString(buf, ent->ExternalID);		 xmlBufferWriteChar(buf, " ");		 xmlBufferWriteQuotedString(buf, ent->SystemID);	    } else {		 xmlBufferWriteChar(buf, " SYSTEM ");		 xmlBufferWriteQuotedString(buf, ent->SystemID);	    }	    xmlBufferWriteChar(buf, ">\n");	    break;	default:	    fprintf(stderr,		"xmlDumpEntitiesTable: internal: unknown type %d\n",		    ent->etype);    }}/** * xmlDumpEntitiesTable: * @buf:  An XML buffer. * @table:  An entity table * * This will dump the content of the entity table as an XML DTD definition */voidxmlDumpEntitiesTable(xmlBufferPtr buf, xmlEntitiesTablePtr table) {    int i;    xmlEntityPtr cur;    if (table == NULL) return;    for (i = 0;i < table->nb_entities;i++) {        cur = table->table[i];	xmlDumpEntityDecl(buf, cur);    }}

⌨️ 快捷键说明

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