Example #1
0
        public static string GetFullAssemblyName(this SRM.AssemblyReference reference, MetadataReader reader)
        {
            string publicKey = "null";

            if (!reference.PublicKeyOrToken.IsNil)
            {
                if ((reference.Flags & AssemblyFlags.PublicKey) != 0)
                {
                    publicKey = CalculatePublicKeyToken(reference.PublicKeyOrToken, reader);
                }
                else
                {
                    publicKey = reader.GetBlobBytes(reference.PublicKeyOrToken).ToHexString(8);
                }
            }
            string properties = "";

            if ((reference.Flags & AssemblyFlags.Retargetable) != 0)
            {
                properties = ", Retargetable=true";
            }
            return($"{reader.GetString(reference.Name)}, " +
                   $"Version={reference.Version}, " +
                   $"Culture={(reference.Culture.IsNil ? "neutral" : reader.GetString(reference.Culture))}, " +
                   $"PublicKeyToken={publicKey}{properties}");
        }
Example #2
0
 public AssemblyRefEntry(PEFile module, AssemblyReferenceHandle handle)
 {
     this.metadataOffset = module.Reader.PEHeaders.MetadataStartOffset;
     this.module         = module;
     this.metadata       = module.Metadata;
     this.handle         = handle;
     this.assemblyRef    = metadata.GetAssemblyReference(handle);
 }
Example #3
0
        private void InitializeTypeForwarder(ForwarderKey key, TypeForwarder record)
        {
            Cts.Ecma.EcmaModule module       = key.Module;
            Ecma.MetadataReader reader       = module.MetadataReader;
            Ecma.ExportedType   exportedType = reader.GetExportedType(key.ExportedType);

            record.Name = HandleString(reader.GetString(exportedType.Name));

            switch (exportedType.Implementation.Kind)
            {
            case Ecma.HandleKind.AssemblyReference:
            {
                string ns = reader.GetString(exportedType.Namespace);
                NamespaceDefinition namespaceDefinition = HandleNamespaceDefinition(module, ns);

                Ecma.AssemblyReference assemblyRef = reader.GetAssemblyReference((Ecma.AssemblyReferenceHandle)exportedType.Implementation);
                AssemblyName           refName     = new AssemblyName
                {
                    ContentType = (AssemblyContentType)((int)(assemblyRef.Flags & AssemblyFlags.ContentTypeMask) >> 9),
                    Flags       = (AssemblyNameFlags)(assemblyRef.Flags & ~AssemblyFlags.ContentTypeMask),
                    CultureName = reader.GetString(assemblyRef.Culture),
                    Name        = reader.GetString(assemblyRef.Name),
                    Version     = assemblyRef.Version,
                };

                if ((assemblyRef.Flags & AssemblyFlags.PublicKey) != 0)
                {
                    refName.SetPublicKey(reader.GetBlobBytes(assemblyRef.PublicKeyOrToken));
                }
                else
                {
                    refName.SetPublicKeyToken(reader.GetBlobBytes(assemblyRef.PublicKeyOrToken));
                }

                record.Scope = HandleScopeReference(refName);

                namespaceDefinition.TypeForwarders.Add(record);
            }
            break;

            case Ecma.HandleKind.ExportedType:
            {
                TypeForwarder scope = HandleTypeForwarder(module, (Ecma.ExportedTypeHandle)exportedType.Implementation);

                record.Scope = scope.Scope;

                scope.NestedTypes.Add(record);
            }
            break;

            default:
                throw new BadImageFormatException();
            }
        }
