2018-11-18 19:37:41 +00:00
|
|
|
|
using Ryujinx.HLE.HOS;
|
|
|
|
|
using System;
|
2018-08-14 00:13:01 +00:00
|
|
|
|
using System.Globalization;
|
2018-11-18 19:37:41 +00:00
|
|
|
|
using System.IO;
|
2018-08-14 00:13:01 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
2018-08-16 23:47:36 +00:00
|
|
|
|
namespace Ryujinx.HLE.Utilities
|
2018-08-14 00:13:01 +00:00
|
|
|
|
{
|
|
|
|
|
static class StringUtils
|
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
public static byte[] GetFixedLengthBytes(string InputString, int Size, Encoding Encoding)
|
2018-08-14 00:13:01 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
InputString = InputString + "\0";
|
2018-08-14 00:13:01 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
int BytesCount = Encoding.GetByteCount(InputString);
|
2018-08-14 00:13:01 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
byte[] Output = new byte[Size];
|
2018-08-14 00:13:01 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
if (BytesCount < Size)
|
2018-08-14 00:13:01 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
Encoding.GetBytes(InputString, 0, InputString.Length, Output, 0);
|
2018-08-14 00:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
int NullSize = Encoding.GetByteCount("\0");
|
2018-08-14 00:13:01 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
Output = Encoding.GetBytes(InputString);
|
2018-08-14 00:13:01 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
Array.Resize(ref Output, Size - NullSize);
|
2018-08-14 00:13:01 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
Output = Output.Concat(Encoding.GetBytes("\0")).ToArray();
|
2018-08-14 00:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
return Output;
|
2018-08-14 00:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
public static byte[] HexToBytes(string HexString)
|
2018-08-14 00:13:01 +00:00
|
|
|
|
{
|
2018-08-14 22:02:42 +00:00
|
|
|
|
//Ignore last charactor if HexLength % 2 != 0.
|
2018-12-05 00:52:39 +00:00
|
|
|
|
int BytesInHex = HexString.Length / 2;
|
2018-08-14 00:13:01 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
byte[] Output = new byte[BytesInHex];
|
2018-08-14 00:13:01 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
for (int Index = 0; Index < BytesInHex; Index++)
|
2018-08-14 00:13:01 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
Output[Index] = byte.Parse(HexString.Substring(Index * 2, 2), NumberStyles.HexNumber);
|
2018-08-14 00:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
return Output;
|
2018-08-14 00:13:01 +00:00
|
|
|
|
}
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
public static string ReadUtf8String(ServiceCtx Context, int Index = 0)
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
long Position = Context.Request.PtrBuff[Index].Position;
|
|
|
|
|
long Size = Context.Request.PtrBuff[Index].Size;
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
using (MemoryStream MS = new MemoryStream())
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
while (Size-- > 0)
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
2018-12-05 00:52:39 +00:00
|
|
|
|
byte Value = Context.Memory.ReadByte(Position++);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
if (Value == 0)
|
2018-11-18 19:37:41 +00:00
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
MS.WriteByte(Value);
|
2018-11-18 19:37:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 00:52:39 +00:00
|
|
|
|
return Encoding.UTF8.GetString(MS.ToArray());
|
2018-11-18 19:37:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-14 00:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|