Skip to content

Commit

Permalink
Fixed BOM detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed May 29, 2024
1 parent d5fef0d commit 24f8a11
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
19 changes: 14 additions & 5 deletions src/Bannerlord.LauncherManager.Native/ReaderUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,29 @@ namespace Bannerlord.LauncherManager.Native;

public static class ReaderUtils2
{
private static readonly byte[] BOMMarkUtf8 = Encoding.UTF8.GetPreamble();
private static readonly byte[] BOMMarkUtf8 = [0xEF, 0xBB, 0xBF];
private static readonly byte[] BOMMarkUtf16BE = [0xFE, 0xFF];
private static readonly byte[] BOMMarkUtf16LE = [0xFF, 0xFE];

public static XmlDocument Read(ReadOnlySpan<char> xml)
{
var doc = new XmlDocument();
doc.LoadXml(new string(RemoveBOM2(xml)));
return doc;
}

private static ReadOnlySpan<char> RemoveBOM2(ReadOnlySpan<char> xml)
{
var bom = MemoryMarshal.Cast<byte, char>(BOMMarkUtf8.AsSpan());
if (xml.StartsWith(bom))
xml = xml.Slice(bom.Length);
var utf8BOM = MemoryMarshal.Cast<byte, char>(BOMMarkUtf8.AsSpan());
var utf16BEBOM = MemoryMarshal.Cast<byte, char>(BOMMarkUtf16BE.AsSpan());
var utf16LEBOM = MemoryMarshal.Cast<byte, char>(BOMMarkUtf16LE.AsSpan());

var bomOffset = xml.StartsWith(utf8BOM) ? utf8BOM.Length :
xml.StartsWith(utf16BEBOM) ? utf16BEBOM.Length :
xml.StartsWith(utf16LEBOM) ? utf16LEBOM.Length : 0;

xml = xml.Slice(bomOffset > 0 ? bomOffset : 0);

return xml;
}
}
25 changes: 17 additions & 8 deletions src/Bannerlord.LauncherManager/Utils/ReaderUtils.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
using System;
using System.IO;
using System.Text;
using System.Xml;

namespace Bannerlord.LauncherManager.Utils;

public static class ReaderUtils
{
private static readonly byte[] BOMMarkUtf8 = Encoding.UTF8.GetPreamble();
private static readonly byte[] BOMMarkUtf8 = [0xEF, 0xBB, 0xBF];
private static readonly byte[] BOMMarkUtf16BE = [0xFE, 0xFF];
private static readonly byte[] BOMMarkUtf16LE = [0xFF, 0xFE];

public static XmlDocument Read(byte[] data)
{
using var ms = new MemoryStream(data);
using var sr = new StreamReader(ms);

Span<byte> bom = BOMMarkUtf8.AsSpan();
var utf8BOM = BOMMarkUtf8.AsSpan();
var utf16BEBOM = BOMMarkUtf16BE.AsSpan();
var utf16LEBOM = BOMMarkUtf16LE.AsSpan();

#if NETSTANDARD2_1_OR_GREATER
Span<byte> preamblePreallocated = stackalloc byte[BOMMarkUtf8.Length];
Span<byte> preamblePreallocated = stackalloc byte[4];
ms.Read(preamblePreallocated);
#else
var preamblePreallocated = new byte[BOMMarkUtf8.Length];
ms.Read(preamblePreallocated, 0, BOMMarkUtf8.Length);
var preamblePreallocatedArray = new byte[4];
ms.Read(preamblePreallocatedArray, 0, 4);
var preamblePreallocated = preamblePreallocatedArray.AsSpan();
#endif

ms.Seek(bom.SequenceEqual(preamblePreallocated) ? BOMMarkUtf8.Length : 0, SeekOrigin.Begin);

var bomOffset = preamblePreallocated.StartsWith(utf8BOM) ? BOMMarkUtf8.Length :
preamblePreallocated.StartsWith(utf16BEBOM) ? BOMMarkUtf16BE.Length :
preamblePreallocated.StartsWith(utf16LEBOM) ? BOMMarkUtf16LE.Length : 0;

ms.Seek(bomOffset > 0 ? bomOffset : 0, SeekOrigin.Begin);
var doc = new XmlDocument();
doc.Load(new XmlTextReader(sr));
return doc;
Expand Down

0 comments on commit 24f8a11

Please sign in to comment.