5 using System.IO.Compression;
6 using System.Reflection;
9 static ZipArchiveEntry FindSpecFile (ZipArchive zip) {
10 foreach (var entry in zip.Entries) {
11 if (entry.Name.EndsWith (".nuspec"))
14 throw new Exception ("Could not find nuspec file");
17 static void DumpNuget (string nupkg) {
18 var zip = new ZipArchive(new FileStream (nupkg, FileMode.Open));
20 var nuspec = FindSpecFile (zip);
21 var l = XElement.Load (new StreamReader (nuspec.Open ()));
22 var version = (from el in l.Descendants() where el.Name.LocalName == "version" select el.Value).FirstOrDefault ();
25 // Logic copied from https://github.com/NuGet/NuGet.Client/blob/4cccb13833ad29d6a0bcff055460d964f1b49cfe/src/NuGet.Core/NuGet.Frameworks/DefaultFrameworkMappings.cs#L385
27 var entries = from e in zip.Entries where e.FullName.StartsWith ("lib/net4") && e.Name.EndsWith (".dll") select e;
28 if (!entries.Any ()) {
29 entries = from e in zip.Entries where e.FullName.StartsWith ("lib/netstandard1") && e.Name.EndsWith (".dll") select e;
32 foreach (var et in entries) {
33 LoadAndDump (et, version);
36 static void Main (string[] args) {
37 foreach (var f in Directory.GetFiles (args [0], "*.nupkg")) {
42 static byte[] StreamToArray (Stream s) {
43 using(var ms = new MemoryStream ()) {
49 static int domain_id = 1;
50 static void LoadAndDump (ZipArchiveEntry entry, string version) {
51 // Console.WriteLine ("Dumping {0}", entry);
52 var data = StreamToArray (entry.Open ());
53 AppDomain ad = AppDomain.CreateDomain ("parse_" + ++domain_id);
54 DoParse p = (DoParse)ad.CreateInstanceAndUnwrap (typeof (DoParse).Assembly.FullName, typeof (DoParse).FullName);
55 p.ParseAssembly (data, version, entry.Name, entry.FullName);
56 AppDomain.Unload (ad);
60 class DoParse : MarshalByRefObject {
61 static int Hash (string str) {
63 for (int i = 0; i < str.Length; ++i)
64 h = ((h << 5) + h) ^ str[i];
67 static string FileToEnum (string name) {
69 case "System.Runtime.InteropServices.RuntimeInformation.dll": return "SYS_RT_INTEROP_RUNTIME_INFO";
70 case "System.Globalization.Extensions.dll": return "SYS_GLOBALIZATION_EXT";
71 case "System.IO.Compression.dll": return "SYS_IO_COMPRESSION";
72 case "System.Net.Http.dll": return "SYS_NET_HTTP";
73 case "System.Text.Encoding.CodePages.dll": return "SYS_TEXT_ENC_CODEPAGES";
74 case "System.Reflection.DispatchProxy.dll": return "SYS_REF_DISP_PROXY";
75 default: throw new Exception ($"No idea what to do with {name}");
79 static string FileToMoniker (string p) {
80 var parts = p.Split (Path.DirectorySeparatorChar);
81 return parts[parts.Length - 2];
84 public void ParseAssembly (byte[] data, string version, string name, string fullname) {
85 var a = Assembly.ReflectionOnlyLoad (data);
86 var m = a.GetModules ()[0];
87 var id = m.ModuleVersionId.ToString ().ToUpper ();
88 var hash_code = Hash (id).ToString ("X");
89 var str = FileToEnum (name);
91 string ver_str = version + " " + FileToMoniker (fullname);
92 Console.WriteLine ($"IGNORED_ASSEMBLY (0x{hash_code}, {str}, \"{id}\", \"{ver_str}\"),");