Example #4
0
        private TypeForwarder HandleTypeForwarder(Cts.Ecma.EcmaModule module, Ecma.ExportedType exportedType)
        {
            Ecma.MetadataReader reader = module.MetadataReader;
            string        name         = reader.GetString(exportedType.Name);
            TypeForwarder result;

            switch (exportedType.Implementation.Kind)
            {
            case Ecma.HandleKind.AssemblyReference:
            {
                string ns = reader.GetString(exportedType.Namespace);
                NamespaceDefinition namespaceDefinition = HandleNamespaceDefinition(module, ns);

                Ecma.AssemblyReference assemblyRef = reader.GetAssemblyReference((Ecma.AssemblyReferenceHandle)exportedType.Implementation);
                AssemblyName           refName     = new AssemblyName
                {
                    ContentType = (AssemblyContentType)((int)(assemblyRef.Flags & AssemblyFlags.ContentTypeMask) >> 9),
                    Flags       = (AssemblyNameFlags)(assemblyRef.Flags & ~AssemblyFlags.ContentTypeMask),
                    CultureName = reader.GetString(assemblyRef.Culture),
                    Name        = reader.GetString(assemblyRef.Name),
                    Version     = assemblyRef.Version,
                };

                result = new TypeForwarder
                {
                    Name  = HandleString(name),
                    Scope = HandleScopeReference(refName),
                };

                namespaceDefinition.TypeForwarders.Add(result);
            }
            break;

            case Ecma.HandleKind.ExportedType:
            {
                TypeForwarder scope = HandleTypeForwarder(module, reader.GetExportedType((Ecma.ExportedTypeHandle)exportedType.Implementation));

                result = new TypeForwarder
                {
                    Name  = HandleString(name),
                    Scope = scope.Scope,
                };

                scope.NestedTypes.Add(result);
            }
            break;

            default:
                throw new BadImageFormatException();
            }

            return(result);
        }
Example #5
0
 public static bool TryGetFullAssemblyName(this SRM.AssemblyReference reference, MetadataReader reader, out string assemblyName)
 {
     try
     {
         assemblyName = GetFullAssemblyName(reference, reader);
         return(true);
     }
     catch (BadImageFormatException)
     {
         assemblyName = null;
         return(false);
     }
 }
Example #6
0
 public AssemblyReference(PEFile module, AssemblyReferenceHandle handle)
 {
     if (module == null)
     {
         throw new ArgumentNullException(nameof(module));
     }
     if (handle.IsNil)
     {
         throw new ArgumentNullException(nameof(handle));
     }
     Metadata = module.Metadata;
     Handle   = handle;
     entry    = Metadata.GetAssemblyReference(handle);
 }
Example #7
0
 public AssemblyReference(MetadataReader metadata, AssemblyReferenceHandle handle)
 {
     if (metadata == null)
     {
         throw new ArgumentNullException(nameof(metadata));
     }
     if (handle.IsNil)
     {
         throw new ArgumentNullException(nameof(handle));
     }
     Metadata = metadata;
     Handle   = handle;
     entry    = metadata.GetAssemblyReference(handle);
 }
        public static string GetFullAssemblyName(this MetadataReader metadataReader, AssemblyReference assemblyReference)
        {
            var assemblyName = metadataReader.GetString(assemblyReference.Name);
            var culture = metadataReader.GetString(assemblyReference.Culture);
            if (string.IsNullOrEmpty(culture))
            {
                culture = "neutral";
            }

            var version = assemblyReference.Version;
            var publicKeyTokenBytes = metadataReader.GetBytes(assemblyReference.PublicKeyOrToken);
            var publicKeyToken = Conversion.ByteArrayToHexString(publicKeyTokenBytes);
            publicKeyToken = string.IsNullOrEmpty(publicKeyToken) ? "null" : publicKeyToken;

            var fullAssemblyName = string.Format(
                "{0}, Version={1}, Culture={2}, PublicKeyToken={3}",
                assemblyName,
                version.ToString(),
                culture,
                publicKeyToken);
            return fullAssemblyName;
        }
Example #9
0
        public static string GetFullAssemblyName(this SRM.AssemblyReference reference, MetadataReader reader)
        {
            StringBuilder builder = new StringBuilder();

            builder.Append(reader.GetString(reference.Name));
            builder.Append(", Version=");
            builder.Append(reference.Version);
            builder.Append(", Culture=");
            if (reference.Culture.IsNil)
            {
                builder.Append("neutral");
            }
            else
            {
                builder.Append(reader.GetString(reference.Culture));
            }

            if (reference.PublicKeyOrToken.IsNil)
            {
                builder.Append(", PublicKeyToken=null");
            }
            else if ((reference.Flags & AssemblyFlags.PublicKey) != 0)
            {
                builder.Append(", PublicKeyToken=");
                builder.Append(CalculatePublicKeyToken(reference.PublicKeyOrToken, reader));
            }
            else
            {
                builder.Append(", PublicKeyToken=");
                builder.AppendHexString(reader.GetBlobReader(reference.PublicKeyOrToken));
            }
            if ((reference.Flags & AssemblyFlags.Retargetable) != 0)
            {
                builder.Append(", Retargetable=true");
            }
            return(builder.ToString());
        }
        public static AssemblyReferenceInformation FormatAssemblyInfo(this MetadataReader metadataReader, AssemblyReference assemblyReference)
        {
            var name = metadataReader.GetString(assemblyReference.Name);

            return metadataReader.FormatAssemblyInfo(name, assemblyReference.Culture, assemblyReference.PublicKeyOrToken, assemblyReference.Version);
        }
