Example #1
0
        public PropertySignature ReadPropertySignature(uint signature, PropertyDefinition parentProperty)
        {
            PropertySignature   propertySig = null;
            BlobSignatureReader reader;

            if (TryGetBlobReader(signature, out reader))
            {
                using (reader)
                {
                    reader.GenericContext = parentProperty.DeclaringType;

                    byte flag = reader.ReadByte();

                    if ((flag & 8) == 0)
                    {
                        throw new ArgumentException("Signature doesn't refer to a valid property signature.");
                    }

                    propertySig         = new PropertySignature();
                    propertySig.HasThis = (flag & 0x20) != 0;
                    NETGlobals.ReadCompressedUInt32(reader);
                    propertySig.ReturnType = reader.ReadTypeReference();
                }
            }
            return(propertySig);
        }
Example #2
0
        public VariableDefinition[] ReadVariableSignature(uint signature, MethodDefinition parentMethod)
        {
            VariableDefinition[] variables = null;
            using (BlobSignatureReader reader = GetBlobReader(signature))
            {
                reader.GenericContext = parentMethod;

                byte local_sig = reader.ReadByte();

                if (local_sig != 0x7)
                {
                    throw new ArgumentException("Signature doesn't refer to a valid local variable signature");
                }

                uint count = NETGlobals.ReadCompressedUInt32(reader);

                if (count == 0)
                {
                    return(null);
                }

                variables = new VariableDefinition[count];

                for (int i = 0; i < count; i++)
                {
                    variables[i] = new VariableDefinition(i, ReadTypeReference(reader, (ElementType)reader.ReadByte()));
                }
            }
            return(variables);
        }
Example #3
0
        public IMemberSignature ReadMemberRefSignature(uint sig, IGenericContext context)
        {
            IMemberSignature    signature = null;
            BlobSignatureReader reader;

            if (TryGetBlobReader(sig, context, out reader))
            {
                using (reader)
                {
                    byte flag = reader.ReadByte();

                    if (flag == 0x6)
                    {
                        FieldSignature fieldsignature = new FieldSignature();
                        fieldsignature.ReturnType = reader.ReadTypeReference((ElementType)reader.ReadByte());
                        signature = fieldsignature;
                    }
                    else
                    {
                        MethodSignature methodsignature = new MethodSignature();

                        if ((flag & 0x20) != 0)
                        {
                            methodsignature.HasThis = true;
                            flag = (byte)(flag & -33);
                        }
                        if ((flag & 0x40) != 0)
                        {
                            methodsignature.ExplicitThis = true;
                            flag = (byte)(flag & -65);
                        }
                        if ((flag & 0x10) != 0)
                        {
                            int genericsig = NETGlobals.ReadCompressedInt32(reader);
                            if (!context.IsDefinition)
                            {
                                AddMissingGenericParameters(context.Method, genericsig - 1);
                            }
                        }
                        methodsignature.CallingConvention = (MethodCallingConvention)flag;

                        uint paramCount = NETGlobals.ReadCompressedUInt32(reader);
                        methodsignature.ReturnType = reader.ReadTypeReference();

                        ParameterReference[] parameters = new ParameterReference[paramCount];
                        for (int i = 0; i < paramCount; i++)
                        {
                            parameters[i] = new ParameterReference()
                            {
                                ParameterType = reader.ReadTypeReference((ElementType)reader.ReadByte())
                            };
                        }
                        methodsignature.Parameters = parameters;

                        signature = methodsignature;
                    }
                }
            }
            return(signature);
        }
Example #4
0
        /// <summary>
        /// Gets a string by its offset.
        /// </summary>
        /// <param name="offset">The offset of the string.</param>
        /// <returns></returns>
        public string GetStringByOffset(uint offset)
        {
            string stringValue;

            if (readStrings.TryGetValue(offset, out stringValue))
            {
                return(stringValue);
            }

            mainStream.Seek(offset, SeekOrigin.Begin);

            uint length = (uint)(NETGlobals.ReadCompressedUInt32(binReader) & -2);

            if (length == 0)
            {
                readStrings.Add(offset, string.Empty);
                return(string.Empty);
            }

            char[] chars = new char[length / 2];

            for (int i = 0; i < length; i += 2)
            {
                chars[i / 2] = (char)binReader.ReadInt16();
            }


            stringValue = new string(chars);
            readStrings.Add(offset, stringValue);
            return(stringValue);
        }
