Exemple #1
0
        public static Type Read(ElementType leadByte, BinaryStreamReader signatureBlobReader)
        {
            switch (leadByte)
            {
            case ElementType.End:
                break;

            case ElementType.Void:
                break;

            case ElementType.Boolean: return(Boolean.Instance);

            case ElementType.Char: return(Char.Instance);

            case ElementType.I1: return(I1.Instance);

            case ElementType.U1: return(U1.Instance);

            case ElementType.I2: return(I2.Instance);

            case ElementType.U2: return(U2.Instance);

            case ElementType.I4: return(I4.Instance);

            case ElementType.U4: return(U4.Instance);

            case ElementType.I8: return(I8.Instance);

            case ElementType.U8: return(U8.Instance);

            case ElementType.R4: return(R4.Instance);

            case ElementType.R8: return(R8.Instance);

            case ElementType.String: return(String.Instance);

            case ElementType.Ptr:
                ElementType ptrLeadByte;
                var         ptrCustomMod = CustomMod.ReadCustomModArray(out ptrLeadByte, signatureBlobReader);
                Ptr         resultPtr;
                if (ptrLeadByte == ElementType.Void)
                {
                    resultPtr = Ptr.Void.Instance;
                }
                else
                {
                    Type ptrType = Type.Read(ptrLeadByte, signatureBlobReader);
                    resultPtr = new Ptr.Type
                    {
                        PtrType = ptrType
                    };
                }
                resultPtr.CustomMods = ptrCustomMod;
                return(resultPtr);

            case ElementType.ByRef:
                break;

            case ElementType.ValueType:
                var valueTypeTypeDefOrRefOrSpecEncoded = signatureBlobReader.ReadTypeDefOrRefOrSpecEncoded();
                return(new ValueType {
                    TypeDefOrRefEncoded = valueTypeTypeDefOrRefOrSpecEncoded
                });

            case ElementType.Class:
                var classTypeDefOrRefOrSpecEncoded = signatureBlobReader.ReadTypeDefOrRefOrSpecEncoded();
                return(new Class {
                    TypeDefOrRefEncoded = classTypeDefOrRefOrSpecEncoded
                });

            case ElementType.Var:
                uint?varNumber = signatureBlobReader.ReadCompressedUInt32();
                return(new Var
                {
                    Number = varNumber
                });

            case ElementType.Array:
                // TODO: implement ArrayShape (ECMA-335 §23.2.13)
                throw new NotImplementedException("TODO: implement ArrayShape (ECMA-335 §23.2.13).");

            case ElementType.GenericInst:
                var         genericLeadByte = (ElementType)signatureBlobReader.ReadByte();
                GenericInst genericInst;
                switch (genericLeadByte)
                {
                case ElementType.Class:
                    genericInst = new GenericInst.Class();
                    break;

                case ElementType.ValueType:
                    genericInst = new GenericInst.ValueType();
                    break;

                default:
                    throw new BadImageFormatException("Invalid lead byte for " + ElementType.GenericInst + " class/valuetype value.");
                }

                var typeDefOrRefOrSpecEncoded = signatureBlobReader.ReadTypeDefOrRefOrSpecEncoded();
                genericInst.TypeDefOrRefOrSpecEncoded = typeDefOrRefOrSpecEncoded;

                uint?genArgCount = signatureBlobReader.ReadCompressedUInt32();
                if (genArgCount == null)
                {
                    throw new BadImageFormatException("Invalid null value for GenArgCount value in " + ElementType.GenericInst + ".");
                }

                var genericInstTypes = new Type[genArgCount.Value];
                for (int i = 0; i < genericInstTypes.Length; i++)
                {
                    var genericInstTypeLeadByte = (ElementType)signatureBlobReader.ReadByte();
                    genericInstTypes[i] = Type.Read(genericInstTypeLeadByte, signatureBlobReader);
                }

                genericInst.Types = genericInstTypes;
                return(genericInst);

            case ElementType.TypedByRef:
                break;

            case ElementType.I: return(I.Instance);

            case ElementType.U: return(U.Instance);

            case ElementType.FnPtr:
                var methodDefSig = MethodSig.Read(signatureBlobReader);
                return(new FnPtr
                {
                    MethodDefSig = methodDefSig
                });

            case ElementType.Object: return(Object.Instance);

            case ElementType.SZArray:
                ElementType szArrayLeadByte;
                var         szArrayCustomMod = CustomMod.ReadCustomModArray(out szArrayLeadByte, signatureBlobReader);
                var         szArrayType      = Type.Read(szArrayLeadByte, signatureBlobReader);
                return(new SZArray
                {
                    CustomMods = szArrayCustomMod,
                    Type = szArrayType
                });

            case ElementType.MVar:
                uint?mvarNumber = signatureBlobReader.ReadCompressedUInt32();
                return(new MVar
                {
                    Number = mvarNumber
                });

            case ElementType.CMod_ReqD:
                break;

            case ElementType.CMod_Opt:
                break;

            case ElementType.Internal:
                break;

            case ElementType.Modifier:
                break;

            case ElementType.Sentinel:
                break;

            case ElementType.Pinned:
                break;

            case ElementType.R4_Hfa:
                break;

            case ElementType.R8_Hfa:
                break;

            case ElementType.ArgumentType_:
                break;

            case ElementType.CustomAttribute_BoxedObject_:
                break;

            case ElementType.CustomAttribute_Field_:
                break;

            case ElementType.CustomAttribute_Property_:
                break;

            case ElementType.CustomAttribute_Enum_:
                break;

            default:
                break;
            }

            throw new BadImageFormatException("Invalid lead byte " + leadByte + ".");
        }
Exemple #2
0
        public static Type Read(ElementType leadByte, BinaryStreamReader signatureBlobReader)
        {
            switch (leadByte)
            {
                case ElementType.End:
                    break;
                case ElementType.Void:
                    break;

                case ElementType.Boolean: return Boolean.Instance;
                case ElementType.Char: return Char.Instance;
                case ElementType.I1: return I1.Instance;
                case ElementType.U1: return U1.Instance;
                case ElementType.I2: return I2.Instance;
                case ElementType.U2: return U2.Instance;
                case ElementType.I4: return I4.Instance;
                case ElementType.U4: return U4.Instance;
                case ElementType.I8: return I8.Instance;
                case ElementType.U8: return U8.Instance;
                case ElementType.R4: return R4.Instance;
                case ElementType.R8: return R8.Instance;

                case ElementType.String: return String.Instance;

                case ElementType.Ptr:
                    ElementType ptrLeadByte;
                    var ptrCustomMod = CustomMod.ReadCustomModArray(out ptrLeadByte, signatureBlobReader);
                    Ptr resultPtr;
                    if (ptrLeadByte == ElementType.Void)
                    {
                        resultPtr = Ptr.Void.Instance;
                    }
                    else
                    {
                        Type ptrType = Type.Read(ptrLeadByte, signatureBlobReader);
                        resultPtr = new Ptr.Type
                        {
                            PtrType = ptrType
                        };
                    }
                    resultPtr.CustomMods = ptrCustomMod;
                    return resultPtr;

                case ElementType.ByRef:
                    break;

                case ElementType.ValueType:
                    var valueTypeTypeDefOrRefOrSpecEncoded = signatureBlobReader.ReadTypeDefOrRefOrSpecEncoded();
                    return new ValueType { TypeDefOrRefEncoded = valueTypeTypeDefOrRefOrSpecEncoded };

                case ElementType.Class:
                    var classTypeDefOrRefOrSpecEncoded = signatureBlobReader.ReadTypeDefOrRefOrSpecEncoded();
                    return new Class { TypeDefOrRefEncoded = classTypeDefOrRefOrSpecEncoded };

                case ElementType.Var:
                    uint? varNumber = signatureBlobReader.ReadCompressedUInt32();
                    return new Var
                    {
                        Number = varNumber
                    };

                case ElementType.Array:
                    // TODO: implement ArrayShape (ECMA-335 §23.2.13)
                    throw new NotImplementedException("TODO: implement ArrayShape (ECMA-335 §23.2.13).");

                case ElementType.GenericInst:
                    var genericLeadByte = (ElementType)signatureBlobReader.ReadByte();
                    GenericInst genericInst;
                    switch (genericLeadByte)
                    {
                        case ElementType.Class:
                            genericInst = new GenericInst.Class();
                            break;

                        case ElementType.ValueType:
                            genericInst = new GenericInst.ValueType();
                            break;

                        default:
                            throw new BadImageFormatException("Invalid lead byte for "+ElementType.GenericInst+" class/valuetype value.");
                    }

                    var typeDefOrRefOrSpecEncoded = signatureBlobReader.ReadTypeDefOrRefOrSpecEncoded();
                    genericInst.TypeDefOrRefOrSpecEncoded = typeDefOrRefOrSpecEncoded;

                    uint? genArgCount = signatureBlobReader.ReadCompressedUInt32();
                    if (genArgCount == null)
                        throw new BadImageFormatException("Invalid null value for GenArgCount value in " + ElementType.GenericInst+".");

                    var genericInstTypes = new Type[genArgCount.Value];
                    for(int i = 0; i<genericInstTypes.Length; i++)
                    {
                        var genericInstTypeLeadByte = (ElementType)signatureBlobReader.ReadByte();
                        genericInstTypes[i] = Type.Read(genericInstTypeLeadByte, signatureBlobReader);
                    }

                    genericInst.Types = genericInstTypes;
                    return genericInst;

                case ElementType.TypedByRef:
                    break;

                case ElementType.I: return I.Instance;
                case ElementType.U: return U.Instance;

                case ElementType.FnPtr:
                    var methodDefSig = MethodSig.Read(signatureBlobReader);
                    return new FnPtr
                    {
                        MethodDefSig = methodDefSig
                    };

                case ElementType.Object: return Object.Instance;

                case ElementType.SZArray:
                    ElementType szArrayLeadByte;
                    var szArrayCustomMod = CustomMod.ReadCustomModArray(out szArrayLeadByte, signatureBlobReader);
                    var szArrayType = Type.Read(szArrayLeadByte, signatureBlobReader);
                    return new SZArray
                    {
                        CustomMods = szArrayCustomMod,
                        Type = szArrayType
                    };

                case ElementType.MVar:
                    uint? mvarNumber = signatureBlobReader.ReadCompressedUInt32();
                    return new MVar
                    {
                        Number = mvarNumber
                    };

                case ElementType.CMod_ReqD:
                    break;

                case ElementType.CMod_Opt:
                    break;
                case ElementType.Internal:
                    break;
                case ElementType.Modifier:
                    break;
                case ElementType.Sentinel:
                    break;
                case ElementType.Pinned:
                    break;
                case ElementType.R4_Hfa:
                    break;
                case ElementType.R8_Hfa:
                    break;
                case ElementType.ArgumentType_:
                    break;
                case ElementType.CustomAttribute_BoxedObject_:
                    break;
                case ElementType.CustomAttribute_Field_:
                    break;
                case ElementType.CustomAttribute_Property_:
                    break;
                case ElementType.CustomAttribute_Enum_:
                    break;
                default:
                    break;
            }

            throw new BadImageFormatException("Invalid lead byte "+leadByte+".");
        }