PackedSwitchMethodItem.java revision 754b3c4dc009b7a02e39001560c3f0fd6a7cc2c0
1/* 2 * [The "BSD licence"] 3 * Copyright (c) 2010 Ben Gruver (JesusFreke) 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.Adaptors.Format; 30 31import org.jf.baksmali.Adaptors.LabelMethodItem; 32import org.jf.baksmali.Adaptors.MethodDefinition; 33import org.jf.dexlib2.iface.instruction.SwitchElement; 34import org.jf.dexlib2.iface.instruction.formats.PackedSwitchPayload; 35import org.jf.util.IndentingWriter; 36import org.jf.baksmali.Renderers.IntegerRenderer; 37 38import java.io.IOException; 39import java.util.ArrayList; 40import java.util.List; 41 42public class PackedSwitchMethodItem extends InstructionMethodItem<PackedSwitchPayload> { 43 private final List<PackedSwitchTarget> targets; 44 private final int firstKey; 45 46 public PackedSwitchMethodItem(MethodDefinition methodDef, int codeAddress, PackedSwitchPayload instruction) { 47 super(methodDef, codeAddress, instruction); 48 49 int baseCodeAddress = methodDef.getPackedSwitchBaseAddress(codeAddress); 50 51 targets = new ArrayList<PackedSwitchTarget>(); 52 53 boolean first = true; 54 //TODO: does dalvik allow switc payloads with no cases? 55 int firstKey = 0; 56 if (baseCodeAddress >= 0) { 57 for (SwitchElement switchElement: instruction.getSwitchElements()) { 58 if (first) { 59 firstKey = switchElement.getKey(); 60 first = false; 61 } 62 LabelMethodItem label = new LabelMethodItem(baseCodeAddress + switchElement.getOffset(), "pswitch_"); 63 targets.add(new PackedSwitchLabelTarget(label)); 64 } 65 } else { 66 for (SwitchElement switchElement: instruction.getSwitchElements()) { 67 if (first) { 68 firstKey = switchElement.getKey(); 69 first = false; 70 } 71 targets.add(new PackedSwitchOffsetTarget(switchElement.getOffset())); 72 } 73 } 74 this.firstKey = firstKey; 75 } 76 77 @Override 78 public boolean writeTo(IndentingWriter writer) throws IOException { 79 writer.write(".packed-switch "); 80 IntegerRenderer.writeTo(writer, firstKey); 81 writer.indent(4); 82 writer.write('\n'); 83 for (PackedSwitchTarget target: targets) { 84 target.writeTargetTo(writer); 85 writer.write('\n'); 86 } 87 writer.deindent(4); 88 writer.write(".end packed-switch"); 89 return true; 90 } 91 92 private static abstract class PackedSwitchTarget { 93 public abstract void writeTargetTo(IndentingWriter writer) throws IOException; 94 } 95 96 private static class PackedSwitchLabelTarget extends PackedSwitchTarget { 97 private final LabelMethodItem target; 98 public PackedSwitchLabelTarget(LabelMethodItem target) { 99 this.target = target; 100 } 101 public void writeTargetTo(IndentingWriter writer) throws IOException { 102 target.writeTo(writer); 103 } 104 } 105 106 private static class PackedSwitchOffsetTarget extends PackedSwitchTarget { 107 private final int target; 108 public PackedSwitchOffsetTarget(int target) { 109 this.target = target; 110 } 111 public void writeTargetTo(IndentingWriter writer) throws IOException { 112 if (target >= 0) { 113 writer.write('+'); 114 } 115 writer.printSignedIntAsDec(target); 116 } 117 } 118} 119