protected override void Load(SwfStream stream, ushort length)
 {
     var array = new string[stream.ReadUShort()];
     for (int i = 0; i < array.Length; i++)
         array[i] = stream.ReadString();
     Pool = new ConstantPool(array);
 }
Exemple #2
0
 /// <summary>
 /// Default ctor
 /// </summary>
 internal CodeReader(MethodDefinition method, CodeAttribute codeAttribute, byte[] code, ConstantPool cp)
 {
     this.method = method;
     this.codeAttribute = codeAttribute;
     this.code = code;
     this.cp = cp;
 }
Exemple #3
0
        /// <summary>
        /// Default ctor
        /// </summary>
        internal CodeAttribute(MethodDefinition method, ConstantPool cp, int maxStack, int maxLocals, byte[] code)
        {
            this.method = method;
            this.cp = cp;
            this.maxStack = maxStack;
            this.maxLocals = maxLocals;
            this.code = code;

            localVariableMap = new List<Tuple<LocalVariable, LocalVariableReference>>();
        }
Exemple #4
0
        public ExpressionCompiler(bool generateDebugInfo = true)
        {
            _contexts = new List<FunctionContext>();
            _currentScope = null;
            _labelIndex = 0;

            FrameIndex = -1;

            GeneratingDebugInfo = generateDebugInfo;

            NumberPool = new ConstantPool<double>();
            StringPool = new ConstantPool<string>();
        }
Exemple #5
0
 public abstract void PutToConstantPool(ConstantPool constantPool);
Exemple #6
0
 /// <summary>
 /// Default ctor
 /// </summary>
 internal ConstantPoolDouble(ConstantPool constantPool, double value)
     : base(constantPool, value)
 {
 }
Exemple #7
0
 protected override void ReadExtends(ClassData classData, ConstantPool constantPool)
 {
     this.NumAnnotations = classData.ReadUint16();
     this.Annotations    = new TypeAnnotationReader(classData).ReadAnnotations(this.NumAnnotations);
 }
Exemple #8
0
 public void ReadAttributeInfo(ClassReader classReader, ConstantPool constantPool)
 {
     this.attributeLength = classReader.ReadU4();
     Read(classReader, constantPool);
 }
Exemple #9
0
 public MemberConstantInfo(ConstantPool pool, ushort classIndex, ushort nameTypeIndex) : base(pool)
 {
     ClassIndex       = classIndex;
     NameAndTypeIndex = nameTypeIndex;
 }
 /// <summary>
 /// Default ctor
 /// </summary>
 internal ConstantPoolMemberRef(ConstantPool constantPool, int classIndex, int nameAndTypeIndex)
     : base(constantPool)
 {
     this.classIndex = classIndex;
     this.nameAndTypeIndex = nameAndTypeIndex;
 }
Exemple #11
0
 /// <summary>
 /// Read an element_value (4.7.16.1)
 /// </summary>
 private ElementValue ReadElementValue(ConstantPool cp)
 {
     var tag = (char) stream.ReadU1();
     switch (tag)
     {
         case 'B':
         case 'C':
         case 'D':
         case 'F':
         case 'I':
         case 'J':
         case 'S':
         case 'Z':
         case 's':
             return new ConstElementValue(cp, tag, stream.ReadU2());
         case 'e':
             {
                 var typeNameIndex = stream.ReadU2();
                 var constNameIndex = stream.ReadU2();
                 return new EnumConstElementValue(cp, typeNameIndex, constNameIndex);
             }
         case 'c':
             return new ClassElementValue(cp, stream.ReadU2());
         case '@':
             return new AnnotationElementValue(ReadAnnotation(cp));
         case '[':
             {
                 var numValues = stream.ReadU2();
                 var values = new ElementValue[numValues];
                 for (var i = 0; i < numValues; i++)
                 {
                     values[i] = ReadElementValue(cp);
                 }
                 return new ArrayElementValue(values);
             }
         default:
             throw new Dot42Exception(string.Format("Unknown element_value tag '{0}'", tag));
     }
 }
 public override ConstantPoolInfo ReadNotPublic(ClassData classData, ConstantPool constantPool)
 {
     this.ReferenceKind  = classData.ReadUint8();
     this.ReferenceIndex = classData.ReadUint16();
     return(this);
 }
 /// <exception cref="System.IO.IOException"/>
 public override void InitContent(DataInputFullStream data, ConstantPool pool)
 {
     defaultValue = StructAnnotationAttribute.ParseAnnotationElement(data, pool);
 }
Exemple #14
0
 public override void Write(BinaryWriter Writer, ConstantPool Pool)
 {
     base.Write(Writer, Pool);
     Writer.WriteBE(Pool.AddConstant(new Utf8(Value)));
 }
Exemple #15
0
 public MethodRefConstantInfo(ConstantPool pool, ushort classIndex, ushort nameTypeIndex) : base(pool, classIndex, nameTypeIndex)
 {
 }
