package com.badoo.hprof.unobfuscator;

import com.badoo.hprof.library.HprofReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import proguard.obfuscate.MappingProcessor;
import proguard.obfuscate.MappingReader;

/**
 * Created by Erik Andre on 13/08/2014.
 */
public class HprofUnobfuscator implements MappingProcessor {

    public HprofUnobfuscator(String mappingFile, String hprofFile, String outFile) {
        MappingReader mappingReader = new MappingReader(new File(mappingFile));
        try {
            mappingReader.pump(this);
            UnobfuscatingProcessor unobfuscatingProcessor = new UnobfuscatingProcessor(new FileOutputStream(outFile));
            HprofReader hprofReader = new HprofReader(new FileInputStream(hprofFile), unobfuscatingProcessor);
            while (hprofReader.hasNext()) {
                hprofReader.next();
            }
        }
        catch (IOException e) {
            System.err.println("Failed to convert hprof file: " + e.getMessage());
            e.printStackTrace();
        }
    }

    public static void main(String args[]) {
        new HprofUnobfuscator("/Users/erikandre/temp/hprof/mapping.txt", "/Users/erikandre/temp/hprof/obfuscated.hprof", "/Users/erikandre/temp/hprof/out.hprof");
    }

    @Override
    public boolean processClassMapping(String className, String newClassName) {
//        System.out.println("Class mapping " + className + "->" + newClassName);
        return true;
    }

    @Override
    public void processFieldMapping(String className, String fieldType, String fieldName, String newFieldName) {
//        System.out.println("Field mapping " + className + "." + fieldName + "->" + newFieldName);
    }

    @Override
    public void processMethodMapping(String className, int firstLineNumber, int lastLineNumber, String methodReturnType, String methodName, String methodArguments, String newMethodName) {
//        System.out.println("Method mapping " + className + "." + methodName + "->" + newMethodName);
    }

}

@@ -75,16 +75,70 @@
 fReader;
+%0Aimport com.badoo.hprof.library.model.ClassDefinition;
 %0A%0Aimport
@@ -240,24 +240,72 @@
 IOException;
+%0Aimport java.util.HashMap;%0Aimport java.util.Map;
 %0A%0Aimport pro
@@ -490,16 +490,99 @@
 ssor %7B%0A%0A
+    private Map%3CString, String%3E classNameMapping = new HashMap%3CString, String%3E();%0A%0A
     publ
@@ -1106,32 +1106,406 @@
 ;%0A            %7D%0A
+            for (ClassDefinition cls : unobfuscatingProcessor.getClasses().values()) %7B%0A                String name = unobfuscatingProcessor.getStrings().get(cls.getNameStringId());%0A                if (classNameMapping.containsKey(name)) %7B%0A                    System.out.println(%22Unobfuscating %22 + name + %22 to %22 + classNameMapping.get(name));%0A                %7D%0A            %7D%0A
         %7D%0A      
@@ -1968,34 +1968,32 @@
 lassName) %7B%0A
-//
         
 System.out.p
@@ -1984,69 +1984,44 @@
     
-System.out.println(%22Class mapping %22 + className + %22-%3E%22 + newC
+classNameMapping.put(newClassName, c
 lass

Find matching unobfuscated class names