mirror of
https://gitlab.gnome.org/GNOME/libxml2.git
synced 2025-10-24 13:33:01 +03:00
html: Rework serialization of meta encoding attributes
Don't allocate memory.
This commit is contained in:
23
HTMLtree.c
23
HTMLtree.c
@@ -787,7 +787,7 @@ htmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlAttrPtr cur) {
|
|||||||
if (isUri) {
|
if (isUri) {
|
||||||
htmlSerializeUri(buf, content);
|
htmlSerializeUri(buf, content);
|
||||||
} else {
|
} else {
|
||||||
xmlSerializeText(buf, content,
|
xmlSerializeText(buf, content, SIZE_MAX,
|
||||||
XML_ESCAPE_HTML | XML_ESCAPE_ATTR);
|
XML_ESCAPE_HTML | XML_ESCAPE_ATTR);
|
||||||
}
|
}
|
||||||
} else if (child->type == XML_ENTITY_REF_NODE) {
|
} else if (child->type == XML_ENTITY_REF_NODE) {
|
||||||
@@ -917,21 +917,18 @@ htmlNodeDumpInternal(xmlOutputBufferPtr buf, xmlNodePtr cur,
|
|||||||
if ((!isMeta) || (attr != menc.attr)) {
|
if ((!isMeta) || (attr != menc.attr)) {
|
||||||
htmlAttrDumpOutput(buf, attr);
|
htmlAttrDumpOutput(buf, attr);
|
||||||
} else {
|
} else {
|
||||||
xmlChar *newVal;
|
|
||||||
|
|
||||||
xmlOutputBufferWrite(buf, 1, " ");
|
xmlOutputBufferWrite(buf, 1, " ");
|
||||||
xmlOutputBufferWriteString(buf, (char *) attr->name);
|
xmlOutputBufferWriteString(buf, (char *) attr->name);
|
||||||
|
|
||||||
newVal = htmlUpdateMetaEncoding(&menc, encoding);
|
|
||||||
if (newVal == NULL) {
|
|
||||||
buf->error = XML_ERR_NO_MEMORY;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
xmlOutputBufferWrite(buf, 2, "=\"");
|
xmlOutputBufferWrite(buf, 2, "=\"");
|
||||||
xmlSerializeText(buf, newVal,
|
xmlSerializeText(buf, menc.attrValue, menc.off.start,
|
||||||
|
XML_ESCAPE_HTML | XML_ESCAPE_ATTR);
|
||||||
|
xmlSerializeText(buf, BAD_CAST encoding, SIZE_MAX,
|
||||||
|
XML_ESCAPE_HTML | XML_ESCAPE_ATTR);
|
||||||
|
xmlSerializeText(buf, menc.attrValue + menc.off.end,
|
||||||
|
menc.off.size - menc.off.end,
|
||||||
XML_ESCAPE_HTML | XML_ESCAPE_ATTR);
|
XML_ESCAPE_HTML | XML_ESCAPE_ATTR);
|
||||||
xmlOutputBufferWrite(buf, 1, "\"");
|
xmlOutputBufferWrite(buf, 1, "\"");
|
||||||
xmlFree(newVal);
|
|
||||||
}
|
}
|
||||||
attr = attr->next;
|
attr = attr->next;
|
||||||
}
|
}
|
||||||
@@ -941,7 +938,7 @@ htmlNodeDumpInternal(xmlOutputBufferPtr buf, xmlNodePtr cur,
|
|||||||
} else if (cur->children == NULL) {
|
} else if (cur->children == NULL) {
|
||||||
if (addMeta) {
|
if (addMeta) {
|
||||||
xmlOutputBufferWrite(buf, 16, "><meta charset=\"");
|
xmlOutputBufferWrite(buf, 16, "><meta charset=\"");
|
||||||
xmlSerializeText(buf, BAD_CAST encoding,
|
xmlSerializeText(buf, BAD_CAST encoding, SIZE_MAX,
|
||||||
XML_ESCAPE_HTML | XML_ESCAPE_ATTR);
|
XML_ESCAPE_HTML | XML_ESCAPE_ATTR);
|
||||||
xmlOutputBufferWrite(buf, 4, "\"></");
|
xmlOutputBufferWrite(buf, 4, "\"></");
|
||||||
} else {
|
} else {
|
||||||
@@ -967,7 +964,7 @@ htmlNodeDumpInternal(xmlOutputBufferPtr buf, xmlNodePtr cur,
|
|||||||
xmlOutputBufferWrite(buf, 1, "\n");
|
xmlOutputBufferWrite(buf, 1, "\n");
|
||||||
if (addMeta) {
|
if (addMeta) {
|
||||||
xmlOutputBufferWrite(buf, 15, "<meta charset=\"");
|
xmlOutputBufferWrite(buf, 15, "<meta charset=\"");
|
||||||
xmlSerializeText(buf, BAD_CAST encoding,
|
xmlSerializeText(buf, BAD_CAST encoding, SIZE_MAX,
|
||||||
XML_ESCAPE_HTML | XML_ESCAPE_ATTR);
|
XML_ESCAPE_HTML | XML_ESCAPE_ATTR);
|
||||||
xmlOutputBufferWrite(buf, 2, "\">");
|
xmlOutputBufferWrite(buf, 2, "\">");
|
||||||
if ((format) &&
|
if ((format) &&
|
||||||
@@ -1008,7 +1005,7 @@ htmlNodeDumpInternal(xmlOutputBufferPtr buf, xmlNodePtr cur,
|
|||||||
(isRaw)) {
|
(isRaw)) {
|
||||||
xmlOutputBufferWriteString(buf, (const char *)cur->content);
|
xmlOutputBufferWriteString(buf, (const char *)cur->content);
|
||||||
} else {
|
} else {
|
||||||
xmlSerializeText(buf, cur->content, XML_ESCAPE_HTML);
|
xmlSerializeText(buf, cur->content, SIZE_MAX, XML_ESCAPE_HTML);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,10 @@
|
|||||||
#include <libxml/tree.h>
|
#include <libxml/tree.h>
|
||||||
#include <libxml/xmlversion.h>
|
#include <libxml/xmlversion.h>
|
||||||
|
|
||||||
|
#ifndef SIZE_MAX
|
||||||
|
#define SIZE_MAX ((size_t)(-1))
|
||||||
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Initial buffer size should include
|
* Initial buffer size should include
|
||||||
*
|
*
|
||||||
@@ -24,7 +28,7 @@ XML_HIDDEN xmlChar *
|
|||||||
xmlEscapeText(const xmlChar *text, int flags);
|
xmlEscapeText(const xmlChar *text, int flags);
|
||||||
#ifdef LIBXML_OUTPUT_ENABLED
|
#ifdef LIBXML_OUTPUT_ENABLED
|
||||||
XML_HIDDEN void
|
XML_HIDDEN void
|
||||||
xmlSerializeText(xmlOutputBufferPtr buf, const xmlChar *string,
|
xmlSerializeText(xmlOutputBufferPtr buf, const xmlChar *string, size_t maxSize,
|
||||||
unsigned flags);
|
unsigned flags);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
20
xmlIO.c
20
xmlIO.c
@@ -374,9 +374,9 @@ xmlEscapeText(const xmlChar *string, int flags) {
|
|||||||
|
|
||||||
#ifdef LIBXML_OUTPUT_ENABLED
|
#ifdef LIBXML_OUTPUT_ENABLED
|
||||||
void
|
void
|
||||||
xmlSerializeText(xmlOutputBufferPtr buf, const xmlChar *string,
|
xmlSerializeText(xmlOutputBufferPtr buf, const xmlChar *string, size_t maxSize,
|
||||||
unsigned flags) {
|
unsigned flags) {
|
||||||
const char *cur;
|
const xmlChar *cur;
|
||||||
const signed char *tab;
|
const signed char *tab;
|
||||||
|
|
||||||
if (string == NULL)
|
if (string == NULL)
|
||||||
@@ -400,10 +400,10 @@ xmlSerializeText(xmlOutputBufferPtr buf, const xmlChar *string,
|
|||||||
tab = xmlEscapeTab;
|
tab = xmlEscapeTab;
|
||||||
}
|
}
|
||||||
|
|
||||||
cur = (const char *) string;
|
cur = string;
|
||||||
|
|
||||||
while (*cur != 0) {
|
while (1) {
|
||||||
const char *base;
|
const xmlChar *base;
|
||||||
int c;
|
int c;
|
||||||
int offset;
|
int offset;
|
||||||
|
|
||||||
@@ -411,6 +411,9 @@ xmlSerializeText(xmlOutputBufferPtr buf, const xmlChar *string,
|
|||||||
offset = -1;
|
offset = -1;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
|
if ((size_t) (cur - string) >= maxSize)
|
||||||
|
break;
|
||||||
|
|
||||||
c = (unsigned char) *cur;
|
c = (unsigned char) *cur;
|
||||||
|
|
||||||
if (c < 0x80) {
|
if (c < 0x80) {
|
||||||
@@ -425,7 +428,10 @@ xmlSerializeText(xmlOutputBufferPtr buf, const xmlChar *string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (cur > base)
|
if (cur > base)
|
||||||
xmlOutputBufferWrite(buf, cur - base, base);
|
xmlOutputBufferWrite(buf, cur - base, (char *) base);
|
||||||
|
|
||||||
|
if ((size_t) (cur - string) >= maxSize)
|
||||||
|
break;
|
||||||
|
|
||||||
if (offset >= 0) {
|
if (offset >= 0) {
|
||||||
if (c == 0)
|
if (c == 0)
|
||||||
@@ -439,7 +445,7 @@ xmlSerializeText(xmlOutputBufferPtr buf, const xmlChar *string,
|
|||||||
int tempSize;
|
int tempSize;
|
||||||
int val = 0, len = 4;
|
int val = 0, len = 4;
|
||||||
|
|
||||||
val = xmlGetUTF8Char((const xmlChar *) cur, &len);
|
val = xmlGetUTF8Char(cur, &len);
|
||||||
if (val < 0) {
|
if (val < 0) {
|
||||||
val = 0xFFFD;
|
val = 0xFFFD;
|
||||||
cur += 1;
|
cur += 1;
|
||||||
|
|||||||
@@ -227,7 +227,7 @@ xmlSaveWriteText(xmlSaveCtxt *ctxt, const xmlChar *text, unsigned flags) {
|
|||||||
if (ctxt->encoding == NULL)
|
if (ctxt->encoding == NULL)
|
||||||
flags |= XML_ESCAPE_NON_ASCII;
|
flags |= XML_ESCAPE_NON_ASCII;
|
||||||
|
|
||||||
xmlSerializeText(ctxt->buf, text, flags);
|
xmlSerializeText(ctxt->buf, text, SIZE_MAX, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -780,7 +780,7 @@ xmlNsDumpOutput(xmlOutputBufferPtr buf, xmlNsPtr cur, xmlSaveCtxtPtr ctxt) {
|
|||||||
} else
|
} else
|
||||||
xmlOutputBufferWrite(buf, 5, "xmlns");
|
xmlOutputBufferWrite(buf, 5, "xmlns");
|
||||||
xmlOutputBufferWrite(buf, 2, "=\"");
|
xmlOutputBufferWrite(buf, 2, "=\"");
|
||||||
xmlSerializeText(buf, cur->href, escapeFlags);
|
xmlSerializeText(buf, cur->href, SIZE_MAX, escapeFlags);
|
||||||
xmlOutputBufferWrite(buf, 1, "\"");
|
xmlOutputBufferWrite(buf, 1, "\"");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2137,7 +2137,7 @@ xmlBufAttrSerializeTxtContent(xmlOutputBufferPtr buf, xmlDocPtr doc,
|
|||||||
|
|
||||||
if ((doc == NULL) || (doc->encoding == NULL))
|
if ((doc == NULL) || (doc->encoding == NULL))
|
||||||
flags |= XML_ESCAPE_NON_ASCII;
|
flags |= XML_ESCAPE_NON_ASCII;
|
||||||
xmlSerializeText(buf, string, flags);
|
xmlSerializeText(buf, string, SIZE_MAX, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user