Exemple #16
0
 public override void Write(BinaryWriter Writer, ConstantPool Pool)
 {
 }
        /// <exception cref="IOException"/>
        public static Exprent ParseAnnotationElement(DataInputStream data, ConstantPool pool
                                                     )
        {
            int tag = data.ReadUnsignedByte();

            switch (tag)
            {
            case 'e':
            {
                // enum constant
                string className = pool.GetPrimitiveConstant(data.ReadUnsignedShort()).GetString(
                    );
                string constName = pool.GetPrimitiveConstant(data.ReadUnsignedShort()).GetString(
                    );
                FieldDescriptor descr = FieldDescriptor.ParseDescriptor(className);
                return(new FieldExprent(constName, descr.type.value, true, null, descr, null));
            }

            case 'c':
            {
                // class
                string descriptor = pool.GetPrimitiveConstant(data.ReadUnsignedShort()).GetString
                                        ();
                VarType type = FieldDescriptor.ParseDescriptor(descriptor).type;
                string  value;
                switch (type.type)
                {
                case ICodeConstants.Type_Object:
                {
                    value = type.value;
                    break;
                }

                case ICodeConstants.Type_Byte:
                {
                    value = typeof(byte).FullName;
                    break;
                }

                case ICodeConstants.Type_Char:
                {
                    value = typeof(char).FullName;
                    break;
                }

                case ICodeConstants.Type_Double:
                {
                    value = typeof(double).FullName;
                    break;
                }

                case ICodeConstants.Type_Float:
                {
                    value = typeof(float).FullName;
                    break;
                }

                case ICodeConstants.Type_Int:
                {
                    value = typeof(int).FullName;
                    break;
                }

                case ICodeConstants.Type_Long:
                {
                    value = typeof(long).FullName;
                    break;
                }

                case ICodeConstants.Type_Short:
                {
                    value = typeof(short).FullName;
                    break;
                }

                case ICodeConstants.Type_Boolean:
                {
                    value = typeof(bool).FullName;
                    break;
                }

                case ICodeConstants.Type_Void:
                {
                    value = typeof(void).FullName;
                    break;
                }

                default:
                {
                    throw new Exception("invalid class type: " + type.type);
                }
                }
                return(new ConstExprent(VarType.Vartype_Class, value, null));
            }

            case '[':
            {
                // array
                List <Exprent> elements = new System.Collections.Generic.List <Exprent>();
                int            len      = data.ReadUnsignedShort();
                if (len > 0)
                {
                    elements = new List <Exprent>(len);
                    for (int i = 0; i < len; i++)
                    {
                        elements.Add(ParseAnnotationElement(data, pool));
                    }
                }
                VarType newType;
                if ((elements.Count == 0))
                {
                    newType = new VarType(ICodeConstants.Type_Object, 1, "java/lang/Object");
                }
                else
                {
                    VarType elementType = elements[0].GetExprType();
                    newType = new VarType(elementType.type, 1, elementType.value);
                }
                NewExprent newExpr = new NewExprent(newType, new System.Collections.Generic.List <
                                                        Exprent>(), null);
                newExpr.SetDirectArrayInit(true);
                newExpr.SetLstArrayElements(elements);
                return(newExpr);
            }

            case '@':
            {
                // annotation
                return(ParseAnnotation(data, pool));
            }

            default:
            {
                PrimitiveConstant cn = pool.GetPrimitiveConstant(data.ReadUnsignedShort());
                switch (tag)
                {
                case 'B':
                {
                    return(new ConstExprent(VarType.Vartype_Byte, cn.value, null));
                }

                case 'C':
                {
                    return(new ConstExprent(VarType.Vartype_Char, cn.value, null));
                }

                case 'D':
                {
                    return(new ConstExprent(VarType.Vartype_Double, cn.value, null));
                }

                case 'F':
                {
                    return(new ConstExprent(VarType.Vartype_Float, cn.value, null));
                }

                case 'I':
                {
                    return(new ConstExprent(VarType.Vartype_Int, cn.value, null));
                }

                case 'J':
                {
                    return(new ConstExprent(VarType.Vartype_Long, cn.value, null));
                }

                case 'S':
                {
                    return(new ConstExprent(VarType.Vartype_Short, cn.value, null));
                }

                case 'Z':
                {
                    return(new ConstExprent(VarType.Vartype_Boolean, cn.value, null));
                }

                case 's':
                {
                    return(new ConstExprent(VarType.Vartype_String, cn.value, null));
                }

                default:
                {
                    throw new Exception("invalid element type!");
                }
                }
                break;
            }
            }
        }
 /// <exception cref="IOException"/>
 public override void InitContent(DataInputFullStream data, ConstantPool pool)
 {
     annotations = ParseAnnotations(pool, data);
 }
Exemple #19
0
        /// <summary>
        /// Read a Code attribute
        /// </summary>
        private CodeAttribute ReadCodeAttribute(MethodDefinition method, ConstantPool cp)
        {
            var maxStack = stream.ReadU2();
            var maxLocals = stream.ReadU2();
            var codeLength = (int)stream.ReadU4();
            var code = new byte[codeLength];
            stream.Read(code, 0, codeLength);
            var result = new CodeAttribute(method, cp, maxStack, maxLocals, code);

            var ecount = stream.ReadU2();
            for (var i = 0; i < ecount; i++)
            {
                var startPC = stream.ReadU2();
                var endPC = stream.ReadU2();
                var handlerPC = stream.ReadU2();
                var catchTypeIndex = stream.ReadU2();
                var catchType = (catchTypeIndex != 0) ? cp.GetEntry<ConstantPoolClass>(catchTypeIndex).Type : null;
                result.Add(new ExceptionHandler(result, startPC, endPC, handlerPC, catchType));
            }

            ReadAttributes(cp, result);

            return result;
        }
Exemple #20
0
 /// <summary>
 /// Read a single annotation structure.
 /// </summary>
 private Annotation ReadAnnotation(ConstantPool cp)
 {
     var typeIndex = stream.ReadU2();
     var numPairs = stream.ReadU2();
     var pairs = new ElementValuePair[numPairs];
     for (var j = 0; j < numPairs; j++)
     {
         var nameIndex = stream.ReadU2();
         var value = ReadElementValue(cp);
         pairs[j] = new ElementValuePair(cp, nameIndex, value);
     }
     return new Annotation(cp, typeIndex, pairs);
 }
Exemple #21
0
 /// <summary>
 /// Default ctor
 /// </summary>
 internal ElementValuePair(ConstantPool cp, int elementNameIndex, ElementValue value)
 {
     this.cp = cp;
     this.elementNameIndex = elementNameIndex;
     this.value = value;
 }