Example #5
0
        //internal override void Reconstruct()
        //{
        //    MemoryStream newStream = new MemoryStream();
        //    BinaryWriter writer = new BinaryWriter(newStream);
        //    writer.Write((byte)0);
        //    ReadAllStrings();
        //    foreach (var readString in readStrings)
        //    {
        //        byte[] bytes = Encoding.Unicode.GetBytes(readString.Value);
        //        NETGlobals.WriteCompressedUInt32(writer, (uint)bytes.Length + 1); // length + terminator length
        //        writer.Write(bytes); // data
        //        writer.Write((byte)0); // terminator
        //    }
        //    binaryreader.Dispose();
        //    stream.Dispose();
        //    stream = newStream;
        //    binaryreader = new BinaryReader(newStream);
        //    this.streamHeader.Size = (uint)newStream.Length;
        //    this.contents = newStream.ToArray();
        //}

        internal void ReadAllStrings()
        {
            mainStream.Seek(0, SeekOrigin.Begin);

            uint lastPosition = (uint)mainStream.Position;

            while (mainStream.Position + 1 < mainStream.Length)
            {
                // TODO: write string.empty strings..

                bool   alreadyExisted = readStrings.ContainsKey((uint)mainStream.Position + 1);
                string value          = GetStringByOffset((uint)mainStream.Position + 1);


                int length = value.Length * 2;
                if (length == 0 && lastPosition == (uint)mainStream.Position)
                {
                    mainStream.Seek(1, SeekOrigin.Current);
                }
                if (alreadyExisted)
                {
                    mainStream.Seek(length + NETGlobals.GetCompressedUInt32Size((uint)length) + 1, SeekOrigin.Current);
                }

                lastPosition = (uint)mainStream.Position;
            }

            hasReadAllStrings = true;
            newEntryOffset    = (uint)mainStream.Length;
        }
Example #6
0
        public IMemberSignature ReadMemberRefSignature(uint sig, IGenericContext context)
        {
            IMemberSignature signature = null;

            using (BlobSignatureReader reader = GetBlobReader(sig, context))
            {
                byte flag = reader.ReadByte();

                if (flag == 0x6)
                {
                    FieldSignature fieldsignature = new FieldSignature();
                    fieldsignature.ReturnType = ReadTypeReference(reader, (ElementType)reader.ReadByte());
                    signature = fieldsignature;
                }
                else
                {
                    MethodSignature methodsignature = new MethodSignature();

                    if ((flag & 0x20) != 0)
                    {
                        methodsignature.HasThis = true;
                        flag = (byte)(flag & -33);
                    }
                    if ((flag & 0x40) != 0)
                    {
                        methodsignature.ExplicitThis = true;
                        flag = (byte)(flag & -65);
                    }
                    if ((flag & 0x10) != 0)
                    {
                        uint genericsig = NETGlobals.ReadCompressedUInt32(reader);
                    }
                    methodsignature.CallingConvention = (MethodCallingConvention)flag;

                    uint        num3 = NETGlobals.ReadCompressedUInt32(reader);
                    ElementType type = (ElementType)reader.ReadByte();

                    methodsignature.ReturnType = ReadTypeReference(reader, type);

                    if (num3 != 0)
                    {
                        ParameterReference[] parameters = new ParameterReference[num3];
                        for (int i = 0; i < num3; i++)
                        {
                            parameters[i] = new ParameterReference()
                            {
                                ParameterType = ReadTypeReference(reader, (ElementType)reader.ReadByte())
                            };
                        }
                        methodsignature.Parameters = parameters;
                    }

                    signature = methodsignature;
                }
            }
            return(signature);
        }
Example #7
0
        private TypeReference ReadTypeToken(BlobSignatureReader reader)
        {
            TypeReference typeRef = netheader.TablesHeap.TypeDefOrRef.GetMember((int)NETGlobals.ReadCompressedUInt32(reader)) as TypeReference;

            if (typeRef is ISpecification)
            {
                typeRef = (typeRef as TypeSpecification).TransformWith(reader.GenericContext) as TypeReference;
            }
            return(typeRef);
        }
