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