Exemple #22
0
 /// <summary>
 /// Read a InnerClasses attribute
 /// </summary>
 private InnerClassesAttribute ReadInnerClassesAttribute(ConstantPool cp)
 {
     var result = new InnerClassesAttribute();
     var count = stream.ReadU2();
     for (var i = 0; i < count; i++)
     {
         var innerClassIndex = stream.ReadU2();
         var innerClass = (innerClassIndex != 0) ? cp.GetEntry<ConstantPoolClass>(innerClassIndex).Type : null;
         var outerClassIndex = stream.ReadU2();
         var outerClass = (outerClassIndex != 0) ? cp.GetEntry<ConstantPoolClass>(outerClassIndex).Type : null;
         var innerNameIndex = stream.ReadU2();
         var innerName = (innerNameIndex != 0) ? cp.GetEntry<ConstantPoolUtf8>(innerNameIndex).Value : null;
         var accessFlags = (NestedClassAccessFlags)stream.ReadU2();
         result.Classes.Add(new InnerClass(cp.Loader, innerClass, outerClass, innerName, accessFlags));
     }
     return result;
 }
Exemple #23
0
 public override void ProcessFromConstantPool(ConstantPool constantPool)
 {
 }
 /// <summary>
 /// Default ctor
 /// </summary>
 internal ConstantPoolInterfaceMethodRef(ConstantPool constantPool, int classIndex, int nameAndTypeIndex)
     : base(constantPool, classIndex, nameAndTypeIndex)
 {
 }
        public static InstructionCollection Parse(Stream input, long instructionsPosition)
        {
            var instructions = new SortedList <int, InstructionBase>();

            using (var helper = new InstructionParseHelper(input, instructionsPosition))
            {
                var reader = helper.GetReader();
                while (helper.CanParse(instructions))
                {
                    //now reader the instructions
                    var instructionPosition = helper.CurrentPosition;
                    var type             = reader.ReadByteAsEnum <InstructionType>();
                    var requireAlignment = InstructionAlignment.IsAligned(type);

                    if (requireAlignment)
                    {
                        reader.Align(4);
                    }

                    InstructionBase instruction = null;
                    var             parameters  = new List <Value>();

                    switch (type)
                    {
                    case InstructionType.ToNumber:
                        instruction = new ToNumber();
                        break;

                    case InstructionType.NextFrame:
                        instruction = new NextFrame();
                        break;

                    case InstructionType.Play:
                        instruction = new Play();
                        break;

                    case InstructionType.Stop:
                        instruction = new Stop();
                        break;

                    case InstructionType.Add:
                        instruction = new Add();
                        break;

                    case InstructionType.Subtract:
                        instruction = new Subtract();
                        break;

                    case InstructionType.Multiply:
                        instruction = new Multiply();
                        break;

                    case InstructionType.Divide:
                        instruction = new Divide();
                        break;

                    case InstructionType.Not:
                        instruction = new Not();
                        break;

                    case InstructionType.StringEquals:
                        instruction = new StringEquals();
                        break;

                    case InstructionType.Pop:
                        instruction = new Pop();
                        break;

                    case InstructionType.ToInteger:
                        instruction = new ToInteger();
                        break;

                    case InstructionType.GetVariable:
                        instruction = new GetVariable();
                        break;

                    case InstructionType.SetVariable:
                        instruction = new SetVariable();
                        break;

                    case InstructionType.StringConcat:
                        instruction = new StringConcat();
                        break;

                    case InstructionType.GetProperty:
                        instruction = new GetProperty();
                        break;

                    case InstructionType.SetProperty:
                        instruction = new SetProperty();
                        break;

                    case InstructionType.Trace:
                        instruction = new Trace();
                        break;

                    case InstructionType.Random:
                        instruction = new RandomNumber();
                        break;

                    case InstructionType.Delete:
                        instruction = new Delete();
                        break;

                    case InstructionType.Delete2:
                        instruction = new Delete2();
                        break;

                    case InstructionType.DefineLocal:
                        instruction = new DefineLocal();
                        break;

                    case InstructionType.CallFunction:
                        instruction = new CallFunction();
                        break;

                    case InstructionType.Return:
                        instruction = new Return();
                        break;

                    case InstructionType.Modulo:
                        instruction = new Modulo();
                        break;

                    case InstructionType.NewObject:
                        instruction = new NewObject();
                        break;

                    case InstructionType.InitArray:
                        instruction = new InitArray();
                        break;

                    case InstructionType.InitObject:
                        instruction = new InitObject();
                        break;

                    case InstructionType.TypeOf:
                        instruction = new TypeOf();
                        break;

                    case InstructionType.Add2:
                        instruction = new Add2();
                        break;

                    case InstructionType.LessThan2:
                        instruction = new LessThan2();
                        break;

                    case InstructionType.Equals2:
                        instruction = new Equals2();
                        break;

                    case InstructionType.ToString:
                        instruction = new ToString();
                        break;

                    case InstructionType.PushDuplicate:
                        instruction = new PushDuplicate();
                        break;

                    case InstructionType.GetMember:
                        instruction = new GetMember();
                        break;

                    case InstructionType.SetMember:
                        instruction = new SetMember();
                        break;

                    case InstructionType.Increment:
                        instruction = new Increment();
                        break;

                    case InstructionType.Decrement:
                        instruction = new Decrement();
                        break;

                    case InstructionType.CallMethod:
                        instruction = new CallMethod();
                        break;

                    case InstructionType.Enumerate2:
                        instruction = new Enumerate2();
                        break;

                    case InstructionType.EA_PushThis:
                        instruction = new PushThis();
                        break;

                    case InstructionType.EA_PushZero:
                        instruction = new PushZero();
                        break;

                    case InstructionType.EA_PushOne:
                        instruction = new PushOne();
                        break;

                    case InstructionType.EA_CallFunc:
                        instruction = new CallFunc();
                        break;

                    case InstructionType.EA_CallMethodPop:
                        instruction = new CallMethodPop();
                        break;

                    case InstructionType.BitwiseXOr:
                        instruction = new BitwiseXOr();
                        break;

                    case InstructionType.Greater:
                        instruction = new Greater();
                        break;

                    case InstructionType.EA_PushThisVar:
                        instruction = new PushThisVar();
                        break;

                    case InstructionType.EA_PushGlobalVar:
                        instruction = new PushGlobalVar();
                        break;

                    case InstructionType.EA_ZeroVar:
                        instruction = new ZeroVar();
                        break;

                    case InstructionType.EA_PushTrue:
                        instruction = new PushTrue();
                        break;

                    case InstructionType.EA_PushFalse:
                        instruction = new PushFalse();
                        break;

                    case InstructionType.EA_PushNull:
                        instruction = new PushNull();
                        break;

                    case InstructionType.EA_PushUndefined:
                        instruction = new PushUndefined();
                        break;

                    case InstructionType.GotoFrame:
                        instruction = new GotoFrame();
                        parameters.Add(Value.FromInteger(reader.ReadInt32()));
                        break;

                    case InstructionType.GetURL:
                        instruction = new GetUrl();
                        parameters.Add(Value.FromString(reader.ReadStringAtOffset()));
                        parameters.Add(Value.FromString(reader.ReadStringAtOffset()));
                        break;

                    case InstructionType.SetRegister:
                        instruction = new SetRegister();
                        parameters.Add(Value.FromInteger(reader.ReadInt32()));
                        break;

                    case InstructionType.ConstantPool:
                    {
                        instruction = new ConstantPool();
                        var count     = reader.ReadUInt32();
                        var constants = reader.ReadFixedSizeArrayAtOffset <uint>(() => reader.ReadUInt32(), count);

                        foreach (var constant in constants)
                        {
                            parameters.Add(Value.FromConstant(constant));
                        }
                    }
                    break;

                    case InstructionType.GotoLabel:
                        instruction = new GotoLabel();
                        parameters.Add(Value.FromString(reader.ReadStringAtOffset()));
                        break;

                    case InstructionType.DefineFunction2:
                    {
                        instruction = new DefineFunction2();
                        var name       = reader.ReadStringAtOffset();
                        var nParams    = reader.ReadUInt32();
                        var nRegisters = reader.ReadByte();
                        var flags      = reader.ReadUInt24();

                        //list of parameter strings
                        var paramList = reader.ReadFixedSizeListAtOffset <FunctionArgument>(() => new FunctionArgument()
                            {
                                Register  = reader.ReadInt32(),
                                Parameter = reader.ReadStringAtOffset(),
                            }, nParams);

                        parameters.Add(Value.FromString(name));
                        parameters.Add(Value.FromInteger((int)nParams));
                        parameters.Add(Value.FromInteger((int)nRegisters));
                        parameters.Add(Value.FromInteger((int)flags));
                        foreach (var param in paramList)
                        {
                            parameters.Add(Value.FromInteger(param.Register));
                            parameters.Add(Value.FromString(param.Parameter));
                        }
                        //body size of the function
                        parameters.Add(Value.FromInteger(reader.ReadInt32()));
                        //skip 8 bytes
                        reader.ReadUInt64();
                    }
                    break;

                    case InstructionType.PushData:
                    {
                        instruction = new PushData();

                        var count     = reader.ReadUInt32();
                        var constants = reader.ReadFixedSizeArrayAtOffset <uint>(() => reader.ReadUInt32(), count);

                        foreach (var constant in constants)
                        {
                            parameters.Add(Value.FromConstant(constant));
                        }
                    }
                    break;

                    case InstructionType.BranchAlways:
                    {
                        instruction = new BranchAlways();
                        var offset = reader.ReadInt32();
                        parameters.Add(Value.FromInteger(offset));
                        helper.ReportBranchOffset(offset);
                    }
                    break;

                    case InstructionType.GetURL2:
                        instruction = new GetUrl2();
                        break;

                    case InstructionType.DefineFunction:
                    {
                        instruction = new DefineFunction();
                        var name = reader.ReadStringAtOffset();
                        //list of parameter strings
                        var paramList = reader.ReadListAtOffset <string>(() => reader.ReadStringAtOffset());

                        parameters.Add(Value.FromString(name));
                        parameters.Add(Value.FromInteger(paramList.Count));
                        foreach (var param in paramList)
                        {
                            parameters.Add(Value.FromString(param));
                        }
                        //body size of the function
                        parameters.Add(Value.FromInteger(reader.ReadInt32()));
                        //skip 8 bytes
                        reader.ReadUInt64();
                    }
                    break;

                    case InstructionType.BranchIfTrue:
                    {
                        instruction = new BranchIfTrue();
                        var offset = reader.ReadInt32();
                        parameters.Add(Value.FromInteger(offset));
                        helper.ReportBranchOffset(offset);
                    }
                    break;

                    case InstructionType.GotoFrame2:
                        instruction = new GotoFrame2();
                        parameters.Add(Value.FromInteger(reader.ReadInt32()));
                        break;

                    case InstructionType.EA_PushString:
                        instruction = new PushString();
                        //the constant id that should be pushed
                        parameters.Add(Value.FromString(reader.ReadStringAtOffset()));
                        break;

                    case InstructionType.EA_PushConstantByte:
                        instruction = new PushConstantByte();
                        //the constant id that should be pushed
                        parameters.Add(Value.FromConstant(reader.ReadByte()));
                        break;

                    case InstructionType.EA_GetStringVar:
                        instruction = new GetStringVar();
                        parameters.Add(Value.FromString(reader.ReadStringAtOffset()));
                        break;

                    case InstructionType.EA_SetStringVar:
                        instruction = new SetStringVar();
                        parameters.Add(Value.FromString(reader.ReadStringAtOffset()));
                        break;

                    case InstructionType.EA_GetStringMember:
                        instruction = new GetStringMember();
                        parameters.Add(Value.FromString(reader.ReadStringAtOffset()));
                        break;

                    case InstructionType.EA_SetStringMember:
                        instruction = new SetStringMember();
                        parameters.Add(Value.FromString(reader.ReadStringAtOffset()));
                        break;

                    case InstructionType.EA_PushValueOfVar:
                        instruction = new PushValueOfVar();
                        //the constant id that should be pushed
                        parameters.Add(Value.FromConstant(reader.ReadByte()));
                        break;

                    case InstructionType.EA_GetNamedMember:
                        instruction = new GetNamedMember();
                        parameters.Add(Value.FromConstant(reader.ReadByte()));
                        break;

                    case InstructionType.EA_CallNamedFuncPop:
                        instruction = new CallNamedFuncPop();
                        parameters.Add(Value.FromConstant(reader.ReadByte()));
                        break;

                    case InstructionType.EA_CallNamedFunc:
                        instruction = new CallNamedFunc();
                        parameters.Add(Value.FromConstant(reader.ReadByte()));
                        break;

                    case InstructionType.EA_CallNamedMethodPop:
                        instruction = new CallNamedMethodPop();
                        parameters.Add(Value.FromConstant(reader.ReadByte()));
                        break;

                    case InstructionType.EA_PushFloat:
                        instruction = new PushFloat();
                        parameters.Add(Value.FromFloat(reader.ReadSingle()));
                        break;

                    case InstructionType.EA_PushByte:
                        instruction = new PushByte();
                        parameters.Add(Value.FromInteger(reader.ReadByte()));
                        break;

                    case InstructionType.EA_PushShort:
                        instruction = new PushShort();
                        parameters.Add(Value.FromInteger(reader.ReadUInt16()));
                        break;

                    case InstructionType.End:
                        instruction = new End();
                        break;

                    case InstructionType.EA_CallNamedMethod:
                        instruction = new CallNamedMethod();
                        parameters.Add(Value.FromConstant(reader.ReadByte()));
                        break;

                    case InstructionType.Var:
                        instruction = new Var();
                        break;

                    case InstructionType.EA_PushRegister:
                        instruction = new PushRegister();
                        parameters.Add(Value.FromInteger(reader.ReadByte()));
                        break;

                    case InstructionType.EA_PushConstantWord:
                        instruction = new PushConstantWord();
                        parameters.Add(Value.FromConstant(reader.ReadUInt16()));
                        break;

                    case InstructionType.EA_CallFuncPop:
                        instruction = new CallFunctionPop();
                        break;

                    case InstructionType.StrictEqual:
                        instruction = new StrictEquals();
                        break;

                    default:
                        throw new InvalidDataException("Unimplemented bytecode instruction:" + type.ToString());
                    }

                    if (instruction != null)
                    {
                        instruction.Parameters = parameters;
                        instructions.Add(instructionPosition, instruction);
                    }
                }
            }

            return(new InstructionCollection(instructions));
        }
