2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / System.Reflection.Emit / AssemblyBuilder.cs
index 106d607b8fb0d22fab10435088d60be37c21d47e..b4dacd032be0d94e7909a25122020a64a42591e5 100755 (executable)
@@ -7,6 +7,29 @@
 // (C) 2001 Ximian, Inc.  http://www.ximian.com
 //
 
+//
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
 using System;
 using System.Reflection;
 using System.Resources;
@@ -17,6 +40,7 @@ using System.Globalization;
 using System.Runtime.CompilerServices;
 using System.Collections;
 using System.Runtime.InteropServices;
+using System.Security;
 using System.Security.Cryptography;
 using System.Security.Permissions;
 
@@ -76,6 +100,12 @@ namespace System.Reflection.Emit {
                uint access;
                Module[] loaded_modules;
                MonoWin32Resource[] win32_resources;
+               private RefEmitPermissionSet[] permissions_minimum;
+               private RefEmitPermissionSet[] permissions_optional;
+               private RefEmitPermissionSet[] permissions_refused;
+               PortableExecutableKind peKind;
+               ImageFileMachine machine;
+               bool corlib_internal;
                #endregion
                internal Type corlib_object_type = typeof (System.Object);
                internal Type corlib_value_type = typeof (System.ValueType);
@@ -90,7 +120,7 @@ namespace System.Reflection.Emit {
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                private static extern void basic_init (AssemblyBuilder ab);
                
-               internal AssemblyBuilder (AssemblyName n, string directory, AssemblyBuilderAccess access) {
+               internal AssemblyBuilder (AssemblyName n, string directory, AssemblyBuilderAccess access, bool corlib_internal) {
                        name = n.Name;
                        if (directory == null || directory == String.Empty)
                                dir = Directory.GetCurrentDirectory ();
@@ -108,8 +138,18 @@ namespace System.Reflection.Emit {
                        }
 
                        if (n.KeyPair != null) {
+                               // full keypair is available (for signing)
                                sn = n.KeyPair.StrongName ();
                        }
+                       else {
+                               // public key is available (for delay-signing)
+                               byte[] pk = n.GetPublicKey ();
+                               if ((pk != null) && (pk.Length > 0)) {
+                                       sn = new Mono.Security.StrongName (pk);
+                               }
+                       }
+
+                       this.corlib_internal = corlib_internal;
 
                        basic_init (this);
                }
@@ -173,12 +213,45 @@ namespace System.Reflection.Emit {
                        resources [p].attrs = attribute;
                }
 
-               public void EmbedResourceFile (string name, string fileName)
+               /// <summary>
+               /// Don't change the method name and parameters order. It is used by mcs 
+               /// </summary>
+               internal void AddPermissionRequests (PermissionSet required, PermissionSet optional, PermissionSet refused)
+               {
+                       if (created)
+                               throw new InvalidOperationException ("Assembly was already saved.");
+
+                       // required for base Assembly class (so the permissions
+                       // can be used even if the assembly isn't saved to disk)
+                       _minimum = required;
+                       _optional = optional;
+                       _refuse = refused;
+
+                       // required to reuse AddDeclarativeSecurity support 
+                       // already present in the runtime
+                       if (required != null) {
+                               permissions_minimum = new RefEmitPermissionSet [1];
+                               permissions_minimum [0] = new RefEmitPermissionSet (
+                                       SecurityAction.RequestMinimum, required.ToXml ().ToString ());
+                       }
+                       if (optional != null) {
+                               permissions_optional = new RefEmitPermissionSet [1];
+                               permissions_optional [0] = new RefEmitPermissionSet (
+                                       SecurityAction.RequestOptional, optional.ToXml ().ToString ());
+                       }
+                       if (refused != null) {
+                               permissions_refused = new RefEmitPermissionSet [1];
+                               permissions_refused [0] = new RefEmitPermissionSet (
+                                       SecurityAction.RequestRefuse, refused.ToXml ().ToString ());
+                       }
+               }
+
+               internal void EmbedResourceFile (string name, string fileName)
                {
                        EmbedResourceFile (name, fileName, ResourceAttributes.Public);
                }
 
-               public void EmbedResourceFile (string name, string fileName, ResourceAttributes attribute)
+               internal void EmbedResourceFile (string name, string fileName, ResourceAttributes attribute)
                {
                        if (resources != null) {
                                MonoResource[] new_r = new MonoResource [resources.Length + 1];
@@ -381,7 +454,7 @@ namespace System.Reflection.Emit {
                                        else if (attrname == "System.Reflection.AssemblyTrademarkAttribute")
                                                version_res.LegalTrademarks = cb.string_arg ();
                                        else if (attrname == "System.Reflection.AssemblyCultureAttribute")
-                                               version_res.FileLanguage = new CultureInfo (cb.string_arg ()).LCID;
+                                               version_res.FileLanguage = new CultureInfo (GetCultureString (cb.string_arg ())).LCID;
                                        else if (attrname == "System.Reflection.AssemblyFileVersionAttribute")
                                                version_res.FileVersion = cb.string_arg ();
                                        else if (attrname == "System.Reflection.AssemblyInformationalVersionAttribute")
@@ -475,10 +548,6 @@ namespace System.Reflection.Emit {
                        throw not_supported ();
                }
 
-               public override FileStream[] GetFiles() {
-                       throw not_supported ();
-               }
-               
                public override FileStream[] GetFiles(bool getResourceModules) {
                        throw not_supported ();
                }
@@ -524,8 +593,16 @@ namespace System.Reflection.Emit {
                        }
                }
 
-               public void Save (string assemblyFileName)
+#if NET_2_0
+               public 
+#else
+               internal
+#endif
+               void Save (string assemblyFileName, PortableExecutableKind portableExecutableKind, ImageFileMachine imageFileMachine)
                {
+                       this.peKind = portableExecutableKind;
+                       this.machine = imageFileMachine;
+
                        if (resource_writers != null) {
                                foreach (IResourceWriter writer in resource_writers) {
                                        writer.Generate ();
@@ -591,6 +668,11 @@ namespace System.Reflection.Emit {
                        created = true;
                }
 
+               public void Save (string assemblyFileName)
+               {
+                       Save (assemblyFileName, PortableExecutableKind.ILOnly, ImageFileMachine.I386);
+               }
+
                public void SetEntryPoint (MethodInfo entryMethod)
                {
                        SetEntryPoint (entryMethod, PEFileKinds.ConsoleApplication);
@@ -620,7 +702,7 @@ namespace System.Reflection.Emit {
                                version = create_assembly_version (customBuilder.string_arg ());
                                return;
                        } else if (attrname == "System.Reflection.AssemblyCultureAttribute") {
-                               culture = customBuilder.string_arg ();
+                               culture = GetCultureString (customBuilder.string_arg ());
                        } else if (attrname == "System.Reflection.AssemblyAlgorithmIdAttribute") {
                                data = customBuilder.Data;
                                pos = 2;
@@ -658,13 +740,13 @@ namespace System.Reflection.Emit {
                        SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
                }
 
-               public void SetCorlibTypeBuilders (Type corlib_object_type, Type corlib_value_type, Type corlib_enum_type) {
+               internal void SetCorlibTypeBuilders (Type corlib_object_type, Type corlib_value_type, Type corlib_enum_type) {
                        this.corlib_object_type = corlib_object_type;
                        this.corlib_value_type = corlib_value_type;
                        this.corlib_enum_type = corlib_enum_type;
                }
 
-               public void SetCorlibTypeBuilders (Type corlib_object_type, Type corlib_value_type, Type corlib_enum_type, Type corlib_void_type)
+               internal void SetCorlibTypeBuilders (Type corlib_object_type, Type corlib_value_type, Type corlib_enum_type, Type corlib_void_type)
                {
                        SetCorlibTypeBuilders (corlib_object_type, corlib_value_type, corlib_enum_type);
                        this.corlib_void_type = corlib_void_type;
@@ -751,5 +833,10 @@ namespace System.Reflection.Emit {
 
                        return ver [0] + "." + ver [1] + "." + ver [2] + "." + ver [3];
                }
+
+               private string GetCultureString (string str)
+               {
+                       return (str == "neutral" ? String.Empty : str);
+               }
        }
 }