baksmali.java revision 5b89857df3124851183c75a134f10b6ba41512a9
1/* 2 * [The "BSD licence"] 3 * Copyright (c) 2009 Ben Gruver 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29package org.jf.baksmali; 30 31import org.antlr.stringtemplate.StringTemplate; 32import org.antlr.stringtemplate.StringTemplateGroup; 33import org.jf.baksmali.Adaptors.ClassDefinition; 34import org.jf.baksmali.Renderers.*; 35import org.jf.dexlib.DexFile; 36import org.jf.dexlib.ClassDefItem; 37import org.jf.dexlib.StringIdItem; 38import org.jf.dexlib.Util.Deodexerant; 39import org.jf.dexlib.Util.DeodexUtil; 40 41import java.io.*; 42 43public class baksmali { 44 public static boolean noParameterRegisters = false; 45 public static DeodexUtil deodexUtil = null; 46 47 public static void disassembleDexFile(DexFile dexFile, Deodexerant deodexerant, String outputDirectory, 48 boolean noParameterRegisters) 49 { 50 baksmali.noParameterRegisters = noParameterRegisters; 51 if (deodexerant != null) { 52 baksmali.deodexUtil = new DeodexUtil(deodexerant); 53 } 54 55 File outputDirectoryFile = new File(outputDirectory); 56 if (!outputDirectoryFile.exists()) { 57 if (!outputDirectoryFile.mkdirs()) { 58 System.err.println("Can't create the output directory " + outputDirectory); 59 System.exit(1); 60 } 61 } 62 63 //load and initialize the templates 64 InputStream templateStream = baksmali.class.getClassLoader().getResourceAsStream("templates/baksmali.stg"); 65 StringTemplateGroup templates = new StringTemplateGroup(new InputStreamReader(templateStream)); 66 templates.registerRenderer(Long.class, new LongRenderer()); 67 templates.registerRenderer(Integer.class, new IntegerRenderer()); 68 templates.registerRenderer(Short.class, new ShortRenderer()); 69 templates.registerRenderer(Byte.class, new ByteRenderer()); 70 templates.registerRenderer(Float.class, new FloatRenderer()); 71 templates.registerRenderer(Character.class, new CharRenderer()); 72 templates.registerRenderer(StringIdItem.class, new StringIdItemRenderer()); 73 74 75 for (ClassDefItem classDefItem: dexFile.ClassDefsSection.getItems()) { 76 /** 77 * The path for the disassembly file is based on the package name 78 * The class descriptor will look something like: 79 * Ljava/lang/Object; 80 * Where the there is leading 'L' and a trailing ';', and the parts of the 81 * package name are separated by '/' 82 */ 83 84 String classDescriptor = classDefItem.getClassType().getTypeDescriptor(); 85 86 //TODO: GROT 87 System.out.println(classDescriptor); 88 89 //validate that the descriptor is formatted like we expect 90 if (classDescriptor.charAt(0) != 'L' || 91 classDescriptor.charAt(classDescriptor.length()-1) != ';') { 92 System.err.println("Unrecognized class descriptor - " + classDescriptor + " - skipping class"); 93 continue; 94 } 95 96 //trim off the leading L and trailing ; 97 classDescriptor = classDescriptor.substring(1, classDescriptor.length()-1); 98 99 //trim off the leading 'L' and trailing ';', and get the individual package elements 100 String[] pathElements = classDescriptor.split("/"); 101 102 //build the path to the smali file to generate for this class 103 StringBuilder smaliPath = new StringBuilder(outputDirectory); 104 for (String pathElement: pathElements) { 105 smaliPath.append(File.separatorChar); 106 smaliPath.append(pathElement); 107 } 108 smaliPath.append(".smali"); 109 110 File smaliFile = new File(smaliPath.toString()); 111 112 //create and initialize the top level string template 113 ClassDefinition classDefinition = new ClassDefinition(templates, classDefItem); 114 115 StringTemplate smaliFileST = classDefinition.makeTemplate(); 116 117 //generate the disassembly 118 String output = smaliFileST.toString(); 119 120 //write the disassembly 121 FileWriter writer = null; 122 try 123 { 124 File smaliParent = smaliFile.getParentFile(); 125 if (!smaliParent.exists()) { 126 if (!smaliParent.mkdirs()) { 127 System.err.println("Unable to create directory " + smaliParent.toString() + " - skipping class"); 128 continue; 129 } 130 } 131 132 if (!smaliFile.exists()){ 133 if (!smaliFile.createNewFile()) { 134 System.err.println("Unable to create file " + smaliFile.toString() + " - skipping class"); 135 continue; 136 } 137 } 138 139 writer = new FileWriter(smaliFile); 140 writer.write(output); 141 } catch (Throwable ex) { 142 System.err.println("\n\nError occured while disassembling class " + classDescriptor.replace('/', '.') + " - skipping class"); 143 ex.printStackTrace(); 144 } 145 finally 146 { 147 if (writer != null) { 148 try { 149 writer.close(); 150 } catch (Throwable ex) { 151 System.err.println("\n\nError occured while closing file " + smaliFile.toString()); 152 ex.printStackTrace(); 153 } 154 } 155 } 156 } 157 } 158} 159