Exemple #26
0
 /// <summary>
 /// Default ctor
 /// </summary>
 internal ConstantPoolClass(ConstantPool constantPool, int nameIndex)
     : base(constantPool)
 {
     this.nameIndex = nameIndex;
 }
Exemple #27
0
 public override void ProcessFromConstantPool(ConstantPool constantPool)
 {
     Name       = constantPool.GetEntry <Utf8Entry>(nameIndex);
     Descriptor = constantPool.GetEntry <Utf8Entry>(descriptorIndex);
 }
Exemple #28
0
 public override void PutToConstantPool(ConstantPool constantPool)
 {
 }
Exemple #29
0
 public override void PutToConstantPool(ConstantPool constantPool)
 {
     nameIndex       = constantPool.Find(Name);
     descriptorIndex = constantPool.Find(Descriptor);
 }
Exemple #30
0
 public override void Write(BinaryWriter Writer, ConstantPool Pool)
 {
     base.Write(Writer, Pool);
     Writer.WriteBE(Value);
 }
Exemple #31
0
 protected override void Read(uint Length, System.IO.BinaryReader Reader, ConstantPool Pool)
 {
     Value = Pool[Reader.ReadUInt16BE()];
 }
Exemple #32
0
 protected abstract void Read(ClassReader classReader, ConstantPool constantPool);