Example #8
0
        private object ReadElement(BlobSignatureReader reader, TypeReference paramType)
        {
            switch (paramType.elementType)
            {
            case ElementType.I1:
                return(reader.ReadSByte());

            case ElementType.I2:
                return(reader.ReadInt16());

            case ElementType.I4:
                return(reader.ReadInt32());

            case ElementType.I8:
                return(reader.ReadInt64());

            case ElementType.U1:
                return(reader.ReadByte());

            case ElementType.U2:
                return(reader.ReadInt16());

            case ElementType.U4:
                return(reader.ReadInt32());

            case ElementType.U8:
                return(reader.ReadInt64());

            case ElementType.R4:
                return(reader.ReadSingle());

            case ElementType.R8:
                return(reader.ReadDouble());

            case ElementType.Type:
            case ElementType.String:
                uint size = NETGlobals.ReadCompressedUInt32(reader);
                if (size == 0xFF)
                {
                    return(string.Empty);
                }
                byte[] rawdata = reader.ReadBytes((int)size);
                return(Encoding.UTF8.GetString(rawdata));

            case ElementType.Char:
                return(reader.ReadChar());

                throw new NotSupportedException();

            case ElementType.Boolean:
                return(reader.ReadByte() == 1);
            }
            return(null);
        }
Example #9
0
        private void ReadGenericInstanceSignature(BlobSignatureReader reader, GenericInstanceType genericType)
        {
            uint number = NETGlobals.ReadCompressedUInt32(reader);

            genericType.genericArguments = new TypeReference[number];

            for (int i = 0; i < number; i++)
            {
                genericType.genericArguments[i] = ReadTypeReference(reader, (ElementType)reader.ReadByte());
            }
        }
Example #10
0
        public TypeReference ReadTypeSignature(uint signature, IGenericContext paramProvider)
        {
            TypeReference typeRef = null;

            using (BlobSignatureReader reader = GetBlobReader(signature))
            {
                reader.GenericContext = paramProvider;
                typeRef = ReadTypeReference(reader, (ElementType)NETGlobals.ReadCompressedUInt32(reader));
            }

            return(typeRef);
        }
        public string ReadUtf8String()
        {
            if (ReadByte() == 0xFF)
            {
                return(string.Empty);
            }
            BaseStream.Seek(-1, SeekOrigin.Current);
            uint size = NETGlobals.ReadCompressedUInt32(this);

            byte[] rawdata = this.ReadBytes((int)size);
            return(Encoding.UTF8.GetString(rawdata));
        }
        public TypeReference[] ReadGenericArguments()
        {
            uint number = NETGlobals.ReadCompressedUInt32(this);

            var genericArguments = new TypeReference[number];

            for (int i = 0; i < number; i++)
            {
                genericArguments[i] = ReadTypeReference();
            }

            return(genericArguments);
        }
        public TypeReference ReadTypeToken()
        {
            TypeReference typeRef;

            if (_netHeader.TablesHeap.TypeDefOrRef.TryGetMember((int)NETGlobals.ReadCompressedUInt32(this), out typeRef))
            {
                if (typeRef is ISpecification)
                {
                    typeRef = (typeRef as TypeSpecification).TransformWith(GenericContext) as TypeReference;
                }
            }

            return(typeRef);
        }
Example #14
0
        /// <summary>
        /// Gets the blob value by it's signature/index.
        /// </summary>
        /// <param name="index">The index or signature to get the blob value from.</param>
        /// <returns></returns>
        public byte[] GetBlob(uint index)
        {
            byte[] bytes = null;
            if (readBlobs.TryGetValue(index, out bytes))
            {
                return(bytes);
            }

            mainStream.Seek(index, SeekOrigin.Begin);
            int length = (int)NETGlobals.ReadCompressedUInt32(binReader);

            bytes = binReader.ReadBytes(length);
            readBlobs.Add(index, bytes);
            return(bytes);
        }
