示例#1
0
        public new static CilRawFatMethodBody FromReader(IBinaryStreamReader reader)
        {
            var body = new CilRawFatMethodBody
            {
                _header  = reader.ReadUInt16(),
                MaxStack = reader.ReadUInt16()
            };

            int codeSize = reader.ReadInt32();

            body.LocalVarSigToken = reader.ReadUInt32();

            body.Code = reader.ReadBytes(codeSize);

            if (body.HasSections)
            {
                reader.Align(4);

                CilExtraSection section;
                do
                {
                    section = CilExtraSection.FromReader(reader);
                    body.ExtraSections.Add(section);
                } while (section.HasMoreSections);
            }

            return(body);
        }
示例#2
0
        public override FileSegment CreateRawMethodBody(MetadataBuffer buffer)
        {
            using (var codeStream = new MemoryStream())
            {
                WriteCode(buffer, new BinaryStreamWriter(codeStream));

                if (!IsFat)
                {
                    return(new CilRawSmallMethodBody()
                    {
                        Code = codeStream.ToArray()
                    });
                }

                var fatBody = new CilRawFatMethodBody
                {
                    HasSections      = ExceptionHandlers.Count > 0,
                    InitLocals       = InitLocals,
                    LocalVarSigToken = buffer.TableStreamBuffer.GetStandaloneSignatureToken(Signature).ToUInt32(),
                    MaxStack         = (ushort)MaxStack,
                    Code             = codeStream.ToArray()
                };

                if (ExceptionHandlers.Count > 0)
                {
                    using (var sectionStream = new MemoryStream())
                    {
                        var  sectionWriter = new BinaryStreamWriter(sectionStream);
                        bool useFatFormat  = ExceptionHandlers.Any(x => x.IsFatFormatRequired);
                        foreach (var handler in ExceptionHandlers)
                        {
                            handler.IsFat = useFatFormat;
                            handler.Write(buffer, sectionWriter);
                        }

                        fatBody.ExtraSections.Add(new CilExtraSection
                        {
                            IsExceptionHandler = true,
                            IsFat           = useFatFormat,
                            HasMoreSections = false,
                            Data            = sectionStream.ToArray()
                        });
                    }
                }

                return(fatBody);
            }
        }
示例#3
0
        /// <summary>
        /// Reads a raw method body from the given binary input stream.
        /// </summary>
        /// <param name="reader">The binary input stream to read from.</param>
        /// <returns>The raw method body.</returns>
        /// <exception cref="NotSupportedException">Occurs when the method header indicates an invalid or unsupported
        /// method body format.</exception>
        public static CilRawMethodBody FromReader(IBinaryStreamReader reader)
        {
            byte bodyHeader = reader.ReadByte();

            reader.Position--;

            if ((bodyHeader & 0x3) == 0x3)
            {
                return(CilRawFatMethodBody.FromReader(reader));
            }
            if ((bodyHeader & 0x2) == 0x2)
            {
                return(CilRawSmallMethodBody.FromReader(reader));
            }

            throw new NotSupportedException("Invalid or unsupported method body header.");
        }