Exemple #33
0
 /// <summary>
 /// Default ctor
 /// </summary>
 internal ConstantPoolInterfaceMethodRef(ConstantPool constantPool, int classIndex, int nameAndTypeIndex)
     : base(constantPool, classIndex, nameAndTypeIndex)
 {
 }
 protected override void ReadExtends(ClassData classData, ConstantPool constantPool)
 {
     this.ConstantValueIndex = classData.ReadUint16();
 }
Exemple #35
0
        /// <exception cref="System.IO.IOException"/>
        public StructClass(DataInputFullStream @in, bool own, LazyLoader loader)
        {
            /*
             * class_file {
             * u4 magic;
             * u2 minor_version;
             * u2 major_version;
             * u2 constant_pool_count;
             * cp_info constant_pool[constant_pool_count-1];
             * u2 access_flags;
             * u2 this_class;
             * u2 super_class;
             * u2 interfaces_count;
             * u2 interfaces[interfaces_count];
             * u2 fields_count;
             * field_info fields[fields_count];
             * u2 methods_count;
             * method_info methods[methods_count];
             * u2 attributes_count;
             * attribute_info attributes[attributes_count];
             * }
             */
            this.own    = own;
            this.loader = loader;
            @in.Discard(4);
            minorVersion = @in.ReadUnsignedShort();
            majorVersion = @in.ReadUnsignedShort();
            pool         = new ConstantPool(@in);
            accessFlags  = @in.ReadUnsignedShort();
            int thisClassIdx  = @in.ReadUnsignedShort();
            int superClassIdx = @in.ReadUnsignedShort();

            qualifiedName = pool.GetPrimitiveConstant(thisClassIdx).GetString();
            superClass    = pool.GetPrimitiveConstant(superClassIdx);
            // interfaces
            int length = @in.ReadUnsignedShort();

            interfaces     = new int[length];
            interfaceNames = new string[length];
            for (int i = 0; i < length; i++)
            {
                interfaces[i]     = @in.ReadUnsignedShort();
                interfaceNames[i] = pool.GetPrimitiveConstant(interfaces[i]).GetString();
            }
            // fields
            length = @in.ReadUnsignedShort();
            fields = new VBStyleCollection <StructField, string>(length);
            for (int i = 0; i < length; i++)
            {
                StructField field = new StructField(@in, this);
                fields.AddWithKey(field, InterpreterUtil.MakeUniqueKey(field.GetName(), field.GetDescriptor
                                                                           ()));
            }
            // methods
            length  = @in.ReadUnsignedShort();
            methods = new VBStyleCollection <StructMethod, string>(length);
            for (int i = 0; i < length; i++)
            {
                StructMethod method = new StructMethod(@in, this);
                methods.AddWithKey(method, InterpreterUtil.MakeUniqueKey(method.GetName(), method
                                                                         .GetDescriptor()));
            }
            // attributes
            attributes = ReadAttributes(@in, pool);
            ReleaseResources();
        }
