Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

337 lines
11 KiB

  1. using System;
  2. using System.Text;
  3. using System.Net;
  4. using System.IO;
  5. using System.Text.RegularExpressions;
  6. using System.Runtime.Remoting;
  7. using System.Globalization;
  8. using System.Security;
  9. using System.Security.Policy;
  10. using System.Security.Permissions;
  11. using System.Collections;
  12. using System.Runtime.InteropServices;
  13. using System.Reflection;
  14. using System.Configuration.Assemblies;
  15. using System.Threading;
  16. using System.Xml;
  17. using System.Xml.XPath;
  18. namespace Microsoft.Fusion.ADF
  19. {
  20. public interface IAssemblyManifestImport
  21. {
  22. AssemblyIdentity GetAssemblyIdentity();
  23. DependentFileInfo GetNextDependentFileInfo();
  24. DependentAssemblyInfo GetNextDependentAssemblyInfo();
  25. void ResetIterators();
  26. FileType GetFileType();
  27. }
  28. //----------------------------------------------------------
  29. // ApplicationManifestImport
  30. //----------------------------------------------------------
  31. public class ApplicationManifestImport : IAssemblyManifestImport
  32. {
  33. XPathDocument _xPathDocument;
  34. XPathNodeIterator _xPathFileIterator;
  35. XPathNodeIterator _xPathAssemblyIterator;
  36. AssemblyIdentity _assemblyIdentity;
  37. Uri _manifestUri;
  38. //----------------------------------------------------------
  39. // ctor
  40. //----------------------------------------------------------
  41. public ApplicationManifestImport(Uri manifestUri)
  42. {
  43. _manifestUri = manifestUri;
  44. _xPathDocument = new XPathDocument(_manifestUri.ToString());
  45. _assemblyIdentity = GetAssemblyIdentity();
  46. _xPathFileIterator = null;
  47. _xPathAssemblyIterator = null;
  48. }
  49. //----------------------------------------------------------
  50. // GetActivationInfo
  51. //----------------------------------------------------------
  52. public ActivationInfo GetActivationInfo()
  53. {
  54. XPathNavigator xPathNavigator = _xPathDocument.CreateNavigator();
  55. XPathNodeIterator xPathNodeIterator = xPathNavigator.Select ("/assembly/application/activation");
  56. if (xPathNodeIterator.MoveNext() == false)
  57. return null;
  58. ActivationInfo ai = new ActivationInfo();
  59. string assemblyName = xPathNodeIterator.Current.GetAttribute("assemblyName", "");
  60. string assemblyClass = xPathNodeIterator.Current.GetAttribute("assemblyClass", "");
  61. string assemblyMethod = xPathNodeIterator.Current.GetAttribute("assemblyMethod", "");
  62. string assemblyMethodArgs = xPathNodeIterator.Current.GetAttribute("assemblyMethodArgs", "");
  63. ai["assemblyName"] = assemblyName;
  64. ai["assemblyClass"] = assemblyClass;
  65. ai["assemblyMethod"] = assemblyMethod;
  66. ai["assemblyMethodArgs"] = assemblyMethodArgs;
  67. return ai;
  68. }
  69. //----------------------------------------------------------
  70. // GetAssemblyIdentity
  71. //----------------------------------------------------------
  72. public AssemblyIdentity GetAssemblyIdentity()
  73. {
  74. if (_assemblyIdentity != null)
  75. goto exit;
  76. XPathNavigator myXPathNavigator = _xPathDocument.CreateNavigator();
  77. XPathNodeIterator myXPathNodeIterator = myXPathNavigator.Select ("/assembly/assemblyIdentity");
  78. myXPathNodeIterator.MoveNext();
  79. _assemblyIdentity = XMLToAssemblyId(myXPathNodeIterator);
  80. exit:
  81. return _assemblyIdentity;
  82. }
  83. //----------------------------------------------------------
  84. // GetNextDependentFileInfo
  85. //----------------------------------------------------------
  86. public DependentFileInfo GetNextDependentFileInfo()
  87. {
  88. if (_xPathFileIterator == null)
  89. {
  90. XPathNavigator myXPathNavigator = _xPathDocument.CreateNavigator();
  91. _xPathFileIterator = myXPathNavigator.Select ("/assembly/file");
  92. }
  93. if (_xPathFileIterator.MoveNext() == false)
  94. return null;
  95. DependentFileInfo dfi = new DependentFileInfo();
  96. dfi["name"] = _xPathFileIterator.Current.GetAttribute("name", "");
  97. dfi["hash"] = _xPathFileIterator.Current.GetAttribute("hash", "");
  98. return dfi;
  99. }
  100. //----------------------------------------------------------
  101. // GetNextDependentAssemblyInfo
  102. //----------------------------------------------------------
  103. public DependentAssemblyInfo GetNextDependentAssemblyInfo()
  104. {
  105. if (_xPathAssemblyIterator == null)
  106. {
  107. XPathNavigator myXPathNavigator = _xPathDocument.CreateNavigator();
  108. _xPathAssemblyIterator = myXPathNavigator.Select ("/assembly/dependency/dependentAssembly");
  109. }
  110. if (_xPathAssemblyIterator.MoveNext() == false)
  111. return null;
  112. XPathNodeIterator asmIter = _xPathAssemblyIterator.Current.Select("assemblyIdentity");
  113. asmIter.MoveNext();
  114. DependentAssemblyInfo dai = new DependentAssemblyInfo();
  115. dai.assemblyIdentity = XMLToAssemblyId(asmIter);
  116. XPathNodeIterator installIter = _xPathAssemblyIterator.Current.Select("install");
  117. installIter.MoveNext();
  118. dai["codeBase"] = installIter.Current.GetAttribute("codebase", "");
  119. return dai;
  120. }
  121. //----------------------------------------------------------
  122. // XMLToAssemblyId
  123. //----------------------------------------------------------
  124. private AssemblyIdentity XMLToAssemblyId(XPathNodeIterator xPathNodeIterator)
  125. {
  126. AssemblyIdentity assemblyIdentity = new AssemblyIdentity();
  127. assemblyIdentity["name"] =xPathNodeIterator.Current.GetAttribute("name", "");
  128. assemblyIdentity["version"] =xPathNodeIterator.Current.GetAttribute("version", "");
  129. assemblyIdentity["processorArchitecture"] =xPathNodeIterator.Current.GetAttribute("processorArchitecture", "");
  130. assemblyIdentity["publicKeyToken"] =xPathNodeIterator.Current.GetAttribute("publicKeyToken", "");
  131. assemblyIdentity["language"] =xPathNodeIterator.Current.GetAttribute("language", "");
  132. return assemblyIdentity;
  133. }
  134. //----------------------------------------------------------
  135. // ResetIterators
  136. //----------------------------------------------------------
  137. public void ResetIterators()
  138. {
  139. _xPathAssemblyIterator = null;
  140. _xPathFileIterator = null;
  141. }
  142. //----------------------------------------------------------
  143. // GetFileType()
  144. //----------------------------------------------------------
  145. public FileType GetFileType()
  146. {
  147. return FileType.ApplicationManifest;
  148. }
  149. }
  150. //----------------------------------------------------------
  151. // ComponentManifestImport
  152. //----------------------------------------------------------
  153. public class ComponentManifestImport : IAssemblyManifestImport
  154. {
  155. Uri _manifestUri;
  156. string _manifestFileName;
  157. Assembly _assembly;
  158. AssemblyIdentity _assemblyIdentity;
  159. Module[] _dependentFiles;
  160. AssemblyName[] _dependentAssemblies;
  161. int _fileCount;
  162. int _assemblyCount;
  163. //----------------------------------------------------------
  164. // ctor
  165. //----------------------------------------------------------
  166. public ComponentManifestImport(Uri manifestUri)
  167. {
  168. _manifestUri = manifestUri;
  169. _manifestFileName = System.IO.Path.GetFileName(_manifestUri.ToString());
  170. _assembly = Assembly.LoadFrom(manifestUri.ToString());
  171. _assemblyIdentity = null;
  172. _dependentFiles = null;
  173. _dependentAssemblies = null;
  174. _fileCount = _assemblyCount = 0;
  175. }
  176. //----------------------------------------------------------
  177. // GetAssemblyIdentity
  178. //----------------------------------------------------------
  179. public AssemblyIdentity GetAssemblyIdentity()
  180. {
  181. if (_assemblyIdentity != null)
  182. goto exit;
  183. AssemblyName assemblyName = _assembly.GetName();
  184. _assemblyIdentity = NameToAssemblyIdentity(assemblyName);
  185. exit:
  186. return _assemblyIdentity;
  187. }
  188. //----------------------------------------------------------
  189. // GetNextDependentFileInfo
  190. //----------------------------------------------------------
  191. public DependentFileInfo GetNextDependentFileInfo()
  192. {
  193. if (_dependentFiles == null)
  194. _dependentFiles = _assembly.GetModules(true);
  195. if (_fileCount >= _dependentFiles.Length)
  196. return null;
  197. if (String.Compare(_dependentFiles[_fileCount].ScopeName, _manifestFileName) == 0)
  198. _fileCount++;
  199. if (_fileCount >= _dependentFiles.Length)
  200. return null;
  201. DependentFileInfo dfi = new DependentFileInfo();
  202. dfi["name"] = _dependentFiles[_fileCount].ScopeName;
  203. dfi["hash"] = null; // damn, we have to fix this!!!!
  204. _fileCount++;
  205. return dfi;
  206. }
  207. /*
  208. //----------------------------------------------------------
  209. // GetNextDependentFileInfo
  210. //----------------------------------------------------------
  211. public DependentFileInfo GetNextDependentFileInfo()
  212. {
  213. if (_dependentFiles == null)
  214. _dependentFiles = _assembly.GetFiles();
  215. if (_fileCount >= _dependentFiles.Length)
  216. return null;
  217. Uri fileUri = new Uri(_dependentFiles[_fileCount].Name);
  218. if (String.Compare(fileUri.ToString(), _manifestUri.ToString(), true) == 0)
  219. _fileCount++;
  220. if (_fileCount >= _dependentFiles.Length)
  221. return null;
  222. DependentFileInfo dfi = new DependentFileInfo();
  223. dfi.fileName = _dependentFiles[_fileCount].Name;
  224. dfi.fileHash = null; // damn, we have to fix this!!!!
  225. _fileCount++;
  226. return dfi;
  227. }
  228. */
  229. //----------------------------------------------------------
  230. // GetNextDependentAssemblyInfo
  231. //----------------------------------------------------------
  232. public DependentAssemblyInfo GetNextDependentAssemblyInfo()
  233. {
  234. if (_dependentAssemblies == null)
  235. _dependentAssemblies = _assembly.GetReferencedAssemblies();
  236. if (_assemblyCount >= _dependentAssemblies.Length)
  237. return null;
  238. DependentAssemblyInfo dai = new DependentAssemblyInfo();
  239. dai.assemblyIdentity = NameToAssemblyIdentity(_dependentAssemblies[_assemblyCount]);
  240. dai["codeBase"] = _dependentAssemblies[_assemblyCount].CodeBase;
  241. _assemblyCount++;
  242. return dai;
  243. }
  244. //----------------------------------------------------------
  245. // ResetIterators
  246. //----------------------------------------------------------
  247. public void ResetIterators()
  248. {
  249. _fileCount = _assemblyCount = 0;
  250. }
  251. //----------------------------------------------------------
  252. // GetFileType()
  253. //----------------------------------------------------------
  254. public FileType GetFileType()
  255. {
  256. return FileType.ComponentManifest;
  257. }
  258. //----------------------------------------------------------
  259. // BytesToHex
  260. //----------------------------------------------------------
  261. private static String BytesToHex(byte[] bytes)
  262. {
  263. StringBuilder sb = new StringBuilder(bytes.Length * 2);
  264. foreach (byte b in bytes)
  265. sb.Append(b.ToString("x"));
  266. return sb.ToString();
  267. }
  268. //----------------------------------------------------------
  269. // NameToAssemblyIdentity
  270. //----------------------------------------------------------
  271. private AssemblyIdentity NameToAssemblyIdentity(AssemblyName assemblyName)
  272. {
  273. AssemblyIdentity assemblyIdentity = new AssemblyIdentity();
  274. assemblyIdentity["name"] = assemblyName.Name;
  275. assemblyIdentity["version"] = assemblyName.Version.ToString();
  276. assemblyIdentity["processorArchitecture"] ="x86";
  277. assemblyIdentity["publicKeyToken"] =BytesToHex(assemblyName.GetPublicKeyToken());
  278. assemblyIdentity["language"] =assemblyName.CultureInfo.ToString();
  279. return assemblyIdentity;
  280. }
  281. }
  282. }