PackedSwitchMethodItem.java revision 7e9231a211bf00451229d88edb5c7fbd5085f73e
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 = methodDef.getLabelCache().internLabel(
63                        new LabelMethodItem(methodDef.classDef.options, baseCodeAddress + switchElement.getOffset(),
64                                "pswitch_"));
65                targets.add(new PackedSwitchLabelTarget(label));
66            }
67        } else {
68            for (SwitchElement switchElement: instruction.getSwitchElements()) {
69                if (first) {
70                    firstKey = switchElement.getKey();
71                    first = false;
72                }
73                targets.add(new PackedSwitchOffsetTarget(switchElement.getOffset()));
74            }
75        }
76        this.firstKey = firstKey;
77    }
78
79    @Override
80    public boolean writeTo(IndentingWriter writer) throws IOException {
81        writer.write(".packed-switch ");
82        IntegerRenderer.writeTo(writer, firstKey);
83        writer.indent(4);
84        writer.write('\n');
85        long key = firstKey;
86        for (PackedSwitchTarget target: targets) {
87            target.writeTargetTo(writer);
88            writeResourceId(writer, key);
89            writer.write('\n');
90            key++;
91        }
92        writer.deindent(4);
93        writer.write(".end packed-switch");
94        return true;
95    }
96
97    private static abstract class PackedSwitchTarget {
98        public abstract void writeTargetTo(IndentingWriter writer) throws IOException;
99    }
100
101    private static class PackedSwitchLabelTarget extends PackedSwitchTarget {
102        private final LabelMethodItem target;
103        public PackedSwitchLabelTarget(LabelMethodItem target) {
104            this.target = target;
105        }
106        public void writeTargetTo(IndentingWriter writer) throws IOException {
107            target.writeTo(writer);
108        }
109    }
110
111    private static class PackedSwitchOffsetTarget extends PackedSwitchTarget {
112        private final int target;
113        public PackedSwitchOffsetTarget(int target) {
114            this.target = target;
115        }
116        public void writeTargetTo(IndentingWriter writer) throws IOException {
117            if (target >= 0) {
118                writer.write('+');
119            }
120            writer.printSignedIntAsDec(target);
121        }
122    }
123}
124