Exemple #36
0
 public abstract void ProcessFromConstantPool(ConstantPool constantPool);
Exemple #37
0
 /// <summary>
 /// Default ctor
 /// </summary>
 internal ConstantPoolFloat(ConstantPool constantPool, float value)
     : base(constantPool, value)
 {
 }
Exemple #38
0
        /// <summary>
        /// Read a list of attributes
        /// </summary>
        private void ReadAttributes(ConstantPool cp, IModifiableAttributeProvider provider)
        {
            var count = stream.ReadU2();
            for (var i = 0; i < count; i++)
            {
                var nameIndex = stream.ReadU2();
                var name = cp.GetEntry<ConstantPoolUtf8>(nameIndex).Value;
                var length = stream.ReadU4();

                Attribute attr;
                int tmp;
                switch (name)
                {
                    case CodeAttribute.AttributeName:
                        attr = ReadCodeAttribute((MethodDefinition)provider, cp);
                        break;
                    case ConstantValueAttribute.AttributeName:
                        tmp = stream.ReadU2();
                        attr = new ConstantValueAttribute(((IConstantPoolValue)cp[tmp]).Value);
                        break;
                    case ExceptionsAttribute.AttributeName:
                        attr = ReadExceptionsAttribute(cp);
                        break;
                    case InnerClassesAttribute.AttributeName:
                        attr = ReadInnerClassesAttribute(cp);
                       break;
                    case SyntheticAttribute.AttributeName:
                        attr = new SyntheticAttribute();
                        break;
                    case SourceFileAttribute.AttributeName:
                        tmp = stream.ReadU2();
                        attr = new SourceFileAttribute(cp.GetEntry<ConstantPoolUtf8>(tmp).Value);
                        break;
                    case LineNumberTableAttribute.AttributeName:
                        attr = ReadLineNumberTableAttribute();
                        break;
                    case LocalVariableTableAttribute.AttributeName:
                        attr = ReadLocalVariableTableAttribute(cp);
                        break;
                    case DeprecatedAttribute.AttributeName:
                        attr = new DeprecatedAttribute();
                        break;
                    case OverrideAttribute.AttributeName:
                        attr = new OverrideAttribute();
                        break;
                    case SignatureAttribute.AttributeName:
                        tmp = stream.ReadU2();
                        attr = new SignatureAttribute(cp.GetEntry<ConstantPoolUtf8>(tmp).Value);
                        break;
                    case RuntimeVisibleAnnotationsAttribute.AttributeName:
                        attr = new RuntimeVisibleAnnotationsAttribute(ReadAnnotationsAttribute(cp));
                        break;
                    case RuntimeInvisibleAnnotationsAttribute.AttributeName:
                        attr = new RuntimeInvisibleAnnotationsAttribute(ReadAnnotationsAttribute(cp));
                        break;
                    case RuntimeVisibleParameterAnnotationsAttribute.AttributeName:
                        attr = new RuntimeVisibleParameterAnnotationsAttribute(ReadAnnotationsAttribute(cp));
                        break;
                    case RuntimeInvisibleParameterAnnotationsAttribute.AttributeName:
                        attr = new RuntimeInvisibleParameterAnnotationsAttribute(ReadAnnotationsAttribute(cp));
                        break;
                    case AnnotationDefaultAttribute.AttributeName:
                        attr = new AnnotationDefaultAttribute(ReadElementValue(cp));
                        break;
                    default:
                        stream.Skip(length);
                        attr = new UnknownAttribute(name);
                        break;
                }
                provider.Add(attr);
            }
            provider.AttributesLoaded();
        }
Exemple #39
0
 public override void Write(BinaryWriter Writer, ConstantPool Pool)
 {
     base.Write(Writer, Pool);
     Writer.WriteBE(Pool.AddConstant(new Class(Class)));
     Writer.WriteBE(Pool.AddConstant(new NameAndType(Name, Descriptor)));
 }
Exemple #40
0
 /// <summary>
 /// Read the data of a RuntimeVisibleAnnotations or RuntimeInvisibleAnnotations attribute.
 /// </summary>
 private Annotation[] ReadAnnotationsAttribute(ConstantPool cp)
 {
     var count = stream.ReadU2();
     var result = new Annotation[count];
     for (var i = 0; i < count; i++)
     {
         result[i] = ReadAnnotation(cp);
     }
     return result;
 }
