Exemplo n.º 1
0
        public static MethodBody FromReadingContext(MethodDefinition method, ReadingContext context)
        {
            var reader = context.Reader;
            var body   = new MethodBody(method)
            {
                StartOffset = reader.Position,
            };

            var  bodyHeader = reader.ReadByte();
            uint codeSize;

            if ((bodyHeader & 0x3) == 0x3)
            {
                reader.Position--;
                var fatBodyHeader = reader.ReadUInt16();
                var headerSize    = (fatBodyHeader >> 12) * 4;

                var hasSections = (fatBodyHeader & 0x8) == 0x8;
                body.InitLocals = (fatBodyHeader & 0x10) == 0x10;
                body.MaxStack   = reader.ReadUInt16();
                codeSize        = reader.ReadUInt32();

                var localVarSig = reader.ReadUInt32();
                if (localVarSig != 0)
                {
                    var header      = method.Header;
                    var tableStream = header.GetStream <TableStream>();

                    MetadataMember signature;
                    tableStream.TryResolveMember(new MetadataToken(localVarSig), out signature);
                    body.Signature = signature as StandAloneSignature;
                }

                if (hasSections)
                {
                    body._sectionReadingContext = context.CreateSubContext(reader.Position + codeSize);
                    body._sectionReadingContext.Reader.Align(4);
                }
            }
            else if ((bodyHeader & 0x2) == 0x2)
            {
                codeSize      = (uint)(bodyHeader >> 2);
                body.MaxStack = 8;
            }
            else
            {
                throw new ArgumentException("Invalid method body header signature.");
            }

            body._msilReadingContext = context.CreateSubContext(reader.Position, (int)codeSize);
            return(body);
        }
Exemplo n.º 2
0
        public static X86MethodBody FromReadingContext(ReadingContext context)
        {
            var body = new X86MethodBody();

            body._readingContext = context.CreateSubContext(context.Reader.StartPosition);
            return(body);
        }
Exemplo n.º 3
0
        internal static ImageSymbolImport FromReadingContext(ReadingContext context)
        {
            var reader      = context.Reader;
            var application = context.Assembly;

            var optionalHeader = application.NtHeaders.OptionalHeader;

            var import = new ImageSymbolImport(optionalHeader.Magic == OptionalHeaderMagic.Pe32Plus
                ? reader.ReadUInt64()
                : reader.ReadUInt32());

            if (import.Lookup == 0)
            {
                return(import);
            }

            import.IsImportByOrdinal = import.Lookup >> (optionalHeader.Magic == OptionalHeaderMagic.Pe32Plus ? 63 : 31) == 1;

            if (!import.IsImportByOrdinal)
            {
                import.HintName =
                    HintName.FromReadingContext(context.CreateSubContext(application.RvaToFileOffset(import.HintNameRva)));
            }

            return(import);
        }
Exemplo n.º 4
0
        internal static ImageResourceDataEntry FromReadingContext(ReadingContext context)
        {
            var reader = context.Reader;
            var entry  = new ImageResourceDataEntry
            {
                StartOffset  = reader.Position,
                OffsetToData = reader.ReadUInt32(),
                _size        = reader.ReadUInt32(),
                CodePage     = reader.ReadUInt32(),
                Reserved     = reader.ReadUInt32(),
            };

            entry._dataReaderContext = context.CreateSubContext(context.Assembly.RvaToFileOffset(entry.OffsetToData));

            return(entry);
        }
Exemplo n.º 5
0
        public static ImageDebugDirectory FromReadingContext(ReadingContext context)
        {
            var reader    = context.Reader;
            var directory = new ImageDebugDirectory()
            {
                StartOffset      = reader.StartPosition,
                Characteristics  = reader.ReadUInt32(),
                TimeDateStamp    = reader.ReadUInt32(),
                MajorVersion     = reader.ReadUInt16(),
                MinorVersion     = reader.ReadUInt16(),
                Type             = (DebugInformationFormat)reader.ReadUInt32(),
                SizeOfData       = reader.ReadUInt32(),
                AddressOfRawData = reader.ReadUInt32(),
                PointerToRawData = reader.ReadUInt32(),
            };

            directory._dataReadingContext = context.CreateSubContext(directory.PointerToRawData, (int)directory.SizeOfData);
            return(directory);
        }
