StringUtils.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using Ryujinx.HLE.HOS;
  2. using System;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. namespace Ryujinx.HLE.Utilities
  8. {
  9. static class StringUtils
  10. {
  11. public static byte[] GetFixedLengthBytes(string inputString, int size, Encoding encoding)
  12. {
  13. inputString = inputString + "\0";
  14. int bytesCount = encoding.GetByteCount(inputString);
  15. byte[] output = new byte[size];
  16. if (bytesCount < size)
  17. {
  18. encoding.GetBytes(inputString, 0, inputString.Length, output, 0);
  19. }
  20. else
  21. {
  22. int nullSize = encoding.GetByteCount("\0");
  23. output = encoding.GetBytes(inputString);
  24. Array.Resize(ref output, size - nullSize);
  25. output = output.Concat(encoding.GetBytes("\0")).ToArray();
  26. }
  27. return output;
  28. }
  29. public static byte[] HexToBytes(string hexString)
  30. {
  31. // Ignore last character if HexLength % 2 != 0.
  32. int bytesInHex = hexString.Length / 2;
  33. byte[] output = new byte[bytesInHex];
  34. for (int index = 0; index < bytesInHex; index++)
  35. {
  36. output[index] = byte.Parse(hexString.Substring(index * 2, 2), NumberStyles.HexNumber);
  37. }
  38. return output;
  39. }
  40. public static string ReadUtf8String(ServiceCtx context, int index = 0)
  41. {
  42. long position = context.Request.PtrBuff[index].Position;
  43. long size = context.Request.PtrBuff[index].Size;
  44. using (MemoryStream ms = new MemoryStream())
  45. {
  46. while (size-- > 0)
  47. {
  48. byte value = context.Memory.ReadByte(position++);
  49. if (value == 0)
  50. {
  51. break;
  52. }
  53. ms.WriteByte(value);
  54. }
  55. return Encoding.UTF8.GetString(ms.ToArray());
  56. }
  57. }
  58. public static string ReadUtf8StringSend(ServiceCtx context, int index = 0)
  59. {
  60. long position = context.Request.SendBuff[index].Position;
  61. long size = context.Request.SendBuff[index].Size;
  62. using (MemoryStream ms = new MemoryStream())
  63. {
  64. while (size-- > 0)
  65. {
  66. byte value = context.Memory.ReadByte(position++);
  67. if (value == 0)
  68. {
  69. break;
  70. }
  71. ms.WriteByte(value);
  72. }
  73. return Encoding.UTF8.GetString(ms.ToArray());
  74. }
  75. }
  76. public static unsafe int CompareCStr(char* s1, char* s2)
  77. {
  78. int s1Index = 0;
  79. int s2Index = 0;
  80. while (s1[s1Index] != 0 && s2[s2Index] != 0 && s1[s1Index] == s2[s2Index])
  81. {
  82. s1Index += 1;
  83. s2Index += 1;
  84. }
  85. return s2[s2Index] - s1[s1Index];
  86. }
  87. public static unsafe int LengthCstr(char* s)
  88. {
  89. int i = 0;
  90. while (s[i] != '\0')
  91. {
  92. i++;
  93. }
  94. return i;
  95. }
  96. }
  97. }