Exemple #1
0
        internal static CilType DecodeType(EntityHandle handle, CilTypeProvider provider, bool?isValueType)
        {
            SignatureTypeHandleCode code;

            if (isValueType.HasValue)
            {
                code = isValueType.Value ? SignatureTypeHandleCode.ValueType : SignatureTypeHandleCode.Class;
            }
            else
            {
                code = SignatureTypeHandleCode.Unresolved;
            }

            switch (handle.Kind)
            {
            case HandleKind.TypeDefinition:
                return(provider.GetTypeFromDefinition(provider.Reader, (TypeDefinitionHandle)handle, code));

            case HandleKind.TypeReference:
                return(provider.GetTypeFromReference(provider.Reader, (TypeReferenceHandle)handle, code));

            case HandleKind.TypeSpecification:
                return(provider.GetTypeFromSpecification(provider.Reader, (TypeSpecificationHandle)handle, code));

            default:
                throw new ArgumentException("Handle is not a type definition, reference, or specification.", nameof(handle));
            }
        }
        internal static CilType DecodeType(EntityHandle handle, CilTypeProvider provider, byte rawTypeKind)
        {
            switch (handle.Kind)
            {
            case HandleKind.TypeDefinition:
                return(provider.GetTypeFromDefinition(provider.Reader, (TypeDefinitionHandle)handle, rawTypeKind));

            case HandleKind.TypeReference:
                return(provider.GetTypeFromReference(provider.Reader, (TypeReferenceHandle)handle, rawTypeKind));

            case HandleKind.TypeSpecification:
                return(provider.GetTypeFromSpecification(provider.Reader, null, (TypeSpecificationHandle)handle, rawTypeKind));

            default:
                throw new ArgumentException("Handle is not a type definition, reference, or specification.", nameof(handle));
            }
        }
 public string ToString(CilTypeProvider provider)
 {
     switch (Kind)
     {
         case HandlerKind.Try:
             return ".try";
         case HandlerKind.Finally:
             return "finally";
         case HandlerKind.Filter:
             return "filter";
         case HandlerKind.Fault:
             return "fault";
         case HandlerKind.Catch:
             return string.Format("catch {0}", CilDecoder.DecodeType(CatchType, provider).ToString(false));
         default:
             throw new InvalidOperationException("Handler Kind doesn't exist.");
     }
 }
Exemple #4
0
        public string ToString(CilTypeProvider provider)
        {
            switch (Kind)
            {
            case HandlerKind.Try:
                return(".try");

            case HandlerKind.Finally:
                return("finally");

            case HandlerKind.Filter:
                return("filter");

            case HandlerKind.Fault:
                return("fault");

            case HandlerKind.Catch:
                return(string.Format("catch {0}", CilDecoder.DecodeType(CatchType, provider).ToString(false)));

            default:
                throw new InvalidOperationException("Handler Kind doesn't exist.");
            }
        }
        internal static CilType DecodeType(EntityHandle handle, CilTypeProvider provider, bool? isValueType)
        {
            SignatureTypeHandleCode code;
            if (isValueType.HasValue)
            {
                code = isValueType.Value ? SignatureTypeHandleCode.ValueType : SignatureTypeHandleCode.Class;
            }
            else
            {
                code = SignatureTypeHandleCode.Unresolved;
            }

            switch (handle.Kind)
            {
                case HandleKind.TypeDefinition:
                    return provider.GetTypeFromDefinition(provider.Reader, (TypeDefinitionHandle)handle, code);
                case HandleKind.TypeReference:
                    return provider.GetTypeFromReference(provider.Reader, (TypeReferenceHandle)handle, code);
                case HandleKind.TypeSpecification:
                    return provider.GetTypeFromSpecification(provider.Reader, (TypeSpecificationHandle)handle, code);
                default:
                    throw new ArgumentException("Handle is not a type definition, reference, or specification.", nameof(handle));
            }
        }
