StringUtils.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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(ServiceCtx context, int index = 0)
  48. {
  49. ulong position = context.Request.PtrBuff[index].Position;
  50. ulong size = context.Request.PtrBuff[index].Size;
  51. using (MemoryStream ms = new MemoryStream())
  52. {
  53. while (size-- > 0)
  54. {
  55. byte value = context.Memory.Read<byte>(position++);
  56. if (value == 0)
  57. {
  58. break;
  59. }
  60. ms.WriteByte(value);
  61. }
  62. return Encoding.UTF8.GetString(ms.ToArray());
  63. }
  64. }
  65. public static U8Span ReadUtf8Span(ServiceCtx context, int index = 0)
  66. {
  67. ulong position = context.Request.PtrBuff[index].Position;
  68. ulong size = context.Request.PtrBuff[index].Size;
  69. ReadOnlySpan<byte> buffer = context.Memory.GetSpan(position, (int)size);
  70. return new U8Span(buffer);
  71. }
  72. public static string ReadUtf8StringSend(ServiceCtx context, int index = 0)
  73. {
  74. ulong position = context.Request.SendBuff[index].Position;
  75. ulong size = context.Request.SendBuff[index].Size;
  76. using (MemoryStream ms = new MemoryStream())
  77. {
  78. while (size-- > 0)
  79. {
  80. byte value = context.Memory.Read<byte>(position++);
  81. if (value == 0)
  82. {
  83. break;
  84. }
  85. ms.WriteByte(value);
  86. }
  87. return Encoding.UTF8.GetString(ms.ToArray());
  88. }
  89. }
  90. public static int CompareCStr(ReadOnlySpan<char> s1, ReadOnlySpan<char> s2)
  91. {
  92. int s1Index = 0;
  93. int s2Index = 0;
  94. while (s1[s1Index] != 0 && s2[s2Index] != 0 && s1[s1Index] == s2[s2Index])
  95. {
  96. s1Index += 1;
  97. s2Index += 1;
  98. }
  99. return s2[s2Index] - s1[s1Index];
  100. }
  101. public static int LengthCstr(ReadOnlySpan<char> s)
  102. {
  103. int i = 0;
  104. while (s[i] != '\0')
  105. {
  106. i++;
  107. }
  108. return i;
  109. }
  110. }
  111. }