|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Text; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Reflection; |
| 7 | +using System.Reflection.Metadata; |
| 8 | +using System.Reflection.PortableExecutable; |
| 9 | + |
| 10 | +namespace AssemblyChecker |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// This is a simple console application that is designed to answer True or False |
| 14 | + /// questions about whether a given file is a managed assembly or not. |
| 15 | + /// You can also ask whether or not the assembly is debuggable. |
| 16 | + /// Return code of 0 indicates the file is a managed assembly. |
| 17 | + /// Return code of 1 indicates the file is not a managed assembly. No errors will be printed for this one. |
| 18 | + /// </summary> |
| 19 | + public class Program |
| 20 | + { |
| 21 | + private const string HelpText = @" |
| 22 | +Usage: |
| 23 | + <filePath>: Check if the file-path is a managed assembly. |
| 24 | + --is-debug <filePath>: Check if the file-path is a managed assembly that is built with debuggability. |
| 25 | + --is-exe <filePath>: Check if the file-path is a managed assembly that is an executable. |
| 26 | +"; |
| 27 | + |
| 28 | + static bool IsAssembly(string path) |
| 29 | + { |
| 30 | + using var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); |
| 31 | + |
| 32 | + // Try to read CLI metadata from the PE file. |
| 33 | + using var peReader = new PEReader(fs); |
| 34 | + |
| 35 | + if (!peReader.HasMetadata) |
| 36 | + { |
| 37 | + return false; // File does not have CLI metadata. |
| 38 | + } |
| 39 | + |
| 40 | + // Check that file has an assembly manifest. |
| 41 | + MetadataReader reader = peReader.GetMetadataReader(); |
| 42 | + return reader.IsAssembly; |
| 43 | + } |
| 44 | + |
| 45 | + static bool IsDebug(string path) |
| 46 | + { |
| 47 | + return |
| 48 | + Assembly.LoadFrom(path) |
| 49 | + .GetCustomAttributes(typeof(DebuggableAttribute), false) |
| 50 | + .OfType<DebuggableAttribute>().Any(x => x.IsJITOptimizerDisabled); |
| 51 | + } |
| 52 | + |
| 53 | + static bool IsExe(string path) |
| 54 | + { |
| 55 | + using var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); |
| 56 | + |
| 57 | + // Try to read CLI metadata from the PE file. |
| 58 | + using var peReader = new PEReader(fs); |
| 59 | + |
| 60 | + if (!peReader.HasMetadata) |
| 61 | + { |
| 62 | + return false; // File does not have CLI metadata. |
| 63 | + } |
| 64 | + |
| 65 | + return peReader.PEHeaders.IsExe; |
| 66 | + } |
| 67 | + |
| 68 | + static int Main(string[] args) |
| 69 | + { |
| 70 | + if (args.Length == 0) |
| 71 | + { |
| 72 | + Console.WriteLine(HelpText); |
| 73 | + Console.Error.WriteLine("\nExpected assembly file-path."); |
| 74 | + return 2; |
| 75 | + } |
| 76 | + |
| 77 | + // Help |
| 78 | + if (args.Contains("-h")) |
| 79 | + { |
| 80 | + Console.WriteLine(HelpText); |
| 81 | + return 0; |
| 82 | + } |
| 83 | + |
| 84 | + if (args.Length == 1) |
| 85 | + { |
| 86 | + return IsAssembly(args[0]) ? 0 : 1; |
| 87 | + } |
| 88 | + |
| 89 | + if (args.Length == 2) |
| 90 | + { |
| 91 | + switch (args[0]) |
| 92 | + { |
| 93 | + case "--is-debug": |
| 94 | + { |
| 95 | + return IsDebug(args[1]) ? 0 : 1; |
| 96 | + } |
| 97 | + |
| 98 | + case "--is-exe": |
| 99 | + { |
| 100 | + return IsExe(args[1]) ? 0 : 1; |
| 101 | + } |
| 102 | + |
| 103 | + default: |
| 104 | + { |
| 105 | + Console.WriteLine(HelpText); |
| 106 | + Console.Error.WriteLine("\nInvalid option."); |
| 107 | + return 2; |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + Console.WriteLine(HelpText); |
| 113 | + Console.Error.WriteLine("\nToo many arguments."); |
| 114 | + return 2; |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments