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.

42 lines
1.6 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace IllusionInjector.Utilities
  7. {
  8. public static class LoneFunctions
  9. {
  10. public static byte[] StringToByteArray(string hex)
  11. {
  12. int NumberChars = hex.Length;
  13. byte[] bytes = new byte[NumberChars / 2];
  14. for (int i = 0; i < NumberChars; i += 2)
  15. bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
  16. return bytes;
  17. }
  18. // Copyright (c) 2008-2013 Hafthor Stefansson
  19. // Distributed under the MIT/X11 software license
  20. // Ref: http://www.opensource.org/licenses/mit-license.php.
  21. // From: https://stackoverflow.com/a/8808245/3117125
  22. public static unsafe bool UnsafeCompare(byte[] a1, byte[] a2)
  23. {
  24. if (a1 == a2) return true;
  25. if (a1 == null || a2 == null || a1.Length != a2.Length)
  26. return false;
  27. fixed (byte* p1 = a1, p2 = a2)
  28. {
  29. byte* x1 = p1, x2 = p2;
  30. int l = a1.Length;
  31. for (int i = 0; i < l / 8; i++, x1 += 8, x2 += 8)
  32. if (*((long*)x1) != *((long*)x2)) return false;
  33. if ((l & 4) != 0) { if (*((int*)x1) != *((int*)x2)) return false; x1 += 4; x2 += 4; }
  34. if ((l & 2) != 0) { if (*((short*)x1) != *((short*)x2)) return false; x1 += 2; x2 += 2; }
  35. if ((l & 1) != 0) if (*((byte*)x1) != *((byte*)x2)) return false;
  36. return true;
  37. }
  38. }
  39. }
  40. }