copying the latest Sys.Web.Services from trunk.
[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         [Serializable]
50         [MonoTODO ("Fix serialization compatibility with MS.NET")]
51         public sealed class AssemblyName  : ICloneable, ISerializable, IDeserializationCallback {
52                 string name;
53                 string codebase;
54                 int major, minor, build, revision;
55                 CultureInfo cultureinfo;
56                 AssemblyNameFlags flags;
57                 AssemblyHashAlgorithm hashalg;
58                 StrongNameKeyPair keypair;
59                 byte[] publicKey;
60                 byte[] keyToken;
61                 AssemblyVersionCompatibility versioncompat;
62                 Version version;
63                 
64                 public AssemblyName ()
65                 {
66                         // defaults
67                         versioncompat = AssemblyVersionCompatibility.SameMachine;
68                 }
69
70 #if NET_2_0
71                 public AssemblyName (string assemblyName)
72                 {
73                         name = assemblyName;
74                 }
75 #endif
76
77                 internal AssemblyName (SerializationInfo si, StreamingContext sc)
78                 {
79                         name = si.GetString ("_Name");
80                         codebase = si.GetString ("_CodeBase");
81                         version = (Version)si.GetValue ("_Version", typeof (Version));
82                         publicKey = (byte[])si.GetValue ("_PublicKey", typeof (byte[]));
83                         keyToken = (byte[])si.GetValue ("_PublicToken", typeof (byte[]));
84                         hashalg = (AssemblyHashAlgorithm)si.GetValue ("_HashAlgorithm", typeof (AssemblyHashAlgorithm));
85                         keypair = (StrongNameKeyPair)si.GetValue ("_StrongNameKeyPair", typeof (StrongNameKeyPair));
86                         versioncompat = (AssemblyVersionCompatibility)si.GetValue ("_VersionCompatibility", typeof (AssemblyVersionCompatibility));
87                         flags = (AssemblyNameFlags)si.GetValue ("_Flags", typeof (AssemblyNameFlags));
88                         int lcid = si.GetInt32 ("_CultureInfo");
89                         if (lcid != -1) cultureinfo = new CultureInfo (lcid);
90                 }
91
92                 public string Name {
93                         get { return name; }
94                         set { name = value; }
95                 }
96
97                 public string CodeBase {
98                         get { return codebase; }
99                         set { codebase = value; }
100                 }
101
102                 public string EscapedCodeBase {
103                         get {
104                                 if (codebase == null)
105                                         return null;
106                                 return Uri.EscapeString (codebase, false, true, true);
107                         }
108                 }
109
110                 public CultureInfo CultureInfo {
111                         get { return cultureinfo; }
112                         set { cultureinfo = value; }
113                 }
114
115                 public AssemblyNameFlags Flags {
116                         get { return flags; }
117                         set { flags = value; }
118                 }
119
120                 public string FullName {
121                         get {
122                                 if (name == null)
123                                         return null;
124                                 StringBuilder fname = new StringBuilder ();
125                                 fname.Append (name);
126                                 if (Version.ToString () != "0.0.0.0") {
127                                         fname.Append (", Version=");
128                                         fname.Append (Version.ToString ());
129                                 }
130                                 if (cultureinfo != null) {
131                                         fname.Append (", Culture=");
132                                         if (cultureinfo.LCID == CultureInfo.InvariantCulture.LCID)
133                                                 fname.Append ("neutral");
134                                         else
135                                                 fname.Append (cultureinfo.Name);
136                                 }
137                                 byte[] pub_tok = GetPublicKeyToken ();
138                                 if (pub_tok != null) {
139                                         if (pub_tok.Length == 0)
140                                                 fname.Append (", PublicKeyToken=null");
141                                         else {
142                                                 fname.Append (", PublicKeyToken=");
143                                                 for (int i = 0; i < pub_tok.Length; i++)
144                                                         fname.Append (pub_tok[i].ToString ("x2"));
145                                         }
146                                 }
147                                 return fname.ToString ();
148                         }
149                 }
150
151                 public AssemblyHashAlgorithm HashAlgorithm {
152                         get { return hashalg; }
153                         set { hashalg = value; }
154                 }
155
156                 public StrongNameKeyPair KeyPair {
157                         get { return keypair; }
158                         set { keypair = value; }
159                 }
160
161                 public Version Version {
162                         get {
163                                 if (version != null) return version;
164                                 
165                                 if (name == null)
166                                         return null;
167                                 if (build == -1)
168                                         version = new Version (major, minor);
169                                 else
170                                         if (revision == -1)
171                                                 version = new Version (major, minor, build);
172                                 else
173                                         version = new Version (major, minor, build, revision);
174
175                                 return version;
176                         }
177
178                         set {
179                                 major = value.Major;
180                                 minor = value.Minor;
181                                 build = value.Build;
182                                 revision = value.Revision;
183                                 version = value;
184                         }
185                 }
186
187                 public AssemblyVersionCompatibility VersionCompatibility {
188                         get { return versioncompat; }
189                         set { versioncompat = value; }
190                 }
191                 
192                 public override string ToString ()
193                 {
194                         string name = FullName;
195                         return (name != null) ? name : base.ToString ();
196                 }
197
198                 public byte[] GetPublicKey() 
199                 {
200                         // to match MS implementation -- funny one
201                         if (publicKey != null)
202                                 return publicKey;
203                         else if (name == null)
204                                 return null;
205                         else
206                                 return new byte [0];
207                 }
208
209                 public byte[] GetPublicKeyToken() 
210                 {
211                         if (keyToken != null)
212                                 return keyToken;
213                         else if (publicKey == null)
214                                 return null;
215                         else {
216                                 HashAlgorithm ha = null;
217                                 switch (hashalg) {
218                                         case AssemblyHashAlgorithm.MD5:
219                                                 ha = MD5.Create ();
220                                                 break;
221                                         default:
222                                                 // None default to SHA1
223                                                 ha = SHA1.Create ();
224                                                 break;
225                                 }
226                                 byte[] hash = ha.ComputeHash (publicKey);
227                                 // we need the last 8 bytes in reverse order
228                                 keyToken = new byte [8];
229                                 Array.Copy (hash, (hash.Length - 8), keyToken, 0, 8);
230                                 Array.Reverse (keyToken, 0, 8);
231                                 return keyToken;
232                         }
233                 }
234
235                 public void SetPublicKey (byte[] publicKey) 
236                 {
237                         flags = AssemblyNameFlags.PublicKey;
238                         this.publicKey = publicKey;
239                 }
240
241                 public void SetPublicKeyToken (byte[] publicKeyToken) 
242                 {
243                         keyToken = publicKeyToken;
244                 }
245
246                 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
247                 public void GetObjectData (SerializationInfo info, StreamingContext context)
248                 {
249                         if (info == null)
250                                 throw new ArgumentNullException ("info");
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 }