StringUtils.cs 4.4 KB

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