private static void ReadTypes(Collection <TypeDefinition> types)
 {
     for (int i = 0; i < types.Count; i++)
     {
         ImmediateModuleReader.ReadType(types[i]);
     }
 }
        private static void ReadMethods(TypeDefinition type)
        {
            Collection <MethodDefinition> methods = type.Methods;

            for (int i = 0; i < methods.Count; i++)
            {
                MethodDefinition item = methods[i];
                ImmediateModuleReader.ReadGenericParameters(item);
                if (item.HasParameters)
                {
                    ImmediateModuleReader.ReadParameters(item);
                }
                if (item.HasOverrides)
                {
                    ImmediateModuleReader.Read(item.Overrides);
                }
                if (item.IsPInvokeImpl)
                {
                    ImmediateModuleReader.Read(item.PInvokeInfo);
                }
                ImmediateModuleReader.ReadSecurityDeclarations(item);
                ImmediateModuleReader.ReadCustomAttributes(item);
                MethodReturnType methodReturnType = item.MethodReturnType;
                if (methodReturnType.HasConstant)
                {
                    ImmediateModuleReader.Read(methodReturnType.Constant);
                }
                if (methodReturnType.HasMarshalInfo)
                {
                    ImmediateModuleReader.Read(methodReturnType.MarshalInfo);
                }
                ImmediateModuleReader.ReadCustomAttributes(methodReturnType);
            }
        }
 private static void ReadType(TypeDefinition type)
 {
     ImmediateModuleReader.ReadGenericParameters(type);
     if (type.HasInterfaces)
     {
         ImmediateModuleReader.Read(type.Interfaces);
     }
     if (type.HasNestedTypes)
     {
         ImmediateModuleReader.ReadTypes(type.NestedTypes);
     }
     if (type.HasLayoutInfo)
     {
         ImmediateModuleReader.Read(type.ClassSize);
     }
     if (type.HasFields)
     {
         ImmediateModuleReader.ReadFields(type);
     }
     if (type.HasMethods)
     {
         ImmediateModuleReader.ReadMethods(type);
     }
     if (type.HasProperties)
     {
         ImmediateModuleReader.ReadProperties(type);
     }
     if (type.HasEvents)
     {
         ImmediateModuleReader.ReadEvents(type);
     }
     ImmediateModuleReader.ReadSecurityDeclarations(type);
     ImmediateModuleReader.ReadCustomAttributes(type);
 }
示例#4
0
        public void ReadSymbols(ISymbolReader reader, bool throwIfSymbolsAreNotMaching)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            symbol_reader = reader;

            if (!symbol_reader.ProcessDebugHeader(GetDebugHeader()))
            {
                symbol_reader = null;

                if (throwIfSymbolsAreNotMaching)
                {
                    throw new SymbolsNotMatchingException("Symbols were found but are not matching the assembly");
                }

                return;
            }

            if (HasImage && ReadingMode == ReadingMode.Immediate)
            {
                var immediate_reader = new ImmediateModuleReader(Image);
                immediate_reader.ReadSymbols(this);
            }
        }
        private static void ReadFields(TypeDefinition type)
        {
            Collection <FieldDefinition> fields = type.Fields;

            for (int i = 0; i < fields.Count; i++)
            {
                FieldDefinition item = fields[i];
                if (item.HasConstant)
                {
                    ImmediateModuleReader.Read(item.Constant);
                }
                if (item.HasLayoutInfo)
                {
                    ImmediateModuleReader.Read(item.Offset);
                }
                if (item.RVA > 0)
                {
                    ImmediateModuleReader.Read(item.InitialValue);
                }
                if (item.HasMarshalInfo)
                {
                    ImmediateModuleReader.Read(item.MarshalInfo);
                }
                ImmediateModuleReader.ReadCustomAttributes(item);
            }
        }
 protected override void ReadModule()
 {
     this.module.Read <ModuleDefinition, ModuleDefinition>(this.module, (ModuleDefinition module, MetadataReader reader) => {
         base.ReadModuleManifest(reader);
         ImmediateModuleReader.ReadModule(module);
         return(module);
     });
 }
 public void FullLoad()
 {
     if (this.HasImage && this.ReadingMode == ReadingMode.Deferred)
     {
         ImmediateModuleReader.ReadModule(this);
         this.ReadingMode = ReadingMode.Immediate;
     }
 }
