StringUtils.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 = 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 byte[] HexToBytes(string hexString)
  31. {
  32. // Ignore last character if HexLength % 2 != 0.
  33. int bytesInHex = hexString.Length / 2;
  34. byte[] output = new byte[bytesInHex];
  35. for (int index = 0; index < bytesInHex; index++)
  36. {
  37. output[index] = byte.Parse(hexString.Substring(index * 2, 2), NumberStyles.HexNumber);
  38. }
  39. return output;
  40. }
  41. public static string ReadUtf8String(ServiceCtx context, int index = 0)
  42. {
  43. long position = context.Request.PtrBuff[index].Position;
  44. long size = context.Request.PtrBuff[index].Size;
  45. using (MemoryStream ms = new MemoryStream())
  46. {
  47. while (size-- > 0)
  48. {
  49. byte value = context.Memory.Read<byte>((ulong)position++);
  50. if (value == 0)
  51. {
  52. break;
  53. }
  54. ms.WriteByte(value);
  55. }
  56. return Encoding.UTF8.GetString(ms.ToArray());
  57. }
  58. }
  59. public static U8Span ReadUtf8Span(ServiceCtx context, int index = 0)
  60. {
  61. ulong position = (ulong)context.Request.PtrBuff[index].Position;
  62. ulong size = (ulong)context.Request.PtrBuff[index].Size;
  63. ReadOnlySpan<byte> buffer = context.Memory.GetSpan(position, (int)size);
  64. return new U8Span(buffer);
  65. }
  66. public static string ReadUtf8StringSend(ServiceCtx context, int index = 0)
  67. {
  68. long position = context.Request.SendBuff[index].Position;
  69. long size = context.Request.SendBuff[index].Size;
  70. using (MemoryStream ms = new MemoryStream())
  71. {
  72. while (size-- > 0)
  73. {
  74. byte value = context.Memory.Read<byte>((ulong)position++);
  75. if (value == 0)
  76. {
  77. break;
  78. }
  79. ms.WriteByte(value);
  80. }
  81. return Encoding.UTF8.GetString(ms.ToArray());
  82. }
  83. }
  84. public static unsafe int CompareCStr(char* s1, char* s2)
  85. {
  86. int s1Index = 0;
  87. int s2Index = 0;
  88. while (s1[s1Index] != 0 && s2[s2Index] != 0 && s1[s1Index] == s2[s2Index])
  89. {
  90. s1Index += 1;
  91. s2Index += 1;
  92. }
  93. return s2[s2Index] - s1[s1Index];
  94. }
  95. public static unsafe int LengthCstr(char* s)
  96. {
  97. int i = 0;
  98. while (s[i] != '\0')
  99. {
  100. i++;
  101. }
  102. return i;
  103. }
  104. }
  105. }