Example #1
0
        internal static MethodSignature Decode(BufferedBinaryReader reader, SignatureFlags flags)
        {
            var type = (flags & SignatureFlags.TYPEMASK);

            if (!(type == SignatureFlags.PROPERTY || (type >= SignatureFlags.DEFAULT && type <= SignatureFlags.VARARG)))
            {
                throw new BadMetadataException("Incorrect signature for method.");
            }

            var sig = new MethodSignature();

            if (type == SignatureFlags.PROPERTY)
            {
                sig.IsProperty = true;
            }

            if ((flags & SignatureFlags.HASTHIS) != 0)
            {
                sig.CallingConventions |= CallingConventions.HasThis;
            }
            if ((flags & SignatureFlags.EXPLICITTHIS) != 0)
            {
                sig.CallingConventions |= CallingConventions.ExplicitThis;
            }
            if (type == SignatureFlags.VARARG)
            {
                sig.CallingConventions &= CallingConventions.Standard;
                sig.CallingConventions |= CallingConventions.VarArgs;
            }

            if ((flags & SignatureFlags.GENERIC) != 0)
            {
                sig.GenericParamCount = reader.ReadPackedInt();
            }

            int paramCount = reader.ReadPackedInt();

            sig.Type   = TypeSignature.Decode(reader);
            sig.Params = new TypeSignature[paramCount];
            for (int i = 0; i < paramCount; ++i)
            {
                sig.Params[i] = TypeSignature.Decode(reader);
            }
            return(sig);
        }
Example #2
0
        public static string ReadCountedUtf8(this BufferedBinaryReader reader)
        {
            int b0 = reader.ReadUInt8();

            if (b0 == 0xFF)
            {
                return(null);
            }
            if (b0 == 0)
            {
                return(string.Empty);
            }
            int len = reader.ReadPackedInt(b0);

            return(reader.ReadUtf8(len));
        }
Example #3
0
        private IType[] ReadMethodSpecArgs(BufferedBinaryReader reader, Context context)
        {
            if (reader.ReadByte() != 0x0A)
            {
                throw new BadSignatureException("Invalid MethodSpec signature");
            }

            int n    = reader.ReadPackedInt();
            var args = new IType[n];

            for (int i = 0; i < n; ++i)
            {
                var sig = TypeSignature.Decode(reader);
                args[i] = _loader.ResolveType(sig, context);
            }

            return(args);
        }
Example #4
0
        public BufferedBinaryReader Fetch(uint offset)
        {
            if (offset >= _heap.Length)
            {
                throw new BadMetadataException("Invalid #Blob heap offset.");
            }

            _heap.Seek(offset, SeekOrigin.Begin);

            int length = _heap.ReadPackedInt();

            if (length <= 0)
            {
                return(Zero);
            }

            //TODO: implement slice without need to know that _heap is another slice
            return(_heap.Slice(0, length));
        }
Example #5
0
        public string Fetch(uint offset)
        {
            if (offset == 0)
            {
                throw new BadMetadataException("Invalid #US heap offset.");
            }


            _heap.Seek(offset, SeekOrigin.Begin);

            int length = _heap.ReadPackedInt();
            var bytes  = _heap.ReadBytes(length);

            if (bytes[length - 1] == 0 || bytes[length - 1] == 1)
            {
                length--;
            }

            return(Encoding.Unicode.GetString(bytes, 0, length));
        }
Example #6
0
        public static TypeSignature Decode(BufferedBinaryReader reader)
        {
            var e = (ElementType)reader.ReadPackedInt();

            switch (e)
            {
            case ElementType.End:
            case ElementType.Void:
            case ElementType.Boolean:
            case ElementType.Char:
            case ElementType.Int8:
            case ElementType.UInt8:
            case ElementType.Int16:
            case ElementType.UInt16:
            case ElementType.Int32:
            case ElementType.UInt32:
            case ElementType.Int64:
            case ElementType.UInt64:
            case ElementType.Single:
            case ElementType.Double:
            case ElementType.String:
            case ElementType.TypedReference:
            case ElementType.IntPtr:
            case ElementType.UIntPtr:
            case ElementType.Object:
                return(new TypeSignature(e));

            case ElementType.Ptr:
            case ElementType.ByRef:
            {
                var type = Decode(reader);
                if (type == null)
                {
                    throw new BadSignatureException(String.Format("Unable to decode type of {0} signature.", e));
                }
                return(new TypeSignature(e, type));
            }

            case ElementType.ValueType:
            case ElementType.Class:
            case ElementType.CustomArgsEnum:
            {
                var index = DecodeTypeDefOrRef(reader);
                return(new TypeSignature(e, index));
            }

            case ElementType.Array:
            {
                var type = Decode(reader);
                if (type == null)
                {
                    throw new BadSignatureException("Unable to decode type of array.");
                }

                int rank     = reader.ReadPackedInt();
                int numSizes = reader.ReadPackedInt();
                var info     = new ArrayShape {
                    Rank = rank, Sizes = new int[numSizes]
                };
                for (int i = 0; i < numSizes; i++)
                {
                    info.Sizes[i] = reader.ReadPackedInt();
                }

                int numLoBounds = reader.ReadPackedInt();
                info.LoBounds = new int[numLoBounds];
                for (int i = 0; i < numLoBounds; i++)
                {
                    info.LoBounds[i] = reader.ReadPackedInt();
                }

                return(new TypeSignature(e, type, info));
            }

            case ElementType.ArraySz:
            {
                var type = Decode(reader);
                if (type == null)
                {
                    throw new BadSignatureException("Unable to decode type of single-dimensional array signature.");
                }
                return(new TypeSignature(e, type, ArrayShape.Single));
            }

            case ElementType.MethodPtr:
            {
                var msig = MethodSignature.Decode(reader);
                return(new TypeSignature(e, msig));
            }

            case ElementType.RequiredModifier:
            case ElementType.OptionalModifier:
            {
                var i    = DecodeTypeDefOrRef(reader);
                var type = Decode(reader);
                return(new TypeSignature(e, i, type));
            }

            case ElementType.Sentinel:
            case ElementType.Pinned:
            {
                var type = Decode(reader);
                return(new TypeSignature(e, type));
            }

            case ElementType.GenericInstantiation:
            {
                var type          = Decode(reader);
                int n             = reader.ReadPackedInt();
                var genericParams = new TypeSignature[n];
                for (int i = 0; i < n; i++)
                {
                    genericParams[i] = Decode(reader);
                }
                return(new TypeSignature(e, type, genericParams));
            }

            case ElementType.Var:
            case ElementType.MethodVar:
            {
                int num = reader.ReadPackedInt();
                return(new TypeSignature(e, num));
            }
            }

            throw new BadSignatureException("Unknown element type.");
        }
Example #7
0
        private static SimpleIndex DecodeTypeDefOrRef(BufferedBinaryReader reader)
        {
            var token = (uint)reader.ReadPackedInt();

            return(CodedIndex.TypeDefOrRef.Decode(token));
        }