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 {
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;
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 ();
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];
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 ();
}
}
- build_metadata (this);
-
+ // Create a main module if not already created
+ ModuleBuilder mainModule = null;
foreach (ModuleBuilder module in modules)
- module.Save ();
-
- if (dir != null) {
- assemblyFileName = String.Format ("{0}{1}{2}", dir, System.IO.Path.DirectorySeparatorChar, assemblyFileName);
- }
+ if (module.FullyQualifiedName == assemblyFileName)
+ mainModule = module;
+ if (mainModule == null)
+ mainModule = DefineDynamicModule ("RefEmit_OnDiskManifestModule", assemblyFileName);
- file = new FileStream (assemblyFileName, FileMode.Create, FileAccess.Write);
+ mainModule.IsMain = true;
- offset = 0;
- while ((count = getDataChunk (this, buf, offset)) != 0) {
- file.Write (buf, 0, count);
- offset += count;
- }
- file.Close ();
-
- created = 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);
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;
byte[] snkeypair = new byte [fs.Length];
fs.Read (snkeypair, 0, snkeypair.Length);
- /* FIXME: Extract public key from the keypair */
- public_key = snkeypair;
+ // 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;