📄 entities.c.svn-base
字号:
if (out - buffer > buffer_size - 100) { int indx = out - buffer; growBufferReentrant(); out = &buffer[indx]; } /* * 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 >= 0x20) && (*cur < 0x80)) || (*cur == '\n') || (*cur == '\t') || ((html) && (*cur == '\r'))) { /* * default case, just copy ! */ *out++ = *cur; } else if (*cur >= 0x80) { if (((doc != NULL) && (doc->encoding != NULL)) || (html)) { /* * Bj鴕n Reese <br@sseusa.com> provided the patch xmlChar xc; xc = (*cur & 0x3F) << 6; if (cur[1] != 0) { xc += *(++cur) & 0x3F; *out++ = xc; } else */ *out++ = *cur; } else { /* * We assume we have UTF-8 input. */ char buf[11], *ptr; int val = 0, l = 1; if (*cur < 0xC0) { xmlGenericError(xmlGenericErrorContext, "xmlEncodeEntitiesReentrant : input not UTF-8\n"); if (doc != NULL) doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1"); _snprintf(buf, sizeof(buf), "&#%d;", *cur); buf[sizeof(buf) - 1] = 0; ptr = buf; while (*ptr != 0) *out++ = *ptr++; cur++; 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))) { xmlGenericError(xmlGenericErrorContext, "xmlEncodeEntitiesReentrant : char out of range\n"); if (doc != NULL) doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1"); _snprintf(buf, sizeof(buf), "&#%d;", *cur); buf[sizeof(buf) - 1] = 0; ptr = buf; while (*ptr != 0) *out++ = *ptr++; cur++; continue; } /* * We could do multiple things here. Just save as a char ref */ if (html) _snprintf(buf, sizeof(buf), "&#%d;", val); else _snprintf(buf, sizeof(buf), "&#x%X;", val); buf[sizeof(buf) - 1] = 0; ptr = buf; while (*ptr != 0) *out++ = *ptr++; cur += l; continue; } } else if (IS_BYTE_CHAR(*cur)) { char buf[11], *ptr; _snprintf(buf, sizeof(buf), "&#%d;", *cur); buf[sizeof(buf) - 1] = 0; ptr = buf; while (*ptr != 0) *out++ = *ptr++; } cur++; } *out++ = 0; return(buffer);}/** * xmlEncodeSpecialChars: * @doc: the document containing the string * @input: A string to convert to XML. * * Do a global encoding of a string, replacing the predefined entities * this routine is reentrant, and result must be deallocated. * * Returns A newly allocated string with the substitution done. */xmlChar *xmlEncodeSpecialChars(xmlDocPtr doc ATTRIBUTE_UNUSED, const xmlChar *input) { const xmlChar *cur = input; xmlChar *buffer = NULL; xmlChar *out = NULL; int buffer_size = 0; if (input == NULL) return(NULL); /* * allocate an translation buffer. */ buffer_size = 1000; buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar)); if (buffer == NULL) { xmlGenericError(xmlGenericErrorContext, "malloc failed\n"); return(NULL); } out = buffer; while (*cur != '\0') { if (out - buffer > buffer_size - 10) { int indx = out - buffer; growBufferReentrant(); out = &buffer[indx]; } /* * 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 == '\r') { *out++ = '&'; *out++ = '#'; *out++ = '1'; *out++ = '3'; *out++ = ';'; } else { /* * Works because on UTF-8, all extended sequences cannot * result in bytes in the ASCII range. */ *out++ = *cur; } 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) { return((xmlEntitiesTablePtr) xmlHashCreate(0));}/** * xmlFreeEntityWrapper: * @entity: An entity * @name: its name * * Deallocate the memory used by an entities in the hash table. */static voidxmlFreeEntityWrapper(xmlEntityPtr entity, const xmlChar *name ATTRIBUTE_UNUSED) { if (entity != NULL) xmlFreeEntity(entity);}/** * xmlFreeEntitiesTable: * @table: An entity table * * Deallocate the memory used by an entities hash table. */voidxmlFreeEntitiesTable(xmlEntitiesTablePtr table) { xmlHashFree(table, (xmlHashDeallocator) xmlFreeEntityWrapper);}#ifdef LIBXML_TREE_ENABLED/** * xmlCopyEntity: * @ent: An entity * * Build a copy of an entity * * Returns the new xmlEntitiesPtr or NULL in case of error. */static xmlEntityPtrxmlCopyEntity(xmlEntityPtr ent) { xmlEntityPtr cur; cur = (xmlEntityPtr) xmlMalloc(sizeof(xmlEntity)); if (cur == NULL) { xmlGenericError(xmlGenericErrorContext, "xmlCopyEntity: out of memory !\n"); return(NULL); } memset(cur, 0, sizeof(xmlEntity)); cur->type = XML_ENTITY_DECL; 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); if (ent->URI != NULL) cur->URI = xmlStrdup(ent->URI); return(cur);}/** * 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) { return(xmlHashCopy(table, (xmlHashCopier) xmlCopyEntity));}#endif /* LIBXML_TREE_ENABLED */#ifdef LIBXML_OUTPUT_ENABLED/** * xmlDumpEntityContent: * @buf: An XML buffer. * @content: The entity content. * * This will dump the quoted string value, taking care of the special * treatment required by % */static voidxmlDumpEntityContent(xmlBufferPtr buf, const xmlChar *content) { if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; if (xmlStrchr(content, '%')) { const xmlChar * base, *cur; xmlBufferCCat(buf, "\""); base = cur = content; while (*cur != 0) { if (*cur == '"') { if (base != cur) xmlBufferAdd(buf, base, cur - base); xmlBufferAdd(buf, BAD_CAST """, 6); cur++; base = cur; } else if (*cur == '%') { if (base != cur) xmlBufferAdd(buf, base, cur - base); xmlBufferAdd(buf, BAD_CAST "%", 6); cur++; base = cur; } else { cur++; } } if (base != cur) xmlBufferAdd(buf, base, cur - base); xmlBufferCCat(buf, "\""); } else { xmlBufferWriteQuotedString(buf, content); }}/** * 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 xmlDumpEntityContent(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) xmlDumpEntityContent(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: xmlGenericError(xmlGenericErrorContext, "xmlDumpEntitiesDecl: internal: unknown type %d\n", ent->etype); }}/** * xmlDumpEntityDeclScan: * @ent: An entity table * @buf: An XML buffer. * * When using the hash table scan function, arguments need to be reversed */static voidxmlDumpEntityDeclScan(xmlEntityPtr ent, xmlBufferPtr buf) { xmlDumpEntityDecl(buf, ent);} /** * 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) { xmlHashScan(table, (xmlHashScanner)xmlDumpEntityDeclScan, buf);}#endif /* LIBXML_OUTPUT_ENABLED */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -