aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/Utilities/StringUtils.cs
blob: a10273eef7a8a979f295001848bccdee51679fed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using Ryujinx.HLE.HOS;
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;

namespace Ryujinx.HLE.Utilities
{
    static class StringUtils
    {
        public static byte[] GetFixedLengthBytes(string InputString, int Size, Encoding Encoding)
        {
            InputString = InputString + "\0";

            int BytesCount = Encoding.GetByteCount(InputString);

            byte[] Output = new byte[Size];

            if (BytesCount < Size)
            {
                Encoding.GetBytes(InputString, 0, InputString.Length, Output, 0);
            }
            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)
        {
            //Ignore last charactor if HexLength % 2 != 0.
            int BytesInHex = HexString.Length / 2;

            byte[] Output = new byte[BytesInHex];

            for (int Index = 0; Index < BytesInHex; Index++)
            {
                Output[Index] = byte.Parse(HexString.Substring(Index * 2, 2), NumberStyles.HexNumber);
            }

            return Output;
        }

        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());
            }
        }
    }
}