Exemplo n.º 1
0
    def parse(fh):

        assert fh is not None

        nameIndex = ByteToDec(fh.read(2))
        descriptorIndex = ByteToDec(fh.read(2))

        return ConstantNameAndTypeInfo(nameIndex, descriptorIndex)
Exemplo n.º 2
0
    def parse(fh):

        assert fh is not None

        classInfoIndex = ByteToDec(fh.read(2))
        nameAndTypeIndex = ByteToDec(fh.read(2))

        return ConstantInterfacerefInfo(classInfoIndex, nameAndTypeIndex)
Exemplo n.º 3
0
    def __parseVersion(self):
        """
        Parse the version of this class file.
        Version is composed by minor version and major version.
        Each will occupy 2 bytes.
        """

        assert self.clsFile is not None

        majorVerBytes = self.clsFile.read(2)
        minorVerBytes = self.clsFile.read(2)

        self.result.setVersion(ByteToDec(minorVerBytes), ByteToDec(majorVerBytes))
Exemplo n.º 4
0
    def parse(clsFile, result):

        attribute = LineNumberTableAttribute()

        tableLen = ByteToDec(clsFile.read(2))

        for i in range(tableLen):
            startPC = ByteToDec(clsFile.read(2))
            lineNumber = ByteToDec(clsFile.read(2))

            attribute.addCode2Line(startPC, lineNumber)

        return attribute
Exemplo n.º 5
0
    def __parseInterface(self):
        """
        Parse "interface" section
        """

        assert self.clsFile is not None

        interfaceCount = ByteToDec(self.clsFile.read(2))

        interfaceIndex = []
        if interfaceCount > 0:
            for i in range(interfaceCount):
                interfaceIndex.append(ByteToDec(self.clsFile.read(2)))

        self.result.setInterfaces(interfaceCount, interfaceIndex)
Exemplo n.º 6
0
    def parse(count, fh):
        constants = []

        skipped = False

        for index in range(count):

            if skipped:
                skipped = False
                constants.append(ConstantPlaceHolder())
                continue

            type = ByteToDec(fh.read(1))

            handler = ConstantPool.getHandler(type)
            if handler is not None:
                attribute = handler(fh)
                constants.append(attribute)
            else:
                readBytes = ConstantPool.getContentBytes(type)
                if readBytes:
                    fh.read(readBytes)
                    constants.append(None)

            if type == CONSTANT_LONG_INFO or type == CONSTANT_DOUBLE_INFO:
                skipped = True

        return constants
Exemplo n.º 7
0
    def __parseMethods(self):
        """
        Parse methods section.
        """

        assert  self.clsFile is not None

        methodCount = ByteToDec(self.clsFile.read(2))

        methods = []
        for index in range(methodCount):
            accessFlag = ByteToDec(self.clsFile.read(2))
            name = self.result.getUtf8(ByteToDec(self.clsFile.read(2)))
            descriptor = self.result.getUtf8(ByteToDec(self.clsFile.read(2)))

            method = Method(name, descriptor, accessFlag)

            attrCount = ByteToDec(self.clsFile.read(2))
            if attrCount > 0:
                for i in range(attrCount):
                    attributeName = self.result.getUtf8(ByteToDec(self.clsFile.read(2)))
                    attributeLength = ByteToDec(self.clsFile.read(4))

                    # for now, only parse the "Code Attribute
                    parser = Attribute.getParser(attributeName)

                    if parser is not None:
                        attribute = parser(self.clsFile, self.result)
                        method.addAttribute(attribute)
                    else:
                        self.clsFile.read(attributeLength)

            methods.append(method)

        self.result.setMethods(methodCount, methods)
Exemplo n.º 8
0
    def parse(clsFile, result):

        attribute = ConstantValueAttribute()

        classIndex = ByteToDec(clsFile.read(2))

        attribute.setValueIndex(classIndex)

        return attribute
Exemplo n.º 9
0
    def __parseAttribute(self):
        """
        Parse the attributes of class file.
        """

        attrCount = ByteToDec(self.clsFile.read(2))
        if attrCount > 0:
            for i in range(attrCount):
                attributeName = self.result.getUtf8(ByteToDec(self.clsFile.read(2)))
                attributeLength = ByteToDec(self.clsFile.read(4))

                parser = Attribute.getParser(attributeName)

                if parser is not None:
                    attribute = parser(self.clsFile, self.result)
                    self.result.addAttribute(attribute)
                else:
                    self.clsFile.read(attributeLength)
Exemplo n.º 10
0
    def parse(clsFile, result):

        attribute = SourceFileAttribute()

        sourceFileIndex = ByteToDec(clsFile.read(2))

        attribute.setSourceFile(result.getUtf8(sourceFileIndex))

        return attribute
Exemplo n.º 11
0
    def __parseSuper(self):
        """
        Parse "super" section
        """

        assert self.clsFile is not None

        superIndex = ByteToDec(self.clsFile.read(2))
        self.result.setSuperIndex(superIndex)
