You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace IllusionInjector.Utilities
  8. {
  9. public static class LoneFunctions
  10. {
  11. public static byte[] StringToByteArray(string hex)
  12. {
  13. int NumberChars = hex.Length;
  14. byte[] bytes = new byte[NumberChars / 2];
  15. for (int i = 0; i < NumberChars; i += 2)
  16. bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
  17. return bytes;
  18. }
  19. public static string ByteArrayToString(byte[] ba)
  20. {
  21. StringBuilder hex = new StringBuilder(ba.Length * 2);
  22. foreach (byte b in ba)
  23. hex.AppendFormat("{0:x2}", b);
  24. return hex.ToString();
  25. }
  26. // Copyright (c) 2008-2013 Hafthor Stefansson
  27. // Distributed under the MIT/X11 software license
  28. // Ref: http://www.opensource.org/licenses/mit-license.php.
  29. // From: https://stackoverflow.com/a/8808245/3117125
  30. public static unsafe bool UnsafeCompare(byte[] a1, byte[] a2)
  31. {
  32. if (a1 == a2) return true;
  33. if (a1 == null || a2 == null || a1.Length != a2.Length)
  34. return false;
  35. fixed (byte* p1 = a1, p2 = a2)
  36. {
  37. byte* x1 = p1, x2 = p2;
  38. int l = a1.Length;
  39. for (int i = 0; i < l / 8; i++, x1 += 8, x2 += 8)
  40. if (*((long*)x1) != *((long*)x2)) return false;
  41. if ((l & 4) != 0) { if (*((int*)x1) != *((int*)x2)) return false; x1 += 4; x2 += 4; }
  42. if ((l & 2) != 0) { if (*((short*)x1) != *((short*)x2)) return false; x1 += 2; x2 += 2; }
  43. if ((l & 1) != 0) if (*((byte*)x1) != *((byte*)x2)) return false;
  44. return true;
  45. }
  46. }
  47. public static string GetRelativePath(string filespec, string folder)
  48. {
  49. Uri pathUri = new Uri(filespec);
  50. // Folders must end in a slash
  51. if (!folder.EndsWith(Path.DirectorySeparatorChar.ToString()))
  52. {
  53. folder += Path.DirectorySeparatorChar;
  54. }
  55. Uri folderUri = new Uri(folder);
  56. return Uri.UnescapeDataString(folderUri.MakeRelativeUri(pathUri).ToString().Replace('/', Path.DirectorySeparatorChar));
  57. }
  58. }
  59. }