Exemplo n.º 6
0
        internal static TableStream FromReadingContext(ReadingContext context)
        {
            var reader = context.Reader;
            var stream = new TableStream
            {
                StartOffset = reader.Position,

                Reserved        = reader.ReadUInt32(),
                MajorVersion    = reader.ReadByte(),
                MinorVersion    = reader.ReadByte(),
                HeapSizes       = reader.ReadByte(),
                Reserved2       = reader.ReadByte(),
                ValidBitVector  = reader.ReadUInt64(),
                SortedBitVector = reader.ReadUInt64(),
            };

            var presentTables = stream.GetPresentTables().ToArray();
            var currentOffset = reader.Position + (presentTables.Length * sizeof(uint));

            foreach (var table in presentTables)
            {
                table.SetRowCount(reader.ReadUInt32());
            }

            if (stream.HasExtraData)
            {
                stream.ExtraData = reader.ReadUInt32();
                currentOffset   += 4;
            }

            foreach (var table in presentTables)
            {
                var size         = (int)table.GetPhysicalLength();
                var tableContext = context.CreateSubContext(currentOffset, size);

                table.SetReadingContext(tableContext);

                currentOffset += size;
            }

            return(stream);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Gets the managed resource data at the given offset.
        /// </summary>
        /// <param name="offset">The offset of the managed resource to get.</param>
        /// <returns>The raw data of the managed resource.</returns>
        public byte[] GetResourceData(uint offset)
        {
            if (_readingContext == null || ResourcesDirectory.VirtualAddress == 0)
            {
                return(null);
            }

            var context = _readingContext.CreateSubContext(
                Assembly.RvaToFileOffset(ResourcesDirectory.VirtualAddress) + offset,
                (int)ResourcesDirectory.Size);

            if (context == null)
            {
                return(null);
            }

            var length = context.Reader.ReadInt32();

            return(context.Reader.ReadBytes(length));
        }
Exemplo n.º 8
0
        internal static MetadataStreamHeader FromReadingContext(ReadingContext context)
        {
            var reader = context.Reader;

            var header = new MetadataStreamHeader
            {
                StartOffset = reader.Position,

                Offset = reader.ReadUInt32(),
                Size   = reader.ReadUInt32(),
                Name   = reader.ReadAlignedAsciiString(4),
            };

            header._stream = new LazyValue <MetadataStream>(() =>
            {
                var mdHeader = context.Assembly.NetDirectory.MetadataHeader;
                var stream   = mdHeader.StreamParser.ReadStream(header.Name,
                                                                context.CreateSubContext(mdHeader.StartOffset + header.Offset, (int)header.Size));
                stream.StreamHeader = header;
                return(stream);
            });

            return(header);
        }
Exemplo n.º 9
0
        public static MethodBody FromReadingContext(MethodDefinition method, ReadingContext context)
        {
            var reader = context.Reader;
            var body = new MethodBody(method)
            {
                StartOffset = reader.Position,
            };

            var bodyHeader = reader.ReadByte();
            uint codeSize;

            if ((bodyHeader & 0x3) == 0x3)
            {
                reader.Position--;
                var fatBodyHeader = reader.ReadUInt16();
                var headerSize = (fatBodyHeader >> 12) * 4;

                var hasSections = (fatBodyHeader & 0x8) == 0x8;
                body.InitLocals = (fatBodyHeader & 0x10) == 0x10;
                body.MaxStack = reader.ReadUInt16();
                codeSize = reader.ReadUInt32();

                var localVarSig = reader.ReadUInt32();
                if (localVarSig != 0)
                {
                    var header = method.Header;
                    var tableStream = header.GetStream<TableStream>();

                    MetadataMember signature;
                    tableStream.TryResolveMember(new MetadataToken(localVarSig), out signature);
                    body.Signature = signature as StandAloneSignature;
                }

                if (hasSections)
                {
                    body._sectionReadingContext = context.CreateSubContext(reader.Position + codeSize);
                    body._sectionReadingContext.Reader.Align(4);
                }
            }
            else if ((bodyHeader & 0x2) == 0x2)
            {
                codeSize = (uint)(bodyHeader >> 2);
                body.MaxStack = 8;
            }
            else
                throw new ArgumentException("Invalid method body header signature.");

            body._msilReadingContext = context.CreateSubContext(reader.Position, (int)codeSize);
            return body;
        }
Exemplo n.º 10
0
        internal static ImageResourceDataEntry FromReadingContext(ReadingContext context)
        {
            var reader = context.Reader;
            var entry = new ImageResourceDataEntry
            {
                StartOffset = reader.Position,
                OffsetToData = reader.ReadUInt32(),
                Size = reader.ReadUInt32(),
                CodePage = reader.ReadUInt32(),
                Reserved = reader.ReadUInt32(),

            };

            entry._dataReaderContext = context.CreateSubContext(context.Assembly.RvaToFileOffset(entry.OffsetToData));

            return entry;
        }
Exemplo n.º 11
0
 public static X86MethodBody FromReadingContext(ReadingContext context)
 {
     var body = new X86MethodBody();
     body._readingContext = context.CreateSubContext(context.Reader.StartPosition);
     return body;
 }