instructionsequencematcher.java

来自「proguard 一个java的混淆器」· Java 代码 · 共 625 行 · 第 1/2 页

JAVA
625
字号
/* * ProGuard -- shrinking, optimization, obfuscation, and preverification *             of Java bytecode. * * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu) * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package proguard.classfile.util;import proguard.classfile.*;import proguard.classfile.attribute.CodeAttribute;import proguard.classfile.constant.*;import proguard.classfile.constant.visitor.ConstantVisitor;import proguard.classfile.instruction.*;import proguard.classfile.instruction.visitor.InstructionVisitor;/** * This InstructionVisitor checks whether a given pattern instruction sequence * occurs in the instructions that are visited. The arguments of the * instruction sequence can be wildcards that are matched. * * @author Eric Lafortune */public class InstructionSequenceMatcherextends      SimplifiedVisitorimplements   InstructionVisitor,             ConstantVisitor{    /*    private static       boolean DEBUG      = false;    public  static       boolean DEBUG_MORE = false;    /*/    private static final boolean DEBUG      = false;    private static final boolean DEBUG_MORE = false;    //*/    public static final int X = 0x40000000;    public static final int Y = 0x40000001;    public static final int Z = 0x40000002;    public static final int A = 0x40000003;    public static final int B = 0x40000004;    public static final int C = 0x40000005;    public static final int D = 0x40000006;    private final Constant[]    patternConstants;    private final Instruction[] patternInstructions;    private boolean matching;    private int   patternInstructionIndex;    private final int[] matchedInstructionOffsets;    private int   matchedArgumentFlags;    private final int[] matchedArguments = new int[7];    private int   matchedConstantFlags;    private final int[] matchedConstantIndices;    // Fields acting as a parameter and a return value for visitor methods.    private Constant patternConstant;    private boolean  matchingConstant;    /**     * Creates a new InstructionSequenceMatcher.     * @param patternConstants        any constants referenced by the pattern     *                                instruction.     * @param patternInstructions     the pattern instruction sequence.     */    public InstructionSequenceMatcher(Constant[]    patternConstants,                                      Instruction[] patternInstructions)    {        this.patternConstants    = patternConstants;        this.patternInstructions = patternInstructions;        matchedInstructionOffsets = new int[patternInstructions.length];        matchedConstantIndices    = new int[patternConstants.length];    }    /**     * Starts matching from the first instruction again next time.     */    public void reset()    {        patternInstructionIndex = 0;        matchedArgumentFlags    = 0;        matchedConstantFlags    = 0;    }    public boolean isMatching()    {        return matching;    }    public int instructionCount()    {        return patternInstructions.length;    }    public int matchedInstructionOffset(int index)    {        return matchedInstructionOffsets[index];    }    public int matchedArgument(int argument)    {        int argumentIndex = argument - X;        return argumentIndex < 0 ?            argument :            matchedArguments[argumentIndex];    }    public int[] matchedArguments(int[] arguments)    {        int[] matchedArguments = new int[arguments.length];        for (int index = 0; index < arguments.length; index++)        {            matchedArguments[index] = matchedArgument(arguments[index]);        }        return matchedArguments;    }    public int matchedConstantIndex(int constantIndex)    {        int argumentIndex = constantIndex - X;        return argumentIndex < 0 ?            matchedConstantIndices[constantIndex] :            matchedArguments[argumentIndex];    }    public int matchedBranchOffset(int offset, int branchOffset)    {        int argumentIndex = branchOffset - X;        return argumentIndex < 0 ?            branchOffset :            matchedArguments[argumentIndex] - offset;    }    public int[] matchedJumpOffsets(int offset, int[] jumpOffsets)    {        int[] matchedJumpOffsets = new int[jumpOffsets.length];        for (int index = 0; index < jumpOffsets.length; index++)        {            matchedJumpOffsets[index] = matchedBranchOffset(offset,                                                            jumpOffsets[index]);        }        return matchedJumpOffsets;    }    // Implementations for InstructionVisitor.    public void visitSimpleInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, SimpleInstruction simpleInstruction)    {        Instruction patternInstruction = patternInstructions[patternInstructionIndex];        // Check if the instruction matches the next instruction in the sequence.        boolean condition =            matchingOpcodes(simpleInstruction, patternInstruction) &&            matchingArguments(simpleInstruction.constant,                              ((SimpleInstruction)patternInstruction).constant);        // Check if the instruction sequence is matching now.        checkMatch(condition,                   clazz,                   method,                   codeAttribute,                   offset,                   simpleInstruction);    }    public void visitVariableInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VariableInstruction variableInstruction)    {        Instruction patternInstruction = patternInstructions[patternInstructionIndex];        // Check if the instruction matches the next instruction in the sequence.        boolean condition =            matchingOpcodes(variableInstruction, patternInstruction) &&            matchingArguments(variableInstruction.variableIndex,                              ((VariableInstruction)patternInstruction).variableIndex) &&            matchingArguments(variableInstruction.constant,                              ((VariableInstruction)patternInstruction).constant);        // Check if the instruction sequence is matching now.        checkMatch(condition,                   clazz,                   method,                   codeAttribute,                   offset,                   variableInstruction);    }    public void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction)    {        Instruction patternInstruction = patternInstructions[patternInstructionIndex];        // Check if the instruction matches the next instruction in the sequence.        boolean condition =            matchingOpcodes(constantInstruction, patternInstruction) &&            matchingConstantIndices(clazz,                                    constantInstruction.constantIndex,                                    ((ConstantInstruction)patternInstruction).constantIndex) &&            matchingArguments(constantInstruction.constant,                              ((ConstantInstruction)patternInstruction).constant);        // Check if the instruction sequence is matching now.        checkMatch(condition,                   clazz,                   method,                   codeAttribute,                   offset,                   constantInstruction);    }    public void visitBranchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, BranchInstruction branchInstruction)    {        Instruction patternInstruction = patternInstructions[patternInstructionIndex];        // Check if the instruction matches the next instruction in the from        // sequence.        boolean condition =            matchingOpcodes(branchInstruction, patternInstruction) &&            matchingBranchOffsets(offset,                                  branchInstruction.branchOffset,                                  ((BranchInstruction)patternInstruction).branchOffset);        // Check if the instruction sequence is matching now.        checkMatch(condition,                   clazz,                   method,                   codeAttribute,                   offset,                   branchInstruction);    }    public void visitTableSwitchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, TableSwitchInstruction tableSwitchInstruction)    {        Instruction patternInstruction = patternInstructions[patternInstructionIndex];        // Check if the instruction matches the next instruction in the sequence.        boolean condition =            matchingOpcodes(tableSwitchInstruction, patternInstruction) &&            matchingBranchOffsets(offset,                                  tableSwitchInstruction.defaultOffset,                                  ((TableSwitchInstruction)patternInstruction).defaultOffset) &&            matchingArguments(tableSwitchInstruction.lowCase,                              ((TableSwitchInstruction)patternInstruction).lowCase)  &&            matchingArguments(tableSwitchInstruction.highCase,                              ((TableSwitchInstruction)patternInstruction).highCase) &&            matchingJumpOffsets(offset,                                tableSwitchInstruction.jumpOffsets,                                ((TableSwitchInstruction)patternInstruction).jumpOffsets);        // Check if the instruction sequence is matching now.        checkMatch(condition,                   clazz,                   method,                   codeAttribute,                   offset,                   tableSwitchInstruction);    }    public void visitLookUpSwitchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, LookUpSwitchInstruction lookUpSwitchInstruction)    {        Instruction patternInstruction = patternInstructions[patternInstructionIndex];        // Check if the instruction matches the next instruction in the sequence.        boolean condition =            matchingOpcodes(lookUpSwitchInstruction, patternInstruction) &&            matchingBranchOffsets(offset,                                  lookUpSwitchInstruction.defaultOffset,                                  ((LookUpSwitchInstruction)patternInstruction).defaultOffset) &&            matchingArguments(lookUpSwitchInstruction.cases,                              ((LookUpSwitchInstruction)patternInstruction).cases) &&            matchingJumpOffsets(offset,                                lookUpSwitchInstruction.jumpOffsets,                                ((LookUpSwitchInstruction)patternInstruction).jumpOffsets);        // Check if the instruction sequence is matching now.        checkMatch(condition,                   clazz,                   method,                   codeAttribute,

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?