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