Example #15
0
        public uint GetBlobIndex(byte[] blobValue)
        {
            ReadAllBlobs();

            if (readBlobs.ContainsValue(blobValue))
            {
                return(readBlobs.FirstOrDefault(b => b.Value == blobValue).Key);
            }

            mainStream.Seek(0, SeekOrigin.End);
            uint index = (uint)mainStream.Position;

            NETGlobals.WriteCompressedUInt32(binWriter, (uint)blobValue.Length);
            binWriter.Write(blobValue);
            readBlobs.Add(index, blobValue);
            return(index);
        }
Example #16
0
        private ArrayType ReadArrayType(BlobSignatureReader reader)
        {
            TypeReference arrayType = ReadTypeReference(reader, (ElementType)reader.ReadByte());
            uint          rank      = NETGlobals.ReadCompressedUInt32(reader);

            uint[] upperbounds = new uint[NETGlobals.ReadCompressedUInt32(reader)];

            for (int i = 0; i < upperbounds.Length; i++)
            {
                upperbounds[i] = NETGlobals.ReadCompressedUInt32(reader);
            }

            int[] lowerbounds = new int[NETGlobals.ReadCompressedUInt32(reader)];

            for (int i = 0; i < lowerbounds.Length; i++)
            {
                lowerbounds[i] = NETGlobals.ReadCompressedInt32(reader);
            }


            ArrayDimension[] dimensions = new ArrayDimension[rank];

            for (int i = 0; i < rank; i++)
            {
                int?lower = null;
                int?upper = null;

                if (i < lowerbounds.Length)
                {
                    lower = new int?(lowerbounds[i]);
                }

                if (i < upperbounds.Length)
                {
                    int x = (int)upperbounds[i];
                    upper = (lower.HasValue ? new int?(lower.GetValueOrDefault() + x) : 0) - 1;
                }
                ArrayDimension dimension = new ArrayDimension(lower, upper);
                dimensions[i] = dimension;
            }



            return(new ArrayType(arrayType, (int)rank, dimensions));
        }
Example #17
0
        public TypeReference[] ReadGenericArgumentsSignature(uint signature, IGenericContext context)
        {
            using (BlobSignatureReader reader = GetBlobReader(signature, context))
            {
                if (reader.ReadByte() == 0xa)
                {
                    uint            count = NETGlobals.ReadCompressedUInt32(reader);
                    TypeReference[] types = new TypeReference[count];
                    for (int i = 0; i < count; i++)
                    {
                        types[i] = ReadTypeReference(reader, (ElementType)reader.ReadByte());
                    }

                    return(types);
                }
            }
            throw new ArgumentException("Signature doesn't point to a valid generic arguments signature");
        }
Example #18
0
        internal void ReadAllBlobs()
        {
            mainStream.Seek(1, SeekOrigin.Begin);
            while (mainStream.Position < mainStream.Length)
            {
                bool   alreadyExisted = readBlobs.ContainsKey((uint)mainStream.Position);
                byte[] value          = GetBlob((uint)mainStream.Position);

                int length = value.Length;
                if (length == 0)
                {
                    break;
                }
                if (alreadyExisted)
                {
                    mainStream.Seek(length + NETGlobals.GetCompressedUInt32Size((uint)length), SeekOrigin.Current);
                }
            }
        }
Example #19
0
        private TypeReference ReadGenericType(BlobSignatureReader reader)
        {
            // not finished yet!

            uint   token = NETGlobals.ReadCompressedUInt32(reader);
            object genericType;

            if (reader.GenericContext.IsDefinition)
            {
                if (TryGetArrayValue(reader.GenericContext.GenericParameters, token, out genericType))
                {
                    return(genericType as TypeReference);
                }
            }

            if (TryGetArrayValue(reader.GenericContext.GenericArguments, token, out genericType))
            {
                return(genericType as TypeReference);
            }

            return(new TypeReference(string.Empty, token.ToString(), null));
        }
