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

nuget-hash-extractor.cs « nuget-hash-extractor « tools - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 310abd2714a90ac23d50c3df53bbcc4ecdb36420 (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
using System;
using System.Xml.Linq;
using System.Linq;
using System.IO;
using System.IO.Compression;
using System.Reflection;

class Driver {
	static ZipArchiveEntry FindSpecFile (ZipArchive zip) {
		foreach (var entry in zip.Entries) {
			if (entry.Name.EndsWith (".nuspec"))
				return entry;
		}
		throw new Exception ("Could not find nuspec file");
	}

	static void DumpNuget (string nupkg) {
		var zip = new ZipArchive(new FileStream (nupkg, FileMode.Open));

		var nuspec = FindSpecFile (zip);
		var l = XElement.Load (new StreamReader (nuspec.Open ()));
		var version = (from el in l.Descendants() where el.Name.LocalName == "version" select el.Value).FirstOrDefault ();

		foreach (var et in from e in zip.Entries where (e.FullName.StartsWith ("lib/net4") || e.FullName.StartsWith ("lib/netcore")) && e.Name.EndsWith (".dll") select e) {
			LoadAndDump (et, version);
		}
	}
	static void Main (string[] args) {
		foreach (var f in Directory.GetFiles (args [0], "*.nupkg")) {
			DumpNuget (f);
		}
	}

	static byte[] StreamToArray (Stream s) {
		using(var ms = new MemoryStream ()) {
			s.CopyTo (ms);
			return ms.ToArray ();
		}
	}

	static int domain_id = 1;
	static void LoadAndDump (ZipArchiveEntry entry, string version) {
		// Console.WriteLine ("Dumping {0}", entry);
		var data = StreamToArray (entry.Open ());
		AppDomain ad = AppDomain.CreateDomain ("parse_" + ++domain_id);
		DoParse p = (DoParse)ad.CreateInstanceAndUnwrap (typeof (DoParse).Assembly.FullName, typeof (DoParse).FullName);
		p.ParseAssembly (data, version, entry.Name, entry.FullName);
		AppDomain.Unload (ad);
	}
}

class DoParse : MarshalByRefObject {
	static int Hash (string str) {
		int h = 5381;
        for (int i = 0;  i < str.Length; ++i)
            h = ((h << 5) + h) ^ str[i];
		return h;
	}
	static string FileToEnum (string name) {
		switch (name) {
		case "System.Runtime.InteropServices.RuntimeInformation.dll": return "SYS_RT_INTEROP_RUNTIME_INFO";
		case "System.Globalization.Extensions.dll": return "SYS_GLOBALIZATION_EXT";
		case "System.IO.Compression.dll": return "SYS_IO_COMPRESSION";
		case "System.Net.Http.dll": return "SYS_NET_HTTP";
		case "System.Text.Encoding.CodePages.dll": return "SYS_TEXT_ENC_CODEPAGES";
		default: throw new Exception ($"No idea what to do with {name}");
		}
	}

	static string FileToMoniker (string p) {
		var parts = p.Split (Path.DirectorySeparatorChar);
		return parts[parts.Length - 2];
	}

	public void ParseAssembly (byte[] data, string version, string name, string fullname) {
		var a = Assembly.ReflectionOnlyLoad (data);
		var m = a.GetModules ()[0];
		var id = m.ModuleVersionId.ToString ().ToUpper ();
		var hash_code = Hash (id).ToString ("X");
		var str = FileToEnum (name);

		string ver_str = version + " " + FileToMoniker (fullname);	
		Console.WriteLine ($"IGNORED_ASSEMBLY (0x{hash_code}, {str}, \"{id}\", \"{ver_str}\"),");
	}
}