예제 #1
0
        /// <summary>
        /// Reads a single property signature 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>
        /// <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 PropertySignature FromReader(
            MetadataImage image,
            IBinaryStreamReader reader,
            bool readToEnd,
            RecursionProtection protection)
        {
            var signature = new PropertySignature
            {
                Attributes = (CallingConventionAttributes)reader.ReadByte(),
            };

            if (!reader.TryReadCompressedUInt32(out uint paramCount))
            {
                return(null);
            }

            signature.PropertyType = TypeSignature.FromReader(image, reader, false, protection);

            for (int i = 0; i < paramCount; i++)
            {
                signature.Parameters.Add(ParameterSignature.FromReader(image, reader, protection));
            }

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

            return(signature);
        }
예제 #2
0
        /// <summary>
        /// Reads a single marshal descriptor at the current position of the provided binary stream reader.
        /// </summary>
        /// <param name="reader">The reader to use.</param>
        /// <param name="readToEnd">
        /// Determines whether any extra data appearing after the signature should be read and
        /// put in the <see cref="ExtendableBlobSignature.ExtraData"/> property.
        /// </param>
        /// <returns>The read marshal descriptor.</returns>
        public static MarshalDescriptor FromReader(IBinaryStreamReader reader, bool readToEnd = false)
        {
            var descriptor = ReadMarshalDescriptor(reader);

            if (readToEnd)
            {
                descriptor.ExtraData = reader.ReadToEnd();
            }
            return(descriptor);
        }
예제 #3
0
        /// <summary>
        /// Reads a single type signature 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>
        /// <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 static TypeSignature FromReader(MetadataImage image, IBinaryStreamReader reader, bool readToEnd, RecursionProtection protection)
        {
            var signature = ReadTypeSignature(image, reader, protection);

            if (signature != null && readToEnd)
            {
                signature.ExtraData = reader.ReadToEnd();
            }
            return(signature);
        }
예제 #4
0
 /// <summary>
 /// Reads a single field signature at the current position of the provided stream reader.
 /// </summary>
 /// <param name="image">The image the field is 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 FieldSignature FromReader(
     MetadataImage image,
     IBinaryStreamReader reader,
     bool readToEnd,
     RecursionProtection protection)
 {
     return(new FieldSignature
     {
         Attributes = (CallingConventionAttributes)reader.ReadByte(),
         FieldType = TypeSignature.FromReader(image, reader, false, protection),
         ExtraData = readToEnd ? reader.ReadToEnd() : null
     });
 }
        /// <summary>
        /// Reads a single calling convention signature at the current position of the provided stream reader.
        /// </summary>
        /// <param name="image">The image the signature is 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 static CallingConventionSignature FromReader(
            MetadataImage image,
            IBinaryStreamReader reader,
            bool readToEnd,
            RecursionProtection protection)
        {
            var signature = ReadSignature(image, reader, protection);

            if (readToEnd)
            {
                signature.ExtraData = reader.ReadToEnd();
            }
            return(signature);
        }
예제 #6
0
        /// <summary>
        /// Reads a single method signature at the current position of the provided stream reader.
        /// </summary>
        /// <param name="image">The image the field is 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 MethodSignature FromReader(
            MetadataImage image,
            IBinaryStreamReader reader,
            bool readToEnd,
            RecursionProtection protection)
        {
            if (!reader.CanRead(sizeof(byte)))
            {
                return(null);
            }

            var signature = new MethodSignature
            {
                Attributes = (CallingConventionAttributes)reader.ReadByte()
            };

            if (signature.IsGeneric)
            {
                if (!reader.TryReadCompressedUInt32(out uint genericParameterCount))
                {
                    return(signature);
                }
                signature.GenericParameterCount = (int)genericParameterCount;
            }

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

            signature.ReturnType = TypeSignature.FromReader(image, reader);

            for (int i = 0; i < parameterCount; i++)
            {
                signature.Parameters.Add(ParameterSignature.FromReader(image, reader, protection));
            }

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

            return(signature);
        }
예제 #7
0
        /// <summary>
        /// Reads a marshal descriptor signature from the provided input stream.
        /// </summary>
        /// <param name="parentModule">The module that defines the marshal descriptor</param>
        /// <param name="reader">The input stream.</param>
        /// <returns>The marshal descriptor.</returns>
        public static MarshalDescriptor FromReader(ModuleDefinition parentModule, IBinaryStreamReader reader)
        {
            var nativeType = (NativeType)reader.ReadByte();
            MarshalDescriptor descriptor = nativeType switch
            {
                NativeType.SafeArray => SafeArrayMarshalDescriptor.FromReader(parentModule, reader),
                NativeType.FixedArray => FixedArrayMarshalDescriptor.FromReader(reader),
                NativeType.LPArray => LPArrayMarshalDescriptor.FromReader(reader),
                NativeType.CustomMarshaller => CustomMarshalDescriptor.FromReader(parentModule, reader),
                NativeType.FixedSysString => FixedSysStringMarshalDescriptor.FromReader(reader),
                NativeType.Interface => ComInterfaceMarshalDescriptor.FromReader(nativeType, reader),
                NativeType.IDispatch => ComInterfaceMarshalDescriptor.FromReader(nativeType, reader),
                NativeType.IUnknown => ComInterfaceMarshalDescriptor.FromReader(nativeType, reader),
                _ => new SimpleMarshalDescriptor(nativeType)
            };

            descriptor.ExtraData = reader.ReadToEnd();
            return(descriptor);
        }
        /// <summary>
        /// Reads a single custom attribute at the current position of the provided stream reader.
        /// </summary>
        /// <param name="parent">The parent custom attribute the signature is associated to.</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>
        /// <returns>The read argument.</returns>
        public static CustomAttributeSignature FromReader(
            CustomAttribute parent,
            IBinaryStreamReader reader,
            bool readToEnd = false)
        {
            if (!reader.CanRead(sizeof(ushort)) || reader.ReadUInt16() != 0x0001)
            {
                throw new ArgumentException("Signature doesn't refer to a valid custom attribute signature.");
            }

            var signature = new CustomAttributeSignature();

            if (parent.Constructor?.Signature is MethodSignature methodSignature)
            {
                foreach (var parameter in methodSignature.Parameters)
                {
                    signature.FixedArguments.Add(CustomAttributeArgument.FromReader(
                                                     parent.Image,
                                                     parameter.ParameterType,
                                                     reader));
                }
            }

            var namedElementCount = reader.CanRead(sizeof(ushort)) ? reader.ReadUInt16() : 0;

            for (uint i = 0; i < namedElementCount; i++)
            {
                signature.NamedArguments.Add(CustomAttributeNamedArgument.FromReader(parent.Image, reader));
            }

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

            return(signature);
        }
예제 #9
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);
        }
예제 #10
0
 /// <summary>
 /// Reads a single data blob signature from the provided input stream.
 /// </summary>
 /// <param name="reader">The input stream.</param>
 /// <returns>The blob signature.</returns>
 public static DataBlobSignature FromReader(IBinaryStreamReader reader)
 {
     return(new DataBlobSignature(reader.ReadToEnd()));
 }
예제 #11
0
 public IMetadataStream ReadStream(MetadataStreamHeader header, IBinaryStreamReader reader)
 {
     return(header.Name == KoiStreamName
         ? new KoiStream(KoiStreamName, new DataSegment(reader.ReadToEnd()), Logger)
         : _reader.ReadStream(header, reader));
 }