Example #20
0
        internal void Reconstruct()
        {
            // will be removed once blobs are being serialized.

            MemoryStream newStream = new MemoryStream();
            BinaryWriter writer    = new BinaryWriter(newStream);

            writer.Write((byte)0);
            ReadAllBlobs();

            foreach (var blob in readBlobs)
            {
                NETGlobals.WriteCompressedUInt32(writer, (uint)blob.Value.Length);
                writer.Write(blob.Value);
            }

            mainStream.Dispose();
            binReader.Dispose();
            binWriter.Dispose();
            mainStream             = newStream;
            binReader              = new BinaryReader(newStream);
            binWriter              = new BinaryWriter(newStream);
            this.streamHeader.Size = (uint)newStream.Length;
        }
        public TypeReference ReadTypeReference(ElementType type)
        {
            switch (type)
            {
            case ElementType.Void:
                return(_netHeader.TypeSystem.Void);

            case ElementType.I:
                return(_netHeader.TypeSystem.IntPtr);

            case ElementType.I1:
                return(_netHeader.TypeSystem.Int8);

            case ElementType.I2:
                return(_netHeader.TypeSystem.Int16);

            case ElementType.I4:
                return(_netHeader.TypeSystem.Int32);

            case ElementType.I8:
                return(_netHeader.TypeSystem.Int64);

            case ElementType.U:
                return(_netHeader.TypeSystem.UIntPtr);

            case ElementType.U1:
                return(_netHeader.TypeSystem.UInt8);

            case ElementType.U2:
                return(_netHeader.TypeSystem.UInt16);

            case ElementType.U4:
                return(_netHeader.TypeSystem.UInt32);

            case ElementType.U8:
                return(_netHeader.TypeSystem.UInt64);

            case ElementType.Object:
                return(_netHeader.TypeSystem.Object);

            case ElementType.R4:
                return(_netHeader.TypeSystem.Single);

            case ElementType.R8:
                return(_netHeader.TypeSystem.Double);

            case ElementType.String:
                return(_netHeader.TypeSystem.String);

            case ElementType.Char:
                return(_netHeader.TypeSystem.Char);

            case ElementType.Type:
                return(_netHeader.TypeSystem.Type);

            case ElementType.Boolean:
                return(_netHeader.TypeSystem.Boolean);

            case ElementType.Ptr:
                return(new PointerType(ReadTypeReference((ElementType)this.ReadByte())));

            case ElementType.MVar:
                return(GetGenericParameter(GenericParamType.Method, (int)NETGlobals.ReadCompressedUInt32(this)));

            case ElementType.Var:
                return(GetGenericParameter(GenericParamType.Type, (int)NETGlobals.ReadCompressedUInt32(this)));

            case ElementType.Array:
                return(ReadArrayType());

            case ElementType.SzArray:
                return(new ArrayType(ReadTypeReference((ElementType)this.ReadByte())));

            case ElementType.Class:
                TypeReference typeRef;
                if (_netHeader.TablesHeap.TypeDefOrRef.TryGetMember((int)NETGlobals.ReadCompressedUInt32(this), out typeRef))
                {
                    return(typeRef);
                }
                break;

            case ElementType.ValueType:
                if (_netHeader.TablesHeap.TypeDefOrRef.TryGetMember((int)NETGlobals.ReadCompressedUInt32(this), out typeRef))
                {
                    typeRef.IsValueType = true;
                    return(typeRef);
                }
                break;

            case ElementType.ByRef:
                return(new ByReferenceType(ReadTypeReference((ElementType)this.ReadByte())));

            case ElementType.Pinned:
                return(new PinnedType(ReadTypeReference((ElementType)this.ReadByte())));

            case ElementType.GenericInst:
                bool                isValueType = this.ReadByte() == 0x11;
                TypeReference       reference2  = ReadTypeToken();
                GenericInstanceType instance    = new GenericInstanceType(reference2);
                instance._genericArguments = ReadGenericArguments();
                instance.IsValueType       = isValueType;

                return(instance);

            case ElementType.CModOpt:
                return(new CustomModifierType(ReadTypeToken(), ReadTypeReference(), false));

            case ElementType.CModReqD:
                return(new CustomModifierType(ReadTypeToken(), ReadTypeReference(), true));
            }
            return(new TypeReference(string.Empty, type.ToString(), null)
            {
                _netheader = this._netHeader
            });
        }
