/// <summary>
        /// Reads a single security attribute at the current position of the provided stream reader.
        /// </summary>
        /// <param name="image">The image the signature resides in.</param>
        /// <param name="reader">The reader to use.</param>
        /// <returns>The read attribute.</returns>
        public static SecurityAttributeSignature FromReader(MetadataImage image, IBinaryStreamReader reader)
        {
            var signature = new SecurityAttributeSignature
            {
                TypeName = reader.ReadSerString(),
            };

            reader.ReadCompressedUInt32();

            if (!reader.TryReadCompressedUInt32(out uint argumentCount))
            {
                return(signature);
            }

            if (argumentCount == 0)
            {
                return(signature);
            }

            for (int i = 0; i < argumentCount; i++)
            {
                signature.NamedArguments.Add(CustomAttributeNamedArgument.FromReader(image, reader));
            }

            return(signature);
        }
Ejemplo n.º 2
0
        protected string ReadString(IBinaryStreamReader reader)
        {
            var offset = (uint)(reader.Position - reader.StartPosition);

            string value;

            if (_cachedStrings.TryGetValue(offset, out value))
            {
                reader.Position += 1 + Encoding.Unicode.GetByteCount(value) + 1;
            }
            else
            {
                var length = reader.ReadCompressedUInt32();
                if (length <= 0)
                {
                    return(string.Empty);
                }

                value = Encoding.Unicode.GetString(reader.ReadBytes((int)length - 1));
                reader.Position++;

                _cachedStrings.Add(offset, value);
            }

            return(value);
        }
Ejemplo n.º 3
0
        public static new LocalVariableSignature FromReader(MetadataHeader header, IBinaryStreamReader reader)
        {
            var signature = new LocalVariableSignature()
            {
                StartOffset = reader.Position,
                Attributes = (CallingConventionAttributes)reader.ReadByte()
            };

            var count = reader.ReadCompressedUInt32();

            for (int i = 0; i < count; i++)
                signature.Variables.Add(VariableSignature.FromReader(header, reader));
            return signature;
        }
