merge r67228-r67235, r67237, r67251 and r67256-67259 to trunk (they are
[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.Cryptography;
36 using System.Security.Permissions;
37 using System.Text;
38 using System.Runtime.InteropServices;
39 using System.Runtime.CompilerServices;
40 using System.IO;
41
42 using Mono.Security;
43
44 namespace System.Reflection {
45
46 // References:
47 // a.   Uniform Resource Identifiers (URI): Generic Syntax
48 //      http://www.ietf.org/rfc/rfc2396.txt
49
50 #if NET_2_0
51         [ComVisible (true)]
52         [ComDefaultInterfaceAttribute (typeof (_AssemblyName))]
53 #endif
54         [Serializable]
55         [ClassInterfaceAttribute (ClassInterfaceType.None)]
56         [MonoTODO ("Fix serialization compatibility with MS.NET")]
57         public sealed class AssemblyName  : ICloneable, ISerializable, IDeserializationCallback, _AssemblyName {
58
59                 #region Synch with object-internals.h
60                 string name;
61                 string codebase;
62                 int major, minor, build, revision;
63                 CultureInfo cultureinfo;
64                 AssemblyNameFlags flags;
65                 AssemblyHashAlgorithm hashalg;
66                 StrongNameKeyPair keypair;
67                 byte[] publicKey;
68                 byte[] keyToken;
69                 AssemblyVersionCompatibility versioncompat;
70                 Version version;
71 #if NET_2_0
72                 ProcessorArchitecture processor_architecture;
73 #else
74                 int processor_architecture;
75 #endif
76         #endregion
77                 
78                 public AssemblyName ()
79                 {
80                         // defaults
81                         versioncompat = AssemblyVersionCompatibility.SameMachine;
82                 }
83
84 #if NET_2_0 || BOOTSTRAP_NET_2_0
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 #endif
99                 
100 #if NET_2_0
101                 [MonoTODO]
102                 public ProcessorArchitecture ProcessorArchitecture {
103                         get {
104                                 return processor_architecture;
105                         }
106                         set {
107                                 processor_architecture = value;
108                         }
109                 }
110 #endif
111
112                 internal AssemblyName (SerializationInfo si, StreamingContext sc)
113                 {
114                         name = si.GetString ("_Name");
115                         codebase = si.GetString ("_CodeBase");
116                         version = (Version)si.GetValue ("_Version", typeof (Version));
117                         publicKey = (byte[])si.GetValue ("_PublicKey", typeof (byte[]));
118                         keyToken = (byte[])si.GetValue ("_PublicKeyToken", typeof (byte[]));
119                         hashalg = (AssemblyHashAlgorithm)si.GetValue ("_HashAlgorithm", typeof (AssemblyHashAlgorithm));
120                         keypair = (StrongNameKeyPair)si.GetValue ("_StrongNameKeyPair", typeof (StrongNameKeyPair));
121                         versioncompat = (AssemblyVersionCompatibility)si.GetValue ("_VersionCompatibility", typeof (AssemblyVersionCompatibility));
122                         flags = (AssemblyNameFlags)si.GetValue ("_Flags", typeof (AssemblyNameFlags));
123                         int lcid = si.GetInt32 ("_CultureInfo");
124                         if (lcid != -1) cultureinfo = new CultureInfo (lcid);
125                 }
126
127                 public string Name {
128                         get { return name; }
129                         set { name = value; }
130                 }
131
132                 public string CodeBase {
133                         get { return codebase; }
134                         set { codebase = value; }
135                 }
136
137                 public string EscapedCodeBase {
138                         get {
139                                 if (codebase == null)
140                                         return null;
141                                 return Uri.EscapeString (codebase, false, true, true);
142                         }
143                 }
144
145                 public CultureInfo CultureInfo {
146                         get { return cultureinfo; }
147                         set { cultureinfo = value; }
148                 }
149
150                 public AssemblyNameFlags Flags {
151                         get { return flags; }
152                         set { flags = value; }
153                 }
154
155                 public string FullName {
156                         get {
157                                 if (name == null)
158                                         return null;
159                                 StringBuilder fname = new StringBuilder ();
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 = GetPublicKeyToken ();
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                                 return fname.ToString ();
183                         }
184                 }
185
186                 public AssemblyHashAlgorithm HashAlgorithm {
187                         get { return hashalg; }
188                         set { hashalg = value; }
189                 }
190
191                 public StrongNameKeyPair KeyPair {
192                         get { return keypair; }
193                         set { keypair = value; }
194                 }
195
196                 public Version Version {
197                         get {
198                                 return version;
199                         }
200
201                         set {
202                                 version = value;
203                                 if (value == null)
204                                         major = minor = build = revision = 0;
205                                 else {
206                                         major = value.Major;
207                                         minor = value.Minor;
208                                         build = value.Build;
209                                         revision = value.Revision;
210                                 }
211                         }
212                 }
213
214                 public AssemblyVersionCompatibility VersionCompatibility {
215                         get { return versioncompat; }
216                         set { versioncompat = value; }
217                 }
218                 
219                 public override string ToString ()
220                 {
221                         string name = FullName;
222                         return (name != null) ? name : base.ToString ();
223                 }
224
225                 public byte[] GetPublicKey() 
226                 {
227                         return publicKey;
228                         // FIXME: In some cases MS implementation returns 
229                         // "new byte [0]" instead of null
230                 }
231
232                 public byte[] GetPublicKeyToken() 
233                 {
234                         if (keyToken != null)
235                                 return keyToken;
236                         else if (publicKey == null)
237                                 return null;
238                         else {
239                                 HashAlgorithm ha = null;
240                                 switch (hashalg) {
241                                         case AssemblyHashAlgorithm.MD5:
242                                                 ha = MD5.Create ();
243                                                 break;
244                                         default:
245                                                 // None default to SHA1
246                                                 ha = SHA1.Create ();
247                                                 break;
248                                 }
249                                 byte[] hash = ha.ComputeHash (publicKey);
250                                 // we need the last 8 bytes in reverse order
251                                 keyToken = new byte [8];
252                                 Array.Copy (hash, (hash.Length - 8), keyToken, 0, 8);
253                                 Array.Reverse (keyToken, 0, 8);
254                                 return keyToken;
255                         }
256                 }
257
258                 public void SetPublicKey (byte[] publicKey) 
259                 {
260                         flags = AssemblyNameFlags.PublicKey;
261                         this.publicKey = publicKey;
262                 }
263
264                 public void SetPublicKeyToken (byte[] publicKeyToken) 
265                 {
266                         keyToken = publicKeyToken;
267                 }
268
269                 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
270                 public void GetObjectData (SerializationInfo info, StreamingContext context)
271                 {
272                         if (info == null)
273                                 throw new ArgumentNullException ("info");
274
275                         info.AddValue ("_Name", name);
276                         info.AddValue ("_PublicKey", publicKey);
277                         info.AddValue ("_PublicKeyToken", keyToken);
278                         info.AddValue ("_CultureInfo", cultureinfo != null ? cultureinfo.LCID : -1);
279                         info.AddValue ("_CodeBase", codebase);
280                         info.AddValue ("_Version", Version);
281                         info.AddValue ("_HashAlgorithm", hashalg);
282                         info.AddValue ("_HashAlgorithmForControl", AssemblyHashAlgorithm.None);
283                         info.AddValue ("_StrongNameKeyPair", keypair);
284                         info.AddValue ("_VersionCompatibility", versioncompat);
285                         info.AddValue ("_Flags", flags);
286                         info.AddValue ("_HashForControl", null);
287                 }
288
289                 public object Clone() 
290                 {
291                         AssemblyName an = new AssemblyName ();
292                         an.name = name;
293                         an.codebase = codebase;
294                         an.major = major;
295                         an.minor = minor;
296                         an.build = build;
297                         an.revision = revision;
298                         an.version = version;
299                         an.cultureinfo = cultureinfo;
300                         an.flags = flags;
301                         an.hashalg = hashalg;
302                         an.keypair = keypair;
303                         an.publicKey = publicKey;
304                         an.keyToken = keyToken;
305                         an.versioncompat = versioncompat;
306                         return an;
307                 }
308
309                 public void OnDeserialization (object sender) 
310                 {
311                         Version = version;
312                 }
313
314                 public static AssemblyName GetAssemblyName (string assemblyFile) 
315                 {
316                         if (assemblyFile == null)
317                                 throw new ArgumentNullException ("assemblyFile");
318
319                         AssemblyName aname = new AssemblyName ();
320                         Assembly.InternalGetAssemblyName (Path.GetFullPath (assemblyFile), aname);
321                         return aname;
322                 }
323
324 #if NET_1_1
325                 void _AssemblyName.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
326                 {
327                         throw new NotImplementedException ();
328                 }
329
330                 void _AssemblyName.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
331                 {
332                         throw new NotImplementedException ();
333                 }
334
335                 void _AssemblyName.GetTypeInfoCount (out uint pcTInfo)
336                 {
337                         throw new NotImplementedException ();
338                 }
339
340                 void _AssemblyName.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
341                         IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
342                 {
343                         throw new NotImplementedException ();
344                 }
345 #endif
346         }
347 }