Exemple #41
0
 ConstantPool[] ReadConstantPool(BigEndianBinaryReader reader)
 {
     ushort constantPoolCount = reader.ReadUInt16();
     ConstantPool[] constantPool = new ConstantPool[constantPoolCount];
     //Console.WriteLine("Reading {0} ConstantPool items", constantPoolCount);
     // JVM Spec 4.1.
     // The value of the constant_pool_count item is equal to the number
     // of entries in the constant_pool table plus one. A constant_pool
     // index is considered valid if it is greater than zero and less
     // than constant_pool_count, with the exception for constants of
     // type long and double noted in 4.4.5.
     for (ushort i = 1; i < constantPoolCount; i++) {
         ConstantType tag = (ConstantType) reader.ReadByte();
         ConstantPool item = new ConstantPool();
         constantPool[i] = item;
         switch (tag) {
             case ConstantType.Class:
                 /*
                     CONSTANT_Class_info {
                         u1 tag;
                         u2 name_index;
                     }
                 */
                 item.ConstantType = ConstantType.Class;
                 item.NameIndex = reader.ReadUInt16();
                 break;
             case ConstantType.Fieldref:
                 /*
                     CONSTANT_Fieldref_info {
                         u1 tag;
                         u2 class_index;
                         u2 name_and_type_index;
                     }
                 */
                 item.ConstantType = ConstantType.Fieldref;
                 item.ClassIndex = reader.ReadUInt16();
                 item.NameAndTypeIndex = reader.ReadUInt16();
                 break;
             case ConstantType.Methodref:
                 /*
                     CONSTANT_Methodref_info {
                         u1 tag;
                         u2 class_index;
                         u2 name_and_type_index;
                     }
                 */
                 item.ConstantType = ConstantType.Methodref;
                 item.ClassIndex = reader.ReadUInt16();
                 item.NameAndTypeIndex = reader.ReadUInt16();
                 break;
             case ConstantType.InterfaceMethodref:
                 /*
                     CONSTANT_InterfaceMethodref_info {
                         u1 tag;
                         u2 class_index;
                         u2 name_and_type_index;
                     }
                 */
                 item.ConstantType = ConstantType.InterfaceMethodref;
                 item.ClassIndex = reader.ReadUInt16();
                 item.NameAndTypeIndex = reader.ReadUInt16();
                 break;
             case ConstantType.String:
                 /*
                     CONSTANT_String_info {
                         u1 tag;
                         u2 string_index;
                     }
                 */
                 item.ConstantType = ConstantType.String;
                 item.StringIndex = reader.ReadUInt16();
                 break;
             case ConstantType.Integer:
                 /*
                     CONSTANT_Integer_info {
                         u1 tag;
                         u4 bytes;
                     }
                 */
                 item.ConstantType = ConstantType.Integer;
                 item.Integer = reader.ReadInt32();
                 break;
             case ConstantType.Float:
                 /*
                     CONSTANT_Float_info {
                         u1 tag;
                         u4 bytes;
                     }
                 */
                 item.ConstantType = ConstantType.Float;
                 item.Float = reader.ReadSingle();
                 break;
             case ConstantType.Long:
                 /*
                     CONSTANT_Long_info {
                         u1 tag;
                         u4 high_bytes;
                         u4 low_bytes;
                     }
                 */
                 item.ConstantType = ConstantType.Long;
                 item.Long = reader.ReadInt64();
                 // JVM Spec. 4.4.5.
                 // All 8-byte constants take up two entries in the
                 // constant_pool table of the class file. If a
                 // CONSTANT_Long_info or CONSTANT_Double_info structure
                 // is the item in the constant_pool table at index n,
                 // then the next usable item in the pool is located at
                 // index n+2. The constant_pool index n+1 must be valid
                 // but is considered unusable.2
                 // 2 In retrospect, making 8-byte constants take two
                 // constant pool entries was a poor choice.
                 ++i;
                 break;
             case ConstantType.Double:
                 /*
                     CONSTANT_Double_info {
                         u1 tag;
                         u4 high_bytes;
                         u4 low_bytes;
                     }
                 */
                 item.ConstantType = ConstantType.Double;
                 item.Double = reader.ReadDouble();
                 // JVM Spec. 4.4.5.
                 // All 8-byte constants take up two entries in the
                 // constant_pool table of the class file. If a
                 // CONSTANT_Long_info or CONSTANT_Double_info structure
                 // is the item in the constant_pool table at index n,
                 // then the next usable item in the pool is located at
                 // index n+2. The constant_pool index n+1 must be valid
                 // but is considered unusable.2
                 // 2 In retrospect, making 8-byte constants take two
                 // constant pool entries was a poor choice.
                 ++i;
                 break;
             case ConstantType.NameAndType:
                 /*
                     CONSTANT_NameAndType_info {
                         u1 tag;
                         u2 name_index;
                         u2 descriptor_index;
                     }
                 */
                 item.ConstantType = ConstantType.NameAndType;
                 item.NameIndex = reader.ReadUInt16();
                 item.DescriptorIndex = reader.ReadUInt16();
                 break;
             case ConstantType.Utf8:
                 /*
                     CONSTANT_Utf8_info {
                         u1 tag;
                         u2 length;
                         u1 bytes[length];
                     }
                 */
                 item.ConstantType = ConstantType.Utf8;
                 item.String = reader.ReadString(reader.ReadUInt16());
                 break;
             default:
                 throw new ApplicationException("Wrong ConstantType: " +
                     tag);
         }
     }
     return constantPool;
 }
