Skip to content

Commit

Permalink
Merge pull request #3 from Milkitic/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Milkitic authored Feb 17, 2022
2 parents a8623ae + 2591c13 commit 25a58f2
Show file tree
Hide file tree
Showing 8 changed files with 438 additions and 124 deletions.
11 changes: 11 additions & 0 deletions src/Benchmarks/WriteBenchmark/data/binaries.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<confuse__a0>
<hash __type="bin" __size="16">f710b5f1c5c56a69f710b5f1b836de22</hash>
<haha __type="bin" __size="256">f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22</haha>
<haha2 __type="bin" __size="1024">f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22f710b5f1c5c56a69f710b5f1b836de22</haha2>
<hash __type="bin" __size="0"></hash>
<kingdom>
<cf __type="bin" __size="16">F34255041131EDDFC769181B8F33892E</cf>
<qcf __type="bin" __size="32">AA4A965AA8C2C169D145E75B5DA93879CD8AD1A3F32185662DC54341263DBB03</qcf>
<piece __type="bin" __size="40">1CE9481CA73F4B0AD6867EB0D51A0E1672946BE5B6D1B109F327348C9B7CBB2C15781A0482C3953C</piece>
</kingdom>
</confuse__a0>
45 changes: 24 additions & 21 deletions src/KbinXml.Net/KbinConverter.Writers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private static byte[] WriterImpl(Encoding encoding, WriteContext context, XmlRea
var holdingAttrs = new SortedDictionary<string, string>(StringComparer.Ordinal);
string holdingValue = "";
string? typeStr = null;
string? sizeStr = null;
string? arrayCountStr = null;
byte typeid = 0;

void EnsureHolding()
Expand All @@ -104,23 +104,23 @@ void EnsureHolding()
{
var type = TypeDictionary.TypeMap[typeid];
var value = holdingValue.SpanSplit(' ');
var size = (uint)(type.Size * type.Count);
if (sizeStr != null)
var requiredBytes = (uint)(type.Size * type.Count);
if (arrayCountStr != null)
{
size *= uint.Parse(sizeStr);
context.DataWriter.WriteU32(size);
requiredBytes *= uint.Parse(arrayCountStr);
context.DataWriter.WriteU32(requiredBytes);
}

if (size > int.MaxValue)
if (requiredBytes > int.MaxValue)
throw new KbinException("uint size is greater than int.MaxValue");

var iSize = (int)size;
var iRequiredBytes = (int)requiredBytes;
byte[]? arr = null;
var span = iSize <= Constants.MaxStackLength
? stackalloc byte[iSize]
: arr = ArrayPool<byte>.Shared.Rent(iSize);
var span = iRequiredBytes <= Constants.MaxStackLength
? stackalloc byte[iRequiredBytes]
: arr = ArrayPool<byte>.Shared.Rent(iRequiredBytes);

if (arr != null) span = span.Slice(0, iSize);
if (arr != null) span = span.Slice(0, iRequiredBytes);
var builder = new ValueListBuilder<byte>(span);

try
Expand All @@ -130,7 +130,7 @@ void EnsureHolding()
{
try
{
if (i == iSize) break;
if (i == iRequiredBytes) break;
var add = type.WriteString(ref builder, s);
if (add < type.Size)
{
Expand All @@ -156,7 +156,7 @@ void EnsureHolding()
}

typeStr = null;
sizeStr = null;
arrayCountStr = null;
holdingValue = "";
typeid = 0;
}
Expand All @@ -165,8 +165,6 @@ void EnsureHolding()
{
foreach (var attribute in holdingAttrs)
{
if (attribute.Key == "__size") continue;

context.NodeWriter.WriteU8(0x2E);
context.NodeWriter.WriteString(attribute.Key);
context.DataWriter.WriteString(attribute.Value);
Expand Down Expand Up @@ -194,7 +192,11 @@ void EnsureHolding()
}
else if (reader.Name == "__count")
{
sizeStr = reader.Value;
arrayCountStr = reader.Value;
}
else if (reader.Name == "__size")
{
// ignore
}
else
{
Expand All @@ -213,7 +215,7 @@ void EnsureHolding()
else
{
typeid = TypeDictionary.ReverseTypeMap[typeStr];
if (sizeStr != null)
if (arrayCountStr != null)
context.NodeWriter.WriteU8((byte)(typeid | 0x40));
else
context.NodeWriter.WriteU8(typeid);
Expand Down Expand Up @@ -250,10 +252,11 @@ void EnsureHolding()

//Write header data
var output = new BeBinaryWriter();
output.WriteU8(0xA0); //Magic
output.WriteU8(0x42); //Compression flag
output.WriteU8(EncodingDictionary.ReverseEncodingMap[encoding]);
output.WriteU8((byte)~EncodingDictionary.ReverseEncodingMap[encoding]);
output.WriteU8(0xA0); // Signature
output.WriteU8((byte)(context.NodeWriter.Compressed ? 0x42 : 0x45)); // Compression flag
var encodingBytes = EncodingDictionary.ReverseEncodingMap[encoding];
output.WriteU8(encodingBytes);
output.WriteU8((byte)~encodingBytes);

//Write node buffer length and contents.
var nodeStream = context.NodeWriter.Stream;
Expand Down
59 changes: 2 additions & 57 deletions src/KbinXml.Net/Readers/DataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Buffers;
using System.Text;
using KbinXml.Net.Internal;
using KbinXml.Net.Utils;

namespace KbinXml.Net.Readers;

Expand Down Expand Up @@ -88,54 +89,7 @@ public string ReadBinary(int count)
var bin = Read32BitAligned(count);
if (bin.Length == 0)
return string.Empty;
#if NETCOREAPP3_1_OR_GREATER
var str = string.Create(bin.Length * 2, bin, static (dst, state) =>
{
var src = state.Span;

int i = 0;
int j = 0;

while (i < src.Length)
{
var b = src[i++];
dst[j++] = ToCharLower(b >> 4);
dst[j++] = ToCharLower(b);
}
});
return str;
#else
var src = bin.Span;
var dstLen = bin.Length * 2;

char[]? arr = null;
Span<char> dst = dstLen <= Constants.MaxStackLength
? stackalloc char[dstLen]
: arr = ArrayPool<char>.Shared.Rent(dstLen);
if (arr != null) dst = dst.Slice(0, dstLen);
try
{
int i = 0;
int j = 0;

while (i < bin.Length)
{
var b = src[i++];
dst[j++] = ToCharLower(b >> 4);
dst[j++] = ToCharLower(b);
}

unsafe
{
fixed (char* p = dst)
return new string(p, 0, dstLen);
}
}
finally
{
if (arr != null) ArrayPool<char>.Shared.Return(arr);
}
#endif
return ConvertHelper.ToHexString(bin.Span);
}

private Memory<byte> ReadBytes(int offset, int count)
Expand All @@ -152,13 +106,4 @@ private void Realign16_8()
if (_pos16 % 4 == 0)
_pos16 = _pos32;
}

private static char ToCharLower(int value)
{
value &= 0xF;
value += '0';

if (value > '9') value += ('a' - ('9' + 1));
return (char)value;
}
}
10 changes: 10 additions & 0 deletions src/KbinXml.Net/Utils/ConvertHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ public static string Ip4ToString(ReadOnlySpan<byte> bytes)
}
}

public static string ToHexString(ReadOnlySpan<byte> bytes)
{
if (bytes.Length == 0)
return string.Empty;
if (bytes.Length > int.MaxValue / 2)
throw new ArgumentOutOfRangeException(nameof(bytes));

return HexConverter.ToString(bytes, HexConverter.Casing.Lower);
}

private static int IPv4AddressToStringHelper(uint address, Span<char> dst)
{
int offset = 0;
Expand Down
Loading

0 comments on commit 25a58f2

Please sign in to comment.