|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | + |
| 6 | +namespace Bitness |
| 7 | +{ |
| 8 | + class Program |
| 9 | + { |
| 10 | + |
| 11 | + /// <summary> |
| 12 | + /// Source: https://docs.microsoft.com/en-gb/windows/desktop/debug/pe-format |
| 13 | + /// </summary> |
| 14 | + private static Dictionary<Int32, string> MachineType = new Dictionary<Int32, string>() |
| 15 | + { |
| 16 | + {0x0000, "The contents of this field are assumed to be applicable to any machine type"}, |
| 17 | + {0x01d3, "Matsushita AM33"}, |
| 18 | + {0x8664, "x64"}, |
| 19 | + {0x01c0, "ARM little endian"}, |
| 20 | + {0xaa64, "ARM64 little endian"}, |
| 21 | + {0x01c4, "ARM Thumb-2 little endian"}, |
| 22 | + {0x0ebc, "EFI byte code"}, |
| 23 | + {0x014c, "Intel 386 or later processors and compatible processors"}, |
| 24 | + {0x0200, "Intel Itanium processor family"}, |
| 25 | + {0x9041, "Mitsubishi M32R little endian"}, |
| 26 | + {0x0266, "MIPS16"}, |
| 27 | + {0x0366, "MIPS with FPU"}, |
| 28 | + {0x0466, "MIPS16 with FPU"}, |
| 29 | + {0x01f0, "Power PC little endian"}, |
| 30 | + {0x01f1, "Power PC with floating point support"}, |
| 31 | + {0x0166, "MIPS little endian"}, |
| 32 | + {0x5032, "RISC-V 32-bit address space"}, |
| 33 | + {0x5064, "RISC-V 64-bit address space"}, |
| 34 | + {0x5128, "RISC-V 128-bit address space"}, |
| 35 | + {0x01a2, "Hitachi SH3"}, |
| 36 | + {0x01a3, "Hitachi SH3 DSP"}, |
| 37 | + {0x01a6, "Hitachi SH4"}, |
| 38 | + {0x01a8, "Hitachi SH5"}, |
| 39 | + {0x01c2, "Thumb"}, |
| 40 | + {0x0169, "MIPS little-endian WCE v2"} |
| 41 | + }; |
| 42 | + |
| 43 | + static void Main(string[] args) |
| 44 | + { |
| 45 | + // Initial checks. |
| 46 | + if (args.Length != 1) { Environment.Exit(0); } // Wrong args (probably none). Just get out of here. |
| 47 | + if (!File.Exists(args[0])) { Environment.Exit(2); } // File not found |
| 48 | + |
| 49 | + // File exists. Get PE offset from position 0x3c. FileShare.ReadWrite means we can read the file even if it's in use. |
| 50 | + var Stream = new FileStream(args[0], FileMode.Open, FileAccess.Read, FileShare.ReadWrite); |
| 51 | + byte[] PeOffset = new byte[2]; |
| 52 | + Stream.Seek(0x3c, SeekOrigin.Begin); |
| 53 | + Stream.Read(PeOffset, 0, 2); |
| 54 | + |
| 55 | + // Jump to offset specified in MS-DOS stub and read the PE signature. |
| 56 | + Stream.Seek(PeOffset[0] | PeOffset[1] << 8, SeekOrigin.Begin); |
| 57 | + byte[] Values = new byte[4]; |
| 58 | + Stream.Read(Values, 0, 4); |
| 59 | + |
| 60 | + // For sanity's sake, make sure we've got an actual PE signature - "P E NULL NULL"? |
| 61 | + if (Values.SequenceEqual(new byte[] { 0x50, 0x45, 0x00, 0x00 })) |
| 62 | + { |
| 63 | + // Machine type is the next 2 bytes after the signature. |
| 64 | + Stream.Read(Values, 0, 2); |
| 65 | + Int32 TypeId = Values[0] | Values[1] << 8; |
| 66 | + string TypeName; |
| 67 | + if (MachineType.TryGetValue(TypeId, out TypeName)) |
| 68 | + { |
| 69 | + Console.WriteLine("This assembly is compiled for {0}.", TypeName); |
| 70 | + } |
| 71 | + else |
| 72 | + { |
| 73 | + Console.WriteLine("Machine type could not be determined for this assembly."); |
| 74 | + } |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + Console.WriteLine("Machine type could not be determined for this assembly."); |
| 79 | + } |
| 80 | + Console.WriteLine("Press any key to exit."); |
| 81 | + Console.ReadKey(); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments