Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // maybe I have been bad and haven't completely tested everything
- public class AsmHelper {
- public static void main(String[] args) {}
- public static void printPatternAndMask(AbstractInsnNode start) {
- StringBuilder pattern = new StringBuilder(), mask = new StringBuilder();
- AbstractInsnNode next = start;
- do {
- pattern.append("0x");
- if(next.getOpcode() != F_NEW) {
- mask.append("x");
- pattern.append(Integer.toHexString(next.getOpcode()));
- } else {
- mask.append("?");
- pattern.append("00");
- }
- pattern.append("\\");
- next = next.getNext();
- } while(next != null);
- System.out.printf("\nPattern: " + pattern.toString() + "\n");
- System.out.printf("\nMask: " + mask.toString() + "\n");
- }
- public static int[] convertPattern(String pattern) {
- if(pattern.startsWith("\0"))
- pattern = pattern.substring(1);
- String[] hex = pattern.split("\0");
- int[] buff = new int[hex.length];
- int index = 0;
- for(String number : hex) {
- if(number.startsWith("0x"))
- number = number.substring(2);
- else if(number.startsWith("x"))
- number = number.substring(1);
- buff[index++] = Integer.parseInt(number, 16);
- }
- return buff;
- }
- public static AbstractInsnNode findPattern(AbstractInsnNode start, String pattern, String mask) {
- return findPattern(start,
- convertPattern(pattern),
- mask.toCharArray());
- }
- public static AbstractInsnNode findPattern(AbstractInsnNode start, int[] pattern, char[] mask) {
- if(start != null &&
- pattern.length == mask.length) {
- int found = 0;
- AbstractInsnNode next = start;
- do {
- switch(mask[found]) {
- // Analyze this node
- case 'x': {
- // Check if node and pattern have same opcode
- if(next.getOpcode() == pattern[found]) {
- // Increment number of matched opcodes
- found++;
- } else {
- // Go back to the next node of the first matching node
- for(int i = 1; i <= (found - 1); i++)
- next = next.getPrevious();
- // Reset the number of opcodes found
- found = 0;
- }
- break;
- }
- // Skips over this node
- default:
- case '?':
- found++;
- break;
- }
- // Check if found entire pattern
- if(found >= mask.length) {
- // Go back to top node
- for(int i = 1; i <= (found - 1); i++)
- next = next.getPrevious();
- return next;
- }
- next = next.getNext();
- } while(next != null &&
- found < mask.length);
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment