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.dexlib.Code.Format; 30 31 import org.jf.dexlib.Code.Instruction; 32 import org.jf.dexlib.Code.OffsetInstruction; 33 import org.jf.dexlib.Code.Opcode; 34 import org.jf.dexlib.Code.SingleRegisterInstruction; 35 import org.jf.dexlib.DexFile; 36 import org.jf.dexlib.Util.AnnotatedOutput; 37 import org.jf.dexlib.Util.NumberUtils; 38 39 public class Instruction21t extends OffsetInstruction implements SingleRegisterInstruction { 40 public static final Instruction.InstructionFactory Factory = new Factory(); 41 private byte regA; 42 private short targetAddressOffset; 43 44 public Instruction21t(Opcode opcode, short regA, short offB) { 45 super(opcode); 46 47 if (regA >= 1 << 8) { 48 throw new RuntimeException("The register number must be less than v256"); 49 } 50 51 if (offB == 0) { 52 throw new RuntimeException("The address offset cannot be 0."); 53 } 54 55 this.regA = (byte)regA; 56 this.targetAddressOffset = offB; 57 } 58 59 private Instruction21t(Opcode opcode, byte[] buffer, int bufferIndex) { 60 super(opcode); 61 62 assert buffer[bufferIndex] == opcode.value; 63 64 regA = buffer[bufferIndex + 1]; 65 targetAddressOffset = NumberUtils.decodeShort(buffer, bufferIndex + 2); 66 assert targetAddressOffset != 0; 67 } 68 69 protected void writeInstruction(AnnotatedOutput out, int currentCodeAddress) { 70 out.writeByte(opcode.value); 71 out.writeByte(regA); 72 out.writeShort(targetAddressOffset); 73 } 74 75 public void updateTargetAddressOffset(int targetAddressOffset) { 76 if (targetAddressOffset < Short.MIN_VALUE || targetAddressOffset > Short.MAX_VALUE) { 77 throw new RuntimeException("The address offset " + targetAddressOffset + 78 " is out of range. It must be in [-32768, 32767]"); 79 } 80 if (targetAddressOffset == 0) { 81 throw new RuntimeException("The address offset cannot be 0"); 82 } 83 this.targetAddressOffset = (short) targetAddressOffset; 84 } 85 86 public Format getFormat() { 87 return Format.Format21t; 88 } 89 90 public int getRegisterA() { 91 return regA & 0xFF; 92 } 93 94 public int getTargetAddressOffset() { 95 return targetAddressOffset; 96 } 97 98 private static class Factory implements Instruction.InstructionFactory { 99 public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] buffer, int bufferIndex) { 100 return new Instruction21t(opcode, buffer, bufferIndex); 101 } 102 } 103 } 104