Exemple #6
0
 private static string GetMemberRef(MetadataReader metadataReader, int token, CilTypeProvider provider, string genericParameterSignature = "")
 {
     var refHandle = MetadataTokens.MemberReferenceHandle(token);
     var reference = metadataReader.GetMemberReference(refHandle);
     var parentToken = MetadataTokens.GetToken(reference.Parent);
     string type;
     if (IsTypeSpecification(parentToken))
     {
         var typeSpecificationHandle = MetadataTokens.TypeSpecificationHandle(parentToken);
         var typeSpecification = metadataReader.GetTypeSpecification(typeSpecificationHandle);
         type = SignatureDecoder.DecodeType(typeSpecificationHandle, provider, null).ToString();
     }
     else
     {
         var parentHandle = MetadataTokens.TypeReferenceHandle(parentToken);
         type = SignatureDecoder.DecodeType(parentHandle, provider, null).ToString(false);
     }
     string signatureValue;
     string parameters = string.Empty;
     if (reference.GetKind() == MemberReferenceKind.Method)
     {
         MethodSignature<CilType> signature = SignatureDecoder.DecodeMethodSignature(reference.Signature, provider);
         signatureValue = GetMethodReturnType(signature);
         parameters = provider.GetParameterList(signature);
         return String.Format("{0} {1}::{2}{3}{4}", signatureValue, type, GetString(metadataReader, reference.Name), genericParameterSignature,parameters);
     }
     signatureValue = SignatureDecoder.DecodeFieldSignature(reference.Signature, provider).ToString();
     return String.Format("{0} {1}::{2}{3}", signatureValue, type, GetString(metadataReader, reference.Name), parameters);
 }
Exemple #7
0
 private static string GetTypeInformation(MetadataReader metadataReader, int intOperand, CilTypeProvider provider)
 {
     if (IsTypeReference(intOperand))
     {
         var refHandle = MetadataTokens.TypeReferenceHandle(intOperand);
         return SignatureDecoder.DecodeType(refHandle, provider, null).ToString();
     }
     if (IsTypeSpecification(intOperand))
     {
         var typeHandle = MetadataTokens.TypeSpecificationHandle(intOperand);
         return SignatureDecoder.DecodeType(typeHandle, provider, null).ToString();
     }
     var defHandle = MetadataTokens.TypeDefinitionHandle(intOperand);
     return SignatureDecoder.DecodeType(defHandle, provider, null).ToString();
 }
 private static SignatureDecoder<CilType> NewDecoder(CilTypeProvider provider)
 {
     return new SignatureDecoder<CilType>(provider, provider.Reader, SignatureDecoderOptions.DifferentiateClassAndValueTypes);
 }
 internal static CilType DecodeFieldSignature(BlobHandle handle, CilTypeProvider provider)
 {
     var blobReader = provider.Reader.GetBlobReader(handle);
     return NewDecoder(provider).DecodeFieldSignature(ref blobReader);
 }
Exemple #10
0
        internal static ImmutableArray <CilType> DecodeLocalSignature(StandaloneSignatureHandle handle, CilTypeProvider provider)
        {
            var standaloneSignature = provider.Reader.GetStandaloneSignature(handle);
            var blobReader          = provider.Reader.GetBlobReader(standaloneSignature.Signature);

            return(NewDecoder(provider).DecodeLocalSignature(ref blobReader));
        }
Exemple #11
0
 private static SignatureDecoder <CilType> NewDecoder(CilTypeProvider provider)
 {
     return(new SignatureDecoder <CilType>(provider, provider.Reader, SignatureDecoderOptions.DifferentiateClassAndValueTypes));
 }
Exemple #12
0
 private CilReaders(Stream fileStream)
 {
     _peReader = new PEReader(fileStream);
     _mdReader = _peReader.GetMetadataReader();
     _provider = new CilTypeProvider(_mdReader);
 }
Exemple #13
0
 private static string GetFieldInformation(MetadataReader metadataReader, int intOperand, CilTypeProvider provider)
 {
     if(IsMemberReference(intOperand))
     {
         return GetMemberRef(metadataReader, intOperand, provider);
     }
     var handle = MetadataTokens.FieldDefinitionHandle(intOperand);
     var definition = metadataReader.GetFieldDefinition(handle);
     var typeHandle = definition.GetDeclaringType();
     var typeSignature = SignatureDecoder.DecodeType(typeHandle, provider, null);
     var signature = SignatureDecoder.DecodeFieldSignature(definition.Signature, provider);
     return String.Format("{0} {1}::{2}", signature.ToString(), typeSignature.ToString(false), GetString(metadataReader, definition.Name));
 }
Exemple #14
0
 internal static string SolveMethodName(MetadataReader metadataReader, int token, CilTypeProvider provider)
 {
     string genericParameters = string.Empty;
     if (IsMethodSpecification(token))
     {
         var methodHandle = MetadataTokens.MethodSpecificationHandle(token);
         var methodSpec = metadataReader.GetMethodSpecification(methodHandle);
         token = MetadataTokens.GetToken(methodSpec.Method);
         genericParameters = GetGenericParametersSignature(methodSpec, provider);
     }
     if (IsMemberReference(token))
     {
         return GetMemberRef(metadataReader, token, provider, genericParameters);
     }
     var handle = MetadataTokens.MethodDefinitionHandle(token);
     var definition = metadataReader.GetMethodDefinition(handle);
     var parent = definition.GetDeclaringType();
     MethodSignature<CilType> signature = SignatureDecoder.DecodeMethodSignature(definition.Signature, provider);
     var returnType = GetMethodReturnType(signature);
     var parameters = provider.GetParameterList(signature);
     var parentType = SignatureDecoder.DecodeType(parent, provider, null);
     return string.Format("{0} {1}::{2}{3}{4}",returnType, parentType.ToString(false), GetString(metadataReader, definition.Name), genericParameters, parameters);
 }
Exemple #15
0
 internal static ImmutableArray<CilType> DecodeLocalSignature(StandaloneSignatureHandle handle, CilTypeProvider provider)
 {
     var standaloneSignature = provider.Reader.GetStandaloneSignature(handle);
     var blobReader = provider.Reader.GetBlobReader(standaloneSignature.Signature);
     return NewDecoder(provider).DecodeLocalSignature(ref blobReader);
 }
 private static SignatureDecoder <CilType, object> NewDecoder(CilTypeProvider provider)
 {
     return(new SignatureDecoder <CilType, object>(provider, provider.Reader, null));
 }
Exemple #17
0
 internal static CilType DecodeType(ref BlobReader reader, CilTypeProvider provider)
 {
     return NewDecoder(provider).DecodeType(ref reader);
 }
Exemple #18
0
 internal static ImmutableArray<CilType> DecodeMethodSpecificationSignature(BlobHandle handle, CilTypeProvider provider)
 {
     var blobReader = provider.Reader.GetBlobReader(handle);
     return NewDecoder(provider).DecodeMethodSpecificationSignature(ref blobReader);
 }
Exemple #19
0
 private static string GetGenericParametersSignature(MethodSpecification methodSpec, CilTypeProvider provider)
 {
     var genericParameters = SignatureDecoder.DecodeMethodSpecificationSignature(methodSpec.Signature, provider);
     StringBuilder sb = new StringBuilder();
     int i;
     for(i = 0; i < genericParameters.Length; i++)
     {
         if(i == 0)
         {
             sb.Append("<");
         }
         sb.Append(genericParameters[i]);
         sb.Append(",");
     }
     if(i > 0)
     {
         sb.Length--;
         sb.Append(">");
     }
     return sb.ToString();
 }
Exemple #20
0
 public static MethodSignature<CilType> DecodeMethodSignature(MethodDefinition methodDefinition, CilTypeProvider provider)
 {
     return SignatureDecoder.DecodeMethodSignature(methodDefinition.Signature, provider);
 }
Exemple #21
0
 internal static string DecodeOverridenMethodName(MetadataReader metadataReader, int token, CilTypeProvider provider)
 {
     var handle = MetadataTokens.MethodDefinitionHandle(token);
     var definition = metadataReader.GetMethodDefinition(handle);
     var parent = definition.GetDeclaringType();
     MethodSignature<CilType> signature = SignatureDecoder.DecodeMethodSignature(definition.Signature, provider);
     var parentType = SignatureDecoder.DecodeType(parent, provider, null);
     return string.Format("{0}::{1}", parentType.ToString(false), GetString(metadataReader, definition.Name));
 }
