Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PortableExecutableImage"/> class.
        /// </summary>
        /// <param name="loadOrder">The load order.</param>
        /// <param name="stream">The stream.</param>
        private PortableExecutableImage(int loadOrder, Stream stream)
        {
            _assemblyStream = stream;
            _assemblyReader = new BinaryReader(stream);

            _loadOrder = loadOrder;

            // Load all headers by visiting them sequentially
            _dosHeader.Read(_assemblyReader);

            _assemblyReader.BaseStream.Seek(_dosHeader.e_lfanew, SeekOrigin.Begin);
            _ntHeader.Read(_assemblyReader);

            if (CLI_HEADER_DATA_DIRECTORY >= _ntHeader.OptionalHeader.NumberOfRvaAndSizes)
            {
                throw new BadImageFormatException();
            }

            _sections = new IMAGE_SECTION_HEADER[_ntHeader.FileHeader.NumberOfSections];
            for (int i = 0; i < _ntHeader.FileHeader.NumberOfSections; i++)
            {
                _sections[i].Read(_assemblyReader);
            }

            long position = ResolveVirtualAddress(_ntHeader.OptionalHeader.DataDirectory[CLI_HEADER_DATA_DIRECTORY].VirtualAddress);

            _assemblyReader.BaseStream.Seek(position, SeekOrigin.Begin);
            _cliHeader.Read(_assemblyReader);

            // Load the provider...
            position = ResolveVirtualAddress(_cliHeader.Metadata.VirtualAddress);
            _assemblyReader.BaseStream.Position = position;
            _metadata = _assemblyReader.ReadBytes(_cliHeader.Metadata.Size);

            MetadataRoot mdr = new MetadataRoot(this);

            mdr.Initialize(_metadata);
            _metadataRoot = mdr;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="PortableExecutableImage"/> class.
        /// </summary>
        /// <param name="loadOrder">The load order.</param>
        /// <param name="stream">The stream.</param>
        private PortableExecutableImage(int loadOrder, Stream stream, string codeBase)
        {
            this.codeBase = codeBase;

            _assemblyStream = stream;
            _assemblyReader = new BinaryReader(stream);

            _loadOrder = loadOrder;

            // Load all headers by visiting them sequentially
            _dosHeader.Read(_assemblyReader);

            _assemblyReader.BaseStream.Seek(_dosHeader.e_lfanew, SeekOrigin.Begin);
            _ntHeader.Read(_assemblyReader);

            if (CLI_HEADER_DATA_DIRECTORY >= _ntHeader.OptionalHeader.NumberOfRvaAndSizes)
                throw new BadImageFormatException();

            _sections = new IMAGE_SECTION_HEADER[_ntHeader.FileHeader.NumberOfSections];
            for (int i = 0; i < _ntHeader.FileHeader.NumberOfSections; i++)
                _sections[i].Read(_assemblyReader);

            long position = ResolveVirtualAddress(_ntHeader.OptionalHeader.DataDirectory[CLI_HEADER_DATA_DIRECTORY].VirtualAddress);
            _assemblyReader.BaseStream.Seek(position, SeekOrigin.Begin);
            _cliHeader.Read(_assemblyReader);

            // Load the provider...
            position = ResolveVirtualAddress(_cliHeader.Metadata.VirtualAddress);
            _assemblyReader.BaseStream.Position = position;
            _metadata = _assemblyReader.ReadBytes(_cliHeader.Metadata.Size);

            MetadataRoot mdr = new MetadataRoot(this);
            mdr.Initialize(_metadata);
            _metadataRoot = mdr;
        }