42b32953736273ff92d03d6e24f8f374b9bbfd24
[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 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;
33 using System.Configuration.Assemblies;
34 using System.Globalization;
35 using System.Reflection;
36 using System.Runtime.Serialization;
37 using System.Security.Cryptography;
38 using System.Text;
39 using System.Runtime.InteropServices;
40 using System.Runtime.CompilerServices;
41
42 namespace System.Reflection {
43
44 // References:
45 // a.   Uniform Resource Identifiers (URI): Generic Syntax
46 //      http://www.ietf.org/rfc/rfc2396.txt
47
48         [Serializable]
49         [MonoTODO ("Fix serialization compatibility with MS.NET")]
50         public sealed class AssemblyName  : ICloneable, ISerializable, IDeserializationCallback {
51                 string name;
52                 string codebase;
53                 int major, minor, build, revision;
54                 CultureInfo cultureinfo;
55                 AssemblyNameFlags flags;
56                 AssemblyHashAlgorithm hashalg;
57                 StrongNameKeyPair keypair;
58                 byte[] publicKey;
59                 byte[] keyToken;
60                 AssemblyVersionCompatibility versioncompat;
61                 Version version;
62                 
63                 public AssemblyName ()
64                 {
65                         // defaults
66                         versioncompat = AssemblyVersionCompatibility.SameMachine;
67                 }
68
69 #if NET_2_0
70                 public AssemblyName (string assemblyName)
71                 {
72                         name = assemblyName;
73                 }
74 #endif
75
76                 internal AssemblyName (SerializationInfo si, StreamingContext sc)
77                 {
78                         name = si.GetString ("_Name");
79                         codebase = si.GetString ("_CodeBase");
80                         version = (Version)si.GetValue ("_Version", typeof (Version));
81                         publicKey = (byte[])si.GetValue ("_PublicKey", typeof (byte[]));
82                         keyToken = (byte[])si.GetValue ("_PublicToken", typeof (byte[]));
83                         hashalg = (AssemblyHashAlgorithm)si.GetValue ("_HashAlgorithm", typeof (AssemblyHashAlgorithm));
84                         keypair = (StrongNameKeyPair)si.GetValue ("_StrongNameKeyPair", typeof (StrongNameKeyPair));
85                         versioncompat = (AssemblyVersionCompatibility)si.GetValue ("_VersionCompatibility", typeof (AssemblyVersionCompatibility));
86                         flags = (AssemblyNameFlags)si.GetValue ("_Flags", typeof (AssemblyNameFlags));
87                         int lcid = si.GetInt32 ("_CultureInfo");
88                         if (lcid != -1) cultureinfo = new CultureInfo (lcid);
89                 }
90
91                 public string Name {
92                         get { return name; }
93                         set { name = value; }
94                 }
95
96                 public string CodeBase {
97                         get { return codebase; }
98                         set { codebase = value; }
99                 }
100
101                 [MonoTODO("RFC 2396")]
102                 private string Escape (string url) 
103                 {
104                         // we already have code in mcs\class\System\System\Uri.cs
105                         // but Uri class ins't part of corlib !
106                         // TODO
107                         return url;
108                 }
109
110                 public string EscapedCodeBase {
111                         get { return Escape (codebase); }
112                 }
113
114                 public CultureInfo CultureInfo {
115                         get { return cultureinfo; }
116                         set { cultureinfo = value; }
117                 }
118
119                 public AssemblyNameFlags Flags {
120                         get { return flags; }
121                         set { flags = value; }
122                 }
123
124                 public string FullName {
125                         get {
126                                 if (name == null)
127                                         return null;
128                                 StringBuilder fname = new StringBuilder ();
129                                 fname.Append (name);
130                                 if (Version.ToString () != "0.0.0.0") {
131                                         fname.Append (", Version=");
132                                         fname.Append (Version.ToString ());
133                                 }
134                                 if (cultureinfo != null) {
135                                         fname.Append (", Culture=");
136                                         if (cultureinfo.LCID == CultureInfo.InvariantCulture.LCID)
137                                                 fname.Append ("neutral");
138                                         else
139                                                 fname.Append (cultureinfo.Name);
140                                 }
141                                 byte[] pub_tok = GetPublicKeyToken ();
142                                 if (pub_tok != null) {
143                                         if (pub_tok.Length == 0)
144                                                 fname.Append (", PublicKeyToken=null");
145                                         else {
146                                                 fname.Append (", PublicKeyToken=");
147                                                 for (int i = 0; i < pub_tok.Length; i++)
148                                                         fname.Append (pub_tok[i].ToString ("x2"));
149                                         }
150                                 }
151                                 return fname.ToString ();
152                         }
153                 }
154
155                 public AssemblyHashAlgorithm HashAlgorithm {
156                         get { return hashalg; }
157                         set { hashalg = value; }
158                 }
159
160                 public StrongNameKeyPair KeyPair {
161                         get { return keypair; }
162                         set { keypair = value; }
163                 }
164
165                 public Version Version {
166                         get {
167                                 if (version != null) return version;
168                                 
169                                 if (name == null)
170                                         return null;
171                                 if (build == -1)
172                                         version = new Version (major, minor);
173                                 else
174                                         if (revision == -1)
175                                                 version = new Version (major, minor, build);
176                                 else
177                                         version = new Version (major, minor, build, revision);
178
179                                 return version;
180                         }
181
182                         set {
183                                 major = value.Major;
184                                 minor = value.Minor;
185                                 build = value.Build;
186                                 revision = value.Revision;
187                                 version = value;
188                         }
189                 }
190
191                 public AssemblyVersionCompatibility VersionCompatibility {
192                         get { return versioncompat; }
193                         set { versioncompat = value; }
194                 }
195                 
196                 public override string ToString ()
197                 {
198                         string name = FullName;
199                         return (name != null) ? name : base.ToString ();
200                 }
201
202                 public byte[] GetPublicKey() 
203                 {
204                         // to match MS implementation -- funny one
205                         if (publicKey != null)
206                                 return publicKey;
207                         else if (name == null)
208                                 return null;
209                         else
210                                 return new byte [0];
211                 }
212
213                 public byte[] GetPublicKeyToken() 
214                 {
215                         if (keyToken != null)
216                                 return keyToken;
217                         else if (publicKey == null)
218                                 return null;
219                         else {
220                                 HashAlgorithm ha = null;
221                                 switch (hashalg) {
222                                         case AssemblyHashAlgorithm.MD5:
223                                                 ha = MD5.Create ();
224                                                 break;
225                                         default:
226                                                 // None default to SHA1
227                                                 ha = SHA1.Create ();
228                                                 break;
229                                 }
230                                 byte[] hash = ha.ComputeHash (publicKey);
231                                 // we need the last 8 bytes in reverse order
232                                 keyToken = new byte [8];
233                                 Array.Copy (hash, (hash.Length - 8), keyToken, 0, 8);
234                                 Array.Reverse (keyToken, 0, 8);
235                                 return keyToken;
236                         }
237                 }
238
239                 public void SetPublicKey (byte[] publicKey) 
240                 {
241                         flags = AssemblyNameFlags.PublicKey;
242                         this.publicKey = publicKey;
243                 }
244
245                 public void SetPublicKeyToken (byte[] publicKeyToken) 
246                 {
247                         keyToken = publicKeyToken;
248                 }
249
250                 public void GetObjectData (SerializationInfo info, StreamingContext context)
251                 {
252                         info.AddValue ("_Name", name);
253                         info.AddValue ("_PublicKey", publicKey);
254                         info.AddValue ("_PublicToken", keyToken);
255                         info.AddValue ("_CultureInfo", cultureinfo != null ? cultureinfo.LCID : -1);
256                         info.AddValue ("_CodeBase", codebase);
257                         info.AddValue ("_Version", Version);
258                         info.AddValue ("_HashAlgorithm", hashalg);
259                         info.AddValue ("_HashAlgorithmForControl", AssemblyHashAlgorithm.None);
260                         info.AddValue ("_StrongNameKeyPair", keypair);
261                         info.AddValue ("_VersionCompatibility", versioncompat);
262                         info.AddValue ("_Flags", flags);
263                         info.AddValue ("_HashForControl", null);
264                 }
265
266                 public object Clone() 
267                 {
268                         AssemblyName an = new AssemblyName ();
269                         an.name = name;
270                         an.codebase = codebase;
271                         an.major = major;
272                         an.minor = minor;
273                         an.build = build;
274                         an.revision = revision;
275                         an.cultureinfo = cultureinfo;
276                         an.flags = flags;
277                         an.hashalg = hashalg;
278                         an.keypair = keypair;
279                         an.publicKey = publicKey;
280                         an.keyToken = keyToken;
281                         an.versioncompat = versioncompat;
282                         return an;
283                 }
284
285                 public void OnDeserialization (object sender) 
286                 {
287                         Version = version;
288                 }
289
290                 public static AssemblyName GetAssemblyName (string assemblyFile) 
291                 {
292                         if (assemblyFile == null)
293                                 throw new ArgumentNullException ("assemblyFile");
294
295                         AssemblyName aname = new AssemblyName ();
296                         Assembly.InternalGetAssemblyName (System.IO.Path.GetFullPath (assemblyFile), aname);
297                         return aname;
298                 }
299         }
300 }