示例#8
0
        public static void WriteModuleTo(ModuleDefinition module, Stream stream, WriterParameters parameters)
        {
            AssemblyNameDefinition name;

            if ((int)(module.Attributes & ModuleAttributes.ILOnly) == 0)
            {
                throw new NotSupportedException("Writing mixed-mode assemblies is not supported");
            }
            if (module.HasImage && module.ReadingMode == ReadingMode.Deferred)
            {
                ImmediateModuleReader.ReadModule(module);
            }
            module.MetadataSystem.Clear();
            if (module.assembly != null)
            {
                name = module.assembly.Name;
            }
            else
            {
                name = null;
            }
            AssemblyNameDefinition publicKey           = name;
            string fullyQualifiedName                  = stream.GetFullyQualifiedName();
            ISymbolWriterProvider symbolWriterProvider = parameters.SymbolWriterProvider;

            if (symbolWriterProvider == null && parameters.WriteSymbols)
            {
                symbolWriterProvider = SymbolProvider.GetPlatformWriterProvider();
            }
            ISymbolWriter symbolWriter = ModuleWriter.GetSymbolWriter(module, fullyQualifiedName, symbolWriterProvider);

            if (parameters.StrongNameKeyPair != null && publicKey != null)
            {
                publicKey.PublicKey = parameters.StrongNameKeyPair.PublicKey;
                ModuleDefinition attributes = module;
                attributes.Attributes = attributes.Attributes | ModuleAttributes.StrongNameSigned;
            }
            MetadataBuilder metadataBuilder = new MetadataBuilder(module, fullyQualifiedName, symbolWriterProvider, symbolWriter);

            ModuleWriter.BuildMetadata(module, metadataBuilder);
            if (module.symbol_reader != null)
            {
                module.symbol_reader.Dispose();
            }
            ImageWriter imageWriter = ImageWriter.CreateWriter(module, metadataBuilder, stream);

            imageWriter.WriteImage();
            if (parameters.StrongNameKeyPair != null)
            {
                CryptoService.StrongName(stream, imageWriter, parameters.StrongNameKeyPair);
            }
            if (symbolWriter != null)
            {
                symbolWriter.Dispose();
            }
        }
        private static void ReadEvents(TypeDefinition type)
        {
            Collection <EventDefinition> events = type.Events;

            for (int i = 0; i < events.Count; i++)
            {
                EventDefinition item = events[i];
                ImmediateModuleReader.Read(item.AddMethod);
                ImmediateModuleReader.ReadCustomAttributes(item);
            }
        }
示例#10
0
        public void ImmediateRead()
        {
            if (!HasImage)
            {
                return;
            }
            ReadingMode = ReadingMode.Immediate;
            var moduleReader = new ImmediateModuleReader(Image);

            moduleReader.ReadModule(this, resolve_attributes: true);
        }
        private static void ReadCustomAttributes(ICustomAttributeProvider provider)
        {
            if (!provider.HasCustomAttributes)
            {
                return;
            }
            Collection <CustomAttribute> customAttributes = provider.CustomAttributes;

            for (int i = 0; i < customAttributes.Count; i++)
            {
                ImmediateModuleReader.Read(customAttributes[i].ConstructorArguments);
            }
        }
        private static void ReadSecurityDeclarations(ISecurityDeclarationProvider provider)
        {
            if (!provider.HasSecurityDeclarations)
            {
                return;
            }
            Collection <SecurityDeclaration> securityDeclarations = provider.SecurityDeclarations;

            for (int i = 0; i < securityDeclarations.Count; i++)
            {
                ImmediateModuleReader.Read(securityDeclarations[i].SecurityAttributes);
            }
        }
        private static void ReadProperties(TypeDefinition type)
        {
            Collection <PropertyDefinition> properties = type.Properties;

            for (int i = 0; i < properties.Count; i++)
            {
                PropertyDefinition item = properties[i];
                ImmediateModuleReader.Read(item.GetMethod);
                if (item.HasConstant)
                {
                    ImmediateModuleReader.Read(item.Constant);
                }
                ImmediateModuleReader.ReadCustomAttributes(item);
            }
        }
示例#14
0
        public void ReadSymbols(ISymbolReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            symbol_reader = reader;

            ProcessDebugHeader();

            if (HasImage && ReadingMode == ReadingMode.Immediate)
            {
                var immediate_reader = new ImmediateModuleReader(Image);
                immediate_reader.ReadSymbols(this);
            }
        }
