baksmali.java revision 7ab77bc90be62b0688c97d4476e3bd219eace0da
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; 38 39import java.io.*; 40 41public class baksmali { 42 public static void disassembleDexFile(DexFile dexFile, String outputDirectory) 43 { 44 File outputDirectoryFile = new File(outputDirectory); 45 if (!outputDirectoryFile.exists()) { 46 if (!outputDirectoryFile.mkdirs()) { 47 System.err.println("Can't create the output directory " + outputDirectory); 48 System.exit(1); 49 } 50 } 51 52 //load and initialize the templates 53 InputStream templateStream = baksmali.class.getClassLoader().getResourceAsStream("templates/baksmali.stg"); 54 StringTemplateGroup templates = new StringTemplateGroup(new InputStreamReader(templateStream)); 55 templates.registerRenderer(Long.class, new LongRenderer()); 56 templates.registerRenderer(Integer.class, new IntegerRenderer()); 57 templates.registerRenderer(Short.class, new ShortRenderer()); 58 templates.registerRenderer(Byte.class, new ByteRenderer()); 59 templates.registerRenderer(Float.class, new FloatRenderer()); 60 templates.registerRenderer(Character.class, new CharRenderer()); 61 templates.registerRenderer(StringIdItem.class, new StringIdItemRenderer()); 62 63 64 for (ClassDefItem classDefItem: dexFile.ClassDefsSection.getItems()) { 65 /** 66 * The path for the disassembly file is based on the package name 67 * The class descriptor will look something like: 68 * Ljava/lang/Object; 69 * Where the there is leading 'L' and a trailing ';', and the parts of the 70 * package name are separated by '/' 71 */ 72 73 String classDescriptor = classDefItem.getClassType().getTypeDescriptor(); 74 75 //validate that the descriptor is formatted like we expect 76 if (classDescriptor.charAt(0) != 'L' || 77 classDescriptor.charAt(classDescriptor.length()-1) != ';') { 78 System.err.println("Unrecognized class descriptor - " + classDescriptor + " - skipping class"); 79 continue; 80 } 81 82 //trim off the leading L and trailing ; 83 classDescriptor = classDescriptor.substring(1, classDescriptor.length()-1); 84 85 //trim off the leading 'L' and trailing ';', and get the individual package elements 86 String[] pathElements = classDescriptor.split("/"); 87 88 //build the path to the smali file to generate for this class 89 StringBuilder smaliPath = new StringBuilder(outputDirectory); 90 for (String pathElement: pathElements) { 91 smaliPath.append(File.separatorChar); 92 smaliPath.append(pathElement); 93 } 94 smaliPath.append(".smali"); 95 96 File smaliFile = new File(smaliPath.toString()); 97 98 //create and initialize the top level string template 99 StringTemplate smaliFileST = templates.getInstanceOf("smaliFile"); 100 smaliFileST.setAttribute("classDef", new ClassDefinition(templates, classDefItem)); 101 102 //generate the disassembly 103 String output = smaliFileST.toString(); 104 105 //write the disassembly 106 FileWriter writer = null; 107 try 108 { 109 File smaliParent = smaliFile.getParentFile(); 110 if (!smaliParent.exists()) { 111 if (!smaliParent.mkdirs()) { 112 System.err.println("Unable to create directory " + smaliParent.toString() + " - skipping class"); 113 continue; 114 } 115 } 116 117 if (!smaliFile.exists()){ 118 if (!smaliFile.createNewFile()) { 119 System.err.println("Unable to create file " + smaliFile.toString() + " - skipping class"); 120 continue; 121 } 122 } 123 124 writer = new FileWriter(smaliFile); 125 writer.write(output); 126 } catch (Throwable ex) { 127 System.err.println("\n\nError occured while disassembling class " + classDescriptor.replace('/', '.') + " - skipping class"); 128 ex.printStackTrace(); 129 continue; 130 } 131 finally 132 { 133 if (writer != null) { 134 try { 135 writer.close(); 136 } catch (Throwable ex) { 137 System.err.println("\n\nError occured while closing file " + smaliFile.toString()); 138 ex.printStackTrace(); 139 }; 140 } 141 } 142 } 143 } 144} 145