Ejemplo n.º 4
0
        public new static LocalVariableSignature FromReader(MetadataImage image, IBinaryStreamReader reader)
        {
            var signature = new LocalVariableSignature
            {
                Attributes = (CallingConventionAttributes)reader.ReadByte()
            };

            var count = reader.ReadCompressedUInt32();

            for (int i = 0; i < count; i++)
            {
                signature.Variables.Add(VariableSignature.FromReader(image, reader));
            }
            return(signature);
        }
        public static SecurityAttributeSignature FromReader(MetadataHeader header, IBinaryStreamReader reader)
        {
            var signature = new SecurityAttributeSignature()
            {
                StartOffset = reader.Position,
                TypeName = reader.ReadSerString(),
            };

            reader.ReadCompressedUInt32();

            uint argumentCount;
            if (!reader.TryReadCompressedUInt32(out argumentCount))
                return signature;

            if (argumentCount == 0)
                return signature;

            for (int i = 0; i < argumentCount; i++)
                signature.NamedArguments.Add(CustomAttributeNamedArgument.FromReader(header, reader));

            return signature;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Reads a single local variable signature at the current position of the provided stream reader.
        /// </summary>
        /// <param name="image">The image the signature was defined in.</param>
        /// <param name="reader">The reader to use.</param>
        /// <param name="readToEnd">Determines whether any extra data after the signature should be read and
        /// put into the <see cref="ExtendableBlobSignature.ExtraData"/> property.</param>
        /// <param name="protection">The recursion protection that is used to detect malicious loops in the metadata.</param>
        /// <returns>The read signature.</returns>
        public new static LocalVariableSignature FromReader(
            MetadataImage image,
            IBinaryStreamReader reader,
            bool readToEnd, RecursionProtection protection)
        {
            var signature = new LocalVariableSignature
            {
                Attributes = (CallingConventionAttributes)reader.ReadByte()
            };

            uint count = reader.ReadCompressedUInt32();

            for (int i = 0; i < count; i++)
            {
                signature.Variables.Add(VariableSignature.FromReader(image, reader, protection));
            }

            if (readToEnd)
            {
                signature.ExtraData = reader.ReadToEnd();
            }

            return(signature);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Reads a type signature from a blob reader.
        /// </summary>
        /// <param name="module">The module containing the blob signature.</param>
        /// <param name="reader">The blob signature reader.</param>
        /// <param name="protection">The object responsible for detecting infinite recursion.</param>
        /// <returns>The type signature.</returns>
        /// <exception cref="ArgumentOutOfRangeException">Occurs when the blob reader points to an element type that is
        /// invalid or unsupported.</exception>
        public static TypeSignature FromReader(ModuleDefinition module, IBinaryStreamReader reader,
                                               RecursionProtection protection)
        {
            var elementType = (ElementType)reader.ReadByte();

            switch (elementType)
            {
            case ElementType.Void:
            case ElementType.Boolean:
            case ElementType.Char:
            case ElementType.I1:
            case ElementType.U1:
            case ElementType.I2:
            case ElementType.U2:
            case ElementType.I4:
            case ElementType.U4:
            case ElementType.I8:
            case ElementType.U8:
            case ElementType.R4:
            case ElementType.R8:
            case ElementType.String:
            case ElementType.I:
            case ElementType.U:
            case ElementType.TypedByRef:
            case ElementType.Object:
                return(module.CorLibTypeFactory.FromElementType(elementType));

            case ElementType.ValueType:
                return(new TypeDefOrRefSignature(ReadTypeDefOrRef(module, reader, protection, false), true));

            case ElementType.Class:
                return(new TypeDefOrRefSignature(ReadTypeDefOrRef(module, reader, protection, false), false));

            case ElementType.Ptr:
                return(new PointerTypeSignature(FromReader(module, reader, protection)));

            case ElementType.ByRef:
                return(new ByReferenceTypeSignature(FromReader(module, reader, protection)));

            case ElementType.Var:
                return(new GenericParameterSignature(module, GenericParameterType.Type,
                                                     (int)reader.ReadCompressedUInt32()));

            case ElementType.MVar:
                return(new GenericParameterSignature(module, GenericParameterType.Method,
                                                     (int)reader.ReadCompressedUInt32()));

            case ElementType.Array:
                return(ArrayTypeSignature.FromReader(module, reader, protection));

            case ElementType.GenericInst:
                return(GenericInstanceTypeSignature.FromReader(module, reader, protection));

            case ElementType.FnPtr:
                throw new NotImplementedException();

            case ElementType.SzArray:
                return(new SzArrayTypeSignature(FromReader(module, reader, protection)));

            case ElementType.CModReqD:
                return(new CustomModifierTypeSignature(
                           ReadTypeDefOrRef(module, reader, protection, true),
                           true,
                           FromReader(module, reader, protection)));

            case ElementType.CModOpt:
                return(new CustomModifierTypeSignature(
                           ReadTypeDefOrRef(module, reader, protection, true),
                           false,
                           FromReader(module, reader, protection)));

            case ElementType.Sentinel:
                return(new SentinelTypeSignature());

            case ElementType.Pinned:
                return(new PinnedTypeSignature(FromReader(module, reader, protection)));

            case ElementType.Boxed:
                return(new BoxedTypeSignature(FromReader(module, reader, protection)));

            case ElementType.Internal:
                IntPtr address = IntPtr.Size switch
                {
                    4 => new IntPtr(reader.ReadInt32()),
                    _ => new IntPtr(reader.ReadInt64())
                };

                //Get Internal Method Through Reflection
                var GetTypeFromHandleUnsafeReflection = typeof(Type)
                                                        .GetMethod("GetTypeFromHandleUnsafe", ((BindingFlags)(-1)), null, new[] { typeof(IntPtr) },
                                                                   null);
                //Invoke It To Get The Value
                var Type = (Type)GetTypeFromHandleUnsafeReflection?.Invoke(null, new object[] { address });
                //Import it
                return(new TypeDefOrRefSignature(new ReferenceImporter(module).ImportType((Type))));

            default:
                throw new ArgumentOutOfRangeException($"Invalid or unsupported element type {elementType}.");
            }
        }