Example #11
0
            private static AssemblyReference[] GetAssemblyReferences(MetadataReader reader)
            {
                var count = reader.GetTableRowCount(TableIndex.AssemblyRef);
                var references = new AssemblyReference[count];

                for (int i = 0; i < count; i++)
                {
                    var reference = reader.GetAssemblyReference(MetadataTokens.AssemblyReferenceHandle(i + 1));
                    references[i] = new AssemblyReference(reader.GetString(reference.Name), reference.Version);
                }

                return references.ToArray();
            }
Example #12
0
 public AssemblyInfo(string path, string name, Version version, AssemblyReference[] references, string[] moduleReferences)
 {
     Path = path;
     Name = name;
     Version = version;
     References = references;
     ModuleReferences = moduleReferences;
     State = CheckState.Unchecked;
 }
Example #13
0
 private bool ShouldIgnore(AssemblyReference reference)
 {
     Version toIgnore;
     return ignoredReferences.TryGetValue(reference.Name, out toIgnore) && (toIgnore == null || toIgnore >= reference.Version);
 }
 private bool IsSameResolutionScope(MetadataReader referenceMetadata, MetadataReader newMetadata, AssemblyReference referenceResolutionScope, AssemblyReference newResolutionScope)
 {
     return IsSameAssembly(referenceMetadata, newMetadata, referenceResolutionScope, newResolutionScope);
 }
        private bool IsSameAssembly(MetadataReader referenceMetadata, MetadataReader newMetadata, AssemblyReference referenceAssemblyReference, AssemblyReference newAssemblyReference)
        {
            string referenceName = referenceMetadata.GetString(referenceAssemblyReference.Name);
            string newName = newMetadata.GetString(newAssemblyReference.Name);
            if (!string.Equals(referenceName, newName, StringComparison.Ordinal))
                return false;

            string referenceCulture = referenceMetadata.GetString(referenceAssemblyReference.Culture);
            string newCulture = newMetadata.GetString(newAssemblyReference.Culture);
            if (!string.Equals(referenceCulture, newCulture, StringComparison.Ordinal))
                return false;

            Version referenceVersion = referenceAssemblyReference.Version;
            Version newVersion = newAssemblyReference.Version;
            if (referenceVersion != newVersion)
                return false;

            byte[] referencePublicKeyOrToken = referenceMetadata.GetBlobBytes(referenceAssemblyReference.PublicKeyOrToken);
            byte[] newPublicKeyOrToken = newMetadata.GetBlobBytes(newAssemblyReference.PublicKeyOrToken);
            if (referencePublicKeyOrToken != null)
            {
                if (newPublicKeyOrToken == null || referencePublicKeyOrToken.Length != newPublicKeyOrToken.Length)
                    return false;

                for (int i = 0; i < referencePublicKeyOrToken.Length; i++)
                {
                    if (referencePublicKeyOrToken[i] != newPublicKeyOrToken[i])
                        return false;
                }
            }
            else if (newPublicKeyOrToken != null)
            {
                return false;
            }

            return true;
        }
 private void CheckResolutionScope(MetadataReader referenceMetadata, MetadataReader newMetadata, AssemblyReference referenceResolutionScope, AssemblyReference newResolutionScope)
 {
     if (!IsSameAssembly(referenceMetadata, newMetadata, referenceResolutionScope, newResolutionScope))
         throw new NotImplementedException("ResolutionScope assembly reference changed.");
 }