Exemple #22
0
 private static IEnumerable<CilInstruction> DecodeMethodBody(BlobReader ilReader, MetadataReader metadataReader, CilTypeProvider provider, CilMethodDefinition methodDefinition)
 {
     ilReader.Reset();
     int intOperand;
     ushort shortOperand;
     int ilOffset = 0;
     CilInstruction instruction = null;
     while (ilReader.Offset < ilReader.Length)
     {
         OpCode opCode;
         int expectedSize;
         byte _byte = ilReader.ReadByte();
         /*If the byte read is 0xfe it means is a two byte instruction, 
         so since it is going to read the second byte to get the actual
         instruction it has to check that the offset is still less than the length.*/
         if (_byte == 0xfe && ilReader.Offset < ilReader.Length)
         {
             opCode = CilDecoderHelpers.Instance.twoByteOpCodes[ilReader.ReadByte()];
             expectedSize = 2;
         }
         else
         {
             opCode = CilDecoderHelpers.Instance.oneByteOpCodes[_byte];
             expectedSize = 1;
         }
         switch (opCode.OperandType)
         {
             //The instruction size is the expected size (1 or 2 depending if it is a one or two byte instruction) + the size of the operand.
             case OperandType.InlineField:
                 intOperand = ilReader.ReadInt32();
                 string fieldInfo = GetFieldInformation(metadataReader, intOperand, provider);
                 instruction = new CilStringInstruction(opCode, fieldInfo, intOperand, expectedSize + (int)CilInstructionSize.Int32);
                 break;
             case OperandType.InlineString:
                 intOperand = ilReader.ReadInt32();
                 bool isPrintable;
                 string str = GetArgumentString(metadataReader, intOperand, out isPrintable);
                 instruction = new CilStringInstruction(opCode, str, intOperand, expectedSize + (int)CilInstructionSize.Int32, isPrintable);
                 break;
             case OperandType.InlineMethod:
                 intOperand = ilReader.ReadInt32();
                 string methodCall = SolveMethodName(metadataReader, intOperand, provider);
                 instruction = new CilStringInstruction(opCode, methodCall, intOperand, expectedSize + (int)CilInstructionSize.Int32);
                 break;
             case OperandType.InlineType:
                 intOperand = ilReader.ReadInt32();
                 string type = GetTypeInformation(metadataReader, intOperand, provider);
                 instruction = new CilStringInstruction(opCode, type, intOperand, expectedSize + (int)CilInstructionSize.Int32);
                 break;
             case OperandType.InlineTok:
                 intOperand = ilReader.ReadInt32();
                 string tokenType = GetInlineTokenType(metadataReader, intOperand, provider);
                 instruction = new CilStringInstruction(opCode, tokenType, intOperand, expectedSize + (int)CilInstructionSize.Int32);
                 break;
             case OperandType.InlineI:
                 instruction = new CilInt32Instruction(opCode, ilReader.ReadInt32(), -1, expectedSize + (int)CilInstructionSize.Int32);
                 break;
             case OperandType.InlineI8:
                 instruction = new CilInt64Instruction(opCode, ilReader.ReadInt64(), -1, expectedSize + (int)CilInstructionSize.Int64);
                 break;
             case OperandType.InlineR:
                 instruction = new CilDoubleInstruction(opCode, ilReader.ReadDouble(), -1, expectedSize + (int)CilInstructionSize.Double);
                 break;
             case OperandType.InlineSwitch:
                 instruction = CreateSwitchInstruction(ref ilReader, expectedSize, ilOffset, opCode);
                 break;
             case OperandType.ShortInlineBrTarget:
                 instruction = new CilInt16BranchInstruction(opCode, ilReader.ReadSByte(), ilOffset, expectedSize + (int)CilInstructionSize.Byte);
                 break;
             case OperandType.InlineBrTarget:
                 instruction = new CilBranchInstruction(opCode, ilReader.ReadInt32(), ilOffset, expectedSize + (int)CilInstructionSize.Int32);
                 break;
             case OperandType.ShortInlineI:
                 instruction = new CilByteInstruction(opCode, ilReader.ReadByte(), -1, expectedSize + (int)CilInstructionSize.Byte);
                 break;
             case OperandType.ShortInlineR:
                 instruction = new CilSingleInstruction(opCode, ilReader.ReadSingle(), -1, expectedSize + (int)CilInstructionSize.Single);
                 break;
             case OperandType.InlineNone:
                 instruction = new CilInstructionWithNoValue(opCode, expectedSize);
                 break;
             case OperandType.ShortInlineVar:
                 byte token = ilReader.ReadByte();
                 instruction = new CilInt16VariableInstruction(opCode, GetVariableName(opCode, token, methodDefinition), token, expectedSize + (int)CilInstructionSize.Byte);
                 break;
             case OperandType.InlineVar:
                 shortOperand = ilReader.ReadUInt16();
                 instruction = new CilVariableInstruction(opCode, GetVariableName(opCode, shortOperand, methodDefinition), shortOperand, expectedSize + (int)CilInstructionSize.Int16);
                 break;
             case OperandType.InlineSig:
                 intOperand = ilReader.ReadInt32();
                 instruction = new CilStringInstruction(opCode, GetSignature(metadataReader, intOperand, provider), intOperand, expectedSize + (int)CilInstructionSize.Int32);
                 break;
             default:
                 break;
         }
         ilOffset += instruction.Size;
         yield return instruction;
     }
 }