Exemplo n.º 12
0
    def __parseThis(self):
        """
        Parse "this" section
        """

        assert self.clsFile is not None

        thisIndex = ByteToDec(self.clsFile.read(2))
        self.result.setThisIndex(thisIndex)
Exemplo n.º 13
0
    def __parseAccessFlag(self):
        """
        Parse the access flag
        """

        assert self.clsFile is not None

        accessFlag = ByteToDec(self.clsFile.read(2))
        self.result.setAccessFlag(accessFlag)
Exemplo n.º 14
0
    def parse(fh):  # fh stands for file handler
        """
        Constant class section has 3 bytes.
        """

        assert fh is not None

        nameIndex = ByteToDec(fh.read(2))

        return ConstantClassInfo(nameIndex)
Exemplo n.º 15
0
    def parse(fh):
        """
        Constant UTF8 section
        """

        assert fh is not None

        length = ByteToDec(fh.read(2))
        content = fh.read(length)

        return ConstantUTF8Info(length, content)
Exemplo n.º 16
0
    def parse(clsFile, result):

        attribute = InnerClassesAttribute()

        numberOfClasses = ByteToDec(clsFile.read(2))

        if numberOfClasses > 0:

            for i in range(numberOfClasses):

                innerClassIndex = ByteToDec(clsFile.read(2))
                outerClassIndex = ByteToDec(clsFile.read(2))
                innerNameIndex = ByteToDec(clsFile.read(2))
                innerClassAccessFlags = ByteToDec(clsFile.read(2))

                innerClassInfo = InnerClassesAttribute.InnerClassesInfo(
                    innerClassIndex, outerClassIndex, innerNameIndex,
                    innerClassAccessFlags)

                attribute.addInnerClass(innerClassInfo)

        return attribute
Exemplo n.º 17
0
    def __parseConstantPool(self):
        """
        Parse the constant pool of class file
        First 2 bytes of this section is the count.
        Pay attention here. Constant index starts from 1, not 0.
        """

        assert self.clsFile is not None

        const_count = ByteToDec(self.clsFile.read(2)) - 1
        self.result.setConstPoolCount(const_count)

        constants = ConstantPool.parse(const_count, self.clsFile)
        self.result.setConstants(constants)
Exemplo n.º 18
0
    def parse(clsFile, result):

        attribute = LocalVariableTableAttribute()

        variableCount = ByteToDec(clsFile.read(2))

        for i in range(variableCount):
            startPC = ByteToDec(clsFile.read(2))
            length = ByteToDec(clsFile.read(2))
            argName = result.getUtf8(ByteToDec(clsFile.read(2)))
            descriptorName = result.getUtf8(ByteToDec(clsFile.read(2)))
            index = ByteToDec(clsFile.read(2))

            info = LocalVariableTableAttribute.LocalVarInfo(
                startPC, length, argName, descriptorName, index)

            attribute.addVarInfo(info)

        return attribute
Exemplo n.º 19
0
    def parse(clsFile, result):
        codeAttr = CodeAttribute()

        maxStack = ByteToDec(clsFile.read(2))
        maxLocal = ByteToDec(clsFile.read(2))

        codeLength = ByteToDec(clsFile.read(4))
        code = ByteToHex(clsFile.read(codeLength))

        codeAttr.setMaxStack(maxStack)
        codeAttr.setMaxLocal(maxLocal)
        codeAttr.setCode(code)

        exceptionLen = ByteToDec(clsFile.read(2))
        if exceptionLen > 0:
            for i in range(exceptionLen):
                startPC = ByteToDec(clsFile.read(2))
                endPC = ByteToDec(clsFile.read(2))
                handlerPC = ByteToDec(clsFile.read(2))
                classIndex = ByteToDec(clsFile.read(2))

                exceptionClassName = "any"
                if classIndex > 0:  # if class index is 0, the class name is any.
                    exceptionClassName = result.getClassName(classIndex)

                exceptionInfo = ExceptionInfo(startPC, endPC, handlerPC,
                                              exceptionClassName)

                codeAttr.addException(exceptionInfo)

        subAttrCount = ByteToDec(clsFile.read(2))
        if subAttrCount > 0:
            for i in range(subAttrCount):
                subAttr = result.getUtf8(ByteToDec(clsFile.read(2)))
                subAttrLength = ByteToDec(clsFile.read(4))

                parser = Attribute.getParser(subAttr)
                if parser is not None:
                    attribute = parser(clsFile, result)
                    codeAttr.addAttribute(attribute)
                else:
                    clsFile.read(subAttrLength)

        return codeAttr
Exemplo n.º 20
0
    def parse(fh):

        assert fh is not None

        return ConstantStringInfo(ByteToDec(fh.read(2)))
Exemplo n.º 21
0
    def parse(fh):

        assert fh is not None

        return ConstantIntegerInfo(ByteToDec(fh.read(4)))