提交 7c23f96f 编写于 作者: A Atsushi Eno

Auto detect BOM-less utf-16 and utf-32. Fixed bug #674580.

上级 707b30ed
......@@ -339,11 +339,13 @@ namespace System.Xml
class XmlInputStream : Stream
{
public static readonly Encoding StrictUTF8;
internal static readonly Encoding StrictUTF8, Strict1234UTF32, StrictBigEndianUTF16;
static XmlInputStream ()
{
StrictUTF8 = new UTF8Encoding (false, true);
Strict1234UTF32 = new UTF32Encoding (true, false, true);
StrictBigEndianUTF16 = new UnicodeEncoding (true, false, true);
}
Encoding enc;
......@@ -422,9 +424,23 @@ namespace System.Xml
buffer [--bufPos] = 0xEF;
}
break;
case 0:
// It could still be 1234/2143/3412 variants of UTF32, but only 1234 version is available on .NET.
c = ReadByteSpecial ();
if (c == 0)
enc = Strict1234UTF32;
else
enc = StrictBigEndianUTF16;
break;
case '<':
// try to get encoding name from XMLDecl.
if (bufLength >= 5 && GetStringFromBytes (1, 4) == "?xml") {
c = ReadByteSpecial ();
if (c == 0) {
if (ReadByteSpecial () == 0)
enc = Encoding.UTF32; // little endian UTF32
else
enc = Encoding.Unicode; // little endian UTF16
} else if (bufLength >= 4 && GetStringFromBytes (1, 4) == "?xml") {
// try to get encoding name from XMLDecl.
bufPos += 4;
c = SkipWhitespace ();
......
......@@ -1374,5 +1374,13 @@ namespace MonoTests.System.Xml
} while (read > 0);
Assert.AreEqual ("<child>a</child>", sb.ToString (), "#1");
}
[Test]
public void BOMLessUTF16Detection () // bug #674580
{
var ms = new MemoryStream (Encoding.Unicode.GetBytes ("<root />"));
var xtr = new XmlTextReader (ms);
xtr.Read ();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册