Home | History | Annotate | Download | only in Format
      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 
     29 package org.jf.baksmali.Adaptors.Format;
     30 
     31 import org.jf.baksmali.Adaptors.CommentingIndentingWriter;
     32 import org.jf.baksmali.Adaptors.LabelMethodItem;
     33 import org.jf.baksmali.Adaptors.MethodDefinition;
     34 import org.jf.dexlib2.iface.instruction.SwitchElement;
     35 import org.jf.dexlib2.iface.instruction.formats.PackedSwitchPayload;
     36 import org.jf.util.IndentingWriter;
     37 import org.jf.baksmali.Renderers.IntegerRenderer;
     38 
     39 import java.io.IOException;
     40 import java.util.ArrayList;
     41 import java.util.List;
     42 
     43 public class PackedSwitchMethodItem extends InstructionMethodItem<PackedSwitchPayload> {
     44     private final List<PackedSwitchTarget> targets;
     45     private final int firstKey;
     46 
     47     // Whether this sparse switch instruction should be commented out because it is never referenced
     48     private boolean commentedOut;
     49 
     50     public PackedSwitchMethodItem(MethodDefinition methodDef, int codeAddress, PackedSwitchPayload instruction) {
     51         super(methodDef, codeAddress, instruction);
     52 
     53         int baseCodeAddress = methodDef.getPackedSwitchBaseAddress(codeAddress);
     54 
     55         targets = new ArrayList<PackedSwitchTarget>();
     56 
     57         boolean first = true;
     58         int firstKey = 0;
     59         if (baseCodeAddress >= 0) {
     60             for (SwitchElement switchElement: instruction.getSwitchElements()) {
     61                 if (first) {
     62                     firstKey = switchElement.getKey();
     63                     first = false;
     64                 }
     65                 LabelMethodItem label = methodDef.getLabelCache().internLabel(
     66                         new LabelMethodItem(methodDef.classDef.options, baseCodeAddress + switchElement.getOffset(),
     67                                 "pswitch_"));
     68                 targets.add(new PackedSwitchLabelTarget(label));
     69             }
     70         } else {
     71             commentedOut = true;
     72             for (SwitchElement switchElement: instruction.getSwitchElements()) {
     73                 if (first) {
     74                     firstKey = switchElement.getKey();
     75                     first = false;
     76                 }
     77                 targets.add(new PackedSwitchOffsetTarget(switchElement.getOffset()));
     78             }
     79         }
     80         this.firstKey = firstKey;
     81     }
     82 
     83     @Override
     84     public boolean writeTo(IndentingWriter writer) throws IOException {
     85         if (commentedOut) {
     86             writer = new CommentingIndentingWriter(writer);
     87         }
     88         writer.write(".packed-switch ");
     89         IntegerRenderer.writeTo(writer, firstKey);
     90         writer.indent(4);
     91         writer.write('\n');
     92         int key = firstKey;
     93         for (PackedSwitchTarget target: targets) {
     94             target.writeTargetTo(writer);
     95             writeCommentIfResourceId(writer, key);
     96             writer.write('\n');
     97             key++;
     98         }
     99         writer.deindent(4);
    100         writer.write(".end packed-switch");
    101         return true;
    102     }
    103 
    104     private static abstract class PackedSwitchTarget {
    105         public abstract void writeTargetTo(IndentingWriter writer) throws IOException;
    106     }
    107 
    108     private static class PackedSwitchLabelTarget extends PackedSwitchTarget {
    109         private final LabelMethodItem target;
    110         public PackedSwitchLabelTarget(LabelMethodItem target) {
    111             this.target = target;
    112         }
    113         public void writeTargetTo(IndentingWriter writer) throws IOException {
    114             target.writeTo(writer);
    115         }
    116     }
    117 
    118     private static class PackedSwitchOffsetTarget extends PackedSwitchTarget {
    119         private final int target;
    120         public PackedSwitchOffsetTarget(int target) {
    121             this.target = target;
    122         }
    123         public void writeTargetTo(IndentingWriter writer) throws IOException {
    124             if (target >= 0) {
    125                 writer.write('+');
    126             }
    127             writer.printSignedIntAsDec(target);
    128         }
    129     }
    130 }
    131