Example #22
0
        private TypeReference ReadTypeReference(BlobSignatureReader reader, ElementType type)
        {
            switch (type)
            {
            case ElementType.Void:
                return(netheader.TypeSystem.Void);

            case ElementType.I:
                return(netheader.TypeSystem.IntPtr);

            case ElementType.I1:
                return(netheader.TypeSystem.Int8);

            case ElementType.I2:
                return(netheader.TypeSystem.Int16);

            case ElementType.I4:
                return(netheader.TypeSystem.Int32);

            case ElementType.I8:
                return(netheader.TypeSystem.Int64);

            case ElementType.U:
                return(netheader.TypeSystem.UIntPtr);

            case ElementType.U1:
                return(netheader.TypeSystem.UInt8);

            case ElementType.U2:
                return(netheader.TypeSystem.UInt16);

            case ElementType.U4:
                return(netheader.TypeSystem.UInt32);

            case ElementType.U8:
                return(netheader.TypeSystem.UInt64);

            case ElementType.Object:
                return(netheader.TypeSystem.Object);

            case ElementType.R4:
                return(netheader.TypeSystem.Single);

            case ElementType.R8:
                return(netheader.TypeSystem.Double);

            case ElementType.String:
                return(netheader.TypeSystem.String);

            case ElementType.Char:
                return(netheader.TypeSystem.Char);

            case ElementType.Type:
                return(netheader.TypeSystem.Type);

            case ElementType.Boolean:
                return(netheader.TypeSystem.Boolean);

            case ElementType.Ptr:
                return(new PointerType(ReadTypeReference(reader, (ElementType)reader.ReadByte())));

            case ElementType.MVar:

                if (reader.GenericContext == null)
                {
                    return(new GenericParamReference(NETGlobals.ReadCompressedInt32(reader), new TypeReference(string.Empty, "MVar", null)
                    {
                        elementType = ElementType.MVar, @namespace = "", netheader = this.netheader
                    }));
                }

                return(ReadGenericType(reader));

            case ElementType.Var:
                uint token = NETGlobals.ReadCompressedUInt32(reader);
                if (reader.GenericContext != null)
                {
                    if (reader.GenericContext.DeclaringType != null && reader.GenericContext.DeclaringType.GenericParameters != null && reader.GenericContext.DeclaringType.GenericParameters.Length > token)
                    {
                        return(reader.GenericContext.DeclaringType.GenericParameters[token]);
                    }
                    else if (reader.GenericContext.GenericParameters != null && reader.GenericContext.GenericParameters.Length > token)
                    {
                        return(reader.GenericContext.GenericParameters[token]);
                    }
                }
                return(new GenericParamReference((int)token, new TypeReference(string.Empty, "Var", null)
                {
                    elementType = ElementType.Var, @namespace = "", netheader = this.netheader
                }));

            case ElementType.Array:
                return(ReadArrayType(reader));

            case ElementType.SzArray:
                return(new ArrayType(ReadTypeReference(reader, (ElementType)reader.ReadByte())));

            case ElementType.Class:
                return((TypeReference)netheader.TablesHeap.TypeDefOrRef.GetMember((int)NETGlobals.ReadCompressedUInt32(reader)));

            case ElementType.ValueType:
                TypeReference typeRef = (TypeReference)netheader.TablesHeap.TypeDefOrRef.GetMember((int)NETGlobals.ReadCompressedUInt32(reader));
                typeRef.IsValueType = true;
                return(typeRef);

            case ElementType.ByRef:
                return(new ByReferenceType(ReadTypeReference(reader, (ElementType)reader.ReadByte())));

            case ElementType.Pinned:
                return(new PinnedType(ReadTypeReference(reader, (ElementType)reader.ReadByte())));

            case ElementType.GenericInst:
                bool                flag       = reader.ReadByte() == 0x11;
                TypeReference       reference2 = ReadTypeToken(reader);
                GenericInstanceType instance   = new GenericInstanceType(reference2);
                this.ReadGenericInstanceSignature(reader, instance);
                if (flag)
                {
                    instance.IsValueType = true;
                }
                return(instance);
            }
            return(new TypeReference(string.Empty, type.ToString(), null)
            {
                netheader = this.netheader
            });
        }