Repository navigation
Expand file tree
/
Copy pathInputFileLoader.cs
More file actions
257 lines (239 loc) · 9.22 KB
/
Copy pathInputFileLoader.cs
File metadata and controls
257 lines (239 loc) · 9.22 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright (c) 2026 Siegfried Pammer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Text;
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.ILSpyX;
using ICSharpCode.ILSpyX.FileLoaders;
namespace ICSharpCode.ILSpyCmd
{
/// <summary>
/// Thrown when the input file is a package (a single-file bundle, an archive) and the entry
/// to work on was either not named or names nothing in the package. The message is the
/// complete text to print, including the entries to choose from.
/// </summary>
public sealed class PackageEntryRequiredException : Exception
{
public PackageEntryRequiredException(string message, int exitCode)
: base(message)
{
this.ExitCode = exitCode;
}
public int ExitCode { get; }
}
/// <summary>
/// Turns an input file name into the module to work on. Every mode of the tool loads its
/// input through here, so a package is recognized the same way in all of them.
/// </summary>
static class InputFileLoader
{
static readonly FileLoaderRegistry loaders = new FileLoaderRegistry();
/// <summary>
/// Loads <paramref name="fileName"/>, or the entry named by <paramref name="entryName"/>
/// if the file turns out to be a package.
/// </summary>
/// <exception cref="PackageEntryRequiredException">
/// The file is a package and <paramref name="entryName"/> is null or unknown.
/// </exception>
public static PEFile Load(string fileName, string entryName, bool applyWinRTProjections = true)
{
var file = LoadMetadata(fileName, entryName, applyWinRTProjections);
if (file is PEFile module)
{
return module;
}
file.Dispose();
throw new BadImageFormatException($"'{fileName}' contains metadata, but no PE image.");
}
/// <summary>
/// Like <see cref="Load"/>, but also accepts a file that carries metadata without a PE
/// image around it: a standalone Portable PDB, or a raw metadata blob.
/// </summary>
public static MetadataFile LoadMetadata(string fileName, string entryName, bool applyWinRTProjections = true)
{
var context = new FileLoadContext(applyWinRTProjections, null);
var result = LoadFile(fileName, context);
if (result?.Package is { } package)
{
return LoadPackageEntry(package, fileName, entryName, context);
}
if (result?.MetadataFile is { } file)
{
return file;
}
// Not a file any loader recognized: let PEFile report what is wrong with it, which is
// the error the tool has always produced for such input.
return new PEFile(fileName, metadataOptions: MetadataOptions(context));
}
/// <summary>
/// Loads the selected package entry when <paramref name="fileName"/> is a package, and
/// returns false for ordinary files so callers can keep path-based behavior.
/// </summary>
public static bool TryLoadPackageEntry(string fileName, string entryName, bool applyWinRTProjections, out PEFile module)
{
var context = new FileLoadContext(applyWinRTProjections, null);
var result = LoadFile(fileName, context);
if (result?.Package is { } package)
{
module = LoadPackageEntry(package, fileName, entryName, context);
return true;
}
module = null;
return false;
}
static LoadResult LoadFile(string fileName, FileLoadContext context)
{
using var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
foreach (var loader in loaders.RegisteredLoaders)
{
stream.Position = 0;
LoadResult result;
try
{
// The loaders are synchronous in fact; only their signature is not.
result = loader.Load(fileName, stream, context).GetAwaiter().GetResult();
}
catch (Exception)
{
// A loader that chokes on a file it does not own says nothing about the file.
// If no other loader claims it either, the caller reloads it as a PE file and
// reports the failure from there.
continue;
}
if (result?.IsSuccess == true)
return result;
}
return null;
}
static PEFile LoadPackageEntry(LoadedPackage package, string packageFileName, string entryName, FileLoadContext context)
{
var managedEntries = GetManagedEntries(package);
string kind = package.Kind == LoadedPackage.PackageKind.Bundle ? "single-file bundle" : "package";
if (string.IsNullOrEmpty(entryName))
{
throw new PackageEntryRequiredException(
BuildListing($"error: {kind}; name an entry with --bundle-entry <name>.", package, managedEntries),
ProgramExitCodes.EX_USAGE);
}
var entry = managedEntries.FirstOrDefault(e => string.Equals(entryName, e.Name, StringComparison.OrdinalIgnoreCase));
if (entry == null)
{
throw new PackageEntryRequiredException(
BuildListing($"error: '{entryName}' is not a managed entry of this {kind}.", package, managedEntries),
ProgramExitCodes.EX_DATAERR);
}
using var stream = entry.TryOpenStream();
if (stream == null)
{
throw new PackageEntryRequiredException(
$"error: entry '{entry.Name}' could not be read from the {kind}.",
ProgramExitCodes.EX_DATAERR);
}
stream.Position = 0;
return new PEFile(LoadedPackage.GetEntryFileName(packageFileName, entry.Name), stream,
PEStreamOptions.PrefetchEntireImage | PEStreamOptions.LeaveOpen,
MetadataOptions(context));
}
/// <summary>
/// The entries worth decompiling. A bundle manifest states the type of each entry; an
/// archive - and a bundle whose entries are all typed as unknown - is filtered by
/// extension instead.
/// </summary>
static IReadOnlyList<PackageEntry> GetManagedEntries(LoadedPackage package)
{
var assemblyNames = GetAssemblyEntryNames(package);
if (assemblyNames.Count > 0)
{
return package.Entries.Where(e => assemblyNames.Contains(e.Name)).ToList();
}
return package.Entries
.Where(e => e.Name.EndsWith(".dll", StringComparison.OrdinalIgnoreCase)
|| e.Name.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
.ToList();
}
static HashSet<string> GetAssemblyEntryNames(LoadedPackage package)
{
var names = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var entries = package.BundleHeader.Entries;
if (!entries.IsDefaultOrEmpty)
{
foreach (var entry in entries)
{
if (entry.Type == SingleFileBundle.FileType.Assembly)
names.Add(entry.RelativePath);
}
}
return names;
}
/// <summary>
/// The name of the application assembly, or null if it cannot be determined. The manifest
/// carries no entry-point marker, so it is derived the way the host does it: from the
/// runtime-config entry, whose name is the application name plus ".runtimeconfig.json".
/// </summary>
static string FindEntryPoint(LoadedPackage package, IReadOnlyList<PackageEntry> managedEntries)
{
const string suffix = ".runtimeconfig.json";
var entries = package.BundleHeader.Entries;
if (entries.IsDefaultOrEmpty)
return null;
foreach (var entry in entries)
{
if (entry.Type != SingleFileBundle.FileType.RuntimeConfigJson)
continue;
if (!entry.RelativePath.EndsWith(suffix, StringComparison.OrdinalIgnoreCase))
continue;
string candidate = entry.RelativePath[..^suffix.Length] + ".dll";
var match = managedEntries.FirstOrDefault(e => string.Equals(candidate, e.Name, StringComparison.OrdinalIgnoreCase));
if (match != null)
return match.Name;
}
return null;
}
static string BuildListing(string message, LoadedPackage package, IReadOnlyList<PackageEntry> managedEntries)
{
var text = new StringBuilder(message);
if (managedEntries.Count == 0)
{
text.Append(" It contains no managed entries.");
return text.ToString();
}
string entryPoint = FindEntryPoint(package, managedEntries);
text.AppendLine(" Managed entries:");
foreach (var entry in managedEntries)
{
text.Append(" ").Append(entry.Name);
if (entry.Name == entryPoint)
text.Append(" (entry point)");
text.AppendLine();
}
return text.ToString().TrimEnd();
}
static MetadataReaderOptions MetadataOptions(FileLoadContext context)
{
return context.ApplyWinRTProjections
? MetadataReaderOptions.ApplyWindowsRuntimeProjections
: MetadataReaderOptions.None;
}
}
}