Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Program.cs - github.com/mono/ikdasm.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8d99b66cc7c7994c0b18bb3b9d6bd27ecdb9fbc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
  Copyright (C) 2012-2013 Jeroen Frijters

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

  Jeroen Frijters
  jeroen@frijters.net
  
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Mono.Options;

namespace Ildasm
{
    class Program
    {
        static int Main(string[] args)
        {
            string outputFile = null;
            string inputFile = null;
            MetadataTableIndex? tableToDump = null;
            var compatLevel = CompatLevel.None;
            var flags = Flags.None;

			if (typeof (int).Assembly.GetType ("Mono.Runtime") != null) {
				bool printUsage = false;

				var p = new OptionSet () {
						{ "help", v => printUsage = true },
						{ "out", v => outputFile = v },
						{ "assembly", v =>tableToDump = MetadataTableIndex.Assembly },
						{ "assemblyref", v =>tableToDump = MetadataTableIndex.AssemblyRef },
						{ "moduleref", v =>tableToDump = MetadataTableIndex.ModuleRef },
						{ "exported", v =>tableToDump = MetadataTableIndex.ExportedType },
						{ "customattr", v =>tableToDump = MetadataTableIndex.CustomAttribute },
						{ "module", v => tableToDump = MetadataTableIndex.Module },
						{ "document", v => tableToDump = MetadataTableIndex.Document },
						{ "methoddebuginformation", v => tableToDump = MetadataTableIndex.MethodDebugInformation },
						{ "localscope", v => tableToDump = MetadataTableIndex.LocalScope },
						{ "localvariable", v => tableToDump = MetadataTableIndex.LocalVariable },
						{ "localconstant", v => tableToDump = MetadataTableIndex.LocalConstant },
						{ "importscope", v =>tableToDump = MetadataTableIndex.ImportScope },
						{ "customdebuginfo", v => tableToDump = MetadataTableIndex.CustomDebugInformation },
					};
				args = p.Parse (args).ToArray ();
				if (printUsage) {
					PrintUsage ();
					return 0;
				}
				if (args.Length < 1) {
					PrintUsage ();
					return 1;
				}
				inputFile = args [0];
			} else {
				foreach (var arg in args)
				{
					if (arg.StartsWith("-", StringComparison.Ordinal) || arg.StartsWith("/", StringComparison.Ordinal))
					{
						string value;
						if (TryMatchOption(arg, "out", out value))
						{
							outputFile = value;
						}
						else if (TryMatchOption(arg, "compat", out value))
						{
							switch (value)
							{
                            case "2.0":
                                compatLevel = CompatLevel.V20;
                                break;
                            case "4.0":
                                compatLevel = CompatLevel.V40;
                                break;
                            case "4.5":
                                compatLevel = CompatLevel.V45;
                                break;
                            default:
                                PrintUsage();
                                return 1;
							}
						}
						else if (String.Compare(arg, 1, "diffmode", 0, 8, StringComparison.OrdinalIgnoreCase) == 0)
						{
							flags |= Flags.DiffMode;
						}
						else if (IsIldasmOption(arg, "caverbal"))
						{
							flags |= Flags.Caverbal;
						}
                        else if (IsIldasmOption(arg, "project"))
                        {
                            flags |= Flags.Project;
                        }
                    }
                    else
                    {
                        if (inputFile != null)
                        {
                            PrintUsage();
                            return 1;
                        }
                        else
                        {
                            inputFile = arg;
                        }
                    }
                }
            }

            if (inputFile == null)
            {
                PrintUsage();
                return 1;
            }
			if (tableToDump.HasValue)
			{
				var tableDumper = new TableDumper(inputFile);
				if (outputFile != null)
				{
					using (StreamWriter sw = new StreamWriter(outputFile, false))
					{
						tableDumper.DumpTable(sw, tableToDump.Value);
					}
				}
				else
				{
					tableDumper.DumpTable(Console.Out, tableToDump.Value);
				}
				return 0;
			}
            var disassembler = new Disassembler(inputFile, outputFile, compatLevel, flags);
            if (outputFile != null)
            {
                Encoding enc;
                switch (compatLevel)
                {
                    case CompatLevel.None:
                        enc = Encoding.UTF8;
                        break;
                    case CompatLevel.V20:
                    case CompatLevel.V40:
                        // instantiate new UTF8Encoding to avoid the preamble that Encoding.UTF8 has
                        // (note that the only non-ASCII character that we're encoding is the \uFFFD placeholder for non-ASCII characters)
                        enc = new UTF8Encoding();
                        break;
                    default:
                        enc = Console.OutputEncoding;
                        break;
                }
                using (StreamWriter sw = new StreamWriter(outputFile, false, enc))
                {
                    disassembler.Save(sw);
                }
            }
            else
            {
                disassembler.Save(Console.Out);
            }
			return 0;
        }

        static bool TryMatchOption(string arg, string key, out string value)
        {
            if (arg.Length > key.Length + 2 && (arg[key.Length + 1] == ':' || arg[key.Length + 1] == '=') && String.Compare(arg, 1, key, 0, key.Length, true) == 0)
            {
                value = arg.Substring(key.Length + 2);
                return true;
            }
            value = null;
            return false;
        }

        static bool IsIldasmOption(string arg, string option)
        {
            // we match ildasm options on the first three letters (like ildasm)
            return String.Compare(arg, 1, option, 0, 3, StringComparison.OrdinalIgnoreCase) == 0;
        }

        static void PrintUsage()
        {
            Console.WriteLine("IKDASM - IL disassembler example for IKVM.Reflection");
            Console.WriteLine("Copyright (C) 2012-2013 Jeroen Frijters");
            Console.WriteLine();
            Console.WriteLine("Usage: ikdasm [options] <file_name> [options]");
            Console.WriteLine();
            Console.WriteLine("Options:");
            if (typeof (int).Assembly.GetType ("Mono.Runtime") != null) {
                Console.WriteLine ("  -out=<file name>   Direct output to file rather than stdout");
                Console.WriteLine ("  -help              Print this help");

                Console.WriteLine ("  -assembly          Dumps the contents of the Assembly table");
                Console.WriteLine ("  -assemblyref       Dumps the contents of the AssemblyRef table");
                Console.WriteLine ("  -moduleref         Dumps the contents of the ModuleRef table");
                Console.WriteLine ("  -exported          Dumps the contents of the ExportedType table");
                Console.WriteLine ("  -customattr        Dumps the contents of the CustomAttribute table");
            } else {
                Console.WriteLine("  /OUT=<file name>    Direct output to file rather than to stdout.");
				Console.WriteLine("  /COMPAT=<version>   Match ildasm behavior. (<version> = 2.0 | 4.0 | 4.5)");
				Console.WriteLine("  /DIFFMODE           Remove superficial differences to allow assembly comparisons");
				Console.WriteLine("  /CAVERBAL           Try to decode custom attribute blobs");
                Console.WriteLine("  /PROJECT            Project WinMD metadata into .NET format");
			}
        }
    }
}