Exemplo n.º 1
0
        public LoaderResult64 LoadImage()
        {
            if (!stream.CanRead)
            {
                throw new ArgumentException("Stream is not available for reading", nameof(stream));
            }
            if (!stream.CanSeek)
            {
                throw new ArgumentException("Stream is not available for seeking", nameof(stream));
            }

            var metadata = new List <object>();

            var msDosStubHeader = new MsDosStubHeader();

            msDosStubHeader.Read(stream);
            metadata.Add(msDosStubHeader);

            stream.Seek((long)msDosStubHeader.e_lfanew, SeekOrigin.Begin);
            var peHeader = new PEHeader();

            peHeader.Read(stream);
            metadata.Add(peHeader);

            UInt32 entryPoint = 0;

            if (peHeader.mSizeOfOptionalHeader > 0)
            {
                PEHeaderOption64 pe64;
                if (!PEHeaderOption64.TryRead(stream, out pe64))
                {
                    return(LoaderResult64.Error("Unable to read Pe64OptionalHeader"));
                }
                metadata.Add(pe64);

                entryPoint = pe64.mAddressOfEntryPoint;
            }

            return(new LoaderResult64(entryPoint, new byte[0], metadata: metadata));
        }