Exemple #23
0
 internal static CilLocal[] DecodeLocalSignature(MethodBodyBlock methodBody, MetadataReader metadataReader, CilTypeProvider provider)
 {
     if (methodBody.LocalSignature.IsNil)
     {
         return new CilLocal[0];
     }
     ImmutableArray<CilType> localTypes = SignatureDecoder.DecodeLocalSignature(methodBody.LocalSignature, provider);
     CilLocal[] locals = new CilLocal[localTypes.Length];
     for (int i = 0; i < localTypes.Length; i++)
     {
         string name = "V_" + i;
         locals[i] = new CilLocal(name, localTypes[i].ToString());
     }
     return locals;
 }
Exemple #24
0
 internal static CilType DecodeType(EntityHandle type, CilTypeProvider provider)
 {
     return SignatureDecoder.DecodeType(type, provider, null);
 }
Exemple #25
0
        internal static CilType DecodeFieldSignature(BlobHandle handle, CilTypeProvider provider)
        {
            var blobReader = provider.Reader.GetBlobReader(handle);

            return(NewDecoder(provider).DecodeFieldSignature(ref blobReader));
        }
Exemple #26
0
 private static string GetSignature(MetadataReader metadataReader, int intOperand, CilTypeProvider provider)
 {
     if (IsStandaloneSignature(intOperand))
     {
         var handle = MetadataTokens.StandaloneSignatureHandle(intOperand);
         var standaloneSignature = metadataReader.GetStandaloneSignature(handle);
         var signature = SignatureDecoder.DecodeMethodSignature(standaloneSignature.Signature, provider);
         return string.Format("{0}{1}", GetMethodReturnType(signature), provider.GetParameterList(signature));
     }
     throw new ArgumentException("Get signature invalid token");
 }
Exemple #27
0
        internal static ImmutableArray <CilType> DecodeMethodSpecificationSignature(BlobHandle handle, CilTypeProvider provider)
        {
            var blobReader = provider.Reader.GetBlobReader(handle);

            return(NewDecoder(provider).DecodeMethodSpecificationSignature(ref blobReader));
        }
Exemple #28
0
 private static string GetInlineTokenType(MetadataReader metadataReader, int intOperand, CilTypeProvider provider)
 {
     if (IsMethodDefinition(intOperand) || IsMethodSpecification(intOperand) || IsMemberReference(intOperand))
     {
         return "method " + SolveMethodName(metadataReader, intOperand, provider);
     }
     if (IsFieldDefinition(intOperand))
     {
         return "field " + GetFieldInformation(metadataReader, intOperand, provider);
     }
     return GetTypeInformation(metadataReader, intOperand, provider);
 }
Exemple #29
0
 internal static CilType DecodeType(ref BlobReader reader, CilTypeProvider provider)
 {
     return(NewDecoder(provider).DecodeType(ref reader));
 }
Exemple #30
0
 private CilReaders(Stream fileStream)
 {
     _peReader = new PEReader(fileStream);
     _mdReader = _peReader.GetMetadataReader();
     _provider = new CilTypeProvider(_mdReader);
 }