MachOFileCheck.cs 995 B

1234567891011121314151617181920212223242526272829303132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace NativeLibraryLoader.MAC
  5. {
  6. internal class MachOFileCheck : IFileCheck
  7. {
  8. private Dictionary<uint, bool> magicToMachOType = new Dictionary<uint, bool>()
  9. {
  10. { 0xFEEDFACE, false },
  11. { 0xFEEDFACF, true },
  12. { 0xCEFAEDFE, false },
  13. { 0xCFFEEDFE, true },
  14. };
  15. public MachineType CheckFile(string path)
  16. {
  17. MachineType machineType = MachineType.NoSupport;
  18. var stream = new System.IO.BinaryReader(System.IO.File.OpenRead(path));
  19. if (magicToMachOType.TryGetValue(stream.ReadUInt32(), out var val))
  20. {
  21. if (val) machineType = MachineType.Bit64;
  22. else machineType = MachineType.Bit32;
  23. }
  24. else machineType = MachineType.NoSupport;
  25. stream.Close();
  26. stream.Dispose();
  27. return machineType;
  28. }
  29. }
  30. }