[MSBuild] Fix minor assembly resolution issue
[mono.git] / mcs / class / corlib / System.Reflection / AssemblyName.cs
1 //
2 // System.Reflection/AssemblyName.cs
3 //
4 // Authors:
5 //      Paolo Molaro (lupus@ximian.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2001 Ximian, Inc.  http://www.ximian.com
9 // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Configuration.Assemblies;
33 using System.Globalization;
34 using System.Runtime.Serialization;
35 using System.Security;
36 using System.Security.Cryptography;
37 using System.Security.Permissions;
38 using System.Text;
39 using System.Runtime.InteropServices;
40 using System.Runtime.CompilerServices;
41 using System.IO;
42
43 using Mono.Security;
44 using Mono.Security.Cryptography;
45
46 namespace System.Reflection {
47
48 // References:
49 // a.   Uniform Resource Identifiers (URI): Generic Syntax
50 //      http://www.ietf.org/rfc/rfc2396.txt
51
52         [ComVisible (true)]
53         [ComDefaultInterfaceAttribute (typeof (_AssemblyName))]
54         [Serializable]
55         [ClassInterfaceAttribute (ClassInterfaceType.None)]
56         [StructLayout (LayoutKind.Sequential)]
57 #if MOBILE
58         public sealed class AssemblyName  : ICloneable, ISerializable, IDeserializationCallback {
59 #else
60         public sealed class AssemblyName  : ICloneable, ISerializable, IDeserializationCallback, _AssemblyName {
61 #endif
62 #pragma warning disable 169
63                 #region Synch with object-internals.h
64                 string name;
65                 string codebase;
66                 int major, minor, build, revision;
67                 CultureInfo cultureinfo;
68                 AssemblyNameFlags flags;
69                 AssemblyHashAlgorithm hashalg;
70                 StrongNameKeyPair keypair;
71                 byte[] publicKey;
72                 byte[] keyToken;
73                 AssemblyVersionCompatibility versioncompat;
74                 Version version;
75                 ProcessorArchitecture processor_architecture = ProcessorArchitecture.None;
76                 #endregion
77 #pragma warning restore 169             
78
79                 AssemblyContentType contentType;
80                 public AssemblyName ()
81                 {
82                         // defaults
83                         versioncompat = AssemblyVersionCompatibility.SameMachine;
84                 }
85
86                 [MethodImpl (MethodImplOptions.InternalCall)]
87                 static extern bool ParseName (AssemblyName aname, string assemblyName);
88                 
89                 public AssemblyName (string assemblyName)
90                 {
91                         if (assemblyName == null)
92                                 throw new ArgumentNullException ("assemblyName");
93                         if (assemblyName.Length < 1)
94                                 throw new ArgumentException ("assemblyName cannot have zero length.");
95                                 
96                         if (!ParseName (this, assemblyName))
97                                 throw new FileLoadException ("The assembly name is invalid.");
98                 }
99                 
100                 [MonoLimitation ("Not used, as the values are too limited;  Mono supports more")]
101                 public ProcessorArchitecture ProcessorArchitecture {
102                         get {
103                                 return processor_architecture;
104                         }
105                         set {
106                                 processor_architecture = value;
107                         }
108                 }
109
110                 internal AssemblyName (SerializationInfo si, StreamingContext sc)
111                 {
112                         name = si.GetString ("_Name");
113                         codebase = si.GetString ("_CodeBase");
114                         version = (Version)si.GetValue ("_Version", typeof (Version));
115                         publicKey = (byte[])si.GetValue ("_PublicKey", typeof (byte[]));
116                         keyToken = (byte[])si.GetValue ("_PublicKeyToken", typeof (byte[]));
117                         hashalg = (AssemblyHashAlgorithm)si.GetValue ("_HashAlgorithm", typeof (AssemblyHashAlgorithm));
118                         keypair = (StrongNameKeyPair)si.GetValue ("_StrongNameKeyPair", typeof (StrongNameKeyPair));
119                         versioncompat = (AssemblyVersionCompatibility)si.GetValue ("_VersionCompatibility", typeof (AssemblyVersionCompatibility));
120                         flags = (AssemblyNameFlags)si.GetValue ("_Flags", typeof (AssemblyNameFlags));
121                         int lcid = si.GetInt32 ("_CultureInfo");
122                         if (lcid != -1) cultureinfo = new CultureInfo (lcid);
123                 }
124
125                 public string Name {
126                         get { return name; }
127                         set { name = value; }
128                 }
129
130                 public string CodeBase {
131                         get { return codebase; }
132                         set { codebase = value; }
133                 }
134
135                 public string EscapedCodeBase {
136                         get {
137                                 if (codebase == null)
138                                         return null;
139                                 return Uri.EscapeString (codebase, false, true, true);
140                         }
141                 }
142
143                 public CultureInfo CultureInfo {
144                         get { return cultureinfo; }
145                         set { cultureinfo = value; }
146                 }
147
148                 public AssemblyNameFlags Flags {
149                         get { return flags; }
150                         set { flags = value; }
151                 }
152
153                 public string FullName {
154                         get {
155                                 if (name == null)
156                                         return string.Empty;
157                                 StringBuilder fname = new StringBuilder ();
158                                 if (Char.IsWhiteSpace (name [0]))
159                                         fname.Append ("\"" + name + "\"");
160                                 else
161                                         fname.Append (name);
162                                 if (Version != null) {
163                                         fname.Append (", Version=");
164                                         fname.Append (Version.ToString ());
165                                 }
166                                 if (cultureinfo != null) {
167                                         fname.Append (", Culture=");
168                                         if (cultureinfo.LCID == CultureInfo.InvariantCulture.LCID)
169                                                 fname.Append ("neutral");
170                                         else
171                                                 fname.Append (cultureinfo.Name);
172                                 }
173                                 byte [] pub_tok = InternalGetPublicKeyToken ();
174                                 if (pub_tok != null) {
175                                         if (pub_tok.Length == 0)
176                                                 fname.Append (", PublicKeyToken=null");
177                                         else {
178                                                 fname.Append (", PublicKeyToken=");
179                                                 for (int i = 0; i < pub_tok.Length; i++)
180                                                         fname.Append (pub_tok[i].ToString ("x2"));
181                                         }
182                                 }
183
184                                 if ((Flags & AssemblyNameFlags.Retargetable) != 0)
185                                         fname.Append (", Retargetable=Yes");
186
187                                 return fname.ToString ();
188                         }
189                 }
190
191                 public AssemblyHashAlgorithm HashAlgorithm {
192                         get { return hashalg; }
193                         set { hashalg = value; }
194                 }
195
196                 public StrongNameKeyPair KeyPair {
197                         get { return keypair; }
198                         set { keypair = value; }
199                 }
200
201                 public Version Version {
202                         get {
203                                 return version;
204                         }
205
206                         set {
207                                 version = value;
208                                 if (value == null)
209                                         major = minor = build = revision = 0;
210                                 else {
211                                         major = value.Major;
212                                         minor = value.Minor;
213                                         build = value.Build;
214                                         revision = value.Revision;
215                                 }
216                         }
217                 }
218
219                 public AssemblyVersionCompatibility VersionCompatibility {
220                         get { return versioncompat; }
221                         set { versioncompat = value; }
222                 }
223                 
224                 public override string ToString ()
225                 {
226                         string name = FullName;
227                         return (name != null) ? name : base.ToString ();
228                 }
229
230                 public byte[] GetPublicKey()
231                 {
232                         return publicKey;
233                 }
234
235                 public byte[] GetPublicKeyToken ()
236                 {
237                         if (keyToken != null)
238                                 return keyToken;
239                         if (publicKey == null)
240                                 return null;
241
242                                 if (publicKey.Length == 0)
243                                         return EmptyArray<byte>.Value;
244
245                                 if (!IsPublicKeyValid)
246                                         throw new  SecurityException ("The public key is not valid.");
247
248                                 keyToken = ComputePublicKeyToken ();
249                                 return keyToken;
250                 }
251
252                 private bool IsPublicKeyValid {
253                         get {
254                                 // check for ECMA key
255                                 if (publicKey.Length == 16) {
256                                         int i = 0;
257                                         int sum = 0;
258                                         while (i < publicKey.Length)
259                                                 sum += publicKey [i++];
260                                         if (sum == 4)
261                                                 return true;
262                                 }
263
264                                 switch (publicKey [0]) {
265                                 case 0x00: // public key inside a header
266                                         if (publicKey.Length > 12 && publicKey [12] == 0x06) {
267 #if MOBILE
268                                                 return true;
269 #else
270                                                 try {
271                                                         CryptoConvert.FromCapiPublicKeyBlob (
272                                                                 publicKey, 12);
273                                                         return true;
274                                                 } catch (CryptographicException) {
275                                                 }
276 #endif
277                                         }
278                                         break;
279                                 case 0x06: // public key
280 #if MOBILE
281                                         return true;
282 #else
283                                         try {
284                                                 CryptoConvert.FromCapiPublicKeyBlob (publicKey);
285                                                 return true;
286                                         } catch (CryptographicException) {
287                                         }
288                                         break;                                  
289 #endif
290                                 case 0x07: // private key
291                                         break;
292                                 }
293
294                                 return false;
295                         }
296                 }
297
298                 private byte [] InternalGetPublicKeyToken ()
299                 {
300                         if (keyToken != null)
301                                 return keyToken;
302
303                         if (publicKey == null)
304                                 return null;
305
306                         if (publicKey.Length == 0)
307                                 return EmptyArray<byte>.Value;
308
309                         if (!IsPublicKeyValid)
310                                 throw new  SecurityException ("The public key is not valid.");
311
312                         return ComputePublicKeyToken ();
313                 }
314
315                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
316                 extern unsafe static void get_public_token (byte* token, byte* pubkey, int len);
317
318                 private unsafe byte [] ComputePublicKeyToken ()
319                 {
320                         byte [] token = new byte [8];
321                         fixed (byte* pkt = token)
322                         fixed (byte *pk = publicKey)
323                                 get_public_token (pkt, pk, publicKey.Length);
324                         return token;
325                 }
326
327                 public static bool ReferenceMatchesDefinition (AssemblyName reference, AssemblyName definition)
328                 {
329                         if (reference == null)
330                                 throw new ArgumentNullException ("reference");
331                         if (definition == null)
332                                 throw new ArgumentNullException ("definition");
333                         
334                         // we only compare the simple assembly name to be consistent with MS .NET,
335                         // which is the result of a bug in their implementation (see https://connect.microsoft.com/VisualStudio/feedback/details/752902)
336                         return string.Equals (reference.Name, definition.Name, StringComparison.OrdinalIgnoreCase);
337                 }
338
339                 public void SetPublicKey (byte[] publicKey) 
340                 {
341                         if (publicKey == null)
342                                 flags ^= AssemblyNameFlags.PublicKey;
343                         else
344                                 flags |= AssemblyNameFlags.PublicKey;
345                         this.publicKey = publicKey;
346                 }
347
348                 public void SetPublicKeyToken (byte[] publicKeyToken) 
349                 {
350                         keyToken = publicKeyToken;
351                 }
352
353                 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
354                 public void GetObjectData (SerializationInfo info, StreamingContext context)
355                 {
356                         if (info == null)
357                                 throw new ArgumentNullException ("info");
358
359                         info.AddValue ("_Name", name);
360                         info.AddValue ("_PublicKey", publicKey);
361                         info.AddValue ("_PublicKeyToken", keyToken);
362                         info.AddValue ("_CultureInfo", cultureinfo != null ? cultureinfo.LCID : -1);
363                         info.AddValue ("_CodeBase", codebase);
364                         info.AddValue ("_Version", Version);
365                         info.AddValue ("_HashAlgorithm", hashalg);
366                         info.AddValue ("_HashAlgorithmForControl", AssemblyHashAlgorithm.None);
367                         info.AddValue ("_StrongNameKeyPair", keypair);
368                         info.AddValue ("_VersionCompatibility", versioncompat);
369                         info.AddValue ("_Flags", flags);
370                         info.AddValue ("_HashForControl", null);
371                 }
372
373                 public object Clone() 
374                 {
375                         AssemblyName an = new AssemblyName ();
376                         an.name = name;
377                         an.codebase = codebase;
378                         an.major = major;
379                         an.minor = minor;
380                         an.build = build;
381                         an.revision = revision;
382                         an.version = version;
383                         an.cultureinfo = cultureinfo;
384                         an.flags = flags;
385                         an.hashalg = hashalg;
386                         an.keypair = keypair;
387                         an.publicKey = publicKey;
388                         an.keyToken = keyToken;
389                         an.versioncompat = versioncompat;
390                         an.processor_architecture = processor_architecture;
391                         return an;
392                 }
393
394                 public void OnDeserialization (object sender) 
395                 {
396                         Version = version;
397                 }
398
399                 public static AssemblyName GetAssemblyName (string assemblyFile) 
400                 {
401                         if (assemblyFile == null)
402                                 throw new ArgumentNullException ("assemblyFile");
403
404                         AssemblyName aname = new AssemblyName ();
405                         Assembly.InternalGetAssemblyName (Path.GetFullPath (assemblyFile), aname);
406                         return aname;
407                 }
408
409 #if !MOBILE
410                 void _AssemblyName.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
411                 {
412                         throw new NotImplementedException ();
413                 }
414
415                 void _AssemblyName.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
416                 {
417                         throw new NotImplementedException ();
418                 }
419
420                 void _AssemblyName.GetTypeInfoCount (out uint pcTInfo)
421                 {
422                         throw new NotImplementedException ();
423                 }
424
425                 void _AssemblyName.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
426                         IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
427                 {
428                         throw new NotImplementedException ();
429                 }
430 #endif
431
432                 public string CultureName {
433                         get {
434                                 if (cultureinfo == null)
435                                         return null;
436                                 if (cultureinfo.LCID == CultureInfo.InvariantCulture.LCID)
437                                         return "neutral";
438                                 return cultureinfo.Name;
439                         }
440                 }
441
442                 [ComVisibleAttribute(false)]
443                 public AssemblyContentType ContentType {
444                         get {
445                                 return contentType;
446                         }
447                         set {
448                                 contentType = value;
449                         }
450                 }
451         }
452 }