private IEnumerable <string> FindAssembliesGacAny(GacInfo gacInfo, IAssembly assembly, ModuleDef sourceModule) { if (gacInfo != null) { var asmSimpleName = UTF8String.ToSystemStringOrEmpty(assembly.Name); foreach (var subDir in gacInfo.subDirs) { var baseDir = Path.Combine(gacInfo.path, subDir); baseDir = Path.Combine(baseDir, asmSimpleName); foreach (var dir in GetDirs(baseDir)) { var pathName = Path.Combine(dir, asmSimpleName + ".dll"); if (File.Exists(pathName)) { yield return(pathName); } } } } }
/// <summary> /// Returns an assembly name string /// </summary> /// <param name="name">Simple assembly name</param> /// <param name="version">Version or <c>null</c></param> /// <param name="culture">Culture or <c>null</c></param> /// <param name="publicKey">Public key / public key token or <c>null</c></param> /// <param name="attributes">Assembly attributes</param> /// <returns>An assembly name string</returns> internal static string GetAssemblyNameString(UTF8String name, Version version, UTF8String culture, PublicKeyBase publicKey, AssemblyAttributes attributes) { var sb = new StringBuilder(); foreach (var c in UTF8String.ToSystemStringOrEmpty(name)) { if (c == ',' || c == '=') { sb.Append('\\'); } sb.Append(c); } if (version != null) { sb.Append(", Version="); sb.Append(CreateVersionWithNoUndefinedValues(version).ToString()); } if ((object)culture != null) { sb.Append(", Culture="); sb.Append(UTF8String.IsNullOrEmpty(culture) ? "neutral" : culture.String); } sb.Append(", "); sb.Append(publicKey == null || publicKey is PublicKeyToken ? "PublicKeyToken=" : "PublicKey="); sb.Append(publicKey == null ? "null" : publicKey.ToString()); if ((attributes & AssemblyAttributes.Retargetable) != 0) { sb.Append(", Retargetable=Yes"); } if ((attributes & AssemblyAttributes.ContentType_Mask) == AssemblyAttributes.ContentType_WindowsRuntime) { sb.Append(", ContentType=WindowsRuntime"); } return(sb.ToString()); }
/// <summary> /// Reads the new (.NET 2.0+) DeclSecurity blob format /// </summary> /// <returns></returns> private ThreadSafe.IList <SecurityAttribute> ReadBinaryFormat() { int numAttrs = (int)reader.ReadCompressedUInt32(); var list = ThreadSafeListCreator.Create <SecurityAttribute>(numAttrs); for (int i = 0; i < numAttrs; i++) { var name = ReadUTF8String(); // Use CA search rules. Some tools don't write the fully qualified name. var attrRef = TypeNameParser.ParseReflection(module, UTF8String.ToSystemStringOrEmpty(name), new CAAssemblyRefFinder(module), gpContext); int blobLength = (int)reader.ReadCompressedUInt32(); int numNamedArgs = (int)reader.ReadCompressedUInt32(); var namedArgs = CustomAttributeReader.ReadNamedArguments(module, reader, numNamedArgs, gpContext); if (namedArgs == null) { throw new ApplicationException("Could not read named arguments"); } list.Add(new SecurityAttribute(attrRef, namedArgs)); } return(list); }
private IEnumerable <string> FindAssembliesGacExactly(GacInfo gacInfo, IAssembly assembly, ModuleDef sourceModule) { var pkt = PublicKeyBase.ToPublicKeyToken(assembly.PublicKeyOrToken); if (gacInfo != null && pkt != null) { string pktString = pkt.ToString(); string verString = Utils.CreateVersionWithNoUndefinedValues(assembly.Version).ToString(); var asmSimpleName = UTF8String.ToSystemStringOrEmpty(assembly.Name); foreach (var subDir in gacInfo.subDirs) { var baseDir = Path.Combine(gacInfo.path, subDir); baseDir = Path.Combine(baseDir, asmSimpleName); baseDir = Path.Combine(baseDir, string.Format("{0}{1}__{2}", gacInfo.prefix, verString, pktString)); var pathName = Path.Combine(baseDir, asmSimpleName + ".dll"); if (File.Exists(pathName)) { yield return(pathName); } } } }
/// <inheritdoc/> public override string ToString() { return(string.Format("{0} - file: {1}", UTF8String.ToSystemStringOrEmpty(Name), UTF8String.ToSystemStringOrEmpty(FileName))); }
/// <inheritdoc/> public override string ToString() { return(string.Format("{0} - assembly: {1}", UTF8String.ToSystemStringOrEmpty(Name), asmRef.FullName)); }
/// <inheritdoc/> public override string ToString() { var ds = dataStream; return(string.Format("{0} - size: {1}", UTF8String.ToSystemStringOrEmpty(Name), ds == null ? 0 : ds.Length)); }
private static string GetCanonicalLocale(UTF8String locale) { return(GetCanonicalLocale(UTF8String.ToSystemStringOrEmpty(locale))); }
private MarshalType Read() { MarshalType returnValue; try { var nativeType = (NativeType)reader.ReadByte(); NativeType nt; int size; switch (nativeType) { case NativeType.FixedSysString: size = CanRead() ? (int)reader.ReadCompressedUInt32() : -1; returnValue = new FixedSysStringMarshalType(size); break; case NativeType.SafeArray: var vt = CanRead() ? (VariantType)reader.ReadCompressedUInt32() : VariantType.NotInitialized; var udtName = CanRead() ? ReadUTF8String() : null; var udtRef = (object)udtName == null ? null : TypeNameParser.ParseReflection(module, UTF8String.ToSystemStringOrEmpty(udtName), null, gpContext); returnValue = new SafeArrayMarshalType(vt, udtRef); break; case NativeType.FixedArray: size = CanRead() ? (int)reader.ReadCompressedUInt32() : -1; nt = CanRead() ? (NativeType)reader.ReadCompressedUInt32() : NativeType.NotInitialized; returnValue = new FixedArrayMarshalType(size, nt); break; case NativeType.Array: nt = CanRead() ? (NativeType)reader.ReadCompressedUInt32() : NativeType.NotInitialized; int paramNum = CanRead() ? (int)reader.ReadCompressedUInt32() : -1; size = CanRead() ? (int)reader.ReadCompressedUInt32() : -1; int flags = CanRead() ? (int)reader.ReadCompressedUInt32() : -1; returnValue = new ArrayMarshalType(nt, paramNum, size, flags); break; case NativeType.CustomMarshaler: var guid = ReadUTF8String(); var nativeTypeName = ReadUTF8String(); var custMarshalerName = ReadUTF8String(); var cmRef = TypeNameParser.ParseReflection(module, UTF8String.ToSystemStringOrEmpty(custMarshalerName), new CAAssemblyRefFinder(module), gpContext); var cookie = ReadUTF8String(); returnValue = new CustomMarshalType(guid, nativeTypeName, cmRef, cookie); break; case NativeType.IUnknown: case NativeType.IDispatch: case NativeType.IntF: int iidParamIndex = CanRead() ? (int)reader.ReadCompressedUInt32() : -1; return(new InterfaceMarshalType(nativeType, iidParamIndex)); default: returnValue = new MarshalType(nativeType); break; } } catch { returnValue = new RawMarshalType(reader.ReadAllBytes()); } return(returnValue); }
/// <inheritdoc/> public AssemblyDef Resolve(IAssembly assembly, ModuleDef sourceModule) { if (assembly == null) { return(null); } if (EnableFrameworkRedirect && !FindExactMatch) { FrameworkRedirect.ApplyFrameworkRedirect(ref assembly, sourceModule); } #if THREAD_SAFE theLock.EnterWriteLock(); try { #endif AssemblyDef resolvedAssembly = Resolve2(assembly, sourceModule); if (resolvedAssembly == null) { string asmName = UTF8String.ToSystemStringOrEmpty(assembly.Name); string asmNameTrimmed = asmName.Trim(); if (asmName != asmNameTrimmed) { assembly = new AssemblyNameInfo { Name = asmNameTrimmed, Version = assembly.Version, PublicKeyOrToken = assembly.PublicKeyOrToken, Culture = assembly.Culture, }; resolvedAssembly = Resolve2(assembly, sourceModule); } } if (resolvedAssembly == null) { // Make sure we don't search for this assembly again. This speeds up callers who // keep asking for this assembly when trying to resolve many different TypeRefs cachedAssemblies[GetAssemblyNameKey(assembly)] = null; return(null); } var key1 = GetAssemblyNameKey(resolvedAssembly); var key2 = GetAssemblyNameKey(assembly); AssemblyDef asm1, asm2; cachedAssemblies.TryGetValue(key1, out asm1); cachedAssemblies.TryGetValue(key2, out asm2); if (asm1 != resolvedAssembly && asm2 != resolvedAssembly) { // This assembly was just resolved if (enableTypeDefCache) { foreach (var module in resolvedAssembly.Modules.GetSafeEnumerable()) { if (module != null) { module.EnableTypeDefFindCache = true; } } } } bool inserted = false; if (!cachedAssemblies.ContainsKey(key1)) { cachedAssemblies.Add(key1, resolvedAssembly); inserted = true; } if (!cachedAssemblies.ContainsKey(key2)) { cachedAssemblies.Add(key2, resolvedAssembly); inserted = true; } if (inserted || asm1 == resolvedAssembly || asm2 == resolvedAssembly) { return(resolvedAssembly); } // Dupe assembly. Don't insert it. var dupeModule = resolvedAssembly.ManifestModule; if (dupeModule != null) { dupeModule.Dispose(); } return(asm1 ?? asm2); #if THREAD_SAFE } finally { theLock.ExitWriteLock(); } #endif }