context: /*
    Copyright (C) 2007-2011 Jeroen Frijters
  
    This software is provided 'as-is', without any express or implied
    warranty.  In no event will the authors be held liable for any damages
    arising from the use of this software.
  
    Permission is granted to anyone to use this software for any purpose,
    including commercial applications, and to alter it and redistribute it
    freely, subject to the following restrictions:
  
    1. The origin of this software must not be misrepresented; you must not
       claim that you wrote the original software. If you use this software
       in a product, an acknowledgment in the product documentation would be
       appreciated but is not required.
    2. Altered source versions must be plainly marked as such, and must not be
       misrepresented as being the original software.
    3. This notice may not be removed or altered from any source distribution.
  
    Jeroen Frijters
    jeroen@frijters.net
    
  */
  using System;
  using System.Collections.Generic;
  using System.Threading;
  using System.Reflection;
  using System.Security.Cryptography.X509Certificates;
  
  namespace IKVM.Internal
  {
  	static class VirtualFileSystem
  	{
  		internal static readonly string RootPath = JVM.IsUnix ? "/.virtual-ikvm-home/" : @"C:\.virtual-ikvm-home\";
  
  		internal static bool IsVirtualFS(string path)
  		{
  			return (path.Length == RootPath.Length - 1 && String.CompareOrdinal(path, 0, RootPath, 0, RootPath.Length - 1) == 0)
  				|| String.CompareOrdinal(path, 0, RootPath, 0, RootPath.Length) == 0;
  		}
  
  #if !FIRST_PASS
  		private static VfsDirectory root;
  
  		private abstract class VfsEntry
  		{
  		}
  
  		private abstract class VfsFile : VfsEntry
  		{
  			internal abstract long Size { get; }
  			internal abstract System.IO.Stream Open();
  		}
  
  		private class VfsDirectory : VfsEntry
  		{
  			protected readonly Dictionary<string, VfsEntry> entries = new Dictionary<string,VfsEntry>();
  
  			internal VfsDirectory AddDirectory(string name)
  			{
  				VfsDirectory dir = new VfsDirectory();
  				Add(name, dir);
  				return dir;
  			}
  
  			internal void Add(string name, VfsEntry entry)
  			{
  				lock (entries)
  				{
  					entries.Add(name, entry);
  				}
  			}
  
  			internal virtual VfsEntry GetEntry(int index, string[] path)
  			{
  				VfsEntry entry = GetEntry(path[index++]);
  				if (index == path.Length)
  				{
  					return entry;
  				}
  				else
  				{
  					VfsDirectory dir = entry as VfsDirectory;
  					if (dir == null)
  					{
  						return null;
  					}
  					return dir.GetEntry(index, path);
  				}
  			}
  
  			internal virtual VfsEntry GetEntry(string name)
  			{
  				VfsEntry entry;
  				lock (entries)
  				{
  					entries.TryGetValue(name, out entry);
  				}
  				return entry;
  			}
  
  			internal virtual string[] List()
  			{
  				lock (entries)
  				{
  					string[] list = new string[entries.Keys.Count];
  					entries.Keys.CopyTo(list, 0);
  					return list;
  				}
  			}
  		}
  
  		internal static string GetAssemblyClassesPath(Assembly asm)
  		{
  #if FIRST_PASS
  			return null;
  #else
  			// we can't use java.io.File.separatorChar here, because we're invoked by the system property setup code
  			return RootPath + "assembly" + System.IO.Path.DirectorySeparatorChar + VfsAssembliesDirectory.GetName(asm) + System.IO.Path.DirectorySeparatorChar + "classes" + System.IO.Path.DirectorySeparatorChar;
  #endif
  		}
  
  		internal static string GetAssemblyResourcesPath(Assembly asm)
  		{
  #if FIRST_PASS
  			return null;
  #else
  			return RootPath + "assembly" + System.IO.Path.DirectorySeparatorChar + VfsAssembliesDirectory.GetName(asm) + System.IO.Path.DirectorySeparatorChar + "resources" + System.IO.Path.DirectorySeparatorChar;
  #endif
  		}
  
  		private sealed class VfsAssembliesDirectory : VfsDirectory
  		{
  			internal override VfsEntry GetEntry(string name)
  			{
  				VfsEntry entry = base.GetEntry(name);
  				if (entry == null)
  				{
  					Guid guid;
  					if (TryParseGuid(name, out guid))
  					{
  						foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
  						{
  							if (asm.ManifestModule.ModuleVersionId == guid
  								&& !ReflectUtil.IsDynamicAssembly(asm))
  							{
  								return GetOrAddEntry(name, asm);
  							}
  						}
  					}
  					string assemblyName = ParseName(name);
  					if (assemblyName != null)
  					{
  						Assembly asm = null;
  						try
  						{
  							asm = Assembly.Load(assemblyName);
  						}
  						catch
  						{
  						}
  						if (asm != null
  							&& !ReflectUtil.IsDynamicAssembly(asm)
  							&& name == GetName(asm))
  						{
  							return GetOrAddEntry(name, asm);
  						}
  					}
  				}
  				return entry;
  			}
  
  			private VfsEntry GetOrAddEntry(string name, Assembly asm)
  			{
  				lock (entries)
  				{
  					VfsEntry entry;
  					if (!entries.TryGetValue(name, out entry))
  					{
  						VfsDirectory dir = new VfsDirectory();
  						dir.Add("resources", new VfsAssemblyResourcesDirectory(asm));
  						dir.Add("classes", new VfsAssemblyClassesDirectory(asm));
  						Add(name, dir);
  						entry = dir;
  					}
  					return entry;
  				}
  			}
  
  			// HACK we try to figure out if an assembly is loaded in the Load context
  			// http://blogs.msdn.com/b/suzcook/archive/2003/05/29/57143.aspx
  			private static bool IsLoadContext(Assembly asm)
  			{
  				if (asm.ReflectionOnly)
  				{
  					return false;
  				}
  
  #if NONMONOTOUCH
  				if (asm.GlobalAssemblyCache)
  				{
  					return true;
  				}
  #endif
  
  				if (ReflectUtil.IsDynamicAssembly(asm) || asm.Location == "")
  				{
  					return false;
  				}
  
  				if (System.IO.Path.GetDirectoryName(asm.Location) == System.IO.Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory))
  				{
  					// this is an optimization for the common case were the assembly was loaded from the app directory
  					return true;
  				}
  
  				try
  				{
  					if (Assembly.Load(asm.FullName) == asm)
  					{
  						return true;
  					}
  				}
  				catch
  				{
  				}
  
  				return false;
  			}
  
  			internal static string GetName(Assembly asm)
  			{
  				if (!IsLoadContext(asm))
  				{
  					return asm.ManifestModule.ModuleVersionId.ToString("N");
  				}
  				AssemblyName name = asm.GetName();
  				System.Text.StringBuilder sb = new System.Text.StringBuilder();
  				string simpleName = name.Name;
  				for (int i = 0; i < simpleName.Length; i++)
  				{
  					if (simpleName[i] == '_')
  					{
  						sb.Append("_!");
  					}
  					else
  					{
  						sb.Append(simpleName[i]);
  					}
  				}
  				byte[] publicKeyToken = name.GetPublicKeyToken();
  				if (publicKeyToken != null && publicKeyToken.Length != 0)
  				{
  					sb.Append("__").Append(name.Version).Append("__");
  					for (int i = 0; i < publicKeyToken.Length; i++)
  					{
  						sb.AppendFormat("{0:x2}", publicKeyToken[i]);
  					}
  				}
  				if (name.CultureInfo != null && !string.IsNullOrEmpty(name.CultureInfo.Name))
  				{
  					sb.Append("__").Append(name.CultureInfo.Name);
  				}
  				return sb.ToString();
  			}
  
  			private static bool TryParseGuid(string name, out Guid guid)
  			{
  				if (name.Length != 32)
  				{
  					guid = Guid.Empty;
  					return false;
  				}
  				for (int i = 0; i < 32; i++)
  				{
  					if ("0123456789abcdefABCDEF".IndexOf(name[i]) == -1)
  					{
  						guid = Guid.Empty;
  						return false;
  					}
  				}
  				guid = new Guid(name);
  				return true;
  			}
  
  			private static string ParseName(string directoryName)
  			{
  				try
  				{
  					string simpleName = null;
  					string version = "0.0.0.0";
  					string publicKeyToken = "null";
  					string culture = "neutral";
  					System.Text.StringBuilder sb = new System.Text.StringBuilder();
  					int part = 0;
  					for (int i = 0; i <= directoryName.Length; i++)
  					{
  						if (i == directoryName.Length || directoryName[i] == '_')
  						{
  							if (i < directoryName.Length - 1 && directoryName[i + 1] == '!')
  							{
  								sb.Append('_');
  								i++;
  							}
  							else if (i == directoryName.Length || directoryName[i + 1] == '_')
  							{
  								switch (part++)
  								{
  									case 0:
  										simpleName = sb.ToString();
  										break;
  									case 1:
  
gt: 										version = sb.ToString();