/* * [The "BSD licence"] * Copyright (c) 2010 Ben Gruver (JesusFreke) * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package org.jf.dexlib.Code.Format; import org.jf.dexlib.Code.Instruction; import org.jf.dexlib.Code.MultiOffsetInstruction; import org.jf.dexlib.Code.Opcode; import org.jf.dexlib.DexFile; import org.jf.dexlib.Util.AnnotatedOutput; import org.jf.dexlib.Util.NumberUtils; import java.util.Iterator; public class PackedSwitchDataPseudoInstruction extends Instruction implements MultiOffsetInstruction { public static final Instruction.InstructionFactory Factory = new Factory(); private int firstKey; private int[] targets; @Override public int getSize(int codeAddress) { return getTargetCount() * 2 + 4 + (codeAddress % 2); } public PackedSwitchDataPseudoInstruction(int firstKey, int[] targets) { super(Opcode.NOP); if (targets.length > 0xFFFF) { throw new RuntimeException("The packed-switch data contains too many elements. " + "The maximum number of switch elements is 65535"); } this.firstKey = firstKey; this.targets = targets; } public PackedSwitchDataPseudoInstruction(byte[] buffer, int bufferIndex) { super(Opcode.NOP); byte opcodeByte = buffer[bufferIndex]; if (opcodeByte != 0x00) { throw new RuntimeException("Invalid opcode byte for a PackedSwitchData pseudo-instruction"); } byte subopcodeByte = buffer[bufferIndex+1]; if (subopcodeByte != 0x01) { throw new RuntimeException("Invalid sub-opcode byte for a PackedSwitchData pseudo-instruction"); } int targetCount = NumberUtils.decodeUnsignedShort(buffer, bufferIndex + 2); this.firstKey = NumberUtils.decodeInt(buffer, bufferIndex + 4); this.targets = new int[targetCount]; for (int i = 0; i iterateKeysAndTargets() { return new Iterator() { final int targetCount = getTargetCount(); int i = 0; int value = getFirstKey(); PackedSwitchTarget packedSwitchTarget = new PackedSwitchTarget(); public boolean hasNext() { return i