StringUtils.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using LibHac.Common;
  2. using Ryujinx.HLE.HOS;
  3. using System;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. namespace Ryujinx.HLE.Utilities
  9. {
  10. static class StringUtils
  11. {
  12. public static byte[] GetFixedLengthBytes(string inputString, int size, Encoding encoding)
  13. {
  14. inputString += "\0";
  15. int bytesCount = encoding.GetByteCount(inputString);
  16. byte[] output = new byte[size];
  17. if (bytesCount < size)
  18. {
  19. encoding.GetBytes(inputString, 0, inputString.Length, output, 0);
  20. }
  21. else
  22. {
  23. int nullSize = encoding.GetByteCount("\0");
  24. output = encoding.GetBytes(inputString);
  25. Array.Resize(ref output, size - nullSize);
  26. output = output.Concat(encoding.GetBytes("\0")).ToArray();
  27. }
  28. return output;
  29. }
  30. public static string ReadInlinedAsciiString(BinaryReader reader, int maxSize)
  31. {
  32. byte[] data = reader.ReadBytes(maxSize);
  33. int stringSize = Array.IndexOf<byte>(data, 0);
  34. return Encoding.ASCII.GetString(data, 0, stringSize < 0 ? maxSize : stringSize);
  35. }
  36. public static byte[] HexToBytes(string hexString)
  37. {
  38. // Ignore last character if HexLength % 2 != 0.
  39. int bytesInHex = hexString.Length / 2;
  40. byte[] output = new byte[bytesInHex];
  41. for (int index = 0; index < bytesInHex; index++)
  42. {
  43. output[index] = byte.Parse(hexString.Substring(index * 2, 2), NumberStyles.HexNumber);
  44. }
  45. return output;
  46. }
  47. public static string ReadUtf8String(ReadOnlySpan<byte> data, out int dataRead)
  48. {
  49. dataRead = data.IndexOf((byte)0) + 1;
  50. if (dataRead <= 1)
  51. {
  52. return string.Empty;
  53. }
  54. return Encoding.UTF8.GetString(data[..dataRead]);
  55. }
  56. public static string ReadUtf8String(ServiceCtx context, int index = 0)
  57. {
  58. ulong position = context.Request.PtrBuff[index].Position;
  59. ulong size = context.Request.PtrBuff[index].Size;
  60. using (MemoryStream ms = new MemoryStream())
  61. {
  62. while (size-- > 0)
  63. {
  64. byte value = context.Memory.Read<byte>(position++);
  65. if (value == 0)
  66. {
  67. break;
  68. }
  69. ms.WriteByte(value);
  70. }
  71. return Encoding.UTF8.GetString(ms.ToArray());
  72. }
  73. }
  74. public static U8Span ReadUtf8Span(ServiceCtx context, int index = 0)
  75. {
  76. ulong position = context.Request.PtrBuff[index].Position;
  77. ulong size = context.Request.PtrBuff[index].Size;
  78. ReadOnlySpan<byte> buffer = context.Memory.GetSpan(position, (int)size);
  79. return new U8Span(buffer);
  80. }
  81. public static string ReadUtf8StringSend(ServiceCtx context, int index = 0)
  82. {
  83. ulong position = context.Request.SendBuff[index].Position;
  84. ulong size = context.Request.SendBuff[index].Size;
  85. using (MemoryStream ms = new MemoryStream())
  86. {
  87. while (size-- > 0)
  88. {
  89. byte value = context.Memory.Read<byte>(position++);
  90. if (value == 0)
  91. {
  92. break;
  93. }
  94. ms.WriteByte(value);
  95. }
  96. return Encoding.UTF8.GetString(ms.ToArray());
  97. }
  98. }
  99. public static int CompareCStr(ReadOnlySpan<char> s1, ReadOnlySpan<char> s2)
  100. {
  101. int s1Index = 0;
  102. int s2Index = 0;
  103. while (s1[s1Index] != 0 && s2[s2Index] != 0 && s1[s1Index] == s2[s2Index])
  104. {
  105. s1Index += 1;
  106. s2Index += 1;
  107. }
  108. return s2[s2Index] - s1[s1Index];
  109. }
  110. public static int LengthCstr(ReadOnlySpan<char> s)
  111. {
  112. int i = 0;
  113. while (s[i] != '\0')
  114. {
  115. i++;
  116. }
  117. return i;
  118. }
  119. }
  120. }