AssemblyNameReference.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //
  2. // AssemblyNameReference.cs
  3. //
  4. // Author:
  5. // Jb Evain (jbevain@gmail.com)
  6. //
  7. // Copyright (c) 2008 - 2011 Jb Evain
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Globalization;
  30. using System.Security.Cryptography;
  31. using System.Text;
  32. namespace Mono.Cecil
  33. {
  34. public class AssemblyNameReference : IMetadataScope
  35. {
  36. string name;
  37. string culture;
  38. Version version;
  39. uint attributes;
  40. byte[] public_key;
  41. byte[] public_key_token;
  42. AssemblyHashAlgorithm hash_algorithm;
  43. byte[] hash;
  44. internal MetadataToken token;
  45. string full_name;
  46. public string Name
  47. {
  48. get { return name; }
  49. set
  50. {
  51. name = value;
  52. full_name = null;
  53. }
  54. }
  55. public string Culture
  56. {
  57. get { return culture; }
  58. set
  59. {
  60. culture = value;
  61. full_name = null;
  62. }
  63. }
  64. public Version Version
  65. {
  66. get { return version; }
  67. set
  68. {
  69. version = value;
  70. full_name = null;
  71. }
  72. }
  73. public AssemblyAttributes Attributes
  74. {
  75. get { return (AssemblyAttributes)attributes; }
  76. set { attributes = (uint)value; }
  77. }
  78. public bool HasPublicKey
  79. {
  80. get { return Mixin.GetAttributes(attributes,(uint)AssemblyAttributes.PublicKey); }
  81. set { attributes =Mixin.SetAttributes(attributes,(uint)AssemblyAttributes.PublicKey, value); }
  82. }
  83. public bool IsSideBySideCompatible
  84. {
  85. get { return Mixin.GetAttributes(attributes,(uint)AssemblyAttributes.SideBySideCompatible); }
  86. set { attributes =Mixin.SetAttributes(attributes,(uint)AssemblyAttributes.SideBySideCompatible, value); }
  87. }
  88. public bool IsRetargetable
  89. {
  90. get { return Mixin.GetAttributes(attributes,(uint)AssemblyAttributes.Retargetable); }
  91. set { attributes =Mixin.SetAttributes(attributes,(uint)AssemblyAttributes.Retargetable, value); }
  92. }
  93. public bool IsWindowsRuntime
  94. {
  95. get { return Mixin.GetAttributes(attributes,(uint)AssemblyAttributes.WindowsRuntime); }
  96. set { attributes =Mixin.SetAttributes(attributes,(uint)AssemblyAttributes.WindowsRuntime, value); }
  97. }
  98. public byte[] PublicKey
  99. {
  100. get { return public_key ?? Empty<byte>.Array; }
  101. set
  102. {
  103. public_key = value;
  104. HasPublicKey = !Mixin.IsNullOrEmpty(public_key);
  105. public_key_token = Empty<byte>.Array;
  106. full_name = null;
  107. }
  108. }
  109. public byte[] PublicKeyToken
  110. {
  111. get
  112. {
  113. if (Mixin.IsNullOrEmpty(public_key_token) && !Mixin.IsNullOrEmpty(public_key))
  114. {
  115. var hash = HashPublicKey();
  116. // we need the last 8 bytes in reverse order
  117. byte[] local_public_key_token = new byte[8];
  118. Array.Copy(hash, (hash.Length - 8), local_public_key_token, 0, 8);
  119. Array.Reverse(local_public_key_token, 0, 8);
  120. public_key_token = local_public_key_token; // publish only once finished (required for thread-safety)
  121. }
  122. return public_key_token ?? Empty<byte>.Array;
  123. }
  124. set
  125. {
  126. public_key_token = value;
  127. full_name = null;
  128. }
  129. }
  130. byte[] HashPublicKey()
  131. {
  132. HashAlgorithm algorithm;
  133. switch (hash_algorithm)
  134. {
  135. case AssemblyHashAlgorithm.Reserved:
  136. throw new NotSupportedException();
  137. default:
  138. // None default to SHA1
  139. algorithm = new SHA1Managed();
  140. break;
  141. }
  142. using (algorithm)
  143. return algorithm.ComputeHash(public_key);
  144. }
  145. public virtual MetadataScopeType MetadataScopeType
  146. {
  147. get { return MetadataScopeType.AssemblyNameReference; }
  148. }
  149. public string FullName
  150. {
  151. get
  152. {
  153. if (full_name != null)
  154. return full_name;
  155. const string sep = ", ";
  156. var builder = new StringBuilder();
  157. builder.Append(name);
  158. if (version != null)
  159. {
  160. builder.Append(sep);
  161. builder.Append("Version=");
  162. builder.Append(version.ToString());
  163. }
  164. builder.Append(sep);
  165. builder.Append("Culture=");
  166. builder.Append(string.IsNullOrEmpty(culture) ? "neutral" : culture);
  167. builder.Append(sep);
  168. builder.Append("PublicKeyToken=");
  169. var pk_token = PublicKeyToken;
  170. if (!Mixin.IsNullOrEmpty(pk_token) && pk_token.Length > 0)
  171. {
  172. for (int i = 0; i < pk_token.Length; i++)
  173. {
  174. builder.Append(pk_token[i].ToString("x2"));
  175. }
  176. }
  177. else
  178. builder.Append("null");
  179. return full_name = builder.ToString();
  180. }
  181. }
  182. public static AssemblyNameReference Parse(string fullName)
  183. {
  184. if (fullName == null)
  185. throw new ArgumentNullException("fullName");
  186. if (fullName.Length == 0)
  187. throw new ArgumentException("Name can not be empty");
  188. var name = new AssemblyNameReference();
  189. var tokens = fullName.Split(',');
  190. for (int i = 0; i < tokens.Length; i++)
  191. {
  192. var token = tokens[i].Trim();
  193. if (i == 0)
  194. {
  195. name.Name = token;
  196. continue;
  197. }
  198. var parts = token.Split('=');
  199. if (parts.Length != 2)
  200. throw new ArgumentException("Malformed name");
  201. switch (parts[0].ToLowerInvariant())
  202. {
  203. case "version":
  204. name.Version = new Version(parts[1]);
  205. break;
  206. case "culture":
  207. name.Culture = parts[1];
  208. break;
  209. case "publickeytoken":
  210. var pk_token = parts[1];
  211. if (pk_token == "null")
  212. break;
  213. name.PublicKeyToken = new byte[pk_token.Length / 2];
  214. for (int j = 0; j < name.PublicKeyToken.Length; j++)
  215. name.PublicKeyToken[j] = Byte.Parse(pk_token.Substring(j * 2, 2), NumberStyles.HexNumber);
  216. break;
  217. }
  218. }
  219. return name;
  220. }
  221. public AssemblyHashAlgorithm HashAlgorithm
  222. {
  223. get { return hash_algorithm; }
  224. set { hash_algorithm = value; }
  225. }
  226. public virtual byte[] Hash
  227. {
  228. get { return hash; }
  229. set { hash = value; }
  230. }
  231. public MetadataToken MetadataToken
  232. {
  233. get { return token; }
  234. set { token = value; }
  235. }
  236. internal AssemblyNameReference()
  237. {
  238. }
  239. public AssemblyNameReference(string name, Version version)
  240. {
  241. if (name == null)
  242. throw new ArgumentNullException("name");
  243. this.name = name;
  244. this.version = version;
  245. this.hash_algorithm = AssemblyHashAlgorithm.None;
  246. this.token = new MetadataToken(TokenType.AssemblyRef);
  247. }
  248. public override string ToString()
  249. {
  250. return this.FullName;
  251. }
  252. }
  253. }