Fix invalid XML output due to special chars, bug1444.

Signed-off-by: Wilson Snyder <[email protected]>
This commit is contained in:
Kanad Kanhere
2019-05-29 18:41:03 -04:00
committed by Wilson Snyder
parent b83b606267
commit 72eb361131
5 changed files with 83 additions and 30 deletions
+41 -18
View File
@@ -847,26 +847,49 @@ void V3OutFormatter::putcNoTracking(char chr) {
putcOutput(chr);
}
string V3OutFormatter::quoteNameControls(const string& namein, V3OutFormatter::Language) {
// Encode control chars into C style escapes
string V3OutFormatter::quoteNameControls(const string& namein, V3OutFormatter::Language lang) {
// Encode control chars into output-appropriate escapes
// Reverse is V3Parse::deQuote
string out;
for (string::const_iterator pos=namein.begin(); pos!=namein.end(); ++pos) {
if (pos[0]=='\\' || pos[0]=='"') {
out += string("\\")+pos[0];
} else if (pos[0]=='\n') {
out += "\\n";
} else if (pos[0]=='\r') {
out += "\\r";
} else if (pos[0]=='\t') {
out += "\\t";
} else if (isprint(pos[0])) {
out += pos[0];
} else {
// This will also cover \a etc
// Can't use %03o as messes up when signed
char octal[10]; sprintf(octal, "\\%o%o%o", (pos[0]>>6)&3, (pos[0]>>3)&7, pos[0]&7);
out += octal;
if (lang==LA_XML) {
// Encode chars into XML string
for (string::const_iterator pos=namein.begin(); pos!=namein.end(); ++pos) {
if (pos[0]=='"') {
out += string("&quot;");
} else if (pos[0]=='\'') {
out += string("&apos;");
} else if (pos[0]=='<') {
out += string("&lt;");
} else if (pos[0]=='>') {
out += string("&gt;");
} else if (pos[0]=='&') {
out += string("&amp;");
} else if (isprint(pos[0])) {
out += pos[0];
} else {
char decimal[10]; sprintf(decimal, "&#%u;", (unsigned char)pos[0]);
out += decimal;
}
}
} else {
// Encode control chars into C style escapes
for (string::const_iterator pos=namein.begin(); pos!=namein.end(); ++pos) {
if (pos[0]=='\\' || pos[0]=='"') {
out += string("\\")+pos[0];
} else if (pos[0]=='\n') {
out += "\\n";
} else if (pos[0]=='\r') {
out += "\\r";
} else if (pos[0]=='\t') {
out += "\\t";
} else if (isprint(pos[0])) {
out += pos[0];
} else {
// This will also cover \a etc
// Can't use %03o as messes up when signed
char octal[10]; sprintf(octal, "\\%o%o%o", (pos[0]>>6)&3, (pos[0]>>3)&7, pos[0]&7);
out += octal;
}
}
}
return out;