2003-11-06 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / System.Reflection.Emit / AssemblyBuilder.cs
index e2b71189c387b35f5a0a5971a7627a4e02c4f3e2..144b7133a1d33efb30e17300851641970cd1c1a7 100755 (executable)
@@ -17,6 +17,10 @@ using System.Globalization;
 using System.Runtime.CompilerServices;
 using System.Collections;
 using System.Runtime.InteropServices;
+using System.Security.Cryptography;
+
+using Mono.Security;
+using Mono.Security.Cryptography;
 
 namespace System.Reflection.Emit {
 
@@ -35,7 +39,7 @@ namespace System.Reflection.Emit {
                private string dir;
                private CustomAttributeBuilder[] cattrs;
                private MonoResource[] resources;
-               string keyfile;
+               byte[] public_key;
                string version;
                string culture;
                uint algid;
@@ -47,8 +51,6 @@ namespace System.Reflection.Emit {
                internal Type corlib_value_type = typeof (System.ValueType);
                internal Type corlib_enum_type = typeof (System.Enum);
                internal Type corlib_void_type = typeof (void);
-               private int[] table_indexes;
-               Hashtable us_string_cache = new Hashtable ();
                ArrayList resource_writers = null;
                bool created;
 
@@ -72,20 +74,6 @@ namespace System.Reflection.Emit {
                        basic_init (this);
                }
 
-               internal int get_next_table_index (object obj, int table, bool inc) {
-                       if (table_indexes == null) {
-                               table_indexes = new int [64];
-                               for (int i=0; i < 64; ++i)
-                                       table_indexes [i] = 1;
-                               /* allow room for .<Module> in TypeDef table */
-                               table_indexes [0x02] = 2;
-                       }
-                       // Console.WriteLine ("getindex for table "+table.ToString()+" got "+table_indexes [table].ToString());
-                       if (inc)
-                               return table_indexes [table]++;
-                       return table_indexes [table];
-               }
-
                public override string CodeBase {
                        get {
                                throw not_supported ();
@@ -214,7 +202,7 @@ namespace System.Reflection.Emit {
                                        throw new InvalidOperationException ("Assembly was already saved.");
                        }
 
-                       ModuleBuilder r = new ModuleBuilder (this, name, fileName, emitSymbolInfo, modules == null, transient);
+                       ModuleBuilder r = new ModuleBuilder (this, name, fileName, emitSymbolInfo, transient);
 
                        if (modules != null) {
                                ModuleBuilder[] new_modules = new ModuleBuilder [modules.Length + 1];
@@ -329,43 +317,20 @@ namespace System.Reflection.Emit {
                        throw not_supported ();
                }
 
-               [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               private static extern int getUSIndex (AssemblyBuilder ab, string str);
-
-               [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               private static extern int getToken (AssemblyBuilder ab, object obj);
-
-               internal int GetToken (string str) {
-                       if (us_string_cache.Contains (str))
-                               return (int)us_string_cache [str];
-                       int result = getUSIndex (this, str);
-                       us_string_cache [str] = result;
-                       return result;
-               }
-               
-               internal int GetToken (MemberInfo member) {
-                       return getToken (this, member);
-               }
-
-               internal int GetToken (SignatureHelper helper) {
-                       return getToken (this, helper);
-               }
-
                internal bool IsSave {
                        get {
                                return access != (uint)AssemblyBuilderAccess.Run;
                        }
                }
 
-               [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               private static extern int getDataChunk (AssemblyBuilder ab, byte[] buf, int offset);
+               internal string AssemblyDir {
+                       get {
+                               return dir;
+                       }
+               }
 
                public void Save (string assemblyFileName)
                {
-                       byte[] buf = new byte [65536];
-                       FileStream file;
-                       int count, offset;
-
                        if (resource_writers != null) {
                                foreach (IResourceWriter writer in resource_writers) {
                                        writer.Generate ();
@@ -373,35 +338,20 @@ namespace System.Reflection.Emit {
                                }
                        }
 
-                       build_metadata (this);
-
+                       // Create a main module if not already created
+                       ModuleBuilder mainModule = null;
                        foreach (ModuleBuilder module in modules)
-                               module.Save ();
+                               if (module.FullyQualifiedName == assemblyFileName)
+                                       mainModule = module;
+                       if (mainModule == null)
+                               mainModule = DefineDynamicModule ("RefEmit_OnDiskManifestModule", assemblyFileName);
 
-                       if (dir != null) {
-                               assemblyFileName = String.Format ("{0}{1}{2}", dir, System.IO.Path.DirectorySeparatorChar, assemblyFileName);
-                       }
-
-                       file = new FileStream (assemblyFileName, FileMode.Create, FileAccess.Write);
-
-                       offset = 0;
-                       while ((count = getDataChunk (this, buf, offset)) != 0) {
-                               file.Write (buf, 0, count);
-                               offset += count;
-                       }
-                       file.Close ();
-
-                       created = true;
+                       mainModule.IsMain = true;
 
-                       //
-                       // The constant 0x80000000 is internal to Mono, it means `make executable'
-                       //
-                       File.SetAttributes (assemblyFileName, (FileAttributes) (unchecked ((int) 0x80000000)));
+                       foreach (ModuleBuilder module in modules)
+                               module.Save ();
                }
 
-               [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               private static extern void build_metadata (AssemblyBuilder ab);
-
                public void SetEntryPoint (MethodInfo entryMethod)
                {
                        SetEntryPoint (entryMethod, PEFileKinds.ConsoleApplication);
@@ -418,13 +368,15 @@ namespace System.Reflection.Emit {
                        pekind = fileKind;
                }
 
-               public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
+               public void SetCustomAttribute( CustomAttributeBuilder customBuilder) 
+               {
                        if (customBuilder == null)
                                throw new ArgumentNullException ("customBuilder");
 
                        string attrname = customBuilder.Ctor.ReflectedType.FullName;
                        byte[] data;
                        int len, pos;
+                       Mono.Security.StrongName sn;
                        if (attrname == "System.Reflection.AssemblyVersionAttribute") {
                                data = customBuilder.Data;
                                pos = 2;
@@ -435,7 +387,33 @@ namespace System.Reflection.Emit {
                                data = customBuilder.Data;
                                pos = 2;
                                len = CustomAttributeBuilder.decode_len (data, pos, out pos);
-                               keyfile = CustomAttributeBuilder.string_from_bytes (data, pos, len);
+                               string keyfile_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
+                               if (keyfile_name == String.Empty)
+                                       return;
+                               using (FileStream fs = new FileStream (keyfile_name, FileMode.Open)) {
+                                       byte[] snkeypair = new byte [fs.Length];
+                                       fs.Read (snkeypair, 0, snkeypair.Length);
+
+                                       // this will import public or private/public keys
+                                       RSA rsa = CryptoConvert.FromCapiKeyBlob (snkeypair);
+                                       // and export only the public part
+                                       sn = new Mono.Security.StrongName (rsa);
+                                       public_key = sn.PublicKey;
+                               }
+                               return;
+                       } else if (attrname == "System.Reflection.AssemblyKeyNameAttribute") {
+                               data = customBuilder.Data;
+                               pos = 2;
+                               len = CustomAttributeBuilder.decode_len (data, pos, out pos);
+                               string key_name = CustomAttributeBuilder.string_from_bytes (data, pos, len);
+                               if (key_name == String.Empty)
+                                       return;
+                               CspParameters csparam = new CspParameters ();
+                               csparam.KeyContainerName = key_name;
+                               RSA rsacsp = new RSACryptoServiceProvider (csparam);
+                               sn = new Mono.Security.StrongName (rsacsp);
+                               public_key = sn.PublicKey;
+                               return;
                        } else if (attrname == "System.Reflection.AssemblyCultureAttribute") {
                                data = customBuilder.Data;
                                pos = 2;
@@ -537,6 +515,11 @@ namespace System.Reflection.Emit {
                                                throw new ArgumentException ("Duplicate name '" + name + "'");
                                }
                        }
-               }                       
+               }
+
+               // Same as in Mono.Security/Mono>Security.Cryptography/CryptoConvert.cs
+
+
+
        }
 }