示例#15
0
        public static void WriteModuleTo(ModuleDefinition module, Stream stream, WriterParameters parameters)
        {
            if ((module.Attributes & ModuleAttributes.ILOnly) == 0)
            {
                throw new NotSupportedException("Writing mixed-mode assemblies is not supported");
            }
            if (module.HasImage && (module.ReadingMode == ReadingMode.Deferred))
            {
                ImmediateModuleReader.ReadModule(module);
            }
            module.MetadataSystem.Clear();
            AssemblyNameDefinition name = module.assembly?.Name;
            string fullyQualifiedName   = stream.GetFullyQualifiedName();
            ISymbolWriterProvider symbolWriterProvider = parameters.SymbolWriterProvider;

            if ((symbolWriterProvider == null) && parameters.WriteSymbols)
            {
                symbolWriterProvider = SymbolProvider.GetPlatformWriterProvider();
            }
            ISymbolWriter writer = GetSymbolWriter(module, fullyQualifiedName, symbolWriterProvider);

            if ((parameters.StrongNameKeyPair != null) && (name != null))
            {
                name.PublicKey     = parameters.StrongNameKeyPair.PublicKey;
                module.Attributes |= ModuleAttributes.StrongNameSigned;
            }
            MetadataBuilder metadata = new MetadataBuilder(module, fullyQualifiedName, symbolWriterProvider, writer);

            BuildMetadata(module, metadata);
            if (module.symbol_reader != null)
            {
                module.symbol_reader.Dispose();
            }
            ImageWriter writer2 = ImageWriter.CreateWriter(module, metadata, stream);

            writer2.WriteImage();
            if (parameters.StrongNameKeyPair != null)
            {
                CryptoService.StrongName(stream, writer2, parameters.StrongNameKeyPair);
            }
            if (writer != null)
            {
                writer.Dispose();
            }
        }
        private static void ReadParameters(MethodDefinition method)
        {
            Collection <ParameterDefinition> parameters = method.Parameters;

            for (int i = 0; i < parameters.Count; i++)
            {
                ParameterDefinition item = parameters[i];
                if (item.HasConstant)
                {
                    ImmediateModuleReader.Read(item.Constant);
                }
                if (item.HasMarshalInfo)
                {
                    ImmediateModuleReader.Read(item.MarshalInfo);
                }
                ImmediateModuleReader.ReadCustomAttributes(item);
            }
        }
        private static void ReadGenericParameters(IGenericParameterProvider provider)
        {
            if (!provider.HasGenericParameters)
            {
                return;
            }
            Collection <GenericParameter> genericParameters = provider.GenericParameters;

            for (int i = 0; i < genericParameters.Count; i++)
            {
                GenericParameter item = genericParameters[i];
                if (item.HasConstraints)
                {
                    ImmediateModuleReader.Read(item.Constraints);
                }
                ImmediateModuleReader.ReadCustomAttributes(item);
            }
        }
示例#18
0
        private static void Write(ModuleDefinition module, Disposable <Stream> stream, WriterParameters parameters)
        {
            if ((module.Attributes & ModuleAttributes.ILOnly) == (ModuleAttributes)0)
            {
                throw new NotSupportedException("Writing mixed-mode assemblies is not supported");
            }
            if (module.HasImage && module.ReadingMode == ReadingMode.Deferred)
            {
                ImmediateModuleReader immediateModuleReader = new ImmediateModuleReader(module.Image);
                immediateModuleReader.ReadModule(module, false);
                immediateModuleReader.ReadSymbols(module);
            }
            module.MetadataSystem.Clear();
            if (module.symbol_reader != null)
            {
                module.symbol_reader.Dispose();
            }
            AssemblyNameDefinition assemblyNameDefinition = (module.assembly != null) ? module.assembly.Name : null;
            string fileName  = stream.value.GetFileName();
            uint   timestamp = parameters.Timestamp ?? module.timestamp;
            ISymbolWriterProvider symbolWriterProvider = parameters.SymbolWriterProvider;

            if (symbolWriterProvider == null && parameters.WriteSymbols)
            {
                symbolWriterProvider = new DefaultSymbolWriterProvider();
            }
            if (parameters.StrongNameKeyPair != null && assemblyNameDefinition != null)
            {
                assemblyNameDefinition.PublicKey = parameters.StrongNameKeyPair.PublicKey;
                module.Attributes |= ModuleAttributes.StrongNameSigned;
            }
            using (ISymbolWriter symbol_writer = GetSymbolWriter(module, fileName, symbolWriterProvider, parameters))
            {
                MetadataBuilder metadata = new MetadataBuilder(module, fileName, timestamp, symbolWriterProvider, symbol_writer);
                BuildMetadata(module, metadata);
                ImageWriter imageWriter = ImageWriter.CreateWriter(module, metadata, stream);
                stream.value.SetLength(0L);
                imageWriter.WriteImage();
                if (parameters.StrongNameKeyPair != null)
                {
                    CryptoService.StrongName(stream.value, imageWriter, parameters.StrongNameKeyPair);
                }
            }
        }
        public static void ReadModule(ModuleDefinition module)
        {
            if (module.HasAssemblyReferences)
            {
                ImmediateModuleReader.Read(module.AssemblyReferences);
            }
            if (module.HasResources)
            {
                ImmediateModuleReader.Read(module.Resources);
            }
            if (module.HasModuleReferences)
            {
                ImmediateModuleReader.Read(module.ModuleReferences);
            }
            if (module.HasTypes)
            {
                ImmediateModuleReader.ReadTypes(module.Types);
            }
            if (module.HasExportedTypes)
            {
                ImmediateModuleReader.Read(module.ExportedTypes);
            }
            if (module.HasCustomAttributes)
            {
                ImmediateModuleReader.Read(module.CustomAttributes);
            }
            AssemblyDefinition assembly = module.Assembly;

            if (assembly == null)
            {
                return;
            }
            if (assembly.HasCustomAttributes)
            {
                ImmediateModuleReader.ReadCustomAttributes(assembly);
            }
            if (assembly.HasSecurityDeclarations)
            {
                ImmediateModuleReader.Read(assembly.SecurityDeclarations);
            }
        }