Bug 15574. XML deserialization recursion: add array type to known_types?
[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                 public AssemblyName ()
80                 {
81                         // defaults
82                         versioncompat = AssemblyVersionCompatibility.SameMachine;
83                 }
84
85                 [MethodImpl (MethodImplOptions.InternalCall)]
86                 static extern bool ParseName (AssemblyName aname, string assemblyName);
87                 
88                 public AssemblyName (string assemblyName)
89                 {
90                         if (assemblyName == null)
91                                 throw new ArgumentNullException ("assemblyName");
92                         if (assemblyName.Length < 1)
93                                 throw new ArgumentException ("assemblyName cannot have zero length.");
94                                 
95                         if (!ParseName (this, assemblyName))
96                                 throw new FileLoadException ("The assembly name is invalid.");
97                 }
98                 
99                 [MonoLimitation ("Not used, as the values are too limited;  Mono supports more")]
100                 public ProcessorArchitecture ProcessorArchitecture {
101                         get {
102                                 return processor_architecture;
103                         }
104                         set {
105                                 processor_architecture = value;
106                         }
107                 }
108
109                 internal AssemblyName (SerializationInfo si, StreamingContext sc)
110                 {
111                         name = si.GetString ("_Name");
112                         codebase = si.GetString ("_CodeBase");
113                         version = (Version)si.GetValue ("_Version", typeof (Version));
114                         publicKey = (byte[])si.GetValue ("_PublicKey", typeof (byte[]));
115                         keyToken = (byte[])si.GetValue ("_PublicKeyToken", typeof (byte[]));
116                         hashalg = (AssemblyHashAlgorithm)si.GetValue ("_HashAlgorithm", typeof (AssemblyHashAlgorithm));
117                         keypair = (StrongNameKeyPair)si.GetValue ("_StrongNameKeyPair", typeof (StrongNameKeyPair));
118                         versioncompat = (AssemblyVersionCompatibility)si.GetValue ("_VersionCompatibility", typeof (AssemblyVersionCompatibility));
119                         flags = (AssemblyNameFlags)si.GetValue ("_Flags", typeof (AssemblyNameFlags));
120                         int lcid = si.GetInt32 ("_CultureInfo");
121                         if (lcid != -1) cultureinfo = new CultureInfo (lcid);
122                 }
123
124                 public string Name {
125                         get { return name; }
126                         set { name = value; }
127                 }
128
129                 public string CodeBase {
130                         get { return codebase; }
131                         set { codebase = value; }
132                 }
133
134                 public string EscapedCodeBase {
135                         get {
136                                 if (codebase == null)
137                                         return null;
138                                 return Uri.EscapeString (codebase, false, true, true);
139                         }
140                 }
141
142                 public CultureInfo CultureInfo {
143                         get { return cultureinfo; }
144                         set { cultureinfo = value; }
145                 }
146
147                 public AssemblyNameFlags Flags {
148                         get { return flags; }
149                         set { flags = value; }
150                 }
151
152                 public string FullName {
153                         get {
154                                 if (name == null)
155                                         return string.Empty;
156                                 StringBuilder fname = new StringBuilder ();
157                                 if (Char.IsWhiteSpace (name [0]))
158                                         fname.Append ("\"" + name + "\"");
159                                 else
160                                         fname.Append (name);
161                                 if (Version != null) {
162                                         fname.Append (", Version=");
163                                         fname.Append (Version.ToString ());
164                                 }
165                                 if (cultureinfo != null) {
166                                         fname.Append (", Culture=");
167                                         if (cultureinfo.LCID == CultureInfo.InvariantCulture.LCID)
168                                                 fname.Append ("neutral");
169                                         else
170                                                 fname.Append (cultureinfo.Name);
171                                 }
172                                 byte [] pub_tok = InternalGetPublicKeyToken ();
173                                 if (pub_tok != null) {
174                                         if (pub_tok.Length == 0)
175                                                 fname.Append (", PublicKeyToken=null");
176                                         else {
177                                                 fname.Append (", PublicKeyToken=");
178                                                 for (int i = 0; i < pub_tok.Length; i++)
179                                                         fname.Append (pub_tok[i].ToString ("x2"));
180                                         }
181                                 }
182
183                                 if ((Flags & AssemblyNameFlags.Retargetable) != 0)
184                                         fname.Append (", Retargetable=Yes");
185
186                                 return fname.ToString ();
187                         }
188                 }
189
190                 public AssemblyHashAlgorithm HashAlgorithm {
191                         get { return hashalg; }
192                         set { hashalg = value; }
193                 }
194
195                 public StrongNameKeyPair KeyPair {
196                         get { return keypair; }
197                         set { keypair = value; }
198                 }
199
200                 public Version Version {
201                         get {
202                                 return version;
203                         }
204
205                         set {
206                                 version = value;
207                                 if (value == null)
208                                         major = minor = build = revision = 0;
209                                 else {
210                                         major = value.Major;
211                                         minor = value.Minor;
212                                         build = value.Build;
213                                         revision = value.Revision;
214                                 }
215                         }
216                 }
217
218                 public AssemblyVersionCompatibility VersionCompatibility {
219                         get { return versioncompat; }
220                         set { versioncompat = value; }
221                 }
222                 
223                 public override string ToString ()
224                 {
225                         string name = FullName;
226                         return (name != null) ? name : base.ToString ();
227                 }
228
229                 public byte[] GetPublicKey()
230                 {
231                         return publicKey;
232                 }
233
234                 public byte[] GetPublicKeyToken ()
235                 {
236                         if (keyToken != null)
237                                 return keyToken;
238                         if (publicKey == null)
239                                 return null;
240
241                                 if (publicKey.Length == 0)
242                                         return EmptyArray<byte>.Value;
243
244                                 if (!IsPublicKeyValid)
245                                         throw new  SecurityException ("The public key is not valid.");
246
247                                 keyToken = ComputePublicKeyToken ();
248                                 return keyToken;
249                 }
250
251                 private bool IsPublicKeyValid {
252                         get {
253                                 // check for ECMA key
254                                 if (publicKey.Length == 16) {
255                                         int i = 0;
256                                         int sum = 0;
257                                         while (i < publicKey.Length)
258                                                 sum += publicKey [i++];
259                                         if (sum == 4)
260                                                 return true;
261                                 }
262
263                                 switch (publicKey [0]) {
264                                 case 0x00: // public key inside a header
265                                         if (publicKey.Length > 12 && publicKey [12] == 0x06) {
266 #if MOBILE
267                                                 return true;
268 #else
269                                                 try {
270                                                         CryptoConvert.FromCapiPublicKeyBlob (
271                                                                 publicKey, 12);
272                                                         return true;
273                                                 } catch (CryptographicException) {
274                                                 }
275 #endif
276                                         }
277                                         break;
278                                 case 0x06: // public key
279 #if MOBILE
280                                         return true;
281 #else
282                                         try {
283                                                 CryptoConvert.FromCapiPublicKeyBlob (publicKey);
284                                                 return true;
285                                         } catch (CryptographicException) {
286                                         }
287                                         break;                                  
288 #endif
289                                 case 0x07: // private key
290                                         break;
291                                 }
292
293                                 return false;
294                         }
295                 }
296
297                 private byte [] InternalGetPublicKeyToken ()
298                 {
299                         if (keyToken != null)
300                                 return keyToken;
301
302                         if (publicKey == null)
303                                 return null;
304
305                         if (publicKey.Length == 0)
306                                 return EmptyArray<byte>.Value;
307
308                         if (!IsPublicKeyValid)
309                                 throw new  SecurityException ("The public key is not valid.");
310
311                         return ComputePublicKeyToken ();
312                 }
313
314                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
315                 extern unsafe static void get_public_token (byte* token, byte* pubkey, int len);
316
317                 private unsafe byte [] ComputePublicKeyToken ()
318                 {
319                         byte [] token = new byte [8];
320                         fixed (byte* pkt = token)
321                         fixed (byte *pk = publicKey)
322                                 get_public_token (pkt, pk, publicKey.Length);
323                         return token;
324                 }
325
326                 public static bool ReferenceMatchesDefinition (AssemblyName reference, AssemblyName definition)
327                 {
328                         if (reference == null)
329                                 throw new ArgumentNullException ("reference");
330                         if (definition == null)
331                                 throw new ArgumentNullException ("definition");
332                         
333                         // we only compare the simple assembly name to be consistent with MS .NET,
334                         // which is the result of a bug in their implementation (see https://connect.microsoft.com/VisualStudio/feedback/details/752902)
335                         return string.Equals (reference.Name, definition.Name, StringComparison.OrdinalIgnoreCase);
336                 }
337
338                 public void SetPublicKey (byte[] publicKey) 
339                 {
340                         if (publicKey == null)
341                                 flags ^= AssemblyNameFlags.PublicKey;
342                         else
343                                 flags |= AssemblyNameFlags.PublicKey;
344                         this.publicKey = publicKey;
345                 }
346
347                 public void SetPublicKeyToken (byte[] publicKeyToken) 
348                 {
349                         keyToken = publicKeyToken;
350                 }
351
352                 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
353                 public void GetObjectData (SerializationInfo info, StreamingContext context)
354                 {
355                         if (info == null)
356                                 throw new ArgumentNullException ("info");
357
358                         info.AddValue ("_Name", name);
359                         info.AddValue ("_PublicKey", publicKey);
360                         info.AddValue ("_PublicKeyToken", keyToken);
361                         info.AddValue ("_CultureInfo", cultureinfo != null ? cultureinfo.LCID : -1);
362                         info.AddValue ("_CodeBase", codebase);
363                         info.AddValue ("_Version", Version);
364                         info.AddValue ("_HashAlgorithm", hashalg);
365                         info.AddValue ("_HashAlgorithmForControl", AssemblyHashAlgorithm.None);
366                         info.AddValue ("_StrongNameKeyPair", keypair);
367                         info.AddValue ("_VersionCompatibility", versioncompat);
368                         info.AddValue ("_Flags", flags);
369                         info.AddValue ("_HashForControl", null);
370                 }
371
372                 public object Clone() 
373                 {
374                         AssemblyName an = new AssemblyName ();
375                         an.name = name;
376                         an.codebase = codebase;
377                         an.major = major;
378                         an.minor = minor;
379                         an.build = build;
380                         an.revision = revision;
381                         an.version = version;
382                         an.cultureinfo = cultureinfo;
383                         an.flags = flags;
384                         an.hashalg = hashalg;
385                         an.keypair = keypair;
386                         an.publicKey = publicKey;
387                         an.keyToken = keyToken;
388                         an.versioncompat = versioncompat;
389                         return an;
390                 }
391
392                 public void OnDeserialization (object sender) 
393                 {
394                         Version = version;
395                 }
396
397                 public static AssemblyName GetAssemblyName (string assemblyFile) 
398                 {
399                         if (assemblyFile == null)
400                                 throw new ArgumentNullException ("assemblyFile");
401
402                         AssemblyName aname = new AssemblyName ();
403                         Assembly.InternalGetAssemblyName (Path.GetFullPath (assemblyFile), aname);
404                         return aname;
405                 }
406
407 #if !MOBILE
408                 void _AssemblyName.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
409                 {
410                         throw new NotImplementedException ();
411                 }
412
413                 void _AssemblyName.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
414                 {
415                         throw new NotImplementedException ();
416                 }
417
418                 void _AssemblyName.GetTypeInfoCount (out uint pcTInfo)
419                 {
420                         throw new NotImplementedException ();
421                 }
422
423                 void _AssemblyName.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
424                         IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
425                 {
426                         throw new NotImplementedException ();
427                 }
428 #endif
429
430 #if NET_4_5
431                 public string CultureName {
432                         get {
433                                 if (cultureinfo == null)
434                                         return string.Empty;
435                                 else if (cultureinfo.LCID == CultureInfo.InvariantCulture.LCID)
436                                         return "neutral";
437                                 else
438                                         return cultureinfo.Name;
439                         }
440                 }
441
442                 [ComVisibleAttribute(false)]
443                 public AssemblyContentType ContentType {
444                         get { return AssemblyContentType.Default; }
445                         set {
446                                 if (value != AssemblyContentType.Default)
447                                         throw new InvalidOperationException ();
448                         }
449                 }
450 #endif
451         }
452 }