2018-11-18 14:37:41 -05:00
|
|
|
|
using Ryujinx.HLE.HOS;
|
|
|
|
|
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-04 15:23:37 -05:00
|
|
|
|
public static byte[] GetFixedLengthBytes(string inputString, int size, Encoding encoding)
|
2018-08-13 20:13:01 -04:00
|
|
|
|
{
|
2018-12-04 15:23:37 -05:00
|
|
|
|
inputString = inputString + "\0";
|
2018-08-13 20:13:01 -04:00
|
|
|
|
|
2018-12-04 15:23:37 -05:00
|
|
|
|
int bytesCount = encoding.GetByteCount(inputString);
|
2018-08-13 20:13:01 -04:00
|
|
|
|
|
2018-12-04 15:23:37 -05:00
|
|
|
|
byte[] output = new byte[size];
|
2018-08-13 20:13:01 -04:00
|
|
|
|
|
2018-12-04 15:23:37 -05:00
|
|
|
|
if (bytesCount < size)
|
2018-08-13 20:13:01 -04:00
|
|
|
|
{
|
2018-12-04 15:23:37 -05:00
|
|
|
|
encoding.GetBytes(inputString, 0, inputString.Length, output, 0);
|
2018-08-13 20:13:01 -04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-12-04 15:23:37 -05:00
|
|
|
|
int nullSize = encoding.GetByteCount("\0");
|
2018-08-13 20:13:01 -04:00
|
|
|
|
|
2018-12-04 15:23:37 -05:00
|
|
|
|
output = encoding.GetBytes(inputString);
|
2018-08-13 20:13:01 -04:00
|
|
|
|
|
2018-12-04 15:23:37 -05:00
|
|
|
|
Array.Resize(ref output, size - nullSize);
|
2018-08-13 20:13:01 -04:00
|
|
|
|
|
2018-12-04 15:23:37 -05:00
|
|
|
|
output = output.Concat(encoding.GetBytes("\0")).ToArray();
|
2018-08-13 20:13:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 15:23:37 -05:00
|
|
|
|
return output;
|
2018-08-13 20:13:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 15:23:37 -05:00
|
|
|
|
public static byte[] HexToBytes(string hexString)
|
2018-08-13 20:13:01 -04:00
|
|
|
|
{
|
2018-08-14 18:02:42 -04:00
|
|
|
|
//Ignore last charactor if HexLength % 2 != 0.
|
2018-12-04 15:23:37 -05:00
|
|
|
|
int bytesInHex = hexString.Length / 2;
|
2018-08-13 20:13:01 -04:00
|
|
|
|
|
2018-12-04 15:23:37 -05:00
|
|
|
|
byte[] output = new byte[bytesInHex];
|
2018-08-13 20:13:01 -04:00
|
|
|
|
|
2018-12-04 15:23:37 -05:00
|
|
|
|
for (int index = 0; index < bytesInHex; index++)
|
2018-08-13 20:13:01 -04:00
|
|
|
|
{
|
2018-12-04 15:23:37 -05:00
|
|
|
|
output[index] = byte.Parse(hexString.Substring(index * 2, 2), NumberStyles.HexNumber);
|
2018-08-13 20:13:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 15:23:37 -05:00
|
|
|
|
return output;
|
2018-08-13 20:13:01 -04:00
|
|
|
|
}
|
2018-11-18 14:37:41 -05:00
|
|
|
|
|
2018-12-04 15:23:37 -05:00
|
|
|
|
public static string ReadUtf8String(ServiceCtx context, int index = 0)
|
2018-11-18 14:37:41 -05:00
|
|
|
|
{
|
2018-12-04 15:23:37 -05:00
|
|
|
|
long position = context.Request.PtrBuff[index].Position;
|
|
|
|
|
long size = context.Request.PtrBuff[index].Size;
|
2018-11-18 14:37:41 -05:00
|
|
|
|
|
2018-12-04 15:23:37 -05:00
|
|
|
|
using (MemoryStream ms = new MemoryStream())
|
2018-11-18 14:37:41 -05:00
|
|
|
|
{
|
2018-12-04 15:23:37 -05:00
|
|
|
|
while (size-- > 0)
|
2018-11-18 14:37:41 -05:00
|
|
|
|
{
|
2018-12-04 15:23:37 -05:00
|
|
|
|
byte value = context.Memory.ReadByte(position++);
|
2018-11-18 14:37:41 -05:00
|
|
|
|
|
2018-12-04 15:23:37 -05:00
|
|
|
|
if (value == 0)
|
2018-11-18 14:37:41 -05:00
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 15:23:37 -05:00
|
|
|
|
ms.WriteByte(value);
|
2018-11-18 14:37:41 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 15:23:37 -05:00
|
|
|
|
return Encoding.UTF8.GetString(ms.ToArray());
|
2018-11-18 14:37:41 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-13 20:13:01 -04:00
|
|
|
|
}
|
|
|
|
|
}
|