2020-03-25 04:14:35 -04:00
|
|
|
|
using LibHac.Common;
|
|
|
|
|
using Ryujinx.HLE.HOS;
|
2018-11-18 14:37:41 -05:00
|
|
|
|
using System;
|
2018-08-13 20:13:01 -04:00
|
|
|
|
using System.Globalization;
|
2018-11-18 14:37:41 -05:00
|
|
|
|
using System.IO;
|
2018-08-13 20:13:01 -04:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
2018-08-16 19:47:36 -04:00
|
|
|
|
namespace Ryujinx.HLE.Utilities
|
2018-08-13 20:13:01 -04:00
|
|
|
|
{
|
|
|
|
|
static class StringUtils
|
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
public static byte[] GetFixedLengthBytes(string inputString, int size, Encoding encoding)
|
2018-08-13 20:13:01 -04:00
|
|
|
|
{
|
2021-09-14 19:24:49 -04:00
|
|
|
|
inputString += "\0";
|
2018-08-13 20:13:01 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
int bytesCount = encoding.GetByteCount(inputString);
|
2018-08-13 20:13:01 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
byte[] output = new byte[size];
|
2018-08-13 20:13:01 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
if (bytesCount < size)
|
2018-08-13 20:13:01 -04:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
encoding.GetBytes(inputString, 0, inputString.Length, output, 0);
|
2018-08-13 20:13:01 -04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
int nullSize = encoding.GetByteCount("\0");
|
2018-08-13 20:13:01 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
output = encoding.GetBytes(inputString);
|
2018-08-13 20:13:01 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
Array.Resize(ref output, size - nullSize);
|
2018-08-13 20:13:01 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
output = output.Concat(encoding.GetBytes("\0")).ToArray();
|
2018-08-13 20:13:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return output;
|
2018-08-13 20:13:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-24 18:13:20 -04:00
|
|
|
|
public static string ReadInlinedAsciiString(BinaryReader reader, int maxSize)
|
|
|
|
|
{
|
|
|
|
|
byte[] data = reader.ReadBytes(maxSize);
|
|
|
|
|
|
|
|
|
|
int stringSize = Array.IndexOf<byte>(data, 0);
|
|
|
|
|
|
|
|
|
|
return Encoding.ASCII.GetString(data, 0, stringSize < 0 ? maxSize : stringSize);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
public static byte[] HexToBytes(string hexString)
|
2018-08-13 20:13:01 -04:00
|
|
|
|
{
|
2019-07-01 22:39:22 -04:00
|
|
|
|
// Ignore last character if HexLength % 2 != 0.
|
2018-12-06 06:16:24 -05:00
|
|
|
|
int bytesInHex = hexString.Length / 2;
|
2018-08-13 20:13:01 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
byte[] output = new byte[bytesInHex];
|
2018-08-13 20:13:01 -04:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
for (int index = 0; index < bytesInHex; index++)
|
2018-08-13 20:13:01 -04:00
|
|
|
|
{
|
2022-10-18 19:31:34 -04:00
|
|
|
|
output[index] = byte.Parse(hexString.AsSpan(index * 2, 2), NumberStyles.HexNumber);
|
2018-08-13 20:13:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return output;
|
2018-08-13 20:13:01 -04:00
|
|
|
|
}
|
2018-11-18 14:37:41 -05:00
|
|
|
|
|
2022-01-12 11:43:00 -05:00
|
|
|
|
public static string ReadUtf8String(ReadOnlySpan<byte> data, out int dataRead)
|
|
|
|
|
{
|
|
|
|
|
dataRead = data.IndexOf((byte)0) + 1;
|
|
|
|
|
|
|
|
|
|
if (dataRead <= 1)
|
|
|
|
|
{
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Encoding.UTF8.GetString(data[..dataRead]);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
public static string ReadUtf8String(ServiceCtx context, int index = 0)
|
2018-11-18 14:37:41 -05:00
|
|
|
|
{
|
2021-04-24 06:16:01 -04:00
|
|
|
|
ulong position = context.Request.PtrBuff[index].Position;
|
|
|
|
|
ulong size = context.Request.PtrBuff[index].Size;
|
2018-11-18 14:37:41 -05:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
using (MemoryStream ms = new MemoryStream())
|
2018-11-18 14:37:41 -05:00
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
|
while (size-- > 0)
|
2018-11-18 14:37:41 -05:00
|
|
|
|
{
|
2021-04-24 06:16:01 -04:00
|
|
|
|
byte value = context.Memory.Read<byte>(position++);
|
2018-11-18 14:37:41 -05:00
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
if (value == 0)
|
2018-11-18 14:37:41 -05:00
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
ms.WriteByte(value);
|
2018-11-18 14:37:41 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
|
return Encoding.UTF8.GetString(ms.ToArray());
|
2018-11-18 14:37:41 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-15 21:31:18 -04:00
|
|
|
|
|
2020-03-25 04:14:35 -04:00
|
|
|
|
public static U8Span ReadUtf8Span(ServiceCtx context, int index = 0)
|
|
|
|
|
{
|
2021-09-14 19:24:49 -04:00
|
|
|
|
ulong position = context.Request.PtrBuff[index].Position;
|
|
|
|
|
ulong size = context.Request.PtrBuff[index].Size;
|
2020-03-25 04:14:35 -04:00
|
|
|
|
|
2020-05-03 18:54:50 -04:00
|
|
|
|
ReadOnlySpan<byte> buffer = context.Memory.GetSpan(position, (int)size);
|
2020-03-25 04:14:35 -04:00
|
|
|
|
|
|
|
|
|
return new U8Span(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-15 21:31:18 -04:00
|
|
|
|
public static string ReadUtf8StringSend(ServiceCtx context, int index = 0)
|
|
|
|
|
{
|
2021-04-24 06:16:01 -04:00
|
|
|
|
ulong position = context.Request.SendBuff[index].Position;
|
|
|
|
|
ulong size = context.Request.SendBuff[index].Size;
|
2019-06-15 21:31:18 -04:00
|
|
|
|
|
|
|
|
|
using (MemoryStream ms = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
while (size-- > 0)
|
|
|
|
|
{
|
2021-09-14 19:24:49 -04:00
|
|
|
|
byte value = context.Memory.Read<byte>(position++);
|
2019-06-15 21:31:18 -04:00
|
|
|
|
|
|
|
|
|
if (value == 0)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ms.WriteByte(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Encoding.UTF8.GetString(ms.ToArray());
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-04 11:20:40 -04:00
|
|
|
|
|
2022-06-24 13:04:57 -04:00
|
|
|
|
public static int CompareCStr(ReadOnlySpan<byte> s1, ReadOnlySpan<byte> s2)
|
2019-07-04 11:20:40 -04:00
|
|
|
|
{
|
|
|
|
|
int s1Index = 0;
|
|
|
|
|
int s2Index = 0;
|
|
|
|
|
|
|
|
|
|
while (s1[s1Index] != 0 && s2[s2Index] != 0 && s1[s1Index] == s2[s2Index])
|
|
|
|
|
{
|
|
|
|
|
s1Index += 1;
|
|
|
|
|
s2Index += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return s2[s2Index] - s1[s1Index];
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 13:04:57 -04:00
|
|
|
|
public static int LengthCstr(ReadOnlySpan<byte> s)
|
2019-07-04 11:20:40 -04:00
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
2022-06-24 13:04:57 -04:00
|
|
|
|
while (s[i] != 0)
|
2019-07-04 11:20:40 -04:00
|
|
|
|
{
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return i;
|
|
|
|
|
}
|
2018-08-13 20:13:01 -04:00
|
|
|
|
}
|
2021-09-14 19:24:49 -04:00
|
|
|
|
}
|