ELFFileCheck.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. namespace NativeLibraryLoader.Linux
  7. {
  8. internal class ELFFileCheck : IFileCheck
  9. {
  10. public MachineType CheckFile(string path)
  11. {
  12. MachineType machineType = MachineType.NoSupport;
  13. var stream = System.IO.File.OpenRead(path);
  14. Byte[] bytes = new Byte[] { 0x7f, 0x45, 0x4c, 0x46 };
  15. if (bytes.All(x => stream.ReadByte() == x))
  16. {
  17. switch (stream.ReadByte())
  18. {
  19. case 1:
  20. machineType = MachineType.Bit32;
  21. break;
  22. case 2:
  23. machineType = MachineType.Bit64;
  24. break;
  25. default:
  26. machineType = MachineType.NoSupport;
  27. break;
  28. }
  29. }
  30. stream.Close();
  31. stream.Dispose();
  32. return machineType;
  33. }
  34. }
  35. }