Exemple #42
0
        /// <summary>
        /// Read the header
        /// </summary>
        internal void ReadHeader(ClassFile cf)
        {
            var magic = stream.ReadU4();
            if (magic != ClassFile.Magic)
                throw new Dot42Exception("Invalid magic");

            cf.MinorVersion = stream.ReadU2();
            cf.MajorVersion = stream.ReadU2();

            var cpCount = stream.ReadU2();
            var cp = new ConstantPool(cpCount, cf.Loader);
            for (var i = 1; i < cpCount; i++)
            {
                var tag = (ConstantPoolTags) stream.ReadU1();
                ConstantPoolEntry entry;
                int tmp;
                switch (tag)
                {
                    case ConstantPoolTags.Class:
                        entry = new ConstantPoolClass(cp, stream.ReadU2());
                        break;
                    case ConstantPoolTags.Fieldref:
                        tmp = stream.ReadU2();
                        entry = new ConstantPoolFieldRef(cp, tmp, stream.ReadU2());
                        break;
                    case ConstantPoolTags.Methodref:
                        tmp = stream.ReadU2();
                        entry = new ConstantPoolMethodRef(cp, tmp, stream.ReadU2());
                        break;
                    case ConstantPoolTags.InterfaceMethodref:
                        tmp = stream.ReadU2();
                        entry = new ConstantPoolInterfaceMethodRef(cp, tmp, stream.ReadU2());
                        break;
                    case ConstantPoolTags.String:
                        entry = new ConstantPoolString(cp, stream.ReadU2());
                        break;
                    case ConstantPoolTags.Integer:
                        entry = new ConstantPoolInteger(cp, stream.ReadS4());
                        break;
                    case ConstantPoolTags.Float:
                        entry = new ConstantPoolFloat(cp, stream.ReadF4());
                        break;
                    case ConstantPoolTags.Long:
                        entry = new ConstantPoolLong(cp, stream.ReadS8());
                        break;
                    case ConstantPoolTags.Double:
                        entry = new ConstantPoolDouble(cp, stream.ReadF8());
                        break;
                    case ConstantPoolTags.NameAndType:
                        tmp = stream.ReadU2();
                        entry = new ConstantPoolNameAndType(cp, tmp, stream.ReadU2());
                        break;
                    case ConstantPoolTags.Utf8:
                        tmp = stream.ReadU2();
                        entry = new ConstantPoolUtf8(cp, stream.ReadUTF8(tmp));
                        break;
                    default:
                        throw new Dot42Exception("Unknown constant pool tag: " + (int)tag);
                }
                cp[i] = entry;
                if ((tag == ConstantPoolTags.Double) || (tag == ConstantPoolTags.Long))
                    i++;
            }

            cf.ClassAccessFlags = (ClassAccessFlags) stream.ReadU2();
            
            var index = stream.ReadU2();
            cf.ClassName = cp.GetEntry<ConstantPoolClass>(index).Name;
            index = stream.ReadU2();
            cf.SuperClass = (index == 0) ? null : new ObjectTypeReference(cp.GetEntry<ConstantPoolClass>(index).Name, null);

            // Interfaces
            var icount = stream.ReadU2();
            var interfaces = new string[icount];
            for (var i = 0; i < icount; i++)
            {
                index = stream.ReadU2();
                interfaces[i] = cp.GetEntry<ConstantPoolClass>(index).Name;
            }
            cf.Interfaces = interfaces.Select(x => new ObjectTypeReference(x, null)).ToArray();

            // Fields
            var fcount = stream.ReadU2();
            for (var i = 0; i < fcount; i++)
            {
                var accessFlags = (FieldAccessFlags) stream.ReadU2();
                var nameIndex = stream.ReadU2();
                var descriptorIndex = stream.ReadU2();
                var name = cp.GetEntry<ConstantPoolUtf8>(nameIndex).Value;
                var descriptor = cp.GetEntry<ConstantPoolUtf8>(descriptorIndex).Value;
                var field = new FieldDefinition(cf, accessFlags, name, descriptor, null);
                ReadAttributes(cp, field);
                cf.Fields.Add(field);
            }

            // Methods
            var mcount = stream.ReadU2();
            for (var i = 0; i < mcount; i++)
            {
                var accessFlags = (MethodAccessFlags)stream.ReadU2();
                var nameIndex = stream.ReadU2();
                var descriptorIndex = stream.ReadU2();
                var name = cp.GetEntry<ConstantPoolUtf8>(nameIndex).Value;
                var descriptor = cp.GetEntry<ConstantPoolUtf8>(descriptorIndex).Value;
                var method = new MethodDefinition(cf, accessFlags, name, descriptor, null);
                ReadAttributes(cp, method);
                cf.Methods.Add(method);
            }

            // Attributes
            ReadAttributes(cp, cf);
        }
Exemple #43
0
 public CommonAttributeReader(ClassData classData, ConstantPool constantPool)
 {
     this._classData    = classData;
     this._constantPool = constantPool;
 }
Exemple #44
0
 /// <summary>
 /// Read a Exceptions attribute
 /// </summary>
 private ExceptionsAttribute ReadExceptionsAttribute(ConstantPool cp)
 {
     var result = new ExceptionsAttribute();
     var count = stream.ReadU2();
     for (var i = 0; i < count; i++)
     {
         var index = stream.ReadU2();
         result.Exceptions.Add(cp.GetEntry<ConstantPoolClass>(index).Type);
     }
     return result;
 }
 /// <summary>
 /// Default ctor
 /// </summary>
 internal ElementValuePair(ConstantPool cp, int elementNameIndex, ElementValue value)
 {
     this.cp = cp;
     this.elementNameIndex = elementNameIndex;
     this.value            = value;
 }
Exemple #46
0
 /// <summary>
 /// Read a LocalVariableTable attribute
 /// </summary>
 private LocalVariableTableAttribute ReadLocalVariableTableAttribute(ConstantPool cp)
 {
     var result = new LocalVariableTableAttribute();
     var count = stream.ReadU2();
     for (var i = 0; i < count; i++)
     {
         var startPC = stream.ReadU2();
         var length = stream.ReadU2();
         var nameIndex = stream.ReadU2();
         var name = cp.GetEntry<ConstantPoolUtf8>(nameIndex).Value;
         var descriptorIndex = stream.ReadU2();
         var descriptor = cp.GetEntry<ConstantPoolUtf8>(descriptorIndex).Value;
         var variableType = Descriptors.ParseFieldType(descriptor);
         var index = stream.ReadU2();
         result.Variables.Add(new LocalVariable(startPC, length, name, variableType, index));
     }
     return result;
 }
 protected override void Read(uint Length, BinaryReader Reader, ConstantPool Pool)
 {
     DefaultValue = Annotation.ElementValue.Read(Reader, Pool);
 }
Exemple #48
0
 /// <summary>
 /// Default ctor
 /// </summary>
 /// <param name="cp"></param>
 /// <param name="typeIndex"></param>
 /// <param name="valuePairs"></param>
 internal Annotation(ConstantPool cp, int typeIndex, ElementValuePair[] valuePairs)
 {
     this.cp = cp;
     this.typeIndex = typeIndex;
     this.valuePairs = valuePairs;
 }
Exemple #49
0
 public override ConstantPoolInfo ReadNotPublic(ClassData classData, ConstantPool constantPool)
 {
     this.HighBytes = classData.ReadUint32();
     this.LowBytes  = classData.ReadUint32();
     return(this);
 }
 /// <summary>
 /// Default ctor
 /// </summary>
 internal ConstantPoolFieldRef(ConstantPool constantPool, int classIndex, int nameAndTypeIndex)
     : base(constantPool, classIndex, nameAndTypeIndex)
 {
 }
 /// <summary>
 /// Default ctor
 /// </summary>
 internal ConstantPoolString(ConstantPool constantPool, int stringIndex)
     : base(constantPool)
 {
     this.stringIndex = stringIndex;
 }